Skip to content

Commit 023d08c

Browse files
committed
Merge branch 'dumpgamestate' into develop
2 parents 4fc48a6 + 14c270e commit 023d08c

24 files changed

+193
-74
lines changed

src/main/java/bwapi/Client.java

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ public interface EventHandler {
5252
private static final int SUPPORTED_BWAPI_VERSION = 10003;
5353
static final int MAX_COUNT = 19999;
5454

55-
private ClientData.GameData data;
55+
private ClientData clientData;
56+
private ClientData.GameData gameData;
5657
private boolean connected = false;
5758
private RandomAccessFile pipeObjectHandle = null;
5859
private ByteBuffer mapFileHandle = null;
@@ -64,11 +65,16 @@ public interface EventHandler {
6465
* For test purposes only
6566
*/
6667
Client(ByteBuffer buffer) {
67-
data = new ClientData(buffer).new GameData(0);
68+
clientData = new ClientData(buffer);
69+
gameData = clientData.new GameData(0);
6870
}
6971

70-
public GameData data() {
71-
return data;
72+
ClientData clientData() {
73+
return clientData;
74+
}
75+
76+
GameData gameData() {
77+
return gameData;
7278
}
7379

7480
boolean isConnected() {
@@ -98,7 +104,7 @@ void disconnect() {
98104

99105
mapFileHandle = null;
100106
gameTableFileHandle = null;
101-
data = null;
107+
gameData = null;
102108
connected = false;
103109
}
104110

@@ -176,17 +182,18 @@ boolean connect() {
176182
return false;
177183
}
178184
try {
179-
data = new ClientData(mapFileHandle).new GameData(0);
185+
clientData = new ClientData(mapFileHandle);
186+
gameData = clientData.new GameData(0);
180187
}
181188
catch (Exception e) {
182189
System.err.println("Unable to map game data.");
183190
return false;
184191
}
185192

186-
if (SUPPORTED_BWAPI_VERSION != data.getClient_version()) {
193+
if (SUPPORTED_BWAPI_VERSION != gameData.getClient_version()) {
187194
System.err.println("Error: Client and Server are not compatible!");
188195
System.err.println("Client version: " + SUPPORTED_BWAPI_VERSION);
189-
System.err.println("Server version: " + data.getClient_version());
196+
System.err.println("Server version: " + gameData.getClient_version());
190197
disconnect();
191198
sleep(2000);
192199
return false;
@@ -228,42 +235,42 @@ void update(final EventHandler handler) {
228235
return;
229236
}
230237
}
231-
for (int i = 0; i < data.getEventCount(); i++) {
232-
handler.operation(data.getEvents(i));
238+
for (int i = 0; i < gameData.getEventCount(); i++) {
239+
handler.operation(gameData.getEvents(i));
233240
}
234241
}
235242

236243
String eventString(final int s) {
237-
return data.getEventStrings(s);
244+
return gameData.getEventStrings(s);
238245
}
239246

240247
int addString(final String s) {
241-
int stringCount = data.getStringCount();
248+
int stringCount = gameData.getStringCount();
242249
if (stringCount >= MAX_COUNT) throw new IllegalStateException("Too many strings!");
243-
data.setStringCount(stringCount + 1);
244-
data.setStrings(stringCount, s);
250+
gameData.setStringCount(stringCount + 1);
251+
gameData.setStrings(stringCount, s);
245252
return stringCount;
246253
}
247254

248255
Shape addShape() {
249-
int shapeCount = data.getShapeCount();
256+
int shapeCount = gameData.getShapeCount();
250257
if (shapeCount >= MAX_COUNT) throw new IllegalStateException("Too many shapes!");
251-
data.setShapeCount(shapeCount + 1);
252-
return data.getShapes(shapeCount);
258+
gameData.setShapeCount(shapeCount + 1);
259+
return gameData.getShapes(shapeCount);
253260
}
254261

255262
Command addCommand() {
256-
final int commandCount = data.getCommandCount();
263+
final int commandCount = gameData.getCommandCount();
257264
if (commandCount >= MAX_COUNT) throw new IllegalStateException("Too many commands!");
258-
data.setCommandCount(commandCount + 1);
259-
return data.getCommands(commandCount);
265+
gameData.setCommandCount(commandCount + 1);
266+
return gameData.getCommands(commandCount);
260267
}
261268

262269
ClientData.UnitCommand addUnitCommand() {
263-
int unitCommandCount = data.getUnitCommandCount();
270+
int unitCommandCount = gameData.getUnitCommandCount();
264271
if (unitCommandCount >= MAX_COUNT) throw new IllegalStateException("Too many unit commands!");
265-
data.setUnitCommandCount(unitCommandCount + 1);
266-
return data.getUnitCommands(unitCommandCount);
272+
gameData.setUnitCommandCount(unitCommandCount + 1);
273+
return gameData.getUnitCommands(unitCommandCount);
267274
}
268275

269276
private void sleep(final int millis) {

src/main/java/bwapi/ClientData.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package bwapi;
22
import java.nio.ByteBuffer;
33
final class ClientData {
4-
private final WrappedBuffer buffer;
4+
final WrappedBuffer buffer;
55
ClientData(final ByteBuffer buffer) {
66
this.buffer = new WrappedBuffer(buffer);
77
}

src/main/java/bwapi/Game.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,11 @@ public class Game {
103103

104104
Game(Client client) {
105105
this.client = client;
106-
this.gameData = client.data();
106+
this.gameData = client.gameData();
107+
}
108+
109+
Client getClient() {
110+
return client;
107111
}
108112

109113
private static boolean hasPower(final int x, final int y, final UnitType unitType, final List<Unit> pylons) {

src/main/java/bwapi/WrappedBuffer.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,4 +86,8 @@ void putString(final int offset, final int maxLen, final String string) {
8686
}
8787
unsafe.putByte(pos, (byte) 0);
8888
}
89+
90+
ByteBuffer getBuffer() {
91+
return buffer;
92+
}
8993
}

src/test/java/DumpToClient.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public static void main(String[] args) throws IOException {
9292
out.println("package bwapi;");
9393
out.println("import java.nio.ByteBuffer;");
9494
out.println("final class ClientData {");
95-
out.println(" private final WrappedBuffer buffer;");
95+
out.println(" final WrappedBuffer buffer;");
9696
out.println(" ClientData(final ByteBuffer buffer) {");
9797
out.println(" this.buffer = new WrappedBuffer(buffer);");
9898
out.println(" }");

src/test/java/bwapi/BWEMTest.java

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package bwapi;
2+
3+
import bwem.BWEM;
4+
import bwem.BWMap;
5+
import org.junit.Test;
6+
7+
import java.io.ByteArrayOutputStream;
8+
import java.io.IOException;
9+
import java.nio.ByteBuffer;
10+
import java.nio.file.Files;
11+
import java.nio.file.Paths;
12+
import java.util.HashMap;
13+
import java.util.Map;
14+
import java.util.zip.InflaterOutputStream;
15+
16+
import static org.junit.Assert.assertEquals;
17+
18+
public class BWEMTest {
19+
static class BWEMMap{
20+
int areas;
21+
int bases;
22+
int chokes;
23+
24+
BWEMMap(BWMap bwMap) {
25+
this(bwMap.getAreas().size(), bwMap.getBases().size(), bwMap.getChokePoints().size());
26+
}
27+
28+
BWEMMap(int areas, int bases, int chokes) {
29+
this.areas = areas;
30+
this.bases = bases;
31+
this.chokes = chokes;
32+
}
33+
34+
public boolean equals(Object object) {
35+
BWEMMap bwemMap = (BWEMMap) object;
36+
return areas == bwemMap.areas && bases == bwemMap.bases && chokes == bwemMap.chokes;
37+
}
38+
}
39+
40+
static Map<String, BWEMMap> mapData = new HashMap<>();
41+
static {
42+
mapData.put("(2)Benzene.scx", new BWEMMap(20, 12, 29));
43+
mapData.put("(2)Destination.scx", new BWEMMap(16, 12, 25));
44+
mapData.put("(2)Heartbreak Ridge.scx", new BWEMMap(23, 11, 32));
45+
mapData.put("(3)Neo Moon Glaive.scx", new BWEMMap(28, 12, 35));
46+
mapData.put("(3)Tau Cross.scx", new BWEMMap(21, 12, 21));
47+
mapData.put("(4)Andromeda.scx", new BWEMMap(30, 17, 33));
48+
mapData.put("(4)Circuit Breaker.scx", new BWEMMap(31, 16, 40));
49+
mapData.put("(4)Electric Circuit.scx", new BWEMMap(37, 16, 52));
50+
mapData.put("(4)Empire of the Sun.scm", new BWEMMap(33, 14, 40));
51+
mapData.put("(4)Fighting Spirit.scx", new BWEMMap(25, 13, 29));
52+
mapData.put("(4)Icarus.scm", new BWEMMap(24, 12, 23));
53+
mapData.put("(4)Jade.scx", new BWEMMap(26, 12, 33));
54+
mapData.put("(4)La Mancha1.1.scx", new BWEMMap(22, 12, 26));
55+
mapData.put("(4)Python.scx", new BWEMMap(13, 14, 10));
56+
mapData.put("(4)Roadrunner.scx", new BWEMMap(26, 12, 35));
57+
}
58+
59+
60+
Game initGame(String mapName) throws IOException {
61+
String location = "src/test/resources/" + mapName + "_frame0_buffer.bin";
62+
63+
// load bytebuffer
64+
byte[] compressedBytes = Files.readAllBytes(Paths.get(location));
65+
ByteArrayOutputStream out = new ByteArrayOutputStream();
66+
InflaterOutputStream zin = new InflaterOutputStream(out);
67+
zin.write(compressedBytes);
68+
zin.flush();
69+
zin.close();
70+
byte[] bytes = out.toByteArray();
71+
ByteBuffer buffer = ByteBuffer.allocateDirect(bytes.length);
72+
buffer.put(bytes);
73+
74+
Client client = new Client(buffer);
75+
Game game = new Game(client);
76+
game.init();
77+
return game;
78+
}
79+
80+
@Test
81+
public void checkSSCAITMaps() throws IOException {
82+
for (String mapName : mapData.keySet()) {
83+
Game game = initGame(mapName);
84+
BWEM bwem = new BWEM(game);
85+
bwem.initialize();
86+
assertEquals(new BWEMMap(bwem.getMap()), mapData.get(mapName));
87+
}
88+
}
89+
}

src/test/java/bwapi/ClientDataBenchmark.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public static class FilledWithStrings {
3535
@Setup(Level.Invocation)
3636
public void setup() {
3737
client = new Client(ByteBuffer.allocateDirect(ClientData.GameData.SIZE));
38-
data = client.data();
38+
data = client.gameData();
3939
game = new Game(client);
4040
String[] strings = buildStrings();
4141
for (String s : strings) {
@@ -69,7 +69,7 @@ public int addUnitCommand(EmptyState s) {
6969
for (int i = 0; i < Client.MAX_COUNT; i++) {
7070
s.game.addUnitCommand(0, 1, 2, 3, 4, 5);
7171
}
72-
return s.client.data().getCommandCount();
72+
return s.client.gameData().getCommandCount();
7373
}
7474

7575
@Benchmark
@@ -78,7 +78,7 @@ public int addString(EmptyState s) {
7878
for (int i = 0; i < Client.MAX_COUNT; i++) {
7979
s.client.addString(s.strings[i]);
8080
}
81-
return s.client.data().getStringCount();
81+
return s.client.gameData().getStringCount();
8282
}
8383

8484
@Benchmark
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package bwapi;
2+
3+
import bwem.BWEM;
4+
import bwem.BWMap;
5+
6+
import java.io.ByteArrayOutputStream;
7+
import java.io.File;
8+
import java.io.FileOutputStream;
9+
import java.io.IOException;
10+
import java.nio.ByteBuffer;
11+
import java.util.zip.DeflaterOutputStream;
12+
13+
public class GameStateDumper extends DefaultBWListener{
14+
static BWClient client;
15+
16+
Game game;
17+
String name;
18+
19+
@Override
20+
public void onStart() {
21+
game = client.getGame();
22+
name = "src/test/resources/" + game.mapFileName();
23+
24+
try {
25+
dumpBuffer(name + "_frame" + game.getFrameCount());
26+
} catch (IOException e) {
27+
e.printStackTrace();
28+
}
29+
BWEM bwem = new BWEM(game);
30+
bwem.initialize();
31+
BWMap map = bwem.getMap();
32+
System.out.println("mapData.put(\"" + game.mapFileName() + "\", new BWEMMap("
33+
+ map.getAreas().size() + ", "
34+
+ map.getBases().size() + ", "
35+
+ map.getChokePoints().size() + "));");
36+
}
37+
38+
private void dumpBuffer(String name) throws IOException {
39+
ByteBuffer buf = game.getClient().clientData().buffer.getBuffer();
40+
buf.rewind();
41+
byte[] bytearr = new byte[buf.remaining()];
42+
buf.get(bytearr);
43+
44+
ByteArrayOutputStream out = new ByteArrayOutputStream();
45+
DeflaterOutputStream zout = new DeflaterOutputStream(out);
46+
zout.write(bytearr);
47+
zout.flush();
48+
zout.close();
49+
byte[] compressed = out.toByteArray();
50+
File file = new File(name +"_buffer.bin");
51+
FileOutputStream fos = new FileOutputStream(file, false);
52+
fos.write(compressed);
53+
fos.close();
54+
}
55+
56+
public static void main(String[] args) {
57+
client = new BWClient(new GameStateDumper());
58+
client.startGame();
59+
}
60+
}

src/test/java/other/BWEMAvailabilityTest.java

Lines changed: 0 additions & 45 deletions
This file was deleted.
76.4 KB
Binary file not shown.

0 commit comments

Comments
 (0)