aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Makefile4
-rw-r--r--main.c11
-rw-r--r--map.c84
-rw-r--r--map.h2
-rw-r--r--path.c2
-rw-r--r--path.h3
6 files changed, 76 insertions, 30 deletions
diff --git a/Makefile b/Makefile
index 360214f..3c7addb 100644
--- a/Makefile
+++ b/Makefile
@@ -5,7 +5,7 @@ astar: *.c *.h
$(CC) -Wall -Wpedantic -Wextra -Werror -O3 -o $(EXECUTABLE) priority_queue.c map.c stack.c path.c bmp.c main.c -lncurses -lm
debug: *.c *.h
- $(CC) -Wall -g -o $(EXECUTABLE) priority_queue.c map.c stack.c path.c bmp.c main.c -lncurses -lm
+ $(CC) -Wall -g -o $(EXECUTABLE) -DPID_MESSAGE priority_queue.c map.c stack.c path.c bmp.c main.c -lncurses -lm
-clean:
+clean:
rm ./$(EXECUTABLE)
diff --git a/main.c b/main.c
index c909633..9706875 100644
--- a/main.c
+++ b/main.c
@@ -16,6 +16,8 @@
/* So, TODO for now:
- Allow maps to have costs
+ - Maybe make map editor the main thing
+ - More messages
- check ppq_insert() order
- more info in anim()
- save pathfinding to a series of BMPs
@@ -23,7 +25,6 @@
- MORE MAPS FOR THE MAP PEOPLE
- Clean up unused `#include`s
- keybinding help screen
- - live updating in map_editor()
- 'clean' rendering mode, without all the ugly shit
- Comments */
@@ -61,7 +62,6 @@ int main(int argc, char **argv) {
Position start_pos, end_pos;
size_t width, height;
Map map = NULL;
- Path (*path_func)(int, Map, size_t, size_t, Position, Position, char **, char) = &astar_path;
size_t mwidth = 20; /* Maze width */
size_t mheight = 10; /* Maze height */
@@ -145,6 +145,10 @@ int main(int argc, char **argv) {
exit(0);
}
+#ifdef PID_MESSAGE
+ set_message("PID: %i", getpid());
+#endif
+
while (1) {
draw_map(map, width, height, start_pos, end_pos, NULL, path, visited, NULL);
int c = getch();
@@ -253,7 +257,8 @@ int main(int argc, char **argv) {
visited_free(visited, height);
path_free(path, height);
- map_editor(&map, &width, &height, &start_pos, &end_pos);
+ map_editor(&map, &width, &height, &start_pos, &end_pos, dirs);
+ set_message("You've left the map editor"); print_message(height);
wrefresh(curscr);
visited = visited_new(width, height);
diff --git a/map.c b/map.c
index 3646c74..10b5d4d 100644
--- a/map.c
+++ b/map.c
@@ -436,8 +436,13 @@ void print_map_out(Map map, size_t width, size_t height) {
}
}
-void map_editor(Map *map, size_t *width, size_t *height, Position *start, Position *goal) {
- draw_map(*map, *width, *height, *start, *goal, NULL, NULL, NULL, NULL);
+void map_editor(Map *map, size_t *width, size_t *height, Position *start, Position *goal, int dirs) {
+ char should_pathfind = 0;
+
+ char **visited = NULL;
+ Path path = NULL;
+
+ draw_map(*map, *width, *height, *start, *goal, NULL, path, visited, NULL);
set_message("You've entered the map editor. 'q' to quit");
print_message(*height);
@@ -451,7 +456,8 @@ void map_editor(Map *map, size_t *width, size_t *height, Position *start, Positi
while (1) {
int ch = getch();
- if (ch == 'q') {clear_message(); break; }
+ /* FIXME: move this to the switch statement */
+ if (ch == 'q') { clear_message(); visited_free(visited, *height); path_free(path, *height); break; }
/* Handle keyboard keys */
switch (ch) {
case 'h': map_offset_x += 2; break;
@@ -466,6 +472,19 @@ void map_editor(Map *map, size_t *width, size_t *height, Position *start, Positi
case 'z': clear(); map_offset_x = 2; map_offset_y = 1; break; /* Move to top left corner */
case 'x': clear(); map_offset_x = - *width * 2 + COLS - 2; map_offset_y = - *height + LINES - 2; break; /* Move to bottom right corner */
+ case 'a':
+ should_pathfind = !should_pathfind;
+ if (should_pathfind) {
+ set_message("Will pathfind");
+ visited = visited_new(*width, *height);
+ path = path_func(dirs, *map, *width, *height, *start, *goal, visited, 0);
+ } else {
+ set_message("Won't pathfind");
+ visited_free(visited, *height); visited = NULL;
+ path_free(path, *height); path = NULL;
+ }
+ print_message(*height);
+ break;
case 'g':
switch (getch()) {
@@ -481,6 +500,14 @@ void map_editor(Map *map, size_t *width, size_t *height, Position *start, Positi
break;
}
break;
+
+ case 'd':
+ if (path_func == astar_path) { set_message("Dijkstra"); path_func = &dijkstra_path; }
+ else { set_message("A*"); path_func = &astar_path; };
+ path_free(path, *height);
+ if (should_pathfind) path = path_func(dirs, *map, *width, *height, *start, *goal, visited, 0);
+ break;
+
case 's':
curs_set(2); /* Show the cursor */
echo(); /* Echo characters */
@@ -516,7 +543,8 @@ void map_editor(Map *map, size_t *width, size_t *height, Position *start, Positi
if (row < *height && col < *width && (*map)[row][col] != WALL && (col != goal->x || row != goal->y)) {
start->x = col;
start->y = row;
- draw_map(*map, *width, *height, *start, *goal, NULL, NULL, NULL, NULL);
+ if (should_pathfind) path = path_func(dirs, *map, *width, *height, *start, *goal, visited, 0);
+ draw_map(*map, *width, *height, *start, *goal, NULL, path, visited, NULL);
}
}
} else if (goal->x == col && goal->y == row) { /* Move the goal */
@@ -526,7 +554,8 @@ void map_editor(Map *map, size_t *width, size_t *height, Position *start, Positi
if (row < *height && col < *width && (*map)[row][col] != WALL && (col != start->x || row != start->y)) {
goal->x = col;
goal->y = row;
- draw_map(*map, *width, *height, *start, *goal, NULL, NULL, NULL, NULL);
+ if (should_pathfind) path = path_func(dirs, *map, *width, *height, *start, *goal, visited, 0);
+ draw_map(*map, *width, *height, *start, *goal, NULL, path, visited, NULL);
}
}
} else if (row == *height) { /* Resize vertically */
@@ -536,16 +565,12 @@ void map_editor(Map *map, size_t *width, size_t *height, Position *start, Positi
}
/* Wait for BUTTON1_RELEASED */
- size_t old_event_y = event.y;
while ((ch = getch()) == KEY_MOUSE && getmouse(&event) == OK && !(event.bstate & BUTTON1_RELEASED)) {
if (event.y <= map_offset_y) continue;
- draw_map(*map, *width, *height, *start, *goal, NULL, NULL, NULL, NULL);
+ draw_map(*map, *width, *height, *start, *goal, NULL, path, visited, NULL);
for (size_t i = map_offset_x; i < *width*2 + map_offset_x; i++) {
- if (old_event_y > *height + map_offset_y + 1)
- mvaddch(old_event_y, i, ' ');
mvaddch(event.y, i, '=');
}
- old_event_y = event.y;
};
size_t new_height = event.y - map_offset_y;
@@ -553,12 +578,16 @@ void map_editor(Map *map, size_t *width, size_t *height, Position *start, Positi
Map new_map = empty_map(*width, new_height);
map_copy(*map, *width, *height, new_map, *width, new_height);
+
map_free(*map, *height);
+ visited_free(visited, *height);
+ path_free(path, *height);
*height = new_height;
*map = new_map;
/* Make sure start and goal aren't outside of the map */
+ /* FIXME: they can get stuck in walls */
if (start->y >= *height) {
start->y = *height - 1;
}
@@ -566,8 +595,11 @@ void map_editor(Map *map, size_t *width, size_t *height, Position *start, Positi
goal->y = *height - 1;
}
- clear();
- draw_map(*map, *width, *height, *start, *goal, NULL, NULL, NULL, NULL);
+ if (should_pathfind) {
+ visited = visited_new(*width, *height);
+ path = path_func(dirs, *map, *width, *height, *start, *goal, visited, 0);
+ }
+ draw_map(*map, *width, *height, *start, *goal, NULL, path, visited, NULL);
} else if (col == *width) { /* Resize horizontally */
size_t new_width = (event.x - map_offset_x) / 2;
@@ -578,20 +610,14 @@ void map_editor(Map *map, size_t *width, size_t *height, Position *start, Positi
}
/* Wait for BUTTON1_RELEASED */
- size_t old_new_width = (event.x - map_offset_x) / 2; /* Used to erase the old line */
while ((ch = getch()) == KEY_MOUSE && getmouse(&event) == OK && !(event.bstate & BUTTON1_RELEASED)) {
size_t new_width = (event.x - map_offset_x) / 2;
if (event.x <= map_offset_x || new_width == 0) continue;
- draw_map(*map, *width, *height, *start, *goal, NULL, NULL, NULL, NULL);
+ draw_map(*map, *width, *height, *start, *goal, NULL, path, visited, NULL);
for (size_t i = map_offset_y; i < *height + map_offset_y; i++) {
- if (old_new_width > *width + map_offset_x/2) {
- mvaddch(i, old_new_width * 2 + map_offset_x, ' ');
- mvaddch(i, old_new_width * 2 + map_offset_x + 1, ' ');
- }
mvaddch(i, new_width * 2 + map_offset_x, '|');
mvaddch(i, new_width * 2 + map_offset_x + 1, '|');
}
- old_new_width = new_width;
};
new_width = (event.x - map_offset_x) / 2;
@@ -599,7 +625,10 @@ void map_editor(Map *map, size_t *width, size_t *height, Position *start, Positi
Map new_map = empty_map(new_width, *height);
map_copy(*map, *width, *height, new_map, new_width, *height);
+
map_free(*map, *height);
+ visited_free(visited, *height);
+ path_free(path, *height);
*width = new_width;
*map = new_map;
@@ -612,8 +641,11 @@ void map_editor(Map *map, size_t *width, size_t *height, Position *start, Positi
goal->x = *width - 1;
}
- clear();
- draw_map(*map, *width, *height, *start, *goal, NULL, NULL, NULL, NULL);
+ if (should_pathfind) {
+ visited = visited_new(*width, *height);
+ path = path_func(dirs, *map, *width, *height, *start, *goal, visited, 0);
+ }
+ draw_map(*map, *width, *height, *start, *goal, NULL, path, visited, NULL);
} else { /* Start drawing */
m1down = 1;
if (row < *height && col < *width) {
@@ -632,13 +664,17 @@ void map_editor(Map *map, size_t *width, size_t *height, Position *start, Positi
col = (event.x - map_offset_x) / 2;
if (row < *height && col < *width) {
(*map)[row][col] = cur;
- draw_map(*map, *width, *height, *start, *goal, NULL, NULL, NULL, NULL);
+ path_free(path, *height);
+ if (should_pathfind) {
+ path = path_func(dirs, *map, *width, *height, *start, *goal, visited, 0);
+ }
+ draw_map(*map, *width, *height, *start, *goal, NULL, path, visited, NULL);
}
}
}
} else { /* Only update the map if not a mouse event */
-
- draw_map(*map, *width, *height, *start, *goal, NULL, NULL, NULL, NULL);
+ /* FIXME: I don't like in how many places we call draw_map(), maybe there's a better way? */
+ draw_map(*map, *width, *height, *start, *goal, NULL, path, visited, NULL);
}
}
diff --git a/map.h b/map.h
index 23a8773..a08fadb 100644
--- a/map.h
+++ b/map.h
@@ -60,7 +60,7 @@ void map_free(Map map, size_t height);
void print_map_out(Map map, size_t width, size_t height);
-void map_editor(Map *map, size_t *width, size_t *height, Position *start, Position *goal);
+void map_editor(Map *map, size_t *width, size_t *height, Position *start, Position *goal, int dirs);
void map_copy(Map src, size_t src_w, size_t src_h, Map dest, size_t dest_w, size_t dest_h);
diff --git a/path.c b/path.c
index fbd599c..7d2c0bb 100644
--- a/path.c
+++ b/path.c
@@ -12,6 +12,8 @@
#include "error.h"
#include "config.h"
+Path (*path_func)(int, Map, size_t, size_t, Position, Position, char **, char) = &astar_path;
+
char anim_automatic = 0;
/* TODO: make it move the map maybe to show the path */
diff --git a/path.h b/path.h
index 9f3f38f..6b4dda6 100644
--- a/path.h
+++ b/path.h
@@ -4,6 +4,9 @@
#include "structs.h"
#include "map.h"
+/* The currently chosen path func */
+extern Path (*path_func)(int, Map, size_t, size_t, Position, Position, char **, char);
+
/* dirs can be 4 or 8 to disallow or allow diagonal movement */
Path breadth_first_search_path(int dirs, Map map, size_t width, size_t height, Position start, Position end, char **visited, 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);