aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKirill Petrashin <kirill8201@yandex.ru>2026-04-22 20:26:03 +0300
committerKirill Petrashin <kirill8201@yandex.ru>2026-04-22 20:26:03 +0300
commit1cd0a30593d9b7805752e3e183c93f622211ed0b (patch)
tree574653e214edcff4270daa78b3eeeb3ca0cb2d75
parent8282175440876b28d431167ee0702404b09bf2e6 (diff)
downloadastar-1cd0a30593d9b7805752e3e183c93f622211ed0b.tar.xz
Document keybindings, add MMB to move in map_editor(), add 'A' keybind to force anim
-rw-r--r--main.c52
-rw-r--r--map.c48
2 files changed, 96 insertions, 4 deletions
diff --git a/main.c b/main.c
index 20c3b57..4347551 100644
--- a/main.c
+++ b/main.c
@@ -159,10 +159,54 @@ int main(int argc, char **argv) {
while (1) {
draw_map(map, cell_costs, width, height, start_pos, end_pos, NULL, path, visited, NULL);
int c = getch();
+
+ /*
+ * 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)
+ *
+ * [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 */
+ /* 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,
@@ -218,6 +262,12 @@ int main(int argc, char **argv) {
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"); path_func = &dijkstra_path; }
else { set_message("A*"); path_func = &astar_path; };
diff --git a/map.c b/map.c
index 70f4cc0..d0e48a6 100644
--- a/map.c
+++ b/map.c
@@ -524,10 +524,40 @@ void map_editor(Map *map, size_t *width, size_t *height, Position *start, Positi
while (1) {
int ch = getch();
- /* FIXME: move this to the switch statement */
- if (ch == 'q') { clear_message(); visited_free(visited, *height); path_free(path, *height); break; }
- /* Handle keyboard keys */
+ /*
+ * Keybindings:
+ * [k] \
+ * [h] [l] } Move the view
+ * [j] /
+ *
+ * [K] \
+ * [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
+ *
+ * [a] - Toggle pathfinding
+ *
+ * [d] - Switch algorithms (A* or Dijsktra's)
+ *
+ * [s] - Save the map to a plaintext file
+ *
+ * [q] - Exit the map editor
+ *
+ * Mousebindings:
+ * LMB on wall/empty space to toggle them
+ * Drag start/goal with LMB to move them
+ * Drag right/bottom borders with LMB to resize the map
+ * 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 'h': map_offset_x += 2; break;
case 'l': map_offset_x -= 2; break;
case 'j': map_offset_y -= 1; break;
@@ -598,6 +628,18 @@ void map_editor(Map *map, size_t *width, size_t *height, Position *start, Positi
/* Handle mouse */
if (ch == KEY_MOUSE) {
if (getmouse(&event) == OK) {
+ if (event.bstate & BUTTON2_PRESSED) {
+ int init_x = event.x,
+ init_y = event.y,
+ init_map_offset_x = map_offset_x,
+ init_map_offset_y = map_offset_y;
+ while ((ch = getch()) == KEY_MOUSE \
+ && getmouse(&event) == OK && !(event.bstate & BUTTON2_RELEASED)) {
+ map_offset_y = init_map_offset_y + (event.y - init_y);
+ map_offset_x = init_map_offset_x + ((event.x - init_x) / 2 * 2);
+ draw_map(*map, NULL, *width, *height, *start, *goal, NULL, path, visited, NULL);
+ }
+ }
if (event.bstate & BUTTON1_PRESSED) {
/* Translate event coordinates into map coordinates */
size_t row = event.y - map_offset_y,