-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathPathfinding.c
More file actions
344 lines (315 loc) · 11.3 KB
/
Copy pathPathfinding.c
File metadata and controls
344 lines (315 loc) · 11.3 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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define DIAGONAL 14
#define ORTOGONAL 10
#define NONE 0
#define OPEN 1
#define CLOSED 2
#define PATH 3
#define LCHILD(currentIndex) 2 * currentIndex + 1
#define RCHILD(currentIndex) 2 * currentIndex + 2
#define PARENT(currentIndex) (int)floor((currentIndex - 1) / 2)
typedef struct Nodes{
unsigned short x;
unsigned short y;
unsigned short parentX;
unsigned short parentY;
unsigned int whichlist : 2;
unsigned int openListIndex;
unsigned int g;
unsigned short h;
unsigned int f;
} Node;
typedef struct Blocks{
unsigned int walkable : 1;
unsigned int water : 1;
unsigned int snipleable : 1;
unsigned int cliff : 1;
Node nodeInfo;
} Block;
typedef struct Maps{
unsigned int height;
unsigned int width;
Block **grid;
} Map;
typedef struct {
int x;
int y;
int distanceFromCurrent;
} eachNeigh;
typedef struct {
eachNeigh neighborNodes[8];
int count;
} Neighbors;
typedef struct {
int x;
int y;
int f;
} TypeList;
void freeMap(Map* currentMap){
int i;
for(i = 0; i < currentMap->height; i++);{
free(currentMap->grid[i]);
}
free(currentMap->grid);
free(currentMap);
}
Map* mallocMap(int width, int height){
Map* currentMap = (Map*) malloc(sizeof(Map));
currentMap->width = width;
currentMap->height = height;
currentMap->grid = (Block**) malloc(currentMap->width * sizeof(Block*));
int j;
for(j = 0; j < currentMap->width; j++){
currentMap->grid[j] = (Block*) malloc(currentMap->height * sizeof(Block));
}
return currentMap;
}
Map* GenerateMap(char *field){
FILE *fp = fopen(field, "rb");
int width = fgetc(fp) + fgetc(fp) * 256;
int height = fgetc(fp) + fgetc(fp) * 256;
Map* currentMap = mallocMap(width, height);
int x = 0;
int y = 0;
int i;
while ((i = fgetc(fp)) != EOF) {
currentMap->grid[x][y].walkable = (i & 1) ? 1 : 0;
currentMap->grid[x][y].snipleable = (i & 2) ? 1 : 0;
currentMap->grid[x][y].water = (i & 4) ? 1 : 0;
currentMap->grid[x][y].cliff = (i & 8) ? 1 : 0;
currentMap->grid[x][y].nodeInfo.whichlist = NONE;
currentMap->grid[x][y].nodeInfo.openListIndex = NONE;
if (x == currentMap->width - 1) {
y++;
x = 0;
}
else {
x++;
}
}
fclose(fp);
return currentMap;
}
int heuristic_cost_estimate(Node* currentNode, Node* goalNode)
{
int xDistance = abs(currentNode->x - goalNode->x);
int yDistance = abs(currentNode->y - goalNode->y);
int hScore;
if (xDistance > yDistance) {
hScore = DIAGONAL * yDistance + ORTOGONAL * (xDistance - yDistance);
}
else {
hScore = DIAGONAL * xDistance + ORTOGONAL * (yDistance - xDistance);
}
return hScore;
}
void organizeNeighborsStruct(Neighbors* currentNeighbors, Node* currentNode, Map* currentMap)
{
int count = 0;
int i;
for (i = -1; i <= 1; i++)
{
int j;
for (j = -1; j <= 1; j++)
{
if (i == 0 && j == 0){ continue; }
int x = currentNode->x + i;
int y = currentNode->y + j;
if (x > currentMap->width - 1 || y > currentMap->height - 1){ continue; }
if (x < 0 || y < 0){ continue; }
if (currentMap->grid[x][y].walkable == 0){ continue; }
if (i != 0 && j != 0) {
if (currentMap->grid[x][currentNode->y].walkable == 0 || currentMap->grid[currentNode->x][y].walkable == 0){ continue; }
currentNeighbors->neighborNodes[count].distanceFromCurrent = DIAGONAL;
} else {
currentNeighbors->neighborNodes[count].distanceFromCurrent = ORTOGONAL;
}
currentNeighbors->neighborNodes[count].x = x;
currentNeighbors->neighborNodes[count].y = y;
count++;
}
}
currentNeighbors->count = count;
}
//Openlist is a binary heap of min-heap type
void openListAdd (TypeList* openList, Node* infoAdress, int openListSize, Map* currentMap)
{
openList[openListSize].x = infoAdress->x;
openList[openListSize].y = infoAdress->y;
openList[openListSize].f = infoAdress->f;
currentMap->grid[openList[openListSize].x][openList[openListSize].y].nodeInfo.openListIndex = openListSize;
int currentIndex = openListSize;
TypeList Temporary;
while (PARENT(currentIndex) >= 0) {
if (openList[PARENT(currentIndex)].f > openList[currentIndex].f) {
Temporary = openList[currentIndex];
openList[currentIndex] = openList[PARENT(currentIndex)];
currentMap->grid[openList[currentIndex].x][openList[currentIndex].y].nodeInfo.openListIndex = currentIndex;
openList[PARENT(currentIndex)] = Temporary;
currentMap->grid[openList[PARENT(currentIndex)].x][openList[PARENT(currentIndex)].y].nodeInfo.openListIndex = PARENT(currentIndex);
currentIndex = PARENT(currentIndex);
} else { break; }
}
}
void reajustOpenListItem (TypeList* openList, Node* infoAdress, int openListSize, Map* currentMap)
{
int currentIndex = infoAdress->openListIndex;
openList[currentIndex].f = infoAdress->f;
TypeList Temporary;
while (PARENT(currentIndex) >= 0) {
if (openList[PARENT(currentIndex)].f > openList[currentIndex].f) {
Temporary = openList[currentIndex];
openList[currentIndex] = openList[PARENT(currentIndex)];
currentMap->grid[openList[currentIndex].x][openList[currentIndex].y].nodeInfo.openListIndex = currentIndex;
openList[PARENT(currentIndex)] = Temporary;
currentMap->grid[openList[PARENT(currentIndex)].x][openList[PARENT(currentIndex)].y].nodeInfo.openListIndex = PARENT(currentIndex);
currentIndex = PARENT(currentIndex);
} else { break; }
}
}
Node* openListGetLowest (TypeList* openList, Map* currentMap, int openListSize)
{
Node* lowestNode = ¤tMap->grid[openList[0].x][openList[0].y].nodeInfo;
openList[0] = openList[openListSize-1];
currentMap->grid[openList[0].x][openList[0].y].nodeInfo.openListIndex = 0;
int lowestChildIndex = 0;
int currentIndex = 0;
TypeList Temporary;
while (LCHILD(currentIndex) < openListSize - 2) {
//There are 2 children
if (RCHILD(currentIndex) <= openListSize - 2) {
if (openList[RCHILD(currentIndex)].f <= openList[LCHILD(currentIndex)].f) {
lowestChildIndex = RCHILD(currentIndex);
} else {
lowestChildIndex = LCHILD(currentIndex);
}
} else {
//There is 1 children
if (LCHILD(currentIndex) <= openListSize - 2) {
lowestChildIndex = LCHILD(currentIndex);
} else {
break;
}
}
if (openList[currentIndex].f > openList[lowestChildIndex].f) {
Temporary = openList[currentIndex];
openList[currentIndex] = openList[lowestChildIndex];
currentMap->grid[openList[currentIndex].x][openList[currentIndex].y].nodeInfo.openListIndex = currentIndex;
openList[lowestChildIndex] = Temporary;
currentMap->grid[openList[lowestChildIndex].x][openList[lowestChildIndex].y].nodeInfo.openListIndex = lowestChildIndex;
currentIndex = lowestChildIndex;
} else { break; }
}
return lowestNode;
}
void reconstruct_path(Map* currentMap, Node* startNode, Node* currentNode)
{
int i = 0;
while (currentNode->x != startNode->x || currentNode->y != startNode->y)
{
currentMap->grid[currentNode->parentX][currentNode->parentY].nodeInfo.whichlist = PATH;
currentNode = ¤tMap->grid[currentNode->parentX][currentNode->parentY].nodeInfo;
i++;
}
}
void Pathfind (Node* startNode, Node* goalNode, Map* currentMap)
{
int size = currentMap->height * currentMap->width;
int openListSize = 1;
int Gscore = 0;
int indexNeighbor = 0;
int nodeList;
TypeList* openList = (TypeList*) malloc(size * sizeof(TypeList));
Node* currentNode;
openList[0].x = startNode->x;
openList[0].y = startNode->y;
currentMap->grid[openList[0].x][openList[0].y].nodeInfo.x = startNode->x;
currentMap->grid[openList[0].x][openList[0].y].nodeInfo.y = startNode->y;
Neighbors* currentNeighbors = (Neighbors*) malloc(sizeof(Neighbors));
Node* infoAdress;
while (openListSize > 0) {
//get lowest F score member of openlist and delete it from it
currentNode = openListGetLowest (openList, currentMap, openListSize);
openListSize--;
//add currentNode to closedList
currentNode->whichlist = CLOSED;
//if current is the goal, return the path.
if (currentNode->x == goalNode->x && currentNode->y == goalNode->y) {
//return path
reconstruct_path(currentMap, startNode, currentNode);
break;
}
//add current to closedset.
organizeNeighborsStruct(currentNeighbors, currentNode, currentMap);
for (indexNeighbor = 0; indexNeighbor < currentNeighbors->count; indexNeighbor++) {
infoAdress = ¤tMap->grid[currentNeighbors->neighborNodes[indexNeighbor].x][currentNeighbors->neighborNodes[indexNeighbor].y].nodeInfo;
nodeList = infoAdress->whichlist;
if (nodeList == CLOSED) { continue; }
Gscore = currentNode->g + currentNeighbors->neighborNodes[indexNeighbor].distanceFromCurrent;
if (nodeList != OPEN) {
infoAdress->x = currentNeighbors->neighborNodes[indexNeighbor].x;
infoAdress->y = currentNeighbors->neighborNodes[indexNeighbor].y;
infoAdress->parentX = currentNode->x;
infoAdress->parentY = currentNode->y;
infoAdress->whichlist = OPEN;
infoAdress->g = Gscore;
infoAdress->h = heuristic_cost_estimate(infoAdress, goalNode);
infoAdress->f = Gscore + infoAdress->h;
openListAdd (openList, infoAdress, openListSize, currentMap);
openListSize++;
} else {
if (Gscore < infoAdress->g) {
infoAdress->parentX = currentNode->x;
infoAdress->parentY = currentNode->y;
infoAdress->g = Gscore;
infoAdress->f = Gscore + infoAdress->h;
reajustOpenListItem (openList, infoAdress, openListSize, currentMap);
}
}
}
}
free(openList);
}
void PrintMap(Map* currentMap){
FILE *block = fopen("printedMap.txt", "w");
int tx = 0;
int ty = currentMap->height - 1;
int size = currentMap->width*currentMap->height;
char walkable = ' ';
char unwalkable = '#';
char openList = 'c';
char closedList = '.';
char path = '0';
int j;
for (j = 0; j < size; j++)
{
fprintf(block, "%c", currentMap->grid[tx][ty].walkable ? currentMap->grid[tx][ty].nodeInfo.whichlist == 3? path : currentMap->grid[tx][ty].nodeInfo.whichlist == 2? closedList : currentMap->grid[tx][ty].nodeInfo.whichlist == 1? openList : walkable : unwalkable);
if (tx == currentMap->width - 1) {
ty--;
tx = 0;
fprintf(block, "\n");
}
else {
tx++;
}
}
fclose(block);
}
int main(){
Map* currentMap = GenerateMap("maps\\hugel.fld2");
int startX = 83;
int startY = 57;
int endX = 211;
int endY = 234;
currentMap->grid[startX][startY].nodeInfo.x = startX;
currentMap->grid[startX][startY].nodeInfo.y = startY;
currentMap->grid[startX][startY].nodeInfo.g = 0;
currentMap->grid[endX][endY].nodeInfo.x = endX;
currentMap->grid[endX][endY].nodeInfo.y = endY;
Pathfind(¤tMap->grid[startX][startY].nodeInfo, ¤tMap->grid[endX][endY].nodeInfo, currentMap);
PrintMap(currentMap);
freeMap(currentMap);
return 0;
}