From 492ed6b629d5f1e22fd53f2f44b911d0a45d01f7 Mon Sep 17 00:00:00 2001 From: Kirill Petrashin Date: Thu, 26 Mar 2026 12:54:15 +0300 Subject: Add cost_array into neighbours_8dir() --- map.c | 12 ++++++++++-- map.h | 8 +++++--- path.c | 2 +- 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/map.c b/map.c index 2bf3ab0..9f1d206 100644 --- a/map.c +++ b/map.c @@ -24,7 +24,7 @@ Map empty_map(size_t width, size_t height) { } /* Honestly, what a shitty fucking way to implement this */ -unsigned int neighbours_4dir(Position neighbour_array[], Position pos, size_t width, size_t height, \ +unsigned int neighbours_4dir(Position neighbour_array[4], Position pos, size_t width, size_t height, \ char visited[height][width]) { size_t cur = 0; if (pos.x > 0 && !visited[pos.y][pos.x - 1]) { @@ -51,48 +51,56 @@ unsigned int neighbours_4dir(Position neighbour_array[], Position pos, size_t wi return cur; } -unsigned int neighbours_8dir(Position neighbour_array[], Position pos, size_t width, size_t height, \ +unsigned int neighbours_8dir(Position neighbour_array[8], size_t cost_array[8], Position pos, size_t width, size_t height, \ char visited[height][width]) { size_t cur = 0; 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; 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; 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; 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; 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; 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; 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; 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; cur += 1; } diff --git a/map.h b/map.h index d940357..ef76a7c 100644 --- a/map.h +++ b/map.h @@ -10,10 +10,12 @@ Map empty_map(size_t width, size_t height); /* Stores all the existing 4dir neighbours of pos in neighbour_array and returns their amount */ -unsigned int neighbours_4dir(Position neighbour_array[], Position pos, size_t width, size_t height, \ +unsigned int neighbours_4dir(Position neighbour_array[4], Position pos, size_t width, size_t height, \ char visited[height][width]); -/* Stores all the existing 8dir neighbours of pos in neighbour_array and returns their amount */ -unsigned int neighbours_8dir(Position neighbour_array[], Position pos, size_t width, size_t height, \ +/* Stores all the existing 8dir neighbours of pos in neighbour_array and returns their amount. + * Additionaly stores costs into cost_array if it's not NULL. + * The cost of goint orthogonally is 10, diagonaly is 14 (sqrt(2) * 10) */ +unsigned int neighbours_8dir(Position neighbour_array[8], size_t cost_array[8], Position pos, size_t width, size_t height, \ char visited[height][width]); /* https://en.wikipedia.org/wiki/Maze_generation_algorithm#Randomized_depth-first_search diff --git a/path.c b/path.c index adce6b2..37f2dbf 100644 --- a/path.c +++ b/path.c @@ -83,7 +83,7 @@ Path breadth_first_search_path_8dir(Map map, size_t width, size_t height, Positi } Position na[8]; - unsigned int nc = neighbours_8dir(na, cur, width, height, visited); + unsigned int nc = neighbours_8dir(na, NULL, cur, width, height, visited); for (unsigned int i = 0; i < nc; i++) { /* The Russian constitution doesn't allow walking on walls */ -- cgit v1.2.3