-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmap.cpp
More file actions
247 lines (215 loc) · 5.11 KB
/
Copy pathmap.cpp
File metadata and controls
247 lines (215 loc) · 5.11 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
#include "map.hpp"
Map::Map()
{
size[0] = size[1] = 10;
startPoint.setPosition(0,0);
goalPoint.setPosition(9,9);
addPath(&startPoint);
addPath(&goalPoint);
}
void Map::setSize(int x, int y)
{
size[0] = x;
size[1] = y;
}
const int* Map::getSize()
{
return size;
}
//towers cannot overlap with each other, or be on a path
bool Map::addTower(Tower* t)
{
if(!checkTowerPos(t->getPosition()[0],t->getPosition()[1])) return false;
towers.push_back(t);
return true;
}
//entities can overlap with each other, but not with towers
bool Map::addEntity(Entity* e)
{
if(e->getPosition()[0] >= size[0] ||
e->getPosition()[1] >= size[1]) return false;
for(int i=0;i<towers.size();++i) {
if(towers[i]->getPosition()[0] == e->getPosition()[0] &&
towers[i]->getPosition()[1] == e->getPosition()[1]) {
return false;
}
}
for(int i=0;i<paths.size();++i) {
if(paths[i]->getPosition()[0] == e->getPosition()[0] &&
paths[i]->getPosition()[1] == e->getPosition()[1]) {
entities.push_back(e);
return true;
}
}
return false;
}
//paths cannot overlap with each other, or towers
bool Map::addPath(Path* p)
{
if(p->getPosition()[0] >= size[0] ||
p->getPosition()[1] >= size[1]) return false;
for(int i=0;i<towers.size();++i) {
if(towers[i]->getPosition()[0] == p->getPosition()[0] &&
towers[i]->getPosition()[1] == p->getPosition()[1]) {
return false;
}
}
for(int i=0;i<paths.size();++i) {
if(paths[i]->getPosition()[0] == p->getPosition()[0] &&
paths[i]->getPosition()[1] == p->getPosition()[1]) {
return false;
}
}
paths.push_back(p);
return true;
}
Tower* Map::getTower(int i)
{
if(i<towers.size()) return towers[i];
else return NULL;
}
Entity* Map::getEntity(int i)
{
if(i<entities.size()) return entities[i];
else return NULL;
}
Path* Map::getPath(int i)
{
if(i<paths.size()) return paths[i];
else return NULL;
}
int Map::getNumTowers()
{
return towers.size();
}
int Map::getNumEntities()
{
return entities.size();
}
int Map::getNumPaths()
{
return paths.size();
}
Entity* Map::getClosestEntity(int x, int y)
{
Object o;
o.setPosition(x,y);
Entity* closest = NULL;
double minDist = -1, dist;
int gx = getGoalPoint()->getPosition()[0];
int gy = getGoalPoint()->getPosition()[1];
for(int i=entities.size()-1; i>=0; --i) {
if(entities[i]->getPosition()[0] == gx &&
entities[i]->getPosition()[1] == gy) {
continue;
}
dist = o.distance(entities[i]);
if(minDist < 0 || dist < minDist) {
closest = entities[i];
minDist = dist;
}
}
return closest;
}
bool Map::setStartPoint(int x, int y)
{
if(x >= size[0] || y >= size[1]) return false;
startPoint.setPosition(x,y);
return true;
}
Path* Map::getStartPoint()
{
return &startPoint;
}
bool Map::setGoalPoint(int x, int y)
{
if(x >= size[0] || y >= size[1]) return false;
goalPoint.setPosition(x,y);
return true;
}
Path* Map::getGoalPoint()
{
return &goalPoint;
}
Path* Map::bestAdjacentPath(Object* o)
{
double best,d;
Path* bestPath = NULL;
best = -1;
for(int i=0;i<paths.size();++i) {
if(paths[i]->adjacent(o)) {
d = goalPoint.distance(paths[i]);
if(best < 0 || d < best) {
best = d;
bestPath = paths[i];
}
}
}
return bestPath;
}
void Map::moveEntity(Entity* e)
{
if(e && e->alive()) {
Path* p = bestAdjacentPath(e);
if(p) {
e->setPosition(p->getPosition()[0],p->getPosition()[1]);
}
}
}
std::string Map::print()
{
std::string m[size[0]][size[1]], m_final;
for(int i=0;i<size[0];++i) {
for(int j=0;j<size[1];++j) {
m[i][j] = '.';
}
}
for(int i=0;i<paths.size();++i) {
m[paths[i]->getPosition()[0]]
[paths[i]->getPosition()[1]] = 'P';
}
for(int i=0;i<towers.size();++i) {
char c;
if(towers[i]->getType()==TOWER) c = 'T';
else if(towers[i]->getType()==SLOW_TOWER) c = 'W';
else if(towers[i]->getType()==LONG_TOWER) c = 'L';
else if(towers[i]->getType()==SHORT_TOWER) c = 'H';
m[towers[i]->getPosition()[0]]
[towers[i]->getPosition()[1]] = c;
}
for(int i=0;i<entities.size();++i) {
m[entities[i]->getPosition()[0]]
[entities[i]->getPosition()[1]] = 'E';
}
m[getStartPoint()->getPosition()[0]]
[getStartPoint()->getPosition()[1]] = 'S';
m[getGoalPoint()->getPosition()[0]]
[getGoalPoint()->getPosition()[1]] = 'G';
for(int i=0;i<size[0];++i) {
for(int j=0;j<size[1];++j) {
m_final+=m[i][j]+" ";
}
m_final+='\n';
}
return m_final;
}
bool Map::checkTowerPos(int x, int y)
{
if(x >= size[0] || y >= size[1]) return false;
for(int i=0;i<towers.size();++i) {
if(towers[i]->getPosition()[0] == x &&
towers[i]->getPosition()[1] == y) {
return false;
}
}
for(int i=0;i<paths.size();++i) {
if(paths[i]->getPosition()[0] == x &&
paths[i]->getPosition()[1] == y) {
return false;
}
}
return true;
}
Map::~Map()
{
}