/
astar.c
289 lines (241 loc) · 6.98 KB
/
astar.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
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include "astar.h"
/* defines */
// first try with cost 1, because we do not allow to go diagonally
#define COST 1
/* private structs */
struct star_node {
struct star_node *parent;
point_t val;
unsigned int cost_to_goal, cost_from_start, cost;
};
typedef struct {
struct star_node *end, *cur;
} node_list;
/* function prototypes */
struct star_node *create_node( struct star_node *parent, point_t current, point_t target );
void add(node_list *list, struct star_node *node );
int add_adjacent_nodes( struct star_node *(*list)[4], struct star_node *node, const struct map_data *field, node_list closed, node_list open, point_t target );
int contains( node_list *list, struct star_node *node );
//struct star_node *next( node_list *list );
void reset( node_list *list );
int point_equal( point_t x, point_t y );
void cleanup( node_list list );
unsigned int estimate_cost_to_goal( point_t current, point_t target );
unsigned int get_cost_from_start(struct star_node *parent);
path_t *create_path_from_list( node_list *list );
int (*is_traversable)(const point_t, const struct map_data*);
int init_pathfinder(int (*walkable_function)(const point_t, const struct map_data*))
{
is_traversable = walkable_function;
return 0;
}
int cleanup_pathfinder() {
return 0;
}
path_t *search_path( const point_t start, const point_t target, const struct map_data *field )
{
node_list closed_list = { 0 };
node_list open_list = { 0 };
struct star_node *open_canidates[4] = { 0 };
int target_reached = 0;
struct star_node *start_node = create_node( NULL, start, target );
add( &closed_list, start_node );
add_adjacent_nodes( &open_canidates, start_node, field,\
closed_list, open_list, target );
while( !target_reached ) {
int i = 0;
struct star_node *cur = open_canidates[0];
#ifdef DEBUG_PATHFINDING
printf("current: (%d, %d)\n\r", cur->val.x, cur->val.y );
#endif
unsigned int min = cur->cost;
struct star_node *best = cur;
while ( i < 4 && (cur = open_canidates[++i]) != NULL ) {
add( &open_list, cur );
if ( min > cur->cost ) {
//free( best );
best = cur;
min = best->cost;
} else {
//free( cur );
}
}
add( &closed_list, best );
if ( point_equal( best->val, target ) ) {
target_reached = 1;
break;
}
while ( add_adjacent_nodes( &open_canidates, best, field,\
closed_list, open_list, target ) ) {
best = best->parent;
}
}
path_t *path_to_return = create_path_from_list( &closed_list );
//begin cleanup
cleanup( closed_list );
cleanup( open_list );
return path_to_return;
}
struct star_node *create_node( struct star_node *parent, point_t val, point_t target )
{
struct star_node *ret = malloc(sizeof(struct star_node));
if (!ret) return NULL;
ret->parent = parent;
ret->val = val;
if ( parent == NULL)
ret->cost_from_start = 0;
else
ret->cost_from_start = get_cost_from_start( parent );
ret->cost_to_goal = estimate_cost_to_goal( val, target );
ret->cost = ret->cost_from_start + ret->cost_to_goal;
return ret;
}
unsigned int estimate_cost_to_goal(point_t current, point_t target) // F
{
return (abs(current.x - target.x) + abs(current.y - target.y)) * COST;
}
unsigned int get_cost_from_start(struct star_node *parent) // G
{
return parent->cost_from_start + COST;
}
int add_adjacent_nodes( struct star_node *(*list)[4], struct star_node *node, const struct map_data *field, \
node_list closed, node_list open, point_t target )
{
int i = 0;
point_t cur = node->val;
point_t top = { cur.x, cur.y+1 };
point_t down = { cur.x, cur.y-1 };
point_t left = { cur.x-1, cur.y };
point_t right = { cur.x+1, cur.y };
// test wether they are in the closed list already
struct star_node *test = closed.end;
while ( test != NULL ) {
if ( point_equal( test->val, top) ) {
if ( test->cost_from_start + COST < node->cost_from_start )
node->parent = test;
top.x = -1;
} else if ( point_equal( test->val, down) ) {
if ( test->cost_from_start + COST < node->cost_from_start )
node->parent = test;
down.x = -1;
} else if ( point_equal( test->val, left ) ) {
if ( test->cost_from_start + COST < node->cost_from_start )
node->parent = test;
left.x = -1;
} else if ( point_equal( test->val, right ) ) {
if ( test->cost_from_start + COST < node->cost_from_start )
node->parent = test;
right.x = -1;
}
test = test->parent;
}
/*test = open.end;
while ( test != NULL ) {
if ( point_equal( test->val, top) ) {
if ( test->cost_from_start + COST < node->cost_from_start )
node->parent = test;
top.x = -1;
} else if ( point_equal( test->val, down) ) {
if ( test->cost_from_start + COST < node->cost_from_start )
node->parent = test;
down.x = -1;
} else if ( point_equal( test->val, left ) ) {
if ( test->cost_from_start + COST < node->cost_from_start )
node->parent = test;
left.x = -1;
} else if ( point_equal( test->val, right ) ) {
if ( test->cost_from_start + COST < node->cost_from_start )
node->parent = test;
right.x = -1;
}
test = test->parent;
}*/
if ( is_traversable( top, field ) ) {
(*list)[i++] = create_node( node, top, target );
#ifdef DEBUG_PATHFINDING
printf("Adding node at : (%d, %d)", top.x, top.y);
#endif
}
if ( is_traversable( down, field ) ) {
(*list)[i++] = create_node( node, down, target );
#ifdef DEBUG_PATHFINDING
printf("Adding node at : (%d, %d)", down.x, down.y);
#endif
}
if ( is_traversable( left, field ) ) {
(*list)[i++] = create_node( node, left, target );
#ifdef DEBUG_PATHFINDING
printf("Adding node at : (%d, %d)", left.x, left.y);
#endif
}
if ( is_traversable( right, field ) ) {
(*list)[i++] = create_node( node, right, target );
#ifdef DEBUG_PATHFINDING
printf("Adding node at : (%d, %d)", right.x, right.y);
#endif
}
if ( i == 0 ) {
return 1;
}
while ( i < 5 ) {
(*list)[i++] = NULL;
}
if ( *list[0] == NULL && *list[1] == NULL && *list[2] == NULL && *list[3] == NULL ) {
return 1;
}
return 0;
}
void add( node_list *list, struct star_node *node )
{
if ( node == NULL )
return;
#ifdef DEBUG_PATHFINDING
assert( list->end == node->parent );
#endif
list->end = node;
}
int point_equal( point_t x, point_t y )
{
return (x.x == y.x) && (x.y == y.y);
}
path_t *create_path_from_list( node_list *list )
{
path_t *ret = malloc( sizeof( path_t ) );
if ( ret == NULL)
return NULL;
size_t size = 0;
struct star_node *cur = list->end;
while (cur != NULL) {
size++;
cur = cur->parent;
}
ret->length = size;
ret->field = malloc( sizeof( point_t ) * size );
if ( ret->field == NULL ) {
free( ret );
fprintf( stderr, "Run out of memory\n!" );
return NULL;
}
cur = list->end;
//point_t test = { 0, 0 };
while ( size > 0 ) {
ret->field[--size] = cur->val;
cur = cur->parent;
}
return ret;
}
int contains( node_list *list, struct star_node *node )
{
struct star_node *cur = list->end;
while ( cur != NULL ) {
if ( point_equal( cur->val, node->val ) )
return 1;
}
return 0;
}
void cleanup( node_list list )
{
}