Skip to content
This repository has been archived by the owner on Jul 26, 2022. It is now read-only.

Commit

Permalink
Merge pull request #81 from TorchCraft/develop
Browse files Browse the repository at this point in the history
Merge from develop
  • Loading branch information
ebetica committed Mar 13, 2017
2 parents 11cec68 + 8a37b6d commit c168246
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 43 deletions.
3 changes: 2 additions & 1 deletion BWEnv/include/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ void launchSCWithBWheadless(const std::wstring& sc_path_,
const std::wstring& tc_path_);
void launchSCWithInjectory(const std::wstring& sc_path_,
const std::wstring& tc_path);
void launchSCCustom(const std::wstring& command);
void launchSCCustom(const std::wstring& sc_path_,
const std::wstring& command);
void killStarCraft();

// map changing related stuff
Expand Down
5 changes: 4 additions & 1 deletion BWEnv/src/controller.cc
Original file line number Diff line number Diff line change
Expand Up @@ -918,7 +918,10 @@ void Controller::launchStarCraft()
}
else if (config_->launcher == "custom")
{
// TODO
std::string custom_launcher = config_->custom_launcher;
std::wstring command(custom_launcher.length(), L' ');
std::copy(custom_launcher.begin(), custom_launcher.end(), command.begin());
Utils::launchSCCustom(sc_path_, command);
}
else {
// TODO decide what to do here
Expand Down
5 changes: 3 additions & 2 deletions BWEnv/src/utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,10 @@ void Utils::launchSCWithInjectory(const std::wstring& sc_path_, const std::wstri
starcraftInject(command, sc_path_, true);
}

void Utils::launchSCCustom(const std::wstring& command)
void Utils::launchSCCustom(const std::wstring& sc_path_, const std::wstring& command)
{
// TODO
std::cout << std::string(command.begin(), command.end()) << std::endl;
starcraftInject(command, sc_path_, false);
}

void Utils::killStarCraft()
Expand Down
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ IF(APPLE)
LINK_FLAGS "-undefined dynamic_lookup")
ENDIF()
SET_TARGET_PROPERTIES(torchcraft PROPERTIES
VERSION 1.0.2
SOVERSION 1.0.2)
VERSION 1.1.1
SOVERSION 1.1.1)
TARGET_LINK_LIBRARIES(torchcraft TH luaT zmq)
INSTALL(FILES ${headers}
DESTINATION "${Torch_INSTALL_INCLUDE}/torchcraft"
Expand Down
1 change: 1 addition & 0 deletions client/connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ Connection::Connection(
ss << "tcp://" << hostname << ":" << port;
sock_.setsockopt(ZMQ_SNDTIMEO, &timeoutMs, sizeof(timeoutMs));
sock_.setsockopt(ZMQ_RCVTIMEO, &timeoutMs, sizeof(timeoutMs));
sock_.setsockopt(ZMQ_IPV6, 1);
sock_.connect(ss.str());
} // Connection

Expand Down
5 changes: 5 additions & 0 deletions docs/user/installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ Now you only need one of the following two methods, either AIModule (DLL) or AIC

> We recommend Git Bash for windows: https://git-for-windows.github.io/).
- Set the `BWAPI_DIR` environment variable:
- Right click on My Computer -> Properties ->
Advanced System Settings -> Environment Variables. Add `BWAPI_DIR` to be where
you installed it, likely something like `C:\StarCraft\BWAPI`.
- Restart the OS to apply the variable to the system.
- Open `$STARCRAFT/TorchCraft/BWEnv/VisualStudio/BWEnv.sln` and start hacking.
- Compile in `Release` mode for the AIClient (you will get a `BWEnv.exe` ) and in `DLL-Release` mode for the AIModule (you will get a `BWEnv.dll` ).

Expand Down
6 changes: 3 additions & 3 deletions init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -83,16 +83,16 @@ function torchcraft:connect(port, timeoutMs)

self.state = self.client.state

local ok, setup = pcall(self.client:init({
local ok, setup = pcall(function() return self.client:init({
initial_map = self.initial_map,
window_size = self.window_size,
window_pos = self.window_pos,
micro_battles = self.micro_battles,
only_consider_types = self.only_consider_types,
}))
}) end)
-- reset client's connection to leave TC object in a consistent state
if not ok then
local ok, err = pcall(self.client:close())
local ok, err = pcall(function() self.client:close() end)
if not ok then
error('Error closing connection on init failure:\n' ..
'init failure: ' .. setup .. ',\n' ..
Expand Down
69 changes: 37 additions & 32 deletions replayer/frame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ std::istream& replayer::operator>>(std::istream& in, replayer::Frame& o) {

namespace detail = replayer::detail;
// This macro maps the member variables of Unit to some IDs
#define _DOALL(F) \
#define _DOALL(F) \
F(x, 0) \
F(y, 1) \
F(health, 2) \
Expand Down Expand Up @@ -244,10 +244,10 @@ namespace detail = replayer::detail;
F(resources, 27)

#define _DOALL_ON_ORDER(F) \
F(first_frame, 0) \
F(type, 1) \
F(targetId, 2) \
F(targetX, 3) \
F(first_frame, 0) \
F(type, 1) \
F(targetId, 2) \
F(targetX, 3) \
F(targetY, 4)

replayer::FrameDiff replayer::frame_diff(
Expand All @@ -266,16 +266,16 @@ replayer::FrameDiff replayer::frame_diff(
df.actions = lhs->actions;
df.resources = lhs->resources;
for (auto it : lhs->units) { // Iterates across number of players
// We assume the number of players are the same.
// I think people never run out of units (or they lose)
df.pids.push_back(it.first);

// Set up the units list
df.units.emplace_back();
std::vector<detail::UnitDiff>& ul = df.units.back();

auto lhsu = it.second;
auto rhsu = rhs->units.at(it.first);
auto rhsu = rhs->units.find(it.first) == rhs->units.end()
? std::vector<replayer::Unit>()
: rhs->units.at(it.first);
std::sort(lhsu.begin(), lhsu.end(), detail::orderUnitByiD);
std::sort(rhsu.begin(), rhsu.end(), detail::orderUnitByiD);
auto rit = rhsu.begin();
Expand All @@ -292,7 +292,7 @@ replayer::FrameDiff replayer::frame_diff(
lit.id == rit->id) { // Unit exists in both frames
int32_t buffer = 0;
// Fill out diffs for the int32_t variables
#define _GEN_VAR(NAME, NUM) \
#define _GEN_VAR(NAME, NUM) \
buffer = lit.NAME - rit->NAME; \
if (buffer != 0) { \
du.var_ids.push_back(NUM); \
Expand All @@ -302,26 +302,28 @@ replayer::FrameDiff replayer::frame_diff(
#undef _GEN_VAR
// Fill out diffs for orders
for (size_t i = 0; i < lit.orders.size(); i++) {
#define _GEN_VAR(NAME, NUM) \
if (i >= rit->orders.size()) buffer = lit.orders[i].NAME; \
else buffer = lit.orders[i].NAME - rit->orders[i].NAME; \
if (buffer != 0) { \
du.order_ids.push_back(5 * i + NUM); \
du.order_diffs.push_back(buffer); \
#define _GEN_VAR(NAME, NUM) \
if (i >= rit->orders.size()) \
buffer = lit.orders[i].NAME; \
else \
buffer = lit.orders[i].NAME - rit->orders[i].NAME; \
if (buffer != 0) { \
du.order_ids.push_back(5 * i + NUM); \
du.order_diffs.push_back(buffer); \
}
_DOALL_ON_ORDER(_GEN_VAR)
#undef _GEN_VAR
}
} else { // Unit only exist in latter frame;
// Fill out diffs for the int32_t variables
#define _GEN_VAR(NAME, NUM) \
#define _GEN_VAR(NAME, NUM) \
du.var_ids.push_back(NUM); \
du.var_diffs.push_back(lit.NAME);
_DOALL(_GEN_VAR)
#undef _GEN_VAR
// Fill out diffs for orders
for (size_t i = 0; i < lit.orders.size(); i++) {
#define _GEN_VAR(NAME, NUM) \
#define _GEN_VAR(NAME, NUM) \
du.order_ids.push_back(5 * i + NUM); \
du.order_diffs.push_back(lit.orders[i].NAME);
_DOALL_ON_ORDER(_GEN_VAR)
Expand All @@ -345,7 +347,9 @@ replayer::Frame* detail::add(replayer::Frame* frame, replayer::FrameDiff* df) {
auto pid = df->pids[i];
f->units[pid] = std::vector<replayer::Unit>();

auto f_units = frame->units.at(pid);
auto f_units = frame->units.find(pid) == frame->units.end()
? std::vector<replayer::Unit>()
: frame->units.at(pid);
std::sort(f_units.begin(), f_units.end(), detail::orderUnitByiD);

// Should be in order
Expand All @@ -366,7 +370,7 @@ replayer::Frame* detail::add(replayer::Frame* frame, replayer::FrameDiff* df) {

for (size_t k = 0; k < du.var_diffs.size(); k++) {
switch (du.var_ids[k]) { // assumes int32_t are 0 initted
#define _SWITCHES(NAME, NUM) \
#define _SWITCHES(NAME, NUM) \
case NUM: \
u.NAME += du.var_diffs[k]; \
break;
Expand All @@ -380,7 +384,7 @@ replayer::Frame* detail::add(replayer::Frame* frame, replayer::FrameDiff* df) {
auto order_n = du.order_ids[k] / 5;
auto field_n = du.order_ids[k] % 5;
switch (field_n) {
#define _SWITCHES(NAME, NUM) \
#define _SWITCHES(NAME, NUM) \
case NUM: \
u.orders[order_n].NAME += du.order_diffs[k]; \
break;
Expand Down Expand Up @@ -492,24 +496,25 @@ std::istream& replayer::operator>>(std::istream& in, detail::UnitDiff& o) {

#define STRINGIFY2(X) #X
#define STRINGIFY(X) STRINGIFY2(X)
#define _TESTMSG(COND, MSG) if (!(COND)) { \
std::cerr << MSG << std::endl; \
return false; \
} else
#define _TESTMSG(COND, MSG) \
if (!(COND)) { \
std::cerr << MSG << std::endl; \
return false; \
} else
#define _TEST(COND) _TESTMSG(COND, "(" STRINGIFY(COND) ") not satisfied")
#define _EQV(VAR, CODE) (f1 ## VAR) CODE == (f2 ## VAR) CODE
#define _EQ(CODE) (f1) CODE == (f2) CODE
#define _EQV(VAR, CODE) (f1##VAR) CODE == (f2##VAR)CODE
#define _EQ(CODE) (f1) CODE == (f2)CODE
bool detail::frameEq(replayer::Frame* f1, replayer::Frame* f2) {
_TEST(_EQ(->reward));
_TEST(_EQ(->is_terminal));
_TEST(_EQ(->bullets.size()));
for (size_t i=0; i<f1->bullets.size(); i++) {
for (size_t i = 0; i < f1->bullets.size(); i++) {
_TEST(_EQ(->bullets[i].type));
_TEST(_EQ(->bullets[i].x));
_TEST(_EQ(->bullets[i].y));
}
_TEST(f1->resources.size() == f2->resources.size());
for (size_t i=0; i<f1->resources.size(); i++) {
for (size_t i = 0; i < f1->resources.size(); i++) {
_TEST(_EQ(->resources[i].ore));
_TEST(_EQ(->resources[i].gas));
_TEST(_EQ(->resources[i].used_psi));
Expand All @@ -521,11 +526,11 @@ bool detail::frameEq(replayer::Frame* f1, replayer::Frame* f2) {
auto f1actions = elem.second;
auto f2actions = f2->actions.at(elem.first);
_TEST(_EQV(actions, .size()));
for (size_t i=0; i<f1actions.size(); i++) {
for (size_t i = 0; i < f1actions.size(); i++) {
_TEST(_EQV(actions, [i].uid));
_TEST(_EQV(actions, [i].aid));
_TEST(_EQV(actions, [i].action.size()));
for (size_t k=0; k<f1actions[i].action.size(); k++)
for (size_t k = 0; k < f1actions[i].action.size(); k++)
_TEST(_EQV(actions, [i].action[k]));
}
}
Expand All @@ -536,14 +541,14 @@ bool detail::frameEq(replayer::Frame* f1, replayer::Frame* f2) {
std::sort(f1units.begin(), f1units.end(), detail::orderUnitByiD);
std::sort(f2units.begin(), f2units.end(), detail::orderUnitByiD);
_TEST(_EQV(units, .size()));
for (size_t i=0; i<f1units.size(); i++) {
for (size_t i = 0; i < f1units.size(); i++) {
#define _GEN_VAR(NAME, NUM) _TEST(_EQV(units, [i].NAME));
_DOALL(_GEN_VAR)
#undef _GEN_VAR
_TEST(_EQV(units, [i].velocityX));
_TEST(_EQV(units, [i].velocityY));
_TEST(_EQV(units, [i].orders.size()));
for (size_t k=0; k<f1units[i].orders.size(); k++)
for (size_t k = 0; k < f1units[i].orders.size(); k++)
_TEST(_EQV(units, [i].orders[k]));
}
}
Expand Down
4 changes: 2 additions & 2 deletions torchcraft-1.1-0.rockspec → torchcraft-1.1-1.rockspec
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package = "TorchCraft"
version = "1.1-0"
version = "1.1-1"
source = {
url = "git://github.com/torchcraft/torchcraft",
tag = "v1.1-0",
tag = "v1.1-1",
}
description = {
summary = "Connects Torch to StarCraft through BWAPI",
Expand Down

0 comments on commit c168246

Please sign in to comment.