aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKirill Petrashin <kirill8201@yandex.ru>2026-03-26 14:14:28 +0300
committerKirill Petrashin <kirill8201@yandex.ru>2026-03-26 14:14:28 +0300
commit7bf60474879c52a9b10198e0571a4a46c8dd1fec (patch)
treeec8f517a0abf17eb4727e87d353131e5ecc8fd10
parent86f00ecf96149e0dd489979bd39e076131a16cf5 (diff)
downloadastar-7bf60474879c52a9b10198e0571a4a46c8dd1fec.tar.xz
Add cost defines and draw path cost under the map
-rw-r--r--config.h3
-rw-r--r--map.c27
2 files changed, 21 insertions, 9 deletions
diff --git a/config.h b/config.h
index a540c5c..a2c97dd 100644
--- a/config.h
+++ b/config.h
@@ -1,6 +1,9 @@
#ifndef CONFIG_H_
#define CONFIG_H_
+#define COST_ORTHOGONAL 10
+#define COST_DIAGONAL 14 /* sqrt(2) * 10 */
+
/* The characters that represent different tiles.
* Some have two characters -- that's because of the rendering trick where we
* use two characters back-to-back so they look like a square. */
diff --git a/map.c b/map.c
index 9443bcf..262d2ec 100644
--- a/map.c
+++ b/map.c
@@ -57,50 +57,50 @@ unsigned int neighbours_8dir(Position neighbour_array[8], size_t cost_array[8],
if (pos.x > 0 && !visited[pos.y][pos.x - 1]) {
neighbour_array[cur].x = pos.x - 1;
neighbour_array[cur].y = pos.y;
- if (cost_array != NULL) cost_array[cur] = 10;
+ if (cost_array != NULL) cost_array[cur] = COST_ORTHOGONAL;
cur += 1;
}
if (pos.x + 1 < width && !visited[pos.y][pos.x + 1]) {
neighbour_array[cur].x = pos.x + 1;
neighbour_array[cur].y = pos.y;
- if (cost_array != NULL) cost_array[cur] = 10;
+ if (cost_array != NULL) cost_array[cur] = COST_ORTHOGONAL;
cur += 1;
}
if (pos.y > 0 && !visited[pos.y - 1][pos.x]) {
neighbour_array[cur].x = pos.x;
neighbour_array[cur].y = pos.y - 1;
- if (cost_array != NULL) cost_array[cur] = 10;
+ if (cost_array != NULL) cost_array[cur] = COST_ORTHOGONAL;
cur += 1;
}
if (pos.y + 1 < height && !visited[pos.y + 1][pos.x]) {
neighbour_array[cur].x = pos.x;
neighbour_array[cur].y = pos.y + 1;
- if (cost_array != NULL) cost_array[cur] = 10;
+ if (cost_array != NULL) cost_array[cur] = COST_ORTHOGONAL;
cur += 1;
}
if (pos.x > 0 && pos.y > 0 && !visited[pos.y - 1][pos.x - 1]) {
neighbour_array[cur].x = pos.x - 1;
neighbour_array[cur].y = pos.y - 1;
- if (cost_array != NULL) cost_array[cur] = 14;
+ if (cost_array != NULL) cost_array[cur] = COST_DIAGONAL;
cur += 1;
}
if (pos.x + 1 < width && pos.y > 0 && !visited[pos.y - 1][pos.x + 1]) {
neighbour_array[cur].x = pos.x + 1;
neighbour_array[cur].y = pos.y - 1;
- if (cost_array != NULL) cost_array[cur] = 14;
+ if (cost_array != NULL) cost_array[cur] = COST_DIAGONAL;
cur += 1;
}
if (pos.x + 1 < width && pos.y + 1 < height && !visited[pos.y + 1][pos.x + 1]) {
neighbour_array[cur].x = pos.x + 1;
neighbour_array[cur].y = pos.y + 1;
- if (cost_array != NULL) cost_array[cur] = 14;
+ if (cost_array != NULL) cost_array[cur] = COST_DIAGONAL;
cur += 1;
}
if (pos.x > 0 && pos.y + 1 < height && !visited[pos.y + 1][pos.x - 1]) {
neighbour_array[cur].x = pos.x - 1;
neighbour_array[cur].y = pos.y + 1;
- if (cost_array != NULL) cost_array[cur] = 14;
+ if (cost_array != NULL) cost_array[cur] = COST_DIAGONAL;
cur += 1;
}
@@ -209,8 +209,9 @@ void draw_map(Map map, size_t width, size_t height, int offset_x, int offset_y,
for (ssize_t i = -1; i <= (ssize_t)(width*2 + 4); i++) { /* Horizontal */
mvaddch(DRAW_MAP_OFFSET_Y - 1 + offset_y - 1, i + offset_x, ' ');
mvaddch(DRAW_MAP_OFFSET_Y + height + offset_y + 1, i + offset_x, ' ');
+ mvaddch(DRAW_MAP_OFFSET_Y + height + offset_y + 2, i + offset_x, ' ');
}
- for (size_t i = 0; i <= height + 2; i++) { /* Horizontal */
+ for (size_t i = 0; i <= height + 2; i++) { /* Vertical */
mvaddch(i + offset_y, DRAW_MAP_OFFSET_X - 2 + offset_x - 2, ' ');
mvaddch(i + offset_y, DRAW_MAP_OFFSET_X - 1 + offset_x - 2, ' ');
mvaddch(i + offset_y, DRAW_MAP_OFFSET_X + width * 2 + offset_x + 2, ' ');
@@ -286,13 +287,21 @@ void draw_map(Map map, size_t width, size_t height, int offset_x, int offset_y,
/* Draw path */
if (path != NULL) {
+ size_t length = 0;
attron(COLOR_PAIR(PATH_COLOR));
Position cur = goal;
while (cur.x != start.x || cur.y != start.y) {
mvaddch(cur.y + DRAW_MAP_OFFSET_Y + offset_y, cur.x*2 + DRAW_MAP_OFFSET_X + offset_x, ' ');
mvaddch(cur.y + DRAW_MAP_OFFSET_Y + offset_y, cur.x*2 + DRAW_MAP_OFFSET_X + 1 + offset_x, ' ');
+ if (cur.x - path[cur.y][cur.x].parent.x == 0 || cur.y - path[cur.y][cur.x].parent.y == 0) {
+ length += COST_ORTHOGONAL;
+ } else {
+ length += COST_DIAGONAL;
+ }
cur = path[cur.y][cur.x].parent;
}
+ attroff(COLOR_PAIR(PATH_COLOR));
+ mvprintw(height + offset_y + DRAW_MAP_OFFSET_Y + 1, offset_x, "Path cost: %zu", length);
}
/* Draw the start */