From b0b6aecd5cb3a9a02376ec071699f827feee6934 Mon Sep 17 00:00:00 2001 From: Kirill Petrashin Date: Sun, 22 Mar 2026 11:25:36 +0300 Subject: Add rendering of visited tiles --- main.c | 17 +++++++++-------- map.c | 7 +++++-- map.h | 2 +- path.c | 9 ++++++--- path.h | 2 +- structs.h | 9 +++++---- 6 files changed, 27 insertions(+), 19 deletions(-) diff --git a/main.c b/main.c index 747254d..3b96f52 100644 --- a/main.c +++ b/main.c @@ -32,9 +32,9 @@ 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); /* Using white as bg makes - them seem solid */ + init_pair(WALL_COLOR, COLOR_WHITE, COLOR_WHITE); init_pair(START_COLOR, -1, COLOR_RED); init_pair(PATH_COLOR, COLOR_RED, COLOR_RED); } @@ -52,8 +52,8 @@ int main(void) { init_ncurses(); - size_t mwidth = 20; - size_t mheight = 10; + size_t mwidth = 20; /* Maze width */ + size_t mheight = 10; /* Maze height */ Map map = rbt_maze_map(mwidth, mheight, (unsigned int) time(NULL)); Position start_pos = {0, 0}; Position end_pos = {mwidth*2-2, mheight*2-2}; @@ -65,12 +65,13 @@ int main(void) { //Position start_pos, end_pos; //Map map = file_plaintext_map("maps/test", &width, &height, &start_pos, &end_pos); //print_map_out(map, width, height); - draw_map(map, width, height, offset_x, offset_y, start_pos, end_pos, NULL); + //draw_map(map, width, height, offset_x, offset_y, start_pos, end_pos, NULL); - Path path = breadth_first_search_path_4dir(map, width, height, start_pos, end_pos); + char visited[height][width]; + Path path = breadth_first_search_path_4dir(map, width, height, start_pos, end_pos, visited); while (1) { - draw_map(map, width, height, offset_x, offset_y, start_pos, end_pos, path); + draw_map(map, width, height, offset_x, offset_y, start_pos, end_pos, path, visited); char c = getch(); switch (c) { case 'h': offset_x -= 2; break; @@ -80,7 +81,7 @@ int main(void) { case 'n': //FIXME: free it all before generating a new one map = rbt_maze_map(mwidth, mheight, (unsigned int) time(NULL)); - path = breadth_first_search_path_4dir(map, width, height, start_pos, end_pos); + path = breadth_first_search_path_4dir(map, width, height, start_pos, end_pos, visited); break; case 'q': endwin(); return 0; } diff --git a/map.c b/map.c index 0c0bf86..3d89efc 100644 --- a/map.c +++ b/map.c @@ -145,7 +145,7 @@ Map file_plaintext_map(char *filename, size_t *width, size_t *height, Position * /* FIXME: I don't think we need DRAW_MAP_OFFSET_{X,Y} anymore * Although, at the same time, it does make less magic values, so I guess it's fine */ /* TODO: Maybe add an option to render with ▄? that doubles the resolution, but requires us to use ncursesw, probably. */ -void draw_map(Map map, size_t width, size_t height, int offset_x, int offset_y, Position start, Position goal, Path path) { +void draw_map(Map map, size_t width, size_t height, int offset_x, int offset_y, Position start, Position goal, Path path, char visited[height][width]) { (void)path; /* I think it flickers less when we do that */ wnoutrefresh(stdscr); @@ -183,7 +183,10 @@ void draw_map(Map map, size_t width, size_t height, int offset_x, int offset_y, int color_pair = 0; /* The color pair of the current char */ switch (map[i][j]) { case EMPTY: - color_pair = COLOR_PAIR(EMPTY_COLOR); + if (visited[i][j]) + color_pair = COLOR_PAIR(VISITED_COLOR); + else + color_pair = COLOR_PAIR(EMPTY_COLOR); c = EMPTY_CHAR; break; case WALL: diff --git a/map.h b/map.h index 7e17479..c580cb3 100644 --- a/map.h +++ b/map.h @@ -36,7 +36,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 */ -void draw_map(Map map, size_t width, size_t height, int offset_x, int offset_y, Position start, Position goal, Path path); +void draw_map(Map map, size_t width, size_t height, int offset_x, int offset_y, Position start, Position goal, Path path, char visited[height][width]); void print_map_out(Map map, size_t width, size_t height); diff --git a/path.c b/path.c index 19fdf0b..346f737 100644 --- a/path.c +++ b/path.c @@ -8,8 +8,7 @@ #include "priority_queue.h" /* BLOODY FUCK IT WORKS */ -Path breadth_first_search_path_4dir(Map map, size_t width, size_t height, Position start, Position end) { - (void) map, (void)end; +Path breadth_first_search_path_4dir(Map map, size_t width, size_t height, Position start, Position end, char visited[height][width]) { Path path = malloc(sizeof(PathNode)*height); if (path == NULL) return NULL; @@ -20,13 +19,17 @@ Path breadth_first_search_path_4dir(Map map, size_t width, size_t height, Positi } PositionPQ *frontier = ppq_new(start, 0); - char visited[height][width]; + memset(visited, 0, height * width * sizeof(char)); while (frontier != NULL) { Position cur = ppq_pop(&frontier); visited[cur.y][cur.x] = 1; + if (cur.x == end.x && cur.y == end.y) { + break; + } + Position na[4]; unsigned int nc = neighbours(na, cur, width, height, visited); diff --git a/path.h b/path.h index 9de376e..5b7392b 100644 --- a/path.h +++ b/path.h @@ -4,7 +4,7 @@ #include "structs.h" #include "map.h" -Path breadth_first_search_path_4dir(Map map, size_t width, size_t height, Position start, Position end); +Path breadth_first_search_path_4dir(Map map, size_t width, size_t height, Position start, Position end, char visited[height][width]); Path astar_path_4dir(Map map, size_t width, size_t height, Position start, Position end); size_t manhattan_distance(Position a, Position b); diff --git a/structs.h b/structs.h index 8e5f28e..2c461fc 100644 --- a/structs.h +++ b/structs.h @@ -18,10 +18,11 @@ typedef enum MapTile_e MapTile; enum Colors_e { EMPTY_COLOR = 1, - GOAL_COLOR = 2, - WALL_COLOR = 3, - START_COLOR = 4, - PATH_COLOR = 5, + VISITED_COLOR = 2, + GOAL_COLOR = 3, + WALL_COLOR = 4, + START_COLOR = 5, + PATH_COLOR = 6, }; /* A map is a 2D array of MapTiles. -- cgit v1.2.3