Skip to content

Commit 2d9d380

Browse files
author
Bytekeeper
committed
Small performance improvements.
1 parent 31ce90f commit 2d9d380

File tree

6 files changed

+49
-30
lines changed

6 files changed

+49
-30
lines changed

src/main/java/org/bk/ass/sim/Agent.java

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,14 @@
77
import java.util.function.BiConsumer;
88

99
import static java.lang.Math.max;
10+
import static java.lang.Math.min;
1011

1112
public class Agent {
1213
static final BiConsumer<Agent, Collection<Agent>> CARRIER_DEATH_HANDLER =
1314
(carrier, agents) -> agents.removeAll(carrier.interceptors);
15+
// Retrieved from OpenBW
16+
private static final int STIM_TIMER = 37;
17+
private static final int STIM_ENERGY_COST_SHIFTED = 10 << 8;
1418

1519
private final String name;
1620
TargetingPriority attackTargetPriority = TargetingPriority.HIGHEST;
@@ -89,10 +93,11 @@ public Agent(String name) {
8993

9094
/**
9195
* Copy constructor. References to other agents are cleared:
96+
*
9297
* <ul>
93-
* <li>attackTarget</li>
94-
* <li>restoreTarget</li>
95-
* <li>interceptors</li>
98+
* <li>attackTarget
99+
* <li>restoreTarget
100+
* <li>interceptors
96101
* </ul>
97102
*/
98103
public Agent(Agent other) {
@@ -255,7 +260,7 @@ public Agent setHealth(int health) {
255260
}
256261

257262
public int getHealth() {
258-
return healthShifted >> 8;
263+
return min(healthShifted, maxHealthShifted) >> 8;
259264
}
260265

261266
public Agent setMaxHealth(int maxHealth) {
@@ -269,7 +274,7 @@ public Agent setShields(int shields) {
269274
}
270275

271276
public int getShields() {
272-
return shieldsShifted >> 8;
277+
return min(shieldsShifted, maxShieldsShifted) >> 8;
273278
}
274279

275280
public Agent setMaxShields(int maxShields) {
@@ -402,6 +407,23 @@ public Agent setPlagueDamage(int plagueDamage) {
402407
return this;
403408
}
404409

410+
public final void consumeEnergy(int amountShifted) {
411+
energyShifted = min(energyShifted, maxEnergyShifted) - amountShifted;
412+
}
413+
414+
public final void consumeHealth(int amountShifted) {
415+
healthShifted = min(healthShifted, maxHealthShifted) - amountShifted;
416+
}
417+
418+
public final void heal(int amountShifted) {
419+
healthShifted += amountShifted;
420+
}
421+
422+
public final void stim() {
423+
remainingStimFrames = STIM_TIMER;
424+
consumeHealth(STIM_ENERGY_COST_SHIFTED);
425+
}
426+
405427
public enum TargetingPriority {
406428
LOW,
407429
MEDIUM,

src/main/java/org/bk/ass/sim/AgentUtil.java

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ public class AgentUtil {
1010
private static final SplittableRandom rnd = new SplittableRandom();
1111

1212
// Retrieved from OpenBW
13-
private static final int STIM_TIMER = 37;
14-
private static final int STIM_ENERGY_COST_SHIFTED = 10 << 8;
1513
public static final int INTERCEPTOR_COOLDOWN = 45;
1614
public static final int REAVER_COOLDOWN = 60;
1715

@@ -160,7 +158,7 @@ public static void dealDamage(Agent agent, Weapon wpn, Agent target) {
160158
}
161159

162160
private static void applyDamage(Agent target, DamageType damageType, int damage, int hits) {
163-
int shields = target.shieldsShifted - damage + target.shieldUpgrades;
161+
int shields = min(target.maxShieldsShifted, target.shieldsShifted) - damage + target.shieldUpgrades;
164162
if (shields > 0) {
165163
target.shieldsShifted = shields;
166164
return;
@@ -176,7 +174,7 @@ private static void applyDamage(Agent target, DamageType damageType, int damage,
176174
reduceDamageByTargetSizeAndDamageType(
177175
target, damageType, damage - target.armorShifted * hits);
178176

179-
target.healthShifted -= max(128, damage);
177+
target.consumeHealth(max(128, damage));
180178
}
181179

182180
public static int reduceDamageByTargetSizeAndDamageType(
@@ -196,9 +194,4 @@ public static int reduceDamageByTargetSizeAndDamageType(
196194
}
197195
return damageShifted;
198196
}
199-
200-
public static void stim(Agent agent) {
201-
agent.remainingStimFrames = STIM_TIMER;
202-
agent.healthShifted -= STIM_ENERGY_COST_SHIFTED;
203-
}
204197
}

src/main/java/org/bk/ass/sim/AttackerBehavior.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ public class AttackerBehavior implements Behavior {
1111

1212
@Override
1313
public boolean simUnit(
14-
int frameSkip, Agent agent, UnorderedCollection<Agent> allies, UnorderedCollection<Agent> enemies) {
14+
int frameSkip,
15+
Agent agent,
16+
UnorderedCollection<Agent> allies,
17+
UnorderedCollection<Agent> enemies) {
1518
if (agent.cooldown > agent.maxCooldown - agent.stopFrames) {
1619
return true;
1720
}
@@ -90,7 +93,7 @@ private void simAttack(
9093
if (agent.canStim
9194
&& agent.remainingStimFrames <= 0
9295
&& agent.healthShifted >= agent.maxHealthShifted / 2) {
93-
AgentUtil.stim(agent);
96+
agent.stim();
9497
}
9598
dealDamage(agent, selectedWeapon, selectedEnemy);
9699
switch (selectedWeapon.splashType) {
@@ -116,7 +119,11 @@ private void simAttack(
116119
}
117120

118121
private void simCombatMove(
119-
int frameSkip, Agent agent, Agent selectedEnemy, int selectedDistanceSquared, Weapon selectedWeapon) {
122+
int frameSkip,
123+
Agent agent,
124+
Agent selectedEnemy,
125+
int selectedDistanceSquared,
126+
Weapon selectedWeapon) {
120127
boolean shouldKite =
121128
agent.isKiter
122129
&& agent.cooldown > 0

src/main/java/org/bk/ass/sim/HealerBehavior.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import org.bk.ass.collection.UnorderedCollection;
44
import org.bk.ass.sim.Simulator.Behavior;
55

6-
import static java.lang.Math.min;
76
import static org.bk.ass.sim.AgentUtil.distanceSquared;
87
import static org.bk.ass.sim.AgentUtil.moveToward;
98

@@ -63,10 +62,9 @@ public boolean simUnit(
6362
if (selectedDistanceSquared > MEDICS_HEAL_RANGE_SQUARED) {
6463
return true;
6564
}
66-
agent.energyShifted -= 256 * frameSkip;
65+
agent.consumeEnergy(256 * frameSkip);
6766
selectedAlly.healedThisFrame = true;
68-
selectedAlly.healthShifted = min(selectedAlly.maxHealthShifted, selectedAlly.healthShifted + 150 * frameSkip);
69-
67+
selectedAlly.heal(150 * frameSkip);
7068
return true;
7169
}
7270
}

src/main/java/org/bk/ass/sim/RepairerBehavior.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import org.bk.ass.collection.UnorderedCollection;
44
import org.bk.ass.sim.Simulator.Behavior;
55

6-
import static java.lang.Math.min;
76
import static java.lang.Math.sqrt;
87
import static org.bk.ass.sim.AgentUtil.distanceSquared;
98
import static org.bk.ass.sim.AgentUtil.moveToward;
@@ -58,7 +57,7 @@ public boolean simUnit(
5857
if (selectedDistanceSquared > SCV_REPAIR_RANGE_SQUARED) {
5958
return true;
6059
}
61-
selectedAlly.healthShifted = min(selectedAlly.maxHealthShifted, selectedAlly.healthShifted + selectedAlly.hpConstructionRate * frameSkip);
60+
selectedAlly.heal(selectedAlly.hpConstructionRate * frameSkip);
6261

6362
return true;
6463
}

src/main/java/org/bk/ass/sim/Simulator.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
import java.util.Objects;
99
import java.util.function.ToIntFunction;
1010

11-
import static java.lang.Math.min;
12-
1311
/**
1412
* Used to simulate 2 groups of agents engaging each other. Either use the default constructor which
1513
* initialized the default behavior or customize the behaviors. Ie. if you want to simulate one
@@ -221,14 +219,16 @@ private void updateStats(UnorderedCollection<Agent> agents) {
221219
agent.vy = 0;
222220
agent.healedThisFrame = false;
223221

224-
agent.cooldown = agent.cooldown - frameSkip;
225-
agent.shieldsShifted = min(agent.maxShieldsShifted, agent.shieldsShifted + 7 * frameSkip);
222+
// Since these calls are potentially made every frame, no boundary checks are done!
223+
// Bounds are established when the fields are modified.
224+
agent.cooldown -= frameSkip;
225+
agent.shieldsShifted += 7 * frameSkip;
226226
if (agent.plagueDamagePerFrameShifted * frameSkip < agent.healthShifted)
227227
agent.healthShifted -= agent.plagueDamagePerFrameShifted * frameSkip;
228-
agent.remainingStimFrames = agent.remainingStimFrames - frameSkip;
228+
agent.remainingStimFrames -= frameSkip;
229229
if (agent.regeneratesHealth)
230-
agent.healthShifted = min(agent.maxHealthShifted, agent.healthShifted + 4 * frameSkip);
231-
agent.energyShifted = min(agent.energyShifted + 8 * frameSkip, agent.maxEnergyShifted);
230+
agent.healthShifted += 4 * frameSkip;
231+
agent.energyShifted += 8 * frameSkip;
232232
}
233233
}
234234

0 commit comments

Comments
 (0)