Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/main/java/bwapi/BotWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,17 @@ void endGame() { }

protected void handleEvents() {
ClientData.GameData botGameData = botGame.botClientData().gameData();
final int eventCount = botGameData.getEventCount();

// Populate gameOver before invoking event handlers (in case the bot throws)
for (int i = 0; i < botGameData.getEventCount(); i++) {
for (int i = 0; i < eventCount; i++) {
gameOver = gameOver || botGameData.getEvents(i).getType() == EventType.MatchEnd;
}

performanceMetrics.getBotResponse().timeIf(
! gameOver && (botGameData.getFrameCount() > 0 || ! configuration.getUnlimitedFrameZero()),
() -> {
for (int i = 0; i < botGameData.getEventCount(); i++) {
for (int i = 0; i < eventCount; i++) {
EventHandler.operation(eventListener, botGame, botGameData.getEvents(i));
}
});
Expand Down
99 changes: 30 additions & 69 deletions src/main/java/bwapi/BotWrapperAsync.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package bwapi;

import java.util.concurrent.locks.ReentrantLock;

/**
* Manages invocation of bot event handlers
*/
Expand All @@ -10,9 +8,7 @@ class BotWrapperAsync extends BotWrapper {
private final FrameBuffer frameBuffer;
private WrappedBuffer liveData;
private Thread botThread;
private Throwable lastBotThrow;
private final ReentrantLock lastBotThrowLock = new ReentrantLock();
private final ReentrantLock unsafeReadReadyLock = new ReentrantLock();
private volatile Throwable lastBotThrow;
private boolean unsafeReadReady = false;

BotWrapperAsync(BWClientConfiguration configuration, BWEventListener eventListener) {
Expand All @@ -28,8 +24,11 @@ class BotWrapperAsync extends BotWrapper {
@Override
void startNewGame(WrappedBuffer liveData, PerformanceMetrics performanceMetrics) {
frameBuffer.initialize(liveData, performanceMetrics);

super.startNewGame(liveData, performanceMetrics);
this.performanceMetrics = performanceMetrics;
botGame = new Game(new ClientDataAsync(liveData));
botGame.setConfiguration(configuration);
botGame.botClientData().setBuffer(liveData);
gameOver = false;

liveClientData.setBuffer(liveData);
this.liveData = liveData;
Expand All @@ -43,32 +42,6 @@ void startNewGame(WrappedBuffer liveData, PerformanceMetrics performanceMetrics)
}


private boolean isUnsafeReadReady() {
unsafeReadReadyLock.lock();
try {
return unsafeReadReady;
}
finally {
unsafeReadReadyLock.unlock();
}
}

private void setUnsafeReadReady(boolean value) {
unsafeReadReadyLock.lock();
try {
unsafeReadReady = value;
}
finally {
unsafeReadReadyLock.unlock();
}
frameBuffer.lockSize.lock();
try {
frameBuffer.conditionSize.signalAll();
} finally {
frameBuffer.lockSize.unlock();
}
}

@Override
void onFrame() {
configuration.log("Main: onFrame asynchronous start");
Expand All @@ -89,9 +62,10 @@ void asyncOnFrame() {
if (frameBuffer.empty()) {
configuration.log("Main: Putting bot on live data");
botGame.botClientData().setBuffer(liveData);
setUnsafeReadReady(true);
unsafeReadReady = true;
frameBuffer.conditionSize.signalAll();
} else {
setUnsafeReadReady(false);
unsafeReadReady = false;
}
} finally {
frameBuffer.lockSize.unlock();
Expand All @@ -111,19 +85,19 @@ void asyncOnFrame() {
}
frameBuffer.lockSize.lock();
try {
while (!frameBuffer.empty()) {
// Unsafe mode: Move the bot off of live data onto the frame buffer
// This is the unsafe step!
// We don't synchronize on calls which access the buffer
// (to avoid tens of thousands of synchronized calls per frame)
// so there's no guarantee of safety here.
if (configuration.getAsyncUnsafe() && frameBuffer.size() == 1) {
configuration.log("Main: Weaning bot off live data");
botGame.botClientData().setBuffer(frameBuffer.peek());
}
// Unsafe mode: Move the bot off of live data onto the frame buffer
// as soon as the copy completes (~0.05-0.35ms) so any cached buffer reference
// has the remainder of the frame while liveData is still frozen to transition
// before control returns to StarCraft.
if (configuration.getAsyncUnsafe() && frameBuffer.size() == 1) {
configuration.log("Main: Weaning bot off live data");
unsafeReadReady = false;
botGame.botClientData().setBuffer(frameBuffer.peek());
}

while (!frameBuffer.empty()) {
// Make bot exceptions fall through to the main thread.
Throwable lastThrow = getLastBotThrow();
Throwable lastThrow = lastBotThrow;
if (lastThrow != null) {
configuration.log("Main: Rethrowing bot throwable");
throw new RuntimeException(lastThrow);
Expand Down Expand Up @@ -163,42 +137,31 @@ void endGame() {
}
}

Throwable getLastBotThrow() {
lastBotThrowLock.lock();
Throwable output = lastBotThrow;
lastBotThrowLock.unlock();
return output;
}

private Thread createBotThread() {
return new Thread(() -> {
try {
configuration.log("Bot: Thread started");
while (!gameOver) {

boolean doUnsafeRead = false;
configuration.log("Bot: Ready for another frame");
performanceMetrics.getBotIdle().startTiming();
frameBuffer.lockSize.lock();
try {
doUnsafeRead = isUnsafeReadReady();
while ( ! doUnsafeRead && frameBuffer.empty()) {
while (!unsafeReadReady && frameBuffer.empty()) {
configuration.log("Bot: Waiting for a frame");
frameBuffer.conditionSize.awaitUninterruptibly();
doUnsafeRead = isUnsafeReadReady();
}
performanceMetrics.getBotIdle().stopTiming();

if (unsafeReadReady) {
configuration.log("Bot: Reading live frame");
unsafeReadReady = false;
} else {
configuration.log("Bot: Peeking next frame from buffer");
botGame.botClientData().setBuffer(frameBuffer.peek());
}
} finally {
frameBuffer.lockSize.unlock();
}
performanceMetrics.getBotIdle().stopTiming();

if (doUnsafeRead) {
configuration.log("Bot: Reading live frame");
setUnsafeReadReady(false);
} else {
configuration.log("Bot: Peeking next frame from buffer");
botGame.botClientData().setBuffer(frameBuffer.peek());
}

configuration.log("Bot: Handling events on frame #" + botGame.getFrameCount());
handleEvents();
Expand All @@ -210,9 +173,7 @@ private Thread createBotThread() {
// Record the throw,
// Then allow the thread to terminate silently.
// The main thread will look for the stored throw.
lastBotThrowLock.lock();
lastBotThrow = throwable;
lastBotThrowLock.unlock();

// Awaken any threads waiting on bot progress
while (!frameBuffer.empty()) {
Expand Down
17 changes: 10 additions & 7 deletions src/main/java/bwapi/ClientData.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@

import com.sun.jna.Pointer;

final class ClientData {
private WrappedBuffer buffer;
class ClientData {
WrappedBuffer buffer;
private GameData gameData;
ClientData() {
gameData = new ClientData.GameData(0);
gameData = createGameData(0);
}
GameData createGameData(int offset) {
return new ClientData.GameData(offset);
}
GameData gameData() {
return gameData;
Expand Down Expand Up @@ -68,7 +71,7 @@ void setExtra(int value) {
}
class GameData {
static final int SIZE = 33017048;
private int myOffset;
protected int myOffset;
GameData(int myOffset) {
this.myOffset = myOffset;
}
Expand Down Expand Up @@ -729,7 +732,7 @@ void setValue2(int value) {
}
class Position {
static final int SIZE = 8;
private int myOffset;
protected int myOffset;
Position(int myOffset) {
this.myOffset = myOffset;
}
Expand All @@ -750,7 +753,7 @@ void setY(int value) {
}
class Event {
static final int SIZE = 12;
private int myOffset;
protected int myOffset;
Event(int myOffset) {
this.myOffset = myOffset;
}
Expand Down Expand Up @@ -778,7 +781,7 @@ void setV2(int value) {
}
class RegionData {
static final int SIZE = 1068;
private int myOffset;
protected int myOffset;
RegionData(int myOffset) {
this.myOffset = myOffset;
}
Expand Down
Loading
Loading