diff options
| -rw-r--r-- | main.c | 10 | ||||
| -rw-r--r-- | path.c | 15 |
2 files changed, 20 insertions, 5 deletions
@@ -176,7 +176,15 @@ int main(int argc, char **argv) { } break; - case 'a': anim = !anim; break; + case 'a': + /* Only animate if there was already a path, otherwise just calculate one */ + if (path == NULL) anim = 0; + else anim = 1; + + path_free(path, height); + path = path_func(dirs, map, width, height, start_pos, end_pos, visited, anim); + clear_message(); + break; case 'd': if (path_func == astar_path) { set_message("Dijkstra"); path_func = &dijkstra_path; } @@ -12,14 +12,15 @@ #include "error.h" #include "config.h" +char anim_automatic = 0; + /* TODO: make it move the map maybe to show the path */ /* TODO: figure out input when automatic = 1. timeout() seems useful */ int anim(Map map, size_t width, size_t height, Position start, Position end, Position *cur, char **visited, PositionPQ *frontier) { - static char automatic = 0; while (1) { draw_map(map, width, height, start, end, cur, NULL, visited, frontier); set_message("cur: %zu %zu", cur->x, cur->y); print_message(height); - if (automatic) { wrefresh(stdscr); usleep(ANIM_DELAY_USEC); return 0; } + if (anim_automatic) { wrefresh(stdscr); usleep(ANIM_DELAY_USEC); return 0; } switch (getch()) { case 'h': map_offset_x += 2; break; case 'l': map_offset_x -= 2; break; @@ -51,7 +52,7 @@ int anim(Map map, size_t width, size_t height, Position start, Position end, Pos /* TODO: Add a binding to do a bmp */ - case 'a': automatic = 1; break; + case 'a': anim_automatic = 1; break; case 'q': clear_message(); return -1; default: return 0; } @@ -60,6 +61,8 @@ int anim(Map map, size_t width, size_t height, Position start, Position end, Pos } /* 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) { + anim_automatic = 0; + /* The function to use to find neighbours */ unsigned int (*neighbours)(Position[], size_t[], Position, size_t, size_t, char**) = NULL; switch (dirs) { @@ -123,6 +126,8 @@ Path breadth_first_search_path(int dirs, Map map, size_t width, size_t height, P } Path dijkstra_path(int dirs, Map map, 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; @@ -191,6 +196,8 @@ Path dijkstra_path(int dirs, Map map, size_t width, size_t height, Position star } Path astar_path(int dirs, Map map, 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; /* The heuristic function */ @@ -200,7 +207,7 @@ Path astar_path(int dirs, Map map, size_t width, size_t height, Position start, switch (dirs) { case 4: neighbours = &neighbours_4dir; heuristic = &manhattan_distance; break; case 8: neighbours = &neighbours_8dir; heuristic = &diagonal_distance; break; - default: error("Tried to call dijkstra_path with wrong direction amount\n"); + default: error("Tried to call astar_path with wrong direction amount\n"); } Path path = malloc(sizeof(PathNode)*height); |
