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
|
// A* graph search algorithm
//
// TODO
// - Set: abstraction for the node set data type.
// - Nearest: save the nearest node when the search is failed or not
// yet finished.
// - Sight: when two nodes are in direct sight of each other, skip
// nodes between them (Thera*).
#include "types.h"
#include <assert.h>
#include <stddef.h>
#ifdef __cplusplus
extern "C" {
#endif
#ifndef ASTAR_SET_SIZE
# define ASTAR_SET_SIZE 1024
#endif
enum {
ASTAR_PROGRESS = 0,
ASTAR_SUCCESS,
ASTAR_FAIL,
};
typedef struct {
i64 id;
i64 parent;
i64 exact_source_to_node;
i64 estimated_node_to_destination;
i64 estimated_source_to_destination;
} astar_node_t;
typedef struct {
b8 stop;
b8 skip;
i64 neighbor;
i64 distance;
} astar_link_t;
typedef struct {
i64 size;
astar_node_t values[ASTAR_SET_SIZE];
} astar_set_t;
typedef struct {
i64 status;
i64 source;
i64 destination;
astar_set_t open;
astar_set_t closed;
} astar_state_t;
#ifndef ASTAR_NEIGHBOR
# define ASTAR_NEIGHBOR(x, n) \
(astar_link_t) { \
.stop = 1, \
}
#endif
#ifndef ASTAR_HEURISTIC
# define ASTAR_HEURISTIC(x, y) (-1)
#endif
#ifdef __GNUC__
# pragma GCC diagnostic push
# pragma GCC diagnostic ignored "-Wunused-function"
# pragma GCC diagnostic ignored "-Wunknown-pragmas"
# pragma GCC push_options
# pragma GCC optimize("O3")
#endif
static void astar_iteration(astar_state_t *state) {
assert(state != NULL);
if (state == NULL)
return;
if (state->open.size == 0) {
state->status = ASTAR_FAIL;
return;
}
assert(state->open.values != NULL);
assert(state->closed.values != NULL);
// FIXME
// Use abstractions for set element search
//
// Find a node that is closest to the destination
//
astar_node_t nearest_node;
{
i64 index_nearest = 0;
for (i64 i = 1; i < state->open.size; i++)
if (state->open.values[i].estimated_source_to_destination <
state->open.values[index_nearest]
.estimated_source_to_destination)
index_nearest = i;
nearest_node = state->open.values[index_nearest];
if (index_nearest != state->open.size - 1)
state->open.values[index_nearest] =
state->open.values[state->open.size - 1];
--state->open.size;
}
// Enumerate all neighbors
//
for (i64 k = 0;; ++k) {
// Get a link to the next neighbor node
//
(void) nearest_node.id;
(void) k;
astar_link_t link = ASTAR_NEIGHBOR(nearest_node.id, k);
// If there is no more neighbors, break the loop
if (link.stop)
break;
// If there is no link, proceed to the next link
if (link.skip)
continue;
astar_node_t neighbor_node = {
.id = link.neighbor,
.parent = nearest_node.id,
.exact_source_to_node = nearest_node.exact_source_to_node +
link.distance,
.estimated_node_to_destination = -1,
.estimated_source_to_destination = -1,
};
// Calculate distance estimations
//
(void) neighbor_node.id;
(void) state->destination;
neighbor_node.estimated_node_to_destination = ASTAR_HEURISTIC(
neighbor_node.id, state->destination);
neighbor_node.estimated_source_to_destination =
neighbor_node.exact_source_to_node +
neighbor_node.estimated_node_to_destination;
// Check if we reached the destination
//
if (neighbor_node.id == state->destination) {
assert(state->closed.size + 2 <= ASTAR_SET_SIZE);
// FIXME
// Use abstractions for set element inserting
//
// Add the nearest node to the closed set
//
state->closed.values[state->closed.size] = nearest_node;
++state->closed.size;
// Add the neighbor node to the closed set
//
state->closed.values[state->closed.size] = neighbor_node;
++state->closed.size;
// Finish the search
state->status = ASTAR_SUCCESS;
return;
}
// FIXME
// Use abstractions for the set search
//
// Check if a node with a better estimate is already in the
// closed set
//
i64 index_in_closed = -1;
for (i64 i = 0; i < state->closed.size; ++i)
if (state->closed.values[i].id == neighbor_node.id) {
index_in_closed = i;
break;
}
if (index_in_closed != -1 &&
state->closed.values[index_in_closed]
.estimated_source_to_destination <
neighbor_node.estimated_source_to_destination)
// Skip this node
continue;
// Check if this node is in the open set
//
i64 index_in_open = -1;
for (i64 i = 0; i < state->closed.size; ++i)
if (state->open.values[i].id == neighbor_node.id) {
index_in_open = i;
break;
}
if (index_in_open != -1) {
if (state->open.values[index_in_open]
.estimated_source_to_destination <
neighbor_node.estimated_source_to_destination)
// Skip this node
continue;
// FIXME
// Use abstractions for set element erasing
//
// Remove this node from the open set
//
if (index_in_open != state->open.size - 1)
state->open.values[index_in_open] =
state->open.values[state->open.size - 1];
--state->open.size;
}
// FIXME
// Use abstractions to set element adding
//
// Add this node to the open set
//
assert(state->open.size + 1 <= ASTAR_SET_SIZE);
state->open.values[state->open.size] = neighbor_node;
++state->open.size;
}
// FIXME
// Use abstractions to set element adding
//
// Add the nearest node to the closed set
//
assert(state->closed.size + 1 <= ASTAR_SET_SIZE);
state->closed.values[state->closed.size] = nearest_node;
++state->closed.size;
// Continue the search
state->status = ASTAR_PROGRESS;
return;
}
#ifdef __GNUC__
# pragma GCC pop_options
# pragma GCC diagnostic pop
#endif
#ifdef __cplusplus
}
#endif
|