Better team building presence check

This commit is contained in:
Anuken 2022-04-17 19:59:36 -04:00
parent eb299f5ec7
commit 224b1b022a
4 changed files with 46 additions and 41 deletions

View File

@ -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<Building> 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);

View File

@ -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)){

View File

@ -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;
}
}

View File

@ -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<BlockPlan> blocks = new Queue<>();
/** Quadtree for all buildings of this team. Null if not active. */
public @Nullable QuadTree<Building> buildings;
public @Nullable QuadTree<Building> buildingTree;
/** Turrets by range. Null if not active. */
public @Nullable QuadTree<Building> turrets;
public @Nullable QuadTree<Building> turretTree;
/** Quadtree for units of this team. Do not access directly. */
public @Nullable QuadTree<Unit> 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<Unit> tree;
/** Units of this team. Updated each frame. */
public Seq<Unit> units = new Seq<>();
public Seq<Unit> units = new Seq<>(false);
/** Same as units, but players. */
public Seq<Player> players = new Seq<>();
public Seq<Player> players = new Seq<>(false);
/** All buildings. Updated on team change / building addition or removal. Includes even buildings that do not update(). */
public Seq<Building> buildings = new Seq<>(false);
/** Units of this team by type. Updated each frame. */
public @Nullable Seq<Unit>[] unitsByType;
@ -270,8 +270,8 @@ public class Teams{
//grab all buildings from quadtree.
var builds = new Seq<Building>();
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<Building>();
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<Building>();
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<Unit> 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){