ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/random_maps/maze_gen.C
(Generate patch)

Comparing deliantra/server/random_maps/maze_gen.C (file contents):
Revision 1.8 by root, Fri Apr 11 21:09:53 2008 UTC vs.
Revision 1.11 by root, Sun May 4 14:12:37 2008 UTC

32int wall_chance; 32int wall_chance;
33 33
34/* the outsize interface routine: accepts sizes, returns a char 34/* the outsize interface routine: accepts sizes, returns a char
35** 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
36mazes have sizable rooms. option = 1, full, 0, sparse.*/ 36mazes have sizable rooms. option = 1, full, 0, sparse.*/
37 37void
38Maze 38maze_gen (Layout maze, int option)
39maze_gen (int xsize, int ysize, int option)
40{ 39{
41 int i, j; 40 maze->clear ();
42 41 maze->border ();
43 Maze maze (xsize, ysize);
44
45 /* write the outer walls */
46 for (i = 0; i < xsize; i++) maze[i][0] = maze[i][ysize - 1] = '#';
47 for (j = 0; j < ysize; j++) maze[0][j] = maze[xsize - 1][j] = '#';
48 42
49 /* find how many free wall spots there are */ 43 /* find how many free wall spots there are */
50 wall_free_size = 2 * (xsize - 4) + 2 * (ysize - 4); 44 wall_free_size = 2 * (maze->w - 4) + 2 * (maze->h - 4);
51 45
52 make_wall_free_list (xsize, ysize); 46 make_wall_free_list (maze->w, maze->h);
53 47
54 /* return the empty maze */ 48 /* return the empty maze */
55 if (wall_free_size <= 0) 49 if (wall_free_size <= 0)
56 return maze; 50 return;
57 51
58 /* recursively generate the walls of the maze */ 52 /* recursively generate the walls of the maze */
59 /* first pop a random starting point */ 53 /* first pop a random starting point */
60 while (wall_free_size > 0) 54 while (wall_free_size > 0)
61 { 55 {
56 int i, j;
57
62 pop_wall_point (&i, &j); 58 pop_wall_point (&i, &j);
63 59
64 if (option) 60 if (option)
65 fill_maze_full (maze, i, j, xsize, ysize); 61 fill_maze_full (maze, i, j, maze->w, maze->h);
66 else 62 else
67 fill_maze_sparse (maze, i, j, xsize, ysize); 63 fill_maze_sparse (maze, i, j, maze->w, maze->h);
68 } 64 }
69 65
70 /* clean up our intermediate data structures. */ 66 /* clean up our intermediate data structures. */
71 67
72 free (wall_x_list); 68 free (wall_x_list);
73 free (wall_y_list); 69 free (wall_y_list);
74
75 return maze;
76} 70}
77 71
78/* 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
79 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. */
80void 74void
115 109
116/* randomly returns one of the elements from the wall point list */ 110/* randomly returns one of the elements from the wall point list */
117void 111void
118pop_wall_point (int *x, int *y) 112pop_wall_point (int *x, int *y)
119{ 113{
120 int index = rndm (wall_free_size); 114 int index = rmg_rndm (wall_free_size);
121 115
122 *x = wall_x_list[index]; 116 *x = wall_x_list[index];
123 *y = wall_y_list[index]; 117 *y = wall_y_list[index];
124 /* write the last array point here */ 118 /* write the last array point here */
125 wall_x_list[index] = wall_x_list[wall_free_size - 1]; 119 wall_x_list[index] = wall_x_list[wall_free_size - 1];
150 dirlist[count] = 1; 144 dirlist[count] = 1;
151 count++; 145 count++;
152 } 146 }
153 } 147 }
154 148
155
156 /* look down */ 149 /* look down */
157 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 */
158 { 151 {
159 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];
160 153
165 dirlist[count] = 2; 158 dirlist[count] = 2;
166 count++; 159 count++;
167 } 160 }
168 } 161 }
169 162
170
171 /* look right */ 163 /* look right */
172 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 */
173 { 165 {
174 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];
175 167
180 dirlist[count] = 3; 172 dirlist[count] = 3;
181 count++; 173 count++;
182 } 174 }
183 } 175 }
184 176
185
186 /* look left */ 177 /* look left */
187 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 */
188 { 179 {
189 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];
190 181
200 if (count == 0) 191 if (count == 0)
201 return -1; /* failed to find any clear points */ 192 return -1; /* failed to find any clear points */
202 193
203 /* choose a random direction */ 194 /* choose a random direction */
204 if (count > 1) 195 if (count > 1)
205 count = rndm (count); 196 count = rmg_rndm (count);
206 else 197 else
207 count = 0; 198 count = 0;
208 199
209 switch (dirlist[count]) 200 switch (dirlist[count])
210 { 201 {
239 return 1; 230 return 1;
240} 231}
241 232
242/* recursive routine which will fill every available space in the maze 233/* recursive routine which will fill every available space in the maze
243 with walls*/ 234 with walls*/
244
245void 235void
246fill_maze_full (char **maze, int x, int y, int xsize, int ysize) 236fill_maze_full (char **maze, int x, int y, int xsize, int ysize)
247{ 237{
248 int xc, yc; 238 int xc, yc;
249 239
250 /* write a wall here */ 240 /* write a wall here */
251 maze[x][y] = '#'; 241 maze[x][y] = '#';
252 242
253 /* decide if we're going to pick from the wall_free_list */ 243 /* decide if we're going to pick from the wall_free_list */
254 if (rndm (4) && wall_free_size > 0) 244 if (rmg_rndm (4) && wall_free_size > 0)
255 { 245 {
256 pop_wall_point (&xc, &yc); 246 pop_wall_point (&xc, &yc);
257 fill_maze_full (maze, xc, yc, xsize, ysize); 247 fill_maze_full (maze, xc, yc, xsize, ysize);
258 } 248 }
259 249
273 263
274 /* write a wall here */ 264 /* write a wall here */
275 maze[x][y] = '#'; 265 maze[x][y] = '#';
276 266
277 /* decide if we're going to pick from the wall_free_list */ 267 /* decide if we're going to pick from the wall_free_list */
278 if (rndm (4) && wall_free_size > 0) 268 if (rmg_rndm (4) && wall_free_size > 0)
279 { 269 {
280 pop_wall_point (&xc, &yc); 270 pop_wall_point (&xc, &yc);
281 fill_maze_sparse (maze, xc, yc, xsize, ysize); 271 fill_maze_sparse (maze, xc, yc, xsize, ysize);
282 } 272 }
283 273

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines