X-Git-Url: https://pd.if.org/git/?p=hexagon;a=blobdiff_plain;f=astar.c;h=107a9869ec38505cf40605f0318affbdfc0812a9;hp=cabe5d08c4c36dec8d0bf86490fe5e2424e4a2a2;hb=f4de9d8b5cc2646e133d8c387fa1de5814cfb453;hpb=5c48a06bc8649958e69013c53e925b83398ebc74 diff --git a/astar.c b/astar.c index cabe5d0..107a986 100644 --- a/astar.c +++ b/astar.c @@ -1,11 +1,11 @@ -#include "hexmap.h" - #include #include #include #include #include +#include "hexagon.h" + /* * TODO use binary heap for the openlist and closed list. * open list should be a min-heap on the f value @@ -136,7 +136,7 @@ struct HL_astar_hex *in_closed(struct HL_astar *s, int hex) { struct HL_astar_hex *h; if (!s) return 0; - for(h = s->closed; h; h = h->next) { + for (h = s->closed; h; h = h->next) { if (h->hex == hex) return h; } return NULL; @@ -146,7 +146,7 @@ struct HL_astar_hex *in_open(struct HL_astar *s, int hex) { struct HL_astar_hex *h; if (!s) return 0; - for(h = s->open; h; h = h->next) { + for (h = s->open; h; h = h->next) { if (h->hex == hex) return h; } return NULL; @@ -167,6 +167,16 @@ static struct HL_astar_hex *lowrank(struct HL_astar *s) { return r; } +#ifdef DEBUG_ASTAR +void dump_list(char *name, HL_astar_hex *x) { + fprintf(stderr, "%s list: ", name); + for(; x; x = x->next) { + fprintf(stderr, "(%d,%d)", HL_cantor_x(x->hex), HL_cantor_y(x->hex)); + } + fprintf(stderr, "\n"); +} +#endif + /* * find a path from start to end. we use A* */ @@ -196,16 +206,8 @@ int HL_findpath(struct HL_astar *s, int loops) { while (s->open) { #ifdef DEBUG_ASTAR - fprintf(stderr, "open list: "); - for(x = s->open; x; x = x->next) { - fprintf(stderr, "(%d,%d)", HL_cantor_x(x->hex), HL_cantor_y(x->hex)); - } - fprintf(stderr, "\n"); - fprintf(stderr, "closed list: "); - for(x = s->closed; x; x = x->next) { - fprintf(stderr, "(%d,%d)", HL_cantor_x(x->hex), HL_cantor_y(x->hex)); - } - fprintf(stderr, "\n"); + dump_list("open", s->open); + dump_list("closed", s->closed); #endif x = lowrank(s); @@ -219,7 +221,8 @@ int HL_findpath(struct HL_astar *s, int loops) { break; } closehex(s, x); - for (dir = 0; y = s->neighbor(x->hex, dir); dir++) { + for (dir = 0; y = s->neighbor(x->hex, dir),dir<6; dir++) { + if (y == 0) continue; /* no hex in that direction */ if (in_closed(s, y)) { continue; } @@ -227,9 +230,8 @@ int HL_findpath(struct HL_astar *s, int loops) { cost = x->g + s->metric(x->hex, y); yopen = in_open(s, y); - if (! yopen) { + if (!yopen) { yopen = add_open(s, y); - tent_better = 1; } else if (cost < yopen->g) { tent_better = 1;