ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/random_maps/layout.C
Revision: 1.13
Committed: Sat Jul 3 01:52:52 2010 UTC (13 years, 10 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.12: +11 -14 lines
Log Message:
handle border in isolation remover wrapper

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