1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
|
#include <curses.h>
#include <signal.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <time.h>
#include "map.h"
#include "structs.h"
#include "config.h"
#include "error.h"
#include "priority_queue.h"
#include "path.h"
/* So, TODO for now:
- Generate an image??
- Allow maps to have costs
- Implement greedy-best-first algorithm (4 and 8 dir)
- Implement Dijkstra algorithm (4 and 8 dir)
- Implement the A* algorithm (4 and 8 dir)
- MORE MAPS FOR THE MAP PEOPLE
- Implement controls (to change maps, move start/goal, etc.)
- Clean up unused `#include`s
- mouse controls to edit map, drag start and end around */
void sigint_handler(int sig) {
(void)sig; /* We know it's a SIGINT */
endwin();
printf("Received SIGINT\n");
exit(1);
}
void initialize_colors(void) {
start_color();
use_default_colors();
init_pair(EMPTY_COLOR, COLOR_BLACK, -1);
init_pair(VISITED_COLOR, COLOR_GREEN, COLOR_GREEN);
init_pair(GOAL_COLOR, -1, COLOR_RED);
init_pair(WALL_COLOR, COLOR_WHITE, COLOR_WHITE);
init_pair(START_COLOR, -1, COLOR_RED);
init_pair(PATH_COLOR, COLOR_RED, COLOR_RED);
init_pair(FRONTIER_COLOR, COLOR_BLUE, COLOR_BLUE);
init_pair(CURSOR_COLOR, -1, COLOR_RED);
}
void init_ncurses(void) {
initscr(); /* Initialize the ncurses screen */
cbreak(); /* Process input one char at a time */
curs_set(0); /* Hide the cursor */
noecho(); /* Don't echo characters */
initialize_colors();
}
int main(int argc, char **argv) {
Position start_pos, end_pos;
size_t width, height;
Map map = NULL;
size_t mwidth = 20; /* Maze width */
size_t mheight = 10; /* Maze height */
char is_maze = 0;
char anim = 0;
int dirs = 8;
srand(time(NULL));
/* 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
* -4 for four directions
* -8 for eight directions */
/* TODO: Argument to choose the algorithm */
/* TODO: Argument to set seed */
int opt;
while ((opt = getopt(argc, argv, ":am:f:48")) != -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, rand());
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 '4': dirs = 4; break;
case '8': dirs = 8; 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]);
mheight = atoi(argv[3]);
}
map = rbt_maze_map(mwidth, mheight, rand());
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;
}
signal(SIGINT, sigint_handler);
init_ncurses();
int offset_x = 0, offset_y = 0;
draw_map(map, width, height, offset_x, offset_y, start_pos, end_pos, NULL, NULL, NULL, NULL);
//print_map_out(map, width, height);
char **visited = visited_new(width, height);
Path path = NULL;
path = breadth_first_search_path(dirs, 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);
char c = getch();
switch (c) {
/* FIXME: scroll the view not the content or whatever */
case 'h': offset_x += 2; break;
case 'l': offset_x -= 2; break;
case 'j': offset_y -= 1; break;
case 'k': offset_y += 1; break;
case 'a': anim = !anim; break;
case 'y':
case 'o':
case 'u':
case 'i':
if(is_maze) {
switch (c) {
case 'y': mwidth -= 1; break;
case 'o': mwidth += 1; break;
case 'u': mheight += 1; break;
case 'i': mheight -= 1; break;
}
map_free(map, height);
visited_free(visited, height);
path_free(path, height);
height = mheight * 2 - 1;
width = mwidth * 2 - 1;
end_pos.x = width - 1;
end_pos.y = height - 1;
map = rbt_maze_map(mwidth, mheight, rand());
visited = visited_new(width, height);
path = breadth_first_search_path(dirs, map, width, height, start_pos, end_pos, visited, anim);
}
break;
case 'n':
if (is_maze) {
map_free(map, height);
map = rbt_maze_map(mwidth, mheight, rand());
path_free(path, height);
path = breadth_first_search_path(dirs, map, width, height, start_pos, end_pos, visited, anim);
}
break;
case 'q': map_free(map, height); path_free(path, height); endwin(); return 0;
}
}
endwin();
return 0;
}
|