aboutsummaryrefslogtreecommitdiff
path: root/map.c
diff options
context:
space:
mode:
Diffstat (limited to 'map.c')
-rw-r--r--map.c123
1 files changed, 123 insertions, 0 deletions
diff --git a/map.c b/map.c
index 817f43c..c46c7db 100644
--- a/map.c
+++ b/map.c
@@ -10,6 +10,8 @@
#include "path.h"
#include "priority_queue.h"
+/* TODO: function to print a message maybe */
+
int map_offset_x = 2;
int map_offset_y = 1;
@@ -215,6 +217,8 @@ Map file_plaintext_map(char *filename, size_t *width, size_t *height, Position *
/* TODO: draw the start and goal with no background, allowing us to see the frontier/path/whatever's underneath. I think attr_get() will be useful */
/* TODO: only draw a portion of the map (for bigger ones) */
/* TODO: draw arrow for visited too */
+/* FIXME: clean up better maybe idk. There's a bug where parts of path may not be rendered if goal slightly above start in an empty area */
+/* FIXME: yeah there are plenty of bugs. the path just doesn't get rendered correctrly at times, even if it's calculated just fine. AND IT's SPECIFICALLY AFTER map_editor() WHAT IN THE ACTUAL FUCK AAAAAAAAAAAAAAAAAAAAAAAAAAAAA*/
void draw_map(Map map, size_t width, size_t height, Position start, Position goal, Position *cursor, Path path, char **visited, PositionPQ *frontier) {
/* I think it flickers less when we do that */
wnoutrefresh(stdscr);
@@ -391,3 +395,122 @@ 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);
+ mvprintw(*height + map_offset_y + 1, map_offset_x - 2, "You've entered the map editor. 'q' to quit");
+
+ MEVENT event;
+ mousemask(ALL_MOUSE_EVENTS | REPORT_MOUSE_POSITION, NULL);
+ mouseinterval(0);
+ printf("\033[?1003h\n"); /* Makes the terminal report mouse movements */
+
+ char m1down = 0;
+ MapTile cur = EMPTY;
+ while (1) {
+ int ch = getch();
+
+ if (ch == 'q') break;
+ switch (ch) {
+ case 'h': map_offset_x += 2; break;
+ case 'l': map_offset_x -= 2; break;
+ case 'j': map_offset_y -= 1; break;
+ case 'k': map_offset_y += 1; break;
+ }
+ draw_map(*map, *width, *height, *start, *goal, NULL, NULL, NULL, NULL);
+
+ if (ch == KEY_MOUSE) {
+ if (getmouse(&event) == OK) {
+ if (event.bstate & BUTTON1_PRESSED) {
+ /* Translate event coordinates into map coordinates */
+ size_t row = event.y - map_offset_y,
+ col = (event.x - map_offset_x) / 2;
+
+ if (start->x == col && start->y == row) { /* Move the start */
+ while ((ch = getch()) == KEY_MOUSE && getmouse(&event) == OK && !(event.bstate & BUTTON1_RELEASED)) {
+ row = event.y - map_offset_y;
+ col = (event.x - map_offset_x) / 2;
+ 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);
+ }
+ }
+ } else if (goal->x == col && goal->y == row) { /* Move the goal */
+ while ((ch = getch()) == KEY_MOUSE && getmouse(&event) == OK && !(event.bstate & BUTTON1_RELEASED)) {
+ row = event.y - map_offset_y;
+ col = (event.x - map_offset_x) / 2;
+ 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);
+ }
+ }
+ } else if (row == *height) { /* Resize vertically */
+ while ((ch = getch()) == KEY_MOUSE && getmouse(&event) == OK && !(event.bstate & BUTTON1_RELEASED)) {/* TODO: draw a line */};
+ size_t new_height = event.y - map_offset_y;
+ if (event.y <= map_offset_y) continue;
+ Map new_map = empty_map(*width, new_height);
+ map_copy(*map, *width, *height, new_map, *width, new_height);
+ map_free(*map, *height);
+ *height = new_height;
+ *map = new_map;
+ if (start->y >= *height) {
+ start->y = *height - 1;
+ }
+ if (goal->y >= *height) {
+ goal->y = *height - 1;
+ }
+ clear();
+ draw_map(*map, *width, *height, *start, *goal, NULL, NULL, NULL, NULL);
+ } else if (col == *width) { /* Resize horizontally */
+ while ((ch = getch()) == KEY_MOUSE && getmouse(&event) == OK && !(event.bstate & BUTTON1_RELEASED)) {/* TODO: draw a line */};
+ size_t new_width = (event.x - map_offset_x) / 2;
+ if (event.x <= map_offset_x) continue;
+ Map new_map = empty_map(new_width, *height);
+ map_copy(*map, *width, *height, new_map, new_width, *height);
+ map_free(*map, *height);
+ *width = new_width;
+ *map = new_map;
+ if (start->x >= *width) {
+ start->x = *width - 1;
+ }
+ if (goal->x >= *width) {
+ goal->x = *width - 1;
+ }
+ clear();
+ draw_map(*map, *width, *height, *start, *goal, NULL, NULL, NULL, NULL);
+ } else { /* Draw */
+ m1down = 1;
+ if (row < *height && col < *width) {
+ if ((*map)[row][col] == WALL)
+ cur = EMPTY;
+ else
+ cur = WALL;
+ }
+ }
+ } else if (event.bstate & BUTTON1_RELEASED) {
+ m1down = 0;
+ }
+ /* Draw */
+ if (m1down) {
+ size_t row = event.y - map_offset_y,
+ 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);
+ }
+ }
+ }
+ }
+ }
+
+ printf("\033[?1003l\n"); /* Makes the terminal NOT report mouse movements */
+}
+
+void map_copy(Map src, size_t src_w, size_t src_h, Map dest, size_t dest_w, size_t dest_h) {
+ for (size_t row = 0; row < src_h && row < dest_h; row++) {
+ for (size_t col = 0; col < src_w && col < dest_w; col++) {
+ dest[row][col] = src[row][col];
+ }
+ }
+}