From 6834ffa31842ca249514854bac8bae0f0022f104 Mon Sep 17 00:00:00 2001 From: Kirill Petrashin Date: Thu, 23 Apr 2026 14:29:15 +0300 Subject: Make wraparound toggleable with a keybind --- main.c | 17 ++++++++++++++++- map.c | 2 +- path.c | 10 ++++++++-- path.h | 3 +++ 4 files changed, 28 insertions(+), 4 deletions(-) diff --git a/main.c b/main.c index 93dfc1e..db1d7a8 100644 --- a/main.c +++ b/main.c @@ -186,6 +186,8 @@ int main(int argc, char **argv) { * * [d] - Switch algorithms (A* or Dijsktra's) * + * [f] - Toggle wraparound + * * [r] - Reverse the path and switch start/end around * * [s] - Save the map to a bmp file @@ -271,13 +273,26 @@ int main(int argc, char **argv) { break; case 'd': - if (path_func == astar_path) { set_message("Dijkstra"); path_func = &dijkstra_path; } + if (path_func == astar_path) { set_message("Dijkstra's"); path_func = &dijkstra_path; } else { set_message("A*"); path_func = &astar_path; }; path_free(path, height); path = path_func(dirs, map, cell_costs, width, height, start_pos, end_pos, visited, anim); /* TODO: print time */ break; + case 'f': + wraparound_enabled = !wraparound_enabled; + if (wraparound_enabled) + set_message("Enabled wraparound, only works on Dijkstra's") + else + set_message("Disabled wraparound"); + + if (path_func == dijkstra_path) { + path_free(path, height); + path = path_func(dirs, map, cell_costs, width, height, start_pos, end_pos, visited, anim); + } + break; + case 'r': path_reverse(&path, width, height, &start_pos, &end_pos); break; diff --git a/map.c b/map.c index 8868e11..005936f 100644 --- a/map.c +++ b/map.c @@ -715,7 +715,7 @@ void map_editor(Map *map, size_t *width, size_t *height, Position *start, Positi break; case 'd': - if (path_func == astar_path) { set_message("Dijkstra"); path_func = &dijkstra_path; } + if (path_func == astar_path) { set_message("Dijkstra's"); path_func = &dijkstra_path; } else { set_message("A*"); path_func = &astar_path; }; path_free(path, *height); if (should_pathfind) path = path_func(dirs, *map, NULL, *width, *height, *start, *goal, visited, 0); diff --git a/path.c b/path.c index 15c7e31..450ea91 100644 --- a/path.c +++ b/path.c @@ -16,6 +16,8 @@ Path (*path_func)(int, Map, size_t **, size_t, size_t, Position, Position, char **, char) = &astar_path; double path_time = 0; +char wraparound_enabled = 0; + char anim_automatic = 0; /* TODO: make it move the map maybe to show the path */ @@ -134,8 +136,12 @@ Path dijkstra_path(int dirs, Map map, size_t **cell_costs, size_t width, size_t 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; + case 4: if (wraparound_enabled) neighbours = &neighbours_4dir_wraparound; + else neighbours = &neighbours_4dir; + break; + case 8: if (wraparound_enabled) neighbours = &neighbours_8dir_wraparound; + else neighbours = &neighbours_8dir; + break; default: error("Tried to call dijkstra_path with wrong direction amount\n"); } diff --git a/path.h b/path.h index 4a2159f..459bca6 100644 --- a/path.h +++ b/path.h @@ -9,6 +9,9 @@ extern Path (*path_func)(int, Map, size_t **, size_t, size_t, Position, Position /* Time it took to calculate a path */ extern double path_time; +/* Controls whether walls should wrap around, only works in Dijkstra's */ +extern char wraparound_enabled; + /* dirs can be 4 or 8 to disallow or allow diagonal movement */ //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); 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); -- cgit v1.2.3