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.5 by root, Fri Jul 2 03:40:14 2010 UTC vs.
Revision 1.12 by root, Sat Jul 3 01:49:18 2010 UTC

23 23
24#include <global.h> 24#include <global.h>
25#include <random_map.h> 25#include <random_map.h>
26#include <rproto.h> 26#include <rproto.h>
27 27
28void
28Layout::Layout (int w, int h) 29layout::alloc (int w, int h)
29: w(w), h(h)
30{ 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!)
31 int size = (sizeof (char *) + sizeof (char) * h) * w; 39 int size = (sizeof (cell *) + sizeof (cell) * h) * w;
32 40
33 col = (char **)salloc<char> (size); 41 data = (cell **)salloc<char> (size);
34 42
35 char *data = (char *)(col + w); 43 cell *p = (cell *)(data + w);
36 44
37 for (int x = w; x--; ) 45 for (int x = w; x--; )
38 col [x] = data + x * h; 46 data [x] = p + x * h;
39} 47}
40 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
41Layout::~Layout () 61layout::~layout ()
42{ 62{
43 int size = (sizeof (char *) + sizeof (char) * h) * w; 63 int size = (sizeof (cell *) + sizeof (cell) * h) * w;
44 64
45 sfree ((char *)col, size); 65 sfree ((char *)data, size);
46} 66}
47 67
48void 68void
49Layout::fill (char fill) 69layout::fill (char fill)
50{ 70{
51 memset (col [0], fill, w * h); 71 memset (data [0], fill, w * h);
52} 72}
53 73
54void 74void
55Layout::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)
56{ 88{
57 for (; x1 < x2; ++x1) 89 for (; x1 < x2; ++x1)
58 memset (col [x1] + y1, fill, y2 - y1); 90 memset (data [x1] + y1, fill, y2 - y1);
59} 91}
60 92
61void Layout::border (char fill) 93void layout::border (char fill)
62{ 94{
63 for (int i = 0; i < w; i++) col [i][0] = col [i][h - 1] = fill; 95 rect (0, 0, w, h, fill);
64 for (int j = 0; j < h; j++) col [0][j] = col [w - 1][j] = fill;
65} 96}
66 97
67void 98void
68Layout::fill_rand (int percent) 99layout::fill_rand (int percent)
69{ 100{
70 percent = lerp (percent, 0, 100, 0, 256); 101 percent = lerp (percent, 0, 100, 0, 256);
71 102
72 for (int x = w - 1; --x > 0; ) 103 for (int x = w - 1; --x > 0; )
73 for (int y = h - 1; --y > 0; ) 104 for (int y = h - 1; --y > 0; )
74 col [x][y] = rmg_rndm (256) > percent ? 0 : '#'; 105 data [x][y] = rmg_rndm (256) > percent ? 0 : '#';
75} 106}
76 107
77///////////////////////////////////////////////////////////////////////////// 108/////////////////////////////////////////////////////////////////////////////
78 109
79// erode by cellular automata 110// erode by cellular automata
80void 111void
81Layout::erode_1_2 (int c1, int c2, int repeat) 112layout::erode_1_2 (int c1, int c2, int repeat)
82{ 113{
83 Layout neu (w, h); 114 layout neu (w, h);
84 115
85 while (repeat--) 116 while (repeat--)
86 { 117 {
87 for (int x = 0; x < w; ++x) 118 for (int x = 0; x < w; ++x)
88 { 119 {
104 for (int i = array_length (dds); i--; ) 135 for (int i = array_length (dds); i--; )
105 { 136 {
106 int nx = x + dds [i][0]; 137 int nx = x + dds [i][0];
107 int ny = y + dds [i][1]; 138 int ny = y + dds [i][1];
108 139
109 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])
110 { 141 {
111 n1 += dds [i][2]; 142 n1 += dds [i][2];
112 n2++; 143 n2++;
113 } 144 }
114 } 145 }
122} 153}
123 154
124///////////////////////////////////////////////////////////////////////////// 155/////////////////////////////////////////////////////////////////////////////
125 156
126void 157void
127Layout::print () const 158layout::print () const
128{ 159{
129 for (int y = 0; y < h; y++) 160 for (int y = 0; y < h; y++)
130 { 161 {
131 for (int x = 0; x < w; x++) 162 for (int x = 0; x < w; x++)
132 { 163 {
133 U8 c = (U8)col [x][y]; 164 U8 c = (U8)data [x][y];
134 165
135 if (!c) 166 if (!c)
136 c = ' '; 167 c = ' ';
137 else if (c < 10) 168 else if (c < 10)
138 c += '0'; 169 c += '0';
151///////////////////////////////////////////////////////////////////////////// 182/////////////////////////////////////////////////////////////////////////////
152// isolation remover - ensures single connected area 183// isolation remover - ensures single connected area
153 184
154typedef fixed_stack<point> pointlist; 185typedef fixed_stack<point> pointlist;
155 186
156static noinline void 187static void noinline
157push_flood_fill (Layout &dist, pointlist &seeds, int x, int y) 188push_flood_fill (layout &dist, pointlist &seeds, int x, int y)
158{ 189{
159 if (dist [x][y]) 190 if (dist [x][y])
160 return; 191 return;
161 192
162 while (y > 0 && !dist [x][y - 1]) 193 while (y > 0 && !dist [x][y - 1])
172 ++y; 203 ++y;
173 } 204 }
174 205
175 while (--y >= y0) 206 while (--y >= y0)
176 { 207 {
177 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);
178 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);
179 } 210 }
180} 211}
181 212
182static inline void 213static inline void
183make_tunnel (Layout &dist, pointlist &seeds, int x, int y, U8 d) 214make_tunnel (layout &dist, pointlist &seeds, int x, int y, U8 d)
184{ 215{
185 for (;;) 216 for (;;)
186 { 217 {
218 point neigh[4];
219 int ncnt = 0;
220
187 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);
188 --x;
189 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);
190 ++x;
191 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);
192 --y;
193 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);
194 ++y; 225
195 else 226 printf ("tunnel %d+%d ncnt %d\n", x, y, ncnt);//D
227
228 if (!ncnt)
196 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;
197 237
198 d = dist [x][y]; 238 d = dist [x][y];
199 dist [x][y] = 1; 239 dist [x][y] = 1;
200 seeds.push (point (x, y));
201 } 240 }
202} 241}
203 242
204static void inline 243static void inline
205maybe_push (Layout &dist, pointlist &seeds, int x, int y, U8 d) 244maybe_push (layout &dist, pointlist &seeds, int x, int y, U8 d)
206{ 245{
207 char &D = dist [x][y]; 246 char &D = dist [x][y];
208 247
209 if (U8 (D) > d) // if wall and higher distance, lower distance 248 if (U8 (D) > d) // if wall and higher distance, lower distance
210 D = d; 249 D = d;
212 return; 251 return;
213 252
214 seeds.push (point (x, y)); 253 seeds.push (point (x, y));
215} 254}
216 255
217static void 256// isolation remover, works on a "distance" map
257// the map must be initialised with 0 == rooms, 255 = walls
258static void noinline
218isolation_remover (Layout &maze, bool dirty) 259isolation_remover (layout &dist)
219{ 260{
220 Layout dist (maze.w, maze.h);
221
222 // dist contains 261 // dist contains
223 // 0 == invisited rooms 262 // 0 == invisited rooms
224 // 1 == visited rooms 263 // 1 == visited rooms
225 // 2+ shortest distance to random near room 264 // 2+ shortest distance to random near room
226 265
227 // phase 1, initialise dist array, find seed 266 // phase 1, find seed
228 int cnt = 0; 267 int cnt = 0;
229 int x, y; 268 int x, y;
230 269
231 for (int i = 0; i < maze.w; ++i) 270 for (int i = 0; i < dist.w; ++i)
232 for (int j = 0; j < maze.h; ++j) 271 for (int j = 0; j < dist.h; ++j)
233 { 272 if (!dist [i][j] && !rmg_rndm (++cnt))
234 if (maze [i][j] == '#')
235 dist [i][j] = U8 (255);
236 else
237 {
238 dist [i][j] = 0;
239 if (!rmg_rndm (++cnt))
240 x = i, y = j; 273 x = i, y = j;
241 }
242 }
243 274
244 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);
245 return; 281 return;
282 }
246 283
247 fixed_stack<point> seeds (maze.w * maze.h * 5); 284 fixed_stack<point> seeds (dist.w * dist.h * 5);
248 285
249 // found first free space - picking the first one gives 286 // found first free space - picking the first one gives
250 // us a slight bias for tunnels, but usually you won't 287 // us a slight bias for tunnels, but usually you won't
251 // notice that in-game 288 // notice that in-game
252 seeds.push (point (x, y)); 289 seeds.push (point (x, y));
264 y = p.y; 301 y = p.y;
265 302
266 if (!dist [x][y]) 303 if (!dist [x][y])
267 { 304 {
268 // found new isolated area, make tunnel 305 // found new isolated area, make tunnel
269 if (!dirty)
270 push_flood_fill (dist, seeds, x, y); 306 push_flood_fill (dist, seeds, x, y);
271
272 make_tunnel (dist, seeds, x, y, 255); 307 make_tunnel (dist, seeds, x, y, 255);
273
274 if (dirty)
275 push_flood_fill (dist, seeds, x, y);
276 } 308 }
277 else 309 else
278 { 310 {
279 // nothing here, continue to expand 311 // nothing here, continue to expand
280 U8 d = U8 (dist [x][y]) + 1; 312 U8 d = U8 (dist [x][y]) + 1;
281 313
282 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);
283 if (x > 0) maybe_push (dist, seeds, x - 1, y, d); 315 if (x > 1) maybe_push (dist, seeds, x - 1, y, d);
284 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);
285 if (y > 0) maybe_push (dist, seeds, x, y - 1, d); 317 if (y > 1) maybe_push (dist, seeds, x, y - 1, d);
286 } 318 }
287 } 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);
288 332
289 // now copy the tunnels over 333 // now copy the tunnels over
290 for (int x = 0; x < maze.w; ++x) 334 for (int x = 1; x < w - 1; ++x)
291 for (int y = 0; y < maze.h; ++y) 335 for (int y = 1; y < h - 1; ++y)
292 if (maze [x][y] == '#' && dist [x][y] == 1) 336 if (data [x][y] == '#' && dist [x][y] == 1)
293 maze [x][y] = 0; 337 data [x][y] = 0;
294}
295
296void
297Layout::isolation_remover (bool dirty)
298{
299 ::isolation_remover (*this, dirty);
300} 338}
301 339
302///////////////////////////////////////////////////////////////////////////// 340/////////////////////////////////////////////////////////////////////////////
303 341
304// inspired mostly by http://www.jimrandomh.org/misc/caves.txt 342// inspired mostly by http://www.jimrandomh.org/misc/caves.txt
305void 343void
306Layout::gen_cave (int subtype) 344layout::gen_cave (int subtype)
307{ 345{
308 switch (subtype) 346 switch (subtype)
309 { 347 {
310 // a rough cave 348 // a rough cave
311 case 0: 349 case 0:
312 fill_rand (rmg_rndm (80, 95)); 350 fill_rand (rmg_rndm (85, 97));
313 break; 351 break;
314 352
315 // corridors 353 // corridors
316 case 1: 354 case 1:
317 fill_rand (rmg_rndm (5, 40)); 355 fill_rand (rmg_rndm (5, 40));
341 379
342///////////////////////////////////////////////////////////////////////////// 380/////////////////////////////////////////////////////////////////////////////
343 381
344//+GPL 382//+GPL
345 383
346/* puts doors at appropriate locations in a layout. */ 384/* puts doors at appropriate locations in a maze. */
347void 385void
348Layout::doorify () 386layout::doorify ()
349{ 387{
350 int ndoors = w * h / 60; /* reasonable number of doors. */ 388 int ndoors = w * h / 60; /* reasonable number of doors. */
351 int doorlocs = 0; /* # of available doorlocations */ 389 int doorlocs = 0; /* # of available doorlocations */
352 390
353 uint16 *doorlist_x = salloc<uint16> (w * h); 391 uint16 *doorlist_x = salloc<uint16> (w * h);
374 int j = doorlist_y [di]; 412 int j = doorlist_y [di];
375 int sindex = surround_flag (*this, i, j); 413 int sindex = surround_flag (*this, i, j);
376 414
377 if (sindex == 3 || sindex == 12) /* these are possible door sindex */ 415 if (sindex == 3 || sindex == 12) /* these are possible door sindex */
378 { 416 {
379 col [i][j] = 'D'; 417 data [i][j] = 'D';
380 ndoors--; 418 ndoors--;
381 } 419 }
382 420
383 /* reduce the size of the list */ 421 /* reduce the size of the list */
384 doorlocs--; 422 doorlocs--;
392 430
393/* takes a map and makes it symmetric: adjusts Xsize and 431/* takes a map and makes it symmetric: adjusts Xsize and
394 * Ysize to produce a symmetric map. 432 * Ysize to produce a symmetric map.
395 */ 433 */
396void 434void
397Layout::symmetrize (int symmetry) 435layout::symmetrize (int symmetry)
398{ 436{
399 if (symmetry == SYMMETRY_NONE) 437 if (symmetry == SYMMETRY_NONE)
400 return; 438 return;
401 439
402 Layout sym_layout ( 440 layout sym_layout (
403 symmetry == SYMMETRY_X || symmetry == SYMMETRY_XY ? w * 2 - 3 : w, 441 symmetry == SYMMETRY_X || symmetry == SYMMETRY_XY ? w * 2 - 3 : w,
404 symmetry == SYMMETRY_Y || symmetry == SYMMETRY_XY ? h * 2 - 3 : h 442 symmetry == SYMMETRY_Y || symmetry == SYMMETRY_XY ? h * 2 - 3 : h
405 ); 443 );
406 444
407 if (symmetry == SYMMETRY_X) 445 if (symmetry == SYMMETRY_X)
408 for (int i = 0; i < sym_layout.w / 2 + 1; i++) 446 for (int i = 0; i < sym_layout.w / 2 + 1; i++)
409 for (int j = 0; j < sym_layout.h; j++) 447 for (int j = 0; j < sym_layout.h; j++)
410 { 448 {
411 sym_layout[i ][j] = 449 sym_layout[i ][j] =
412 sym_layout[sym_layout.w - i - 1][j] = col[i][j]; 450 sym_layout[sym_layout.w - i - 1][j] = data [i][j];
413 } 451 }
414 452
415 if (symmetry == SYMMETRY_Y) 453 if (symmetry == SYMMETRY_Y)
416 for (int i = 0; i < sym_layout.w; i++) 454 for (int i = 0; i < sym_layout.w; i++)
417 for (int j = 0; j < sym_layout.h / 2 + 1; j++) 455 for (int j = 0; j < sym_layout.h / 2 + 1; j++)
418 { 456 {
419 sym_layout[i][j ] = 457 sym_layout[i][j ] =
420 sym_layout[i][sym_layout.h - j - 1] = col[i][j]; 458 sym_layout[i][sym_layout.h - j - 1] = data [i][j];
421 } 459 }
422 460
423 if (symmetry == SYMMETRY_XY) 461 if (symmetry == SYMMETRY_XY)
424 for (int i = 0; i < sym_layout.w / 2 + 1; i++) 462 for (int i = 0; i < sym_layout.w / 2 + 1; i++)
425 for (int j = 0; j < sym_layout.h / 2 + 1; j++) 463 for (int j = 0; j < sym_layout.h / 2 + 1; j++)
426 { 464 {
427 sym_layout[i ][j ] = 465 sym_layout[i ][j ] =
428 sym_layout[i ][sym_layout.h - j - 1] = 466 sym_layout[i ][sym_layout.h - j - 1] =
429 sym_layout[sym_layout.w - i - 1][j ] = 467 sym_layout[sym_layout.w - i - 1][j ] =
430 sym_layout[sym_layout.w - i - 1][sym_layout.h - j - 1] = col[i][j]; 468 sym_layout[sym_layout.w - i - 1][sym_layout.h - j - 1] = data [i][j];
431 } 469 }
432 470
433 /* need to run the isolation remover for some layouts */ 471 /* need to run the isolation remover for some layouts */
434#if 0 472#if 0
435 switch (RP->map_layout_style) 473 switch (RP->map_layout_style)
451} 489}
452 490
453//-GPL 491//-GPL
454 492
455void 493void
456Layout::rotate (int rotation) 494layout::rotate (int rotation)
457{ 495{
458 switch (rotation & 3) 496 switch (rotation & 3)
459 { 497 {
460 case 2: /* a reflection */ 498 case 2: /* a reflection */
461 { 499 {
462 Layout new_layout (w, h); 500 layout new_layout (w, h);
463 501
464 for (int i = 0; i < w; i++) /* copy a reflection back */ 502 for (int i = 0; i < w; i++) /* copy a reflection back */
465 for (int j = 0; j < h; j++) 503 for (int j = 0; j < h; j++)
466 new_layout [i][j] = col [w - i - 1][h - j - 1]; 504 new_layout [i][j] = data [w - i - 1][h - j - 1];
467 505
468 swap (new_layout); 506 swap (new_layout);
469 } 507 }
470 break; 508 break;
471 509
472 case 1: 510 case 1:
473 case 3: 511 case 3:
474 { 512 {
475 Layout new_layout (h, w); 513 layout new_layout (h, w);
476 514
477 if (rotation == 1) /* swap x and y */ 515 if (rotation == 1) /* swap x and y */
478 for (int i = 0; i < w; i++) 516 for (int i = 0; i < w; i++)
479 for (int j = 0; j < h; j++) 517 for (int j = 0; j < h; j++)
480 new_layout [j][i] = col [i][j]; 518 new_layout [j][i] = data [i][j];
481 519
482 if (rotation == 3) /* swap x and y */ 520 if (rotation == 3) /* swap x and y */
483 for (int i = 0; i < w; i++) 521 for (int i = 0; i < w; i++)
484 for (int j = 0; j < h; j++) 522 for (int j = 0; j < h; j++)
485 new_layout [j][i] = col [w - i - 1][h - j - 1]; 523 new_layout [j][i] = data [w - i - 1][h - j - 1];
486 524
487 swap (new_layout); 525 swap (new_layout);
488 } 526 }
489 break; 527 break;
490 } 528 }
493///////////////////////////////////////////////////////////////////////////// 531/////////////////////////////////////////////////////////////////////////////
494 532
495//+GPL 533//+GPL
496 534
497/* 535/*
498 * Expands a layout by 2x in each dimension. 536 * Expands a maze by 2x in each dimension.
499 * H. S. Teoh 537 * H. S. Teoh
500 */ 538 */
501 539
502/* Copy the old tile X into the new one at location (i*2, j*2) and 540/* Copy the old tile X into the new one at location (i*2, j*2) and
503 * fill up the rest of the 2x2 result with \0: 541 * fill up the rest of the 2x2 result with \0:
504 * X ---> X \0 542 * X ---> X \0
505 * \0 \0 543 * \0 \0
506 */ 544 */
507static void inline 545static void inline
508expand_misc (Layout &newlayout, int i, int j, Layout &layout) 546expand_misc (layout &newlayout, int i, int j, layout &maze)
509{ 547{
510 newlayout[i * 2 + rmg_rndm (1)][j * 2 + rmg_rndm (1)] = layout[i][j]; 548 newlayout[i * 2 + rmg_rndm (1)][j * 2 + rmg_rndm (1)] = maze[i][j];
511 /* (Note: no need to reset rest of 2x2 area to \0 because calloc does that 549 /* (Note: no need to reset rest of 2x2 area to \0 because calloc does that
512 * for us.) */ 550 * for us.) */
513} 551}
514 552
515/* Returns a bitmap that represents which squares on the right and bottom 553/* Returns a bitmap that represents which squares on the right and bottom
518 * 2 match on (i, j+1) 556 * 2 match on (i, j+1)
519 * 4 match on (i+1, j+1) 557 * 4 match on (i+1, j+1)
520 * and the possible combinations thereof. 558 * and the possible combinations thereof.
521 */ 559 */
522static int noinline 560static int noinline
523calc_pattern (char ch, Layout &layout, int i, int j) 561calc_pattern (char ch, layout &maze, int i, int j)
524{ 562{
525 int pattern = 0; 563 int pattern = 0;
526 564
527 if (i + 1 < layout.w && layout[i + 1][j] == ch) 565 if (i + 1 < maze.w && maze[i + 1][j] == ch)
528 pattern |= 1; 566 pattern |= 1;
529 567
530 if (j + 1 < layout.h) 568 if (j + 1 < maze.h)
531 { 569 {
532 if (layout[i][j + 1] == ch) 570 if (maze[i][j + 1] == ch)
533 pattern |= 2; 571 pattern |= 2;
534 572
535 if (i + 1 < layout.w && layout[i + 1][j + 1] == ch) 573 if (i + 1 < maze.w && maze[i + 1][j + 1] == ch)
536 pattern |= 4; 574 pattern |= 4;
537 } 575 }
538 576
539 return pattern; 577 return pattern;
540} 578}
542/* Expand a wall. This function will try to sensibly connect the resulting 580/* Expand a wall. This function will try to sensibly connect the resulting
543 * wall to adjacent wall squares, so that the result won't have disconnected 581 * wall to adjacent wall squares, so that the result won't have disconnected
544 * walls. 582 * walls.
545 */ 583 */
546static void inline 584static void inline
547expand_wall (Layout &newlayout, int i, int j, Layout &layout) 585expand_wall (layout &newlayout, int i, int j, layout &maze)
548{ 586{
549 int wall_pattern = calc_pattern ('#', layout, i, j); 587 int wall_pattern = calc_pattern ('#', maze, i, j);
550 int door_pattern = calc_pattern ('D', layout, i, j); 588 int door_pattern = calc_pattern ('D', maze, i, j);
551 int both_pattern = wall_pattern | door_pattern; 589 int both_pattern = wall_pattern | door_pattern;
552 590
553 newlayout[i * 2][j * 2] = '#'; 591 newlayout[i * 2][j * 2] = '#';
554 592
555 if (i + 1 < layout.w) 593 if (i + 1 < maze.w)
556 { 594 {
557 if (both_pattern & 1) 595 if (both_pattern & 1)
558 { /* join walls/doors to the right */ 596 { /* join walls/doors to the right */
559/* newlayout[i*2+1][j*2] = '#'; */ 597/* newlayout[i*2+1][j*2] = '#'; */
560 newlayout[i * 2 + 1][j * 2] = layout[i + 1][j]; 598 newlayout[i * 2 + 1][j * 2] = maze[i + 1][j];
561 } 599 }
562 } 600 }
563 601
564 if (j + 1 < layout.h) 602 if (j + 1 < maze.h)
565 { 603 {
566 if (both_pattern & 2) 604 if (both_pattern & 2)
567 { /* join walls/doors to the bottom */ 605 { /* join walls/doors to the bottom */
568/* newlayout[i*2][j*2+1] = '#'; */ 606/* newlayout[i*2][j*2+1] = '#'; */
569 newlayout[i * 2][j * 2 + 1] = layout[i][j + 1]; 607 newlayout[i * 2][j * 2 + 1] = maze[i][j + 1];
570 } 608 }
571 609
572 if (wall_pattern == 7) 610 if (wall_pattern == 7)
573 { /* if orig layout is a 2x2 wall block, 611 { /* if orig maze is a 2x2 wall block,
574 * we fill the result with walls. */ 612 * we fill the result with walls. */
575 newlayout[i * 2 + 1][j * 2 + 1] = '#'; 613 newlayout[i * 2 + 1][j * 2 + 1] = '#';
576 } 614 }
577 } 615 }
578} 616}
580/* This function will try to sensibly connect doors so that they meet up with 618/* This function will try to sensibly connect doors so that they meet up with
581 * adjacent walls. Note that it will also presumptuously delete (ignore) doors 619 * adjacent walls. Note that it will also presumptuously delete (ignore) doors
582 * that it doesn't know how to correctly expand. 620 * that it doesn't know how to correctly expand.
583 */ 621 */
584static void inline 622static void inline
585expand_door (Layout &newlayout, int i, int j, Layout &layout) 623expand_door (layout &newlayout, int i, int j, layout &maze)
586{ 624{
587 int wall_pattern = calc_pattern ('#', layout, i, j); 625 int wall_pattern = calc_pattern ('#', maze, i, j);
588 int door_pattern = calc_pattern ('D', layout, i, j); 626 int door_pattern = calc_pattern ('D', maze, i, j);
589 int join_pattern; 627 int join_pattern;
590 628
591 /* Doors "like" to connect to walls more than other doors. If there is 629 /* Doors "like" to connect to walls more than other doors. If there is
592 * a wall and another door, this door will connect to the wall and 630 * a wall and another door, this door will connect to the wall and
593 * disconnect from the other door. */ 631 * disconnect from the other door. */
596 else 634 else
597 join_pattern = door_pattern; 635 join_pattern = door_pattern;
598 636
599 newlayout[i * 2][j * 2] = 'D'; 637 newlayout[i * 2][j * 2] = 'D';
600 638
601 if (i + 1 < layout.w) 639 if (i + 1 < maze.w)
602 if (join_pattern & 1) 640 if (join_pattern & 1)
603 /* there is a door/wall to the right */ 641 /* there is a door/wall to the right */
604 newlayout[i * 2 + 1][j * 2] = 'D'; 642 newlayout[i * 2 + 1][j * 2] = 'D';
605 643
606 if (j + 1 < layout.h) 644 if (j + 1 < maze.h)
607 if (join_pattern & 2) 645 if (join_pattern & 2)
608 /* there is a door/wall below */ 646 /* there is a door/wall below */
609 newlayout[i * 2][j * 2 + 1] = 'D'; 647 newlayout[i * 2][j * 2 + 1] = 'D';
610} 648}
611 649
612void 650void
613Layout::expand2x () 651layout::expand2x ()
614{ 652{
615 Layout new_layout (w * 2 - 1, h * 2 - 1); 653 layout new_layout (w * 2 - 1, h * 2 - 1);
616 654
617 new_layout.clear (); 655 new_layout.clear ();
618 656
619 for (int i = 0; i < w; i++) 657 for (int i = 0; i < w; i++)
620 for (int j = 0; j < h; j++) 658 for (int j = 0; j < h; j++)
621 switch (col [i][j]) 659 switch (data [i][j])
622 { 660 {
623 case '#': expand_wall (new_layout, i, j, *this); break; 661 case '#': expand_wall (new_layout, i, j, *this); break;
624 case 'D': expand_door (new_layout, i, j, *this); break; 662 case 'D': expand_door (new_layout, i, j, *this); break;
625 default: expand_misc (new_layout, i, j, *this); break; 663 default: expand_misc (new_layout, i, j, *this); break;
626 } 664 }
628 swap (new_layout); 666 swap (new_layout);
629} 667}
630 668
631///////////////////////////////////////////////////////////////////////////// 669/////////////////////////////////////////////////////////////////////////////
632 670
633/* checks the layout to see if I can stick a horizontal(dir = 0) wall 671/* checks the maze to see if I can stick a horizontal(dir = 0) wall
634 (or vertical, dir == 1) 672 (or vertical, dir == 1)
635 here which ends up on other walls sensibly. */ 673 here which ends up on other walls sensibly. */
636static int 674static int
637can_make_wall (const Layout &maze, int dx, int dy, int dir) 675can_make_wall (const layout &maze, int dx, int dy, int dir)
638{ 676{
639 int i1; 677 int i1;
640 int length = 0; 678 int length = 0;
641 679
642 /* dont make walls if we're on the edge. */ 680 /* dont make walls if we're on the edge. */
729 767
730 return 0; 768 return 0;
731} 769}
732 770
733void 771void
734Layout::roomify () 772layout::roomify ()
735{ 773{
736 int tries = w * h / 30; 774 int tries = w * h / 30;
737 775
738 for (int ti = 0; ti < tries; ti++) 776 for (int ti = 0; ti < tries; ti++)
739 { 777 {
766 } 804 }
767} 805}
768 806
769///////////////////////////////////////////////////////////////////////////// 807/////////////////////////////////////////////////////////////////////////////
770 808
771/* function selects the layout function and gives it whatever 809/* function selects the maze function and gives it whatever
772 arguments it needs. */ 810 arguments it needs. */
773void 811void
774Layout::generate (random_map_params *RP) 812layout::generate (random_map_params *RP)
775{ 813{
776 switch (RP->map_layout_style) 814 switch (RP->map_layout_style)
777 { 815 {
778 case LAYOUT_ONION: 816 case LAYOUT_ONION:
779 map_gen_onion (*this, RP->layoutoptions1, RP->layoutoptions2); 817 map_gen_onion (*this, RP->layoutoptions1, RP->layoutoptions2);
838 876
839 default: 877 default:
840 abort (); 878 abort ();
841 } 879 }
842 880
843 /* rotate the layout randomly */ 881 /* rotate the maze randomly */
844 rotate (rmg_rndm (4)); 882 rotate (rmg_rndm (4));
845 883
846 symmetrize (RP->symmetry_used); 884 symmetrize (RP->symmetry_used);
847 885
848#if 0 886#if 0
858#if 0 896#if 0
859static struct demo 897static struct demo
860{ 898{
861 demo () 899 demo ()
862 { 900 {
863 Layout maze (40, 25);
864 rmg_rndm.seed (time (0)); 901 rmg_rndm.seed (time (0));
865 902
866 for(int i=1;i<10;i) 903 for(int i=1;i<100;i++)
867 { 904 {
905 layout maze (40, 25);
868 maze.fill_rand (97); 906 maze.fill_rand (85);
869 maze.border (); 907 maze.border ();
870 maze.isolation_remover (); 908 maze.isolation_remover ();
871 maze.print (); 909 maze.print ();
872 } 910 }
873 911

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines