ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/random_maps/layout.C
Revision: 1.17
Committed: Sat Jul 3 13:14:35 2010 UTC (13 years, 10 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.16: +47 -13 lines
Log Message:
*** empty log message ***

File Contents

# Content
1 /*
2 * This file is part of Deliantra, the Roguelike Realtime MMORPG.
3 *
4 * Copyright (©) 2010 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
5 * Copyright (©) Crossfire Development Team (restored, original file without copyright notice)
6 *
7 * Deliantra is free software: you can redistribute it and/or modify it under
8 * the terms of the Affero GNU General Public License as published by the
9 * Free Software Foundation, either version 3 of the License, or (at your
10 * option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the Affero GNU General Public License
18 * and the GNU General Public License along with this program. If not, see
19 * <http://www.gnu.org/licenses/>.
20 *
21 * The authors can be reached via e-mail to <support@deliantra.net>
22 */
23
24 #include <global.h>
25 #include <random_map.h>
26 #include <rproto.h>
27
28 void
29 layout::alloc (int w, int h)
30 {
31 assert (sizeof (cell) == 1);
32
33 this->w = w;
34 this->h = h;
35
36 // we store the layout in a single contiguous memory layout
37 // first part consists of pointers to each column, followed
38 // by the actual columns (not rows!)
39 size = (sizeof (cell *) + sizeof (cell) * h) * w;
40 data = (cell **)salloc<char> (size);
41
42 cell *p = (cell *)(data + w);
43
44 for (int x = 0; x < w; ++x)
45 data [x] = p + x * h;
46 }
47
48 layout::layout (int w, int h)
49 {
50 alloc (w, h);
51 }
52
53 layout::layout (layout &copy)
54 {
55 alloc (copy.w, copy.h);
56
57 memcpy (data [0], copy.data [0], sizeof (cell) * h * w);
58 }
59
60 layout::layout (layout &orig, int x1, int y1, int x2, int y2)
61 {
62 w = x2 - x1;
63 h = y2 - y1;
64
65 // we only allocate space for the pointers
66 size = sizeof (cell *) * w;
67 data = (cell **)salloc<char> (size);
68
69 // and now we point back into the original layout
70 for (int x = 0; x < w; ++x)
71 data [x] = orig.data [x + x1] + y1;
72 }
73
74 layout::~layout ()
75 {
76 sfree ((char *)data, size);
77 }
78
79 void
80 layout::fill (char fill)
81 {
82 //memset (data [0], fill, w * h); // only when contiguous :/
83 fill_rect (0, 0, w, h, fill);
84 }
85
86 void
87 layout::rect (int x1, int y1, int x2, int y2, char fill)
88 {
89 --x2;
90
91 memset (data [x1] + y1, fill, y2 - y1);
92 memset (data [x2] + y1, fill, y2 - y1);
93
94 while (++x1 < x2)
95 data [x1][y1] = data [x1][y2 - 1] = fill;
96 }
97
98 void
99 layout::fill_rect (int x1, int y1, int x2, int y2, char fill)
100 {
101 for (; x1 < x2; ++x1)
102 memset (data [x1] + y1, fill, y2 - y1);
103 }
104
105 void layout::border (char fill)
106 {
107 rect (0, 0, w, h, fill);
108 }
109
110 void
111 layout::fill_rand (int percent)
112 {
113 percent = lerp (percent, 0, 100, 0, 256);
114
115 for (int x = w - 1; --x > 0; )
116 for (int y = h - 1; --y > 0; )
117 data [x][y] = rmg_rndm (256) > percent ? 0 : '#';
118 }
119
120 /////////////////////////////////////////////////////////////////////////////
121
122 // erode by cellular automata
123 void
124 layout::erode_1_2 (int c1, int c2, int repeat)
125 {
126 layout neu (w, h);
127
128 while (repeat--)
129 {
130 for (int x = 0; x < w; ++x)
131 {
132 coroapi::cede_to_tick ();
133
134 for (int y = 0; y < h; ++y)
135 {
136 int n1 = 0, n2 = 0;
137
138 // a 5x5 area, dx, dy, distance (1 == <= 1, 0 <= 2)
139 static I8 dds[][3] = {
140 { -2, -1, 0 }, { -2, 0, 0 }, { -2, 1, 0 },
141 { -1, -2, 0 }, { -1, -1, 1 }, { -1, 0, 1 }, { -1, 1, 1 }, { -1, 2, 0 },
142 { 0, -2, 0 }, { 0, -1, 1 }, { 0, 0, 1 }, { 0, 1, 1 }, { 0, 2, 0 },
143 { 1, -2, 0 }, { 1, -1, 1 }, { 1, 0, 1 }, { 1, 1, 1 }, { 1, 2, 0 },
144 { 2, -1, 0 }, { 2, 0, 0 }, { 2, 1, 0 },
145 };
146
147 for (int i = array_length (dds); i--; )
148 {
149 int nx = x + dds [i][0];
150 int ny = y + dds [i][1];
151
152 if (!IN_RANGE_EXC (nx, 0, w) || !IN_RANGE_EXC (ny, 0, h) || !data [nx][ny])
153 {
154 n1 += dds [i][2];
155 n2++;
156 }
157 }
158
159 neu [x][y] = n1 >= c1 || n2 <= c2 ? '#' : 0;
160 }
161 }
162
163 swap (neu);
164 }
165 }
166
167 /////////////////////////////////////////////////////////////////////////////
168
169 void
170 layout::print () const
171 {
172 for (int y = 0; y < h; y++)
173 {
174 for (int x = 0; x < w; x++)
175 {
176 U8 c = (U8)data [x][y];
177
178 if (!c)
179 c = ' ';
180 else if (c < 10)
181 c += '0';
182 else if (c < 32)
183 c += 'a' - 10;
184
185 putc ((char)c, stdout);
186 }
187
188 putc ('\n', stdout);
189 }
190
191 putc ('\n', stdout);
192 }
193
194 /////////////////////////////////////////////////////////////////////////////
195 // isolation remover - ensures single connected area
196
197 typedef fixed_stack<point> pointlist;
198
199 static void noinline
200 push_flood_fill (layout &dist, pointlist &seeds, int x, int y)
201 {
202 if (dist [x][y])
203 return;
204
205 while (y > 0 && !dist [x][y - 1])
206 --y;
207
208 int y0 = y;
209
210 while (y < dist.h && !dist [x][y])
211 {
212 seeds.push (point (x, y));
213
214 dist [x][y] = 1;
215 ++y;
216 }
217
218 while (--y >= y0)
219 {
220 if (x > 0 && !dist [x - 1][y]) push_flood_fill (dist, seeds, x - 1, y);
221 if (x < dist.w - 1 && !dist [x + 1][y]) push_flood_fill (dist, seeds, x + 1, y);
222 }
223 }
224
225 static inline void
226 make_tunnel (layout &dist, pointlist &seeds, int x, int y, U8 d, int perturb)
227 {
228 for (;;)
229 {
230 point neigh[4];
231 int ncnt = 0;
232
233 d += perturb > 1;
234
235 if (x > 0 && U8 (dist [x - 1][y]) < d && dist [x - 1][y] > 1) neigh [ncnt++] = point (x - 1, y);
236 if (x < dist.w - 1 && U8 (dist [x + 1][y]) < d && dist [x + 1][y] > 1) neigh [ncnt++] = point (x + 1, y);
237 if (y > 0 && U8 (dist [x][y - 1]) < d && dist [x][y - 1] > 1) neigh [ncnt++] = point (x, y - 1);
238 if (y < dist.h - 1 && U8 (dist [x][y + 1]) < d && dist [x][y + 1] > 1) neigh [ncnt++] = point (x, y + 1);
239
240 if (!ncnt)
241 return;
242
243 point &p = neigh [perturb ? rmg_rndm (ncnt) : 0];
244
245 seeds.push (p);
246
247 x = p.x;
248 y = p.y;
249
250 d = dist [x][y];
251 dist [x][y] = 1;
252 }
253 }
254
255 static void inline
256 maybe_push (layout &dist, pointlist &seeds, int x, int y, U8 d)
257 {
258 char &D = dist [x][y];
259
260 if (U8 (D) > d) // if wall and higher distance, lower distance
261 D = d;
262 else if (D) // otherwise, if it's no room, this space is uninteresting
263 return;
264
265 seeds.push (point (x, y));
266 }
267
268 // isolation remover, works on a "distance" map
269 // the map must be initialised with 0 == rooms, 255 = walls
270 static void noinline
271 isolation_remover (layout &dist, unsigned int perturb = 2)
272 {
273 // dist contains
274 // 0 == invisited rooms
275 // 1 == visited rooms
276 // 2+ shortest distance to random near room
277
278 max_it (perturb, 0);
279 min_it (perturb, 2);
280
281 // phase 1, find seed
282 int cnt = 0;
283 int x, y;
284
285 for (int i = 0; i < dist.w; ++i)
286 for (int j = 0; j < dist.h; ++j)
287 if (!dist [i][j] && !rmg_rndm (++cnt))
288 x = i, y = j;
289
290 if (!cnt)
291 {
292 // map is completely massive, this is not good,
293 // so make it empty instead.
294 dist.fill (1);
295 return;
296 }
297
298 fixed_stack<point> seeds (dist.w * dist.h * 5);
299
300 // found first free space - picking the first one gives
301 // us a slight bias for tunnels, but usually you won't
302 // notice that in-game
303 seeds.push (point (x, y));
304
305 // phase 2, while we have seeds, if
306 // seed is empty, floodfill, else grow
307
308 while (seeds.size)
309 {
310 coroapi::cede_to_tick ();
311
312 point p = seeds.remove (rmg_rndm (seeds.size));
313
314 x = p.x;
315 y = p.y;
316
317 if (!dist [x][y])
318 {
319 // found new isolated area, make tunnel
320 push_flood_fill (dist, seeds, x, y);
321 make_tunnel (dist, seeds, x, y, 254, perturb);
322 }
323 else
324 {
325 // nothing here, continue to expand
326 U8 d = U8 (dist [x][y]) + 1;
327
328 if (x < dist.w - 1) maybe_push (dist, seeds, x + 1, y, d);
329 if (x > 0) maybe_push (dist, seeds, x - 1, y, d);
330 if (y < dist.h - 1) maybe_push (dist, seeds, x, y + 1, d);
331 if (y > 0) maybe_push (dist, seeds, x, y - 1, d);
332 }
333 }
334 }
335
336 void
337 layout::isolation_remover (int perturb)
338 {
339 layout dist (w - 2, h - 2); // map without border
340
341 for (int x = 1; x < w - 1; ++x)
342 for (int y = 1; y < h - 1; ++y)
343 dist [x - 1][y - 1] = data [x][y] == '#' ? U8 (255) : 0;
344
345 ::isolation_remover (dist, perturb);
346
347 // now copy the tunnels over
348 for (int x = 1; x < w - 1; ++x)
349 for (int y = 1; y < h - 1; ++y)
350 if (data [x][y] == '#' && dist [x - 1][y - 1] == 1)
351 data [x][y] = 0;
352 }
353
354 /////////////////////////////////////////////////////////////////////////////
355
356 //+GPL
357
358 /* puts doors at appropriate locations in a maze. */
359 void
360 layout::doorify ()
361 {
362 int ndoors = w * h / 60; /* reasonable number of doors. */
363 int doorlocs = 0; /* # of available doorlocations */
364
365 uint16 *doorlist_x = salloc<uint16> (w * h);
366 uint16 *doorlist_y = salloc<uint16> (w * h);
367
368 /* make a list of possible door locations */
369 for (int i = 1; i < w - 1; i++)
370 for (int j = 1; j < h - 1; j++)
371 {
372 int sindex = surround_flag (*this, i, j);
373
374 if (sindex == 3 || sindex == 12) /* these are possible door sindex */
375 {
376 doorlist_x [doorlocs] = i;
377 doorlist_y [doorlocs] = j;
378 doorlocs++;
379 }
380 }
381
382 while (ndoors > 0 && doorlocs > 0)
383 {
384 int di = rmg_rndm (doorlocs);
385 int i = doorlist_x [di];
386 int j = doorlist_y [di];
387 int sindex = surround_flag (*this, i, j);
388
389 if (sindex == 3 || sindex == 12) /* these are possible door sindex */
390 {
391 data [i][j] = 'D';
392 ndoors--;
393 }
394
395 /* reduce the size of the list */
396 doorlocs--;
397 doorlist_x[di] = doorlist_x [doorlocs];
398 doorlist_y[di] = doorlist_y [doorlocs];
399 }
400
401 sfree (doorlist_x, w * h);
402 sfree (doorlist_y, w * h);
403 }
404
405 /* takes a map and makes it symmetric: adjusts Xsize and
406 * Ysize to produce a symmetric map.
407 */
408 void
409 layout::symmetrize (int symmetry)
410 {
411 if (symmetry == SYMMETRY_NONE)
412 return;
413
414 layout sym_layout (
415 symmetry == SYMMETRY_X || symmetry == SYMMETRY_XY ? w * 2 - 3 : w,
416 symmetry == SYMMETRY_Y || symmetry == SYMMETRY_XY ? h * 2 - 3 : h
417 );
418
419 if (symmetry == SYMMETRY_X)
420 for (int i = 0; i < sym_layout.w / 2 + 1; i++)
421 for (int j = 0; j < sym_layout.h; j++)
422 {
423 sym_layout[i ][j] =
424 sym_layout[sym_layout.w - i - 1][j] = data [i][j];
425 }
426
427 if (symmetry == SYMMETRY_Y)
428 for (int i = 0; i < sym_layout.w; i++)
429 for (int j = 0; j < sym_layout.h / 2 + 1; j++)
430 {
431 sym_layout[i][j ] =
432 sym_layout[i][sym_layout.h - j - 1] = data [i][j];
433 }
434
435 if (symmetry == SYMMETRY_XY)
436 for (int i = 0; i < sym_layout.w / 2 + 1; i++)
437 for (int j = 0; j < sym_layout.h / 2 + 1; j++)
438 {
439 sym_layout[i ][j ] =
440 sym_layout[i ][sym_layout.h - j - 1] =
441 sym_layout[sym_layout.w - i - 1][j ] =
442 sym_layout[sym_layout.w - i - 1][sym_layout.h - j - 1] = data [i][j];
443 }
444
445 /* need to run the isolation remover for some layouts */
446 #if 0
447 switch (RP->map_layout_style)
448 {
449 case LAYOUT_ONION:
450 case LAYOUT_SNAKE:
451 case LAYOUT_SQUARE_SPIRAL:
452 // safe
453 break;
454
455 default:
456 sym_layout.isolation_remover ();
457 break;
458 }
459 #endif
460 sym_layout.isolation_remover ();
461
462 swap (sym_layout);
463 }
464
465 //-GPL
466
467 void
468 layout::rotate (int rotation)
469 {
470 switch (rotation & 3)
471 {
472 case 2: /* a reflection */
473 {
474 layout new_layout (w, h);
475
476 for (int i = 0; i < w; i++) /* copy a reflection back */
477 for (int j = 0; j < h; j++)
478 new_layout [i][j] = data [w - i - 1][h - j - 1];
479
480 swap (new_layout);
481 }
482 break;
483
484 case 1:
485 case 3:
486 {
487 layout new_layout (h, w);
488
489 if (rotation == 1) /* swap x and y */
490 for (int i = 0; i < w; i++)
491 for (int j = 0; j < h; j++)
492 new_layout [j][i] = data [i][j];
493
494 if (rotation == 3) /* swap x and y */
495 for (int i = 0; i < w; i++)
496 for (int j = 0; j < h; j++)
497 new_layout [j][i] = data [w - i - 1][h - j - 1];
498
499 swap (new_layout);
500 }
501 break;
502 }
503 }
504
505 /////////////////////////////////////////////////////////////////////////////
506
507 //+GPL
508
509 /*
510 * Expands a maze by 2x in each dimension.
511 * H. S. Teoh
512 */
513
514 /* Copy the old tile X into the new one at location (i*2, j*2) and
515 * fill up the rest of the 2x2 result with \0:
516 * X ---> X \0
517 * \0 \0
518 */
519 static void inline
520 expand_misc (layout &newlayout, int i, int j, layout &maze)
521 {
522 newlayout[i * 2 + rmg_rndm (1)][j * 2 + rmg_rndm (1)] = maze[i][j];
523 /* (Note: no need to reset rest of 2x2 area to \0 because calloc does that
524 * for us.) */
525 }
526
527 /* Returns a bitmap that represents which squares on the right and bottom
528 * edges of a square (i,j) match the given character:
529 * 1 match on (i+1, j)
530 * 2 match on (i, j+1)
531 * 4 match on (i+1, j+1)
532 * and the possible combinations thereof.
533 */
534 static int noinline
535 calc_pattern (char ch, layout &maze, int i, int j)
536 {
537 int pattern = 0;
538
539 if (i + 1 < maze.w && maze[i + 1][j] == ch)
540 pattern |= 1;
541
542 if (j + 1 < maze.h)
543 {
544 if (maze[i][j + 1] == ch)
545 pattern |= 2;
546
547 if (i + 1 < maze.w && maze[i + 1][j + 1] == ch)
548 pattern |= 4;
549 }
550
551 return pattern;
552 }
553
554 /* Expand a wall. This function will try to sensibly connect the resulting
555 * wall to adjacent wall squares, so that the result won't have disconnected
556 * walls.
557 */
558 static void inline
559 expand_wall (layout &newlayout, int i, int j, layout &maze)
560 {
561 int wall_pattern = calc_pattern ('#', maze, i, j);
562 int door_pattern = calc_pattern ('D', maze, i, j);
563 int both_pattern = wall_pattern | door_pattern;
564
565 newlayout[i * 2][j * 2] = '#';
566
567 if (i + 1 < maze.w)
568 {
569 if (both_pattern & 1)
570 { /* join walls/doors to the right */
571 /* newlayout[i*2+1][j*2] = '#'; */
572 newlayout[i * 2 + 1][j * 2] = maze[i + 1][j];
573 }
574 }
575
576 if (j + 1 < maze.h)
577 {
578 if (both_pattern & 2)
579 { /* join walls/doors to the bottom */
580 /* newlayout[i*2][j*2+1] = '#'; */
581 newlayout[i * 2][j * 2 + 1] = maze[i][j + 1];
582 }
583
584 if (wall_pattern == 7)
585 { /* if orig maze is a 2x2 wall block,
586 * we fill the result with walls. */
587 newlayout[i * 2 + 1][j * 2 + 1] = '#';
588 }
589 }
590 }
591
592 /* This function will try to sensibly connect doors so that they meet up with
593 * adjacent walls. Note that it will also presumptuously delete (ignore) doors
594 * that it doesn't know how to correctly expand.
595 */
596 static void inline
597 expand_door (layout &newlayout, int i, int j, layout &maze)
598 {
599 int wall_pattern = calc_pattern ('#', maze, i, j);
600 int door_pattern = calc_pattern ('D', maze, i, j);
601 int join_pattern;
602
603 /* Doors "like" to connect to walls more than other doors. If there is
604 * a wall and another door, this door will connect to the wall and
605 * disconnect from the other door. */
606 if (wall_pattern & 3)
607 join_pattern = wall_pattern;
608 else
609 join_pattern = door_pattern;
610
611 newlayout[i * 2][j * 2] = 'D';
612
613 if (i + 1 < maze.w)
614 if (join_pattern & 1)
615 /* there is a door/wall to the right */
616 newlayout[i * 2 + 1][j * 2] = 'D';
617
618 if (j + 1 < maze.h)
619 if (join_pattern & 2)
620 /* there is a door/wall below */
621 newlayout[i * 2][j * 2 + 1] = 'D';
622 }
623
624 void
625 layout::expand2x ()
626 {
627 layout new_layout (w * 2 - 1, h * 2 - 1);
628
629 new_layout.clear ();
630
631 for (int i = 0; i < w; i++)
632 for (int j = 0; j < h; j++)
633 switch (data [i][j])
634 {
635 case '#': expand_wall (new_layout, i, j, *this); break;
636 case 'D': expand_door (new_layout, i, j, *this); break;
637 default: expand_misc (new_layout, i, j, *this); break;
638 }
639
640 swap (new_layout);
641 }
642
643 /////////////////////////////////////////////////////////////////////////////
644
645 /* checks the maze to see if I can stick a horizontal(dir = 0) wall
646 (or vertical, dir == 1)
647 here which ends up on other walls sensibly. */
648 static int
649 can_make_wall (const layout &maze, int dx, int dy, int dir)
650 {
651 int i1;
652 int length = 0;
653
654 /* dont make walls if we're on the edge. */
655 if (dx == 0 || dx == (maze.w - 1) || dy == 0 || dy == (maze.h - 1))
656 return -1;
657
658 /* don't make walls if we're ON a wall. */
659 if (maze [dx][dy] != 0)
660 return -1;
661
662 if (dir == 0) /* horizontal */
663 {
664 int y = dy;
665
666 for (i1 = dx - 1; i1 > 0; i1--)
667 {
668 int sindex = surround_flag2 (maze, i1, y);
669
670 if (sindex == 1) break;
671 if (sindex != 0) return -1; /* can't make horiz. wall here */
672 if (maze[i1][y] != 0) return -1; /* can't make horiz. wall here */
673
674 length++;
675 }
676
677 for (i1 = dx + 1; i1 < maze.w - 1; i1++)
678 {
679 int sindex = surround_flag2 (maze, i1, y);
680
681 if (sindex == 2) break;
682 if (sindex != 0) return -1; /* can't make horiz. wall here */
683 if (maze[i1][y] != 0) return -1; /* can't make horiz. wall here */
684
685 length++;
686 }
687 return length;
688 }
689 else
690 { /* vertical */
691 int x = dx;
692
693 for (i1 = dy - 1; i1 > 0; i1--)
694 {
695 int sindex = surround_flag2 (maze, x, i1);
696
697 if (sindex == 4) break;
698 if (sindex != 0) return -1; /* can't make vert. wall here */
699 if (maze[x][i1] != 0) return -1; /* can't make horiz. wall here */
700
701 length++;
702 }
703
704 for (i1 = dy + 1; i1 < maze.h - 1; i1++)
705 {
706 int sindex = surround_flag2 (maze, x, i1);
707
708 if (sindex == 8) break;
709 if (sindex != 0) return -1; /* can't make verti. wall here */
710 if (maze[x][i1] != 0) return -1; /* can't make horiz. wall here */
711
712 length++;
713 }
714
715 return length;
716 }
717
718 return -1;
719 }
720
721 int
722 make_wall (char **maze, int x, int y, int dir)
723 {
724 maze[x][y] = 'D'; /* mark a door */
725
726 switch (dir)
727 {
728 case 0: /* horizontal */
729 {
730 for (int i1 = x - 1; maze[i1][y] == 0; --i1) maze[i1][y] = '#';
731 for (int i1 = x + 1; maze[i1][y] == 0; ++i1) maze[i1][y] = '#';
732 break;
733 }
734 case 1: /* vertical */
735 {
736 for (int i1 = y - 1; maze[x][i1] == 0; --i1) maze[x][i1] = '#';
737 for (int i1 = y + 1; maze[x][i1] == 0; ++i1) maze[x][i1] = '#';
738 break;
739 }
740 }
741
742 return 0;
743 }
744
745 void
746 layout::roomify ()
747 {
748 int tries = w * h / 30;
749
750 for (int ti = 0; ti < tries; ti++)
751 {
752 /* starting location for looking at creating a door */
753 int dx = rmg_rndm (w);
754 int dy = rmg_rndm (h);
755
756 /* results of checking on creating walls. */
757 int cx = can_make_wall (*this, dx, dy, 0); /* horizontal */
758 int cy = can_make_wall (*this, dx, dy, 1); /* vertical */
759
760 if (cx == -1)
761 {
762 if (cy != -1)
763 make_wall (*this, dx, dy, 1);
764
765 continue;
766 }
767
768 if (cy == -1)
769 {
770 make_wall (*this, dx, dy, 0);
771 continue;
772 }
773
774 if (cx < cy)
775 make_wall (*this, dx, dy, 0);
776 else
777 make_wall (*this, dx, dy, 1);
778 }
779 }
780
781 /////////////////////////////////////////////////////////////////////////////
782
783 // inspired mostly by http://www.jimrandomh.org/misc/caves.txt
784 void
785 layout::gen_cave (int subtype)
786 {
787 switch (subtype)
788 {
789 // a rough cave
790 case 0:
791 fill_rand (rmg_rndm (85, 97));
792 break;
793
794 // corridors
795 case 1:
796 fill_rand (rmg_rndm (5, 40));
797 erode_1_2 (5, 2, 10);
798 erode_1_2 (5, -1, 10);
799 erode_1_2 (5, 2, 1);
800 break;
801
802 // somewhat open, roundish
803 case 2:
804 fill_rand (45);
805 erode_1_2 (5, 0, 5);
806 erode_1_2 (5, 1, 1);
807 break;
808
809 // wide open, some room-like structures
810 case 3:
811 fill_rand (45);
812 erode_1_2 (5, 2, 4);
813 erode_1_2 (5, -1, 3);
814 break;
815 }
816
817 border ();
818 isolation_remover ();
819 }
820
821 void
822 layout::gen_castle ()
823 {
824 fill ('#');
825
826 for (int n = w * h / 30 + 1; n--; )
827 {
828 int rw = rmg_rndm (6, 10);
829 int rh = rmg_rndm (6, 10);
830
831 int rx = rmg_rndm (0, w - rw);
832 int ry = rmg_rndm (0, h - rh);
833
834 rect (rx, ry, rx + rw, ry + rh, '#');
835 fill_rect (rx + 1, ry + 1, rx + rw - 1, ry + rh - 1, 0);
836 }
837
838 border ();
839 isolation_remover (0);
840 }
841
842 static void
843 gen_mixed_ (layout &maze, random_map_params *RP, int dir)
844 {
845 if (maze.w < 20 && maze.h < 20 && !rmg_rndm (3))
846 dir = 2; // stop recursion randomly
847
848 if (dir == 0 && maze.w > 16)
849 {
850 int m = rmg_rndm (8, maze.w - 8);
851
852 layout m1 (maze, 0, 0, m , maze.h); gen_mixed_ (m1, RP, !dir);
853 layout m2 (maze, m, 0, maze.w, maze.h); gen_mixed_ (m2, RP, !dir);
854 }
855 else if (dir == 1 && maze.h > 16)
856 {
857 int m = rmg_rndm (8, maze.h - 8);
858
859 layout m1 (maze, 0, 0, maze.w, m ); gen_mixed_ (m1, RP, !dir);
860 layout m2 (maze, 0, m, maze.w, maze.h); gen_mixed_ (m2, RP, !dir);
861 }
862 else
863 {
864 RP->map_layout_style = rmg_rndm (NROFLAYOUTS - 2) + 1;
865
866 if (RP->map_layout_style == LAYOUT_MULTIPLE)
867 ++RP->map_layout_style;
868
869 maze.generate (RP);
870 }
871 }
872
873 // recursive subdivision with random sublayouts
874 static void
875 gen_mixed (layout &maze, random_map_params *RP)
876 {
877 random_map_params &rp = *new random_map_params (RP);
878 gen_mixed_ (maze, &rp, rmg_rndm (2));
879 delete &rp;
880
881 maze.border ();
882 maze.isolation_remover ();
883 }
884
885 /* function selects the maze function and gives it whatever
886 arguments it needs. */
887 void
888 layout::generate (random_map_params *RP)
889 {
890 switch (RP->map_layout_style)
891 {
892 case LAYOUT_ONION:
893 map_gen_onion (*this, RP->layoutoptions1, RP->layoutoptions2);
894
895 if (!(rmg_rndm (3)) && !(RP->layoutoptions1 & (RMOPT_WALLS_ONLY | RMOPT_WALL_OFF)))
896 roomify ();
897
898 break;
899
900 case LAYOUT_MAZE:
901 maze_gen (*this, RP->get_iv ("maze_type", rmg_rndm (4)));
902
903 if (rmg_rndm (2))
904 doorify ();
905
906 break;
907
908 case LAYOUT_SPIRAL:
909 map_gen_spiral (*this, RP->layoutoptions1);
910
911 if (rmg_rndm (2))
912 doorify ();
913
914 break;
915
916 case LAYOUT_ROGUELIKE:
917 /* Don't put symmetry in rogue maps. There isn't much reason to
918 * do so in the first place (doesn't make it any more interesting),
919 * but more importantly, the symmetry code presumes we are symmetrizing
920 * spirals, or maps with lots of passages - making a symmetric rogue
921 * map fails because its likely that the passages the symmetry process
922 * creates may not connect the rooms.
923 */
924 RP->symmetry_used = SYMMETRY_NONE;
925 roguelike_layout_gen (*this, RP->layoutoptions1);
926 /* no doorifying... done already */
927 break;
928
929 case LAYOUT_SNAKE:
930 make_snake_layout (*this, RP->layoutoptions1);
931
932 if (rmg_rndm (2))
933 roomify ();
934
935 break;
936
937 case LAYOUT_SQUARE_SPIRAL:
938 make_square_spiral_layout (*this, RP->layoutoptions1);
939
940 if (rmg_rndm (2))
941 roomify ();
942
943 break;
944
945 case LAYOUT_CAVE:
946 gen_cave (RP->get_iv ("cave_type", rmg_rndm (4)));
947
948 if (rmg_rndm (2))
949 doorify ();
950
951 break;
952
953 case LAYOUT_CASTLE:
954 gen_castle ();
955
956 if (rmg_rndm (2))
957 doorify ();
958
959 break;
960
961 case LAYOUT_MULTIPLE:
962 gen_mixed (*this, RP);
963 break;
964
965 default:
966 abort ();
967 }
968 }
969
970 //-GPL
971
972 #if 0
973 static struct demo
974 {
975 demo ()
976 {
977 rmg_rndm.seed (time (0));
978
979 for(int i=1;i<100;i++)
980 {
981 layout maze (40, 25);
982 maze.gen_castle ();
983 maze.doorify ();
984 maze.print ();
985 exit(0);
986 }
987
988 exit (1);
989 }
990 } demo;
991 #endif