aboutsummaryrefslogtreecommitdiff
path: root/map.c
blob: a69affaea143df1e4d65cbca7abbe032bd8b48a3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#include <string.h>
#include <stdlib.h>
#include <curses.h>
#include <limits.h>
#include "map.h"
#include "config.h"
#include "stack.h"

Map empty_map(size_t width, size_t height) {
    Map map = malloc(sizeof(MapTile*) * height);
    if (map == NULL) return NULL;

    for (size_t row = 0; row < height; row++) {
        map[row] = malloc(sizeof(MapTile) * width);
        if (map[row] == NULL) return NULL;
        memset(map[row], 0, width*sizeof(MapTile)); 
    }

    return map;
}

unsigned int neighbours(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;
    }

    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;
    Map map = empty_map(map_width, map_height);

    /* Vertical walls */
    for (size_t i = 1; i < map_width; i += 2) {
        for (size_t j = 0; j < map_height; j += 1) {
            map[j][i] = WALL;
        }
    }
    /* Horizontal walls */
    for (size_t i = 1; i < map_height; i += 2) {
        for (size_t j = 0; j < map_width; j += 1) {
            map[i][j] = WALL;
        }
    }

    Position beginning_cell = {width - 1, height - 1};

    char visited[height][width]; /* 1 if visited, 0 if not */
    memset(visited, 0, sizeof(char) * width * height);

    PositionStack ps = ps_new();
    ps_push(&ps, beginning_cell);
    visited[beginning_cell.y][beginning_cell.x] = 1;

    Position na[4];

    srand(seed);

    do {
        int nc = neighbours(na, ps_peek(ps), width, height, visited);
        if (nc > 0) {
            Position next = na[rand() % nc];
            Position prev = ps_peek(ps);
            ps_push(&ps, next);
            visited[next.y][next.x] = 1;
            map[(next.y * 2 + prev.y * 2) / 2][(next.x * 2 + prev.x * 2) / 2] = EMPTY;
        } else {
            (void)ps_pop(&ps);
        }
    } while (ps_peek(ps).x != beginning_cell.x || ps_peek(ps).y != beginning_cell.y) ;

    return map;
}

/* 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) {
    /* 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, ' ');
    }
    for (size_t i = 0; i <= height + 2; i++) { /* Horizontal */
        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, ' ');
    }

    /* 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);
    }
    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);
    }
    attroff(COLOR_PAIR(WALL_COLOR));

    /* Draw field */
    char c; /* The char for the current tile */
    for (size_t i = 0; i < height; i++) {
        for (size_t j = 0; j < width; j++) {
            int color_pair = 0; /* The color pair of the current char  */
            switch (map[i][j]) {
                case EMPTY: 
                    color_pair = COLOR_PAIR(EMPTY_COLOR);
                    c = EMPTY_CHAR; 
                    break;
                case WALL: 
                    color_pair = COLOR_PAIR(WALL_COLOR);
                    c = WALL_CHAR; 
                    break;

            }
            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);
            attroff(color_pair);
        }
    }

    /* 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);
    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);
    attroff(COLOR_PAIR(GOAL_COLOR));
    attroff(A_BOLD);

    doupdate();
}