aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKirill Petrashin <kirill8201@yandex.ru>2026-05-03 12:20:19 +0300
committerKirill Petrashin <kirill8201@yandex.ru>2026-05-03 12:20:19 +0300
commit9d15e1ab1966f949f0a1efbe54ada55fec095327 (patch)
tree968c1c9f6072285ef0c89389a92de8d4dcc14c2d
parent90234564e5502f9e6b15f3c9e0d9c431b1e50d34 (diff)
downloadastar-9d15e1ab1966f949f0a1efbe54ada55fec095327.tar.xz
Do keybinds through defines + other shit
-rw-r--r--config.h60
-rw-r--r--main.c76
-rw-r--r--map.c54
-rw-r--r--path.c29
4 files changed, 135 insertions, 84 deletions
diff --git a/config.h b/config.h
index 85b63fe..0c2679a 100644
--- a/config.h
+++ b/config.h
@@ -18,9 +18,67 @@
#define CURSOR_CHAR_1 '<'
#define CURSOR_CHAR_2 '>'
+/* WARNING: if you're changing the bindings, please update the comments in
+ * main.c and map.c */
+
+/* --- KEYBINDINGS --- */
+/* - Basic movements - */
+#define KEYBINDING_UP 'k'
+#define KEYBINDING_DOWN 'j'
+#define KEYBINDING_LEFT 'h'
+#define KEYBINDING_RIGHT 'l'
+
+/* - Fast movements - */
+#define KEYBINDING_FUP 'K'
+#define KEYBINDING_FDOWN 'J'
+#define KEYBINDING_FLEFT 'H'
+#define KEYBINDING_FRIGHT 'L' /* lmao fright */
+
+
+/* Move view to start/goal */
+#define KEYBINDING_GOTO 'g'
+#define KEYBINDING_GOTO_ST 's'
+#define KEYBINDING_GOTO_GL 'e'
+
+/* -- Miscellaneous -- */
+#define KEYBINDING_ALGO 'd' /* Toggle algorithms */
+#define KEYBINDING_DIRS '4' /* Toggle amount of directions */
+
+#define KEYBINDING_WRAPAROUND 'f'
+#define KEYBINDING_REVERSE_PATH 'r'
+#define KEYBINDING_PATH_TIME 't' /* Prints time it took to find a path */
+#define KEYBINDING_QUIT 'q'
+
+/* - Main screen-specific - */
+/* Maze resizing */
+#define KEYBINDING_MAZE_HOR_SHRINK 'y'
+#define KEYBINDING_MAZE_HOR_GROW 'o'
+#define KEYBINDING_MAZE_VER_SHRINK 'i'
+#define KEYBINDING_MAZE_VER_GROW 'u'
+
+#define KEYBINDING_REPATHFIND 'a'
+#define KEYBINDING_ANIM 'A'
+
+#define KEYBINDING_BMP 's'
+#define KEYBINDING_OPEN_MAP 'w'
+#define KEYBINDING_NEW_MAZE 'n'
+#define KEYBINDING_LOAD_COSTS 'c'
+
+#define KEYBINDING_EDITOR 'e'
+
+/* - Editor-specific - */
+#define KEYBINDING_TOGGLE_PATHFINDING 'a'
+#define KEYBINDING_CLEAR 'c'
+#define KEYBINDING_SAVE_MAP 's'
+
+/* - anim()-specific - */
+#define KEYBINDING_AUTO_ANIM 'a'
+
+/* --- End of keybindings --- */
+
#define MESSAGE_MAX_SIZE 256
#define FILENAME_BUF_SIZE 128
#define FILENAME_PROMPT "Filename:"
-#endif /*CONFIG_H_ */
+#endif /* CONFIG_H_ */
diff --git a/main.c b/main.c
index 58bcb04..3120c3b 100644
--- a/main.c
+++ b/main.c
@@ -161,7 +161,8 @@ int main(int argc, char **argv) {
draw_map(map, cell_costs, width, height, start_pos, end_pos, NULL, path, visited, NULL);
int c = getch();
- /*
+ /* WARNING: these might change. If something doesn't work, see config.h
+ *
* Keybindings:
* [k] \
* [h] [l] } Move the view
@@ -175,9 +176,6 @@ int main(int argc, char **argv) {
* [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
@@ -230,35 +228,30 @@ int main(int argc, char **argv) {
}
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 KEYBINDING_LEFT: map_offset_x += 2; break;
+ case KEYBINDING_RIGHT: map_offset_x -= 2; break;
+ case KEYBINDING_DOWN: map_offset_y -= 1; break;
+ case KEYBINDING_UP: 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 KEYBINDING_FLEFT: map_offset_x += 20; break;
+ case KEYBINDING_FRIGHT: map_offset_x -= 20; break;
+ case KEYBINDING_FDOWN: map_offset_y -= 10; break;
+ case KEYBINDING_FUP: 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':
+ case KEYBINDING_GOTO:
switch (getch()) {
- case 's':
- clear();
+ case KEYBINDING_GOTO_ST:
map_offset_x = -start_pos.x * 2 + COLS/2;
map_offset_y = -start_pos.y + LINES/2;
break;
- case 'e':
- clear();
+ case KEYBINDING_GOTO_GL:
map_offset_x = -end_pos.x * 2 + COLS/2;
map_offset_y = -end_pos.y + LINES/2;
break;
}
break;
- case 'a':
+ case KEYBINDING_REPATHFIND:
/* Only animate if there was already a path, otherwise just calculate one */
if (path == NULL) anim = 0;
else anim = 1;
@@ -266,15 +259,16 @@ int main(int argc, char **argv) {
path_free(path, height);
path = path_func(dirs, map, cell_costs, width, height, start_pos, end_pos, visited, anim);
clear_message();
+ anim = 0;
break;
- case 'A':
+ case KEYBINDING_ANIM:
path_free(path, height);
path = path_func(dirs, map, cell_costs, width, height, start_pos, end_pos, visited, 1);
clear_message();
break;
- case 'd':
+ case KEYBINDING_ALGO:
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);
@@ -282,7 +276,7 @@ int main(int argc, char **argv) {
/* TODO: print time */
break;
- case '4':
+ case KEYBINDING_DIRS:
if (dirs == 4) { set_message("8 directions"); dirs = 8; }
else { set_message("4 directions"); dirs = 4; };
@@ -290,7 +284,7 @@ int main(int argc, char **argv) {
path = path_func(dirs, map, cell_costs, width, height, start_pos, end_pos, visited, anim);
break;
- case 'f':
+ case KEYBINDING_WRAPAROUND:
wraparound_enabled = !wraparound_enabled;
if (wraparound_enabled)
set_message("Enabled wraparound, only works on Dijkstra's")
@@ -303,20 +297,20 @@ int main(int argc, char **argv) {
}
break;
- case 'r':
+ case KEYBINDING_REVERSE_PATH:
path_reverse(&path, width, height, &start_pos, &end_pos);
break;
- case 'y':
- case 'o':
- case 'u':
- case 'i':
+ case KEYBINDING_MAZE_HOR_SHRINK:
+ case KEYBINDING_MAZE_HOR_GROW:
+ case KEYBINDING_MAZE_VER_SHRINK:
+ case KEYBINDING_MAZE_VER_GROW:
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;
+ case KEYBINDING_MAZE_HOR_SHRINK: if (mwidth > 5) mwidth -= 1; break;
+ case KEYBINDING_MAZE_HOR_GROW: mwidth += 1; break;
+ case KEYBINDING_MAZE_VER_GROW: mheight += 1; break;
+ case KEYBINDING_MAZE_VER_SHRINK: if (mheight > 2) mheight -= 1; break;
}
map_free(map, height);
visited_free(visited, height);
@@ -335,7 +329,7 @@ int main(int argc, char **argv) {
}
break;
- case 's':
+ case KEYBINDING_BMP:
curs_set(2); /* Show the cursor */
echo(); /* Echo characters */
@@ -351,7 +345,7 @@ int main(int argc, char **argv) {
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 */
+ case KEYBINDING_OPEN_MAP:
is_maze = 0;
curs_set(2); /* Show the cursor */
echo(); /* Echo characters */
@@ -378,7 +372,7 @@ int main(int argc, char **argv) {
break;
- case 'n':
+ case KEYBINDING_NEW_MAZE:
if (is_maze) {
map_free(map, height);
path_free(path, height);
@@ -388,11 +382,11 @@ int main(int argc, char **argv) {
path = path_func(dirs, map, cell_costs, width, height, start_pos, end_pos, visited, anim);
}
break;
- case 't':
+ case KEYBINDING_PATH_TIME:
set_message("%f seconds", path_time); print_message(height);
break;
- case 'c': /* Load a cost file */
+ case KEYBINDING_LOAD_COSTS: /* Load a cost file */
curs_set(2); /* Show the cursor */
echo(); /* Echo characters */
@@ -409,7 +403,7 @@ int main(int argc, char **argv) {
break;
- case 'e':
+ case KEYBINDING_EDITOR:
is_maze = 0;
visited_free(visited, height);
path_free(path, height);
@@ -426,7 +420,7 @@ int main(int argc, char **argv) {
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;
+ case KEYBINDING_QUIT: cost_free(cell_costs, height); visited_free(visited, height); map_free(map, height); path_free(path, height); endwin(); return 0;
}
}
diff --git a/map.c b/map.c
index 2abaf68..dd062ce 100644
--- a/map.c
+++ b/map.c
@@ -632,7 +632,7 @@ void map_editor(Map *map, size_t *width, size_t *height, Position *start, Positi
char **visited = NULL;
Path path = NULL;
- set_message("You've entered the map editor. 'q' to quit");
+ set_message("You've entered the map editor. '%c' to quit", KEYBINDING_QUIT);
draw_map(*map, NULL, *width, *height, *start, *goal, NULL, path, visited, NULL);
MEVENT event;
@@ -646,7 +646,8 @@ void map_editor(Map *map, size_t *width, size_t *height, Position *start, Positi
while (1) {
int ch = getch();
- /*
+ /* WARNING: these might change. If something doesn't work, see config.h
+ *
* Keybindings:
* [k] \
* [h] [l] } Move the view
@@ -656,9 +657,6 @@ void map_editor(Map *map, size_t *width, size_t *height, Position *start, Positi
* [H] [L] } Move the view faster
* [J] /
*
- * [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
@@ -672,6 +670,8 @@ void map_editor(Map *map, size_t *width, size_t *height, Position *start, Positi
*
* [r] - Reverse path
*
+ * [t] - Show time it took to calculate last path
+ *
* [c] - Clear the map
*
* [s] - Save the map to a plaintext file
@@ -685,21 +685,19 @@ void map_editor(Map *map, size_t *width, size_t *height, Position *start, Positi
* Drag with MMB to move the view (buggy)
*/
switch (ch) {
- case 'q': clear_message(); visited_free(visited, *height); path_free(path, *height); mousemask(oldmask, NULL); printf("\033[?1003l\n"); return;
+ case KEYBINDING_QUIT: clear_message(); visited_free(visited, *height); path_free(path, *height); mousemask(oldmask, NULL); printf("\033[?1003l\n"); return;
- 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 KEYBINDING_LEFT: map_offset_x += 2; break;
+ case KEYBINDING_RIGHT: map_offset_x -= 2; break;
+ case KEYBINDING_DOWN: map_offset_y -= 1; break;
+ case KEYBINDING_UP: 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 KEYBINDING_FLEFT: map_offset_x += 20; break;
+ case KEYBINDING_FRIGHT: map_offset_x -= 20; break;
+ case KEYBINDING_FDOWN: map_offset_y -= 10; break;
+ case KEYBINDING_FUP: 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 'a':
+ case KEYBINDING_TOGGLE_PATHFINDING:
should_pathfind = !should_pathfind;
if (should_pathfind) {
set_message("Will pathfind");
@@ -713,14 +711,14 @@ void map_editor(Map *map, size_t *width, size_t *height, Position *start, Positi
print_message(*height);
break;
- case 'g':
+ case KEYBINDING_GOTO:
switch (getch()) {
- case 's':
+ case KEYBINDING_GOTO_ST:
clear();
map_offset_x = -start->x * 2 + COLS/2;
map_offset_y = -start->y + LINES/2;
break;
- case 'e':
+ case KEYBINDING_GOTO_GL:
clear();
map_offset_x = -goal->x * 2 + COLS/2;
map_offset_y = -goal->y + LINES/2;
@@ -728,7 +726,7 @@ void map_editor(Map *map, size_t *width, size_t *height, Position *start, Positi
}
break;
- case 'd':
+ case KEYBINDING_ALGO:
if (path_func == astar_path) { set_message("Dijkstra's"); path_func = &dijkstra_path; }
else { set_message("A*"); path_func = &astar_path; };
@@ -736,7 +734,7 @@ void map_editor(Map *map, size_t *width, size_t *height, Position *start, Positi
if (should_pathfind) path = path_func(dirs, *map, NULL, *width, *height, *start, *goal, visited, 0);
break;
- case '4':
+ case KEYBINDING_DIRS:
if (dirs == 4) { set_message("8 directions"); dirs = 8; }
else { set_message("4 directions"); dirs = 4; };
@@ -744,7 +742,7 @@ void map_editor(Map *map, size_t *width, size_t *height, Position *start, Positi
if (should_pathfind) path = path_func(dirs, *map, NULL, *width, *height, *start, *goal, visited, 0);
break;
- case 'f':
+ case KEYBINDING_WRAPAROUND:
wraparound_enabled = !wraparound_enabled;
if (wraparound_enabled)
set_message("Enabled wraparound, only works on Dijkstra's")
@@ -757,11 +755,11 @@ void map_editor(Map *map, size_t *width, size_t *height, Position *start, Positi
}
break;
- case 'r':
+ case KEYBINDING_REVERSE_PATH:
path_reverse(&path, *width, *height, start, goal);
break;
- case 'c':
+ case KEYBINDING_CLEAR:
map_free(*map, *height);
path_free(path, *height);
*map = empty_map(*width, *height);
@@ -770,7 +768,11 @@ void map_editor(Map *map, size_t *width, size_t *height, Position *start, Positi
}
break;
- case 's':
+ case KEYBINDING_PATH_TIME:
+ set_message("%f seconds", path_time); print_message(*height);
+ break;
+
+ case KEYBINDING_SAVE_MAP:
curs_set(2); /* Show the cursor */
echo(); /* Echo characters */
diff --git a/path.c b/path.c
index d60d800..9a2fe1f 100644
--- a/path.c
+++ b/path.c
@@ -27,27 +27,24 @@ int anim(Map map, size_t width, size_t height, Position start, Position end, Pos
set_message("cur: %zu %zu", cur->x, cur->y); print_message(height);
if (anim_automatic) { wrefresh(stdscr); usleep(ANIM_DELAY_USEC); return 0; }
switch (getch()) {
- 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 KEYBINDING_LEFT: map_offset_x += 2; break;
+ case KEYBINDING_RIGHT: map_offset_x -= 2; break;
+ case KEYBINDING_DOWN: map_offset_y -= 1; break;
+ case KEYBINDING_UP: 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 KEYBINDING_FLEFT: map_offset_x += 20; break;
+ case KEYBINDING_FRIGHT: map_offset_x -= 20; break;
+ case KEYBINDING_FDOWN: map_offset_y -= 10; break;
+ case KEYBINDING_FUP: 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':
+ case KEYBINDING_GOTO:
switch (getch()) {
- case 's':
+ case KEYBINDING_GOTO_ST:
clear();
map_offset_x = -start.x * 2 + COLS/2;
map_offset_y = -start.y + LINES/2;
break;
- case 'e':
+ case KEYBINDING_GOTO_GL:
clear();
map_offset_x = -end.x * 2 + COLS/2;
map_offset_y = -end.y + LINES/2;
@@ -57,8 +54,8 @@ int anim(Map map, size_t width, size_t height, Position start, Position end, Pos
/* TODO: Add a binding to do a bmp */
- case 'a': anim_automatic = 1; break;
- case 'q': clear_message(); return -1;
+ case KEYBINDING_AUTO_ANIM: anim_automatic = 1; break;
+ case KEYBINDING_QUIT: clear_message(); return -1;
default: return 0;
}
}