10 * TODO use binary heap for the openlist and closed list.
11 * open list should be a min-heap on the f value
12 * closed list is just tested for existence, so a sort on
20 static double default_metric(int a, int b) {
24 static double default_heuristic(int a, int b) {
25 return (double)HL_distance(a, b);
28 struct HL_astar *HL_astar_init(struct HL_astar *state) {
30 state = malloc(sizeof *state);
31 if (!state) return NULL;
43 state->metric = default_metric;
44 state->heuristic = default_heuristic;
45 state->neighbor = HL_adjacent_hex;
51 * Reset a state to enpty.
53 struct HL_astar *HL_astar_clear(struct HL_astar *s) {
54 struct HL_astar_hex *hex;
62 hex = s->closed->next;
75 void HL_astar_free(struct HL_astar *s) {
76 struct HL_astar_hex *hex;
77 /* free open and closed lists */
84 hex = s->closed->next;
94 static struct HL_astar_hex *add_open(struct HL_astar *s, int hex) {
95 struct HL_astar_hex *node;
97 fprintf(stderr, "add to open %d\n", hex);
100 if (!s || s->error) {
102 fprintf(stderr, "returning null\n");
108 fprintf(stderr, "mallocing node(%zd)\n", sizeof *node);
111 /* TODO check in open or closed */
112 node = malloc(sizeof *node);
114 fprintf(stderr, "add to open %d = %p\n", hex, node);
124 s->open->prev = node;
126 node->next = s->open;
133 static struct HL_astar_hex *closehex(struct HL_astar *s, struct HL_astar_hex *h) {
134 if (!s || s->error) return NULL;
141 fprintf(stderr, "Closing hex: (%d,%d)\n", HL_cantor_x(h->hex), HL_cantor_y(h->hex));
144 if (h->prev) h->prev->next = h->next;
145 if (h->next) h->next->prev = h->prev;
146 if (s->closed) s->closed->prev = h;
147 if (s->open == h) s->open = h->next;
155 struct HL_astar_hex *in_closed(struct HL_astar *s, int hex) {
156 struct HL_astar_hex *h;
159 for (h = s->closed; h; h = h->next) {
160 if (h->hex == hex) return h;
165 struct HL_astar_hex *in_open(struct HL_astar *s, int hex) {
166 struct HL_astar_hex *h;
169 for (h = s->open; h; h = h->next) {
170 if (h->hex == hex) return h;
175 static struct HL_astar_hex *lowrank(struct HL_astar *s) {
176 struct HL_astar_hex *n, *r;
178 if (!s || s->open == 0) return 0;
181 for(n = r; n; n = n->next) {
191 void dump_list(char *name, struct HL_astar_hex *x) {
192 fprintf(stderr, "%s list: ", name);
193 for(; x; x = x->next) {
194 fprintf(stderr, "(%d,%d)", HL_cantor_x(x->hex), HL_cantor_y(x->hex));
196 fprintf(stderr, "\n");
201 * find a path from start to end. we use A*
203 int HL_findpath(struct HL_astar *s, int loops) {
205 struct HL_astar_hex *node, *x, *yopen;
211 if (s->start == s->goal) return 0;
213 node = add_open(s, s->start);
215 node->h = s->heuristic(s->start, s->goal);
219 fprintf(stderr, "A* findpath: ");
220 fprintf(stderr, "(%d,%d)", HL_cantor_x(s->start), HL_cantor_y(s->start));
221 fprintf(stderr, " -> ");
222 fprintf(stderr, "(%d,%d)", HL_cantor_x(s->goal), HL_cantor_y(s->goal));
223 fprintf(stderr, "\n");
229 dump_list("open", s->open);
230 dump_list("closed", s->closed);
235 fprintf(stderr, "checking lowest f hex = (%d,%d) f = %f, g = %f, h = %f\n",
236 HL_cantor_x(x->hex), HL_cantor_y(x->hex),
240 if (x->hex == s->goal) {
245 for (dir = 0; y = s->neighbor(x->hex, dir),dir<6; dir++) {
246 if (y == 0) continue; /* no hex in that direction */
247 if (in_closed(s, y)) {
251 fprintf(stderr, "checking dir %d, hex (%d, %d)\n", dir,
252 HL_cantor_x(y), HL_cantor_y(y));
255 cost = x->g + s->metric(x->hex, y);
257 yopen = in_open(s, y);
259 fprintf(stderr, "checking inopen = %p\n", yopen);
262 yopen = add_open(s, y);
264 } else if (cost < yopen->g) {
272 if (x->f < yopen->parent->f) {
279 yopen->h = s->heuristic(y, s->goal);
280 yopen->f = yopen->g + yopen->h;
282 fprintf(stderr, "maybe better hex = (%d,%d) f = %f, g = %f, h = %f\n",
283 HL_cantor_x(yopen->hex), HL_cantor_y(yopen->hex),
284 yopen->f, yopen->g, yopen->h
292 /* error no path ? */
293 if (x->hex != s->goal) {
299 fprintf(stderr, "found path: ");
302 /* reconstruct path */
305 for (s->pathlen = 0, yopen = x; yopen; yopen = yopen->parent) {
307 yopen->parent->next = yopen;
309 if (yopen->hex == s->start) s->from = yopen;
311 fprintf(stderr, "(%p %d %d,%d)",
313 HL_cantor_x(yopen->hex),
314 HL_cantor_y(yopen->hex)
322 fprintf(stderr, "\n");