-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy pathBlockIndexer.java
505 lines (415 loc) · 17 KB
/
BlockIndexer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
package mindustry.ai;
import arc.*;
import arc.func.*;
import arc.math.*;
import arc.math.geom.*;
import arc.struct.*;
import arc.util.*;
import mindustry.content.*;
import mindustry.game.EventType.*;
import mindustry.game.*;
import mindustry.game.Teams.*;
import mindustry.gen.*;
import mindustry.logic.*;
import mindustry.type.*;
import mindustry.world.*;
import mindustry.world.meta.*;
import static mindustry.Vars.*;
/** Class used for indexing special target blocks for AI. */
public class BlockIndexer{
/** Size of one quadrant. */
private static final int quadrantSize = 20;
private static final Rect rect = new Rect();
private static boolean returnBool = false;
private int quadWidth, quadHeight;
/** Stores all ore quadrants on the map. Maps ID to qX to qY to a list of tiles with that ore. */
private IntSeq[][][] ores;
/** Stores all damaged tile entities by team. */
private Seq<Building>[] damagedTiles = new Seq[Team.all.length];
/** All ores available on this map. */
private ObjectIntMap<Item> allOres = new ObjectIntMap<>();
/** Stores teams that are present here as tiles. */
private Seq<Team> activeTeams = new Seq<>(Team.class);
/** Maps teams to a map of flagged tiles by flag. */
private Seq<Building>[][] flagMap = new Seq[Team.all.length][BlockFlag.all.length];
/** Counts whether a certain floor is present in the world upon load. */
private boolean[] blocksPresent;
/** Array used for returning and reusing. */
private Seq<Building> breturnArray = new Seq<>(Building.class);
public BlockIndexer(){
clearFlags();
Events.on(TilePreChangeEvent.class, event -> removeIndex(event.tile));
Events.on(TileChangeEvent.class, event -> addIndex(event.tile));
Events.on(WorldLoadEvent.class, event -> {
damagedTiles = new Seq[Team.all.length];
flagMap = new Seq[Team.all.length][BlockFlag.all.length];
activeTeams = new Seq<>(Team.class);
clearFlags();
allOres.clear();
ores = new IntSeq[content.items().size][][];
quadWidth = Mathf.ceil(world.width() / (float)quadrantSize);
quadHeight = Mathf.ceil(world.height() / (float)quadrantSize);
blocksPresent = new boolean[content.blocks().size];
//so WorldLoadEvent gets called twice sometimes... ugh
for(Team team : Team.all){
var data = state.teams.get(team);
if(data != null){
if(data.buildingTree != null) data.buildingTree.clear();
if(data.turretTree != null) data.turretTree.clear();
}
}
for(Tile tile : world.tiles){
process(tile);
var drop = tile.drop();
if(drop != null){
int qx = (tile.x / quadrantSize);
int qy = (tile.y / quadrantSize);
//add position of quadrant to list
if(tile.block() == Blocks.air){
if(ores[drop.id] == null){
ores[drop.id] = new IntSeq[quadWidth][quadHeight];
}
if(ores[drop.id][qx][qy] == null){
ores[drop.id][qx][qy] = new IntSeq(false, 16);
}
ores[drop.id][qx][qy].add(tile.pos());
allOres.increment(drop);
}
}
}
});
}
public void removeIndex(Tile tile){
var team = tile.team();
if(tile.build != null && tile.isCenter()){
var build = tile.build;
var flags = tile.block().flags;
var data = team.data();
if(flags.size > 0){
for(BlockFlag flag : flags.array){
getFlagged(team)[flag.ordinal()].remove(build);
}
}
//no longer part of the building list
data.buildings.remove(build);
data.buildingTypes.get(build.block, () -> new Seq<>(false)).remove(build);
//update the unit cap when building is removed
data.unitCap -= tile.block().unitCapModifier;
//unregister building from building quadtree
if(data.buildingTree != null){
data.buildingTree.remove(build);
}
//remove indexed turret
if(data.turretTree != null && build.block.attacks){
data.turretTree.remove(build);
}
//unregister damaged buildings
if(build.wasDamaged && damagedTiles[team.id] != null){
damagedTiles[team.id].remove(build);
}
//is no longer registered
build.wasDamaged = false;
}
}
public void addIndex(Tile tile){
process(tile);
var drop = tile.drop();
if(drop != null && ores != null){
int qx = tile.x / quadrantSize;
int qy = tile.y / quadrantSize;
if(ores[drop.id] == null){
ores[drop.id] = new IntSeq[quadWidth][quadHeight];
}
if(ores[drop.id][qx][qy] == null){
ores[drop.id][qx][qy] = new IntSeq(false, 16);
}
int pos = tile.pos();
var seq = ores[drop.id][qx][qy];
if(tile.block() == Blocks.air){
//add the index if it is a valid new spot to mine at
if(!seq.contains(pos)){
seq.add(pos);
allOres.increment(drop);
}
}else if(seq.contains(pos)){ //otherwise, it likely became blocked, remove it
seq.removeValue(pos);
allOres.increment(drop, -1);
}
}
}
/** @return whether a certain block is anywhere on this map. */
public boolean isBlockPresent(Block block){
return blocksPresent != null && blocksPresent[block.id];
}
private void clearFlags(){
for(int i = 0; i < flagMap.length; i++){
for(int j = 0; j < BlockFlag.all.length; j++){
flagMap[i][j] = new Seq();
}
}
}
private Seq<Building>[] getFlagged(Team team){
return flagMap[team.id];
}
/** @return whether this item is present on this map. */
public boolean hasOre(Item item){
return allOres.get(item) > 0;
}
/** Returns all damaged tiles by team. */
public Seq<Building> getDamaged(Team team){
if(damagedTiles[team.id] == null){
return damagedTiles[team.id] = new Seq<>(false);
}
var tiles = damagedTiles[team.id];
tiles.removeAll(b -> !b.damaged());
return tiles;
}
/** Get all allied blocks with a flag. */
public Seq<Building> getFlagged(Team team, BlockFlag type){
return flagMap[team.id][type.ordinal()];
}
@Nullable
public Building findClosestFlag(float x, float y, Team team, BlockFlag flag){
return Geometry.findClosest(x, y, getFlagged(team, flag));
}
public boolean eachBlock(Teamc team, float range, Boolf<Building> pred, Cons<Building> cons){
return eachBlock(team.team(), team.getX(), team.getY(), range, pred, cons);
}
public boolean eachBlock(@Nullable Team team, float wx, float wy, float range, Boolf<Building> pred, Cons<Building> cons){
if(team == null){
returnBool = false;
allBuildings(wx, wy, range, b -> {
if(pred.get(b)){
returnBool = true;
cons.get(b);
}
});
return returnBool;
}else{
breturnArray.clear();
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)){
breturnArray.add(b);
}
});
}
int size = breturnArray.size;
var items = breturnArray.items;
for(int i = 0; i < size; i++){
cons.get(items[i]);
items[i] = null;
}
breturnArray.size = 0;
return size > 0;
}
/** Does not work with null teams. */
public boolean eachBlock(Team team, Rect rect, Boolf<Building> pred, Cons<Building> cons){
if(team == null) return false;
breturnArray.clear();
var buildings = team.data().buildingTree;
if(buildings == null) return false;
buildings.intersect(rect, b -> {
if(pred.get(b)){
breturnArray.add(b);
}
});
int size = breturnArray.size;
var items = breturnArray.items;
for(int i = 0; i < size; i++){
cons.get(items[i]);
items[i] = null;
}
breturnArray.size = 0;
return size > 0;
}
/** Get all enemy blocks with a flag. */
public Seq<Building> getEnemy(Team team, BlockFlag type){
breturnArray.clear();
Seq<TeamData> data = state.teams.present;
//when team data is not initialized, scan through every team. this is terrible
if(data.isEmpty()){
for(Team enemy : Team.all){
if(enemy == team || (enemy == Team.derelict && !state.rules.coreCapture)) continue;
var set = getFlagged(enemy)[type.ordinal()];
if(set != null){
breturnArray.addAll(set);
}
}
}else{
for(int i = 0; i < data.size; i++){
Team enemy = data.items[i].team;
if(enemy == team || (enemy == Team.derelict && !state.rules.coreCapture)) continue;
var set = getFlagged(enemy)[type.ordinal()];
if(set != null){
breturnArray.addAll(set);
}
}
}
return breturnArray;
}
public void notifyHealthChanged(Building build){
boolean damaged = build.damaged();
if(build.wasDamaged != damaged){
if(damagedTiles[build.team.id] == null){
damagedTiles[build.team.id] = new Seq<>(false);
}
if(damaged){
//is now damaged, add to array
damagedTiles[build.team.id].add(build);
}else{
//no longer damaged, remove
damagedTiles[build.team.id].remove(build);
}
build.wasDamaged = damaged;
}
}
public void allBuildings(float x, float y, float range, Cons<Building> cons){
breturnArray.clear();
for(int i = 0; i < activeTeams.size; i++){
Team team = activeTeams.items[i];
var buildings = team.data().buildingTree;
if(buildings == null) continue;
buildings.intersect(x - range, y - range, range*2f, range*2f, breturnArray);
}
var items = breturnArray.items;
int size = breturnArray.size;
for(int i = 0; i < size; i++){
var b = items[i];
if(b != null && b.within(x, y, range + b.hitSize()/2f)){
cons.get(b);
}
items[i] = null;
}
breturnArray.size = 0;
}
public Building findEnemyTile(Team team, float x, float y, float range, Boolf<Building> pred){
Building target = null;
float targetDist = 0;
for(int i = 0; i < activeTeams.size; i++){
Team enemy = activeTeams.items[i];
if(enemy == team || (enemy == Team.derelict && !state.rules.coreCapture)) continue;
Building candidate = indexer.findTile(enemy, x, y, range, b -> pred.get(b) && b.isDiscovered(team), true);
if(candidate == null) continue;
//if a block has the same priority, the closer one should be targeted
float dist = candidate.dst(x, y) - candidate.hitSize() / 2f;
if(target == null ||
//if its closer and is at least equal priority
(dist < targetDist && candidate.block.priority >= target.block.priority) ||
// block has higher priority (so range doesnt matter)
(candidate.block.priority > target.block.priority)){
target = candidate;
targetDist = dist;
}
}
return target;
}
public Building findTile(Team team, float x, float y, float range, Boolf<Building> pred){
return findTile(team, x, y, range, pred, false);
}
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().buildingTree;
if(buildings == null) return null;
breturnArray.clear();
buildings.intersect(rect.setCentered(x, y, range * 2f), breturnArray);
for(int i = 0; i < breturnArray.size; i++){
var next = breturnArray.items[i];
if(!pred.get(next) || !next.block.targetable) continue;
float bdst = next.dst(x, y) - next.hitSize() / 2f;
if(bdst < range && (closest == null ||
//this one is closer, and it is at least of equal priority
(bdst < dst && (!usePriority || closest.block.priority <= next.block.priority)) ||
//priority is used, and new block has higher priority regardless of range
(usePriority && closest.block.priority < next.block.priority))){
dst = bdst;
closest = next;
}
}
return closest;
}
/** Find the closest ore block relative to a position. */
public Tile findClosestOre(float xp, float yp, Item item){
if(ores[item.id] != null){
float minDst = 0f;
Tile closest = null;
for(int qx = 0; qx < quadWidth; qx++){
for(int qy = 0; qy < quadHeight; qy++){
var arr = ores[item.id][qx][qy];
if(arr != null && arr.size > 0){
Tile tile = world.tile(arr.first());
if(tile.block() == Blocks.air){
float dst = Mathf.dst2(xp, yp, tile.worldx(), tile.worldy());
if(closest == null || dst < minDst){
closest = tile;
minDst = dst;
}
}
}
}
}
return closest;
}
return null;
}
/** Find the closest ore block relative to a position. */
public Tile findClosestOre(Unit unit, Item item){
return findClosestOre(unit.x, unit.y, item);
}
private void process(Tile tile){
var team = tile.team();
//only process entity changes with centered tiles
if(tile.isCenter() && tile.build != null){
var data = team.data();
if(tile.block().flags.size > 0 && tile.isCenter()){
var map = getFlagged(team);
for(BlockFlag flag : tile.block().flags.array){
map[flag.ordinal()].add(tile.build);
}
}
//record in list of buildings
data.buildings.add(tile.build);
data.buildingTypes.get(tile.block(), () -> new Seq<>(false)).add(tile.build);
//update the unit cap when new tile is registered
data.unitCap += tile.block().unitCapModifier;
if(!activeTeams.contains(team)){
activeTeams.add(team);
}
//insert the new tile into the quadtree for targeting
if(data.buildingTree == null){
data.buildingTree = new QuadTree<>(new Rect(0, 0, world.unitWidth(), world.unitHeight()));
}
data.buildingTree.insert(tile.build);
if(tile.block().attacks && tile.build instanceof Ranged){
if(data.turretTree == null){
data.turretTree = new TurretQuadtree(new Rect(0, 0, world.unitWidth(), world.unitHeight()));
}
data.turretTree.insert(tile.build);
}
notifyHealthChanged(tile.build);
}
if(blocksPresent != null){
if(!tile.block().isStatic()){
blocksPresent[tile.floorID()] = true;
blocksPresent[tile.overlayID()] = true;
}
//bounds checks only needed in very specific scenarios
if(tile.blockID() < blocksPresent.length) blocksPresent[tile.blockID()] = true;
}
}
static class TurretQuadtree extends QuadTree<Building>{
public TurretQuadtree(Rect bounds){
super(bounds);
}
@Override
public void hitbox(Building build){
tmp.setCentered(build.x, build.y, ((Ranged)build).range() * 2f);
}
@Override
protected QuadTree<Building> newChild(Rect rect){
return new TurretQuadtree(rect);
}
}
}