From 28618b65b32009fd4e718578b3314e2ae91927f7 Mon Sep 17 00:00:00 2001 From: Kirill Petrashin Date: Fri, 27 Mar 2026 09:02:38 +0300 Subject: Combine two functions into one in path.{c,h} --- path.c | 58 ++++++++++++---------------------------------------------- 1 file changed, 12 insertions(+), 46 deletions(-) (limited to 'path.c') 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; } -- cgit v1.2.3