forked from Orpheon/Navmesh-Pathfinder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
find_path.c
131 lines (119 loc) · 3.7 KB
/
find_path.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
#include "find_path.h"
#include "data_types.h"
#include "navmesh.h"
#include <math.h>
#include <limits.h>
#ifdef DEBUG_MODE
#include <stdio.h>
#endif
#define INACTIVE 0
#define ACTIVE 1
#define VISITED 2
RectLinkedList* find_path(Navmesh *mesh, Rect *start, Rect *target)
{
RectLinkedList *list_iterator;
Rect *rect=0, *other_rect=0;
#ifdef DEBUG_MODE
int debug_flag = 0;
if (debug_flag)
{
printf("\n\n\nBEGIN PATHFINDING DEBUG\n");
}
#endif
start->distance = 0.0;
start->activation = ACTIVE;
while (true)
{
rect = find_best_rect(mesh, target);
#ifdef DEBUG_MODE
if (debug_flag)
{
printf("\n\nNew rect observed; next rect=%d", rect);
}
#endif
if (rect != 0)
{
rect->history = add_to_linked_list(rect->history, rect);
for (int i=0; i<rect->num_connections; i++)
{
other_rect = rect->connections[i];
if (other_rect->activation == INACTIVE || rect->distance + distance(rect, other_rect) < other_rect->distance)
{
#ifdef DEBUG_MODE
if (debug_flag && other_rect == target)
{
printf("\n\nTarget (%i|%i) got updated", other_rect->bottomleft.x, other_rect->bottomleft.y);
}
#endif
// Overwrite everything
other_rect->distance = rect->distance + distance(rect, other_rect);
destroy_linked_list(other_rect->history, 0);
other_rect->history = copy_linked_list(rect->history);
#ifdef DEBUG_MODE
if (debug_flag && other_rect == target)
{
RectLinkedList *l = other_rect->history;
printf("\n\nResulting path:\n");
while (l != 0)
{
printf("(%i|%i) ", l->rect->bottomleft.x, l->rect->bottomleft.y);
l = l->next;
}
printf("\nPath done\n");
}
#endif
other_rect->activation = ACTIVE;
if (target->activation != INACTIVE)
{
if (target->distance < other_rect->distance)
{
// No sense in continuing this path anymore
other_rect->activation = VISITED;
}
}
}
}
rect->activation = VISITED;
}
else
{
// No active nodes left
// We're done
break;
}
}
RectLinkedList *path = copy_linked_list(target->history);
// Get rid of all lingering data for the next run
list_iterator = mesh->list;
while (list_iterator != 0)
{
rect = list_iterator->rect;
rect->distance = -1.0;
rect->activation = INACTIVE;
destroy_linked_list(rect->history, 0);
rect->history = 0;
list_iterator = list_iterator->next;
}
return path;
}
Rect* find_best_rect(Navmesh *mesh, Rect *target)
{
RectLinkedList *list_iterator;
Rect *rect, *best_rect=0;
int fitness = INT_MAX;
list_iterator = mesh->list;
while (list_iterator != 0)
{
rect = list_iterator->rect;
if (rect->activation == ACTIVE)
{
if (rect->distance + distance(rect, target) < fitness)
{
fitness = rect->distance + distance(rect, target);
best_rect = rect;
}
}
list_iterator = list_iterator->next;
}
return best_rect;
}