ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/random_maps/layout.C
(Generate patch)

Comparing deliantra/server/random_maps/layout.C (file contents):
Revision 1.1 by root, Wed Jun 30 20:51:02 2010 UTC vs.
Revision 1.35 by root, Sat Dec 1 20:22:13 2018 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines