Skip to content

Commit 30e7a52

Browse files
committed
Implemented frame timeouts.
1 parent 341056d commit 30e7a52

File tree

3 files changed

+17
-5
lines changed

3 files changed

+17
-5
lines changed

src/main/java/bwapi/BWClient.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ public void startGame(boolean autoContinue) {
4747
public void startGame(BWClientConfiguration configuration) {
4848
configuration.validate();
4949
botWrapper = new BotWrapper(configuration, eventListener);
50+
5051
Client client = new Client(configuration);
5152
client.reconnect();
5253

src/main/java/bwapi/BWClientConfiguration.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ public class BWClientConfiguration {
2929
public boolean async = false;
3030

3131
/**
32-
* If JBWAPI detects that this much time (in milliseconds) has passed since a bot's event handlers began, returns control back to BWAPI.
32+
* If JBWAPI detects that this much time (in nanoseconds) has passed since a bot's event handlers began, returns control back to BWAPI.
3333
* Real-time human play typically uses the "fastest" game speed, which has 42.86ms (42,860ns) between frames.
3434
*/
35-
public int asyncFrameDurationMillis = 40;
35+
public int asyncFrameDurationNanos = 40000;
3636

3737
/**
3838
* The maximum number of frames to buffer while waiting on a bot.
@@ -53,8 +53,8 @@ public class BWClientConfiguration {
5353
* Checks that the configuration is in a valid state. Throws an IllegalArgumentException if it isn't.
5454
*/
5555
public void validate() {
56-
if (async && asyncFrameDurationMillis < 0) {
57-
throw new IllegalArgumentException("asyncFrameDurationMillis needs to be a non-negative number (it's how long JBWAPI waits for a bot response before returning control to BWAPI).");
56+
if (async && asyncFrameDurationNanos < 0) {
57+
throw new IllegalArgumentException("asyncFrameDurationNanos needs to be a non-negative number (it's how long JBWAPI waits for a bot response before returning control to BWAPI).");
5858
}
5959
if (async && asyncFrameBufferSize < 1) {
6060
throw new IllegalArgumentException("asyncFrameBufferSize needs to be a positive number (There needs to be at least one frame buffer).");

src/main/java/bwapi/BotWrapper.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ Game getGame() {
6969
*/
7070
void onFrame() {
7171
if (configuration.async) {
72+
long startNanos = System.nanoTime();
73+
long endNanos = startNanos + configuration.asyncFrameDurationNanos;
7274
if (botThread == null) {
7375
System.out.println("Creating bot thread");
7476
botThread = createBotThread();
@@ -78,7 +80,16 @@ void onFrame() {
7880
frameBuffer.enqueueFrame();
7981
frameBuffer.lockSize.lock();
8082
try {
81-
while ( ! frameBuffer.empty()) frameBuffer.conditionSize.awaitUninterruptibly();
83+
while (frameBuffer.empty()) {
84+
if (configuration.asyncWaitOnFrameZero && liveClientData.gameData().getFrameCount() == 0) {
85+
frameBuffer.conditionSize.await();
86+
} else {
87+
long remainingNanos = endNanos - System.nanoTime();
88+
if (remainingNanos <= 0) break;
89+
frameBuffer.conditionSize.awaitNanos(remainingNanos);
90+
}
91+
}
92+
} catch(InterruptedException ignored) {
8293
} finally {
8394
frameBuffer.lockSize.unlock();
8495
}

0 commit comments

Comments
 (0)