-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCSWSilentApplication.cpp
More file actions
276 lines (235 loc) · 8.5 KB
/
CSWSilentApplication.cpp
File metadata and controls
276 lines (235 loc) · 8.5 KB
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
// Copyright (c) 2005-2022 Andreas Rose. All rights reserved.
// Released under the MIT license. (see license.txt)
#include "PrecompiledHeader.h"
#include "CSWSilentApplication.h"
#include "Constants.h"
#include "CSWWorld.h"
#include "CSWObject.h"
#include "CSWLog.h"
#include "CSWUtilities.h"
namespace CodeSubWars
{
CSWSilentApplication::CSWSilentApplication(int argc, char** argv)
: m_bParametersValid(true),
m_WorldType(CSWWorld::DEFAULT_1),
m_BattleType(CSWWorld::SINGLE),
m_nTeamSize(3),
m_fTimeStep(0.01)
{
checkRequirements();
int nReadData = 0;
for (int i = 0; i < argc - 1 && m_bParametersValid; ++i)
{
std::pair<ParameterType, std::string> result = determineParameterType(argv[i + 1]);
switch (result.first)
{
case UNKNOWN:
{
m_bParametersValid = false;
break;
}
case RUNNING_MODE:
{
m_bParametersValid &= result.second == "silent";
break;
}
case WORLD_TYPE:
{
try {
int t = std::stoi(result.second);
if (t >= 1 && t <= 5)
m_WorldType = static_cast<CSWWorld::WorldType>(t);
else
m_bParametersValid = false;
} catch (...) {
m_bParametersValid = false;
}
break;
}
case BATTLE_TYPE:
{
if (result.second == "single")
m_BattleType = CSWWorld::SINGLE;
else if (result.second == "team")
m_BattleType = CSWWorld::TEAM;
else
m_bParametersValid = false;
break;
}
case TEAMSIZE_TYPE:
{
try {
int t = std::stoi(result.second);
if (t == 3 || t == 5 || t == 10)
m_nTeamSize = t;
else
m_bParametersValid = false;
} catch (...) {
m_bParametersValid = false;
}
break;
}
case TIMESTEP_TYPE:
{
try {
double t = std::stod(result.second);
if (t >= 0.01 && t <= 0.1)
m_fTimeStep = t;
else
m_bParametersValid = false;
} catch (...) {
m_bParametersValid = false;
}
break;
}
}
nReadData |= result.first;
}
//at least running mode must given
m_bParametersValid &= nReadData & RUNNING_MODE;
}
CSWSilentApplication::~CSWSilentApplication()
{
}
void CSWSilentApplication::run()
{
std::cout << "CodeSubWars v" << Constants::getVersion().getAsString() << " beta\n";
std::cout << "Copyright (c) 2005-2022 " << qPrintable(Constants::AUTHOR) << ". All rights reserved.\n";
std::cout << "Released under the MIT license. (see license.txt)\n\n";
if (!m_bParametersValid)
{
showSyntax();
return;
}
std::cout.precision(2);
std::cout.setf(std::ios::fixed);
CSWLog::getInstance()->log("starting in silent mode ...");
std::string strBattleType("unknown");
if (m_BattleType == CSWWorld::SINGLE)
strBattleType = "single";
else if (m_BattleType == CSWWorld::TEAM)
strBattleType = "team";
std::stringstream ss;
ss << "parameters: world = " << m_WorldType << " battle = " << strBattleType;
if (m_BattleType == CSWWorld::TEAM)
ss << " teamsize = " << m_nTeamSize;
ss << " timestep = " << m_fTimeStep;
CSWLog::getInstance()->log(ss.str());
//calculate average over last 5 seconds
boost::circular_buffer<double> timeRatios(5.0/m_fTimeStep);
CSWLog::getInstance()->log("initializing ...");
CSWWorld::getInstance()->newWorld(m_WorldType);
CSWWorld::getInstance()->newBattle(CSWUtilities::determineAvailableSubmarines("submarines"),
m_BattleType, m_nTeamSize, ARSTD::Time::MANUAL);
double fOldRealTime = ARSTD::Time::getRealTime();
double fStartRealTime = fOldRealTime;
CSWLog::getInstance()->log("running ... (press ESC to stop)");
std::cout << "\n";
#ifdef _WIN32
while (!_kbhit() && CSWWorld::getInstance()->isBattleRunning())
#else
// On non-Windows systems, just run until battle ends
while (CSWWorld::getInstance()->isBattleRunning())
#endif
{
//recalculate the world
CSWWorld::getInstance()->recalculate();
//add time step to time
ARSTD::Time::step(m_fTimeStep);
//calculate average
double fCurrentRealTime = ARSTD::Time::getRealTime();
timeRatios.push_back(m_fTimeStep/(fCurrentRealTime - fOldRealTime));
fOldRealTime = fCurrentRealTime;
static int a = 0;
++a;
if (a == 100)
{
double fTimeRatio = std::accumulate(timeRatios.begin(), timeRatios.end(), 0) / timeRatios.capacity();
std::cout << "\r Time: " << ARSTD::Time::getTime()
<< " TimeRatio: " << fTimeRatio
<< " Objects: " << CSWWorld::getInstance()->getObjectTree()->getTotalChildNumber() << "\t";
a = 0;
}
}
double fSimulatedTime = ARSTD::Time::getTime();
if (CSWWorld::getInstance()->isBattleRunning())
{
std::cout << "\n\n";
CSWLog::getInstance()->log("finalizing ...");
CSWWorld::getInstance()->finalizeBattle();
CSWWorld::getInstance()->finalizeWorld();
}
CSWLog::getInstance()->log(("total simulated time: " +
QString::number(fSimulatedTime, 'f', 2) +
" ratio: " + QString::number(fSimulatedTime/(ARSTD::Time::getRealTime() - fStartRealTime), 'f', 2)).toStdString());
}
void CSWSilentApplication::checkRequirements()
{
}
std::pair<CSWSilentApplication::ParameterType, std::string> CSWSilentApplication::determineParameterType(const std::string& value)
{
std::pair<ParameterType, std::string> result(UNKNOWN, "");
if (value.size() <= 2)
return result;
if (value[0] != '-')
return result;
const std::string RUNNING_MODE_KEY = "silent";
const std::string WORLD_KEY = "world";
const std::string BATTLE_KEY = "battle";
const std::string TEAMSIZE_KEY = "teamsize";
const std::string TIMESTEP_KEY = "timestep";
if (value.substr(1, value.size() - 1) == RUNNING_MODE_KEY)
{
result.first = RUNNING_MODE;
result.second = value.substr(1, value.size() - 1);
return result;
}
size_t nIdx = value.find(WORLD_KEY + "=");
if (nIdx != std::string::npos)
{
result.first = WORLD_TYPE;
result.second = value.substr(nIdx + WORLD_KEY.size() + 1, value.size() - (nIdx + WORLD_KEY.size() + 1));
return result;
}
nIdx = value.find(BATTLE_KEY + "=");
if (nIdx != std::string::npos)
{
result.first = BATTLE_TYPE;
result.second = value.substr(nIdx + BATTLE_KEY.size() + 1, value.size() - (nIdx + BATTLE_KEY.size() + 1));
return result;
}
nIdx = value.find(TEAMSIZE_KEY + "=");
if (nIdx != std::string::npos)
{
result.first = TEAMSIZE_TYPE;
result.second = value.substr(nIdx + TEAMSIZE_KEY.size() + 1, value.size() - (nIdx + TEAMSIZE_KEY.size() + 1));
return result;
}
nIdx = value.find(TIMESTEP_KEY + "=");
if (nIdx != std::string::npos)
{
result.first = TIMESTEP_TYPE;
result.second = value.substr(nIdx + TIMESTEP_KEY.size() + 1, value.size() - (nIdx + TIMESTEP_KEY.size() + 1));
return result;
}
return result;
}
void CSWSilentApplication::showSyntax()
{
std::cout << "A physics based three dimensional programming game.\n";
std::cout << "\n";
std::cout << "Syntax: CodeSubWars [-silent] [-world=<1-5>] [-battle=<single|team>] [-teamsize=<3|5|10>] [-timestep=x]\n";
std::cout << "\n";
std::cout << " -silent Using this parameter makes the application starts without graphical output.\n";
std::cout << " The other parameters are only valid when this is set.\n";
std::cout << "\n";
std::cout << " -world With this parameter the predefined world environment can be choosen. Default is 1.\n";
std::cout << "\n";
std::cout << " -battle This parameter indicates which battle mode should be run. Default is single.\n";
std::cout << "\n";
std::cout << " -teamsize This parameter defines the number of team member in team mode. Default is 3.\n";
std::cout << "\n";
std::cout << " -timestep Here the time step in seconds for each simulation step can be set. The given\n";
std::cout << " value must be in range [0.01, 0.1]. Default is 0.01.\n";
}
}