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
|
/* peterm@langmuir.eecs.berkeley.edu: this function generates a random
blocked maze with the property that there is only one path from one spot
to any other, and there is always a path from one spot to any other.
input: xsize, ysize;
output: a char** array with # and . for closed and open respectively.
a char value of 0 represents a blank space: a '#' is
a wall.
*/
/* we need to maintain a list of wall points to generate
reasonable mazes: a straightforward recursive random walk maze
generator would generate a map with a trivial circle-the-outer-wall solution */
#include <stdio.h>
#include <global.h>
/*#include <random_map.h>*/
#include <maze_gen.h>
#include <time.h>
/* this include solely, and only, is needed for the definition of RANDOM */
/* global variables that everyone needs: don't want to pass them in
as parameters every time. */
int *wall_x_list=0;
int *wall_y_list=0;
int wall_free_size=0;
/* heuristically, we need to change wall_chance based on the size of
the maze. */
int wall_chance;
/* the outsize interface routine: accepts sizes, returns a char
** maze. option is a flag for either a sparse or a full maze. Sparse
mazes have sizable rooms. option = 1, full, 0, sparse.*/
char **maze_gen(int xsize, int ysize,int option) {
int i,j;
/* allocate that array, set it up */
char **maze = (char **)calloc(sizeof(char*),xsize);
for(i=0;i<xsize;i++) {
maze[i] = (char *) calloc(sizeof(char),ysize);
}
/* write the outer walls */
for(i=0;i<xsize;i++)
maze[i][0] = maze[i][ysize-1] = '#';
for(j=0;j<ysize;j++)
maze[0][j] = maze[xsize-1][j] = '#';
/* find how many free wall spots there are */
wall_free_size = 2 * (xsize-4) + 2*(ysize-4 );
make_wall_free_list(xsize,ysize);
/* return the empty maze */
if(wall_free_size <=0 ) return maze;
/* recursively generate the walls of the maze */
/* first pop a random starting point */
while(wall_free_size > 0) {
pop_wall_point(&i,&j);
if(option) fill_maze_full(maze,i,j,xsize,ysize);
else fill_maze_sparse(maze,i,j,xsize,ysize);
}
/* clean up our intermediate data structures. */
free(wall_x_list);
free(wall_y_list);
return maze;
}
/* the free wall points are those outer points which aren't corners or
near corners, and don't have a maze wall growing out of them already. */
void make_wall_free_list(int xsize, int ysize) {
int i,j,count;
count = 0; /* entries already placed in the free list */
/*allocate it*/
if(wall_free_size < 0) return;
wall_x_list = (int *) calloc(sizeof(int),wall_free_size);
wall_y_list = (int *) calloc(sizeof(int),wall_free_size);
/* top and bottom wall */
for(i = 2; i<xsize-2; i++) {
wall_x_list[count] = i;
wall_y_list[count] = 0;
count++;
wall_x_list[count] = i;
wall_y_list[count] = ysize-1;
count++;
}
/* left and right wall */
for(j = 2; j<ysize-2; j++) {
wall_x_list[count] = 0;
wall_y_list[count] = j;
count++;
wall_x_list[count] = xsize-1;
wall_y_list[count] = j;
count++;
}
}
/* randomly returns one of the elements from the wall point list */
void pop_wall_point(int *x,int *y) {
int index = RANDOM() % wall_free_size;
*x = wall_x_list[index];
*y = wall_y_list[index];
/* write the last array point here */
wall_x_list[index]=wall_x_list[wall_free_size-1];
wall_y_list[index]=wall_y_list[wall_free_size-1];
wall_free_size--;
}
/* find free point: randomly look for a square adjacent to this one where
we can place a new block without closing a path. We may only look
up, down, right, or left. */
int find_free_point(char **maze,int *x, int *y,int xc,int yc, int xsize, int ysize) {
/* we will randomly pick from this list, 1=up,2=down,3=right,4=left */
int dirlist[4];
int count = 0; /* # elements in dirlist */
/* look up */
if(yc < ysize-2 && xc > 2 && xc < xsize-2) /* it is valid to look up */
{
int cleartest = (int) maze[xc][yc+1] + (int)maze[xc-1][yc+1]
+ (int) maze[xc+1][yc+1];
cleartest += (int) maze[xc][yc+2] + (int)maze[xc-1][yc+2]
+ (int) maze[xc+1][yc+2];
if(cleartest == 0) {
dirlist[count] = 1;
count++;
}
}
/* look down */
if(yc > 2 && xc > 2 && xc < xsize-2) /* it is valid to look down */
{
int cleartest = (int) maze[xc][yc-1] + (int)maze[xc-1][yc-1]
+ (int) maze[xc+1][yc-1];
cleartest += (int) maze[xc][yc-2] + (int)maze[xc-1][yc-2]
+ (int) maze[xc+1][yc-2];
if(cleartest == 0) {
dirlist[count] = 2;
count++;
}
}
/* look right */
if(xc < xsize- 2 && yc > 2 && yc < ysize-2) /* it is valid to look left */
{
int cleartest = (int) maze[xc+1][yc] + (int)maze[xc+1][yc-1]
+ (int) maze[xc+1][yc+1];
cleartest += (int) maze[xc+2][yc] + (int)maze[xc+2][yc-1]
+ (int) maze[xc+2][yc+1];
if(cleartest == 0) {
dirlist[count] = 3;
count++;
}
}
/* look left */
if(xc > 2 && yc > 2 && yc < ysize-2) /* it is valid to look down */
{
int cleartest = (int) maze[xc-1][yc] + (int)maze[xc-1][yc-1]
+ (int) maze[xc-1][yc+1];
cleartest += (int) maze[xc-2][yc] + (int)maze[xc-2][yc-1]
+ (int) maze[xc-2][yc+1];
if(cleartest == 0) {
dirlist[count] = 4;
count++;
}
}
if(count==0) return -1; /* failed to find any clear points */
/* choose a random direction */
if(count > 1) count = RANDOM() % count;
else count=0;
switch(dirlist[count]) {
case 1: /* up */
{
*y = yc +1;
*x = xc;
break;
};
case 2: /* down */
{
*y = yc-1;
*x = xc;
break;
};
case 3: /* right */
{
*y = yc;
*x = xc+1;
break;
}
case 4: /* left */
{
*x = xc-1;
*y = yc;
break;
}
default: /* ??? */
{
return -1;
}
}
return 1;
}
/* recursive routine which will fill every available space in the maze
with walls*/
void fill_maze_full(char **maze, int x, int y, int xsize, int ysize ) {
int xc,yc;
/* write a wall here */
maze[x][y] = '#';
/* decide if we're going to pick from the wall_free_list */
if(RANDOM()%4 && wall_free_size > 0) {
pop_wall_point(&xc,&yc);
fill_maze_full(maze,xc,yc,xsize,ysize);
}
/* change the if to a while for a complete maze. */
while(find_free_point(maze,&xc,&yc,x,y,xsize,ysize)!=-1) {
fill_maze_full(maze,xc,yc,xsize,ysize);
}
}
/* recursive routine which will fill much of the maze, but will leave
some free spots (possibly large) toward the center.*/
void fill_maze_sparse(char **maze, int x, int y, int xsize, int ysize ) {
int xc,yc;
/* write a wall here */
maze[x][y] = '#';
/* decide if we're going to pick from the wall_free_list */
if(RANDOM()%4 && wall_free_size > 0) {
pop_wall_point(&xc,&yc);
fill_maze_sparse(maze,xc,yc,xsize,ysize);
}
/* change the if to a while for a complete maze. */
if(find_free_point(maze,&xc,&yc,x,y,xsize,ysize)!=-1) {
fill_maze_sparse(maze,xc,yc,xsize,ysize);
}
}
|