ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/random_maps/layout.C
Revision: 1.9
Committed: Fri Jul 2 17:18:04 2010 UTC (13 years, 10 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.8: +17 -2 lines
Log Message:
*** empty log message ***

File Contents

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