forked from Voidious/BerryBots
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bbpimain.cpp
375 lines (338 loc) · 11.6 KB
/
bbpimain.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
363
364
365
366
367
368
369
370
371
372
373
374
375
/*
Copyright (C) 2012-2013 - Voidious
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include <iostream>
#include <exception>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <sstream>
#include "shapes.h"
#include "bbconst.h"
#include "bbutil.h"
#include "bbpigfx.h"
#include "filemanager.h"
#include "stage.h"
#include "bbengine.h"
#include "replaybuilder.h"
#include "gfxeventhandler.h"
#include "cliprinthandler.h"
#include "clipackagereporter.h"
#include "tarzipper.h"
#include "ResourcePath.hpp"
void printUsage() {
std::cout << "Usage:" << std::endl;
std::cout << " ./berrybots [-nodisplay] [-savereplay]"
<< " <stage.lua> <bot1.lua> [<bot2.lua> ...]" << std::endl;
std::cout << " OR" << std::endl;
std::cout << " ./berrybots -packstage <stage.lua> <version>"
<< std::endl;
std::cout << " OR" << std::endl;
std::cout << " ./berrybots -packbot <bot.lua> <version>"
<< std::endl;
exit(0);
}
char* replayFilename(const char *stageName) {
std::stringstream nameStream;
char *timestamp = getTimestamp();
nameStream << ((stageName == 0) ? "unknown" : stageName) << "-"
<< timestamp << ".html";
delete timestamp;
std::string filename = nameStream.str();
char *newFilename = new char[filename.length() + 1];
strcpy(newFilename, filename.c_str());
return newFilename;
}
int main(int argc, char *argv[]) {
Zipper *zipper = new TarZipper();
FileManager *fileManager = new FileManager(zipper);
CliPackageReporter *packageReporter = new CliPackageReporter();
fileManager->setListener(packageReporter);
char *shipsBaseDir = fileManager->getAbsFilePath(SHIPS_SUBDIR);
char *stagesBaseDir = fileManager->getAbsFilePath(STAGES_SUBDIR);
if (flagExists(argc, argv, "packstage")) {
char **stageInfo = parseFlag(argc, argv, "packstage", 2);
if (stageInfo == 0) {
printUsage();
} else {
// TODO: add a new flag for obfuscating source code
bool obfuscate = false;
try {
char *stageAbsName = fileManager->getAbsFilePath(stageInfo[0]);
char *stageName =
fileManager->parseRelativeFilePath(stagesBaseDir, stageAbsName);
if (stageName == 0) {
std::cout << "Stage must be located under " << STAGES_SUBDIR
<< "/ subdirectory: " << stageInfo[0] << std::endl;
} else {
fileManager->packageStage(stagesBaseDir, stageName, stageInfo[1],
CACHE_SUBDIR, TMP_SUBDIR, obfuscate, true);
delete stageName;
}
delete stageAbsName;
} catch (std::exception *e) {
std::cout << "BerryBots encountered an error:" << std::endl;
std::cout << " " << e->what() << std::endl;
delete e;
}
delete stageInfo;
}
return 0;
}
if (flagExists(argc, argv, "packbot")) {
char **shipInfo = parseFlag(argc, argv, "packbot", 2);
if (shipInfo == 0) {
printUsage();
} else {
// TODO: add a new flag for obfuscating source code
bool obfuscate = false;
try {
char *shipAbsName = fileManager->getAbsFilePath(shipInfo[0]);
char *shipName =
fileManager->parseRelativeFilePath(shipsBaseDir, shipAbsName);
if (shipName == 0) {
std::cout << "Ship must be located under " << SHIPS_SUBDIR
<< "/ subdirectory: " << shipInfo[0] << std::endl;
} else {
fileManager->packageShip(shipsBaseDir, shipName, shipInfo[1],
CACHE_SUBDIR, TMP_SUBDIR, obfuscate, true);
delete shipName;
}
delete shipAbsName;
} catch (std::exception *e) {
std::cout << "BerryBots encountered an error:" << std::endl;
std::cout << " " << e->what() << std::endl;
delete e;
}
delete shipInfo;
}
return 0;
}
bool nodisplay = flagExists(argc, argv, "nodisplay");
bool saveReplay = flagExists(argc, argv, "savereplay");
int optArgsOffset = (nodisplay ? 1 : 0) + (saveReplay ? 1 : 0);
if (argc < 3 + optArgsOffset) {
printUsage();
}
srand(time(NULL));
int screenWidth;
int screenHeight;
init(&screenWidth, &screenHeight);
CliPrintHandler *printHandler = new CliPrintHandler();
BerryBotsEngine *engine =
new BerryBotsEngine(printHandler, fileManager, resourcePath().c_str());
Stage *stage = engine->getStage();
// TODO: Enable graphical debugging on Raspberry Pi. Main barrier is UI.
stage->disableUserGfx();
char *stageAbsName = fileManager->getAbsFilePath(argv[1 + optArgsOffset]);
char *stageName =
fileManager->parseRelativeFilePath(stagesBaseDir, stageAbsName);
if (stageName == 0) {
std::cout << "Stage must be located under " << STAGES_SUBDIR
<< "/ subdirectory: " << argv[1 + optArgsOffset] << std::endl;
return 0;
}
try {
engine->initStage(stagesBaseDir, stageName, CACHE_SUBDIR);
} catch (EngineException *e) {
delete stageAbsName;
delete stageName;
std::cout << "BerryBots initialization failed:" << std::endl;
std::cout << " " << e->what() << std::endl;
delete e;
return 0;
}
delete stageAbsName;
delete stageName;
int firstTeam = (2 + optArgsOffset);
int numTeams = argc - firstTeam;
char **teams = new char*[numTeams];
for (int x = 0; x < numTeams; x++) {
char *teamAbsName = fileManager->getAbsFilePath(argv[x + firstTeam]);
char *teamName =
fileManager->parseRelativeFilePath(shipsBaseDir, teamAbsName);
if (teamName == 0) {
std::cout << "Ship must be located under " << SHIPS_SUBDIR
<< "/ subdirectory: " << argv[x + firstTeam] << std::endl;
return 0;
}
teams[x] = teamName;
delete teamAbsName;
}
printHandler->setNumTeams(numTeams);
try {
engine->initShips(shipsBaseDir, teams, numTeams, CACHE_SUBDIR);
} catch (EngineException *e) {
std::cout << "BerryBots initialization failed:" << std::endl;
std::cout << " " << e->what() << std::endl;
delete e;
return 0;
}
printHandler->updateTeams(engine->getTeams());
GfxEventHandler *gfxHandler = 0;
if (!nodisplay) {
gfxHandler = new GfxEventHandler();
stage->addEventHandler((EventHandler*) gfxHandler);
initVgGfx(screenWidth, screenHeight, stage, engine->getShips(),
engine->getNumShips());
drawGame(screenWidth, screenHeight, stage, engine->getShips(),
engine->getNumShips(), engine->getGameTime(), gfxHandler);
}
time_t realTime1;
time_t realTime2;
time(&realTime1);
int realSeconds = 0;
try {
do {
engine->processTick();
if (!nodisplay) {
drawGame(screenWidth, screenHeight, stage, engine->getShips(),
engine->getNumShips(), engine->getGameTime(), gfxHandler);
}
time(&realTime2);
if (realTime2 - realTime1 > 0) {
realSeconds++;
if (realSeconds % 10 == 0) {
std::cout << "TPS: "
<< (((double) engine->getGameTime()) / realSeconds)
<< std::endl;
}
}
realTime1 = realTime2;
} while (!engine->isGameOver());
} catch (EngineException *e) {
std::cout << "BerryBots encountered an error:" << std::endl;
std::cout << " " << e->what() << std::endl;
delete e;
return 0;
}
if (!nodisplay) {
destroyVgGfx();
finish();
}
const char* winnerName = engine->getWinnerName();
if (winnerName != 0) {
std::cout << std::endl<< winnerName << " wins! Congratulations!"
<< std::endl;
}
std::cout << std::endl << "Results:" << std::endl;
Team **rankedTeams = engine->getRankedTeams();
bool hasScores = false;
for (int x = 0; x < numTeams; x++) {
if (rankedTeams[x]->result.score != 0) {
hasScores = true;
break;
}
}
TeamResult *firstResult = &(rankedTeams[0]->result);
int numStats = firstResult->numStats;
char **statKeys = 0;
if (numStats > 0) {
statKeys = new char*[firstResult->numStats];
for (int x = 0; x < numStats; x++) {
statKeys[x] = new char[strlen(firstResult->stats[x]->key) + 1];
strcpy(statKeys[x], firstResult->stats[x]->key);
}
}
for (int x = 0; x < engine->getNumTeams(); x++) {
TeamResult *result = &(rankedTeams[x]->result);
if (result->showResult) {
std::cout << " " << rankedTeams[x]->name << ":" << std::endl;
std::cout << " Rank: ";
if (result->rank == 0) {
std::cout << "-";
} else {
std::cout << result->rank;
}
std::cout << std::endl;
if (hasScores) {
std::cout << " Score: " << round(result->score, 2) << std::endl;
}
for (int y = 0; y < numStats; y++) {
char *key = statKeys[y];
bool found = false;
for (int z = 0; z < result->numStats; z++) {
char *resultKey = result->stats[z]->key;
if (strcmp(key, resultKey) == 0) {
std::cout << " " << key << ": "
<< round(result->stats[z]->value, 2) << std::endl;
found = true;
break;
}
}
if (!found) {
std::cout << " " << key << ": -" << std::endl;
}
}
}
}
std::cout << std::endl << "CPU time used per tick (microseconds):"
<< std::endl;
for (int x = 0; x < engine->getNumTeams(); x++) {
Team *team = engine->getTeam(x);
if (!team->stageShip && !team->disabled) {
std::cout << " " << team->name << ": "
<< (team->totalCpuTime / team->totalCpuTicks) << std::endl;
}
}
if (realSeconds > 0) {
std::cout << std::endl << "TPS: "
<< (((double) engine->getGameTime()) / realSeconds) << std::endl;
}
if (saveReplay) {
ReplayBuilder *replayBuilder = engine->getReplayBuilder();
// TODO: move this into a function in the engine
Team **rankedTeams = engine->getRankedTeams();
replayBuilder->setResults(rankedTeams, engine->getNumTeams());
char *filename = 0;
char *absFilename = 0;
do {
if (filename != 0) {
delete filename;
}
if (absFilename != 0) {
delete absFilename;
}
filename = replayFilename(stage->getName());
char *filePath = fileManager->getFilePath(REPLAYS_SUBDIR, filename);
absFilename = fileManager->getAbsFilePath(filePath);
delete filePath;
} while (fileManager->fileExists(absFilename));
replayBuilder->saveReplay(filename);
std::cout << std::endl << "Saved replay to: " << REPLAYS_SUBDIR << "/"
<< filename << std::endl;
delete filename;
delete absFilename;
}
std::cout << std::endl;
delete engine;
for (int x = 0; x < numTeams; x++) {
delete teams[x];
}
delete teams;
delete rankedTeams;
for (int x = 0; x < numStats; x++) {
delete statKeys[x];
}
delete statKeys;
delete printHandler;
delete packageReporter;
delete fileManager;
delete zipper;
delete shipsBaseDir;
delete stagesBaseDir;
return 0;
}