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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
|
#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"
#include "bmp.h"
/* So, TODO for now:
- More messages
- more info in anim()?
- save pathfinding to a series of BMPs
- less magical values
- MORE MAPS FOR THE MAP PEOPLE
- Clean up unused `#include`s
- keybinding help screen
- 'clean' rendering mode, without all the ugly shit
- Comments */
void sigint_handler(int sig) {
(void)sig; /* We know it's a SIGINT */
printf("\033[?1003l\n"); /* Makes the terminal NOT report mouse movements */
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_YELLOW, COLOR_RED);
init_pair(FRONTIER_COLOR, COLOR_BLUE, COLOR_BLUE);
init_pair(CURSOR_COLOR, -1, COLOR_RED);
init_pair(WALL_TEXT_COLOR, COLOR_BLACK, COLOR_WHITE);
}
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 */
keypad(stdscr, TRUE);
initialize_colors();
}
int main(int argc, char **argv) {
Position start_pos, end_pos;
size_t width, height;
Map map = NULL;
size_t **cell_costs = NULL;
size_t mwidth = 20; /* Maze width */
size_t mheight = 10; /* Maze height */
char filename[FILENAME_BUF_SIZE] = "\0";
char is_maze = 0;
char bmp_only = 0;
char *bmp_filename = NULL;
char anim = 0;
int dirs = 8;
srand(time(NULL));
/* Handle args.
* Currently supported:
* -a to animate the pathfinding (get input after each step)
* -d for Dijkstra
* -m {width}x{height} to give a maze of given size
* -f {filename} to load a map from the filename
* -b {filename} to just generate a bitmap, no interface
* -4 for four directions
* -8 for eight directions */
int opt;
while ((opt = getopt(argc, argv, ":adm:f:b:48")) != -1) {
switch (opt) {
case 'a': anim = 1; break;
case 'd': path_func = &dijkstra_path; 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 'b':
bmp_only = 1;
bmp_filename = optarg;
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);
if (!bmp_only) {
init_ncurses();
draw_map(map, NULL, width, height, start_pos, end_pos, NULL, NULL, NULL, NULL);
wrefresh(stdscr);
}
char **visited = visited_new(width, height);
Path path = NULL;
path = path_func(dirs, map, cell_costs, width, height, start_pos, end_pos, visited, anim);
if (bmp_only) {
map_to_bmp(map, width, height, start_pos, end_pos, path, visited, bmp_filename);
printf("Wrote the bmp to %s. Pathfinding took %f seconds\n", bmp_filename, path_time);
exit(0);
}
#ifdef PID_MESSAGE
set_message("PID: %i", getpid());
#endif
MEVENT mevent;
//char mwheeldown = 0;
//mousemask(BUTTON3_PRESSED | BUTTON3_RELEASED | REPORT_MOUSE_POSITION, NULL);
mousemask(BUTTON2_PRESSED | BUTTON2_RELEASED, NULL);
mouseinterval(0);
while (1) {
draw_map(map, cell_costs, width, height, start_pos, end_pos, NULL, path, visited, NULL);
int c = getch();
/* TODO: keybinding to change dir amount */
/*
* Keybindings:
* [k] \
* [h] [l] } Move the view
* [j] /
*
* [K] \
* [H] [L] } Move the view faster
* [J] /
*
* - [i] \
* [y] [o] } Resize the maze
* [u] + /
*
* [z] - Move the view to the top left corner
* [x] - Move the view to the bottom right corner
*
* [g] followed by:
* [s] - Move the view to the start
* [e] - Move the view to the end
*
* [a] - If path wasn't found, try again. If it was, animate
* [A] - Force animate
*
* [d] - Switch algorithms (A* or Dijsktra's)
* [4] - Switch amount of directions (4 or 8)
*
* [f] - Toggle wraparound
*
* [r] - Reverse the path and switch start/end around
*
* [s] - Save the map to a bmp file
*
* [w] - Open a map from a plaintext file
*
* [n] - If in maze mode, generate a new maze
*
* [t] - Show time it took to calculate last path
*
* [c] - Load a costs file
*
* [e] - Enter the map editor
*
* [q] - Exit
*
* Mousebindings:
* Drag with MMB to move the view (buggy)
*/
switch (c) {
case KEY_MOUSE:
if (getmouse(&mevent) == OK && (mevent.bstate & BUTTON2_PRESSED)) {
/* FIXME: it's buggy. if we quickly click mwheel, it doesn't let go. Same in map_editor() */
mousemask(BUTTON2_PRESSED | BUTTON2_RELEASED | REPORT_MOUSE_POSITION, NULL);
printf("\033[?1003h"); fflush(stdout); /* Makes the terminal report mouse movements */
int init_x = mevent.x,
init_y = mevent.y,
init_map_offset_x = map_offset_x,
init_map_offset_y = map_offset_y;
while ((c = getch()) == KEY_MOUSE \
&& getmouse(&mevent) == OK && !(mevent.bstate & BUTTON2_RELEASED)) {
map_offset_y = init_map_offset_y + (mevent.y - init_y);
map_offset_x = init_map_offset_x + ((mevent.x - init_x) / 2 * 2);
draw_map(map, cell_costs, width, height, start_pos, end_pos, NULL, path, visited, NULL);
}
mousemask(BUTTON2_PRESSED | BUTTON2_RELEASED, NULL);
printf("\033[?1003l"); fflush(stdout); /* Makes the terminal NOT report mouse movements */
}
break;
case 'h': map_offset_x += 2; break;
case 'l': map_offset_x -= 2; break;
case 'j': map_offset_y -= 1; break;
case 'k': map_offset_y += 1; break;
case 'H': clear(); map_offset_x += 20; break;
case 'L': clear(); map_offset_x -= 20; break;
case 'J': clear(); map_offset_y -= 10; break;
case 'K': clear(); map_offset_y += 10; break;
case 'z': clear(); map_offset_x = 2; map_offset_y = 1; break; /* Move to top left corner */
case 'x': clear(); map_offset_x = - width * 2 + COLS - 2; map_offset_y = - height + LINES - 2; break; /* Move to bottom right corner */
case 'g':
switch (getch()) {
case 's':
clear();
map_offset_x = -start_pos.x * 2 + COLS/2;
map_offset_y = -start_pos.y + LINES/2;
break;
case 'e':
clear();
map_offset_x = -end_pos.x * 2 + COLS/2;
map_offset_y = -end_pos.y + LINES/2;
break;
}
break;
case 'a':
/* Only animate if there was already a path, otherwise just calculate one */
if (path == NULL) anim = 0;
else anim = 1;
path_free(path, height);
path = path_func(dirs, map, cell_costs, width, height, start_pos, end_pos, visited, anim);
clear_message();
break;
case 'A':
path_free(path, height);
path = path_func(dirs, map, cell_costs, width, height, start_pos, end_pos, visited, 1);
clear_message();
break;
case 'd':
if (path_func == astar_path) { set_message("Dijkstra's"); path_func = &dijkstra_path; }
else { set_message("A*"); path_func = &astar_path; };
path_free(path, height);
path = path_func(dirs, map, cell_costs, width, height, start_pos, end_pos, visited, anim);
/* TODO: print time */
break;
case '4':
if (dirs == 4) { set_message("8 directions"); dirs = 8; }
else { set_message("4 directions"); dirs = 4; };
path_free(path, height);
path = path_func(dirs, map, cell_costs, width, height, start_pos, end_pos, visited, anim);
break;
case 'f':
wraparound_enabled = !wraparound_enabled;
if (wraparound_enabled)
set_message("Enabled wraparound, only works on Dijkstra's")
else
set_message("Disabled wraparound");
if (path_func == dijkstra_path) {
path_free(path, height);
path = path_func(dirs, map, cell_costs, width, height, start_pos, end_pos, visited, anim);
}
break;
case 'r':
path_reverse(&path, width, height, &start_pos, &end_pos);
break;
case 'y':
case 'o':
case 'u':
case 'i':
if(is_maze) {
switch (c) {
case 'y': if (mwidth > 5) mwidth -= 1; break;
case 'o': mwidth += 1; break;
case 'u': mheight += 1; break;
case 'i': if (mheight > 2) mheight -= 1; break;
}
map_free(map, height);
visited_free(visited, height);
path_free(path, height);
cost_free(cell_costs, height);
cell_costs = NULL;
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 = path_func(dirs, map, cell_costs, width, height, start_pos, end_pos, visited, anim);
}
break;
case 's':
curs_set(2); /* Show the cursor */
echo(); /* Echo characters */
set_message(FILENAME_PROMPT);
print_message(height);
mvgetnstr(height + map_offset_y + 1, map_offset_x - 2 + sizeof(FILENAME_PROMPT), filename, FILENAME_BUF_SIZE - 1);
map_to_bmp(map, width, height, start_pos, end_pos, path, visited, filename);
curs_set(0); /* Hide the cursor */
noecho(); /* Don't echo characters */
getch();
break;
case 'w': /* FIXME: Keys don't make any sense anymore. 'o' should be open. And do them through defines for god's sake */
is_maze = 0;
curs_set(2); /* Show the cursor */
echo(); /* Echo characters */
set_message(FILENAME_PROMPT);
print_message(height);
char filename[FILENAME_BUF_SIZE] = "\0";
mvgetnstr(height + map_offset_y + 1, map_offset_x - 2 + sizeof(FILENAME_PROMPT), filename, FILENAME_BUF_SIZE - 1);
map_free(map, height);
path_free(path, height);
cost_free(cell_costs, height);
cell_costs = NULL;
visited = visited_new(width, height);
map = file_plaintext_map(filename, &width, &height, &start_pos, &end_pos);
visited = visited_new(width, height);
path = path_func(dirs, map, cell_costs, width, height, start_pos, end_pos, visited, anim);
set_message("Loaded map from %s", filename); print_message(height);
curs_set(0); /* Hide the cursor */
noecho(); /* Don't echo characters */
break;
case 'n':
if (is_maze) {
map_free(map, height);
path_free(path, height);
cost_free(cell_costs, height);
cell_costs = NULL;
map = rbt_maze_map(mwidth, mheight, rand());
path = path_func(dirs, map, cell_costs, width, height, start_pos, end_pos, visited, anim);
}
break;
case 't':
set_message("%f seconds", path_time); print_message(height);
break;
case 'c': /* Load a cost file */
curs_set(2); /* Show the cursor */
echo(); /* Echo characters */
set_message(FILENAME_PROMPT);
print_message(height);
char cost_filename[FILENAME_BUF_SIZE] = "\0";
mvgetnstr(height + map_offset_y + 1, map_offset_x - 2 + sizeof(FILENAME_PROMPT), cost_filename, FILENAME_BUF_SIZE - 1);
curs_set(0); /* Hide the cursor */
noecho(); /* Don't echo characters */
cell_costs = file_plaintext_costs(cost_filename, width, height);
path_free(path, height);
path = path_func(dirs, map, cell_costs, width, height, start_pos, end_pos, visited, anim);
break;
case 'e':
is_maze = 0;
visited_free(visited, height);
path_free(path, height);
cost_free(cell_costs, height);
cell_costs = NULL;
map_editor(&map, &width, &height, &start_pos, &end_pos, dirs);
set_message("You've left the map editor"); print_message(height);
wrefresh(curscr);
visited = visited_new(width, height);
path = path_func(dirs, map, cell_costs, width, height, start_pos, end_pos, visited, anim);
break;
case KEY_RESIZE: clear(); break;
case 'q': cost_free(cell_costs, height); visited_free(visited, height); map_free(map, height); path_free(path, height); endwin(); return 0;
}
}
endwin();
return 0;
}
|