]> pd.if.org Git - hexagon/commitdiff
minor cleanup and refactor
authorNathan Wagner <nw@hydaspes.if.org>
Sun, 20 Dec 2015 22:09:02 +0000 (16:09 -0600)
committerNathan Wagner <nw@hydaspes.if.org>
Sun, 20 Dec 2015 22:09:02 +0000 (16:09 -0600)
astar.c
t/astar.c

diff --git a/astar.c b/astar.c
index 6e897589bf3561f5edceb37d6e243211c3490448..107a9869ec38505cf40605f0318affbdfc0812a9 100644 (file)
--- a/astar.c
+++ b/astar.c
@@ -167,6 +167,16 @@ static struct HL_astar_hex *lowrank(struct HL_astar *s) {
        return r;
 }
 
        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*
  */
 /*
  * 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
        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);
 #endif
 
                x = lowrank(s);
@@ -219,7 +221,8 @@ int HL_findpath(struct HL_astar *s, int loops) {
                        break;
                }
                closehex(s, x);
                        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;
                        }
                        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);
                        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) {
                                yopen = add_open(s, y);
                                tent_better = 1;
                        } else if (cost < yopen->g) {
index abdf4ec94eb090669b9fa1732e838101c586b23d..cb7f0434711c71dd59947a3fbc8e8d94d59351ee 100644 (file)
--- 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 main(void) {
        struct HL_astar *p;
-       int length;
 
        plan_tests(16);
 
 
        plan_tests(16);