aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKirill Petrashin <kirill8201@yandex.ru>2026-04-16 23:02:27 +0300
committerKirill Petrashin <kirill8201@yandex.ru>2026-04-16 23:02:27 +0300
commit22b285ffc4345bee05772509a60fb77fb90d16a0 (patch)
treed981ffb1951028423fb4c832166b4efde6fe8029
parent7dfb37936206de02e9a8c0ef5a3e0593aa535cd1 (diff)
downloadastar-22b285ffc4345bee05772509a60fb77fb90d16a0.tar.xz
Add pathfinding timing
-rw-r--r--main.c8
-rw-r--r--path.c23
-rw-r--r--path.h2
3 files changed, 29 insertions, 4 deletions
diff --git a/main.c b/main.c
index 4577fca..9668236 100644
--- a/main.c
+++ b/main.c
@@ -23,11 +23,8 @@
- Clean up unused `#include`s
- keybinding help screen
- 'clean' rendering mode, without all the ugly shit
- - time
- Comments */
-/* FIXME: Costs are broken */
-
void sigint_handler(int sig) {
(void)sig; /* We know it's a SIGINT */
endwin();
@@ -144,7 +141,7 @@ int main(int argc, char **argv) {
if (bmp_only) {
map_to_bmp(map, width, height, start_pos, end_pos, path, visited, bmp_filename);
- printf("Wrote the bmp to %s\n", bmp_filename);
+ printf("Wrote the bmp to %s. Pathfinding took %f seconds\n", bmp_filename, path_time);
exit(0);
}
@@ -282,6 +279,9 @@ int main(int argc, char **argv) {
path = path_func(dirs, map, cell_costs, width, height, start_pos, end_pos, visited, anim);
}
break;
+ case 't':
+ set_message("%f seconds", path_time); print_message(height);
+ break;
case 'c': /* Load a cost file */
curs_set(2); /* Show the cursor */
diff --git a/path.c b/path.c
index 52f7291..7fcc73f 100644
--- a/path.c
+++ b/path.c
@@ -3,6 +3,7 @@
#include <string.h>
#include <curses.h>
#include <math.h>
+#include <time.h>
#include <unistd.h>
#include "path.h"
@@ -13,6 +14,7 @@
#include "config.h"
Path (*path_func)(int, Map, size_t **, size_t, size_t, Position, Position, char **, char) = &astar_path;
+double path_time = 0;
char anim_automatic = 0;
@@ -124,6 +126,8 @@ int anim(Map map, size_t width, size_t height, Position start, Position end, Pos
//}
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) {
+ clock_t tstart = clock();
+
anim_automatic = 0;
/* The function to use to find neighbours */
@@ -151,6 +155,10 @@ Path dijkstra_path(int dirs, Map map, size_t **cell_costs, size_t width, size_t
if (cur.x == end.x && cur.y == end.y) {
ppq_free(frontier);
cost_free(cost_so_far, height);
+
+ clock_t tend = clock();
+ path_time = (tend - tstart) / (double)CLOCKS_PER_SEC;
+
return path; /* Found path */
}
@@ -176,6 +184,8 @@ Path dijkstra_path(int dirs, Map map, size_t **cell_costs, size_t width, size_t
path_free(path, height);
ppq_free(frontier);
cost_free(cost_so_far, height);
+ clock_t tend = clock();
+ path_time = (tend - tstart) / (double)CLOCKS_PER_SEC;
return NULL;
}
}
@@ -184,10 +194,14 @@ Path dijkstra_path(int dirs, Map map, size_t **cell_costs, size_t width, size_t
path_free(path, height);
ppq_free(frontier);
cost_free(cost_so_far, height);
+ clock_t tend = clock();
+ path_time = (tend - tstart) / (double)CLOCKS_PER_SEC;
return NULL;
}
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) {
+ clock_t tstart = clock();
+
anim_automatic = 0;
/* The function to use to find neighbours */
@@ -218,6 +232,10 @@ Path astar_path(int dirs, Map map, size_t **cell_costs, size_t width, size_t hei
if (cur.x == end.x && cur.y == end.y) {
ppq_free(frontier);
cost_free(cost_so_far, height);
+
+ clock_t tend = clock();
+ path_time = (tend - tstart) / (double)CLOCKS_PER_SEC;
+
return path; /* Found path */
}
@@ -244,6 +262,8 @@ Path astar_path(int dirs, Map map, size_t **cell_costs, size_t width, size_t hei
path_free(path, height);
ppq_free(frontier);
cost_free(cost_so_far, height);
+ clock_t tend = clock();
+ path_time = (tend - tstart) / (double)CLOCKS_PER_SEC;
return NULL;
}
}
@@ -252,6 +272,9 @@ Path astar_path(int dirs, Map map, size_t **cell_costs, size_t width, size_t hei
path_free(path, height);
ppq_free(frontier);
cost_free(cost_so_far, height);
+
+ clock_t tend = clock();
+ path_time = (tend - tstart) / (double)CLOCKS_PER_SEC;
return NULL;
}
diff --git a/path.h b/path.h
index bceed05..ab22f8b 100644
--- a/path.h
+++ b/path.h
@@ -6,6 +6,8 @@
/* The currently chosen path func */
extern Path (*path_func)(int, Map, size_t **, size_t, size_t, Position, Position, char **, char);
+/* Time it took to calculate a path */
+extern double path_time;
/* 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);