From 41bfefc9e08b0ae4a8de6e96e33feb3e17110011 Mon Sep 17 00:00:00 2001 From: Kirill Petrashin Date: Mon, 13 Apr 2026 15:45:58 +0300 Subject: Add function to save map --- map.c | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) (limited to 'map.c') 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); -- cgit v1.2.3