-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.cpp
executable file
·362 lines (316 loc) · 13.4 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
#ifdef _WIN32
#define _USE_MATH_DEFINES
#endif
#include <iostream>
#include <list>
#include "version.hpp"
#include "core/Halite.hpp"
#include "core/util/distributions.hpp"
inline std::istream& operator>>(std::istream& i,
std::pair<signed int, signed int>& p) {
i >> p.first >> p.second;
return i;
}
inline std::ostream& operator<<(std::ostream& o,
const std::pair<signed int, signed int>& p) {
o << p.first << ' ' << p.second;
return o;
}
#include <tclap/CmdLine.h>
namespace TCLAP {
template<>
struct ArgTraits<std::pair<signed int, signed int> > {
typedef TCLAP::ValueLike ValueCategory;
};
}
bool quiet_output =
false; //Need to be passed to a bunch of classes; extern is cleaner.
bool always_log =
false; //Flag to always log game state (regardless of whether bots are error-ing out)
Halite*
my_game; //Is a pointer to avoid problems with assignment, dynamic memory, and default constructors.
Networking promptNetworking();
void promptDimensions(unsigned short& w, unsigned short& h);
int main(int argc, char** argv) {
srand(time(NULL)); //For all non-seeded randomness.
Networking networking;
std::vector<std::string>* names = NULL;
auto id = std::chrono::duration_cast<std::chrono::seconds>(
std::chrono::high_resolution_clock().now().time_since_epoch()).count();
TCLAP::CmdLine cmd("Halite Game Environment", ' ', HALITE_VERSION);
//Switch Args.
TCLAP::SwitchArg quietSwitch("q",
"quiet",
"Runs game in quiet mode, producing machine-parsable output.",
cmd,
false);
TCLAP::SwitchArg overrideSwitch("o",
"override",
"Overrides player-sent names using cmd args [SERVER ONLY].",
cmd,
false);
TCLAP::SwitchArg timeoutSwitch("t",
"timeout",
"Causes game environment to ignore timeouts (give all bots infinite time).",
cmd,
false);
TCLAP::SwitchArg noReplaySwitch
("r", "noreplay", "Turns off the replay generation.", cmd, false);
//Value Args
TCLAP::ValueArg<unsigned int> nPlayersArg("n",
"nplayers",
"Create a map that will accommodate n players [SINGLE PLAYER MODE ONLY].",
false,
1,
"{1,2,3,4,5,6}",
cmd);
TCLAP::ValueArg<std::pair<signed int, signed int> > dimensionArgs("d",
"dimensions",
"The dimensions of the map.",
false,
{ 0,
0 },
"a string containing two space-seprated positive integers",
cmd);
TCLAP::ValueArg<unsigned int> seedArg("s",
"seed",
"The seed for the map generator.",
false,
0,
"positive integer",
cmd);
TCLAP::ValueArg<std::string> replayDirectoryArg("i",
"replaydirectory",
"The path to directory for replay output.",
false,
".",
"path to directory",
cmd);
TCLAP::ValueArg<std::string> constantsArg(
"",
"constantsfile",
"JSON file containing runtime constants to use.",
false,
"",
"path to file",
cmd
);
TCLAP::SwitchArg printConstantsSwitch(
"",
"print-constants",
"Print out the default constants and exit.",
cmd,
false
);
TCLAP::SwitchArg noCompressionSwitch(
"",
"no-compression",
"Disables compression for output files.",
cmd,
false
);
//Remaining Args, be they start commands and/or override names. Description only includes start commands since it will only be seen on local testing.
TCLAP::UnlabeledMultiArg<std::string> otherArgs("NonspecifiedArgs",
"Start commands for bots.",
false,
"Array of strings",
cmd);
TCLAP::SwitchArg logSwitch("",
"log",
"Always produce game logs, instead of only in case of errors",
cmd,
false);
TCLAP::SwitchArg teamSwitch("",
"team",
"Run with 2v2 mode, first player paired with third and second with fourth",
cmd,
false);
cmd.parse(argc, argv);
unsigned short mapWidth = dimensionArgs.getValue().first;
unsigned short mapHeight = dimensionArgs.getValue().second;
unsigned int seed;
if (seedArg.getValue() != 0) seed = seedArg.getValue();
else
seed =
(std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::system_clock::now().time_since_epoch()).count()
% 4294967295);
unsigned short n_players_for_map_creation = nPlayersArg.getValue();
quiet_output = quietSwitch.getValue();
always_log = logSwitch.getValue();
bool override_names = overrideSwitch.getValue();
bool ignore_timeout = timeoutSwitch.getValue();
bool team_battle = teamSwitch.getValue();
if (printConstantsSwitch.getValue()) {
std::cout << hlt::GameConstants::get().to_json().dump(4) << '\n';
return 0;
}
// Update the game constants.
if (constantsArg.isSet()) {
std::ifstream constants_file(constantsArg.getValue());
nlohmann::json constants_json;
constants_file >> constants_json;
auto& constants = hlt::GameConstants::get_mut();
constants.from_json(constants_json);
if (!quiet_output) {
std::cout
<< "Game constants: \n"
<< constants.to_json().dump(4) << '\n';
}
}
else {
if (!quiet_output) {
std::cout << "Game constants: all default\n";
}
}
std::vector<std::string> unlabeledArgsVector = otherArgs.getValue();
std::list<std::string> unlabeledArgs;
for (auto arg : unlabeledArgsVector) {
unlabeledArgs.push_back(arg);
}
if (mapWidth == 0 && mapHeight == 0) {
// Always generate a 3:2 aspect ratio
std::vector<unsigned short> mapSizeChoices =
{ 80, 80, 88, 88, 96, 96, 96, 104, 104, 104, 104, 112, 112, 112, 120, 120, 128, 128 };
std::mt19937 prg(seed);
util::uniform_int_distribution<unsigned long> size_dist(0, mapSizeChoices.size() - 1);
auto mapBase = mapSizeChoices[size_dist(prg)];
mapWidth = 3 * mapBase;
mapHeight = 2 * mapBase;
}
const auto override_factor = overrideSwitch.getValue() ? 2 : 1;
if (unlabeledArgs.size() != 1 &&
unlabeledArgs.size() != 2 * override_factor &&
unlabeledArgs.size() != 4 * override_factor) {
std::cout << "Must have either 2 or 4 players, or a solo player.\n";
return 1;
}
if (unlabeledArgs.size() == 1 && n_players_for_map_creation != 2 &&
n_players_for_map_creation != 4) {
// Limiation of solar system map generator used
std::cout << "Must have either 2 or 4 players for map creation.\n";
return 1;
}
if (override_names) {
if (unlabeledArgs.size() < 4 || unlabeledArgs.size() % 2 != 0) {
std::cout
<< "Invalid number of player parameters with override switch enabled. Override intended for server use only."
<< std::endl;
exit(1);
} else {
try {
names = new std::vector<std::string>();
while (!unlabeledArgs.empty()) {
networking.launch_bot(unlabeledArgs.front());
unlabeledArgs.pop_front();
names->push_back(unlabeledArgs.front());
unlabeledArgs.pop_front();
}
}
catch (...) {
std::cout
<< "Invalid player parameters with override switch enabled. Override intended for server use only."
<< std::endl;
delete names;
names = NULL;
exit(1);
}
}
} else {
if (unlabeledArgs.size() < 1) {
std::cout
<< "Please provide the launch command string for at least one bot."
<< std::endl
<< "Use the --help flag for usage details.\n";
exit(1);
}
try {
while (!unlabeledArgs.empty()) {
networking.launch_bot(unlabeledArgs.front());
if (team_battle) {
networking.set_team();
}
unlabeledArgs.pop_front();
}
}
catch (...) {
std::cout
<< "One or more of your bot launch command strings failed. Please check for correctness and try again."
<< std::endl;
exit(1);
}
}
if (networking.player_count() > 1 && n_players_for_map_creation != 1) {
std::cout << std::endl
<< "Only single-player mode enables specified n-player maps. When entering multiple bots, please do not try to specify n."
<< std::endl << std::endl;
exit(1);
}
if (networking.player_count() != 4 && team_battle){
std::cout << std::endl
<< "Can only run team battles with 4 players."
<< std::endl << std::endl;
exit(1);
}
if (networking.player_count() > 1)
n_players_for_map_creation = networking.player_count();
if (n_players_for_map_creation > 6 || n_players_for_map_creation < 1) {
std::cout << std::endl
<< "A map can only accommodate between 1 and 6 players."
<< std::endl << std::endl;
exit(1);
}
//Create game. Null parameters will be ignored.
my_game = new Halite(mapWidth,
mapHeight,
seed,
n_players_for_map_creation,
networking,
ignore_timeout);
std::string outputFilename = replayDirectoryArg.getValue();
#ifdef _WIN32
if(outputFilename.back() != '\\') outputFilename.push_back('\\');
#else
if (outputFilename.back() != '/') outputFilename.push_back('/');
#endif
GameStatistics stats = my_game->run_game(names,
id,
!noReplaySwitch.getValue(),
!noCompressionSwitch.getValue(),
outputFilename);
if (names != NULL) delete names;
std::string victoryOut;
if (!quiet_output && !team_battle) {
for (unsigned int player_id = 0;
player_id < stats.player_statistics.size(); player_id++) {
auto& player_stats = stats.player_statistics[player_id];
std::cout
<< "Player #" << player_stats.tag << ", "
<< my_game->get_name(player_stats.tag)
<< ", came in rank #" << player_stats.rank
<< " and was last alive on frame #"
<< player_stats.last_frame_alive
<< ", producing " << player_stats.total_ship_count << " ships"
<< " and dealing " << player_stats.damage_dealt << " damage"
<< "!\n";
}
}else{
for (int team_id = 0; team_id < 2; team_id++) {
int team_mate = networking.get_team(team_id);
auto& player_stats = stats.player_statistics[team_id];
auto& mate_stats = stats.player_statistics[team_mate];
std::string rank;
if (player_stats.rank == 1 || mate_stats.rank == 1){
rank = "1st";
}else{
rank = "2nd";
}
std::cout
<< "Team-" << team_id+1 << ","
<< " containing : " << my_game->get_name(player_stats.tag)
<< " and " << my_game->get_name(mate_stats.tag) << ","
<< " came " << rank << std::endl;
}
}
delete my_game;
return 0;
}