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