-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwiz.cpp
More file actions
441 lines (380 loc) · 11.5 KB
/
wiz.cpp
File metadata and controls
441 lines (380 loc) · 11.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
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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
//
// author: Kovacs Marton
// email: tetra666@gmail.com
// license: whatever. note my name
//
// wiz.cpp
//
// the main class that represents and moves the draw logic
//
#include "drawinterface.hpp"
#include "flyerz.hpp"
#include "wiz.hpp"
#include "ais.hpp"
#include "events.hpp"
#include <climits>
#include <algorithm>
#include <iterator>
#include <set>
#include <sstream>
#include <iostream>
const std::string randomNames[] =
{
"robot0x00", "Stanley", "GAURRR", "muszmusz", "d(O_O)b", "Bob", "Greyson", "Robert Paulson", "Jeronimo", "Suzuki",
"robot0xbfffffff", "Utvefuro", "|o|", "|oo|", "<o>", "Roy", "Jaime", "Diablo", "RAMPAGE", "PENTAKILL",
"robot0xcfcfcfcf", "vidya", ">oo>", "<oo<", "isten, csak igy", "n00b", "l33+h4xxoR", "Faust", "gut, besser, gosser", "Hovertank",
"CrayII", "AVENGER", "blaster", "SKYRANGER", "LIGHTNING", "INTERCEPTOR", "FIRESTORM", "Sleeper Service", "Zero Gravitas", "Very Little Gravitas Indeed"
};
Wiz::Wiz()
{
}
Wiz::~Wiz()
{
//TODO
}
typedef DiskShipAiRandom DefaultAi;
void Wiz::Init(const Options& options)
{
logFile = options.logFile;
timeLimit = ((-1 != options.time) ? (options.time) : (INT_MAX));
scoreLimit = ((-1 != options.score) ? (options.score) : (INT_MAX));
startTime = std::time(0);
Margin = std::min(DrawWrapper::GetSize().x, DrawWrapper::GetSize().y) / 10;
std::vector<std::string>::const_iterator nit = options.names.begin();
int teamCounter = 1;
if (1 == options.teams.size())
{
teamCounter = 0;
}
int id = 0;
std::set<std::string> nameBin(&randomNames[0], &randomNames[sizeof(randomNames)/sizeof(randomNames[0])]);
for (std::vector<int>::const_iterator tit = options.teams.begin(); options.teams.end() != tit; ++tit)
{
teamScores.push_back(ScoreSorter());
for (int i = 0; i < *tit; ++i)
{
std::string name = *nit++;
bool randomAi = false;
if (name == "-")
{
if (nameBin.empty())
{
name = "---- not enough names ----";
}
else
{
std::set<std::string>::iterator rname = nameBin.begin();
std::advance(rname, DrawWrapper::Random(nameBin.size()));
name = *rname;
nameBin.erase(rname);
}
randomAi = true;
}
DiskShip* shipPtr = new DiskShip(PlaceMe(teamCounter), GetShipColor(teamCounter), GetLaserColor(teamCounter), name, id++, *this, teamCounter);
DiskShipAi* aiPtr;
if (randomAi)
{
aiPtr = new DefaultAi(shipPtr);
}
else
{
Ipc ipc(name);
//TODO create ipc
aiPtr = new DiskShipAiRemote(shipPtr, ipc);
}
shipPtr->SetAi(aiPtr);
ships.push_back(shipPtr);
scores.push_back(0);
teamScores.back().insert(ScoreType(&scores[i+1], ships.back()));
}
++teamCounter;
}
// scores = ScoreList(ships.size(), 0);
}
void Wiz::DrawFrame()
{
static int i=0;
/*
DrawCircle(Coordinate(200, 200), 100, Color(255, 125, 0), true);
DrawCircle(Coordinate(210, 210), 100, Color(0, 255, 0), false);
DrawLine(Coordinate(300 + i % 100, 300), Coordinate(400, 500), Color(0, 255, 255));
Coordinate points[] = {Coordinate(100, 100), Coordinate(300, 100), Coordinate(300, 300), Coordinate(100, 300)};
DrawShape(&points[0], &points[4], Color(0, 255, 0), true);
*/ ++i;
MoveAll();
Clean();
DrawScore();
if (timeLimit < std::time(0) - startTime)
{
std::cout << "Time is up\n";
ShutDown();
}
}
Coordinate::CoordType DistanceFromSegment(const Coordinate& begin, const Coordinate& end, const Coordinate& point)
{
const double len = DistanceSqr(begin, end);
const double tmp = Dot(point - end, begin - end) / len;
if (tmp < 0.0)
{
return Distance(point, end);
}
else if (tmp > 1.0)
{
return Distance(point, begin);
}
const Coordinate projection = end + tmp * (begin - end);
return Distance(point, projection);
}
bool Wiz::CheckCollision(const Coordinate& begin, const Coordinate& end, int team, int owner) const
{
const Coordinate vektor = begin - end;
//get the potential guys
ShipList potentials = GetPotentials(team, end + vektor / 2, DiskShip::shipSize + (DiskShip::laserLength + 1) / 2);
bool hit = false;
for (ShipList::const_iterator it = potentials.begin(); potentials.end() != it; ++it)
{
if (DistanceFromSegment(begin, end, (*it)->GetCenter()) <= (*it)->GetSize())
{
(*it)->Hit();
if (scoreLimit <= ++scores[owner])
{
std::cout << "Score limit hit\n";
ShutDown();
}
hit = true;
break;
}
}
return hit;
}
void Wiz::AddProjectile(Owned* projectile)
{
projectiles.push_back(projectile);
projectileCache.push_back(std::make_pair(projectile->GetTeam(), projectile->GetCollision()));
}
void Wiz::RemoveProjectile(Owned *projectile)
{
ProjectileList::iterator iter = std::find(projectiles.begin(), projectiles.end(), projectile);
if (projectiles.end() != iter)
{
deads.push_back(projectile);
}
}
Coordinate Wiz::PlaceMe(int team) const
{
Size screenSize = DrawWrapper::GetSize();
if (1 == team)
{
return Coordinate(Margin + DrawWrapper::Random(100) - 50, Margin + DrawWrapper::Random(screenSize.y - 2 * Margin));
}
if (2 == team)
{
return Coordinate(screenSize.x - Margin + DrawWrapper::Random(100) - 50, Margin + DrawWrapper::Random(screenSize.y - 2 * Margin));
}
return Coordinate(Margin + DrawWrapper::Random(screenSize.x - 2 * Margin), Margin + DrawWrapper::Random(screenSize.y - 2 * Margin));
}
//
// (ship : ammo) color theme for teams, 0 for default
// 0 gets random, also teams above the given limit gets random too
//
const Color teamColors[][2] = {{Colors::red, Colors::green}, {Colors::blue, Colors::red}, {Colors::white, Colors::white}, {Colors::purple, Colors::pink}};
const int PredefNum = sizeof(teamColors)/sizeof(teamColors[0]);
Color GetRandomColor()
{
return Color(DrawWrapper::Random(255), DrawWrapper::Random(255), DrawWrapper::Random(255));
}
Color Wiz::GetShipColor(int team) const
{
if (0 == team || PredefNum < team)
{
return teamColors[0][0];
//return GetRandomColor();
}
else
{
return teamColors[team - 1][0];
}
}
Color Wiz::GetLaserColor(int team) const
{
if (0 == team || PredefNum < team)
{
return teamColors[0][1];
//return GetRandomColor();
}
else
{
return teamColors[team - 1][1];
}
}
struct EnemyPredicate
{
EnemyPredicate(int team, bool negate = false): m_team(team), m_negate(negate)
{ }
bool operator()(const Hitable* ship)
{
return (m_negate ^ (0 != ship->GetTeam() && ship->GetTeam() == m_team)) || !ship->Alive();
}
int m_team;
bool m_negate;
};
Wiz::ShipTravel Wiz::GetEnemies(int team) const
{
ShipTravel res;
std::remove_copy_if(ships.begin(), ships.end(), std::back_inserter(res), EnemyPredicate(team));
return res;
}
Wiz::ShipTravel Wiz::GetTeam(int team) const
{
ShipTravel res;
std::remove_copy_if(ships.begin(), ships.end(), std::back_inserter(res), EnemyPredicate(team, true));
return res;
}
Wiz::LaserList Wiz::GetBullets() const
{
return projectileCache;
}
void Wiz::MoveAll()
{
//TODO: C++11 feature
LaserList::iterator lit = projectileCache.begin();
for(ProjectileList::iterator it = projectiles.begin(); projectiles.end() != it; ++it)
{
(*it)->Draw();
(*it)->PrepareMove();
(*it)->ExecuteMove();
lit->second = (*it)->GetCollision();
++lit;
}
for(ShipList::iterator it = ships.begin(); ships.end() != it; ++it)
{
(*it)->Draw();
(*it)->PrepareMove();
(*it)->ExecuteMove();
}
}
void Wiz::Clean()
{
for (ProjectileList::iterator it = deads.begin(); deads.end() != it; ++it)
{
KillProjectile(*it);
}
deads.clear();
}
void DrawRectangle(Coordinate topleft, int width, int height, Color color)
{
Coordinate rect[] = {topleft, topleft + Coordinate(width, 0), topleft + Coordinate(width, height), topleft + Coordinate(0, height)};
DrawWrapper::DrawShape(&rect[0], &rect[4], color);
}
void Wiz::DrawScore()
{
/*
Coordinate startPoint(DrawWrapper::GetSize().x - 100, DrawWrapper::GetSize().y - ships.size() * (FontHeight + 1) + FontHeight / 2);
if (100 < startPoint.x)
{
if (0 > startPoint.y)
{
startPoint.y = FontHeight;
}
int team = 0;
if (1 < teamScores.size())
{
team = 1;
}
std::ostringstream ostr;
Coordinate topleft = startPoint - Coordinate(1, FontHeight);
for (TeamScoreList::iterator sit = teamScores.begin(); teamScores.end() != sit; ++sit)
{
for (ScoreSorter::iterator pit = sit->begin(); sit->end() != pit; ++pit)
{
ostr << pit->GetScore() << ": " << pit->GetName();
DrawWrapper::DrawText(ostr.str(), startPoint, Colors::pink);
startPoint += Coordinate(0, FontHeight + 1);
ostr.str("");
ostr.clear();
}
DrawRectangle(topleft, 100, startPoint.y - topleft.y - FontHeight * 2, teamColors[team][0]);
++team;
topleft = startPoint - Coordinate(1, FontHeight * 2);
}
DrawRectangle(topleft, 100, startPoint.y - topleft.y - FontHeight, teamColors[team][0]);
}
*/
Coordinate startPoint(DrawWrapper::GetSize().x - 100, DrawWrapper::GetSize().y - ships.size() * (FontHeight + 1) + FontHeight / 2);
if (100 < startPoint.x)
{
if (0 > startPoint.y)
{
startPoint.y = FontHeight;
}
std::ostringstream ostr;
int team = ships.front()->GetTeam();
Coordinate topleft = startPoint - Coordinate(1, FontHeight);
for (unsigned i = 0; i < ships.size(); ++i)
{
ostr << scores[i] << ": " << ships[i]->GetName();
DrawWrapper::DrawText(ostr.str(), startPoint, ((ships[i]->Alive()) ? (Colors::pink) : (Colors::purple)));
startPoint += Coordinate(0, FontHeight + 1);
ostr.str("");
ostr.clear();
if (ships[i]->GetTeam() != team)
{
DrawRectangle(topleft, 100, startPoint.y - topleft.y - FontHeight * 2, GetShipColor(team));
team = ships[i]->GetTeam();
topleft = startPoint - Coordinate(1, FontHeight * 2);
}
}
DrawRectangle(topleft, 100, startPoint.y - topleft.y - FontHeight, GetShipColor(team));
}
}
void Wiz::KillProjectile(Owned* projectile)
{
ProjectileList::iterator iter = std::find(projectiles.begin(), projectiles.end(), projectile);
if (projectiles.end() != iter)
{
int pos = iter - projectiles.begin();
delete *iter;
projectiles.erase(iter);
projectileCache.erase(projectileCache.begin() + pos);
}
}
struct PotentialityChecker
{
PotentialityChecker(int team, const Coordinate& center, int dist): m_subpred(team), m_center(center), m_dist(Sqr(dist))
{ }
bool operator()(const Hitable* ship)
{
return m_subpred(ship) || DistanceSqr(ship->GetCenter(), m_center) > m_dist;
}
EnemyPredicate m_subpred;
Coordinate m_center;
int m_dist;
};
Wiz::ShipList Wiz::GetPotentials(int team, const Coordinate& center, int dist) const
{
ShipList res;
std::remove_copy_if(ships.begin(), ships.end(), std::back_inserter(res), PotentialityChecker(team, center, dist));
return res;
}
void Wiz::ShutDown() const
{
std::cout << "Final score:\n";
for (unsigned i(0); i < scores.size(); ++i)
{
std::cout << ships[i]->GetName() << ": " << scores[i] << '\n';
}
std::exit(0);
}
int Wiz::Margin;
int ScoreType::GetScore() const
{
return *m_score;
}
std::string ScoreType::GetName() const
{
return m_ship->GetName();
}
bool operator<(const ScoreType& lhs, const ScoreType& rhs)
{
return lhs.GetScore() > rhs.GetScore();
}