Skip to content

Commit c30289b

Browse files
committed
Fixed bug in which BotWrapper was looking at bot data to determine the live frame
1 parent 4be2706 commit c30289b

File tree

8 files changed

+29
-23
lines changed

8 files changed

+29
-23
lines changed

src/main/java/bwapi/BotWrapper.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,14 @@ class BotWrapper {
5252
/**
5353
* Resets the BotWrapper for a new game.
5454
*/
55-
void startNewGame(ByteBuffer dataSource, PerformanceMetrics performanceMetrics) {
55+
void startNewGame(ByteBuffer liveData, PerformanceMetrics performanceMetrics) {
5656
if (configuration.async) {
57-
frameBuffer.initialize(dataSource, performanceMetrics);
57+
frameBuffer.initialize(liveData, performanceMetrics);
5858
}
5959
this.performanceMetrics = performanceMetrics;
60-
game = new Game(liveClientData);
61-
liveClientData.setBuffer(dataSource);
60+
game = new Game();
61+
game.clientData().setBuffer(liveData);
62+
liveClientData.setBuffer(liveData);
6263
botThread = null;
6364
gameOver = false;
6465
}
@@ -90,8 +91,8 @@ void onFrame() {
9091
If buffer is full, it will wait until it has capacity
9192
Wait for empty buffer OR termination condition
9293
*/
93-
configuration.log("Main: Enqueuing frame");
94-
boolean isFrameZero = liveClientData.gameData().getFrameCount() == 0;
94+
int frame = liveClientData.gameData().getFrameCount();
95+
configuration.log("Main: Enqueuing frame #" + frame);
9596
frameBuffer.enqueueFrame();
9697
performanceMetrics.bwapiResponse.startTiming();
9798
frameBuffer.lockSize.lock();
@@ -106,8 +107,8 @@ void onFrame() {
106107
throw new RuntimeException(lastThrow);
107108
}
108109

109-
if (configuration.unlimitedFrameZero && isFrameZero) {
110-
configuration.log("Main: Waiting indefinitely on frame 0");
110+
if (configuration.unlimitedFrameZero && frame == 0) {
111+
configuration.log("Main: Waiting indefinitely on frame " + frame);
111112
frameBuffer.conditionSize.await();
112113
} else {
113114
long remainingNanos = endNanos - System.nanoTime();

src/main/java/bwapi/ClientData.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ class ClientData {
44
private WrappedBuffer buffer;
55
private GameData gameData;
66
ClientData() {
7+
System.out.println("new ClientData()"); // TODO: REMOVE!
78
gameData = new GameData(0);
89
}
910
GameData gameData() {

src/main/java/bwapi/FrameBuffer.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ of this software and associated documentation files (the "Software"), to deal
3939
class FrameBuffer {
4040
private static final int BUFFER_SIZE = ClientData.GameData.SIZE;
4141

42-
private ByteBuffer dataSource;
42+
private ByteBuffer liveData;
4343
private PerformanceMetrics performanceMetrics;
4444
private int size;
4545
private int stepGame = 0;
@@ -61,8 +61,8 @@ class FrameBuffer {
6161
/**
6262
* Resets for a new game
6363
*/
64-
void initialize(ByteBuffer dataSource, PerformanceMetrics performanceMetrics) {
65-
this.dataSource = dataSource;
64+
void initialize(ByteBuffer liveData, PerformanceMetrics performanceMetrics) {
65+
this.liveData = liveData;
6666
this.performanceMetrics = performanceMetrics;
6767
stepGame = 0;
6868
stepBot = 0;
@@ -125,7 +125,7 @@ void enqueueFrame() {
125125

126126
performanceMetrics.copyingToBuffer.time(() -> {
127127
ByteBuffer dataTarget = dataBuffer.get(indexGame());
128-
copyBuffer(dataSource, dataTarget);
128+
copyBuffer(liveData, dataTarget);
129129
});
130130

131131
lockSize.lock();
@@ -190,6 +190,6 @@ void copyBuffer(ByteBuffer source, ByteBuffer destination) {
190190
// and serves to document the known-good (and cross-platform, for BWAPI 5) way to executing the copy.
191191
source.rewind();
192192
destination.rewind();
193-
destination.put(dataSource);
193+
destination.put(liveData);
194194
}
195195
}

src/main/java/bwapi/Game.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,8 @@ public class Game {
101101

102102
public final SideEffectQueue sideEffects = new SideEffectQueue();
103103

104-
Game(ClientData clientData) {
105-
this.clientData = clientData;
104+
Game() {
105+
clientData = new ClientData();
106106
}
107107

108108
ClientData clientData() {
@@ -142,6 +142,7 @@ private static boolean hasPower(final int x, final int y, final UnitType unitTyp
142142
Call this method in EventHander::OnMatchStart
143143
*/
144144
void init() {
145+
System.out.println("Game.init()"); // TODO: REMOVE!
145146
visibleUnits.clear();
146147

147148
final int forceCount = gameData().getForceCount();

src/test/java/bwapi/ClientDataBenchmark.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ public static class EmptyState {
2020
@Setup(Level.Invocation)
2121
public void setup() {
2222
client = new Client(ByteBuffer.allocateDirect(ClientData.GameData.SIZE));
23-
game = new Game(client.clientData());
23+
game = new Game();
24+
game.clientData().setBuffer(client.mapFile());
2425
strings = buildStrings();
2526
}
2627

@@ -36,7 +37,8 @@ public static class FilledWithStrings {
3637
public void setup() {
3738
client = new Client(ByteBuffer.allocateDirect(ClientData.GameData.SIZE));
3839
data = client.clientData().gameData();
39-
game = new Game(client.clientData());
40+
game = new Game();
41+
game.clientData().setBuffer(client.mapFile());
4042
String[] strings = buildStrings();
4143
for (String s : strings) {
4244
GameDataUtils.addString(client.clientData().gameData(), s);

src/test/java/bwapi/GameBuilder.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ public static Game createGame(String mapName) throws IOException {
2323
}
2424

2525
public static Game createGame(Client client) {
26-
final Game game = new Game(client.clientData());
26+
final Game game = new Game();
27+
game.clientData().setBuffer(client.mapFile());
2728
game.init();
2829
return game;
2930
}

src/test/java/bwapi/GameTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
public class GameTest {
2222

2323
private List<Unit> allUnits = new ArrayList<>();
24-
private Game sut = new Game(mock(ClientData.class)) {
24+
private Game sut = new Game() {
2525
@Override
2626
public List<Unit> getAllUnits() {
2727
return allUnits;

src/test/java/bwapi/SynchronizationEnvironment.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,15 @@ public class SynchronizationEnvironment {
4343
return null;
4444
}).when(client).update();
4545
doAnswer(answer -> {
46-
System.out.println("onStart()");
46+
System.out.println("Test: onStart()");
4747
return null;
4848
}).when(listener).onStart();
4949
doAnswer(answer -> {
50-
System.out.println("onEnd()");
50+
System.out.println("Test: onEnd()");
5151
return null;
5252
}).when(listener).onEnd(anyBoolean());
5353
doAnswer(answer -> {
54-
System.out.println("onFrame()");
54+
System.out.println("Test: onFrame()");
5555
if (onFrames.containsKey(liveFrame())) {
5656
onFrames.get(liveFrame()).run();
5757
}
@@ -92,7 +92,7 @@ private int liveFrame() {
9292

9393
private void clientUpdate() {
9494
client.clientData().gameData().setFrameCount(liveFrame() + 1);
95-
System.out.println("clientUpdate() to liveFrame #" + liveFrame());
95+
System.out.println("Test: clientUpdate() to liveFrame #" + liveFrame());
9696
if (liveFrame() == 0) {
9797
client.clientData().gameData().setIsInGame(true);
9898
client.clientData().gameData().setEventCount(2);

0 commit comments

Comments
 (0)