-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathTMXPathFinding.cpp
More file actions
293 lines (260 loc) · 7.71 KB
/
TMXPathFinding.cpp
File metadata and controls
293 lines (260 loc) · 7.71 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
/**
*
* Created by Smit Patel on 25/09/2015
*
* github: https://github.com/smitpatel88/TMXPathFinding
*
*/
#include "TMXPathFinding.h"
#include <math.h>
TMXPathFinding::TMXPathFinding(TMXTiledMap *map, DIRECTION noOfDirection) {
tileMap = map;
dir = noOfDirection;
switch (tileMap->getMapOrientation()) {
case 0: // Orthogonal
CCASSERT(dir != DIRECTION::SIX, "For Orthogonal Maps SIX direction is not possible");
break;
case 1: // Hexagonal
CCASSERT(dir == DIRECTION::SIX, "For Hexagonal Maps only SIX direction is accepted");
break;
case 2: // Isometric
CCASSERT(dir != DIRECTION::SIX, "For Isometric Maps SIX direction is not possible");
break;
}
iCost = 0;
min = 10000;
inOpen = false;
inClose = false;
tileLayers.clear();
setTileLayers( { } );
extractNode = new TileNode();
}
TMXPathFinding::~TMXPathFinding() {
for (auto o : open)
delete o.first;
for (auto c : close)
delete c.first;
open.clear();
close.clear();
tileLayers.clear();
}
void TMXPathFinding::setTileLayers(std::vector<std::string> layerNames) {
tileLayers.clear();
if (layerNames.size() == 0) {
for (auto &layerNode : tileMap->getChildren()) {
TMXLayer *layer = dynamic_cast<TMXLayer*>(layerNode);
if (layer)
tileLayers.push_back(layer);
}
}
else {
for (auto layerName : layerNames) {
TMXLayer *layer = tileMap->getLayer(layerName);
if (layer)
tileLayers.push_back(layer);
}
}
}
std::vector<Vec2> TMXPathFinding::getPathUsingObstacles(Vec2 startPos, Vec2 endPos, std::vector<int> obstacleGIDs) {
return getPath(startPos, endPos, {}, obstacleGIDs);
}
std::vector<Vec2> TMXPathFinding::getPathUsingWalkable(Vec2 startPos, Vec2 endPos, std::vector<int> walkableGIDs) {
return getPath(startPos, endPos, walkableGIDs, {});
}
int TMXPathFinding::getDistance(TileNode *start, TileNode *end) {
//return euclideanDistance(start, end);
return manhattanDistance(start, end);
}
int TMXPathFinding::euclideanDistance(TileNode *start, TileNode *end) {
int dx = end->getLocation().x - start->getLocation().x;
int dy = end->getLocation().y - start->getLocation().y;
int ans = sqrt(dx * dx + dy * dy);
return ans;
}
int TMXPathFinding::manhattanDistance(TileNode *start, TileNode *end) {
int dx = end->getLocation().x - start->getLocation().x;
int dy = end->getLocation().y - start->getLocation().y;
int ans = abs(dx) + abs(dy);
return ans;
}
std::vector<Vec2> TMXPathFinding::getPath(Vec2 startPos, Vec2 endPos, std::vector<int> walkableGIDs, std::vector<int> obstacleGIDs) {
open.clear();
close.clear();
// inizialize a goal node
goalNode = new TileNode();
goalNode->setLocX(endPos.x);
goalNode->setLocY(endPos.y);
// inizialize a start node
startNode = new TileNode();
startNode->setLocX(startPos.x);
startNode->setLocY(startPos.y);
startNode->setCostFromStart(0);
startNode->setParent(nullptr);
int cost = getDistance(startNode, goalNode);
startNode->setCostToGoal(cost);
startNode->setTotalCost();
open.emplace(startNode, startNode->getTotalCost());
while (open.size() != 0) {
// Fix a Cost to check the values
min = 32767; // std::numeric_limits<int>::max()
TileNode *minNode;
// Find minNode from open QUEUE
for (auto kv : open) {
extractNode = kv.first;
iCost = kv.second;
if (iCost < min) {
min = iCost; // Change min to the New Cost got from the open QUEUE
minNode = extractNode;
}
}
extractNode = minNode;
open.erase(minNode); // pop node from open
// if it's a goal, we're done
if (extractNode->getLocation() == goalNode->getLocation()) {
// 1- retrieve all extractNode's parents
// 2- save into Vec2 vector
// 3- reverse Vec2 vector
// 4- return Vec2 vector
std::vector<Vec2> points;
points.push_back(extractNode->getLocation());
int size = extractNode->getCostFromStart();
for (int i = 0; i < size; i++) {
points.push_back(Vec2(extractNode->getParent()->getLocation().x, extractNode->getParent()->getLocation().y));
extractNode = extractNode->getParent();
}
std::reverse(points.begin(), points.end());
return points;
}
else {
for (int i = 0; i < dir; i++) {
costToOpen = 0;
costToClose = 0;
inOpen = false;
inClose = false;
newNode = new TileNode();
newNode->setLocX(extractNode->getLocation().x);
newNode->setLocY(extractNode->getLocation().y);
switch (i) {
case 0: // left
newNode->setLocX(-1);
newNode->setLocY(0);
break;
case 1: // right
newNode->setLocX(1);
newNode->setLocY(0);
break;
case 2: // up
newNode->setLocX(0);
newNode->setLocY(1);
break;
case 3: // down
newNode->setLocX(0);
newNode->setLocY(-1);
break;
case 4: // top-left
newNode->setLocX(-1);
newNode->setLocY(1);
break;
case 5: // bottom-left
newNode->setLocX(-1);
newNode->setLocY(-1);
break;
case 6: // bottom-right
newNode->setLocX(1);
newNode->setLocY(-1);
break;
case 7: // top-right
newNode->setLocX(1);
newNode->setLocY(1);
break;
}
if (newNode->getLocation() != goalNode->getLocation()) {
if (newNode->getLocation().x < 0 || newNode->getLocation().y < 0 ||
newNode->getLocation().x >= tileMap->getMapSize().width ||
newNode->getLocation().y >= tileMap->getMapSize().height) {
// newNode is invalid, outside tileMap so ignore
continue;
}
bool isNeedToContinue = false;
// check newNode in given/all layers
for (auto layer : tileLayers) {
for (int gid : obstacleGIDs) {
if (layer->getTileGIDAt(newNode->getLocation()) == gid) {
// newNode is obstacle so ignore
isNeedToContinue = true;
break;
}
}
if (isNeedToContinue)
break;
if (walkableGIDs.size() > 0) {
isNeedToContinue = true;
for (int gid : walkableGIDs) {
if (layer->getTileGIDAt(newNode->getLocation()) == gid) {
// newNode is walkable
isNeedToContinue = false;
break;
}
}
if (isNeedToContinue) // newNode is not walkable so ignore
break;
}
}
if (isNeedToContinue)
continue;
}
cost = getDistance(newNode, goalNode);
newNode->setCostFromStart(extractNode->getCostFromStart() + 1);
newNode->setCostToGoal(cost);
newNode->setParent(extractNode);
newNode->setTotalCost();
inOpen = false;
inClose = false;
for (auto kv : open) {
TileNode *node = kv.first;
if (newNode->getLocation() == node->getLocation()) {
costToOpen = node->getTotalCost();
inOpen = true;
break;
}
}
for (auto kv : close) {
TileNode *node = kv.first;
if (newNode->getLocation() == node->getLocation()) {
costToClose = node->getTotalCost();
inClose = true;
break;
}
}
if ((inOpen && (newNode->getTotalCost() >= costToOpen)) || (inClose && (newNode->getTotalCost() >= costToClose))) {
// newNode is already in open or close QUEUE with lower cost so ignore
continue;
}
else {
if (inClose) {
close.erase(newNode);
// reinsert newNode in open
open.emplace(newNode, newNode->getTotalCost());
}
if (inOpen) {
// adjust newNode's location in Open
iCost = costToOpen;
modifiedNode = newNode;
// remove from open
open.erase(newNode);
modifiedNode->setTotalCost(newNode->getTotalCost());
// updated node reinsert in open
open.emplace(modifiedNode, modifiedNode->getTotalCost());
}
else {
// if its neither in OPEN nor in CLOSE insert it in OPEN
open.emplace(newNode, newNode->getTotalCost());
}
}
}
}
close.emplace(extractNode, extractNode->getTotalCost());
}
std::vector<Vec2> dummy;
return dummy;
}