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.14 by root, Fri Nov 6 13:03:34 2009 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines