diff options
| author | Kirill Petrashin <kirill8201@yandex.ru> | 2026-04-13 15:45:58 +0300 |
|---|---|---|
| committer | Kirill Petrashin <kirill8201@yandex.ru> | 2026-04-13 15:45:58 +0300 |
| commit | 41bfefc9e08b0ae4a8de6e96e33feb3e17110011 (patch) | |
| tree | d9f73a88bf9ffdc332caf060943a83c1ee0d1704 | |
| parent | 3cc28bcfc63369013f8c24a4cf07a13fb54c86dc (diff) | |
| download | astar-41bfefc9e08b0ae4a8de6e96e33feb3e17110011.tar.xz | |
Add function to save map
| -rw-r--r-- | map.c | 41 | ||||
| -rw-r--r-- | map.h | 2 |
2 files changed, 43 insertions, 0 deletions
@@ -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); @@ -46,6 +46,8 @@ Map rbt_maze_map(size_t width, size_t height, unsigned int seed); * ....#.#... * ..@....... */ Map file_plaintext_map(char *filename, size_t *width, size_t *height, Position *start_pos, Position *end_pos); +/* The reverse of above */ +void map_to_file_plaintext(char *filename, Map map, size_t width, size_t height, Position start, Position end); /* Draw the map. Bet you didn't expect that. * path could be NULL to draw a map with no path. So can cursor, frontier and visited */ |
