From 2fc9dfc526892ae05fae2fc059464ec394493f5a Mon Sep 17 00:00:00 2001 From: Markus Armbruster Date: Mon, 21 Feb 2011 22:49:44 +0100 Subject: [PATCH] New path_find_visualize(), to aid debugging --- include/path.h | 3 ++ src/lib/common/pathfind.c | 74 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+) diff --git a/include/path.h b/include/path.h index 15ae099d6..287ec02c7 100644 --- a/include/path.h +++ b/include/path.h @@ -73,6 +73,9 @@ extern void path_find_from(coord, coord, natid, int); extern double path_find_to(coord, coord); extern double path_find(coord, coord, coord, coord, natid, int); extern size_t path_find_route(char *, size_t, coord, coord, coord, coord); +#ifdef PATH_FIND_DEBUG +extern void path_find_visualize(coord, coord, coord, coord); +#endif #ifdef PATH_FIND_STATS extern void path_find_print_stats(void); #else diff --git a/src/lib/common/pathfind.c b/src/lib/common/pathfind.c index a7f963618..7316d63ee 100644 --- a/src/lib/common/pathfind.c +++ b/src/lib/common/pathfind.c @@ -280,6 +280,20 @@ pf_close(void) pf_check(); } +/* silence "not used" warning */ +#ifdef PATH_FIND_DEBUG +/* + * Return cost from source to sector with uid UID. + * It must be visited, i.e. open or closed. + */ +static double +pf_cost(int uid) +{ + assert(!pf_is_unvisited(uid)); + return pf_map[uid].cost; +} +#endif + static coord x_in_dir(coord x, int dir) { @@ -476,6 +490,66 @@ bufrotate(char *buf, size_t bufsz, size_t i) return buf; } +#ifdef PATH_FIND_DEBUG +void +path_find_visualize(coord sx, coord sy, coord dx, coord dy) +{ + int uid; + int xmin, xmax, ymin, ymax, x, y, odd, ch; + double c, u, cost; + char buf[1024]; + + assert(pf_cost(XYOFFSET(sx, sy)) == 0.0); + c = pf_cost(XYOFFSET(dx, dy)); + u = c / 10.0; + + /* find bounding box */ + xmin = xmax = 0; + ymin = ymax = 0; + for (y = -WORLD_Y / 2; y < WORLD_Y / 2; y++) { + odd = ((sx + -WORLD_X / 2) ^ (sy + y)) & 1; + for (x = -WORLD_X / 2 + odd; x < WORLD_X / 2; x += 2) { + uid = XYOFFSET(XNORM(sx + x), YNORM(sy + y)); + if (pf_is_unvisited(uid)) + continue; + if (xmin > x) + xmin = x; + if (xmax < x) + xmax = x; + if (ymin > y) + ymin = y; + if (ymax < y) + ymax = y; + } + } + printf("bbox %d:%d,%d:%d origin %d,%d\n", + xmin, xmax, ymin, ymax, sx, sy); + + for (y = ymin; y <= ymax; y++) { + odd = ((sx + xmin) ^ (sy + y)) & 1; + if (odd) + printf(" "); + for (x = xmin + odd; x <= xmax; x += 2) { + uid = XYOFFSET(XNORM(sx + x), YNORM(sy + y)); + if (pf_is_unvisited(uid)) + ch = ' '; + else if (uid == XYOFFSET(dx, dy)) + ch = 'D'; + else if (uid == XYOFFSET(sx, sy)) + ch = 'S'; + else { + cost = pf_cost(uid); + ch = cost > c ? '+' : '0' + (int)(10 * (cost / c)); + } + printf(" %c", ch); + } + printf("\n"); + } + path_find_route(buf, sizeof(buf), sx, sy, dx, dy); + printf("%s %g\n", buf, pf_cost(XYOFFSET(dx, dy))); +} +#endif + #ifdef PATH_FIND_STATS void path_find_print_stats(void) -- 2.43.0