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

Comparing deliantra/server/random_maps/room_gen_onion.C (file contents):
Revision 1.4 by root, Thu Sep 14 22:34:02 2006 UTC vs.
Revision 1.11 by root, Sun Jul 1 05:00:19 2007 UTC

1/* 1/*
2 CrossFire, A Multiplayer game for X-windows 2 * This file is part of Crossfire TRT, the Roguelike Realtime MORPG.
3 3 *
4 * Copyright (©) 2005,2006,2007 Marc Alexander Lehmann / Robin Redeker / the Crossfire TRT team
4 Copyright (C) 2001 Mark Wedel & Crossfire Development Team 5 * Copyright (©) 2001,2007 Mark Wedel & Crossfire Development Team
5 Copyright (C) 1992 Frank Tore Johansen 6 * Copyright (©) 1992,2007 Frank Tore Johansen
6 7 *
7 This program is free software; you can redistribute it and/or modify 8 * Crossfire TRT is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by 9 * it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or 10 * the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version. 11 * (at your option) any later version.
11 12 *
12 This program is distributed in the hope that it will be useful, 13 * This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details. 16 * GNU General Public License for more details.
16 17 *
17 You should have received a copy of the GNU General Public License 18 * You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software 19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 20 *
20
21 The authors can be reached via e-mail at <crossfire@schmorp.de> 21 * The authors can be reached via e-mail to <crossfire@schmorp.de>
22*/ 22 */
23
24 23
25/* The onion room generator: 24/* The onion room generator:
26Onion rooms are like this: 25Onion rooms are like this:
27 26
28char **map_gen_onion(int xsize, int ysize, int option, int layers); 27char **map_gen_onion(int xsize, int ysize, int option, int layers);
78 } 77 }
79 78
80 /* pick some random options if option = 0 */ 79 /* pick some random options if option = 0 */
81 if (option == 0) 80 if (option == 0)
82 { 81 {
83 switch (RANDOM () % 3) 82 switch (rndm (3))
84 { 83 {
85 case 0: 84 case 0:
86 option |= OPT_CENTERED; 85 option |= RMOPT_CENTERED;
87 break; 86 break;
88 case 1: 87 case 1:
89 option |= OPT_BOTTOM_C; 88 option |= RMOPT_BOTTOM_C;
90 break; 89 break;
91 case 2: 90 case 2:
92 option |= OPT_BOTTOM_R; 91 option |= RMOPT_BOTTOM_R;
93 break; 92 break;
94 } 93 }
95 if (RANDOM () % 2) 94 if (rndm (2))
96 option |= OPT_LINEAR; 95 option |= RMOPT_LINEAR;
97 if (RANDOM () % 2) 96 if (rndm (2))
98 option |= OPT_IRR_SPACE; 97 option |= RMOPT_IRR_SPACE;
99 } 98 }
100 99
101 /* write the outer walls, if appropriate. */ 100 /* write the outer walls, if appropriate. */
102 if (!(option & OPT_WALL_OFF)) 101 if (!(option & RMOPT_WALL_OFF))
103 { 102 {
104 for (i = 0; i < xsize; i++) 103 for (i = 0; i < xsize; i++)
105 maze[i][0] = maze[i][ysize - 1] = '#'; 104 maze[i][0] = maze[i][ysize - 1] = '#';
106 for (j = 0; j < ysize; j++) 105 for (j = 0; j < ysize; j++)
107 maze[0][j] = maze[xsize - 1][j] = '#'; 106 maze[0][j] = maze[xsize - 1][j] = '#';
108 }; 107 };
109 108
110 if (option & OPT_WALLS_ONLY) 109 if (option & RMOPT_WALLS_ONLY)
111 return maze; 110 return maze;
112 111
113 /* pick off the mutually exclusive options */ 112 /* pick off the mutually exclusive options */
114 if (option & OPT_BOTTOM_R) 113 if (option & RMOPT_BOTTOM_R)
115 bottom_right_centered_onion (maze, xsize, ysize, option, layers); 114 bottom_right_centered_onion (maze, xsize, ysize, option, layers);
116 else if (option & OPT_BOTTOM_C) 115 else if (option & RMOPT_BOTTOM_C)
117 bottom_centered_onion (maze, xsize, ysize, option, layers); 116 bottom_centered_onion (maze, xsize, ysize, option, layers);
118 else if (option & OPT_CENTERED) 117 else if (option & RMOPT_CENTERED)
119 centered_onion (maze, xsize, ysize, option, layers); 118 centered_onion (maze, xsize, ysize, option, layers);
120 119
121 return maze; 120 return maze;
122} 121}
123 122
124void 123void
125centered_onion (char **maze, int xsize, int ysize, int option, int layers) 124centered_onion (char **maze, int xsize, int ysize, int option, int layers)
125{
126 int i, maxlayers;
127 float *xlocations;
128 float *ylocations;
129
130 maxlayers = (MIN (xsize, ysize) - 2) / 5;
131 if (!maxlayers)
132 return; /* map too small to onionize */
133
134 if (layers > maxlayers)
135 layers = maxlayers;
136
137 if (layers == 0)
138 layers = rndm (maxlayers) + 1;
139
140 xlocations = (float *) calloc (sizeof (float), 2 * layers);
141 ylocations = (float *) calloc (sizeof (float), 2 * layers);
142
143 /* place all the walls */
144 if (option & RMOPT_IRR_SPACE) /* randomly spaced */
145 {
146 int x_spaces_available, y_spaces_available;
147
148 /* the "extra" spaces available for spacing between layers */
149 x_spaces_available = (xsize - 2) - 6 * layers + 1;
150 y_spaces_available = (ysize - 2) - 6 * layers + 1;
151
152
153 /* pick an initial random pitch */
154 for (i = 0; i < 2 * layers; i++)
155 {
156 float xpitch = 2, ypitch = 2;
157
158 if (x_spaces_available > 0)
159 xpitch = 2 + (rndm (x_spaces_available) + rndm (x_spaces_available) + rndm (x_spaces_available)) / 3;
160
161 if (y_spaces_available > 0)
162 ypitch = 2 + (rndm (y_spaces_available) + rndm (y_spaces_available) + rndm (y_spaces_available)) / 3;
163
164 xlocations[i] = ((i > 0) ? xlocations[i - 1] : 0) + xpitch;
165 ylocations[i] = ((i > 0) ? ylocations[i - 1] : 0) + ypitch;
166 x_spaces_available -= (int) (xpitch - 2);
167 y_spaces_available -= (int) (ypitch - 2);
168 }
169
170 }
171 if (!(option & RMOPT_IRR_SPACE))
172 { /* evenly spaced */
173 float xpitch, ypitch; /* pitch of the onion layers */
174
175 xpitch = (xsize - 2.0) / (2.0 * layers + 1);
176 ypitch = (ysize - 2.0) / (2.0 * layers + 1);
177 xlocations[0] = xpitch;
178 ylocations[0] = ypitch;
179 for (i = 1; i < 2 * layers; i++)
180 {
181 xlocations[i] = xlocations[i - 1] + xpitch;
182 ylocations[i] = ylocations[i - 1] + ypitch;
183 }
184 }
185
186 /* draw all the onion boxes. */
187
188 draw_onion (maze, xlocations, ylocations, layers);
189 make_doors (maze, xlocations, ylocations, layers, option);
190
191}
192
193void
194bottom_centered_onion (char **maze, int xsize, int ysize, int option, int layers)
126{ 195{
127 int i, maxlayers; 196 int i, maxlayers;
128 float *xlocations; 197 float *xlocations;
129 float *ylocations; 198 float *ylocations;
130 199
132 if (!maxlayers) 201 if (!maxlayers)
133 return; /* map too small to onionize */ 202 return; /* map too small to onionize */
134 if (layers > maxlayers) 203 if (layers > maxlayers)
135 layers = maxlayers; 204 layers = maxlayers;
136 if (layers == 0) 205 if (layers == 0)
137 layers = (RANDOM () % maxlayers) + 1; 206 layers = rndm (maxlayers) + 1;
138 xlocations = (float *) calloc (sizeof (float), 2 * layers); 207 xlocations = (float *) calloc (sizeof (float), 2 * layers);
139 ylocations = (float *) calloc (sizeof (float), 2 * layers); 208 ylocations = (float *) calloc (sizeof (float), 2 * layers);
140 209
141 210
142 /* place all the walls */ 211 /* place all the walls */
143 if (option & OPT_IRR_SPACE) /* randomly spaced */ 212 if (option & RMOPT_IRR_SPACE) /* randomly spaced */
144 { 213 {
145 int x_spaces_available, y_spaces_available; 214 int x_spaces_available, y_spaces_available;
146 215
147 /* the "extra" spaces available for spacing between layers */ 216 /* the "extra" spaces available for spacing between layers */
148 x_spaces_available = (xsize - 2) - 6 * layers + 1; 217 x_spaces_available = (xsize - 2) - 6 * layers + 1;
149 y_spaces_available = (ysize - 2) - 6 * layers + 1; 218 y_spaces_available = (ysize - 2) - 3 * layers + 1;
150 219
151 220
152 /* pick an initial random pitch */ 221 /* pick an initial random pitch */
153 for (i = 0; i < 2 * layers; i++) 222 for (i = 0; i < 2 * layers; i++)
154 { 223 {
155 float xpitch = 2, ypitch = 2; 224 float xpitch = 2, ypitch = 2;
156 225
157 if (x_spaces_available > 0) 226 if (x_spaces_available > 0)
158 xpitch = 2 + (RANDOM () % x_spaces_available + RANDOM () % x_spaces_available + RANDOM () % x_spaces_available) / 3; 227 xpitch = 2 + (rndm (x_spaces_available) + rndm (x_spaces_available) + rndm (x_spaces_available)) / 3;
159 228
160 if (y_spaces_available > 0) 229 if (y_spaces_available > 0)
161 ypitch = 2 + (RANDOM () % y_spaces_available + RANDOM () % y_spaces_available + RANDOM () % y_spaces_available) / 3; 230 ypitch = 2 + (rndm (y_spaces_available) + rndm (y_spaces_available) + rndm (y_spaces_available)) / 3;
231
162 xlocations[i] = ((i > 0) ? xlocations[i - 1] : 0) + xpitch; 232 xlocations[i] = ((i > 0) ? xlocations[i - 1] : 0) + xpitch;
233 if (i < layers)
163 ylocations[i] = ((i > 0) ? ylocations[i - 1] : 0) + ypitch; 234 ylocations[i] = ((i > 0) ? ylocations[i - 1] : 0) + ypitch;
235 else
236 ylocations[i] = ysize - 1;
237
164 x_spaces_available -= (int) (xpitch - 2); 238 x_spaces_available -= (int) (xpitch - 2);
165 y_spaces_available -= (int) (ypitch - 2); 239 y_spaces_available -= (int) (ypitch - 2);
166 } 240 }
167 241
168 } 242 }
169 if (!(option & OPT_IRR_SPACE)) 243 if (!(option & RMOPT_IRR_SPACE))
170 { /* evenly spaced */ 244 { /* evenly spaced */
171 float xpitch, ypitch; /* pitch of the onion layers */ 245 float xpitch, ypitch; /* pitch of the onion layers */
172 246
173 xpitch = (xsize - 2.0) / (2.0 * layers + 1); 247 xpitch = (xsize - 2.0) / (2.0 * layers + 1);
174 ypitch = (ysize - 2.0) / (2.0 * layers + 1); 248 ypitch = (ysize - 2.0) / (layers + 1);
175 xlocations[0] = xpitch; 249 xlocations[0] = xpitch;
176 ylocations[0] = ypitch; 250 ylocations[0] = ypitch;
177 for (i = 1; i < 2 * layers; i++) 251 for (i = 1; i < 2 * layers; i++)
178 { 252 {
179 xlocations[i] = xlocations[i - 1] + xpitch; 253 xlocations[i] = xlocations[i - 1] + xpitch;
254 if (i < layers)
180 ylocations[i] = ylocations[i - 1] + ypitch; 255 ylocations[i] = ylocations[i - 1] + ypitch;
256 else
257 ylocations[i] = ysize - 1;
181 } 258 }
182 } 259 }
183 260
184 /* draw all the onion boxes. */ 261 /* draw all the onion boxes. */
185 262
186 draw_onion (maze, xlocations, ylocations, layers); 263 draw_onion (maze, xlocations, ylocations, layers);
187 make_doors (maze, xlocations, ylocations, layers, option); 264 make_doors (maze, xlocations, ylocations, layers, option);
188 265
189} 266}
190 267
268
269/* draw_boxes: draws the lines in the maze defining the onion layers */
270
191void 271void
272draw_onion (char **maze, float *xlocations, float *ylocations, int layers)
273{
274 int i, j, l;
275
276 for (l = 0; l < layers; l++)
277 {
278 int x1, x2, y1, y2;
279
280 /* horizontal segments */
281 y1 = (int) ylocations[l];
282 y2 = (int) ylocations[2 * layers - l - 1];
283 for (i = (int) xlocations[l]; i <= (int) xlocations[2 * layers - l - 1]; i++)
284 {
285 maze[i][y1] = '#';
286 maze[i][y2] = '#';
287 }
288
289 /* vertical segments */
290 x1 = (int) xlocations[l];
291 x2 = (int) xlocations[2 * layers - l - 1];
292 for (j = (int) ylocations[l]; j <= (int) ylocations[2 * layers - l - 1]; j++)
293 {
294 maze[x1][j] = '#';
295 maze[x2][j] = '#';
296 }
297
298 }
299}
300
301void
302make_doors (char **maze, float *xlocations, float *ylocations, int layers, int options)
303{
304 int freedoms; /* number of different walls on which we could place a door */
305 int which_wall; /* left, 1, top, 2, right, 3, bottom 4 */
306 int l, x1 = 0, x2, y1 = 0, y2;
307
308 freedoms = 4; /* centered */
309 if (options & RMOPT_BOTTOM_C)
310 freedoms = 3;
311 if (options & RMOPT_BOTTOM_R)
312 freedoms = 2;
313 if (layers <= 0)
314 return;
315
316 /* pick which wall will have a door. */
317 which_wall = rndm (freedoms) + 1;
318 for (l = 0; l < layers; l++)
319 {
320 if (options & RMOPT_LINEAR)
321 { /* linear door placement. */
322 switch (which_wall)
323 {
324 case 1:
325 { /* left hand wall */
326 x1 = (int) xlocations[l];
327 y1 = (int) ((ylocations[l] + ylocations[2 * layers - l - 1]) / 2);
328 break;
329 }
330 case 2:
331 { /* top wall placement */
332 x1 = (int) ((xlocations[l] + xlocations[2 * layers - l - 1]) / 2);
333 y1 = (int) ylocations[l];
334 break;
335 }
336 case 3:
337 { /* right wall placement */
338 x1 = (int) xlocations[2 * layers - l - 1];
339 y1 = (int) ((ylocations[l] + ylocations[2 * layers - l - 1]) / 2);
340 break;
341 }
342 case 4:
343 { /* bottom wall placement */
344 x1 = (int) ((xlocations[l] + xlocations[2 * layers - l - 1]) / 2);
345 y1 = (int) ylocations[2 * layers - l - 1];
346 break;
347 }
348 }
349 }
350 else
351 { /* random door placement. */
352 which_wall = rndm (freedoms) + 1;
353 switch (which_wall)
354 {
355 case 1:
356 { /* left hand wall */
357 x1 = (int) xlocations[l];
358 y2 = (int) (ylocations[2 * layers - l - 1] - ylocations[l] - 1);
359 if (y2 > 0)
360 y1 = (int) (ylocations[l] + rndm (y2) + 1);
361 else
362 y1 = (int) (ylocations[l] + 1);
363 break;
364 }
365 case 2:
366 { /* top wall placement */
367 x2 = (int) ((-xlocations[l] + xlocations[2 * layers - l - 1])) - 1;
368 if (x2 > 0)
369 x1 = (int) (xlocations[l] + rndm (x2) + 1);
370 else
371 x1 = (int) (xlocations[l] + 1);
372 y1 = (int) ylocations[l];
373 break;
374 }
375 case 3:
376 { /* right wall placement */
377 x1 = (int) xlocations[2 * layers - l - 1];
378 y2 = (int) ((-ylocations[l] + ylocations[2 * layers - l - 1])) - 1;
379 if (y2 > 0)
380 y1 = (int) (ylocations[l] + rndm (y2) + 1);
381 else
382 y1 = (int) (ylocations[l] + 1);
383
384 break;
385 }
386 case 4:
387 { /* bottom wall placement */
388 x2 = (int) ((-xlocations[l] + xlocations[2 * layers - l - 1])) - 1;
389 if (x2 > 0)
390 x1 = (int) (xlocations[l] + rndm (x2) + 1);
391 else
392 x1 = (int) (xlocations[l] + 1);
393 y1 = (int) ylocations[2 * layers - l - 1];
394 break;
395 }
396
397 }
398 }
399 if (options & RMOPT_NO_DOORS)
400 maze[x1][y1] = '#'; /* no door. */
401 else
402 maze[x1][y1] = 'D'; /* write the door */
403
404 }
405 /* mark the center of the maze with a C */
406 l = layers - 1;
407 x1 = (int) (xlocations[l] + xlocations[2 * layers - l - 1]) / 2;
408 y1 = (int) (ylocations[l] + ylocations[2 * layers - l - 1]) / 2;
409 maze[x1][y1] = 'C';
410
411 /* not needed anymore */
412 free (xlocations);
413 free (ylocations);
414
415}
416
417void
192bottom_centered_onion (char **maze, int xsize, int ysize, int option, int layers) 418bottom_right_centered_onion (char **maze, int xsize, int ysize, int option, int layers)
193{ 419{
194 int i, maxlayers; 420 int i, maxlayers;
195 float *xlocations; 421 float *xlocations;
196 float *ylocations; 422 float *ylocations;
197 423
199 if (!maxlayers) 425 if (!maxlayers)
200 return; /* map too small to onionize */ 426 return; /* map too small to onionize */
201 if (layers > maxlayers) 427 if (layers > maxlayers)
202 layers = maxlayers; 428 layers = maxlayers;
203 if (layers == 0) 429 if (layers == 0)
204 layers = (RANDOM () % maxlayers) + 1; 430 layers = rndm (maxlayers) + 1;
431
205 xlocations = (float *) calloc (sizeof (float), 2 * layers); 432 xlocations = (float *) calloc (sizeof (float), 2 * layers);
206 ylocations = (float *) calloc (sizeof (float), 2 * layers); 433 ylocations = (float *) calloc (sizeof (float), 2 * layers);
207 434
208
209 /* place all the walls */ 435 /* place all the walls */
210 if (option & OPT_IRR_SPACE) /* randomly spaced */ 436 if (option & RMOPT_IRR_SPACE) /* randomly spaced */
211 { 437 {
212 int x_spaces_available, y_spaces_available; 438 int x_spaces_available, y_spaces_available;
213 439
214 /* the "extra" spaces available for spacing between layers */ 440 /* the "extra" spaces available for spacing between layers */
215 x_spaces_available = (xsize - 2) - 6 * layers + 1; 441 x_spaces_available = (xsize - 2) - 3 * layers + 1;
216 y_spaces_available = (ysize - 2) - 3 * layers + 1; 442 y_spaces_available = (ysize - 2) - 3 * layers + 1;
217 443
218 444
219 /* pick an initial random pitch */ 445 /* pick an initial random pitch */
220 for (i = 0; i < 2 * layers; i++) 446 for (i = 0; i < 2 * layers; i++)
221 { 447 {
222 float xpitch = 2, ypitch = 2; 448 float xpitch = 2, ypitch = 2;
223 449
224 if (x_spaces_available > 0) 450 if (x_spaces_available > 0)
225 xpitch = 2 + (RANDOM () % x_spaces_available + RANDOM () % x_spaces_available + RANDOM () % x_spaces_available) / 3; 451 xpitch = 2 + (rndm (x_spaces_available) + rndm (x_spaces_available) + rndm (x_spaces_available)) / 3;
226 452
227 if (y_spaces_available > 0) 453 if (y_spaces_available > 0)
228 ypitch = 2 + (RANDOM () % y_spaces_available + RANDOM () % y_spaces_available + RANDOM () % y_spaces_available) / 3; 454 ypitch = 2 + (rndm (y_spaces_available) + rndm (y_spaces_available) + rndm (y_spaces_available)) / 3;
455
456 if (i < layers)
229 xlocations[i] = ((i > 0) ? xlocations[i - 1] : 0) + xpitch; 457 xlocations[i] = ((i > 0) ? xlocations[i - 1] : 0) + xpitch;
458 else
459 xlocations[i] = xsize - 1;
460
230 if (i < layers) 461 if (i < layers)
231 ylocations[i] = ((i > 0) ? ylocations[i - 1] : 0) + ypitch; 462 ylocations[i] = ((i > 0) ? ylocations[i - 1] : 0) + ypitch;
232 else 463 else
233 ylocations[i] = ysize - 1; 464 ylocations[i] = ysize - 1;
234 x_spaces_available -= (int) (xpitch - 2); 465 x_spaces_available -= (int) (xpitch - 2);
235 y_spaces_available -= (int) (ypitch - 2); 466 y_spaces_available -= (int) (ypitch - 2);
236 } 467 }
237 468
238 } 469 }
239 if (!(option & OPT_IRR_SPACE)) 470 if (!(option & RMOPT_IRR_SPACE))
240 { /* evenly spaced */ 471 { /* evenly spaced */
241 float xpitch, ypitch; /* pitch of the onion layers */ 472 float xpitch, ypitch; /* pitch of the onion layers */
242 473
243 xpitch = (xsize - 2.0) / (2.0 * layers + 1); 474 xpitch = (xsize - 2.0) / (2.0 * layers + 1);
244 ypitch = (ysize - 2.0) / (layers + 1); 475 ypitch = (ysize - 2.0) / (layers + 1);
245 xlocations[0] = xpitch; 476 xlocations[0] = xpitch;
246 ylocations[0] = ypitch; 477 ylocations[0] = ypitch;
247 for (i = 1; i < 2 * layers; i++) 478 for (i = 1; i < 2 * layers; i++)
248 { 479 {
480 if (i < layers)
249 xlocations[i] = xlocations[i - 1] + xpitch; 481 xlocations[i] = xlocations[i - 1] + xpitch;
482 else
483 xlocations[i] = xsize - 1;
250 if (i < layers) 484 if (i < layers)
251 ylocations[i] = ylocations[i - 1] + ypitch; 485 ylocations[i] = ylocations[i - 1] + ypitch;
252 else 486 else
253 ylocations[i] = ysize - 1; 487 ylocations[i] = ysize - 1;
254 } 488 }
258 492
259 draw_onion (maze, xlocations, ylocations, layers); 493 draw_onion (maze, xlocations, ylocations, layers);
260 make_doors (maze, xlocations, ylocations, layers, option); 494 make_doors (maze, xlocations, ylocations, layers, option);
261 495
262} 496}
263
264
265/* draw_boxes: draws the lines in the maze defining the onion layers */
266
267void
268draw_onion (char **maze, float *xlocations, float *ylocations, int layers)
269{
270 int i, j, l;
271
272 for (l = 0; l < layers; l++)
273 {
274 int x1, x2, y1, y2;
275
276 /* horizontal segments */
277 y1 = (int) ylocations[l];
278 y2 = (int) ylocations[2 * layers - l - 1];
279 for (i = (int) xlocations[l]; i <= (int) xlocations[2 * layers - l - 1]; i++)
280 {
281 maze[i][y1] = '#';
282 maze[i][y2] = '#';
283 }
284
285 /* vertical segments */
286 x1 = (int) xlocations[l];
287 x2 = (int) xlocations[2 * layers - l - 1];
288 for (j = (int) ylocations[l]; j <= (int) ylocations[2 * layers - l - 1]; j++)
289 {
290 maze[x1][j] = '#';
291 maze[x2][j] = '#';
292 }
293
294 }
295}
296
297void
298make_doors (char **maze, float *xlocations, float *ylocations, int layers, int options)
299{
300 int freedoms; /* number of different walls on which we could place a door */
301 int which_wall; /* left, 1, top, 2, right, 3, bottom 4 */
302 int l, x1 = 0, x2, y1 = 0, y2;
303
304 freedoms = 4; /* centered */
305 if (options & OPT_BOTTOM_C)
306 freedoms = 3;
307 if (options & OPT_BOTTOM_R)
308 freedoms = 2;
309 if (layers <= 0)
310 return;
311 /* pick which wall will have a door. */
312 which_wall = RANDOM () % freedoms + 1;
313 for (l = 0; l < layers; l++)
314 {
315 if (options & OPT_LINEAR)
316 { /* linear door placement. */
317 switch (which_wall)
318 {
319 case 1:
320 { /* left hand wall */
321 x1 = (int) xlocations[l];
322 y1 = (int) ((ylocations[l] + ylocations[2 * layers - l - 1]) / 2);
323 break;
324 }
325 case 2:
326 { /* top wall placement */
327 x1 = (int) ((xlocations[l] + xlocations[2 * layers - l - 1]) / 2);
328 y1 = (int) ylocations[l];
329 break;
330 }
331 case 3:
332 { /* right wall placement */
333 x1 = (int) xlocations[2 * layers - l - 1];
334 y1 = (int) ((ylocations[l] + ylocations[2 * layers - l - 1]) / 2);
335 break;
336 }
337 case 4:
338 { /* bottom wall placement */
339 x1 = (int) ((xlocations[l] + xlocations[2 * layers - l - 1]) / 2);
340 y1 = (int) ylocations[2 * layers - l - 1];
341 break;
342 }
343 }
344 }
345 else
346 { /* random door placement. */
347 which_wall = RANDOM () % freedoms + 1;
348 switch (which_wall)
349 {
350 case 1:
351 { /* left hand wall */
352 x1 = (int) xlocations[l];
353 y2 = (int) (ylocations[2 * layers - l - 1] - ylocations[l] - 1);
354 if (y2 > 0)
355 y1 = (int) (ylocations[l] + RANDOM () % y2 + 1);
356 else
357 y1 = (int) (ylocations[l] + 1);
358 break;
359 }
360 case 2:
361 { /* top wall placement */
362 x2 = (int) ((-xlocations[l] + xlocations[2 * layers - l - 1])) - 1;
363 if (x2 > 0)
364 x1 = (int) (xlocations[l] + RANDOM () % x2 + 1);
365 else
366 x1 = (int) (xlocations[l] + 1);
367 y1 = (int) ylocations[l];
368 break;
369 }
370 case 3:
371 { /* right wall placement */
372 x1 = (int) xlocations[2 * layers - l - 1];
373 y2 = (int) ((-ylocations[l] + ylocations[2 * layers - l - 1])) - 1;
374 if (y2 > 0)
375 y1 = (int) (ylocations[l] + RANDOM () % y2 + 1);
376 else
377 y1 = (int) (ylocations[l] + 1);
378
379 break;
380 }
381 case 4:
382 { /* bottom wall placement */
383 x2 = (int) ((-xlocations[l] + xlocations[2 * layers - l - 1])) - 1;
384 if (x2 > 0)
385 x1 = (int) (xlocations[l] + RANDOM () % x2 + 1);
386 else
387 x1 = (int) (xlocations[l] + 1);
388 y1 = (int) ylocations[2 * layers - l - 1];
389 break;
390 }
391
392 }
393 }
394 if (options & OPT_NO_DOORS)
395 maze[x1][y1] = '#'; /* no door. */
396 else
397 maze[x1][y1] = 'D'; /* write the door */
398
399 }
400 /* mark the center of the maze with a C */
401 l = layers - 1;
402 x1 = (int) (xlocations[l] + xlocations[2 * layers - l - 1]) / 2;
403 y1 = (int) (ylocations[l] + ylocations[2 * layers - l - 1]) / 2;
404 maze[x1][y1] = 'C';
405
406 /* not needed anymore */
407 free (xlocations);
408 free (ylocations);
409
410}
411
412void
413bottom_right_centered_onion (char **maze, int xsize, int ysize, int option, int layers)
414{
415 int i, maxlayers;
416 float *xlocations;
417 float *ylocations;
418
419 maxlayers = (MIN (xsize, ysize) - 2) / 5;
420 if (!maxlayers)
421 return; /* map too small to onionize */
422 if (layers > maxlayers)
423 layers = maxlayers;
424 if (layers == 0)
425 layers = (RANDOM () % maxlayers) + 1;
426 xlocations = (float *) calloc (sizeof (float), 2 * layers);
427 ylocations = (float *) calloc (sizeof (float), 2 * layers);
428
429
430 /* place all the walls */
431 if (option & OPT_IRR_SPACE) /* randomly spaced */
432 {
433 int x_spaces_available, y_spaces_available;
434
435 /* the "extra" spaces available for spacing between layers */
436 x_spaces_available = (xsize - 2) - 3 * layers + 1;
437 y_spaces_available = (ysize - 2) - 3 * layers + 1;
438
439
440 /* pick an initial random pitch */
441 for (i = 0; i < 2 * layers; i++)
442 {
443 float xpitch = 2, ypitch = 2;
444
445 if (x_spaces_available > 0)
446 xpitch = 2 + (RANDOM () % x_spaces_available + RANDOM () % x_spaces_available + RANDOM () % x_spaces_available) / 3;
447
448 if (y_spaces_available > 0)
449 ypitch = 2 + (RANDOM () % y_spaces_available + RANDOM () % y_spaces_available + RANDOM () % y_spaces_available) / 3;
450 if (i < layers)
451 xlocations[i] = ((i > 0) ? xlocations[i - 1] : 0) + xpitch;
452 else
453 xlocations[i] = xsize - 1;
454
455 if (i < layers)
456 ylocations[i] = ((i > 0) ? ylocations[i - 1] : 0) + ypitch;
457 else
458 ylocations[i] = ysize - 1;
459 x_spaces_available -= (int) (xpitch - 2);
460 y_spaces_available -= (int) (ypitch - 2);
461 }
462
463 }
464 if (!(option & OPT_IRR_SPACE))
465 { /* evenly spaced */
466 float xpitch, ypitch; /* pitch of the onion layers */
467
468 xpitch = (xsize - 2.0) / (2.0 * layers + 1);
469 ypitch = (ysize - 2.0) / (layers + 1);
470 xlocations[0] = xpitch;
471 ylocations[0] = ypitch;
472 for (i = 1; i < 2 * layers; i++)
473 {
474 if (i < layers)
475 xlocations[i] = xlocations[i - 1] + xpitch;
476 else
477 xlocations[i] = xsize - 1;
478 if (i < layers)
479 ylocations[i] = ylocations[i - 1] + ypitch;
480 else
481 ylocations[i] = ysize - 1;
482 }
483 }
484
485 /* draw all the onion boxes. */
486
487 draw_onion (maze, xlocations, ylocations, layers);
488 make_doors (maze, xlocations, ylocations, layers, option);
489
490}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines