This repository has been archived by the owner on Apr 26, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDijkstra.c
301 lines (243 loc) · 7.93 KB
/
Dijkstra.c
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
/*
* Dijkstra.c
* Brogue
*
* Copyright 2012. All rights reserved.
*
* This file is part of Brogue.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
// This code was created by Joshua Day, to replace my clunkier and slower Dijkstra scanning algorithm in Movement.c.
// Thanks very much, Joshua.
#include "Rogue.h"
#include "IncludeGlobals.h"
struct pdsLink {
short distance;
short cost;
pdsLink *left, *right;
};
struct pdsMap {
boolean eightWays;
pdsLink front;
pdsLink links[DCOLS * DROWS];
};
void pdsUpdate(pdsMap *map) {
short dir, dirs;
pdsLink *left = NULL, *right = NULL, *link = NULL;
dirs = map->eightWays ? 8 : 4;
pdsLink *head = map->front.right;
map->front.right = NULL;
while (head != NULL) {
for (dir = 0; dir < dirs; dir++) {
link = head + (nbDirs[dir][0] + DCOLS * nbDirs[dir][1]);
if (link < map->links || link >= map->links + DCOLS * DROWS) continue;
// verify passability
if (link->cost < 0) continue;
if (dir >= 4) {
pdsLink *way1, *way2;
way1 = head + nbDirs[dir][0];
way2 = head + DCOLS * nbDirs[dir][1];
if (way1->cost == PDS_OBSTRUCTION || way2->cost == PDS_OBSTRUCTION) continue;
}
if (head->distance + link->cost < link->distance) {
link->distance = head->distance + link->cost;
// reinsert the touched cell; it'll be close to the beginning of the list now, so
// this will be very fast. start by removing it.
if (link->right != NULL) link->right->left = link->left;
if (link->left != NULL) link->left->right = link->right;
left = head;
right = head->right;
while (right != NULL && right->distance < link->distance) {
left = right;
right = right->right;
}
if (left != NULL) left->right = link;
link->right = right;
link->left = left;
if (right != NULL) right->left = link;
}
}
right = head->right;
head->left = NULL;
head->right = NULL;
head = right;
}
}
void pdsClear(pdsMap *map, short maxDistance, boolean eightWays) {
short i;
map->eightWays = eightWays;
map->front.right = NULL;
for (i=0; i < DCOLS*DROWS; i++) {
map->links[i].distance = maxDistance;
map->links[i].left = map->links[i].right = NULL;
}
}
short pdsGetDistance(pdsMap *map, short x, short y) {
pdsUpdate(map);
return PDS_CELL(map, x, y)->distance;
}
void pdsSetDistance(pdsMap *map, short x, short y, short distance) {
pdsLink *left, *right, *link;
if (x > 0 && y > 0 && x < DCOLS - 1 && y < DROWS - 1) {
link = PDS_CELL(map, x, y);
if (link->distance > distance) {
link->distance = distance;
if (link->right != NULL) link->right->left = link->left;
if (link->left != NULL) link->left->right = link->right;
left = &map->front;
right = map->front.right;
while (right != NULL && right->distance < link->distance) {
left = right;
right = right->right;
}
link->right = right;
link->left = left;
left->right = link;
if (right != NULL) right->left = link;
}
}
}
void pdsSetCosts(pdsMap *map, short **costMap) {
short i, j;
for (i=0; i<DCOLS; i++) {
for (j=0; j<DROWS; j++) {
if (i != 0 && j != 0 && i < DCOLS - 1 && j < DROWS - 1) {
PDS_CELL(map, i, j)->cost = costMap[i][j];
} else {
PDS_CELL(map, i, j)->cost = PDS_FORBIDDEN;
}
}
}
}
void pdsBatchInput(pdsMap *map, short **distanceMap, short **costMap, short maxDistance, boolean eightWays) {
short i, j;
pdsLink *left, *right;
map->eightWays = eightWays;
left = NULL;
right = NULL;
map->front.right = NULL;
for (i=0; i<DCOLS; i++) {
for (j=0; j<DROWS; j++) {
pdsLink *link = PDS_CELL(map, i, j);
if (distanceMap != NULL) {
link->distance = distanceMap[i][j];
} else {
if (costMap != NULL) {
// totally hackish; refactor
link->distance = maxDistance;
}
}
int cost;
if (i == 0 || j == 0 || i == DCOLS - 1 || j == DROWS - 1) {
cost = PDS_OBSTRUCTION;
} else if (costMap == NULL) {
if (cellHasTerrainFlag(i, j, T_OBSTRUCTS_PASSABILITY) && cellHasTerrainFlag(i, j, T_OBSTRUCTS_DIAGONAL_MOVEMENT)) cost = PDS_OBSTRUCTION;
else cost = PDS_FORBIDDEN;
} else {
cost = costMap[i][j];
}
link->cost = cost;
if (cost > 0) {
if (link->distance < maxDistance) {
if (right == NULL || right->distance > link->distance) {
// left and right are used to traverse the list; if many cells have similar values,
// some time can be saved by not clearing them with each insertion. this time,
// sadly, we have to start from the front.
left = &map->front;
right = map->front.right;
}
while (right != NULL && right->distance < link->distance) {
left = right;
right = right->right;
}
link->right = right;
link->left = left;
left->right = link;
if (right != NULL) right->left = link;
left = link;
} else {
link->right = NULL;
link->left = NULL;
}
} else {
link->right = NULL;
link->left = NULL;
}
}
}
}
void pdsBatchOutput(pdsMap *map, short **distanceMap) {
short i, j;
pdsUpdate(map);
// transfer results to the distanceMap
for (i=0; i<DCOLS; i++) {
for (j=0; j<DROWS; j++) {
distanceMap[i][j] = PDS_CELL(map, i, j)->distance;
}
}
}
void pdsInvalidate(pdsMap *map, short maxDistance) {
pdsBatchInput(map, NULL, NULL, maxDistance, map->eightWays);
}
void dijkstraScan(short **distanceMap, short **costMap, boolean useDiagonals) {
static pdsMap map;
pdsBatchInput(&map, distanceMap, costMap, 30000, useDiagonals);
pdsBatchOutput(&map, distanceMap);
}
void calculateDistances(short **distanceMap,
short destinationX, short destinationY,
unsigned long blockingTerrainFlags,
creature *traveler,
boolean canUseSecretDoors,
boolean eightWays) {
creature *monst;
static pdsMap map;
short i, j;
for (i=0; i<DCOLS; i++) {
for (j=0; j<DROWS; j++) {
char cost;
monst = monsterAtLoc(i, j);
if (monst
&& (monst->info.flags & (MONST_IMMUNE_TO_WEAPONS | MONST_INVULNERABLE))
&& (monst->info.flags & (MONST_IMMOBILE | MONST_GETS_TURN_ON_ACTIVATION))) {
// Always avoid damage-immune stationary monsters.
cost = PDS_FORBIDDEN;
} else if (canUseSecretDoors
&& cellHasTMFlag(i, j, TM_IS_SECRET)
&& cellHasTerrainFlag(i, j, T_OBSTRUCTS_PASSABILITY)
&& !(discoveredTerrainFlagsAtLoc(i, j) & T_OBSTRUCTS_PASSABILITY)) {
cost = 1;
} else if (cellHasTerrainFlag(i, j, T_OBSTRUCTS_PASSABILITY)
|| (traveler && traveler == &player && !(pmap[i][j].flags & (DISCOVERED | MAGIC_MAPPED)))) {
cost = cellHasTerrainFlag(i, j, T_OBSTRUCTS_DIAGONAL_MOVEMENT) ? PDS_OBSTRUCTION : PDS_FORBIDDEN;
} else if ((traveler && monsterAvoids(traveler, i, j)) || cellHasTerrainFlag(i, j, blockingTerrainFlags)) {
cost = PDS_FORBIDDEN;
} else {
cost = 1;
}
PDS_CELL(&map, i, j)->cost = cost;
}
}
pdsClear(&map, 30000, eightWays);
pdsSetDistance(&map, destinationX, destinationY, 0);
pdsBatchOutput(&map, distanceMap);
}
short pathingDistance(short x1, short y1, short x2, short y2, unsigned long blockingTerrainFlags) {
short retval, **distanceMap = allocGrid();
calculateDistances(distanceMap, x2, y2, blockingTerrainFlags, NULL, true, true);
retval = distanceMap[x1][y1];
freeGrid(distanceMap);
return retval;
}