From bab2ff2090e875ec5c0465e0d39011b0382d4c9e Mon Sep 17 00:00:00 2001 From: Kirill Petrashin Date: Wed, 15 Apr 2026 17:38:50 +0300 Subject: Add costs everywhere --- path.c | 59 ++++++++++++++++++++++++++++++----------------------------- 1 file changed, 30 insertions(+), 29 deletions(-) (limited to 'path.c') diff --git a/path.c b/path.c index 7d2c0bb..85f7804 100644 --- a/path.c +++ b/path.c @@ -12,7 +12,7 @@ #include "error.h" #include "config.h" -Path (*path_func)(int, Map, size_t, size_t, Position, Position, char **, char) = &astar_path; +Path (*path_func)(int, Map, size_t **, size_t, size_t, Position, Position, char **, char) = &astar_path; char anim_automatic = 0; @@ -62,11 +62,12 @@ 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(int dirs, Map map, size_t width, size_t height, Position start, Position end, char **visited, char should_anim) { +Path breadth_first_search_path(int dirs, Map map, size_t **cell_costs, size_t width, size_t height, Position start, Position end, char **visited, char should_anim) { + (void)cell_costs; /* BFS doesn't support costs */ anim_automatic = 0; /* The function to use to find neighbours */ - unsigned int (*neighbours)(Position[], size_t[], Position, size_t, size_t, char**) = NULL; + unsigned int (*neighbours)(Position[], size_t[], Position, size_t, size_t, char**, size_t**) = NULL; switch (dirs) { case 4: neighbours = &neighbours_4dir; break; case 8: neighbours = &neighbours_8dir; break; @@ -93,7 +94,7 @@ Path breadth_first_search_path(int dirs, Map map, size_t width, size_t height, P } Position na[dirs]; - unsigned int nc = neighbours(na, NULL, cur, width, height, visited); + unsigned int nc = neighbours(na, NULL, cur, width, height, visited, NULL); for (unsigned int i = 0; i < nc; i++) { /* The Russian constitution doesn't allow walking on walls */ @@ -121,11 +122,11 @@ Path breadth_first_search_path(int dirs, Map map, size_t width, size_t height, P return NULL; } -Path dijkstra_path(int dirs, Map map, size_t width, size_t height, Position start, Position end, char **visited, char should_anim) { +Path dijkstra_path(int dirs, Map map, size_t **cell_costs, size_t width, size_t height, Position start, Position end, char **visited, char should_anim) { anim_automatic = 0; /* The function to use to find neighbours */ - unsigned int (*neighbours)(Position[], size_t[], Position, size_t, size_t, char**) = NULL; + unsigned int (*neighbours)(Position[], size_t[], Position, size_t, size_t, char**, size_t**) = NULL; switch (dirs) { case 4: neighbours = &neighbours_4dir; break; @@ -139,7 +140,7 @@ Path dijkstra_path(int dirs, Map map, size_t width, size_t height, Position star PositionPQ *frontier = ppq_new(start, 0); visited_clear(visited, width, height); - size_t **cost_so_far = cost_so_far_new(width, height); + size_t **cost_so_far = cost_new(width, height); cost_so_far[start.y][start.x] = 0; while (frontier != NULL) { @@ -148,13 +149,13 @@ Path dijkstra_path(int dirs, Map map, size_t width, size_t height, Position star if (cur.x == end.x && cur.y == end.y) { ppq_free(frontier); - cost_so_far_free(cost_so_far, height); + cost_free(cost_so_far, height); return path; /* Found path */ } Position na[dirs]; size_t costs[dirs]; - unsigned int nc = neighbours(na, costs, cur, width, height, visited); + unsigned int nc = neighbours(na, costs, cur, width, height, visited, cell_costs); for (unsigned int i = 0; i < nc; i++) { /* The Russian constitution doesn't allow walking on walls */ @@ -173,7 +174,7 @@ Path dijkstra_path(int dirs, Map map, size_t width, size_t height, Position star clear(); path_free(path, height); ppq_free(frontier); - cost_so_far_free(cost_so_far, height); + cost_free(cost_so_far, height); return NULL; } } @@ -181,15 +182,15 @@ Path dijkstra_path(int dirs, Map map, size_t width, size_t height, Position star path_free(path, height); ppq_free(frontier); - cost_so_far_free(cost_so_far, height); + cost_free(cost_so_far, height); return NULL; } -Path astar_path(int dirs, Map map, size_t width, size_t height, Position start, Position end, char **visited, char should_anim) { +Path astar_path(int dirs, Map map, size_t **cell_costs, size_t width, size_t height, Position start, Position end, char **visited, char should_anim) { anim_automatic = 0; /* The function to use to find neighbours */ - unsigned int (*neighbours)(Position[], size_t[], Position, size_t, size_t, char**) = NULL; + unsigned int (*neighbours)(Position[], size_t[], Position, size_t, size_t, char**, size_t**) = NULL; /* The heuristic function */ size_t (*heuristic)(Position, Position) = NULL; @@ -206,7 +207,7 @@ Path astar_path(int dirs, Map map, size_t width, size_t height, Position start, PositionPQ *frontier = ppq_new(start, 0); visited_clear(visited, width, height); - size_t **cost_so_far = cost_so_far_new(width, height); + size_t **cost_so_far = cost_new(width, height); cost_so_far[start.y][start.x] = 0; while (frontier != NULL) { @@ -215,13 +216,13 @@ Path astar_path(int dirs, Map map, size_t width, size_t height, Position start, if (cur.x == end.x && cur.y == end.y) { ppq_free(frontier); - cost_so_far_free(cost_so_far, height); + cost_free(cost_so_far, height); return path; /* Found path */ } Position na[dirs]; size_t costs[dirs]; - unsigned int nc = neighbours(na, costs, cur, width, height, visited); + unsigned int nc = neighbours(na, costs, cur, width, height, visited, cell_costs); for (unsigned int i = 0; i < nc; i++) { /* The Russian constitution doesn't allow walking on walls */ @@ -241,7 +242,7 @@ Path astar_path(int dirs, Map map, size_t width, size_t height, Position start, clear(); path_free(path, height); ppq_free(frontier); - cost_so_far_free(cost_so_far, height); + cost_free(cost_so_far, height); return NULL; } } @@ -249,7 +250,7 @@ Path astar_path(int dirs, Map map, size_t width, size_t height, Position start, path_free(path, height); ppq_free(frontier); - cost_so_far_free(cost_so_far, height); + cost_free(cost_so_far, height); return NULL; } @@ -362,24 +363,24 @@ size_t visited_count(char **visited, size_t width, size_t height) { return count; } -size_t **cost_so_far_new(size_t width, size_t height) { - size_t **cost_so_far = malloc(sizeof(size_t*) * height); - if (cost_so_far == NULL) return NULL; +size_t **cost_new(size_t width, size_t height) { + size_t **cost = malloc(sizeof(size_t*) * height); + if (cost == NULL) return NULL; for (size_t row = 0; row < height; row++) { - cost_so_far[row] = malloc(sizeof(size_t) * width); - if (cost_so_far[row] == NULL) return NULL; - memset(cost_so_far[row], 0xFF, width*sizeof(size_t)); + cost[row] = malloc(sizeof(size_t) * width); + if (cost[row] == NULL) return NULL; + memset(cost[row], 0xFF, width*sizeof(size_t)); } - return cost_so_far; + return cost; } -void cost_so_far_free(size_t **cost_so_far, size_t height) { - if (cost_so_far == NULL) return; +void cost_free(size_t **cost, size_t height) { + if (cost == NULL) return; for (size_t row = 0; row < height; row++) { - free(cost_so_far[row]); + free(cost[row]); } - free(cost_so_far); + free(cost); return; } -- cgit v1.2.3