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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines