Skip to content

Commit 21d4244

Browse files
author
Bytekeeper
committed
Move Simulator stuff to sim package. Added some grid/map related things
1 parent bb0ee1a commit 21d4244

31 files changed

+194
-90
lines changed

build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ repositories {
2020

2121
dependencies {
2222
implementation(fileTree("lib").include("*.jar"))
23-
implementation("com.github.JavaBWAPI:JBWAPI:0.7.2")
23+
implementation("com.github.JavaBWAPI:JBWAPI:0.8.2")
2424

2525
testImplementation("org.junit.jupiter:junit-jupiter:5.5.1")
2626
testImplementation("org.assertj:assertj-core:3.13.2")

src/jmh/java/org/bk/ass/EvaluatorBenchmark.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package org.bk.ass;
22

3+
import org.bk.ass.sim.Agent;
4+
import org.bk.ass.sim.BWAPI4JAgentFactory;
5+
import org.bk.ass.sim.Evaluator;
36
import org.openbw.bwapi4j.test.BWDataProvider;
47
import org.openbw.bwapi4j.type.UnitType;
58
import org.openjdk.jmh.annotations.*;

src/jmh/java/org/bk/ass/SimulatorBenchmark.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package org.bk.ass;
22

3+
import org.bk.ass.sim.BWAPI4JAgentFactory;
4+
import org.bk.ass.sim.Simulator;
35
import org.openbw.bwapi4j.test.BWDataProvider;
46
import org.openbw.bwapi4j.type.UnitType;
57
import org.openjdk.jmh.annotations.*;

src/jmh/java/org/bk/ass/path/JpsBenchmark.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public class JpsBenchmark {
1616
@State(Scope.Thread)
1717
public static class MyState {
1818
List<Position[]> positions;
19-
Map map;
19+
PPMap map;
2020
Jps jps;
2121
PPJps PPJps;
2222

@@ -30,7 +30,7 @@ public void setup() throws IOException {
3030
data[x][y] = image.getRGB(x, y) == -1;
3131
}
3232
}
33-
map = Map.fromBooleanArray(data);
33+
map = PPMap.fromBooleanArray(data);
3434
jps = new Jps(map);
3535
PPJps = new PPJps(map);
3636

src/main/java/org/bk/ass/PositionOutOfBoundsException.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
/** Thrown if a position is not within the required bounds. */
44
public class PositionOutOfBoundsException extends RuntimeException {
55

6-
PositionOutOfBoundsException(String message) {
6+
public PositionOutOfBoundsException(String message) {
77
super(message);
88
}
99
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package org.bk.ass.grid;
2+
3+
import bwapi.Game;
4+
import org.openbw.bwapi4j.BWMap;
5+
6+
public final class Grids {
7+
private Grids() {
8+
// Utility class
9+
}
10+
11+
public static Grid<Boolean> fromBooleanArray(boolean[][] map) {
12+
return new Grid<Boolean>() {
13+
@Override
14+
public Boolean get(int x, int y) {
15+
return y >= 0 && y < map[0].length && x >= 0 && x < map.length && map[x][y];
16+
}
17+
18+
@Override
19+
public int getWidth() {
20+
return map.length;
21+
}
22+
23+
@Override
24+
public int getHeight() {
25+
return map[0].length;
26+
}
27+
};
28+
}
29+
30+
public static Grid<Boolean> fromTileWalkability(Game game) {
31+
return new Grid<Boolean>() {
32+
@Override
33+
public int getWidth() {
34+
return game.mapWidth();
35+
}
36+
37+
@Override
38+
public int getHeight() {
39+
return game.mapHeight();
40+
}
41+
42+
@Override
43+
public Boolean get(int x, int y) {
44+
return game.isWalkable(x * 4, y * 4)
45+
&& game.isWalkable(x * 4 + 1, y * 4)
46+
&& game.isWalkable(x * 4, y * 4 + 1)
47+
&& game.isWalkable(x * 4 + 1, y * 4 + 1);
48+
}
49+
};
50+
}
51+
52+
public static Grid<Boolean> fromTileWalkability(BWMap map) {
53+
return new Grid<Boolean>() {
54+
@Override
55+
public int getWidth() {
56+
return map.mapWidth();
57+
}
58+
59+
@Override
60+
public int getHeight() {
61+
return map.mapHeight();
62+
}
63+
64+
@Override
65+
public Boolean get(int x, int y) {
66+
return map.isWalkable(x * 4, y * 4)
67+
&& map.isWalkable(x * 4 + 1, y * 4)
68+
&& map.isWalkable(x * 4, y * 4 + 1)
69+
&& map.isWalkable(x * 4 + 1, y * 4 + 1);
70+
}
71+
};
72+
}
73+
}

src/main/java/org/bk/ass/path/AbstractPathFinder.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package org.bk.ass.path;
22

3+
import org.bk.ass.grid.Grid;
4+
35
import java.util.ArrayList;
46
import java.util.Collections;
57
import java.util.List;
@@ -12,9 +14,9 @@ abstract class AbstractPathFinder {
1214
private final PriorityQueue<Node> openQueue = new PriorityQueue<>();
1315
private final Node[] nodes;
1416
final Position target;
15-
private final Map map;
17+
private final Grid<Boolean> map;
1618

17-
AbstractPathFinder(Position target, Map map) {
19+
AbstractPathFinder(Position target, Grid<Boolean> map) {
1820
this.target = target;
1921
this.map = map;
2022
nodes = new Node[map.getHeight() * map.getWidth()];

src/main/java/org/bk/ass/path/Jps.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
package org.bk.ass.path;
22

3+
import org.bk.ass.grid.Grid;
4+
35
import java.util.Collections;
46

57
/**
6-
* Jump point search. Initialize with a {@link Map} instance and call {@link #findPath(Position,
8+
* Jump point search. Initialize with a {@link Grid} instance and call {@link #findPath(Position,
79
* Position)}.
810
*/
911
public class Jps {
1012

11-
private final Map map;
13+
private final Grid<Boolean> map;
1214

13-
public Jps(Map map) {
15+
public Jps(Grid<Boolean> map) {
1416
this.map = map;
1517
}
1618

@@ -29,9 +31,9 @@ public Result findPath(Position start, Position end) {
2931

3032
private static class PathFinder extends AbstractPathFinder {
3133

32-
private final Map map;
34+
private final Grid<Boolean> map;
3335

34-
protected PathFinder(Position target, Map map) {
36+
protected PathFinder(Position target, Grid<Boolean> map) {
3537
super(target, map);
3638
this.map = map;
3739
}

src/main/java/org/bk/ass/path/Map.java

Lines changed: 0 additions & 25 deletions
This file was deleted.

src/main/java/org/bk/ass/path/PPJps.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package org.bk.ass.path;
22

3+
import org.bk.ass.grid.Grid;
4+
35
import java.util.Collections;
46

57
/**
6-
* Jump point search. Initialize with a {@link Map} instance and call {@link #findPath(Position,
8+
* Jump point search. Initialize with a {@link PPMap} instance and call {@link #findPath(Position,
79
* Position)}. This implementation uses preprocessed maps to improve runtime performance (~40%).
810
* (Note: This is not JPS+, only verticals and horizontals are precomputed)
911
*/
@@ -15,7 +17,7 @@ public PPJps(PPMap map) {
1517
this.map = map;
1618
}
1719

18-
public PPJps(Map map) {
20+
public PPJps(Grid<Boolean> map) {
1921
this(PPMap.fromMap(map));
2022
}
2123

0 commit comments

Comments
 (0)