Repository navigation
Expand file tree
/
Copy pathAgent.java
More file actions
508 lines (416 loc) · 11.6 KB
/
Copy pathAgent.java
File metadata and controls
508 lines (416 loc) · 11.6 KB
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
506
507
508
package org.bk.ass.sim;
import static java.lang.Math.max;
import static java.lang.Math.min;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.function.Consumer;
public class Agent implements Cloneable {
static final Consumer<UnitDeathContext> CARRIER_DEATH_HANDLER =
context -> {
Agent carrier = context.deadUnit;
context.removeAgents(carrier.interceptors);
};
// Retrieved from OpenBW
private static final int STIM_TIMER = 37;
private static final int STIM_HEALTH_COST_SHIFTED = 10 << 8;
private static final int ENSNARE_TIMER = 75;
private static final int ENSNARE_ENERGY_COST_SHIFTED = 75 << 8;
private final String name;
TargetingPriority attackTargetPriority = TargetingPriority.HIGHEST;
int armorShifted;
int shieldUpgrades;
Object userObject;
int elevationLevel = -2;
int x;
int y;
int nx;
int ny;
boolean speedUpgrade;
float baseSpeed;
int speedSquared;
float speed;
private float speedFactor = 1;
private boolean scout;
// Velocity (pixel per frame) for this frame to apply
int vx;
int vy;
int healthShifted;
int maxHealthShifted;
boolean healedThisFrame;
int stimTimer;
int ensnareTimer;
int hpConstructionRate;
int shieldsShifted;
int maxShieldsShifted;
int energyShifted;
int maxEnergyShifted;
int attackCounter;
int cooldown;
boolean cooldownUpgrade;
// Number of frames to sleep (ie. to prevent a move to break the attack)
int sleepTimer;
int stopFrames;
boolean canStim;
int plagueDamagePerFrameShifted;
// Is Zerg and not an Egg/Larva
boolean regeneratesHealth;
// Dies on attack
boolean isSuicider;
boolean isHealer;
boolean isFlyer;
boolean isOrganic;
boolean isMechanic;
boolean isKiter;
boolean isRepairer;
boolean protectedByDarkSwarm;
boolean burrowed;
boolean burrowedAttacker;
// Visible to the other force
boolean detected;
int stasisTimer;
// Lockdown or no damage and no movement
UnitSize size;
boolean isMelee;
Weapon airWeapon;
Weapon groundWeapon;
boolean seekableTarget;
int groundSeekRangeSquared = 0;
Agent attackTarget;
// Target for healing/repairing
Agent restoreTarget;
List<Agent> interceptors = Collections.emptyList();
// Allow replacement of units on death (for example bunker -> marines)
Consumer<UnitDeathContext> onDeathHandler = (ignored1) -> {
};
public Agent(String name) {
this.name = name;
}
@Override
public Agent clone() {
try {
Agent agent = (Agent) super.clone();
agent.attackTarget = null;
agent.restoreTarget = null;
agent.interceptors = Collections.emptyList();
return agent;
} catch (CloneNotSupportedException e) {
throw new RuntimeException(e);
}
}
public Agent setUserObject(Object userObject) {
this.userObject = userObject;
return this;
}
public Object getUserObject() {
return userObject;
}
public Agent setMelee(boolean melee) {
isMelee = melee;
return this;
}
public Agent setBurrowedAttacker(boolean burrowedAttacker) {
this.burrowedAttacker = burrowedAttacker;
return this;
}
public Agent setBurrowed(boolean burrowed) {
this.burrowed = burrowed;
return this;
}
public Agent setCanStim(boolean canStim) {
this.canStim = canStim;
return this;
}
public Agent setStimTimer(int stimTimer) {
this.stimTimer = stimTimer;
return this;
}
/**
* Set the unit to sleep for some frames, it can still be attacked but is essentially "locked
* down" in that time. <em>Note: This will override lockdown.</em>
*/
public Agent setSleepTimer(int sleepTimer) {
this.sleepTimer = sleepTimer;
return this;
}
public int getSleepTimer() {
return sleepTimer;
}
public Agent setEnsnareTimer(int ensnareTimer) {
this.ensnareTimer = ensnareTimer;
return this;
}
public Agent setStasisTimer(int stasisTimer) {
this.stasisTimer = stasisTimer;
return this;
}
final boolean isStasised() {
return stasisTimer > 0;
}
/**
* Marks this agent as being under lockdown. This is an alias for {@link #setPassive(boolean)}.
*
* @see #setPassive(boolean)
*/
public Agent setLockDownTimer(int lockDownTimer) {
sleepTimer = max(sleepTimer, lockDownTimer);
return this;
}
/**
* Makes this agent passive in a simulation. It will not damage other units or move. It can still
* be attacked and destroyed.
*/
public Agent setPassive(boolean passive) {
sleepTimer = passive ? Integer.MAX_VALUE : sleepTimer;
return this;
}
@Override
public String toString() {
return name + " (" + x + ", " + y + "), hp: " + getHealth() + ", sh: " + getShields();
}
public Agent setDetected(boolean detected) {
this.detected = detected;
return this;
}
public Agent setArmor(int armor) {
this.armorShifted = armor << 8;
return this;
}
public Agent setMaxEnergy(int maxEnergyShifted) {
this.maxEnergyShifted = maxEnergyShifted << 8;
return this;
}
public Agent setEnergy(int energyShifted) {
this.energyShifted = energyShifted << 8;
return this;
}
public Agent setShieldUpgrades(int shieldUpgrades) {
this.shieldUpgrades = shieldUpgrades;
return this;
}
public Agent setElevationLevel(int elevationLevel) {
this.elevationLevel = elevationLevel;
return this;
}
public Agent setX(int x) {
this.nx = x;
return this;
}
public Agent setY(int y) {
this.ny = y;
return this;
}
public Agent setBaseSpeed(float speed) {
this.baseSpeed = speed;
return this;
}
public Agent setSpeedUpgrade(boolean speedUpgrade) {
this.speedUpgrade = speedUpgrade;
return this;
}
/**
* Multiply the calculated speed by this factor. A potential use is to slow down units running
* away a bit to compensate for obstacles.
*/
public Agent setSpeedFactor(float speedFactor) {
this.speedFactor = speedFactor;
return this;
}
public Agent setCooldownUpgrade(boolean cooldownUpgrade) {
this.cooldownUpgrade = cooldownUpgrade;
return this;
}
void updateSpeed() {
speed = baseSpeed;
int mod = 0;
if (stimTimer > 0) mod++;
if (speedUpgrade) mod++;
if (ensnareTimer > 0) mod--;
if (mod < 0) speed /= 2f;
if (mod > 0) {
if (scout) {
speed = 6 + 2 / 3f;
} else {
speed *= 1.5f;
float minSpeed = 3 + 1 / 3f;
if (speed < minSpeed) {
speed = minSpeed;
}
}
}
speed *= speedFactor;
this.speedSquared = Math.round(speed * speed);
}
public Agent setHealth(int health) {
this.healthShifted = health << 8;
return this;
}
public int getHealth() {
return min(healthShifted, maxHealthShifted) >> 8;
}
public Agent setMaxHealth(int maxHealth) {
this.maxHealthShifted = maxHealth << 8;
return this;
}
public Agent setShields(int shields) {
this.shieldsShifted = shields << 8;
return this;
}
public int getShields() {
return min(shieldsShifted, maxShieldsShifted) >> 8;
}
public Agent setMaxShields(int maxShields) {
this.maxShieldsShifted = maxShields << 8;
return this;
}
public Agent setCooldown(int cooldown) {
this.cooldown = cooldown;
return this;
}
public Agent setStopFrames(int stopFrames) {
this.stopFrames = stopFrames;
return this;
}
public Agent setRegeneratesHealth(boolean regeneratesHealth) {
this.regeneratesHealth = regeneratesHealth;
return this;
}
public Agent setSuicider(boolean suicider) {
isSuicider = suicider;
return this;
}
public Agent setRepairer(boolean repairer) {
isRepairer = repairer;
return this;
}
public Agent setMechanic(boolean mechanic) {
this.isMechanic = mechanic;
return this;
}
public Agent setHealer(boolean healer) {
isHealer = healer;
return this;
}
public Agent setFlyer(boolean flyer) {
isFlyer = flyer;
return this;
}
public Agent setOrganic(boolean organic) {
isOrganic = organic;
return this;
}
public Agent setSize(UnitSize size) {
this.size = size;
return this;
}
public Agent setOnDeathHandler(Consumer<UnitDeathContext> onDeathHandler) {
this.onDeathHandler = onDeathHandler;
return this;
}
public Agent setKiter(boolean kiter) {
isKiter = kiter;
return this;
}
public Agent setAirWeapon(Weapon airWeapon) {
this.airWeapon = airWeapon;
return this;
}
public Agent setGroundWeapon(Weapon groundWeapon) {
this.groundWeapon = groundWeapon;
return this;
}
/** Returns the number of times this agent has started an attack in simulations. */
public int getAttackCounter() {
return attackCounter;
}
public void resetAttackCounter() {
attackCounter = 0;
}
/**
* Sets the agents target to attack. Might be overridden by behavior. Ie. if the target is out of
* range in a simulation frame.
*/
public void setAttackTarget(Agent attackTarget) {
this.attackTarget = attackTarget;
}
public void setRestoreTarget(Agent restoreTarget) {
this.restoreTarget = restoreTarget;
}
/** Set this carriers interceptors. These will be killed as well, if the carrier dies. */
public void setInterceptors(Collection<Agent> childAgents) {
this.interceptors = new ArrayList<>(childAgents);
}
/**
* Change the priority of this agent when other agents try to find a new target. For best
* performance keep most agents on {@link TargetingPriority#HIGHEST}.
*
* <p>Interceptors are assigned a low priority by default, the rest defaults to {@link
* TargetingPriority#HIGHEST}.
*/
public Agent setAttackTargetPriority(TargetingPriority attackTargetPriority) {
this.attackTargetPriority = attackTargetPriority;
return this;
}
/** Mark this agent as being protected by dark swarm (ground unit and under dark swarm). */
public Agent setProtectedByDarkSwarm(boolean protectedByDarkSwarm) {
this.protectedByDarkSwarm = protectedByDarkSwarm;
return this;
}
final Weapon weaponVs(Agent other) {
if (other.isFlyer) {
return airWeapon;
}
return groundWeapon;
}
/** Has to be called *after* max health has been set */
public Agent setHpConstructionRate(int buildTime) {
if (buildTime == 0) {
return this;
}
this.hpConstructionRate =
max(1, (maxHealthShifted - maxHealthShifted / 10 + buildTime - 1) / buildTime);
return this;
}
public Agent setPlagueDamage(int plagueDamage) {
this.plagueDamagePerFrameShifted = (plagueDamage << 8) / 76;
return this;
}
public final void consumeEnergy(int amountShifted) {
energyShifted = min(energyShifted, maxEnergyShifted) - amountShifted;
}
public final void consumeHealth(int amountShifted) {
healthShifted = min(healthShifted, maxHealthShifted) - amountShifted;
}
public Agent setScout(boolean scout) {
this.scout = scout;
return this;
}
public Agent setGroundSeekRange(int range) {
this.groundSeekRangeSquared = range * range;
return this;
}
public Agent setSeekableTarget(boolean seekableTarget) {
this.seekableTarget = seekableTarget;
return this;
}
public boolean isMovable() {
return baseSpeed > 0;
}
public final void heal(int amountShifted) {
healthShifted += amountShifted;
}
public final void stim() {
stimTimer = STIM_TIMER;
consumeHealth(STIM_HEALTH_COST_SHIFTED);
}
public final void ensnare() {
ensnareTimer = ENSNARE_TIMER;
consumeEnergy(ENSNARE_ENERGY_COST_SHIFTED);
}
public enum TargetingPriority {
LOW,
MEDIUM,
HIGHEST // Intentionally named "highest" as it also speeds up target selection
}
}