From f4de9d8b5cc2646e133d8c387fa1de5814cfb453 Mon Sep 17 00:00:00 2001 From: Nathan Wagner Date: Sun, 20 Dec 2015 16:09:02 -0600 Subject: [PATCH] minor cleanup and refactor --- astar.c | 27 +++++++++++++++------------ t/astar.c | 1 - 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/astar.c b/astar.c index 6e89758..107a986 100644 --- a/astar.c +++ b/astar.c @@ -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,7 +230,7 @@ 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) { diff --git a/t/astar.c b/t/astar.c index abdf4ec..cb7f043 100644 --- a/t/astar.c +++ b/t/astar.c @@ -36,7 +36,6 @@ int neighbor55(int hex, int dir) { int main(void) { struct HL_astar *p; - int length; plan_tests(16); -- 2.40.0