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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines