aboutsummaryrefslogtreecommitdiff
path: root/map.c
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 /map.c
parent97decbee0dbc066db7ee94d0988ee26c1f8b35a8 (diff)
downloadastar-0dd0adf9547758381573caa6a7d485415fafb33d.tar.xz
Make map offsets global
Diffstat (limited to 'map.c')
-rw-r--r--map.c69
1 files changed, 36 insertions, 33 deletions
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);