diff options
Diffstat (limited to 'map.c')
| -rw-r--r-- | map.c | 75 |
1 files changed, 74 insertions, 1 deletions
@@ -8,6 +8,7 @@ #include "stack.h" #include "error.h" #include "path.h" +#include "priority_queue.h" Map empty_map(size_t width, size_t height) { Map map = malloc(sizeof(MapTile*) * height); @@ -22,6 +23,7 @@ Map empty_map(size_t width, size_t height) { return map; } +/* Honestly, what a shitty fucking way to implement this */ unsigned int neighbours_4dir(Position neighbour_array[], Position pos, size_t width, size_t height, \ char visited[height][width]) { size_t cur = 0; @@ -49,6 +51,54 @@ unsigned int neighbours_4dir(Position neighbour_array[], Position pos, size_t wi return cur; } +unsigned int neighbours_8dir(Position neighbour_array[], Position pos, size_t width, size_t height, \ + char visited[height][width]) { + size_t cur = 0; + if (pos.x > 0 && !visited[pos.y][pos.x - 1]) { + neighbour_array[cur].x = pos.x - 1; + neighbour_array[cur].y = pos.y; + cur += 1; + } + if (pos.x + 1 < width && !visited[pos.y][pos.x + 1]) { + neighbour_array[cur].x = pos.x + 1; + neighbour_array[cur].y = pos.y; + cur += 1; + } + if (pos.y > 0 && !visited[pos.y - 1][pos.x]) { + neighbour_array[cur].x = pos.x; + neighbour_array[cur].y = pos.y - 1; + cur += 1; + } + if (pos.y + 1 < height && !visited[pos.y + 1][pos.x]) { + neighbour_array[cur].x = pos.x; + neighbour_array[cur].y = pos.y + 1; + cur += 1; + } + + if (pos.x > 0 && pos.y > 0 && !visited[pos.y - 1][pos.x - 1]) { + neighbour_array[cur].x = pos.x - 1; + neighbour_array[cur].y = pos.y - 1; + cur += 1; + } + if (pos.x + 1 < width && pos.y > 0 && !visited[pos.y - 1][pos.x + 1]) { + neighbour_array[cur].x = pos.x + 1; + neighbour_array[cur].y = pos.y - 1; + cur += 1; + } + if (pos.x + 1 < width && pos.y + 1 < height && !visited[pos.y + 1][pos.x + 1]) { + neighbour_array[cur].x = pos.x + 1; + neighbour_array[cur].y = pos.y + 1; + cur += 1; + } + if (pos.x > 0 && pos.y + 1 < height && !visited[pos.y + 1][pos.x - 1]) { + neighbour_array[cur].x = pos.x - 1; + neighbour_array[cur].y = pos.y + 1; + cur += 1; + } + + return cur; +} + Map rbt_maze_map(size_t width, size_t height, unsigned int seed) { size_t map_width = width * 2 - 1, map_height = height * 2 - 1; @@ -145,7 +195,8 @@ 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, char visited[height][width]) { +/* TODO: so many fucking arguments lmao. Break it down into several functions? */ +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[height][width], PositionPQ *frontier) { (void)path; /* I think it flickers less when we do that */ wnoutrefresh(stdscr); @@ -203,6 +254,20 @@ void draw_map(Map map, size_t width, size_t height, int offset_x, int offset_y, attroff(color_pair); } } + + /* Render the frontier */ + 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, ' '); + 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, ' '); + + attroff(COLOR_PAIR(FRONTIER_COLOR)); + } /* Draw path */ if (path != NULL) { @@ -227,6 +292,14 @@ void draw_map(Map map, size_t width, size_t height, int offset_x, int offset_y, 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); 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); + attroff(COLOR_PAIR(CURSOR_COLOR)); + } attroff(A_BOLD); doupdate(); |
