Skip to content

Commit 029c9ed

Browse files
Kevin CalderoneGitHub Enterprise
authored andcommitted
Merge pull request Blizzard#166 from kcalderone/master
Added option to set feature layer size. Foced game to run in windowed…
2 parents 1654f8c + 8e2645c commit 029c9ed

14 files changed

+99
-58
lines changed

examples/feature_layers.cc

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,26 +11,32 @@
1111

1212
using namespace sc2;
1313

14-
void DrawFeatureLayer(const SC2APIProtocol::ImageData& image_data, int width, int height, int off_x, int off_y, int sz_x, int sz_y) {
15-
assert(width);
16-
assert(height);
17-
renderer::Matrix(image_data.data().c_str(), off_x, off_y, sz_x, sz_y, width, height, 255, 0, 0);
14+
const float kCameraWidth = 24.0f;
15+
const int kFeatureLayerSize = 80;
16+
const int kPixelDrawSize = 5;
17+
const int kDrawSize = kFeatureLayerSize * kPixelDrawSize;
18+
19+
void DrawFeatureLayer(const SC2APIProtocol::ImageData& image_data, int width, int height, int off_x, int off_y) {
20+
renderer::Matrix(image_data.data().c_str(), off_x, off_y, kPixelDrawSize, kPixelDrawSize, width, height, 255, 0, 0);
1821
}
1922

2023
class RenderAgent : public Agent {
2124
public:
2225
virtual void OnGameStart() final {
23-
renderer::Initialize("Feature layers", 50, 50, 640, 640);
26+
renderer::Initialize("Feature layers", 50, 50, 2 * kDrawSize, 2 * kDrawSize);
2427
}
2528

2629
virtual void OnStep() final {
2730
const SC2APIProtocol::Observation* observation = Observation()->GetRawObservation();
31+
2832
const SC2APIProtocol::FeatureLayersMap& m = observation->feature_layer_data().map_renders();
29-
DrawFeatureLayer(m.unit_density(), m.width(), m.height(), 0, 0, 5, 5);
30-
DrawFeatureLayer(m.selected(), m.width(), m.height(), 320, 0, 5, 5);
33+
DrawFeatureLayer(m.unit_density(), m.width(), m.height(), 0, 0);
34+
DrawFeatureLayer(m.selected(), m.width(), m.height(), kDrawSize, 0);
35+
3136
const SC2APIProtocol::FeatureLayersMinimap& mi = observation->feature_layer_data().minimap_renders();
32-
DrawFeatureLayer(mi.camera(), mi.width(), mi.height(), 0, 320, 5, 5);
33-
DrawFeatureLayer(mi.height_map(), mi.width(), mi.height(), 320, 320, 5, 5);
37+
DrawFeatureLayer(mi.camera(), mi.width(), mi.height(), 0, kDrawSize);
38+
DrawFeatureLayer(mi.height_map(), mi.width(), mi.height(), kDrawSize, kDrawSize);
39+
3440
renderer::Render();
3541
}
3642

@@ -46,7 +52,8 @@ int main(int argc, char* argv[]) {
4652
return 1;
4753
}
4854

49-
coordinator.SetFeatureLayers(true);
55+
FeatureLayerSettings settings(kCameraWidth, kFeatureLayerSize, kFeatureLayerSize, kFeatureLayerSize, kFeatureLayerSize);
56+
coordinator.SetFeatureLayers(settings);
5057

5158
// Add the custom bot, it will control the players.
5259
RenderAgent bot;

include/sc2api/sc2_control_interfaces.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
namespace sc2 {
77

88
struct ProcessInfo;
9-
struct RenderSettings;
9+
struct InterfaceSettings;
1010

1111
class ControlInterface {
1212
public:
@@ -15,7 +15,7 @@ class ControlInterface {
1515
virtual bool RemoteSaveMap(const void* data, int data_size, std::string remote_path) = 0;
1616
virtual bool CreateGame(const std::string& map_path, const std::vector<PlayerSetup>& players) = 0;
1717

18-
virtual bool RequestJoinGame(PlayerSetup setup, bool feature_layers=false, RenderSettings* render = nullptr, const Ports& ports = Ports()) = 0;
18+
virtual bool RequestJoinGame(PlayerSetup setup, const InterfaceSettings& settings, const Ports& ports = Ports()) = 0;
1919
virtual bool WaitJoinGame() = 0;
2020

2121
// Single player.
@@ -59,7 +59,7 @@ class ReplayControlInterface {
5959
virtual bool IgnoreReplay(const ReplayInfo& replay_info) = 0;
6060

6161
virtual bool GatherReplayInfo(const std::string& path) = 0;
62-
virtual bool LoadReplay(const std::string& replay_path, bool feature_layers, uint32_t player_id) = 0;
62+
virtual bool LoadReplay(const std::string& replay_path, const InterfaceSettings& settings, uint32_t player_id) = 0;
6363
virtual bool WaitForReplay() = 0;
6464

6565
virtual const ReplayInfo& GetReplayInfo() const = 0;

include/sc2api/sc2_coordinator.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class Coordinator {
2525
bool LoadSettings(int argc, char** argv, const std::string& game_settings = "game_settings.ini");
2626
void SetMultithreaded(bool value);
2727
void SetRealtime(bool value);
28-
void SetFeatureLayers(bool value);
28+
void SetFeatureLayers(const FeatureLayerSettings& settings);
2929
void SetRender(const RenderSettings& settings);
3030
void AddCommandLine(const std::string& option);
3131
void SetParticipants(const std::vector<PlayerSetup>& participants);

include/sc2api/sc2_game_settings.h

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,16 +46,33 @@ struct RenderSettings {
4646
int minimap_y_ = 300;
4747
};
4848

49+
struct FeatureLayerSettings {
50+
FeatureLayerSettings () = default;
51+
FeatureLayerSettings (float camera_width, int map_x, int map_y, int minimap_x, int minimap_y)
52+
: camera_width_(camera_width), map_x_(map_x), map_y_(map_y), minimap_x_(minimap_x), minimap_y_(minimap_y)
53+
{}
54+
float camera_width_ = 24.0f;
55+
int map_x_ = 64;
56+
int map_y_ = 64;
57+
int minimap_x_ = 64;
58+
int minimap_y_ = 64;
59+
};
60+
61+
struct InterfaceSettings {
62+
InterfaceSettings();
63+
64+
bool use_feature_layers_;
65+
FeatureLayerSettings feature_layer_settings_;
66+
bool use_render_;
67+
RenderSettings render_settings_;
68+
};
69+
4970
struct GameSettings {
5071
GameSettings();
5172

5273
std::string map_name_;
5374
std::vector<PlayerSetup> player_setup_;
5475
Ports ports_;
55-
bool use_feature_layers_;
56-
57-
bool use_render_;
58-
RenderSettings render_settings_;
5976
};
6077

6178
struct ReplaySettings {
@@ -64,7 +81,6 @@ struct ReplaySettings {
6481
// Fill with replays to analyze.
6582
std::vector<std::string> replay_file_;
6683
std::string replay_dir_;
67-
bool use_feature_layers_;
6884
uint32_t player_id_;
6985
};
7086

src/sc2api/sc2_client.cc

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1110,7 +1110,7 @@ class ControlImp : public ControlInterface {
11101110
bool Connect(const std::string& address, int port, int timeout_ms) override;
11111111
bool CreateGame(const std::string& map_path, const std::vector<PlayerSetup>& players) override;
11121112

1113-
bool RequestJoinGame(PlayerSetup setup, bool feature_layers=false, RenderSettings* render = nullptr, const Ports& ports = Ports()) override;
1113+
bool RequestJoinGame(PlayerSetup setup, const InterfaceSettings& settings, const Ports& ports = Ports()) override;
11141114
bool WaitJoinGame() override;
11151115

11161116
bool RequestEndGame() override;
@@ -1313,7 +1313,7 @@ bool ControlImp::CreateGame(const std::string& map_path, const std::vector<Playe
13131313
return success;
13141314
}
13151315

1316-
bool ControlImp::RequestJoinGame(PlayerSetup setup, bool feature_layers, RenderSettings* render, const Ports& ports) {
1316+
bool ControlImp::RequestJoinGame(PlayerSetup setup, const InterfaceSettings& settings, const Ports& ports) {
13171317
observation_imp_->ClearFlags();
13181318

13191319
is_multiplayer_ = ports.IsValid();
@@ -1339,18 +1339,23 @@ bool ControlImp::RequestJoinGame(PlayerSetup setup, bool feature_layers, RenderS
13391339
}
13401340
}
13411341

1342-
SC2APIProtocol::InterfaceOptions* interface_options = request_join_game->mutable_options();
1343-
interface_options->set_raw(true);
1344-
interface_options->set_score(true);
1345-
if (feature_layers) {
1346-
interface_options->set_allocated_feature_layer(new SC2APIProtocol::SpatialSetup());
1347-
}
1348-
if (render) {
1349-
SC2APIProtocol::SpatialSetup* render_setup = interface_options->mutable_render();
1350-
render_setup->set_map_resolution_x(render->map_x_);
1351-
render_setup->set_map_resolution_y(render->map_y_);
1352-
render_setup->set_minimap_resolution_x(render->minimap_x_);
1353-
render_setup->set_minimap_resolution_x(render->minimap_y_);
1342+
SC2APIProtocol::InterfaceOptions* options = request_join_game->mutable_options();
1343+
options->set_raw(true);
1344+
options->set_score(true);
1345+
if (settings.use_feature_layers_) {
1346+
SC2APIProtocol::SpatialSetup* setupProto = options->mutable_feature_layer();
1347+
setupProto->set_camera_width(settings.feature_layer_settings_.camera_width_);
1348+
setupProto->set_map_resolution_x(settings.feature_layer_settings_.map_x_);
1349+
setupProto->set_map_resolution_y(settings.feature_layer_settings_.map_y_);
1350+
setupProto->set_minimap_resolution_x(settings.feature_layer_settings_.minimap_x_);
1351+
setupProto->set_minimap_resolution_y(settings.feature_layer_settings_.minimap_y_);
1352+
}
1353+
if (settings.use_render_) {
1354+
SC2APIProtocol::SpatialSetup* setupProto = options->mutable_render();
1355+
setupProto->set_map_resolution_x(settings.render_settings_.map_x_);
1356+
setupProto->set_map_resolution_y(settings.render_settings_.map_y_);
1357+
setupProto->set_minimap_resolution_x(settings.render_settings_.minimap_x_);
1358+
setupProto->set_minimap_resolution_y(settings.render_settings_.minimap_y_);
13541359
}
13551360

13561361
if (!proto_.SendRequest(request)) {

src/sc2api/sc2_coordinator.cc

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,12 @@ int LaunchProcesses(ProcessSettings& process_settings, std::vector<Client*> clie
4646
std::vector<std::string> cl = {
4747
"-listen", process_settings.net_address_,
4848
"-port", std::to_string(pi.port_),
49-
"-offline",
5049
"-gamestatelogging",
5150
};
5251

52+
// DirectX will fail if multiple games try to launch in fullscreen mode. Force them into windowed mode.
53+
cl.push_back("-displayMode 0");
54+
5355
cl.push_back("-simulationSpeed");
5456
if (process_settings.real_time_) {
5557
cl.push_back("1");
@@ -142,6 +144,7 @@ class CoordinatorImp {
142144

143145
GameSettings game_settings_;
144146
ReplaySettings replay_settings_;
147+
InterfaceSettings interface_settings_;
145148
ProcessSettings process_settings_;
146149

147150
CoordinatorImp();
@@ -217,7 +220,7 @@ void CoordinatorImp::StartReplay() {
217220

218221
// If the replay isn't being pruned based on replay info start it.
219222
if (!r->ReplayControl()->IgnoreReplay(r->ReplayControl()->GetReplayInfo())) {
220-
r->ReplayControl()->LoadReplay(file, replay_settings_.use_feature_layers_, replay_settings_.player_id_);
223+
r->ReplayControl()->LoadReplay(file, interface_settings_, replay_settings_.player_id_);
221224
}
222225
}
223226

@@ -430,8 +433,7 @@ void CoordinatorImp::StartGame() {
430433
int i = 0;
431434
for (auto c : agents_) {
432435
bool game_join_request = c->Control()->RequestJoinGame(game_settings_.player_setup_[i++],
433-
game_settings_.use_feature_layers_,
434-
game_settings_.use_render_ ? &game_settings_.render_settings_ : nullptr,
436+
interface_settings_,
435437
game_settings_.ports_);
436438

437439
assert(game_join_request);
@@ -606,17 +608,18 @@ void Coordinator::SetRealtime(bool value) {
606608
imp_->process_settings_.real_time_ = value;
607609
}
608610

609-
void Coordinator::SetFeatureLayers(bool value) {
611+
void Coordinator::SetFeatureLayers(const FeatureLayerSettings& settings) {
610612
// Feature Layers must be set before LaunchStarcraft is called.
611613
assert(!imp_->starcraft_started_);
612-
imp_->game_settings_.use_feature_layers_ = value;
614+
imp_->interface_settings_.use_feature_layers_ = true;
615+
imp_->interface_settings_.feature_layer_settings_= settings;
613616
}
614617

615618
void Coordinator::SetRender(const RenderSettings& settings) {
616619
// Render must be set before LaunchStarcraft is called.
617620
assert(!imp_->starcraft_started_);
618-
imp_->game_settings_.use_render_ = true;
619-
imp_->game_settings_.render_settings_ = settings;
621+
imp_->interface_settings_.use_render_ = true;
622+
imp_->interface_settings_.render_settings_ = settings;
620623
}
621624

622625
bool Coordinator::SetReplayPath(const std::string& path) {

src/sc2api/sc2_game_settings.cc

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,20 @@ ProcessSettings::ProcessSettings(bool realtime,
1818
multi_threaded_(multi_threaded) {
1919
}
2020

21+
InterfaceSettings::InterfaceSettings() :
22+
use_feature_layers_(false),
23+
use_render_(false) {
24+
}
25+
2126
GameSettings::GameSettings() :
2227
map_name_(""),
2328
player_setup_(),
24-
ports_(),
25-
use_feature_layers_(false) {
29+
ports_() {
2630
}
2731

2832
ReplaySettings::ReplaySettings() :
2933
replay_file_(),
3034
replay_dir_(),
31-
use_feature_layers_(false),
3235
player_id_(1) {
3336
}
3437

src/sc2api/sc2_replay_observer.cc

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "sc2api/sc2_replay_observer.h"
22
#include "sc2api/sc2_control_interfaces.h"
33
#include "sc2api/sc2_proto_to_pods.h"
4+
#include "sc2api/sc2_game_settings.h"
45

56
#include <iostream>
67

@@ -22,7 +23,7 @@ class ReplayControlImp : public ReplayControlInterface {
2223
virtual bool IgnoreReplay(const ReplayInfo& replay_info) override;
2324

2425
virtual bool GatherReplayInfo(const std::string& path) override;
25-
virtual bool LoadReplay(const std::string& replay_path, bool feature_layers, uint32_t player_id) override;
26+
virtual bool LoadReplay(const std::string& replay_path, const InterfaceSettings& settings, uint32_t player_id) override;
2627
virtual bool WaitForReplay() override;
2728

2829
virtual const ReplayInfo& GetReplayInfo() const override;
@@ -130,21 +131,31 @@ bool ReplayControlImp::GatherReplayInfo(const std::string& path) {
130131
return true;
131132
}
132133

133-
bool ReplayControlImp::LoadReplay(const std::string& replay_path, bool feature_layers, uint32_t player_id) {
134+
bool ReplayControlImp::LoadReplay(const std::string& replay_path, const InterfaceSettings& settings, uint32_t player_id) {
134135
// Send the request.
135136
GameRequestPtr request = control_interface_->Proto().MakeRequest();
136137
SC2APIProtocol::RequestStartReplay* start_replay_request = request->mutable_start_replay();
137138
start_replay_request->set_replay_path(replay_path);
138139
start_replay_request->set_observed_player_id(player_id);
140+
139141
SC2APIProtocol::InterfaceOptions* options = start_replay_request->mutable_options();
140142
options->set_raw(true);
141143
options->set_score(true);
142-
if (feature_layers) {
143-
options->set_allocated_feature_layer(new SC2APIProtocol::SpatialSetup());
144+
if (settings.use_feature_layers_) {
145+
SC2APIProtocol::SpatialSetup* setupProto = options->mutable_feature_layer();
146+
setupProto->set_camera_width(settings.feature_layer_settings_.camera_width_);
147+
setupProto->set_map_resolution_x(settings.feature_layer_settings_.map_x_);
148+
setupProto->set_map_resolution_y(settings.feature_layer_settings_.map_y_);
149+
setupProto->set_minimap_resolution_x(settings.feature_layer_settings_.minimap_x_);
150+
setupProto->set_minimap_resolution_y(settings.feature_layer_settings_.minimap_y_);
151+
}
152+
if (settings.use_render_) {
153+
SC2APIProtocol::SpatialSetup* setupProto = options->mutable_render();
154+
setupProto->set_map_resolution_x(settings.render_settings_.map_x_);
155+
setupProto->set_map_resolution_y(settings.render_settings_.map_y_);
156+
setupProto->set_minimap_resolution_x(settings.render_settings_.minimap_x_);
157+
setupProto->set_minimap_resolution_y(settings.render_settings_.minimap_y_);
144158
}
145-
146-
// TODO: Update this feature.
147-
//options->set_enable_render(render);
148159

149160
if (!control_interface_->Proto().SendRequest(request)) {
150161
std::cerr << "LoadReplay: load replay request failed." << std::endl;

tests/test_app.cc

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,6 @@ bool TestHang(int argc, char** argv) {
9393
if (!coordinator.LoadSettings(argc, argv)) {
9494
return false;
9595
}
96-
coordinator.SetFeatureLayers(true);
9796

9897
// Add the custom bot, it will control the players.
9998
AppTestBotHang bot1;
@@ -119,7 +118,6 @@ bool TestCrash(int argc, char** argv) {
119118
if (!coordinator.LoadSettings(argc, argv)) {
120119
return false;
121120
}
122-
coordinator.SetFeatureLayers(true);
123121

124122
// Add the custom bot, it will control the players.
125123
AppTestBotCrash bot1;

tests/test_feature_layer.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,8 @@ bool TestFeatureLayers(int argc, char** argv) {
272272
if (!coordinator.LoadSettings(argc, argv)) {
273273
return false;
274274
}
275-
coordinator.SetFeatureLayers(true);
275+
276+
coordinator.SetFeatureLayers(FeatureLayerSettings());
276277

277278
// Add the custom bot, it will control the players.
278279
FeatureLayerTestBot bot;

0 commit comments

Comments
 (0)