-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpathfinder.cpp
150 lines (140 loc) · 6.06 KB
/
pathfinder.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
#include <cstdlib>
#include <cstring>
#include <climits>
#include <string>
#include "dungeon.h"
#include "heap.h"
/*****************************************
* Corridor Path Compare *
*****************************************/
static int32_t corridor_path_cmp(const void *key, const void *with)
{
return ((corPath *) key)->cost - ((corPath *) with)->cost;
}
/*****************************************
* Dungeon Cost Finder *
*****************************************/
void dijkstra(dungeon *d, int tunOrNot)
{
static corPath *p;
static uint32_t initialized = 0;
heap_t h;
uint32_t x, y;
if (!initialized) {
for (y = 0; y < floorMaxY; y++) {
for (x = 0; x < floorMaxX; x++) {
d->path[y][x].pos[dimY] = y;
d->path[y][x].pos[dimX] = x;
}
}
initialized = 1;
}
for (y = 0; y < floorMaxY; y++) {
for (x = 0; x < floorMaxX; x++) {
d->path[y][x].cost = INT_MAX;
}
}
for (y = 0; y < floorMaxY; y++) {
for (x = 0; x < floorMaxX; x++) {
if(d->floor[y][x] == playerChar){
d->path[y][x].cost = 0;
}
}
}
heap_init(&h, corridor_path_cmp, NULL);
if(tunOrNot == 0){ // Non-tunneling
for (y = 0; y < floorMaxY; y++) {
for (x = 0; x < floorMaxX; x++) {
if (d->floor[y][x] != edgeChar && d->floor[y][x] != rockChar) {
d->path[y][x].hn = heap_insert(&h, &d->path[y][x]);
} else {
d->path[y][x].hn = NULL;
}
}
}
} else if(tunOrNot == 1){ // Tunneling
for (y = 0; y < floorMaxY; y++) {
for (x = 0; x < floorMaxX; x++) {
if (d->floor[y][x] != edgeChar) {
d->path[y][x].hn = heap_insert(&h, &d->path[y][x]);
} else {
d->path[y][x].hn = NULL;
}
}
}
} else {
exit(1);
}
while ((p = (corPath*) heap_remove_min(&h))) {
p->hn = NULL;
// 4 cases for cardinal directions
if ((d->path[p->pos[dimY] - 1][p->pos[dimX] ].hn) &&
(d->path[p->pos[dimY] - 1][p->pos[dimX] ].cost >
p->cost + d->hardness[p->pos[dimY]][p->pos[dimX]] / 85)) {
d->path[p->pos[dimY] - 1][p->pos[dimX] ].cost = p->cost + d->hardness[p->pos[0]][p->pos[1]] / 85 + 1;
heap_decrease_key_no_replace(&h, d->path[p->pos[dimY] - 1][p->pos[dimX] ].hn);
}
if ((d->path[p->pos[dimY] ][p->pos[dimX] - 1].hn) &&
(d->path[p->pos[dimY] ][p->pos[dimX] - 1].cost >
p->cost + d->hardness[p->pos[dimY]][p->pos[dimX]] / 85)) {
d->path[p->pos[dimY] ][p->pos[dimX] - 1].cost = p->cost + d->hardness[p->pos[0]][p->pos[1]] / 85 + 1;
heap_decrease_key_no_replace(&h, d->path[p->pos[dimY] ][p->pos[dimX] - 1].hn);
}
if ((d->path[p->pos[dimY] ][p->pos[dimX] + 1].hn) &&
(d->path[p->pos[dimY] ][p->pos[dimX] + 1].cost >
p->cost + d->hardness[p->pos[dimY]][p->pos[dimX]] / 85)) {
d->path[p->pos[dimY] ][p->pos[dimX] + 1].cost = p->cost + d->hardness[p->pos[0]][p->pos[1]] / 85 + 1;
heap_decrease_key_no_replace(&h, d->path[p->pos[dimY] ][p->pos[dimX] + 1].hn);
}
if ((d->path[p->pos[dimY] + 1][p->pos[dimX] ].hn) &&
(d->path[p->pos[dimY] + 1][p->pos[dimX] ].cost >
p->cost + d->hardness[p->pos[dimY]][p->pos[dimX]] / 85)) {
d->path[p->pos[dimY] + 1][p->pos[dimX] ].cost = p->cost + d->hardness[p->pos[0]][p->pos[1]] / 85 + 1;
heap_decrease_key_no_replace(&h, d->path[p->pos[dimY] + 1][p->pos[dimX] ].hn);
}
// 4 more cases for diagonal directions.
if ((d->path[p->pos[dimY] - 1][p->pos[dimX] - 1].hn) &&
(d->path[p->pos[dimY] - 1][p->pos[dimX] - 1].cost >
p->cost + d->hardness[p->pos[dimY]][p->pos[dimX]] / 85)) {
d->path[p->pos[dimY] - 1][p->pos[dimX] - 1].cost = p->cost + d->hardness[p->pos[0]][p->pos[1]] / 85 + 1;
heap_decrease_key_no_replace(&h, d->path[p->pos[dimY] - 1][p->pos[dimX] - 1].hn);
}
if ((d->path[p->pos[dimY] - 1][p->pos[dimX] + 1].hn) &&
(d->path[p->pos[dimY] - 1][p->pos[dimX] + 1].cost >
p->cost + d->hardness[p->pos[dimY]][p->pos[dimX]] / 85)) {
d->path[p->pos[dimY] - 1][p->pos[dimX] + 1].cost = p->cost + d->hardness[p->pos[0]][p->pos[1]] / 85 + 1;
heap_decrease_key_no_replace(&h, d->path[p->pos[dimY] - 1][p->pos[dimX] + 1].hn);
}
if ((d->path[p->pos[dimY] + 1][p->pos[dimX] - 1].hn) &&
(d->path[p->pos[dimY] + 1][p->pos[dimX] - 1].cost >
p->cost + d->hardness[p->pos[dimY]][p->pos[dimX]] / 85)) {
d->path[p->pos[dimY] + 1][p->pos[dimX] - 1].cost = p->cost + d->hardness[p->pos[0]][p->pos[1]] / 85 + 1;
heap_decrease_key_no_replace(&h, d->path[p->pos[dimY] + 1][p->pos[dimX] - 1].hn);
}
if ((d->path[p->pos[dimY] + 1][p->pos[dimX] + 1].hn) &&
(d->path[p->pos[dimY] + 1][p->pos[dimX] + 1].cost >
p->cost + d->hardness[p->pos[dimY]][p->pos[dimX]] / 85)) {
d->path[p->pos[dimY] + 1][p->pos[dimX] + 1].cost = p->cost + d->hardness[p->pos[0]][p->pos[1]] / 85 + 1;
heap_decrease_key_no_replace(&h, d->path[p->pos[dimY] + 1][p->pos[dimX] + 1].hn);
}
}
if(tunOrNot == 0){
for (y = 0; y < floorMaxY; y++) {
for (x = 0; x < floorMaxX; x++) {
if(d->hardness[y][x] == 0) // Room or corridor
d->nonTunDist[y][x] = d->path[y][x].cost;
else
d->nonTunDist[y][x] = 255;
}
}
} else {
for (y = 0; y < floorMaxY; y++) {
for (x = 0; x < floorMaxX; x++) {
d->tunDist[y][x] = d->path[y][x].cost;
}
}
}
free(p);
heap_delete(&h);
//printMap(d);
}