aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKirill Petrashin <kirill8201@yandex.ru>2026-04-03 17:31:27 +0300
committerKirill Petrashin <kirill8201@yandex.ru>2026-04-03 17:31:27 +0300
commit0dd0adf9547758381573caa6a7d485415fafb33d (patch)
tree06ed17c351d7adc6920d8bed6f3bdf87b8d958bd
parent97decbee0dbc066db7ee94d0988ee26c1f8b35a8 (diff)
downloadastar-0dd0adf9547758381573caa6a7d485415fafb33d.tar.xz
Make map offsets global
-rw-r--r--main.c17
-rw-r--r--map.c69
-rw-r--r--map.h5
-rw-r--r--path.c14
4 files changed, 52 insertions, 53 deletions
diff --git a/main.c b/main.c
index 88059f5..33b3af7 100644
--- a/main.c
+++ b/main.c
@@ -132,11 +132,6 @@ int main(int argc, char **argv) {
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 = path_func(dirs, map, width, height, start_pos, end_pos, visited, anim);
@@ -148,13 +143,13 @@ int main(int argc, char **argv) {
}
while (1) {
- draw_map(map, width, height, offset_x, offset_y, start_pos, end_pos, NULL, path, visited, NULL);
+ draw_map(map, width, height, start_pos, end_pos, NULL, path, visited, NULL);
char c = getch();
switch (c) {
- 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 '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 'a': anim = !anim; break;
case 'y':
case 'o':
@@ -184,7 +179,7 @@ int main(int argc, char **argv) {
case 's':
/* TODO: prompt for filename? or do a define idk */
map_to_bmp(map, width, height, start_pos, end_pos, path, visited, "out.bmp");
- mvprintw(height + offset_y + DRAW_MAP_OFFSET_Y + 1, offset_x, "Saved to out.bmp");
+ mvprintw(height + map_offset_y + DRAW_MAP_OFFSET_Y + 1, map_offset_x, "Saved to out.bmp");
getch();
break;
case 'n':
diff --git a/map.c b/map.c
index 888f2e3..811daac 100644
--- a/map.c
+++ b/map.c
@@ -10,6 +10,9 @@
#include "path.h"
#include "priority_queue.h"
+int map_offset_x = 0;
+int map_offset_y = 0;
+
Map empty_map(size_t width, size_t height) {
Map map = malloc(sizeof(MapTile*) * height);
if (map == NULL) return NULL;
@@ -212,36 +215,36 @@ Map file_plaintext_map(char *filename, size_t *width, size_t *height, Position *
/* TODO: draw the start and goal with no background, allowing us to see the frontier/path/whatever's underneath. I think attr_get() will be useful */
/* TODO: only draw a portion of the map (for bigger ones) */
/* TODO: draw arrow for visited too */
-void draw_map(Map map, size_t width, size_t height, int offset_x, int offset_y, Position start, Position goal, Position *cursor, Path path, char **visited, PositionPQ *frontier) {
+void draw_map(Map map, size_t width, size_t height, Position start, Position goal, Position *cursor, Path path, char **visited, PositionPQ *frontier) {
/* I think it flickers less when we do that */
wnoutrefresh(stdscr);
/* Clean up the area around the map */
for (ssize_t i = -1; i <= (ssize_t)(width*2 + 4); i++) { /* Horizontal */
- mvaddch(DRAW_MAP_OFFSET_Y - 1 + offset_y - 1, i + offset_x, ' ');
- mvaddch(DRAW_MAP_OFFSET_Y + height + offset_y + 1, i + offset_x, ' ');
- mvaddch(DRAW_MAP_OFFSET_Y + height + offset_y + 2, i + offset_x, ' ');
+ mvaddch(DRAW_MAP_OFFSET_Y - 1 + map_offset_y - 1, i + map_offset_x, ' ');
+ mvaddch(DRAW_MAP_OFFSET_Y + height + map_offset_y + 1, i + map_offset_x, ' ');
+ mvaddch(DRAW_MAP_OFFSET_Y + height + map_offset_y + 2, i + map_offset_x, ' ');
}
for (size_t i = 0; i <= height + 2; i++) { /* Vertical */
- mvaddch(i + offset_y, DRAW_MAP_OFFSET_X - 2 + offset_x - 2, ' ');
- mvaddch(i + offset_y, DRAW_MAP_OFFSET_X - 1 + offset_x - 2, ' ');
- mvaddch(i + offset_y, DRAW_MAP_OFFSET_X + width * 2 + offset_x + 2, ' ');
- mvaddch(i + offset_y, DRAW_MAP_OFFSET_X + width * 2 + 1 + offset_x + 2, ' ');
- mvaddch(i + offset_y, DRAW_MAP_OFFSET_X + width * 2 + 2 + offset_x + 2, ' ');
- mvaddch(i + offset_y, DRAW_MAP_OFFSET_X + width * 2 + 3 + offset_x + 2, ' ');
+ mvaddch(i + map_offset_y, DRAW_MAP_OFFSET_X - 2 + map_offset_x - 2, ' ');
+ mvaddch(i + map_offset_y, DRAW_MAP_OFFSET_X - 1 + map_offset_x - 2, ' ');
+ mvaddch(i + map_offset_y, DRAW_MAP_OFFSET_X + width * 2 + map_offset_x + 2, ' ');
+ mvaddch(i + map_offset_y, DRAW_MAP_OFFSET_X + width * 2 + 1 + map_offset_x + 2, ' ');
+ mvaddch(i + map_offset_y, DRAW_MAP_OFFSET_X + width * 2 + 2 + map_offset_x + 2, ' ');
+ mvaddch(i + map_offset_y, DRAW_MAP_OFFSET_X + width * 2 + 3 + map_offset_x + 2, ' ');
}
/* Draw the borders */
attron(COLOR_PAIR(WALL_COLOR));
for (size_t i = 0; i <= width*2 + 3; i++) { /* Horizontal */
- mvaddch(DRAW_MAP_OFFSET_Y - 1 + offset_y, i + offset_x, WALL_CHAR);
- mvaddch(DRAW_MAP_OFFSET_Y + height + offset_y, i + offset_x, WALL_CHAR);
+ mvaddch(DRAW_MAP_OFFSET_Y - 1 + map_offset_y, i + map_offset_x, WALL_CHAR);
+ mvaddch(DRAW_MAP_OFFSET_Y + height + map_offset_y, i + map_offset_x, WALL_CHAR);
}
for (size_t i = 1; i <= height; i++) { /* Vertical */
- mvaddch(i + offset_y, DRAW_MAP_OFFSET_X - 2 + offset_x, WALL_CHAR);
- mvaddch(i + offset_y, DRAW_MAP_OFFSET_X - 1 + offset_x, WALL_CHAR);
- mvaddch(i + offset_y, DRAW_MAP_OFFSET_X + width * 2 + offset_x, WALL_CHAR);
- mvaddch(i + offset_y, DRAW_MAP_OFFSET_X + width * 2 + 1 + offset_x, WALL_CHAR);
+ mvaddch(i + map_offset_y, DRAW_MAP_OFFSET_X - 2 + map_offset_x, WALL_CHAR);
+ mvaddch(i + map_offset_y, DRAW_MAP_OFFSET_X - 1 + map_offset_x, WALL_CHAR);
+ mvaddch(i + map_offset_y, DRAW_MAP_OFFSET_X + width * 2 + map_offset_x, WALL_CHAR);
+ mvaddch(i + map_offset_y, DRAW_MAP_OFFSET_X + width * 2 + 1 + map_offset_x, WALL_CHAR);
}
attroff(COLOR_PAIR(WALL_COLOR));
@@ -268,8 +271,8 @@ void draw_map(Map map, size_t width, size_t height, int offset_x, int offset_y,
attron(color_pair);
/* We draw two characters because they roughly make a square together.
* It looks WAY better if we do this. */
- mvaddch(i + DRAW_MAP_OFFSET_Y + offset_y, j*2 + DRAW_MAP_OFFSET_X + offset_x, c);
- mvaddch(i + DRAW_MAP_OFFSET_Y + offset_y, j*2 + DRAW_MAP_OFFSET_X + 1 + offset_x, c);
+ mvaddch(i + DRAW_MAP_OFFSET_Y + map_offset_y, j*2 + DRAW_MAP_OFFSET_X + map_offset_x, c);
+ mvaddch(i + DRAW_MAP_OFFSET_Y + map_offset_y, j*2 + DRAW_MAP_OFFSET_X + 1 + map_offset_x, c);
attroff(color_pair);
}
}
@@ -277,8 +280,8 @@ void draw_map(Map map, size_t width, size_t height, int offset_x, int offset_y,
for (size_t i = 0; i < height; i++) {
for (size_t j = 0; j < width; j++) {
if (visited != NULL && visited[i][j]) attron(COLOR_PAIR(VISITED_COLOR));
- mvaddch(i + DRAW_MAP_OFFSET_Y + offset_y, j*2 + DRAW_MAP_OFFSET_X + offset_x, ' ');
- mvaddch(i + DRAW_MAP_OFFSET_Y + offset_y, j*2 + DRAW_MAP_OFFSET_X + 1 + offset_x, ' ');
+ mvaddch(i + DRAW_MAP_OFFSET_Y + map_offset_y, j*2 + DRAW_MAP_OFFSET_X + map_offset_x, ' ');
+ mvaddch(i + DRAW_MAP_OFFSET_Y + map_offset_y, j*2 + DRAW_MAP_OFFSET_X + 1 + map_offset_x, ' ');
if (visited != NULL && visited[i][j]) attroff(COLOR_PAIR(VISITED_COLOR));
}
}
@@ -288,12 +291,12 @@ void draw_map(Map map, size_t width, size_t height, int offset_x, int offset_y,
if (frontier != NULL) {
attron(COLOR_PAIR(FRONTIER_COLOR));
while (frontier->next != NULL) {
- mvaddch(frontier->pos.y + DRAW_MAP_OFFSET_Y + offset_y, frontier->pos.x*2 + DRAW_MAP_OFFSET_X + offset_x, ' ');
- mvaddch(frontier->pos.y + DRAW_MAP_OFFSET_Y + offset_y, frontier->pos.x*2 + DRAW_MAP_OFFSET_X + 1 + offset_x, ' ');
+ mvaddch(frontier->pos.y + DRAW_MAP_OFFSET_Y + map_offset_y, frontier->pos.x*2 + DRAW_MAP_OFFSET_X + map_offset_x, ' ');
+ mvaddch(frontier->pos.y + DRAW_MAP_OFFSET_Y + map_offset_y, frontier->pos.x*2 + DRAW_MAP_OFFSET_X + 1 + map_offset_x, ' ');
frontier = frontier->next;
}
- mvaddch(frontier->pos.y + DRAW_MAP_OFFSET_Y + offset_y, frontier->pos.x*2 + DRAW_MAP_OFFSET_X + offset_x, ' ');
- mvaddch(frontier->pos.y + DRAW_MAP_OFFSET_Y + offset_y, frontier->pos.x*2 + DRAW_MAP_OFFSET_X + 1 + offset_x, ' ');
+ mvaddch(frontier->pos.y + DRAW_MAP_OFFSET_Y + map_offset_y, frontier->pos.x*2 + DRAW_MAP_OFFSET_X + map_offset_x, ' ');
+ mvaddch(frontier->pos.y + DRAW_MAP_OFFSET_Y + map_offset_y, frontier->pos.x*2 + DRAW_MAP_OFFSET_X + 1 + map_offset_x, ' ');
attroff(COLOR_PAIR(FRONTIER_COLOR));
}
@@ -329,8 +332,8 @@ void draw_map(Map map, size_t width, size_t height, int offset_x, int offset_y,
c1 = '/'; c2 = '\\';
}
}
- mvaddch(cur.y + DRAW_MAP_OFFSET_Y + offset_y, cur.x*2 + DRAW_MAP_OFFSET_X + offset_x, c1);
- mvaddch(cur.y + DRAW_MAP_OFFSET_Y + offset_y, cur.x*2 + DRAW_MAP_OFFSET_X + 1 + offset_x, c2);
+ mvaddch(cur.y + DRAW_MAP_OFFSET_Y + map_offset_y, cur.x*2 + DRAW_MAP_OFFSET_X + map_offset_x, c1);
+ mvaddch(cur.y + DRAW_MAP_OFFSET_Y + map_offset_y, cur.x*2 + DRAW_MAP_OFFSET_X + 1 + map_offset_x, c2);
if (cur.x - path[cur.y][cur.x].parent.x == 0 || cur.y - path[cur.y][cur.x].parent.y == 0) {
length += COST_ORTHOGONAL;
} else {
@@ -340,27 +343,27 @@ void draw_map(Map map, size_t width, size_t height, int offset_x, int offset_y,
cur = path[cur.y][cur.x].parent;
}
attroff(COLOR_PAIR(PATH_COLOR));
- mvprintw(height + offset_y + DRAW_MAP_OFFSET_Y + 1, offset_x, "Path cost: %zu", length);
+ mvprintw(height + map_offset_y + DRAW_MAP_OFFSET_Y + 1, map_offset_x, "Path cost: %zu", length);
}
/* Draw the start */
attron(A_BOLD);
attron(COLOR_PAIR(START_COLOR));
- mvaddch(start.y + DRAW_MAP_OFFSET_Y + offset_y, start.x*2 + DRAW_MAP_OFFSET_X + offset_x, START_CHAR_1);
- mvaddch(start.y + DRAW_MAP_OFFSET_Y + offset_y, start.x*2 + DRAW_MAP_OFFSET_X + 1 + offset_x, START_CHAR_2);
+ mvaddch(start.y + DRAW_MAP_OFFSET_Y + map_offset_y, start.x*2 + DRAW_MAP_OFFSET_X + map_offset_x, START_CHAR_1);
+ mvaddch(start.y + DRAW_MAP_OFFSET_Y + map_offset_y, start.x*2 + DRAW_MAP_OFFSET_X + 1 + map_offset_x, START_CHAR_2);
attroff(COLOR_PAIR(START_COLOR));
/* Draw the goal */
attron(COLOR_PAIR(GOAL_COLOR));
- mvaddch(goal.y + DRAW_MAP_OFFSET_Y + offset_y, goal.x*2 + DRAW_MAP_OFFSET_X + offset_x, GOAL_CHAR_1);
- mvaddch(goal.y + DRAW_MAP_OFFSET_Y + offset_y, goal.x*2 + DRAW_MAP_OFFSET_X + 1 + offset_x, GOAL_CHAR_2);
+ mvaddch(goal.y + DRAW_MAP_OFFSET_Y + map_offset_y, goal.x*2 + DRAW_MAP_OFFSET_X + map_offset_x, GOAL_CHAR_1);
+ mvaddch(goal.y + DRAW_MAP_OFFSET_Y + map_offset_y, goal.x*2 + DRAW_MAP_OFFSET_X + 1 + map_offset_x, GOAL_CHAR_2);
attroff(COLOR_PAIR(GOAL_COLOR));
/* Draw the cursor */
if (cursor != NULL) {
attron(COLOR_PAIR(CURSOR_COLOR));
- mvaddch(cursor->y + DRAW_MAP_OFFSET_Y + offset_y, cursor->x*2 + DRAW_MAP_OFFSET_X + offset_x, CURSOR_CHAR_1);
- mvaddch(cursor->y + DRAW_MAP_OFFSET_Y + offset_y, cursor->x*2 + DRAW_MAP_OFFSET_X + 1 + offset_x, CURSOR_CHAR_2);
+ mvaddch(cursor->y + DRAW_MAP_OFFSET_Y + map_offset_y, cursor->x*2 + DRAW_MAP_OFFSET_X + map_offset_x, CURSOR_CHAR_1);
+ mvaddch(cursor->y + DRAW_MAP_OFFSET_Y + map_offset_y, cursor->x*2 + DRAW_MAP_OFFSET_X + 1 + map_offset_x, CURSOR_CHAR_2);
attroff(COLOR_PAIR(CURSOR_COLOR));
}
attroff(A_BOLD);
diff --git a/map.h b/map.h
index 8f0dcab..eb8ea9b 100644
--- a/map.h
+++ b/map.h
@@ -6,6 +6,9 @@
#include "path.h"
#include "priority_queue.h"
+extern int map_offset_x;
+extern int map_offset_y;
+
/* Returns an empty map of given size */
Map empty_map(size_t width, size_t height);
@@ -41,7 +44,7 @@ Map file_plaintext_map(char *filename, size_t *width, size_t *height, Position *
/* Draw the map. Bet you didn't expect that.
* path could be NULL to draw a map with no path. So can cursor, frontier and visited */
-void draw_map(Map map, size_t width, size_t height, int offset_x, int offset_y, Position start, Position goal, Position *cursor, Path path, char **visited, PositionPQ *frontier);
+void draw_map(Map map, size_t width, size_t height, Position start, Position goal, Position *cursor, Path path, char **visited, PositionPQ *frontier);
/* Frees all the memory reserved for the map */
void map_free(Map map, size_t height);
diff --git a/path.c b/path.c
index 2547fd3..589edd6 100644
--- a/path.c
+++ b/path.c
@@ -12,21 +12,19 @@
#include "error.h"
#include "config.h"
-/* TODO: somehow get offsets back to main */
/* TODO: make it move the map maybe to show the path */
/* TODO: figure out input when automatic = 1 */
int anim(Map map, size_t width, size_t height, Position start, Position end, Position *cur, char **visited, PositionPQ *frontier) {
- static int offset_y = 0, offset_x = 0;
static char automatic = 0;
while (1) {
- draw_map(map, width, height, offset_x, offset_y, start, end, cur, NULL, visited, frontier);
- mvprintw(height+2 + offset_y, offset_x, "cur: %zu %zu", cur->x, cur->y);
+ draw_map(map, width, height, start, end, cur, NULL, visited, frontier);
+ mvprintw(height+2 + map_offset_y, map_offset_x, "cur: %zu %zu", cur->x, cur->y);
if (automatic) { usleep(ANIM_DELAY_USEC); return 0; }
switch (getch()) {
- 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 '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 'a': automatic = 1; break;
case 'q': return -1;
default: return 0;