aboutsummaryrefslogtreecommitdiff
path: root/path.c
diff options
context:
space:
mode:
Diffstat (limited to 'path.c')
-rw-r--r--path.c59
1 files changed, 58 insertions, 1 deletions
diff --git a/path.c b/path.c
index e2d42c5..adce6b2 100644
--- a/path.c
+++ b/path.c
@@ -1,14 +1,25 @@
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
+#include <curses.h>
#include "path.h"
#include "map.h"
#include "structs.h"
#include "priority_queue.h"
+int anim(Map map, size_t width, size_t height, Position start, Position end, Position *cur, char visited[height][width], PositionPQ *frontier) {
+ draw_map(map, width, height, 0, 0, start, end, cur, NULL, visited, frontier);
+ mvprintw(height+2, 0, "cur: %zu %zu", cur->x, cur->y);
+ /* TODO: input, automatic mode */
+ switch (getch()) {
+ case 'q':
+ return -1;
+ }
+ return 0;
+}
/* BLOODY FUCK IT WORKS */
-Path breadth_first_search_path_4dir(Map map, size_t width, size_t height, Position start, Position end, char visited[height][width]) {
+Path breadth_first_search_path_4dir(Map map, size_t width, size_t height, Position start, Position end, char visited[height][width], char should_anim) {
Path path = malloc(sizeof(PathNode)*height);
if (path == NULL) return NULL;
@@ -40,6 +51,52 @@ Path breadth_first_search_path_4dir(Map map, size_t width, size_t height, Positi
ppq_insert(&frontier, na[i], 0);
path[na[i].y][na[i].x].parent = cur;
}
+
+ if (should_anim) {
+ if (anim(map, width, height, start, end, &cur, visited, frontier) == -1) return NULL;
+ }
+ }
+
+ return NULL;
+}
+
+Path breadth_first_search_path_8dir(Map map, size_t width, size_t height, Position start, Position end, char visited[height][width], char should_anim) {
+ Path path = malloc(sizeof(PathNode)*height);
+ if (path == NULL) return NULL;
+
+ for (size_t row = 0; row < height; row++) {
+ path[row] = malloc(sizeof(PathNode) * width);
+ if (path[row] == NULL) return NULL;
+ memset(path[row], 0, width * sizeof(PathNode));
+ }
+
+ PositionPQ *frontier = ppq_new(start, 0);
+
+ memset(visited, 0, height * width * sizeof(char));
+
+ while (frontier != NULL) {
+ Position cur = ppq_pop(&frontier);
+ visited[cur.y][cur.x] = 1;
+
+ if (cur.x == end.x && cur.y == end.y) {
+ return path; /* Found path */
+ }
+
+ Position na[8];
+ unsigned int nc = neighbours_8dir(na, cur, width, height, visited);
+
+ for (unsigned int i = 0; i < nc; i++) {
+ /* The Russian constitution doesn't allow walking on walls */
+ if (map[na[i].y][na[i].x] == WALL) continue;
+
+ if (ppq_insert(&frontier, na[i], 0) != 3) {
+ path[na[i].y][na[i].x].parent = cur;
+ }
+ }
+
+ if (should_anim) {
+ if (anim(map, width, height, start, end, &cur, visited, frontier) == -1) return NULL;
+ }
}
return NULL;