Skip to content

Commit

Permalink
Updated BWEnv controller to handle FlatBuffer-serialized frames. Adde…
Browse files Browse the repository at this point in the history
…d README describing serialization tests.
  • Loading branch information
dgant committed Nov 21, 2017
1 parent 303a0e3 commit fd7ba83
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 26 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

/build.luarocks/
/build/
/BWEnv/build/

## VisualStudio ###########

/BWEnv/build/
# User-specific files
*.suo
*.user
Expand Down
4 changes: 2 additions & 2 deletions BWEnv/include/controller.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ class Controller {
bool is_client;

private:
void serializeFrameData(torchcraft::fbs::FrameDataT*);
void serializeFrameData(torchcraft::fbs::FrameOrFrameDiffUnion& frameOrFrameDiff);
std::unique_ptr<ConfigManager> config_;
bool sent_battle_end_frame = false;
bool game_ended = false;
Expand All @@ -161,7 +161,7 @@ class Controller {
bool blocking_ = true;
int max_frame_time_ms_ = 50;
std::vector<std::pair<std::vector<int>, std::string>> draw_cmds_;
torchcraft::fbs::FrameT tcframe_;
torchcraft::fbs::StateUpdateT tcframe_;
};

#endif // TORCHCRAFT_CONTROL_H_
2 changes: 1 addition & 1 deletion BWEnv/include/zmq_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class ZMQ_server
void connect();
void close();
void sendHandshake(const torchcraft::fbs::HandshakeServerT* handshake);
void sendFrame(const torchcraft::fbs::FrameT* frame);
void sendFrame(const torchcraft::fbs::StateUpdateT* stateUpdate);
void sendPlayerLeft(const torchcraft::fbs::PlayerLeftT *pl);
void sendEndGame(const torchcraft::fbs::EndGameT *endgame);
void sendError(const torchcraft::fbs::ErrorT *error);
Expand Down
39 changes: 20 additions & 19 deletions BWEnv/src/controller.cc
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,7 @@ int Controller::getAttackFrames(int unitID) {
int unitType = BWAPI::Broodwar->getUnit(unitID)->getType().getID();
// From
// https://docs.google.com/spreadsheets/d/1bsvPvFil-kpvEUfSG74U3E5PLSTC02JxSkiR8QdLMuw/edit#gid=0
// Photon Cannons may also have a value.
if (unitType == BWAPI::UnitTypes::Enum::Protoss_Dragoon) {
attackFrames += 5;
} else if (unitType == BWAPI::UnitTypes::Enum::Zerg_Devourer) {
Expand All @@ -586,31 +587,34 @@ int Controller::getAttackFrames(int unitID) {
return attackFrames;
}

void Controller::serializeFrameData(torchcraft::fbs::FrameDataT* data) {
std::ostringstream out;
if (prev_sent_frame == nullptr) {
out << *last_frame;
data->is_diff = false;
} else {
out << replayer::frame_diff(last_frame, prev_sent_frame);
data->is_diff = true;
void Controller::serializeFrameData(torchcraft::fbs::FrameOrFrameDiffUnion& frameOrFrameDiff) {
{
flatbuffers::FlatBufferBuilder builder;
if (prev_sent_frame == nullptr) {
frameOrFrameDiff.type = torchcraft::fbs::FrameOrFrameDiff::Frame;
last_frame->addToFlatBufferBuilder(builder);
} else {
frameOrFrameDiff.type = torchcraft::fbs::FrameOrFrameDiff::FrameDiff;
auto frameDiff = replayer::frame_diff(last_frame, prev_sent_frame);
frameDiff.addToFlatBufferBuilder(builder);
}
frameOrFrameDiff.value = builder.GetBufferPointer();
}
if (prev_sent_frame != nullptr)
prev_sent_frame->decref();

if (prev_sent_frame) { prev_sent_frame->decref(); }
prev_sent_frame = last_frame;
last_frame = nullptr;
auto s = out.str();
data->data.assign(s.data(), s.data() + s.size());
}

void Controller::endGame() {
Utils::bwlog(
output_log, "Game ended (%s)", (this->is_winner ? "WON" : "LOST"));

torchcraft::fbs::EndGameT endg;
endg.data.reset(new torchcraft::fbs::FrameDataT());
if (last_frame != nullptr)
this->serializeFrameData(endg.data.get());
endg.data.Reset();
if (last_frame != nullptr) {
this->serializeFrameData(endg.data);
}
endg.game_won = this->is_winner;

clearPendingReceive();
Expand Down Expand Up @@ -806,10 +810,7 @@ void Controller::onFrame() {
}
}

if (!this->tcframe_.data)
this->tcframe_.data.reset(new torchcraft::fbs::FrameDataT());
this->serializeFrameData(this->tcframe_.data.get());

this->serializeFrameData(this->tcframe_.data);
this->tcframe_.deaths = this->deaths;
this->deaths.clear();

Expand Down
5 changes: 4 additions & 1 deletion BWEnv/src/utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -245,8 +245,11 @@ std::string readIni(const std::string& filename, const std::string& section, con
long filesize = ftell(f);
data.resize(filesize);
fseek(f, 0, SEEK_SET);
fread(data.data(), filesize, 1, f);
auto elementsRead = fread(data.data(), filesize, 1, f);
fclose(f);
if (!elementsRead) {
return {};
}
bool correct_section = section.empty();
const char* c = data.data();
const char* e = c + data.size();
Expand Down
4 changes: 2 additions & 2 deletions BWEnv/src/zmq_server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,8 @@ void ZMQ_server::sendHandshake(const torchcraft::fbs::HandshakeServerT* handshak
sendFBObject(this->sock.get(), handshake);
}

void ZMQ_server::sendFrame(const torchcraft::fbs::FrameT* frame) {
sendFBObject(this->sock.get(), frame);
void ZMQ_server::sendFrame(const torchcraft::fbs::StateUpdateT* stateUpdate) {
sendFBObject(this->sock.get(), stateUpdate);
}

void ZMQ_server::sendPlayerLeft(const torchcraft::fbs::PlayerLeftT* pl) {
Expand Down
5 changes: 5 additions & 0 deletions replayer/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Replayer

### Testing

You can test serialization by running the TorchCraft tests via "make test" in directory where you've constructed Makefiles via CMake.

0 comments on commit fd7ba83

Please sign in to comment.