summaryrefslogtreecommitdiffstats
path: root/src/maze.c
blob: 92c1410cbbc8357ece455c3542688a97f5b0757d (plain)
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/*
 * Copyright (C) 2021  P. J. McDermott
 *
 * This file is part of Maze Fight
 *
 * Maze Fight is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Maze Fight is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Maze Fight.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <SDL.h>
#include <stdlib.h>
#include "maze.h"

struct mf_maze {
	int           w;
	int           h;
	int           show_all;
	unsigned int *walls;
	unsigned int *show;
	unsigned int *visited;
};

/*
 * Black magic follows
 */

void
mf_maze_rm_wall(struct mf_maze *m, int x, int y, int dx, int dy)
{
	int i;

	if (x + dx < 0 || x + dx >= m->w || y + dy < 0 || y + dy >= m->h) {
		return;
	}

	if (dx < 0) --x;
	if (dy < 0) --y;
	i = y * m->w + x;
	i *= 2;
	if (dx == 0) ++i;  /* Moving vertically */

	m->walls[i / (sizeof(*m->walls)*8)] |= 1 << (i % (sizeof(*m->walls)*8));
}

int
mf_maze_is_wall(struct mf_maze *m, int x, int y, int dx, int dy)
{
	int i;

	if (x + dx < 0 || x + dx >= m->w || y + dy < 0 || y + dy >= m->h) {
		return 1;
	}

	if (dx < 0) --x;
	if (dy < 0) --y;
	i = y * m->w + x;
	i *= 2;
	if (dx == 0) ++i;  /* Moving vertically */

	return !(m->walls[i / (sizeof(*m->walls)*8)]
		& 1 << (i % (sizeof(*m->walls)*8)));
}

void
mf_maze_reveal_wall(struct mf_maze *m, int x, int y, int dx, int dy)
{
	int i;

	if (x + dx < 0 || x + dx >= m->w || y + dy < 0 || y + dy >= m->h) {
		return;
	}

	if (dx < 0) --x;
	if (dy < 0) --y;
	i = y * m->w + x;
	i *= 2;
	if (dx == 0) ++i;  /* Moving vertically */

	m->show[i / (sizeof(*m->show)*8)] |= 1 << (i % (sizeof(*m->show)*8));
}

static int
_mf_maze_revealed_wall(struct mf_maze *m, int x, int y, int dx, int dy)
{
	int i;

	if (m->show_all) {
		return 1;
	}

	if (x + dx < 0 || x + dx >= m->w || y + dy < 0 || y + dy >= m->h) {
		return 1;
	}

	if (dx < 0) --x;
	if (dy < 0) --y;
	i = y * m->w + x;
	i *= 2;
	if (dx == 0) ++i;  /* Moving vertically */

	return m->show[i / (sizeof(*m->show)*8)]
		& 1 << (i % (sizeof(*m->show)*8));
}

static void
_mf_maze_visit(struct mf_maze *m, int x, int y)
{
	int i;

	++x, ++y;  /* Coordinates start from -1 (edges) */
	i = y * (m->w+2) + x;
	m->visited[i / (sizeof(*m->visited)*8)] |=
		1 << (i % (sizeof(*m->visited)*8));
}

static int
_mf_maze_is_visited(struct mf_maze *m, int x, int y)
{
	int i;

	++x, ++y;  /* Coordinates start from -1 (edges) */
	i = y * (m->w+2) + x;
	return m->visited[i / (sizeof(*m->visited)*8)]
		& 1 << (i % (sizeof(*m->visited)*8));
}

/*
 * End of black magic
 */

static void
_mf_maze_carve(struct mf_maze *m, int x, int y)
{
	int dirs[4][2] = {{0, -1}, {0, 1}, {1, 0}, {-1, 0}};
	int i;
	int j;
	int t_x;
	int t_y;

	/* Shuffle directions */
	for (i = 0; i < 4; ++i) {
		j = rand() / (RAND_MAX / (i+1));
		t_x        = dirs[i][0];
		t_y        = dirs[i][1];
		dirs[i][0] = dirs[j][0];
		dirs[i][1] = dirs[j][1];
		dirs[j][0] = t_x;
		dirs[j][1] = t_y;
	}

	/* You are here */
	_mf_maze_visit(m, x, y);

	/* Carve passages */
	for (i = 0; i < 4; ++i) {
		if (!_mf_maze_is_visited(m, x + dirs[i][0], y + dirs[i][1])) {
			mf_maze_rm_wall(m, x, y, dirs[i][0], dirs[i][1]);
			_mf_maze_carve(m, x + dirs[i][0], y + dirs[i][1]);
		}
	}
}

struct mf_maze *
mf_maze_new(int s, int w, int h, int show_all)
{
	struct mf_maze *m;
	int             x;
	int             y;

	/* Allocate everything */
	m = calloc(1, sizeof(*m));
	if (m == NULL) {
		return NULL;
	}
	m->walls = calloc(
			(w * h * 2 + sizeof(*m->walls)*8 - 1) /
				(sizeof(*m->walls)*8),
			sizeof(*m->walls));
	if (m->walls == NULL) {
		free(m);
		return NULL;
	}
	m->show = calloc(
			(w * h * 2 + sizeof(*m->show)*8 - 1) /
				(sizeof(*m->show)*8),
			sizeof(*m->show));
	if (m->show == NULL) {
		free(m->walls);
		free(m);
		return NULL;
	}
	m->visited = calloc(
			((w+2) * (h+2) + sizeof(*m->visited)*8 - 1) /
				(sizeof(*m->visited)*8),
			sizeof(*m->visited));
	if (m->visited == NULL) {
		free(m->show);
		free(m->walls);
		free(m);
		return NULL;
	}

	/* Initialize parameters */
	m->w        = w;
	m->h        = h;
	m->show_all = show_all;

	/* Mark edges as visited */
	for (x = -1; x <= w; ++x) {
		_mf_maze_visit(m, x, -1);  /* Top */
		_mf_maze_visit(m, x,  h);  /* Bottom */
	}
	for (y = 0; y < h; ++y) {
		_mf_maze_visit(m, -1, y);  /* Left */
		_mf_maze_visit(m,  w, y);  /* Right */
	}

	/* Carve passages */
	srand(s);
	_mf_maze_carve(m, 0, 0);

	free(m->visited);
	return m;
}

int
mf_maze_render(struct mf_maze *m, SDL_Renderer *renderer, SDL_Color *color,
		int cw)
{
	int e;
	int x;
	int y;

	e = 0;

	if (SDL_SetRenderDrawColor(renderer,
				color->r, color->g, color->b, color->a) < 0) {
		SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
				"Couldn't set drawing color: %s",
				SDL_GetError());
		e = -1;
	}

	x = m->w * cw - 1;
	y = m->h * cw - 1;
	if (
			SDL_RenderDrawLine(renderer, 0, 0, x, 0) < 0 ||
			SDL_RenderDrawLine(renderer, 0, y, x, y) < 0 ||
			SDL_RenderDrawLine(renderer, 0, 0, 0, y) < 0 ||
			SDL_RenderDrawLine(renderer, x, 0, x, y) < 0) {
		SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
				"Couldn't draw line: %s", SDL_GetError());
		e = -1;
	}

	for (y = 0; y < m->h; ++y) {
		for (x = 0; x < m->w; ++x) {
			if (y < m->h - 1 && mf_maze_is_wall(m, x, y, 0, 1) &&
					_mf_maze_revealed_wall(m, x, y, 0, 1)) {
				/* Draw h line */
				if (SDL_RenderDrawLine(renderer,
							(x  ) * cw, (y+1) * cw,
							(x+1) * cw, (y+1) * cw)
						< 0) {
					SDL_LogError(
						SDL_LOG_CATEGORY_APPLICATION,
						"Couldn't draw line: %s",
						SDL_GetError());
					e = -1;
				}
			}
			if (x < m->w - 1 && mf_maze_is_wall(m, x, y, 1, 0) &&
					_mf_maze_revealed_wall(m, x, y, 1, 0)) {
				/* Draw v line */
				if (SDL_RenderDrawLine(renderer,
							(x+1) * cw, (y  ) * cw,
							(x+1) * cw, (y+1) * cw)
						< 0) {
					SDL_LogError(
						SDL_LOG_CATEGORY_APPLICATION,
						"Couldn't draw line: %s",
						SDL_GetError());
					e = -1;
				}
			}
		}
	}

	return e;
}

void
mf_maze_destroy(struct mf_maze **m_p)
{
	struct mf_maze *m;

	if (m_p == NULL || *m_p == NULL) {
		return;
	}
	m = *m_p;

	if (m->walls != NULL) {
		free(m->walls);
	}
	if (m->show != NULL) {
		free(m->show);
	}
	free(m);
	m = NULL;
}