diff options
Diffstat (limited to 'path.c')
| -rw-r--r-- | path.c | 42 |
1 files changed, 35 insertions, 7 deletions
@@ -10,7 +10,7 @@ #include "error.h" /* TODO: somehow get offsets back to main */ -int anim(Map map, size_t width, size_t height, Position start, Position end, Position *cur, char visited[height][width], PositionPQ *frontier) { +int anim(Map map, size_t width, size_t height, Position start, Position end, Position *cur, char **visited, PositionPQ *frontier) { static int offset_y = 0, offset_x = 0; while (1) { draw_map(map, width, height, offset_x, offset_y, start, end, cur, NULL, visited, frontier); @@ -27,9 +27,9 @@ 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[height][width], char should_anim) { +Path breadth_first_search_path(int dirs, Map map, size_t width, size_t height, Position start, Position end, char **visited, 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; + unsigned int (*neighbours)(Position[], size_t[], Position, size_t, size_t, char**) = NULL; switch (dirs) { case 4: neighbours = &neighbours_4dir; break; case 8: neighbours = &neighbours_8dir; break; @@ -47,7 +47,7 @@ Path breadth_first_search_path(int dirs, Map map, size_t width, size_t height, P PositionPQ *frontier = ppq_new(start, 0); - memset(visited, 0, height * width * sizeof(char)); + visited_clear(visited, width, height); while (frontier != NULL) { Position cur = ppq_pop(&frontier); @@ -91,10 +91,10 @@ Path breadth_first_search_path(int dirs, Map map, size_t width, size_t height, P } /* FIXME: THIS IS NOT YET DIJKSTRA!!! don't use for now (WIP) */ -Path dijkstra_path(int dirs, Map map, size_t width, size_t height, Position start, Position end, char visited[height][width], char should_anim) { +Path dijkstra_path(int dirs, Map map, size_t width, size_t height, Position start, Position end, char **visited, char should_anim) { return NULL; /* The function to use to find neighbours */ - unsigned int (*neighbours)(Position[], size_t[], Position, size_t, size_t, char[height][width]) = NULL; + unsigned int (*neighbours)(Position[], size_t[], Position, size_t, size_t, char**) = NULL; switch (dirs) { case 4: neighbours = &neighbours_4dir; break; case 8: neighbours = &neighbours_8dir; break; @@ -112,7 +112,7 @@ Path dijkstra_path(int dirs, Map map, size_t width, size_t height, Position star PositionPQ *frontier = ppq_new(start, 0); - memset(visited, 0, height * width * sizeof(char)); + visited_clear(visited, width, height); while (frontier != NULL) { Position cur = ppq_pop(&frontier); @@ -168,3 +168,31 @@ void path_free(Path path, size_t height) { } free(path); } + +char **visited_new(size_t width, size_t height) { + char **visited = malloc(sizeof(char*) * height); + if (visited == NULL) return NULL; + + for (size_t row = 0; row < height; row++) { + visited[row] = malloc(sizeof(char) * width); + if (visited[row] == NULL) return NULL; + memset(visited[row], 0x00, width*sizeof(char)); + } + + return visited; +} + +void visited_clear(char **visited, size_t width, size_t height) { + for (size_t row = 0; row < height; row++) { + memset(visited[row], 0x00, sizeof(char) * width); + } +} + +void visited_free(char **visited, size_t height) { + if (visited == NULL) return; + for (size_t row = 0; row < height; row++) { + free(visited[row]); + } + free(visited); + return; +} |
