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.6 by root, Fri Jul 2 04:05:15 2010 UTC vs.
Revision 1.11 by root, Sat Jul 3 01:12:44 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 if (!ncnt)
196 break; 227 return;
228
229 point &p = neigh [rmg_rndm (ncnt)];
230
231 seeds.push (p);
232
233 x = p.x;
234 y = p.y;
197 235
198 d = dist [x][y]; 236 d = dist [x][y];
199 dist [x][y] = 1; 237 dist [x][y] = 1;
200 seeds.push (point (x, y));
201 } 238 }
202} 239}
203 240
204static void inline 241static void inline
205maybe_push (Layout &dist, pointlist &seeds, int x, int y, U8 d) 242maybe_push (layout &dist, pointlist &seeds, int x, int y, U8 d)
206{ 243{
207 char &D = dist [x][y]; 244 char &D = dist [x][y];
208 245
209 if (U8 (D) > d) // if wall and higher distance, lower distance 246 if (U8 (D) > d) // if wall and higher distance, lower distance
210 D = d; 247 D = d;
212 return; 249 return;
213 250
214 seeds.push (point (x, y)); 251 seeds.push (point (x, y));
215} 252}
216 253
217void 254// isolation remover, works on a "distance" map
218Layout::isolation_remover (bool dirty) 255// the map must be initialised with 0 == rooms, 255 = walls
256static void noinline
257isolation_remover (layout &dist)
219{ 258{
220 Layout dist (w, h);
221
222 // dist contains 259 // dist contains
223 // 0 == invisited rooms 260 // 0 == invisited rooms
224 // 1 == visited rooms 261 // 1 == visited rooms
225 // 2+ shortest distance to random near room 262 // 2+ shortest distance to random near room
226 263
227 // phase 1, initialise dist array, find seed 264 // phase 1, find seed
228 int cnt = 0; 265 int cnt = 0;
229 int x, y; 266 int x, y;
230 267
231 for (int i = 0; i < w; ++i) 268 for (int i = 0; i < dist.w; ++i)
232 for (int j = 0; j < h; ++j) 269 for (int j = 0; j < dist.h; ++j)
233 { 270 if (!dist [i][j] && !rmg_rndm (++cnt))
234 if (col [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; 271 x = i, y = j;
241 }
242 }
243 272
244 if (!cnt) 273 if (!cnt)
245 { 274 {
246 // map is completely massive, this is not good, 275 // map is completely massive, this is not good,
247 // so make it empty instead. 276 // so make it empty instead.
248 clear (); 277 dist.clear ();
249 border (); 278 dist.border (255);
250 return; 279 return;
251 } 280 }
252 281
253 fixed_stack<point> seeds (w * h * 5); 282 fixed_stack<point> seeds (dist.w * dist.h * 5);
254 283
255 // found first free space - picking the first one gives 284 // found first free space - picking the first one gives
256 // us a slight bias for tunnels, but usually you won't 285 // us a slight bias for tunnels, but usually you won't
257 // notice that in-game 286 // notice that in-game
258 seeds.push (point (x, y)); 287 seeds.push (point (x, y));
270 y = p.y; 299 y = p.y;
271 300
272 if (!dist [x][y]) 301 if (!dist [x][y])
273 { 302 {
274 // found new isolated area, make tunnel 303 // found new isolated area, make tunnel
275 if (!dirty)
276 push_flood_fill (dist, seeds, x, y); 304 push_flood_fill (dist, seeds, x, y);
277
278 make_tunnel (dist, seeds, x, y, 255); 305 make_tunnel (dist, seeds, x, y, 255);
279
280 if (dirty)
281 push_flood_fill (dist, seeds, x, y);
282 } 306 }
283 else 307 else
284 { 308 {
285 // nothing here, continue to expand 309 // nothing here, continue to expand
286 U8 d = U8 (dist [x][y]) + 1; 310 U8 d = U8 (dist [x][y]) + 1;
289 if (x > 0) maybe_push (dist, seeds, x - 1, y, d); 313 if (x > 0) maybe_push (dist, seeds, x - 1, y, d);
290 if (y < dist.h - 1) maybe_push (dist, seeds, x, y + 1, d); 314 if (y < dist.h - 1) maybe_push (dist, seeds, x, y + 1, d);
291 if (y > 0) maybe_push (dist, seeds, x, y - 1, d); 315 if (y > 0) maybe_push (dist, seeds, x, y - 1, d);
292 } 316 }
293 } 317 }
318}
319
320void
321layout::isolation_remover ()
322{
323 layout dist (w, h);
324
325 for (int x = 0; x < w; ++x)
326 for (int y = 0; y < h; ++y)
327 dist [x][y] = data [x][y] == '#' ? U8 (255) : 0;
328
329 ::isolation_remover (dist);
294 330
295 // now copy the tunnels over 331 // now copy the tunnels over
296 for (int x = 0; x < w; ++x) 332 for (int x = 0; x < w; ++x)
297 for (int y = 0; y < h; ++y) 333 for (int y = 0; y < h; ++y)
298 if (col [x][y] == '#' && dist [x][y] == 1) 334 if (data [x][y] == '#' && dist [x][y] == 1)
299 col [x][y] = 0; 335 data [x][y] = 0;
300} 336}
301 337
302///////////////////////////////////////////////////////////////////////////// 338/////////////////////////////////////////////////////////////////////////////
303 339
304// inspired mostly by http://www.jimrandomh.org/misc/caves.txt 340// inspired mostly by http://www.jimrandomh.org/misc/caves.txt
305void 341void
306Layout::gen_cave (int subtype) 342layout::gen_cave (int subtype)
307{ 343{
308 switch (subtype) 344 switch (subtype)
309 { 345 {
310 // a rough cave 346 // a rough cave
311 case 0: 347 case 0:
312 fill_rand (rmg_rndm (80, 95)); 348 fill_rand (rmg_rndm (85, 97));
313 break; 349 break;
314 350
315 // corridors 351 // corridors
316 case 1: 352 case 1:
317 fill_rand (rmg_rndm (5, 40)); 353 fill_rand (rmg_rndm (5, 40));
341 377
342///////////////////////////////////////////////////////////////////////////// 378/////////////////////////////////////////////////////////////////////////////
343 379
344//+GPL 380//+GPL
345 381
346/* puts doors at appropriate locations in a layout. */ 382/* puts doors at appropriate locations in a maze. */
347void 383void
348Layout::doorify () 384layout::doorify ()
349{ 385{
350 int ndoors = w * h / 60; /* reasonable number of doors. */ 386 int ndoors = w * h / 60; /* reasonable number of doors. */
351 int doorlocs = 0; /* # of available doorlocations */ 387 int doorlocs = 0; /* # of available doorlocations */
352 388
353 uint16 *doorlist_x = salloc<uint16> (w * h); 389 uint16 *doorlist_x = salloc<uint16> (w * h);
374 int j = doorlist_y [di]; 410 int j = doorlist_y [di];
375 int sindex = surround_flag (*this, i, j); 411 int sindex = surround_flag (*this, i, j);
376 412
377 if (sindex == 3 || sindex == 12) /* these are possible door sindex */ 413 if (sindex == 3 || sindex == 12) /* these are possible door sindex */
378 { 414 {
379 col [i][j] = 'D'; 415 data [i][j] = 'D';
380 ndoors--; 416 ndoors--;
381 } 417 }
382 418
383 /* reduce the size of the list */ 419 /* reduce the size of the list */
384 doorlocs--; 420 doorlocs--;
392 428
393/* takes a map and makes it symmetric: adjusts Xsize and 429/* takes a map and makes it symmetric: adjusts Xsize and
394 * Ysize to produce a symmetric map. 430 * Ysize to produce a symmetric map.
395 */ 431 */
396void 432void
397Layout::symmetrize (int symmetry) 433layout::symmetrize (int symmetry)
398{ 434{
399 if (symmetry == SYMMETRY_NONE) 435 if (symmetry == SYMMETRY_NONE)
400 return; 436 return;
401 437
402 Layout sym_layout ( 438 layout sym_layout (
403 symmetry == SYMMETRY_X || symmetry == SYMMETRY_XY ? w * 2 - 3 : w, 439 symmetry == SYMMETRY_X || symmetry == SYMMETRY_XY ? w * 2 - 3 : w,
404 symmetry == SYMMETRY_Y || symmetry == SYMMETRY_XY ? h * 2 - 3 : h 440 symmetry == SYMMETRY_Y || symmetry == SYMMETRY_XY ? h * 2 - 3 : h
405 ); 441 );
406 442
407 if (symmetry == SYMMETRY_X) 443 if (symmetry == SYMMETRY_X)
408 for (int i = 0; i < sym_layout.w / 2 + 1; i++) 444 for (int i = 0; i < sym_layout.w / 2 + 1; i++)
409 for (int j = 0; j < sym_layout.h; j++) 445 for (int j = 0; j < sym_layout.h; j++)
410 { 446 {
411 sym_layout[i ][j] = 447 sym_layout[i ][j] =
412 sym_layout[sym_layout.w - i - 1][j] = col[i][j]; 448 sym_layout[sym_layout.w - i - 1][j] = data [i][j];
413 } 449 }
414 450
415 if (symmetry == SYMMETRY_Y) 451 if (symmetry == SYMMETRY_Y)
416 for (int i = 0; i < sym_layout.w; i++) 452 for (int i = 0; i < sym_layout.w; i++)
417 for (int j = 0; j < sym_layout.h / 2 + 1; j++) 453 for (int j = 0; j < sym_layout.h / 2 + 1; j++)
418 { 454 {
419 sym_layout[i][j ] = 455 sym_layout[i][j ] =
420 sym_layout[i][sym_layout.h - j - 1] = col[i][j]; 456 sym_layout[i][sym_layout.h - j - 1] = data [i][j];
421 } 457 }
422 458
423 if (symmetry == SYMMETRY_XY) 459 if (symmetry == SYMMETRY_XY)
424 for (int i = 0; i < sym_layout.w / 2 + 1; i++) 460 for (int i = 0; i < sym_layout.w / 2 + 1; i++)
425 for (int j = 0; j < sym_layout.h / 2 + 1; j++) 461 for (int j = 0; j < sym_layout.h / 2 + 1; j++)
426 { 462 {
427 sym_layout[i ][j ] = 463 sym_layout[i ][j ] =
428 sym_layout[i ][sym_layout.h - j - 1] = 464 sym_layout[i ][sym_layout.h - j - 1] =
429 sym_layout[sym_layout.w - i - 1][j ] = 465 sym_layout[sym_layout.w - i - 1][j ] =
430 sym_layout[sym_layout.w - i - 1][sym_layout.h - j - 1] = col[i][j]; 466 sym_layout[sym_layout.w - i - 1][sym_layout.h - j - 1] = data [i][j];
431 } 467 }
432 468
433 /* need to run the isolation remover for some layouts */ 469 /* need to run the isolation remover for some layouts */
434#if 0 470#if 0
435 switch (RP->map_layout_style) 471 switch (RP->map_layout_style)
451} 487}
452 488
453//-GPL 489//-GPL
454 490
455void 491void
456Layout::rotate (int rotation) 492layout::rotate (int rotation)
457{ 493{
458 switch (rotation & 3) 494 switch (rotation & 3)
459 { 495 {
460 case 2: /* a reflection */ 496 case 2: /* a reflection */
461 { 497 {
462 Layout new_layout (w, h); 498 layout new_layout (w, h);
463 499
464 for (int i = 0; i < w; i++) /* copy a reflection back */ 500 for (int i = 0; i < w; i++) /* copy a reflection back */
465 for (int j = 0; j < h; j++) 501 for (int j = 0; j < h; j++)
466 new_layout [i][j] = col [w - i - 1][h - j - 1]; 502 new_layout [i][j] = data [w - i - 1][h - j - 1];
467 503
468 swap (new_layout); 504 swap (new_layout);
469 } 505 }
470 break; 506 break;
471 507
472 case 1: 508 case 1:
473 case 3: 509 case 3:
474 { 510 {
475 Layout new_layout (h, w); 511 layout new_layout (h, w);
476 512
477 if (rotation == 1) /* swap x and y */ 513 if (rotation == 1) /* swap x and y */
478 for (int i = 0; i < w; i++) 514 for (int i = 0; i < w; i++)
479 for (int j = 0; j < h; j++) 515 for (int j = 0; j < h; j++)
480 new_layout [j][i] = col [i][j]; 516 new_layout [j][i] = data [i][j];
481 517
482 if (rotation == 3) /* swap x and y */ 518 if (rotation == 3) /* swap x and y */
483 for (int i = 0; i < w; i++) 519 for (int i = 0; i < w; i++)
484 for (int j = 0; j < h; j++) 520 for (int j = 0; j < h; j++)
485 new_layout [j][i] = col [w - i - 1][h - j - 1]; 521 new_layout [j][i] = data [w - i - 1][h - j - 1];
486 522
487 swap (new_layout); 523 swap (new_layout);
488 } 524 }
489 break; 525 break;
490 } 526 }
493///////////////////////////////////////////////////////////////////////////// 529/////////////////////////////////////////////////////////////////////////////
494 530
495//+GPL 531//+GPL
496 532
497/* 533/*
498 * Expands a layout by 2x in each dimension. 534 * Expands a maze by 2x in each dimension.
499 * H. S. Teoh 535 * H. S. Teoh
500 */ 536 */
501 537
502/* Copy the old tile X into the new one at location (i*2, j*2) and 538/* 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: 539 * fill up the rest of the 2x2 result with \0:
504 * X ---> X \0 540 * X ---> X \0
505 * \0 \0 541 * \0 \0
506 */ 542 */
507static void inline 543static void inline
508expand_misc (Layout &newlayout, int i, int j, Layout &layout) 544expand_misc (layout &newlayout, int i, int j, layout &maze)
509{ 545{
510 newlayout[i * 2 + rmg_rndm (1)][j * 2 + rmg_rndm (1)] = layout[i][j]; 546 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 547 /* (Note: no need to reset rest of 2x2 area to \0 because calloc does that
512 * for us.) */ 548 * for us.) */
513} 549}
514 550
515/* Returns a bitmap that represents which squares on the right and bottom 551/* Returns a bitmap that represents which squares on the right and bottom
518 * 2 match on (i, j+1) 554 * 2 match on (i, j+1)
519 * 4 match on (i+1, j+1) 555 * 4 match on (i+1, j+1)
520 * and the possible combinations thereof. 556 * and the possible combinations thereof.
521 */ 557 */
522static int noinline 558static int noinline
523calc_pattern (char ch, Layout &layout, int i, int j) 559calc_pattern (char ch, layout &maze, int i, int j)
524{ 560{
525 int pattern = 0; 561 int pattern = 0;
526 562
527 if (i + 1 < layout.w && layout[i + 1][j] == ch) 563 if (i + 1 < maze.w && maze[i + 1][j] == ch)
528 pattern |= 1; 564 pattern |= 1;
529 565
530 if (j + 1 < layout.h) 566 if (j + 1 < maze.h)
531 { 567 {
532 if (layout[i][j + 1] == ch) 568 if (maze[i][j + 1] == ch)
533 pattern |= 2; 569 pattern |= 2;
534 570
535 if (i + 1 < layout.w && layout[i + 1][j + 1] == ch) 571 if (i + 1 < maze.w && maze[i + 1][j + 1] == ch)
536 pattern |= 4; 572 pattern |= 4;
537 } 573 }
538 574
539 return pattern; 575 return pattern;
540} 576}
542/* Expand a wall. This function will try to sensibly connect the resulting 578/* 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 579 * wall to adjacent wall squares, so that the result won't have disconnected
544 * walls. 580 * walls.
545 */ 581 */
546static void inline 582static void inline
547expand_wall (Layout &newlayout, int i, int j, Layout &layout) 583expand_wall (layout &newlayout, int i, int j, layout &maze)
548{ 584{
549 int wall_pattern = calc_pattern ('#', layout, i, j); 585 int wall_pattern = calc_pattern ('#', maze, i, j);
550 int door_pattern = calc_pattern ('D', layout, i, j); 586 int door_pattern = calc_pattern ('D', maze, i, j);
551 int both_pattern = wall_pattern | door_pattern; 587 int both_pattern = wall_pattern | door_pattern;
552 588
553 newlayout[i * 2][j * 2] = '#'; 589 newlayout[i * 2][j * 2] = '#';
554 590
555 if (i + 1 < layout.w) 591 if (i + 1 < maze.w)
556 { 592 {
557 if (both_pattern & 1) 593 if (both_pattern & 1)
558 { /* join walls/doors to the right */ 594 { /* join walls/doors to the right */
559/* newlayout[i*2+1][j*2] = '#'; */ 595/* newlayout[i*2+1][j*2] = '#'; */
560 newlayout[i * 2 + 1][j * 2] = layout[i + 1][j]; 596 newlayout[i * 2 + 1][j * 2] = maze[i + 1][j];
561 } 597 }
562 } 598 }
563 599
564 if (j + 1 < layout.h) 600 if (j + 1 < maze.h)
565 { 601 {
566 if (both_pattern & 2) 602 if (both_pattern & 2)
567 { /* join walls/doors to the bottom */ 603 { /* join walls/doors to the bottom */
568/* newlayout[i*2][j*2+1] = '#'; */ 604/* newlayout[i*2][j*2+1] = '#'; */
569 newlayout[i * 2][j * 2 + 1] = layout[i][j + 1]; 605 newlayout[i * 2][j * 2 + 1] = maze[i][j + 1];
570 } 606 }
571 607
572 if (wall_pattern == 7) 608 if (wall_pattern == 7)
573 { /* if orig layout is a 2x2 wall block, 609 { /* if orig maze is a 2x2 wall block,
574 * we fill the result with walls. */ 610 * we fill the result with walls. */
575 newlayout[i * 2 + 1][j * 2 + 1] = '#'; 611 newlayout[i * 2 + 1][j * 2 + 1] = '#';
576 } 612 }
577 } 613 }
578} 614}
580/* This function will try to sensibly connect doors so that they meet up with 616/* 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 617 * adjacent walls. Note that it will also presumptuously delete (ignore) doors
582 * that it doesn't know how to correctly expand. 618 * that it doesn't know how to correctly expand.
583 */ 619 */
584static void inline 620static void inline
585expand_door (Layout &newlayout, int i, int j, Layout &layout) 621expand_door (layout &newlayout, int i, int j, layout &maze)
586{ 622{
587 int wall_pattern = calc_pattern ('#', layout, i, j); 623 int wall_pattern = calc_pattern ('#', maze, i, j);
588 int door_pattern = calc_pattern ('D', layout, i, j); 624 int door_pattern = calc_pattern ('D', maze, i, j);
589 int join_pattern; 625 int join_pattern;
590 626
591 /* Doors "like" to connect to walls more than other doors. If there is 627 /* 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 628 * a wall and another door, this door will connect to the wall and
593 * disconnect from the other door. */ 629 * disconnect from the other door. */
596 else 632 else
597 join_pattern = door_pattern; 633 join_pattern = door_pattern;
598 634
599 newlayout[i * 2][j * 2] = 'D'; 635 newlayout[i * 2][j * 2] = 'D';
600 636
601 if (i + 1 < layout.w) 637 if (i + 1 < maze.w)
602 if (join_pattern & 1) 638 if (join_pattern & 1)
603 /* there is a door/wall to the right */ 639 /* there is a door/wall to the right */
604 newlayout[i * 2 + 1][j * 2] = 'D'; 640 newlayout[i * 2 + 1][j * 2] = 'D';
605 641
606 if (j + 1 < layout.h) 642 if (j + 1 < maze.h)
607 if (join_pattern & 2) 643 if (join_pattern & 2)
608 /* there is a door/wall below */ 644 /* there is a door/wall below */
609 newlayout[i * 2][j * 2 + 1] = 'D'; 645 newlayout[i * 2][j * 2 + 1] = 'D';
610} 646}
611 647
612void 648void
613Layout::expand2x () 649layout::expand2x ()
614{ 650{
615 Layout new_layout (w * 2 - 1, h * 2 - 1); 651 layout new_layout (w * 2 - 1, h * 2 - 1);
616 652
617 new_layout.clear (); 653 new_layout.clear ();
618 654
619 for (int i = 0; i < w; i++) 655 for (int i = 0; i < w; i++)
620 for (int j = 0; j < h; j++) 656 for (int j = 0; j < h; j++)
621 switch (col [i][j]) 657 switch (data [i][j])
622 { 658 {
623 case '#': expand_wall (new_layout, i, j, *this); break; 659 case '#': expand_wall (new_layout, i, j, *this); break;
624 case 'D': expand_door (new_layout, i, j, *this); break; 660 case 'D': expand_door (new_layout, i, j, *this); break;
625 default: expand_misc (new_layout, i, j, *this); break; 661 default: expand_misc (new_layout, i, j, *this); break;
626 } 662 }
628 swap (new_layout); 664 swap (new_layout);
629} 665}
630 666
631///////////////////////////////////////////////////////////////////////////// 667/////////////////////////////////////////////////////////////////////////////
632 668
633/* checks the layout to see if I can stick a horizontal(dir = 0) wall 669/* checks the maze to see if I can stick a horizontal(dir = 0) wall
634 (or vertical, dir == 1) 670 (or vertical, dir == 1)
635 here which ends up on other walls sensibly. */ 671 here which ends up on other walls sensibly. */
636static int 672static int
637can_make_wall (const Layout &maze, int dx, int dy, int dir) 673can_make_wall (const layout &maze, int dx, int dy, int dir)
638{ 674{
639 int i1; 675 int i1;
640 int length = 0; 676 int length = 0;
641 677
642 /* dont make walls if we're on the edge. */ 678 /* dont make walls if we're on the edge. */
729 765
730 return 0; 766 return 0;
731} 767}
732 768
733void 769void
734Layout::roomify () 770layout::roomify ()
735{ 771{
736 int tries = w * h / 30; 772 int tries = w * h / 30;
737 773
738 for (int ti = 0; ti < tries; ti++) 774 for (int ti = 0; ti < tries; ti++)
739 { 775 {
766 } 802 }
767} 803}
768 804
769///////////////////////////////////////////////////////////////////////////// 805/////////////////////////////////////////////////////////////////////////////
770 806
771/* function selects the layout function and gives it whatever 807/* function selects the maze function and gives it whatever
772 arguments it needs. */ 808 arguments it needs. */
773void 809void
774Layout::generate (random_map_params *RP) 810layout::generate (random_map_params *RP)
775{ 811{
776 switch (RP->map_layout_style) 812 switch (RP->map_layout_style)
777 { 813 {
778 case LAYOUT_ONION: 814 case LAYOUT_ONION:
779 map_gen_onion (*this, RP->layoutoptions1, RP->layoutoptions2); 815 map_gen_onion (*this, RP->layoutoptions1, RP->layoutoptions2);
838 874
839 default: 875 default:
840 abort (); 876 abort ();
841 } 877 }
842 878
843 /* rotate the layout randomly */ 879 /* rotate the maze randomly */
844 rotate (rmg_rndm (4)); 880 rotate (rmg_rndm (4));
845 881
846 symmetrize (RP->symmetry_used); 882 symmetrize (RP->symmetry_used);
847 883
848#if 0 884#if 0
860{ 896{
861 demo () 897 demo ()
862 { 898 {
863 rmg_rndm.seed (time (0)); 899 rmg_rndm.seed (time (0));
864 900
865 for(int i=1;i<10;i) 901 for(int i=1;i<100;i++)
866 { 902 {
867 Layout maze (10, 10); 903 layout maze (40, 25);
868 maze.fill_rand (90); 904 maze.fill_rand (85);
869 maze.border (); 905 maze.border ();
870 maze.isolation_remover (); 906 maze.isolation_remover ();
871 maze.doorify ();
872 maze.symmetrize (rmg_rndm (2, 4));
873 maze.rotate (rmg_rndm (4));
874 maze.expand2x ();
875 maze.print (); 907 maze.print ();
876 } 908 }
877 909
878 exit (1); 910 exit (1);
879 } 911 }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines