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