This repository has been archived by the owner on Nov 28, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathPath.cpp
213 lines (178 loc) · 5.57 KB
/
Path.cpp
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
#include "Global.h"
#include "Util.h"
#include "Creature.h"
/* implementation of A* Algorithm */
int
shCreature::estimateDistance (int ox, int oy,
int dx, int dy)
{
return FULLTURN * distance (ox, oy, dx, dy) / 5;
}
//REQUIRES: (ox, oy) adjacent to (dx, dy)
//RETURNS: cost in ms to move between the squares, 9999999 if impossible
int
shCreature::moveCost (int ox, int oy, int dx, int dy)
{
int cost = 0;
shFeature *f;
f = mLevel->getFeature (dx, dy);
if (f && shFeature::kDoorClosed == f->mType) {
if (f->isLockedDoor ()) {
return 9999999;
}
if (!(f->mDoor & shFeature::kAutomatic) &&
0 == numHands ())
{
return 9999999;
} else {
cost += FULLTURN;
}
} else if (mLevel->isObstacle (dx, dy)) {
return 9999999;
}
if (ox == dx || oy == dy) {
cost += FULLTURN;
} else {
cost += DIAGTURN;
}
return cost;
}
struct PathNode
{
struct PathNode *prev;
int g;
int h;
int f;
char isopen;
char isclosed;
};
inline static int
pathNodeCompareFunc (PathNode *a, PathNode *b)
{
if (a->f < b->f) {
return -1;
} else if (a->f == b->f) {
return 0;
} else {
return 1;
}
}
static PathNode data[MAPMAXCOLUMNS*MAPMAXROWS];
int
shCreature::shortestPath (int ox, int oy,
int dx, int dy,
shDirection *resultbuf,
int buflen)
{
int x, y;
int nx, ny;
int i;
// char dirty[MAPMAXCOLUMNS*MAPMAXROWS];
shHeap <PathNode *> Open (pathNodeCompareFunc);
shVector <PathNode *> Closed;
int c1 = 0;
int c2 = 0;
// memset (&data, 0, sizeof (data));
#define getnode(_x, _y) (&data[_y * MAPMAXCOLUMNS + _x])
#define getnodex(_n) ((((char *) _n - (char *) data) / sizeof (PathNode)) % MAPMAXCOLUMNS)
#define getnodey(_n) ((((char *) _n - (char *) data) / sizeof (PathNode)) / MAPMAXCOLUMNS)
PathNode *node;
PathNode *newnode;
node = getnode (ox, oy);
node->g = 0;
node->h = estimateDistance (ox, oy, dx, dy);
node->f = node->g + node->h;
node->prev = NULL;
Open.add (node);
node->isopen = 1;
while (Open.count ()) {
node = Open.extract ();
node->isopen = 0;
++c1;
ny = getnodey (node);
nx = getnodex (node);
// I->debug ("popping open node %d %d, f=%d", nx, ny, node->f);
if (nx == dx && ny == dy) {
/* we found it! */
int i, j, n;
n = 0;
while ((node = node->prev) && n < buflen) {
x = getnodex (node);
y = getnodey (node);
resultbuf[n++] = vectorDirection (x, y, nx, ny);
nx = x;
ny = y;
}
/* reverse */
for (j = 0, i = n - 1; j < i; ++j, --i) {
shDirection tmp = resultbuf[i];
resultbuf[i] = resultbuf[j];
resultbuf[j] = tmp;
}
{
char pathbuf[100];
int l = snprintf (pathbuf, 80, "result: ");
for (i = 0; i < n && i < 15; i++) {
l += snprintf (&pathbuf[l], 99 - l, "%s ",
stringDirection (resultbuf[i]));
}
I->debug (" path %d,%d -> %d,%d; c1=%d c2=%d; %s",
ox, oy, dx, dy, c1, c2, pathbuf);
}
for (i = Open.count () - 1; i >= 0; i--) {
Open.get (i) -> isopen = 0;
}
for (i = Closed.count () - 1; i >= 0; i--) {
Closed.get (i) -> isclosed = 0;
}
return n;
}
for (x = nx - 1; x <= nx + 1; x++) {
for (y = ny - 1; y <= ny + 1; y++) {
int cost;
if ((x == nx && y == ny) || !mLevel->isInBounds (x, y)) {
continue;
}
++c2;
cost = node->g + moveCost (nx, ny, x, y);
if (cost > 200000) {
//I->debug ("too expensive to reach %d %d (%d)", x, y, cost);
continue;
}
newnode = getnode (x, y);
// if ((-1 != Open.find (newnode) || -1 != Closed.find (newnode))
if ((newnode->isopen || newnode->isclosed)
&& newnode->g <= cost)
{
//I->debug ("already a cheaper way to reach %d %d", x, y);
continue;
}
newnode->g = cost;
newnode->h = estimateDistance (x, y, dx, dy);
newnode->f = newnode->g + newnode->h;
newnode->prev = node;
//I->debug ("planning node %d %d, f=%d", x, y, newnode->f);
if (1 == newnode->isclosed) {
Closed.remove (newnode);
newnode->isclosed = 0;
}
if (0 == newnode->isopen) {
Open.insert (newnode);
newnode->isopen = 1;
}
}
}
Closed.add (node);
node->isclosed = 1;
}
/* no path found! */
I->debug (" path %d,%d -> %d,%d; c1=%d c2=%d; none found",
ox, oy, dx, dy, c1, c2);
for (i = Open.count () - 1; i >= 0; i--) {
Open.get (i) -> isopen = 0;
}
for (i = Closed.count () - 1; i >= 0; i--) {
Closed.get (i) -> isclosed = 0;
}
return 0;
}