ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/random_maps/room_gen_onion.C
Revision: 1.13
Committed: Fri Apr 11 21:09:53 2008 UTC (16 years, 2 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.12: +46 -42 lines
Log Message:
*** empty log message ***

File Contents

# Content
1 /*
2 * This file is part of Deliantra, the Roguelike Realtime MMORPG.
3 *
4 * Copyright (©) 2005,2006,2007,2008 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
5 * Copyright (©) 2001,2007 Mark Wedel & Crossfire Development Team
6 * Copyright (©) 1992,2007 Frank Tore Johansen
7 *
8 * Deliantra is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 *
21 * The authors can be reached via e-mail to <support@deliantra.net>
22 */
23
24 /* The onion room generator:
25 Onion rooms are like this:
26
27 char **map_gen_onion(int xsize, int ysize, int option, int layers);
28
29 like this:
30 regular random
31 centered, linear onion bottom/right centered, nonlinear
32
33 ######################### #########################
34 # # # #
35 # ######## ########## # # #####################
36 # # # # # # #
37 # # ###### ######## # # # # #
38 # # # # # # # # ######## ########
39 # # # #### ###### # # # # # # #
40 # # # # # # # # # # # #
41 # # # ############ # # # # # # ########### ##
42 # # # # # # # # # # #
43 # # ################ # # # # # # #########
44 # # # # # # # # #
45 # #################### # # # # # #
46 # # # # # # # #
47 ######################### #########################
48
49 */
50
51
52 #include <global.h>
53 #include <random_map.h>
54
55 #ifndef MIN
56 # define MIN(x,y) (((x)<(y))? (x):(y))
57 #endif
58 void centered_onion (char **maze, int xsize, int ysize, int option, int layers);
59 void bottom_centered_onion (char **maze, int xsize, int ysize, int option, int layers);
60 void bottom_right_centered_onion (char **maze, int xsize, int ysize, int option, int layers);
61
62 void draw_onion (char **maze, float *xlocations, float *ylocations, int layers);
63 void make_doors (char **maze, float *xlocations, float *ylocations, int layers, int options);
64
65 Maze
66 map_gen_onion (int xsize, int ysize, int option, int layers)
67 {
68 int i, j;
69
70 Maze maze (xsize, ysize);
71
72 /* pick some random options if option = 0 */
73 if (option == 0)
74 {
75 switch (rndm (3))
76 {
77 case 0:
78 option |= RMOPT_CENTERED;
79 break;
80 case 1:
81 option |= RMOPT_BOTTOM_C;
82 break;
83 case 2:
84 option |= RMOPT_BOTTOM_R;
85 break;
86 }
87
88 if (rndm (2)) option |= RMOPT_LINEAR;
89 if (rndm (2)) option |= RMOPT_IRR_SPACE;
90 }
91
92 /* write the outer walls, if appropriate. */
93 if (!(option & RMOPT_WALL_OFF))
94 {
95 for (i = 0; i < xsize; i++) maze[i][0] = maze[i][ysize - 1] = '#';
96 for (j = 0; j < ysize; j++) maze[0][j] = maze[xsize - 1][j] = '#';
97 };
98
99 if (option & RMOPT_WALLS_ONLY)
100 return maze;
101
102 /* pick off the mutually exclusive options */
103 if (option & RMOPT_BOTTOM_R)
104 bottom_right_centered_onion (maze, xsize, ysize, option, layers);
105 else if (option & RMOPT_BOTTOM_C)
106 bottom_centered_onion (maze, xsize, ysize, option, layers);
107 else if (option & RMOPT_CENTERED)
108 centered_onion (maze, xsize, ysize, option, layers);
109
110 return maze;
111 }
112
113 void
114 centered_onion (char **maze, int xsize, int ysize, int option, int layers)
115 {
116 int i, maxlayers;
117
118 maxlayers = (MIN (xsize, ysize) - 2) / 5;
119
120 if (!maxlayers)
121 return; /* map too small to onionize */
122
123 if (layers > maxlayers)
124 layers = maxlayers;
125
126 if (layers == 0)
127 layers = rndm (maxlayers) + 1;
128
129 float *xlocations = salloc0<float> (2 * layers);
130 float *ylocations = salloc0<float> (2 * layers);
131
132 /* place all the walls */
133 if (option & RMOPT_IRR_SPACE) /* randomly spaced */
134 {
135 int x_spaces_available, y_spaces_available;
136
137 /* the "extra" spaces available for spacing between layers */
138 x_spaces_available = (xsize - 2) - 6 * layers + 1;
139 y_spaces_available = (ysize - 2) - 6 * layers + 1;
140
141 /* pick an initial random pitch */
142 for (i = 0; i < 2 * layers; i++)
143 {
144 float xpitch = 2, ypitch = 2;
145
146 if (x_spaces_available > 0)
147 xpitch = 2 + (rndm (x_spaces_available) + rndm (x_spaces_available) + rndm (x_spaces_available)) / 3;
148
149 if (y_spaces_available > 0)
150 ypitch = 2 + (rndm (y_spaces_available) + rndm (y_spaces_available) + rndm (y_spaces_available)) / 3;
151
152 xlocations[i] = ((i > 0) ? xlocations[i - 1] : 0) + xpitch;
153 ylocations[i] = ((i > 0) ? ylocations[i - 1] : 0) + ypitch;
154 x_spaces_available -= (int) (xpitch - 2);
155 y_spaces_available -= (int) (ypitch - 2);
156 }
157
158 }
159
160 if (!(option & RMOPT_IRR_SPACE))
161 { /* evenly spaced */
162 float xpitch, ypitch; /* pitch of the onion layers */
163
164 xpitch = (xsize - 2.0) / (2.0 * layers + 1);
165 ypitch = (ysize - 2.0) / (2.0 * layers + 1);
166
167 xlocations[0] = xpitch;
168 ylocations[0] = ypitch;
169
170 for (i = 1; i < 2 * layers; i++)
171 {
172 xlocations[i] = xlocations[i - 1] + xpitch;
173 ylocations[i] = ylocations[i - 1] + ypitch;
174 }
175 }
176
177 /* draw all the onion boxes. */
178 draw_onion (maze, xlocations, ylocations, layers);
179 make_doors (maze, xlocations, ylocations, layers, option);
180
181 sfree (xlocations, 2 * layers);
182 sfree (ylocations, 2 * layers);
183 }
184
185 void
186 bottom_centered_onion (char **maze, int xsize, int ysize, int option, int layers)
187 {
188 int i, maxlayers;
189
190 maxlayers = (MIN (xsize, ysize) - 2) / 5;
191
192 if (!maxlayers)
193 return; /* map too small to onionize */
194
195 if (layers > maxlayers)
196 layers = maxlayers;
197
198 if (layers == 0)
199 layers = rndm (maxlayers) + 1;
200
201 float *xlocations = salloc0<float> (2 * layers);
202 float *ylocations = salloc0<float> (2 * layers);
203
204 /* place all the walls */
205 if (option & RMOPT_IRR_SPACE) /* randomly spaced */
206 {
207 int x_spaces_available, y_spaces_available;
208
209 /* the "extra" spaces available for spacing between layers */
210 x_spaces_available = (xsize - 2) - 6 * layers + 1;
211 y_spaces_available = (ysize - 2) - 3 * layers + 1;
212
213 /* pick an initial random pitch */
214 for (i = 0; i < 2 * layers; i++)
215 {
216 float xpitch = 2, ypitch = 2;
217
218 if (x_spaces_available > 0)
219 xpitch = 2 + (rndm (x_spaces_available) + rndm (x_spaces_available) + rndm (x_spaces_available)) / 3;
220
221 if (y_spaces_available > 0)
222 ypitch = 2 + (rndm (y_spaces_available) + rndm (y_spaces_available) + rndm (y_spaces_available)) / 3;
223
224 xlocations[i] = ((i > 0) ? xlocations[i - 1] : 0) + xpitch;
225
226 if (i < layers)
227 ylocations[i] = ((i > 0) ? ylocations[i - 1] : 0) + ypitch;
228 else
229 ylocations[i] = ysize - 1;
230
231 x_spaces_available -= (int) (xpitch - 2);
232 y_spaces_available -= (int) (ypitch - 2);
233 }
234 }
235
236 if (!(option & RMOPT_IRR_SPACE))
237 { /* evenly spaced */
238 float xpitch, ypitch; /* pitch of the onion layers */
239
240 xpitch = (xsize - 2.0) / (2.0 * layers + 1);
241 ypitch = (ysize - 2.0) / (layers + 1);
242
243 xlocations[0] = xpitch;
244 ylocations[0] = ypitch;
245
246 for (i = 1; i < 2 * layers; i++)
247 {
248 xlocations[i] = xlocations[i - 1] + xpitch;
249
250 if (i < layers)
251 ylocations[i] = ylocations[i - 1] + ypitch;
252 else
253 ylocations[i] = ysize - 1;
254 }
255 }
256
257 /* draw all the onion boxes. */
258
259 draw_onion (maze, xlocations, ylocations, layers);
260 make_doors (maze, xlocations, ylocations, layers, option);
261
262 sfree (xlocations, 2 * layers);
263 sfree (ylocations, 2 * layers);
264 }
265
266 /* draw_boxes: draws the lines in the maze defining the onion layers */
267 void
268 draw_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
297 void
298 make_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
306 if (options & RMOPT_BOTTOM_C)
307 freedoms = 3;
308
309 if (options & RMOPT_BOTTOM_R)
310 freedoms = 2;
311
312 if (layers <= 0)
313 return;
314
315 /* pick which wall will have a door. */
316 which_wall = rndm (freedoms) + 1;
317 for (l = 0; l < layers; l++)
318 {
319 if (options & RMOPT_LINEAR)
320 { /* linear door placement. */
321 switch (which_wall)
322 {
323 case 1:
324 { /* left hand wall */
325 x1 = (int) xlocations[l];
326 y1 = (int) ((ylocations[l] + ylocations[2 * layers - l - 1]) / 2);
327 break;
328 }
329 case 2:
330 { /* top wall placement */
331 x1 = (int) ((xlocations[l] + xlocations[2 * layers - l - 1]) / 2);
332 y1 = (int) ylocations[l];
333 break;
334 }
335 case 3:
336 { /* right wall placement */
337 x1 = (int) xlocations[2 * layers - l - 1];
338 y1 = (int) ((ylocations[l] + ylocations[2 * layers - l - 1]) / 2);
339 break;
340 }
341 case 4:
342 { /* bottom wall placement */
343 x1 = (int) ((xlocations[l] + xlocations[2 * layers - l - 1]) / 2);
344 y1 = (int) ylocations[2 * layers - l - 1];
345 break;
346 }
347 }
348 }
349 else
350 { /* random door placement. */
351 which_wall = rndm (freedoms) + 1;
352 switch (which_wall)
353 {
354 case 1:
355 { /* left hand wall */
356 x1 = (int) xlocations[l];
357 y2 = (int) (ylocations[2 * layers - l - 1] - ylocations[l] - 1);
358 if (y2 > 0)
359 y1 = (int) (ylocations[l] + rndm (y2) + 1);
360 else
361 y1 = (int) (ylocations[l] + 1);
362 break;
363 }
364 case 2:
365 { /* top wall placement */
366 x2 = (int) ((-xlocations[l] + xlocations[2 * layers - l - 1])) - 1;
367 if (x2 > 0)
368 x1 = (int) (xlocations[l] + rndm (x2) + 1);
369 else
370 x1 = (int) (xlocations[l] + 1);
371 y1 = (int) ylocations[l];
372 break;
373 }
374 case 3:
375 { /* right wall placement */
376 x1 = (int) xlocations[2 * layers - l - 1];
377 y2 = (int) ((-ylocations[l] + ylocations[2 * layers - l - 1])) - 1;
378 if (y2 > 0)
379 y1 = (int) (ylocations[l] + rndm (y2) + 1);
380 else
381 y1 = (int) (ylocations[l] + 1);
382
383 break;
384 }
385 case 4:
386 { /* bottom wall placement */
387 x2 = (int) ((-xlocations[l] + xlocations[2 * layers - l - 1])) - 1;
388 if (x2 > 0)
389 x1 = (int) (xlocations[l] + rndm (x2) + 1);
390 else
391 x1 = (int) (xlocations[l] + 1);
392 y1 = (int) ylocations[2 * layers - l - 1];
393 break;
394 }
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
410 maze[x1][y1] = 'C';
411 }
412
413 void
414 bottom_right_centered_onion (char **maze, int xsize, int ysize, int option, int layers)
415 {
416 int i, maxlayers;
417
418 maxlayers = (MIN (xsize, ysize) - 2) / 5;
419
420 if (!maxlayers)
421 return; /* map too small to onionize */
422
423 if (layers > maxlayers)
424 layers = maxlayers;
425
426 if (layers == 0)
427 layers = rndm (maxlayers) + 1;
428
429 float *xlocations = salloc0<float> (2 * layers);
430 float *ylocations = salloc0<float> (2 * layers);
431
432 /* place all the walls */
433 if (option & RMOPT_IRR_SPACE) /* randomly spaced */
434 {
435 int x_spaces_available, y_spaces_available;
436
437 /* the "extra" spaces available for spacing between layers */
438 x_spaces_available = (xsize - 2) - 3 * layers + 1;
439 y_spaces_available = (ysize - 2) - 3 * layers + 1;
440
441
442 /* pick an initial random pitch */
443 for (i = 0; i < 2 * layers; i++)
444 {
445 float xpitch = 2, ypitch = 2;
446
447 if (x_spaces_available > 0)
448 xpitch = 2 + (rndm (x_spaces_available) + rndm (x_spaces_available) + rndm (x_spaces_available)) / 3;
449
450 if (y_spaces_available > 0)
451 ypitch = 2 + (rndm (y_spaces_available) + rndm (y_spaces_available) + rndm (y_spaces_available)) / 3;
452
453 if (i < layers)
454 xlocations[i] = ((i > 0) ? xlocations[i - 1] : 0) + xpitch;
455 else
456 xlocations[i] = xsize - 1;
457
458 if (i < layers)
459 ylocations[i] = ((i > 0) ? ylocations[i - 1] : 0) + ypitch;
460 else
461 ylocations[i] = ysize - 1;
462
463 x_spaces_available -= (int) (xpitch - 2);
464 y_spaces_available -= (int) (ypitch - 2);
465 }
466 }
467
468 if (!(option & RMOPT_IRR_SPACE))
469 { /* evenly spaced */
470 float xpitch, ypitch; /* pitch of the onion layers */
471
472 xpitch = (xsize - 2.0) / (2.0 * layers + 1);
473 ypitch = (ysize - 2.0) / (layers + 1);
474
475 xlocations[0] = xpitch;
476 ylocations[0] = ypitch;
477
478 for (i = 1; i < 2 * layers; i++)
479 {
480 if (i < layers)
481 xlocations[i] = xlocations[i - 1] + xpitch;
482 else
483 xlocations[i] = xsize - 1;
484
485 if (i < layers)
486 ylocations[i] = ylocations[i - 1] + ypitch;
487 else
488 ylocations[i] = ysize - 1;
489 }
490 }
491
492 /* draw all the onion boxes. */
493
494 draw_onion (maze, xlocations, ylocations, layers);
495 make_doors (maze, xlocations, ylocations, layers, option);
496
497 sfree (xlocations, 2 * layers);
498 sfree (ylocations, 2 * layers);
499 }
500