aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKirill Petrashin <kirill8201@yandex.ru>2026-03-27 09:02:38 +0300
committerKirill Petrashin <kirill8201@yandex.ru>2026-03-27 09:02:38 +0300
commit28618b65b32009fd4e718578b3314e2ae91927f7 (patch)
tree8b6e3efc3c2eb8e0e2f5518ccffbaf55bb71d320
parent7e31a84be58805e3751c5aba27772bbb1716a5f5 (diff)
downloadastar-28618b65b32009fd4e718578b3314e2ae91927f7.tar.xz
Combine two functions into one in path.{c,h}
-rw-r--r--main.c12
-rw-r--r--map.c1
-rw-r--r--path.c58
-rw-r--r--path.h4
4 files changed, 17 insertions, 58 deletions
diff --git a/main.c b/main.c
index 331c7b4..7c95dd8 100644
--- a/main.c
+++ b/main.c
@@ -122,11 +122,7 @@ int main(int argc, char **argv) {
char visited[height][width];
Path path = NULL;
- if (dirs == 4) {
- path = breadth_first_search_path_4dir(map, width, height, start_pos, end_pos, visited, anim);
- } else {
- path = breadth_first_search_path_8dir(map, width, height, start_pos, end_pos, visited, anim);
- }
+ path = breadth_first_search_path(dirs, map, width, height, start_pos, end_pos, visited, anim);
while (1) {
draw_map(map, width, height, offset_x, offset_y, start_pos, end_pos, NULL, path, visited, NULL);
@@ -141,11 +137,7 @@ int main(int argc, char **argv) {
map_free(map, height);
map = rbt_maze_map(mwidth, mheight, rand());
path_free(path, height);
- if (dirs == 4) {
- path = breadth_first_search_path_4dir(map, width, height, start_pos, end_pos, visited, anim);
- } else {
- path = breadth_first_search_path_8dir(map, width, height, start_pos, end_pos, visited, anim);
- }
+ path = breadth_first_search_path(dirs, map, width, height, start_pos, end_pos, visited, anim);
}
break;
case 'q': map_free(map, height); path_free(path, height); endwin(); return 0;
diff --git a/map.c b/map.c
index 01f325f..d527cec 100644
--- a/map.c
+++ b/map.c
@@ -25,6 +25,7 @@ Map empty_map(size_t width, size_t height) {
/* Honestly, what a shitty fucking way to implement this */
/* When we allow maps to have costs, these two neighbours functions will have to use the instead of COST_* defines */
+/* TODO: maybe merge them, add `dirs` parameter, like we do in path.c? */
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;
diff --git a/path.c b/path.c
index 677fa8a..ac03b8b 100644
--- a/path.c
+++ b/path.c
@@ -7,6 +7,7 @@
#include "map.h"
#include "structs.h"
#include "priority_queue.h"
+#include "error.h"
int anim(Map map, size_t width, size_t height, Position start, Position end, Position *cur, char visited[height][width], PositionPQ *frontier) {
draw_map(map, width, height, 0, 0, start, end, cur, NULL, visited, frontier);
@@ -19,50 +20,15 @@ int anim(Map map, size_t width, size_t height, Position start, Position end, Pos
return 0;
}
/* BLOODY FUCK IT WORKS */
-Path breadth_first_search_path_4dir(Map map, size_t width, size_t height, Position start, Position end, char visited[height][width], char should_anim) {
- Path path = malloc(sizeof(PathNode)*height);
- if (path == NULL) return NULL;
-
- for (size_t row = 0; row < height; row++) {
- path[row] = malloc(sizeof(PathNode) * width);
- if (path[row] == NULL) return NULL;
- memset(path[row], 0, width * sizeof(PathNode));
+Path breadth_first_search_path(int dirs, Map map, size_t width, size_t height, Position start, Position end, char visited[height][width], char should_anim) {
+ /* The function to use to find neighbours */
+ unsigned int (*neighbours)(Position[], size_t[], Position, size_t, size_t, char[height][width]) = NULL;
+ switch (dirs) {
+ case 4: neighbours = &neighbours_4dir; break;
+ case 8: neighbours = &neighbours_8dir; break;
+ default: error("Tried to call breadth_first_search_path with wrong direction amount\n");
}
-
- PositionPQ *frontier = ppq_new(start, 0);
-
- memset(visited, 0, height * width * sizeof(char));
-
- while (frontier != NULL) {
- Position cur = ppq_pop(&frontier);
- visited[cur.y][cur.x] = 1;
-
- if (cur.x == end.x && cur.y == end.y) {
- ppq_free(frontier);
- return path; /* Found path */
- }
-
- Position na[4];
- 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 */
- if (map[na[i].y][na[i].x] == WALL) continue;
-
- if (ppq_insert(&frontier, na[i], 0) != PPQ_INSERT_ALREADY) {
- path[na[i].y][na[i].x].parent = cur;
- }
- }
-
- if (should_anim) {
- if (anim(map, width, height, start, end, &cur, visited, frontier) == -1) return NULL;
- }
- }
-
- return NULL;
-}
-
-Path breadth_first_search_path_8dir(Map map, size_t width, size_t height, Position start, Position end, char visited[height][width], char should_anim) {
+
Path path = malloc(sizeof(PathNode)*height);
if (path == NULL) return NULL;
@@ -85,8 +51,8 @@ Path breadth_first_search_path_8dir(Map map, size_t width, size_t height, Positi
return path; /* Found path */
}
- Position na[8];
- unsigned int nc = neighbours_8dir(na, NULL, cur, width, height, visited);
+ Position na[dirs];
+ unsigned int nc = neighbours(na, NULL, cur, width, height, visited);
for (unsigned int i = 0; i < nc; i++) {
/* The Russian constitution doesn't allow walking on walls */
@@ -96,7 +62,7 @@ Path breadth_first_search_path_8dir(Map map, size_t width, size_t height, Positi
path[na[i].y][na[i].x].parent = cur;
}
}
-
+
if (should_anim) {
if (anim(map, width, height, start, end, &cur, visited, frontier) == -1) return NULL;
}
diff --git a/path.h b/path.h
index 624516c..3ad089d 100644
--- a/path.h
+++ b/path.h
@@ -4,8 +4,8 @@
#include "structs.h"
#include "map.h"
-Path breadth_first_search_path_4dir(Map map, size_t width, size_t height, Position start, Position end, char visited[height][width], char should_anim);
-Path breadth_first_search_path_8dir(Map map, size_t width, size_t height, Position start, Position end, char visited[height][width], char should_anim);
+/* dirs can be 4 or 8 to disallow or allow diagonal movement */
+Path breadth_first_search_path(int dirs, Map map, size_t width, size_t height, Position start, Position end, char visited[height][width], char should_anim);
Path astar_path_4dir(Map map, size_t width, size_t height, Position start, Position end);
size_t manhattan_distance(Position a, Position b);