[go: up one dir, main page]

Menu

[r3]: / lpi_algorithms.cpp  Maximize  Restore  History

Download this file

323 lines (261 with data), 9.7 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
/*
Copyright (c) 2005-2007 Lode Vandevenne
All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
* Neither the name of Lode Vandevenne nor the names of his contributors may be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
g++ *.cpp -W -Wall -ansi -pedantic
*/
#include "lpi_algorithms.h"
#include <list>
#include <iostream>
namespace
{
struct Node
{
int x;
int y;
int f; //total cost, g + h
int g; //cost from beginning to here
int h; //estimated cost from here to end (heuristic)
Node* parent;
Node(int x, int y)
{
parent = 0;
this->x = x;
this->y = y;
}
Node(const Node& other)
{
*this = other;
}
Node()
{
parent = 0;
}
};
bool operator==(const Node& a, const Node& b)
{
return a.x == b.x && a.y == b.y; //we only compare the coordinates, not the cost
}
}
namespace lpi
{
//world is a 2D tilemap, with rectangular tiles that are either filled or empty, supporting positive and negative coordinates of tiles. x0y0x1y1 are the borders inside the world in which to work.
bool World_AStar::findPath(std::vector<PathFindPos>& o_path, const PathFindPos& start, const PathFindPos& end, const PathFindPos& corner0, const PathFindPos& corner1)
{
Node startnode(start.x, start.y);
Node endnode(end.x, end.y);
struct NestedFunctions //functions nested inside this function for convenience
{
static int heuristic(const Node& current, const Node& end)
{
int hx = (end.x - current.x);
int hy = (end.y - current.y);
if(hx < 0) hx = -hx;
if(hy < 0) hy = -hy;
return 10 * (hx + hy);
}
};
std::list<Node> open; //open list: squares to look at next. Using an std::list allows to insert new squares sorted by cost in it
std::list<Node> closed; //closed list: squares we don't have to look at anymore for now
size_t w = corner1.x - corner0.x, h = corner1.y - corner0.y;
std::vector<bool> closed_table(w * h, false);
startnode.g = 0;
startnode.h = NestedFunctions::heuristic(startnode, endnode);
startnode.f = startnode.g + startnode.h;
startnode.parent = 0;
open.push_back(startnode);
bool path_exists;
for(;;)
{
if(open.size() == 0)
{
//the open list is empty... no path is found
path_exists = false;
break;
}
Node current = open.front(); open.pop_front(); //take the one with lowest cost from the open list
closed.push_back(current);
closed_table[w * current.y + current.x] = true;
if(current == endnode)
{
//the end is reached
path_exists = true;
break;
}
//find the adjendant squares
for(int i = 0; i < 8; i++) //for each neighbor
{
Node neighbor(current);
switch(i)
{
case 0: neighbor.y -= 1; break; // +-x->
case 1: neighbor.x += 1; break; // y 704
case 2: neighbor.y += 1; break; // | 3 1
case 3: neighbor.x -= 1; break; // v 625
case 4:
neighbor.x += 1;
neighbor.y -= 1;
break;
case 5:
neighbor.x += 1;
neighbor.y += 1;
break;
case 6:
neighbor.x -= 1;
neighbor.y += 1;
break;
case 7:
neighbor.x -= 1;
neighbor.y -= 1;
break;
}
int extra_cost;
if(i < 4) extra_cost = 10;
else extra_cost = 14; //10 * sqrt(2)
///is the neigbhbor walkable?
bool walkable = true;
if(neighbor.x < corner0.x || neighbor.x >= corner1.x || neighbor.y < corner0.y || neighbor.y >= corner1.y) walkable = false;
if(blocks(neighbor.x, neighbor.y)) walkable = false;
//diagonal also blocks if two walls are in the way
if(i >= 4)
{
if(blocks(neighbor.x, current.y)) walkable = false;
if(blocks(current.x, neighbor.y)) walkable = false;
}
if(!walkable) continue;
///if the neighbor is already on the closed list, skip it
bool already_on_closed_list = false;
if(closed_table[w * neighbor.y + neighbor.x] == true) already_on_closed_list = true;
/***for(std::list<Node>::iterator i = closed.begin(); i != closed.end(); i++)
{
if(neighbor == *i)
{
already_on_closed_list = true;
break;
}
}***/
if(already_on_closed_list) continue;
neighbor.parent = &closed.back();
neighbor.g = current.g + extra_cost;
neighbor.h = NestedFunctions::heuristic(neighbor, endnode);
neighbor.f = neighbor.g + neighbor.h; //the cost
///find if the neighbor is already on the open list
bool better_one_already_on_open_list = false;
for(std::list<Node>::iterator i = open.begin(); i != open.end(); )
{
if(neighbor == *i)
{
if(neighbor.f < (*i).f)
{
i = open.erase(i);
}
else
{
better_one_already_on_open_list = true;
++i;
}
}
else ++i;
}
if(better_one_already_on_open_list) continue;
///place the neighbor node in the open list, correctly sorted by cost
bool inserted = false;
for(std::list<Node>::iterator i = open.begin(); i != open.end(); i++)
{
if(neighbor.f <= (*i).f)
{
open.insert(i, neighbor);
inserted = true;
break;
}
}
if(!inserted) open.push_back(neighbor); //either open was empty, or neighbor had higher cost than everything in it, so insert it at the back now
}
}
//The while loop is now over. Either a path is found, or there exists none. Now store the result in the return path.
Node node = closed.back();
o_path.push_back(PathFindPos(node.x, node.y));
while(node.parent != 0)
{
node = *(node.parent);
o_path.insert(o_path.begin(), PathFindPos(node.x, node.y));
}
return path_exists;
}
} //namespace lpi
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
///Test and example of pathfind
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
#if 0 //LPI_PATHFIND_TEST
using namespace lpi;
namespace pathfindtest
{
template<int SIZEX, int SIZEY>
class TestWorld : public World_AStar
{
public:
bool map[SIZEX][SIZEY];
virtual bool blocks(int x, int y) const
{
return map[x][y];
}
};
void test_pathfind()
{
const unsigned SIZE = 16;
TestWorld<SIZE, SIZE> w;
char display[SIZE][SIZE];
for(size_t x = 0; x < SIZE; x++)
for(size_t y = 0; y < SIZE; y++)
{
w.map[x][y] = !( ((x & y) == 0) || ((x - y) % 5 < 2) );
}
for(size_t x = 0; x < SIZE; x++)
{
for(size_t y = 0; y < SIZE; y++)
{
if(w.map[x][y]) display[x][y] = '#';
else display[x][y] = '.';
}
}
std::vector<PathFindPos> path;
bool path_exists = w.findPath(path, PathFindPos(0, 0), PathFindPos(SIZE - 1, SIZE - 1), PathFindPos(0, 0), PathFindPos(SIZE, SIZE));
for(size_t i = 0; i < path.size(); i++)
{
display[path[i].x][path[i].y] = 'a' + i;
}
std::cout<<"\n";
for(size_t x = 0; x < SIZE; x++)
{
for(size_t y = 0; y < SIZE; y++)
{
std::cout << display[x][y];
}
std::cout << "\n";
}
if(path_exists) std::cout << "\npath exists\n"; else std::cout << "\npath doesn't exist\n";
std::cout << "path size: " << path.size() << "\n";
}
struct TEST
{
TEST()
{
test_pathfind();
}
} test;
}
#endif