aboutsummaryrefslogtreecommitdiff
path: root/map.c
diff options
context:
space:
mode:
authorKirill Petrashin <kirill8201@yandex.ru>2026-04-16 22:43:56 +0300
committerKirill Petrashin <kirill8201@yandex.ru>2026-04-16 22:43:56 +0300
commit7dfb37936206de02e9a8c0ef5a3e0593aa535cd1 (patch)
treeeef434c007e53a7447f5cc4744cae304fc0fdcd4 /map.c
parent5ef20063da838d81f9a9020a12bb69a01975a372 (diff)
downloadastar-7dfb37936206de02e9a8c0ef5a3e0593aa535cd1.tar.xz
Print path cost correctly
Diffstat (limited to 'map.c')
-rw-r--r--map.c29
1 files changed, 16 insertions, 13 deletions
diff --git a/map.c b/map.c
index 121970c..9917f5f 100644
--- a/map.c
+++ b/map.c
@@ -274,6 +274,8 @@ size_t **file_plaintext_costs(char *filename, size_t width, size_t height) {
fgetc(file); /* the newline */
}
+ set_message("Loaded costs from %s", filename);
+ print_message(height);
return costs;
}
@@ -306,9 +308,8 @@ void map_to_file_plaintext(char *filename, Map map, size_t width, size_t height,
/* TODO: so many fucking arguments lmao. Break it down into several functions? */
/* TODO: draw the start and goal with no background, allowing us to see the frontier/path/whatever's underneath. I think attr_get() will be useful */
-/* TODO: only draw a portion of the map (for bigger ones) */
/* TODO: draw arrow for visited too */
-void draw_map(Map map, size_t width, size_t height, Position start, Position goal, Position *cursor, Path path, char **visited, PositionPQ *frontier) {
+void draw_map(Map map, size_t **cell_costs, size_t width, size_t height, Position start, Position goal, Position *cursor, Path path, char **visited, PositionPQ *frontier) {
/* I think it flickers less when we do that
* UPD: it was causing a bug, so I commented it out. I don't know why either */
//wnoutrefresh(stdscr);
@@ -430,9 +431,11 @@ void draw_map(Map map, size_t width, size_t height, Position start, Position goa
}
if (cur.x - path[cur.y][cur.x].x == 0 || cur.y - path[cur.y][cur.x].y == 0) {
- length += COST_ORTHOGONAL;
+ if (cell_costs != NULL) length += cell_costs[cur.y][cur.x] * COST_ORTHOGONAL;
+ else length += COST_ORTHOGONAL;
} else {
- length += COST_DIAGONAL;
+ if (cell_costs != NULL) length += cell_costs[cur.y][cur.x] * COST_DIAGONAL;
+ else length += COST_DIAGONAL;
}
prev = cur;
cur = path[cur.y][cur.x];
@@ -507,7 +510,7 @@ void map_editor(Map *map, size_t *width, size_t *height, Position *start, Positi
char **visited = NULL;
Path path = NULL;
- draw_map(*map, *width, *height, *start, *goal, NULL, path, visited, NULL);
+ draw_map(*map, NULL, *width, *height, *start, *goal, NULL, path, visited, NULL);
set_message("You've entered the map editor. 'q' to quit");
print_message(*height);
@@ -608,7 +611,7 @@ void map_editor(Map *map, size_t *width, size_t *height, Position *start, Positi
start->x = col;
start->y = row;
if (should_pathfind) path = path_func(dirs, *map, NULL, *width, *height, *start, *goal, visited, 0);
- draw_map(*map, *width, *height, *start, *goal, NULL, path, visited, NULL);
+ draw_map(*map, NULL, *width, *height, *start, *goal, NULL, path, visited, NULL);
}
}
} else if (goal->x == col && goal->y == row) { /* Move the goal */
@@ -619,7 +622,7 @@ void map_editor(Map *map, size_t *width, size_t *height, Position *start, Positi
goal->x = col;
goal->y = row;
if (should_pathfind) path = path_func(dirs, *map, NULL, *width, *height, *start, *goal, visited, 0);
- draw_map(*map, *width, *height, *start, *goal, NULL, path, visited, NULL);
+ draw_map(*map, NULL, *width, *height, *start, *goal, NULL, path, visited, NULL);
}
}
} else if (row == *height) { /* Resize vertically */
@@ -631,7 +634,7 @@ void map_editor(Map *map, size_t *width, size_t *height, Position *start, Positi
/* Wait for BUTTON1_RELEASED */
while ((ch = getch()) == KEY_MOUSE && getmouse(&event) == OK && !(event.bstate & BUTTON1_RELEASED)) {
if (event.y <= map_offset_y) continue;
- draw_map(*map, *width, *height, *start, *goal, NULL, path, visited, NULL);
+ draw_map(*map, NULL, *width, *height, *start, *goal, NULL, path, visited, NULL);
for (size_t i = map_offset_x; i < *width*2 + map_offset_x; i++) {
mvaddch(event.y, i, '=');
}
@@ -664,7 +667,7 @@ void map_editor(Map *map, size_t *width, size_t *height, Position *start, Positi
visited = visited_new(*width, *height);
path = path_func(dirs, *map, NULL, *width, *height, *start, *goal, visited, 0);
}
- draw_map(*map, *width, *height, *start, *goal, NULL, path, visited, NULL);
+ draw_map(*map, NULL, *width, *height, *start, *goal, NULL, path, visited, NULL);
} else if (col == *width) { /* Resize horizontally */
size_t new_width = (event.x - map_offset_x) / 2;
@@ -678,7 +681,7 @@ void map_editor(Map *map, size_t *width, size_t *height, Position *start, Positi
while ((ch = getch()) == KEY_MOUSE && getmouse(&event) == OK && !(event.bstate & BUTTON1_RELEASED)) {
size_t new_width = (event.x - map_offset_x) / 2;
if (event.x <= map_offset_x || new_width == 0) continue;
- draw_map(*map, *width, *height, *start, *goal, NULL, path, visited, NULL);
+ draw_map(*map, NULL, *width, *height, *start, *goal, NULL, path, visited, NULL);
for (size_t i = map_offset_y; i < *height + map_offset_y; i++) {
mvaddch(i, new_width * 2 + map_offset_x, '|');
mvaddch(i, new_width * 2 + map_offset_x + 1, '|');
@@ -711,7 +714,7 @@ void map_editor(Map *map, size_t *width, size_t *height, Position *start, Positi
visited = visited_new(*width, *height);
path = path_func(dirs, *map, NULL, *width, *height, *start, *goal, visited, 0);
}
- draw_map(*map, *width, *height, *start, *goal, NULL, path, visited, NULL);
+ draw_map(*map, NULL, *width, *height, *start, *goal, NULL, path, visited, NULL);
} else { /* Start drawing */
m1down = 1;
if (row < *height && col < *width) {
@@ -734,13 +737,13 @@ void map_editor(Map *map, size_t *width, size_t *height, Position *start, Positi
if (should_pathfind) {
path = path_func(dirs, *map, NULL, *width, *height, *start, *goal, visited, 0);
}
- draw_map(*map, *width, *height, *start, *goal, NULL, path, visited, NULL);
+ draw_map(*map, NULL, *width, *height, *start, *goal, NULL, path, visited, NULL);
}
}
}
} else { /* Only update the map if not a mouse event */
/* FIXME: I don't like in how many places we call draw_map(), maybe there's a better way? */
- draw_map(*map, *width, *height, *start, *goal, NULL, path, visited, NULL);
+ draw_map(*map, NULL, *width, *height, *start, *goal, NULL, path, visited, NULL);
}
}