ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/random_maps/layout.C
Revision: 1.8
Committed: Fri Jul 2 15:43:37 2010 UTC (13 years, 11 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.7: +29 -24 lines
Log Message:
more useless perl accessors

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