Skip to content

Commit 64485d2

Browse files
author
Bytekeeper
committed
Fixed movement within tiles. Also fixed bug trashing the collision map if an agent's position was modified after being added to the simulation.
1 parent c18ce14 commit 64485d2

File tree

7 files changed

+59
-17
lines changed

7 files changed

+59
-17
lines changed

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ public class Agent {
2727
int elevationLevel = -2;
2828
int x;
2929
int y;
30+
int nx;
31+
int ny;
3032
boolean speedUpgrade;
3133
float baseSpeed;
3234
int speedSquared;
@@ -122,6 +124,8 @@ public Agent(Agent other) {
122124
this.elevationLevel = other.elevationLevel;
123125
this.x = other.x;
124126
this.y = other.y;
127+
this.nx = other.nx;
128+
this.ny = other.ny;
125129
this.speedUpgrade = other.speedUpgrade;
126130
this.baseSpeed = other.baseSpeed;
127131
this.speedSquared = other.speedSquared;
@@ -282,12 +286,12 @@ public Agent setElevationLevel(int elevationLevel) {
282286
}
283287

284288
public Agent setX(int x) {
285-
this.x = x;
289+
this.nx = x;
286290
return this;
287291
}
288292

289293
public Agent setY(int y) {
290-
this.y = y;
294+
this.ny = y;
291295
return this;
292296
}
293297

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,8 @@ public static int reduceDamageByTargetSizeAndDamageType(
116116
public static void randomizePositions(Collection<Agent> agents, int ax, int ay, int bx, int by) {
117117
SplittableRandom posRnd = new SplittableRandom(1337L);
118118
for (Agent agent : agents) {
119-
agent.x = posRnd.nextInt(ax, bx + 1);
120-
agent.y = posRnd.nextInt(ay, by + 1);
119+
agent.nx = posRnd.nextInt(ax, bx + 1);
120+
agent.ny = posRnd.nextInt(ay, by + 1);
121121
}
122122
}
123123
}

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,7 @@ public boolean simUnit(
7878
}
7979

8080
if (agent.cooldown <= 0
81-
&& selectedDistanceSquared
82-
<= Math.max(Simulator.MIN_SIMULATION_RANGE, selectedWeapon.maxRangeSquared)) {
81+
&& selectedDistanceSquared <= selectedWeapon.maxRangeSquared) {
8382
simAttack(agent, allies, enemies, selectedEnemy, selectedWeapon);
8483
}
8584

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public class BWAPI4JAgentFactory {
3636

3737
private BiConsumer<Agent, Collection<Agent>> bunkerDeathHandler =
3838
(bunker, agents) -> {
39+
// TODO: Fix collision map not being updated here
3940
agents.add(of(UnitType.Terran_Marine));
4041
agents.add(of(UnitType.Terran_Marine));
4142
agents.add(of(UnitType.Terran_Marine));

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ public class JBWAPIAgentFactory {
4040

4141
private BiConsumer<Agent, Collection<Agent>> bunkerReplacer =
4242
(bunker, agents) -> {
43+
// TODO: Fix collision map not being updated here
4344
agents.add(of(UnitType.Terran_Marine));
4445
agents.add(of(UnitType.Terran_Marine));
4546
agents.add(of(UnitType.Terran_Marine));

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

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,6 @@ public class Simulator {
3232

3333
private static final int MAX_MAP_DIMENSION = 8192;
3434
private static final int TILE_SIZE = 16;
35-
// Hack to fix DTs not being able to hit a target in another TILE due to collision
36-
public static final int MIN_SIMULATION_RANGE =
37-
(TILE_SIZE + TILE_SIZE / 2) * (TILE_SIZE + TILE_SIZE / 2);
3835
private static final int COLLISION_MAP_DIMENSION = MAX_MAP_DIMENSION / TILE_SIZE;
3936
private final UnorderedCollection<Agent> playerA = new UnorderedCollection<>();
4037
private final UnorderedCollection<Agent> playerB = new UnorderedCollection<>();
@@ -55,7 +52,6 @@ private Simulator(int frameSkip, Behavior playerABehavior, Behavior playerBBehav
5552
}
5653

5754
public Simulator addAgentA(Agent agent) {
58-
checkBounds(agent);
5955
playerA.add(agent);
6056
if (!agent.isFlyer) {
6157
collision[colindex(agent.x, agent.y)]++;
@@ -72,7 +68,6 @@ public void removeAgentB(Agent agent) {
7268
}
7369

7470
public Simulator addAgentB(Agent agent) {
75-
checkBounds(agent);
7671
playerB.add(agent);
7772
if (!agent.isFlyer) {
7873
collision[colindex(agent.x, agent.y)]++;
@@ -126,6 +121,30 @@ public int simulate() {
126121
*/
127122
public int simulate(int frames) {
128123
if (frames > 0) frames += Math.floorMod(frameSkip - frames, frameSkip);
124+
for (int i = playerA.size() - 1; i >= 0; i--) {
125+
Agent agent = playerA.get(i);
126+
if (!agent.isFlyer) {
127+
int oldCI = colindex(agent.x, agent.y);
128+
int newCI = colindex(agent.nx, agent.ny);
129+
collision[oldCI]--;
130+
collision[newCI]++;
131+
}
132+
agent.x = agent.nx;
133+
agent.y = agent.ny;
134+
checkBounds(agent);
135+
}
136+
for (int i = playerB.size() - 1; i >= 0; i--) {
137+
Agent agent = playerB.get(i);
138+
if (!agent.isFlyer) {
139+
int oldCI = colindex(agent.x, agent.y);
140+
int newCI = colindex(agent.nx, agent.ny);
141+
collision[oldCI]--;
142+
collision[newCI]++;
143+
}
144+
agent.x = agent.nx;
145+
agent.y = agent.ny;
146+
checkBounds(agent);
147+
}
129148
while (frames != 0 && !playerA.isEmpty() && !playerB.isEmpty()) {
130149
frames -= frameSkip;
131150
if (!step()) {
@@ -134,6 +153,16 @@ public int simulate(int frames) {
134153
}
135154
playerA.clearReferences();
136155
playerB.clearReferences();
156+
for (int i = playerA.size() - 1; i >= 0; i--) {
157+
Agent agent = playerA.get(i);
158+
agent.nx = agent.x;
159+
agent.ny = agent.y;
160+
}
161+
for (int i = playerB.size() - 1; i >= 0; i--) {
162+
Agent agent = playerB.get(i);
163+
agent.nx = agent.x;
164+
agent.ny = agent.y;
165+
}
137166
return frames;
138167
}
139168

@@ -234,10 +263,14 @@ private void updatePosition(Agent agent) {
234263
int newCI = colindex(tx, ty);
235264
if (oldCI != newCI) {
236265
if (collision[newCI] > TILE_SIZE / 8 - 1) {
237-
return;
266+
int cx = agent.x / TILE_SIZE * TILE_SIZE;
267+
int cy = agent.y / TILE_SIZE * TILE_SIZE;
268+
tx = Math.max(cx, Math.min(cx + TILE_SIZE - 1, tx));
269+
ty = Math.max(cy, Math.min(cy + TILE_SIZE - 1, ty));
270+
} else {
271+
collision[oldCI]--;
272+
collision[newCI]++;
238273
}
239-
collision[oldCI]--;
240-
collision[newCI]++;
241274
}
242275
}
243276

src/test/java/org/bk/ass/sim/SimulatorTest.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -696,16 +696,20 @@ void goonShouldDieWhenRunningAwayFromScout() {
696696

697697
@Test
698698
void addAgentAAtInvalidPositionShouldThrowException() {
699+
simulator.addAgentA(factory.of(UnitType.Protoss_Scout).setX(9000));
700+
699701
assertThrows(
700702
PositionOutOfBoundsException.class,
701-
() -> simulator.addAgentA(factory.of(UnitType.Protoss_Scout).setX(9000)));
703+
() -> simulator.simulate(1));
702704
}
703705

704706
@Test
705707
void addAgentBAtInvalidPositionShouldThrowException() {
708+
simulator.addAgentB(factory.of(UnitType.Protoss_Scout).setY(9000));
709+
706710
assertThrows(
707711
PositionOutOfBoundsException.class,
708-
() -> simulator.addAgentB(factory.of(UnitType.Protoss_Scout).setY(9000)));
712+
() -> simulator.simulate(1));
709713
}
710714

711715
@Test
@@ -935,7 +939,7 @@ void reaverVs12Lings() {
935939
// GIVEN
936940
simulator.addAgentA(factory.of(UnitType.Protoss_Reaver));
937941

938-
for (int i = 0; i < 12; i++) {
942+
for (int i = 0; i < 13; i++) {
939943
simulator.addAgentB(factory.of(UnitType.Zerg_Zergling).setX(i * 30));
940944
}
941945

0 commit comments

Comments
 (0)