diff options
Diffstat (limited to 'main.c')
| -rw-r--r-- | main.c | 41 |
1 files changed, 35 insertions, 6 deletions
@@ -3,6 +3,7 @@ #include <stdlib.h> #include <string.h> #include <stdio.h> +#include <unistd.h> #include <time.h> #include "map.h" @@ -54,13 +55,42 @@ void init_ncurses(void) { int main(int argc, char **argv) { Position start_pos, end_pos; size_t width, height; - Map map; + Map map = NULL; size_t mwidth = 20; /* Maze width */ size_t mheight = 10; /* Maze height */ char is_maze = 0; - if (argc == 1 || !strcmp(argv[1], "-m")) { + char anim = 0; + + /* Handle args. + * Currently supported: + * -a to animate the pathfinding (get input after each step) + * -m {width}x{height} to give a maze of given size + * -f {filename} to load a map from the filename */ + int opt; + while ((opt = getopt(argc, argv, ":am:f:")) != -1) { + switch (opt) { + case 'a': anim = 1; break; + case 'm': + if (sscanf(optarg, "%zux%zu", &mwidth, &mheight) != 2) error("Wrong maze size string in argument\n"); + is_maze = 1; + map = rbt_maze_map(mwidth, mheight, (unsigned int) time(NULL)); + start_pos.x = 0; + start_pos.y = 0; + height = mheight * 2 - 1; + width = mwidth * 2 - 1; + end_pos.x = width - 1; + end_pos.y = height - 1; + break; + case 'f': + map = file_plaintext_map(optarg, &width, &height, &start_pos, &end_pos); + break; + case '?': error("Unknown option: %c\n", optopt); + case ':': error("Option %c requires an argument\n", optopt); + } + } + if (map == NULL) { /* If no map were given */ is_maze = 1; if (argc == 4) { /* maze size as argv[2] and [3] */ mwidth = atoi(argv[2]); @@ -73,9 +103,8 @@ int main(int argc, char **argv) { width = mwidth * 2 - 1; end_pos.x = width - 1; end_pos.y = height - 1; - } else { - map = file_plaintext_map(argv[1], &width, &height, &start_pos, &end_pos); } + signal(SIGINT, sigint_handler); init_ncurses(); @@ -86,7 +115,7 @@ int main(int argc, char **argv) { //print_map_out(map, width, height); char visited[height][width]; - Path path = breadth_first_search_path_8dir(map, width, height, start_pos, end_pos, visited, 1); + Path path = breadth_first_search_path_8dir(map, width, height, start_pos, end_pos, visited, anim); while (1) { draw_map(map, width, height, offset_x, offset_y, start_pos, end_pos, NULL, path, visited, NULL); @@ -100,7 +129,7 @@ int main(int argc, char **argv) { if (is_maze) { //FIXME: free it all before generating a new one map = rbt_maze_map(mwidth, mheight, (unsigned int) time(NULL)); - path = breadth_first_search_path_8dir(map, width, height, start_pos, end_pos, visited, 1); + path = breadth_first_search_path_8dir(map, width, height, start_pos, end_pos, visited, anim); } break; case 'q': endwin(); return 0; |
