… | |
… | |
13 | |
13 | |
14 | /* we need to maintain a list of wall points to generate |
14 | /* we need to maintain a list of wall points to generate |
15 | reasonable mazes: a straightforward recursive random walk maze |
15 | reasonable mazes: a straightforward recursive random walk maze |
16 | generator would generate a map with a trivial circle-the-outer-wall solution */ |
16 | generator would generate a map with a trivial circle-the-outer-wall solution */ |
17 | |
17 | |
18 | #include <stdio.h> |
|
|
19 | #include <global.h> |
18 | #include <global.h> |
20 | |
19 | |
21 | /*#include <random_map.h>*/ |
20 | #include "random_map.h" |
22 | #include <maze_gen.h> |
21 | #include "rproto.h" |
23 | #include <time.h> |
|
|
24 | |
22 | |
25 | /* global variables that everyone needs: don't want to pass them in |
23 | /* global variables that everyone needs: don't want to pass them in |
26 | as parameters every time. */ |
24 | as parameters every time. */ |
27 | int *wall_x_list = 0; |
25 | int *wall_x_list = 0; |
28 | int *wall_y_list = 0; |
26 | int *wall_y_list = 0; |
… | |
… | |
34 | int wall_chance; |
32 | int wall_chance; |
35 | |
33 | |
36 | /* the outsize interface routine: accepts sizes, returns a char |
34 | /* the outsize interface routine: accepts sizes, returns a char |
37 | ** maze. option is a flag for either a sparse or a full maze. Sparse |
35 | ** maze. option is a flag for either a sparse or a full maze. Sparse |
38 | mazes have sizable rooms. option = 1, full, 0, sparse.*/ |
36 | mazes have sizable rooms. option = 1, full, 0, sparse.*/ |
39 | |
37 | void |
40 | char ** |
38 | maze_gen (Maze maze, int option) |
41 | maze_gen (int xsize, int ysize, int option) |
|
|
42 | { |
39 | { |
43 | int i, j; |
40 | maze->clear (); |
44 | |
41 | maze->border (); |
45 | /* allocate that array, set it up */ |
|
|
46 | char **maze = (char **) calloc (sizeof (char *), xsize); |
|
|
47 | |
|
|
48 | for (i = 0; i < xsize; i++) |
|
|
49 | { |
|
|
50 | maze[i] = (char *) calloc (sizeof (char), ysize); |
|
|
51 | } |
|
|
52 | |
|
|
53 | /* write the outer walls */ |
|
|
54 | for (i = 0; i < xsize; i++) |
|
|
55 | maze[i][0] = maze[i][ysize - 1] = '#'; |
|
|
56 | for (j = 0; j < ysize; j++) |
|
|
57 | maze[0][j] = maze[xsize - 1][j] = '#'; |
|
|
58 | |
|
|
59 | |
42 | |
60 | /* find how many free wall spots there are */ |
43 | /* find how many free wall spots there are */ |
61 | wall_free_size = 2 * (xsize - 4) + 2 * (ysize - 4); |
44 | wall_free_size = 2 * (maze->w - 4) + 2 * (maze->h - 4); |
62 | |
45 | |
63 | make_wall_free_list (xsize, ysize); |
46 | make_wall_free_list (maze->w, maze->h); |
64 | |
47 | |
65 | /* return the empty maze */ |
48 | /* return the empty maze */ |
66 | if (wall_free_size <= 0) |
49 | if (wall_free_size <= 0) |
67 | return maze; |
50 | return; |
68 | |
51 | |
69 | /* recursively generate the walls of the maze */ |
52 | /* recursively generate the walls of the maze */ |
70 | /* first pop a random starting point */ |
53 | /* first pop a random starting point */ |
71 | while (wall_free_size > 0) |
54 | while (wall_free_size > 0) |
72 | { |
55 | { |
|
|
56 | int i, j; |
|
|
57 | |
73 | pop_wall_point (&i, &j); |
58 | pop_wall_point (&i, &j); |
|
|
59 | |
74 | if (option) |
60 | if (option) |
75 | fill_maze_full (maze, i, j, xsize, ysize); |
61 | fill_maze_full (maze, i, j, maze->w, maze->h); |
76 | else |
62 | else |
77 | fill_maze_sparse (maze, i, j, xsize, ysize); |
63 | fill_maze_sparse (maze, i, j, maze->w, maze->h); |
78 | } |
64 | } |
79 | |
65 | |
80 | /* clean up our intermediate data structures. */ |
66 | /* clean up our intermediate data structures. */ |
81 | |
67 | |
82 | free (wall_x_list); |
68 | free (wall_x_list); |
83 | free (wall_y_list); |
69 | free (wall_y_list); |
84 | |
|
|
85 | return maze; |
|
|
86 | } |
70 | } |
87 | |
|
|
88 | |
|
|
89 | |
71 | |
90 | /* the free wall points are those outer points which aren't corners or |
72 | /* the free wall points are those outer points which aren't corners or |
91 | near corners, and don't have a maze wall growing out of them already. */ |
73 | near corners, and don't have a maze wall growing out of them already. */ |
92 | |
|
|
93 | void |
74 | void |
94 | make_wall_free_list (int xsize, int ysize) |
75 | make_wall_free_list (int xsize, int ysize) |
95 | { |
76 | { |
96 | int i, j, count; |
77 | int i, j, count; |
97 | |
78 | |
98 | count = 0; /* entries already placed in the free list */ |
79 | count = 0; /* entries already placed in the free list */ |
99 | /*allocate it */ |
80 | /*allocate it */ |
100 | if (wall_free_size < 0) |
81 | if (wall_free_size < 0) |
101 | return; |
82 | return; |
|
|
83 | |
102 | wall_x_list = (int *) calloc (sizeof (int), wall_free_size); |
84 | wall_x_list = (int *)calloc (sizeof (int), wall_free_size); |
103 | wall_y_list = (int *) calloc (sizeof (int), wall_free_size); |
85 | wall_y_list = (int *)calloc (sizeof (int), wall_free_size); |
104 | |
|
|
105 | |
86 | |
106 | /* top and bottom wall */ |
87 | /* top and bottom wall */ |
107 | for (i = 2; i < xsize - 2; i++) |
88 | for (i = 2; i < xsize - 2; i++) |
108 | { |
89 | { |
109 | wall_x_list[count] = i; |
90 | wall_x_list[count] = i; |
… | |
… | |
124 | wall_y_list[count] = j; |
105 | wall_y_list[count] = j; |
125 | count++; |
106 | count++; |
126 | } |
107 | } |
127 | } |
108 | } |
128 | |
109 | |
129 | |
|
|
130 | |
|
|
131 | /* randomly returns one of the elements from the wall point list */ |
110 | /* randomly returns one of the elements from the wall point list */ |
132 | |
|
|
133 | void |
111 | void |
134 | pop_wall_point (int *x, int *y) |
112 | pop_wall_point (int *x, int *y) |
135 | { |
113 | { |
136 | int index = rndm (wall_free_size); |
114 | int index = rndm (wall_free_size); |
137 | |
115 | |
… | |
… | |
141 | wall_x_list[index] = wall_x_list[wall_free_size - 1]; |
119 | wall_x_list[index] = wall_x_list[wall_free_size - 1]; |
142 | wall_y_list[index] = wall_y_list[wall_free_size - 1]; |
120 | wall_y_list[index] = wall_y_list[wall_free_size - 1]; |
143 | wall_free_size--; |
121 | wall_free_size--; |
144 | } |
122 | } |
145 | |
123 | |
146 | |
|
|
147 | |
|
|
148 | /* find free point: randomly look for a square adjacent to this one where |
124 | /* find free point: randomly look for a square adjacent to this one where |
149 | we can place a new block without closing a path. We may only look |
125 | we can place a new block without closing a path. We may only look |
150 | up, down, right, or left. */ |
126 | up, down, right, or left. */ |
151 | |
|
|
152 | int |
127 | int |
153 | find_free_point (char **maze, int *x, int *y, int xc, int yc, int xsize, int ysize) |
128 | find_free_point (char **maze, int *x, int *y, int xc, int yc, int xsize, int ysize) |
154 | { |
129 | { |
155 | |
130 | |
156 | /* we will randomly pick from this list, 1=up,2=down,3=right,4=left */ |
131 | /* we will randomly pick from this list, 1=up,2=down,3=right,4=left */ |
… | |
… | |
169 | dirlist[count] = 1; |
144 | dirlist[count] = 1; |
170 | count++; |
145 | count++; |
171 | } |
146 | } |
172 | } |
147 | } |
173 | |
148 | |
174 | |
|
|
175 | /* look down */ |
149 | /* look down */ |
176 | if (yc > 2 && xc > 2 && xc < xsize - 2) /* it is valid to look down */ |
150 | if (yc > 2 && xc > 2 && xc < xsize - 2) /* it is valid to look down */ |
177 | { |
151 | { |
178 | int cleartest = (int) maze[xc][yc - 1] + (int) maze[xc - 1][yc - 1] + (int) maze[xc + 1][yc - 1]; |
152 | int cleartest = (int) maze[xc][yc - 1] + (int) maze[xc - 1][yc - 1] + (int) maze[xc + 1][yc - 1]; |
179 | |
153 | |
… | |
… | |
184 | dirlist[count] = 2; |
158 | dirlist[count] = 2; |
185 | count++; |
159 | count++; |
186 | } |
160 | } |
187 | } |
161 | } |
188 | |
162 | |
189 | |
|
|
190 | /* look right */ |
163 | /* look right */ |
191 | if (xc < xsize - 2 && yc > 2 && yc < ysize - 2) /* it is valid to look left */ |
164 | if (xc < xsize - 2 && yc > 2 && yc < ysize - 2) /* it is valid to look left */ |
192 | { |
165 | { |
193 | int cleartest = (int) maze[xc + 1][yc] + (int) maze[xc + 1][yc - 1] + (int) maze[xc + 1][yc + 1]; |
166 | int cleartest = (int) maze[xc + 1][yc] + (int) maze[xc + 1][yc - 1] + (int) maze[xc + 1][yc + 1]; |
194 | |
167 | |
… | |
… | |
198 | { |
171 | { |
199 | dirlist[count] = 3; |
172 | dirlist[count] = 3; |
200 | count++; |
173 | count++; |
201 | } |
174 | } |
202 | } |
175 | } |
203 | |
|
|
204 | |
176 | |
205 | /* look left */ |
177 | /* look left */ |
206 | if (xc > 2 && yc > 2 && yc < ysize - 2) /* it is valid to look down */ |
178 | if (xc > 2 && yc > 2 && yc < ysize - 2) /* it is valid to look down */ |
207 | { |
179 | { |
208 | int cleartest = (int) maze[xc - 1][yc] + (int) maze[xc - 1][yc - 1] + (int) maze[xc - 1][yc + 1]; |
180 | int cleartest = (int) maze[xc - 1][yc] + (int) maze[xc - 1][yc - 1] + (int) maze[xc - 1][yc + 1]; |
… | |
… | |
250 | *x = xc - 1; |
222 | *x = xc - 1; |
251 | *y = yc; |
223 | *y = yc; |
252 | break; |
224 | break; |
253 | } |
225 | } |
254 | default: /* ??? */ |
226 | default: /* ??? */ |
255 | { |
|
|
256 | return -1; |
227 | return -1; |
257 | } |
228 | |
258 | } |
229 | } |
259 | return 1; |
230 | return 1; |
260 | } |
231 | } |
261 | |
232 | |
262 | /* recursive routine which will fill every available space in the maze |
233 | /* recursive routine which will fill every available space in the maze |
263 | with walls*/ |
234 | with walls*/ |
264 | |
|
|
265 | void |
235 | void |
266 | fill_maze_full (char **maze, int x, int y, int xsize, int ysize) |
236 | fill_maze_full (char **maze, int x, int y, int xsize, int ysize) |
267 | { |
237 | { |
268 | int xc, yc; |
238 | int xc, yc; |
269 | |
239 | |
… | |
… | |
282 | { |
252 | { |
283 | fill_maze_full (maze, xc, yc, xsize, ysize); |
253 | fill_maze_full (maze, xc, yc, xsize, ysize); |
284 | } |
254 | } |
285 | } |
255 | } |
286 | |
256 | |
287 | |
|
|
288 | /* recursive routine which will fill much of the maze, but will leave |
257 | /* recursive routine which will fill much of the maze, but will leave |
289 | some free spots (possibly large) toward the center.*/ |
258 | some free spots (possibly large) toward the center.*/ |
290 | |
|
|
291 | void |
259 | void |
292 | fill_maze_sparse (char **maze, int x, int y, int xsize, int ysize) |
260 | fill_maze_sparse (char **maze, int x, int y, int xsize, int ysize) |
293 | { |
261 | { |
294 | int xc, yc; |
262 | int xc, yc; |
295 | |
263 | |
… | |
… | |
303 | fill_maze_sparse (maze, xc, yc, xsize, ysize); |
271 | fill_maze_sparse (maze, xc, yc, xsize, ysize); |
304 | } |
272 | } |
305 | |
273 | |
306 | /* change the if to a while for a complete maze. */ |
274 | /* change the if to a while for a complete maze. */ |
307 | if (find_free_point (maze, &xc, &yc, x, y, xsize, ysize) != -1) |
275 | if (find_free_point (maze, &xc, &yc, x, y, xsize, ysize) != -1) |
308 | { |
|
|
309 | fill_maze_sparse (maze, xc, yc, xsize, ysize); |
276 | fill_maze_sparse (maze, xc, yc, xsize, ysize); |
310 | } |
|
|
311 | } |
277 | } |