aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKirill Petrashin <kirill8201@yandex.ru>2026-03-27 08:48:48 +0300
committerKirill Petrashin <kirill8201@yandex.ru>2026-03-27 08:48:48 +0300
commit7e31a84be58805e3751c5aba27772bbb1716a5f5 (patch)
treed24954e0403340a68db005a2e4d194947298cf8a
parentc3530ad46236adbba18bf47a5cc3841b0578ff70 (diff)
downloadastar-7e31a84be58805e3751c5aba27772bbb1716a5f5.tar.xz
Add cost_array to neighbours_4dir
-rw-r--r--map.c10
-rw-r--r--map.h2
-rw-r--r--path.c2
3 files changed, 10 insertions, 4 deletions
diff --git a/map.c b/map.c
index 262d2ec..01f325f 100644
--- a/map.c
+++ b/map.c
@@ -24,27 +24,32 @@ 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[4], Position pos, size_t width, size_t height, \
+/* When we allow maps to have costs, these two neighbours functions will have to use the instead of COST_* defines */
+unsigned int neighbours_4dir(Position neighbour_array[4], size_t cost_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]) {
neighbour_array[cur].x = pos.x - 1;
neighbour_array[cur].y = pos.y;
+ 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] = 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] = 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] = COST_ORTHOGONAL;
cur += 1;
}
@@ -139,7 +144,7 @@ Map rbt_maze_map(size_t width, size_t height, unsigned int seed) {
srand(seed);
do {
- int nc = neighbours_4dir(na, ps_peek(ps), width, height, visited);
+ int nc = neighbours_4dir(na, NULL, ps_peek(ps), width, height, visited);
if (nc > 0) {
Position next = na[rand() % nc];
Position prev = ps_peek(ps);
@@ -201,6 +206,7 @@ Map file_plaintext_map(char *filename, size_t *width, size_t *height, Position *
}
/* 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 */
void draw_map(Map map, size_t width, size_t height, int offset_x, int offset_y, Position start, Position goal, Position *cursor, Path path, char visited[height][width], PositionPQ *frontier) {
/* I think it flickers less when we do that */
wnoutrefresh(stdscr);
diff --git a/map.h b/map.h
index b0cb6b1..e981513 100644
--- a/map.h
+++ b/map.h
@@ -10,7 +10,7 @@
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[4], Position pos, size_t width, size_t height, \
+unsigned int neighbours_4dir(Position neighbour_array[4], size_t cost_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.
* Additionaly stores costs into cost_array if it's not NULL.
diff --git a/path.c b/path.c
index e32610b..677fa8a 100644
--- a/path.c
+++ b/path.c
@@ -43,7 +43,7 @@ Path breadth_first_search_path_4dir(Map map, size_t width, size_t height, Positi
}
Position na[4];
- unsigned int nc = neighbours_4dir(na, cur, width, height, visited);
+ unsigned int nc = neighbours_4dir(na, NULL, cur, width, height, visited);
for (unsigned int i = 0; i < nc; i++) {
/* The Russian constitution doesn't allow walking on walls */