diff options
| author | Kirill Petrashin <kirill8201@yandex.ru> | 2026-03-22 11:25:36 +0300 |
|---|---|---|
| committer | Kirill Petrashin <kirill8201@yandex.ru> | 2026-03-22 11:25:36 +0300 |
| commit | b0b6aecd5cb3a9a02376ec071699f827feee6934 (patch) | |
| tree | cc4463ef81a5f376ca3cc1b4164f5d0a6aa71303 | |
| parent | 596eeed9dd378a8994778e03319b538206672bec (diff) | |
Add rendering of visited tiles
| -rw-r--r-- | main.c | 17 | ||||
| -rw-r--r-- | map.c | 7 | ||||
| -rw-r--r-- | map.h | 2 | ||||
| -rw-r--r-- | path.c | 9 | ||||
| -rw-r--r-- | path.h | 2 | ||||
| -rw-r--r-- | structs.h | 9 |
6 files changed, 27 insertions, 19 deletions
@@ -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; } @@ -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: @@ -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); @@ -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); @@ -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); @@ -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. |
