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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines