-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsim.cpp
More file actions
272 lines (245 loc) · 6.54 KB
/
Copy pathsim.cpp
File metadata and controls
272 lines (245 loc) · 6.54 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
#include <stdlib.h>
#include <iostream>
#include <sstream>
#include <iomanip>
#include "sim.hpp"
Sim::Sim()
{
map = new Map();
numTowers = 5;
numEntities = 10;
spawnPerTick = 1;
overrideEntities = -1;
}
void Sim::tick()
{
//move entities
for(int i=0;i<map->getNumEntities();++i) {
map->moveEntity(map->getEntity(i));
}
//spawn new entities
spawn();
//find targets and fire
for(int i=0;i<map->getNumTowers();++i) {
Tower *t = map->getTower(i);
if(!(t->isTargetAlive() && t->isTargetInRange())) {
t->setTarget(map->getClosestEntity(t->getPosition()[0],t->getPosition()[1]));
}
t->fireAtTarget();
}
}
Map* Sim::getMap()
{
return map;
}
bool Sim::populateMap()
{
int x = map->getSize()[0];
int y = map->getSize()[1];
int xx,yy;
if(x*y<numTowers) return false; //FIXME: this should count empty squares
int i=0;
while(map->getNumTowers()<numTowers) {
xx = rand() % x;
yy = rand() % y;
Tower *t;
if(i<2) t = new SlowTower;
else if(i%2) t = new ShortTower;
else t = new LongTower;
t->setPosition(xx,yy);
if(!map->addTower(t)) delete t;
else i++;
}
return true;
}
bool Sim::spawn()
{
for(int i=0;i<spawnPerTick;++i) {
if(map->getNumEntities() < numEntities) {
Entity* e = new Entity();
e->setPosition(map->getStartPoint()->getPosition()[0],
map->getStartPoint()->getPosition()[1]);
map->addEntity(e);
}
else {
return false;
}
}
return true;
}
void Sim::overrideEntitiesAtGoal(int e)
{
overrideEntities = e;
}
int Sim::entitiesAtGoal()
{
if(overrideEntities >=0) return overrideEntities;
int entitiesAtGoal = 0;
int x = map->getGoalPoint()->getPosition()[0];
int y = map->getGoalPoint()->getPosition()[1];
for(int i=0;i<map->getNumEntities();++i) {
if(map->getEntity(i)->getPosition()[0] == x &&
map->getEntity(i)->getPosition()[1] == y) {
entitiesAtGoal++;
}
}
return entitiesAtGoal;
}
int Sim::activeEntities()
{
int activeEntities = 0;
int x = map->getGoalPoint()->getPosition()[0];
int y = map->getGoalPoint()->getPosition()[1];
for(int i=0;i<map->getNumEntities();++i) {
if(!(map->getEntity(i)->getPosition()[0] == x &&
map->getEntity(i)->getPosition()[1] == y) &&
map->getEntity(i)->alive()) {
activeEntities++;
}
}
activeEntities += numEntities - map->getNumEntities();
return activeEntities;
}
std::string Sim::getTowerListString()
{
std::stringstream t;
int *tlist = NULL;
if(map->getNumTowers()) {
tlist = new int[map->getNumTowers()*3];
for(int i=0;i<map->getNumTowers();++i) {
tlist[i*3] = map->getTower(i)->getPosition()[0];
tlist[i*3+1] = map->getTower(i)->getPosition()[1];
tlist[i*3+2] = map->getTower(i)->getType();
t << map->getTower(i)->getPosition()[0] << ' '
<< map->getTower(i)->getPosition()[1] << ' '
<< map->getTower(i)->getType();
if(i<map->getNumTowers()-1) t << ' ';
}
}
return t.str();
}
int* Sim::getTowerList()
{
int *tlist = NULL;
if(map->getNumTowers()) {
tlist = new int[map->getNumTowers()*3];
for(int i=0;i<map->getNumTowers();++i) {
tlist[i*3] = map->getTower(i)->getPosition()[0];
tlist[i*3+1] = map->getTower(i)->getPosition()[1];
tlist[i*3+2] = map->getTower(i)->getType();
}
}
return tlist;
}
void Sim::setTowersFromList(int* tlist, int num)
{
for(int i=0;i<num/3;++i) {
Tower *t;
if(tlist[i*3+2] == TOWER) t = new Tower;
else if(tlist[i*3+2] == SLOW_TOWER) t = new SlowTower;
else if(tlist[i*3+2] == LONG_TOWER) t = new LongTower;
else if(tlist[i*3+2] == SHORT_TOWER) t = new ShortTower;
t->setPosition(tlist[i*3],tlist[i*3+1]);
if(!map->addTower(t)) delete t;
}
}
void Sim::mutateTower()
{
if(map->getNumTowers() == 0) return;
int i = rand() % map->getNumTowers();
int x = rand() % map->getSize()[0];
int y = rand() % map->getSize()[1];
while(!map->checkTowerPos(x,y)) {
x = rand() % map->getSize()[0];
y = rand() % map->getSize()[1];
}
map->getTower(i)->setPosition(x,y);
}
void Sim::crossoverTowers(Sim* s1, Sim* s2)
{
if(s1==NULL || s2==NULL) return;
int x1,y1, x2,y2;
int minTowers = min(s1->getMap()->getNumTowers(),s2->getMap()->getNumTowers());
for(int i=0;i<minTowers;++i) {
x1 = s1->getMap()->getTower(i)->getPosition()[0];
y1 = s1->getMap()->getTower(i)->getPosition()[1];
x2 = s2->getMap()->getTower(i)->getPosition()[0];
y2 = s2->getMap()->getTower(i)->getPosition()[1];
TowerType type = s1->getMap()->getTower(i)->getType();
Tower* t;
if(type==TOWER) t = new Tower();
else if(type==SLOW_TOWER) t = new SlowTower();
else if(type==LONG_TOWER) t = new LongTower();
else if(type==SHORT_TOWER) t = new ShortTower();
//try to grab a tower position from sim1 or sim2
if((rand()%2==0) && map->checkTowerPos(x1,y1)) {
t->setPosition(x1,y1);
map->addTower(t);
}
else if(map->checkTowerPos(x2,y2)) {
t->setPosition(x2,y2);
map->addTower(t);
}
//if both those positions are taken, punt and place it randomly
else {
x1 = rand() % map->getSize()[0];
y1 = rand() % map->getSize()[1];
while(!map->checkTowerPos(x1,y1)) {
x1 = rand() % map->getSize()[0];
y1 = rand() % map->getSize()[1];
}
t->setPosition(x1,y1);
map->addTower(t);
}
}
}
void Sim::setSpawnPerTick(int s)
{
spawnPerTick = s;
}
int Sim::getSpawnPerTick()
{
return spawnPerTick;
}
void Sim::encodeForMPI(int* a)
{
for(int i=0;i<map->getNumTowers();++i) {
a[i*3] = map->getTower(i)->getPosition()[0];
a[i*3+1] = map->getTower(i)->getPosition()[1];
a[i*3+2] = map->getTower(i)->getType();
}
a[map->getNumTowers()*3] = entitiesAtGoal();
}
Sim::~Sim()
{
for(int i=0;i<map->getNumTowers();++i) {
delete map->getTower(i);
}
for(int i=0;i<map->getNumEntities();++i) {
delete map->getEntity(i);
}
for(int i=0;i<map->getNumPaths();++i) {
if(map->getPath(i)==map->getStartPoint()) continue;
if(map->getPath(i)==map->getGoalPoint()) continue;
delete map->getPath(i);
}
delete map;
}
BigSim::BigSim()
{
numTowers = 15;
numEntities = 100;
spawnPerTick = 2;
map = new Map();
map->setSize(50,50);
map->setStartPoint(0,0);
map->setGoalPoint(49,49);
for(int i=1;i<map->getSize()[0]-1;++i) {
Path* p = new Path();
p->setPosition(i,i);
map->addPath(p);
}
}
BigSim::~BigSim()
{
}