diff --git a/core/src/mindustry/ai/BlockIndexer.java b/core/src/mindustry/ai/BlockIndexer.java index 81c7a8457d..e5cf94e462 100644 --- a/core/src/mindustry/ai/BlockIndexer.java +++ b/core/src/mindustry/ai/BlockIndexer.java @@ -70,8 +70,8 @@ public class BlockIndexer{ for(Team team : Team.all){ var data = state.teams.get(team); if(data != null){ - if(data.buildings != null) data.buildings.clear(); - if(data.turrets != null) data.turrets.clear(); + if(data.buildingTree != null) data.buildingTree.clear(); + if(data.turretTree != null) data.turretTree.clear(); } } @@ -113,17 +113,20 @@ public class BlockIndexer{ } } + //no longer part of the building list + data.buildings.remove(tile.build); + //update the unit cap when building is removed data.unitCap -= tile.block().unitCapModifier; //unregister building from building quadtree - if(data.buildings != null){ - data.buildings.remove(build); + if(data.buildingTree != null){ + data.buildingTree.remove(build); } //remove indexed turret - if(data.turrets != null && build.block.attacks){ - data.turrets.remove(build); + if(data.turretTree != null && build.block.attacks){ + data.turretTree.remove(build); } //is no longer registered @@ -230,7 +233,7 @@ public class BlockIndexer{ }else{ breturnArray.clear(); - var buildings = team.data().buildings; + var buildings = team.data().buildingTree; if(buildings == null) return false; buildings.intersect(wx - range, wy - range, range*2f, range*2f, b -> { if(b.within(wx, wy, range + b.hitSize() / 2f) && pred.get(b)){ @@ -256,7 +259,7 @@ public class BlockIndexer{ breturnArray.clear(); - var buildings = team.data().buildings; + var buildings = team.data().buildingTree; if(buildings == null) return false; buildings.intersect(rect, b -> { if(pred.get(b)){ @@ -324,7 +327,7 @@ public class BlockIndexer{ breturnArray.clear(); for(int i = 0; i < activeTeams.size; i++){ Team team = activeTeams.items[i]; - var buildings = team.data().buildings; + var buildings = team.data().buildingTree; if(buildings == null) continue; buildings.intersect(x - range, y - range, range*2f, range*2f, breturnArray); } @@ -374,7 +377,7 @@ public class BlockIndexer{ public Building findTile(Team team, float x, float y, float range, Boolf pred, boolean usePriority){ Building closest = null; float dst = 0; - var buildings = team.data().buildings; + var buildings = team.data().buildingTree; if(buildings == null) return null; breturnArray.clear(); @@ -444,6 +447,9 @@ public class BlockIndexer{ } } + //record in list of buildings + data.buildings.add(tile.build); + //update the unit cap when new tile is registered data.unitCap += tile.block().unitCapModifier; @@ -452,17 +458,17 @@ public class BlockIndexer{ } //insert the new tile into the quadtree for targeting - if(data.buildings == null){ - data.buildings = new QuadTree<>(new Rect(0, 0, world.unitWidth(), world.unitHeight())); + if(data.buildingTree == null){ + data.buildingTree = new QuadTree<>(new Rect(0, 0, world.unitWidth(), world.unitHeight())); } - data.buildings.insert(tile.build); + data.buildingTree.insert(tile.build); if(tile.block().attacks && tile.build instanceof Ranged){ - if(data.turrets == null){ - data.turrets = new TurretQuadtree(new Rect(0, 0, world.unitWidth(), world.unitHeight())); + if(data.turretTree == null){ + data.turretTree = new TurretQuadtree(new Rect(0, 0, world.unitWidth(), world.unitHeight())); } - data.turrets.insert(tile.build); + data.turretTree.insert(tile.build); } notifyBuildDamaged(tile.build); diff --git a/core/src/mindustry/core/Control.java b/core/src/mindustry/core/Control.java index e586e575f0..07564dc93f 100644 --- a/core/src/mindustry/core/Control.java +++ b/core/src/mindustry/core/Control.java @@ -216,9 +216,8 @@ public class Control implements ApplicationListener, Loadable{ float unitsPerTick = 1f; boolean anyBuilds = false; - for(Tile tile : world.tiles){ - var build = tile.build; - if(!(build instanceof CoreBuild) && build != null && build.team == state.rules.defaultTeam){ + for(var build : state.rules.defaultTeam.data().buildings){ + if(!(build instanceof CoreBuild)){ var ccore = build.closestCore(); if(ccore != null && build.within(ccore, state.rules.enemyCoreBuildRadius)){ diff --git a/core/src/mindustry/entities/Units.java b/core/src/mindustry/entities/Units.java index 397597f1b5..86a65b3cf9 100644 --- a/core/src/mindustry/entities/Units.java +++ b/core/src/mindustry/entities/Units.java @@ -211,7 +211,7 @@ public class Units{ buildResult = null; cdist = 0f; - var buildings = team.data().buildings; + var buildings = team.data().buildingTree; if(buildings == null) return null; buildings.intersect(wx - range, wy - range, range*2f, range*2f, b -> { if(pred.get(b)){ @@ -466,7 +466,7 @@ public class Units{ if(other.tree().any(x, y, width, height)){ return true; } - if(other.turrets != null && other.turrets.any(x, y, width, height)){ + if(other.turretTree != null && other.turretTree.any(x, y, width, height)){ return true; } } diff --git a/core/src/mindustry/game/Teams.java b/core/src/mindustry/game/Teams.java index 9381746faf..7032427bc2 100644 --- a/core/src/mindustry/game/Teams.java +++ b/core/src/mindustry/game/Teams.java @@ -147,12 +147,12 @@ public class Teams{ for(Team team : Team.all){ TeamData data = team.data(); - data.presentFlag = false; + data.presentFlag = data.buildings.size > 0; data.unitCount = 0; data.units.clear(); data.players.clear(); - if(data.tree != null){ - data.tree.clear(); + if(data.unitTree != null){ + data.unitTree.clear(); } if(data.typeCounts != null){ @@ -169,9 +169,7 @@ public class Teams{ } } - //update presence flag. - Groups.build.each(b -> b.team.data().presentFlag = true); - + //TODO this is slow and dumb for(Unit unit : Groups.unit){ if(unit.type == null) continue; TeamData data = unit.team.data(); @@ -243,21 +241,23 @@ public class Teams{ public Queue blocks = new Queue<>(); /** Quadtree for all buildings of this team. Null if not active. */ - public @Nullable QuadTree buildings; + public @Nullable QuadTree buildingTree; /** Turrets by range. Null if not active. */ - public @Nullable QuadTree turrets; + public @Nullable QuadTree turretTree; + /** Quadtree for units of this team. Do not access directly. */ + public @Nullable QuadTree unitTree; /** Current unit cap. Do not modify externally. */ public int unitCap; /** Total unit count. */ public int unitCount; /** Counts for each type of unit. Do not access directly. */ public @Nullable int[] typeCounts; - /** Quadtree for units of this team. Do not access directly. */ - public @Nullable QuadTree tree; /** Units of this team. Updated each frame. */ - public Seq units = new Seq<>(); + public Seq units = new Seq<>(false); /** Same as units, but players. */ - public Seq players = new Seq<>(); + public Seq players = new Seq<>(false); + /** All buildings. Updated on team change / building addition or removal. Includes even buildings that do not update(). */ + public Seq buildings = new Seq<>(false); /** Units of this team by type. Updated each frame. */ public @Nullable Seq[] unitsByType; @@ -270,8 +270,8 @@ public class Teams{ //grab all buildings from quadtree. var builds = new Seq(); - if(buildings != null){ - buildings.getObjects(builds); + if(buildingTree != null){ + buildingTree.getObjects(builds); } //no remaining blocks, cease building if applicable @@ -294,8 +294,8 @@ public class Teams{ /** Make all buildings within this range derelict / explode. */ public void makeDerelict(float x, float y, float range){ var builds = new Seq(); - if(buildings != null){ - buildings.intersect(x - range, y - range, range * 2f, range * 2f, builds); + if(buildingTree != null){ + buildingTree.intersect(x - range, y - range, range * 2f, range * 2f, builds); } for(var build : builds){ @@ -308,8 +308,8 @@ public class Teams{ /** Make all buildings within this range explode. */ public void timeDestroy(float x, float y, float range){ var builds = new Seq(); - if(buildings != null){ - buildings.intersect(x - range, y - range, range * 2f, range * 2f, builds); + if(buildingTree != null){ + buildingTree.intersect(x - range, y - range, range * 2f, range * 2f, builds); } for(var build : builds){ @@ -344,8 +344,8 @@ public class Teams{ } public QuadTree tree(){ - if(tree == null) tree = new QuadTree<>(Vars.world.getQuadBounds(new Rect())); - return tree; + if(unitTree == null) unitTree = new QuadTree<>(Vars.world.getQuadBounds(new Rect())); + return unitTree; } public int countType(UnitType type){