aboutsummaryrefslogtreecommitdiff
path: root/map.c
diff options
context:
space:
mode:
authorKirill Petrashin <kirill8201@yandex.ru>2026-04-13 15:45:58 +0300
committerKirill Petrashin <kirill8201@yandex.ru>2026-04-13 15:45:58 +0300
commit41bfefc9e08b0ae4a8de6e96e33feb3e17110011 (patch)
treed9f73a88bf9ffdc332caf060943a83c1ee0d1704 /map.c
parent3cc28bcfc63369013f8c24a4cf07a13fb54c86dc (diff)
downloadastar-41bfefc9e08b0ae4a8de6e96e33feb3e17110011.tar.xz
Add function to save map
Diffstat (limited to 'map.c')
-rw-r--r--map.c41
1 files changed, 41 insertions, 0 deletions
diff --git a/map.c b/map.c
index 569ab46..5dc2782 100644
--- a/map.c
+++ b/map.c
@@ -211,6 +211,31 @@ Map file_plaintext_map(char *filename, size_t *width, size_t *height, Position *
return map;
}
+void map_to_file_plaintext(char *filename, Map map, size_t width, size_t height, Position start, Position end) {
+ FILE *file = fopen(filename, "w");
+ if (file == NULL) {
+ message(height, "Couldn't open %s for writing", filename);
+ return;
+ }
+
+ /* Size and chars */
+ fprintf(file, "%zux%zu\n.#@x\n", width, height);
+ for (size_t row = 0; row < height; row++) {
+ for (size_t col = 0; col < width; col++) {
+ if (row == start.y && col == start.x) { putc('@', file); continue; }
+ if (row == end.y && col == end.x) { putc('x', file); continue; }
+ switch (map[row][col]) {
+ case EMPTY: putc('.', file); break;
+ case WALL: putc('#', file); break;
+ }
+ }
+ putc('\n', file);
+ }
+
+ message(height, "Saved the map to %s", filename);
+ return;
+}
+
/* TODO: so many fucking arguments lmao. Break it down into several functions? */
/* 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) */
@@ -455,6 +480,22 @@ void map_editor(Map *map, size_t *width, size_t *height, Position *start, Positi
break;
}
break;
+ case 's':
+ curs_set(2); /* Show the cursor */
+ echo(); /* Echo characters */
+
+ message(*height, FILENAME_PROMPT);
+ char filename[FILENAME_BUF_SIZE] = "map";
+ mvgetnstr(*height + map_offset_y + 1, map_offset_x - 2 + sizeof(FILENAME_PROMPT), filename, FILENAME_BUF_SIZE - 1);
+
+ curs_set(0); /* Hide the cursor */
+ noecho(); /* Don't echo characters */
+
+ map_to_file_plaintext(filename, *map, *width, *height, *start, *goal);
+
+ getch();
+ break;
+
/* TODO: keybindings to resize the map */
}
draw_map(*map, *width, *height, *start, *goal, NULL, NULL, NULL, NULL);