2020-05-01 16:35:18 -04:00
|
|
|
package mindustry.async;
|
|
|
|
|
|
|
|
import arc.math.geom.*;
|
|
|
|
import mindustry.*;
|
|
|
|
import mindustry.game.*;
|
|
|
|
import mindustry.gen.*;
|
2020-07-21 15:43:02 -04:00
|
|
|
import mindustry.type.*;
|
2020-05-01 16:35:18 -04:00
|
|
|
|
2020-05-15 19:54:04 -04:00
|
|
|
import java.util.*;
|
|
|
|
|
2020-05-01 16:35:18 -04:00
|
|
|
/** Creates quadtrees per unit team. */
|
|
|
|
public class TeamIndexProcess implements AsyncProcess{
|
2020-06-26 14:27:26 -04:00
|
|
|
private QuadTree<Unit>[] trees = new QuadTree[Team.all.length];
|
2020-05-28 11:27:42 -04:00
|
|
|
private int[] counts = new int[Team.all.length];
|
2020-07-21 15:43:02 -04:00
|
|
|
private int[][] typeCounts = new int[Team.all.length][0];
|
2020-05-01 16:35:18 -04:00
|
|
|
|
2020-06-26 14:27:26 -04:00
|
|
|
public QuadTree<Unit> tree(Team team){
|
2020-07-03 12:36:12 -04:00
|
|
|
if(trees[team.id] == null) trees[team.id] = new QuadTree<>(Vars.world.getQuadBounds(new Rect()));
|
2020-05-01 16:35:18 -04:00
|
|
|
|
2020-07-03 12:36:12 -04:00
|
|
|
return trees[team.id];
|
2020-05-01 16:35:18 -04:00
|
|
|
}
|
|
|
|
|
2020-05-12 21:34:27 -04:00
|
|
|
public int count(Team team){
|
|
|
|
return counts[team.id];
|
|
|
|
}
|
|
|
|
|
2020-07-21 15:43:02 -04:00
|
|
|
public int countType(Team team, UnitType type){
|
2020-07-21 19:49:46 -04:00
|
|
|
return typeCounts[team.id].length <= type.id ? 0 : typeCounts[team.id][type.id];
|
2020-07-21 15:43:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public void updateCount(Team team, UnitType type, int amount){
|
|
|
|
|
2020-05-12 21:34:27 -04:00
|
|
|
counts[team.id] += amount;
|
2020-07-21 19:49:46 -04:00
|
|
|
if(typeCounts[team.id].length <= type.id){
|
2020-07-21 15:43:02 -04:00
|
|
|
typeCounts[team.id] = new int[Vars.content.units().size];
|
|
|
|
}
|
2020-07-21 19:49:46 -04:00
|
|
|
typeCounts[team.id][type.id] += amount;
|
2020-05-12 21:34:27 -04:00
|
|
|
}
|
|
|
|
|
2020-05-01 16:35:18 -04:00
|
|
|
@Override
|
|
|
|
public void reset(){
|
2020-05-28 11:27:42 -04:00
|
|
|
counts = new int[Team.all.length];
|
|
|
|
trees = new QuadTree[Team.all.length];
|
2020-05-01 16:35:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void begin(){
|
|
|
|
|
2020-05-28 11:27:42 -04:00
|
|
|
for(Team team : Team.all){
|
2020-07-03 12:36:12 -04:00
|
|
|
if(trees[team.id] != null){
|
|
|
|
trees[team.id].clear();
|
2020-05-01 16:35:18 -04:00
|
|
|
}
|
2020-07-21 15:43:02 -04:00
|
|
|
|
|
|
|
Arrays.fill(typeCounts[team.id], 0);
|
2020-05-01 16:35:18 -04:00
|
|
|
}
|
|
|
|
|
2020-05-15 19:54:04 -04:00
|
|
|
Arrays.fill(counts, 0);
|
2020-05-12 21:34:27 -04:00
|
|
|
|
2020-06-26 14:27:26 -04:00
|
|
|
for(Unit unit : Groups.unit){
|
|
|
|
tree(unit.team).insert(unit);
|
2020-07-21 15:43:02 -04:00
|
|
|
|
|
|
|
updateCount(unit.team, unit.type(), 1);
|
2020-05-01 16:35:18 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean shouldProcess(){
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|