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.11 by root, Sun May 4 14:12:37 2008 UTC vs.
Revision 1.12 by root, Thu May 8 14:20:19 2008 UTC

125we can place a new block without closing a path. We may only look 125we can place a new block without closing a path. We may only look
126up, down, right, or left. */ 126up, down, right, or left. */
127int 127int
128find_free_point (char **maze, int *x, int *y, int xc, int yc, int xsize, int ysize) 128find_free_point (char **maze, int *x, int *y, int xc, int yc, int xsize, int ysize)
129{ 129{
130
131/* we will randomly pick from this list, 1=up,2=down,3=right,4=left */ 130 /* we will randomly pick from this list, 1=up,2=down,3=right,4=left */
132 int dirlist[4]; 131 int dirlist[4];
133 int count = 0; /* # elements in dirlist */ 132 int count = 0; /* # elements in dirlist */
134 133
135 /* look up */ 134 /* look up */
136 if (yc < ysize - 2 && xc > 2 && xc < xsize - 2) /* it is valid to look up */ 135 if (yc < ysize - 2 && xc > 2 && xc < xsize - 2) /* it is valid to look up */
138 int cleartest = (int) maze[xc][yc + 1] + (int) maze[xc - 1][yc + 1] + (int) maze[xc + 1][yc + 1]; 137 int cleartest = (int) maze[xc][yc + 1] + (int) maze[xc - 1][yc + 1] + (int) maze[xc + 1][yc + 1];
139 138
140 cleartest += (int) maze[xc][yc + 2] + (int) maze[xc - 1][yc + 2] + (int) maze[xc + 1][yc + 2]; 139 cleartest += (int) maze[xc][yc + 2] + (int) maze[xc - 1][yc + 2] + (int) maze[xc + 1][yc + 2];
141 140
142 if (cleartest == 0) 141 if (cleartest == 0)
143 {
144 dirlist[count] = 1; 142 dirlist[count++] = 1;
145 count++;
146 }
147 } 143 }
148 144
149 /* look down */ 145 /* look down */
150 if (yc > 2 && xc > 2 && xc < xsize - 2) /* it is valid to look down */ 146 if (yc > 2 && xc > 2 && xc < xsize - 2) /* it is valid to look down */
151 { 147 {
152 int cleartest = (int) maze[xc][yc - 1] + (int) maze[xc - 1][yc - 1] + (int) maze[xc + 1][yc - 1]; 148 int cleartest = (int) maze[xc][yc - 1] + (int) maze[xc - 1][yc - 1] + (int) maze[xc + 1][yc - 1];
153 149
154 cleartest += (int) maze[xc][yc - 2] + (int) maze[xc - 1][yc - 2] + (int) maze[xc + 1][yc - 2]; 150 cleartest += (int) maze[xc][yc - 2] + (int) maze[xc - 1][yc - 2] + (int) maze[xc + 1][yc - 2];
155 151
156 if (cleartest == 0) 152 if (cleartest == 0)
157 {
158 dirlist[count] = 2; 153 dirlist[count++] = 2;
159 count++;
160 }
161 } 154 }
162 155
163 /* look right */ 156 /* look right */
164 if (xc < xsize - 2 && yc > 2 && yc < ysize - 2) /* it is valid to look left */ 157 if (xc < xsize - 2 && yc > 2 && yc < ysize - 2) /* it is valid to look left */
165 { 158 {
166 int cleartest = (int) maze[xc + 1][yc] + (int) maze[xc + 1][yc - 1] + (int) maze[xc + 1][yc + 1]; 159 int cleartest = (int) maze[xc + 1][yc] + (int) maze[xc + 1][yc - 1] + (int) maze[xc + 1][yc + 1];
167 160
168 cleartest += (int) maze[xc + 2][yc] + (int) maze[xc + 2][yc - 1] + (int) maze[xc + 2][yc + 1]; 161 cleartest += (int) maze[xc + 2][yc] + (int) maze[xc + 2][yc - 1] + (int) maze[xc + 2][yc + 1];
169 162
170 if (cleartest == 0) 163 if (cleartest == 0)
171 {
172 dirlist[count] = 3; 164 dirlist[count++] = 3;
173 count++;
174 }
175 } 165 }
176 166
177 /* look left */ 167 /* look left */
178 if (xc > 2 && yc > 2 && yc < ysize - 2) /* it is valid to look down */ 168 if (xc > 2 && yc > 2 && yc < ysize - 2) /* it is valid to look down */
179 { 169 {
180 int cleartest = (int) maze[xc - 1][yc] + (int) maze[xc - 1][yc - 1] + (int) maze[xc - 1][yc + 1]; 170 int cleartest = (int) maze[xc - 1][yc] + (int) maze[xc - 1][yc - 1] + (int) maze[xc - 1][yc + 1];
181 171
182 cleartest += (int) maze[xc - 2][yc] + (int) maze[xc - 2][yc - 1] + (int) maze[xc - 2][yc + 1]; 172 cleartest += (int) maze[xc - 2][yc] + (int) maze[xc - 2][yc - 1] + (int) maze[xc - 2][yc + 1];
183 173
184 if (cleartest == 0) 174 if (cleartest == 0)
185 {
186 dirlist[count] = 4; 175 dirlist[count++] = 4;
187 count++;
188 }
189 } 176 }
190 177
191 if (count == 0) 178 if (count == 0)
192 return -1; /* failed to find any clear points */ 179 return -1; /* failed to find any clear points */
193 180
194 /* choose a random direction */ 181 /* choose a random direction */
195 if (count > 1)
196 count = rmg_rndm (count);
197 else
198 count = 0;
199
200 switch (dirlist[count]) 182 switch (dirlist [rmg_rndm (count)])
201 { 183 {
202 case 1: /* up */ 184 case 1: /* up */
203 {
204 *y = yc + 1; 185 *y = yc + 1;
205 *x = xc; 186 *x = xc;
206 break; 187 break;
207 }; 188
208 case 2: /* down */ 189 case 2: /* down */
209 {
210 *y = yc - 1; 190 *y = yc - 1;
211 *x = xc; 191 *x = xc;
212 break; 192 break;
213 }; 193
214 case 3: /* right */ 194 case 3: /* right */
215 {
216 *y = yc; 195 *y = yc;
217 *x = xc + 1; 196 *x = xc + 1;
218 break; 197 break;
219 } 198
220 case 4: /* left */ 199 case 4: /* left */
221 {
222 *x = xc - 1; 200 *x = xc - 1;
223 *y = yc; 201 *y = yc;
224 break; 202 break;
225 } 203
226 default: /* ??? */ 204 default: /* ??? */
227 return -1; 205 return -1;
228 206
229 } 207 }
208
230 return 1; 209 return 1;
231} 210}
232 211
233/* recursive routine which will fill every available space in the maze 212/* recursive routine which will fill every available space in the maze
234 with walls*/ 213 with walls*/
247 fill_maze_full (maze, xc, yc, xsize, ysize); 226 fill_maze_full (maze, xc, yc, xsize, ysize);
248 } 227 }
249 228
250 /* change the if to a while for a complete maze. */ 229 /* change the if to a while for a complete maze. */
251 while (find_free_point (maze, &xc, &yc, x, y, xsize, ysize) != -1) 230 while (find_free_point (maze, &xc, &yc, x, y, xsize, ysize) != -1)
252 {
253 fill_maze_full (maze, xc, yc, xsize, ysize); 231 fill_maze_full (maze, xc, yc, xsize, ysize);
254 }
255} 232}
256 233
257/* recursive routine which will fill much of the maze, but will leave 234/* recursive routine which will fill much of the maze, but will leave
258 some free spots (possibly large) toward the center.*/ 235 some free spots (possibly large) toward the center.*/
259void 236void
273 250
274 /* change the if to a while for a complete maze. */ 251 /* change the if to a while for a complete maze. */
275 if (find_free_point (maze, &xc, &yc, x, y, xsize, ysize) != -1) 252 if (find_free_point (maze, &xc, &yc, x, y, xsize, ysize) != -1)
276 fill_maze_sparse (maze, xc, yc, xsize, ysize); 253 fill_maze_sparse (maze, xc, yc, xsize, ysize);
277} 254}
255

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines