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.7 by root, Fri Jul 2 15:03:57 2010 UTC vs.
Revision 1.8 by root, Fri Jul 2 15:43:37 2010 UTC

26#include <rproto.h> 26#include <rproto.h>
27 27
28layout::layout (int w, int h) 28layout::layout (int w, int h)
29: w(w), h(h) 29: w(w), h(h)
30{ 30{
31 assert (sizeof (cell) == 1);
32
33 // we store the layout in a single contiguous memory layout
34 // first part consists of pointers to each column, followed
35 // by the actual columns (not rows!)
31 int size = (sizeof (char *) + sizeof (char) * h) * w; 36 int size = (sizeof (cell *) + sizeof (cell) * h) * w;
32 37
33 col = (char **)salloc<char> (size); 38 data = (cell **)salloc<char> (size);
34 39
35 char *data = (char *)(col + w); 40 cell *p = (cell *)(data + w);
36 41
37 for (int x = w; x--; ) 42 for (int x = w; x--; )
38 col [x] = data + x * h; 43 data [x] = p + x * h;
39} 44}
40 45
41layout::~layout () 46layout::~layout ()
42{ 47{
43 int size = (sizeof (char *) + sizeof (char) * h) * w; 48 int size = (sizeof (cell *) + sizeof (cell) * h) * w;
44 49
45 sfree ((char *)col, size); 50 sfree ((char *)data, size);
46} 51}
47 52
48void 53void
49layout::fill (char fill) 54layout::fill (char fill)
50{ 55{
51 memset (col [0], fill, w * h); 56 memset (data [0], fill, w * h);
52} 57}
53 58
54void 59void
55layout::rect (int x1, int y1, int x2, int y2, char fill) 60layout::rect (int x1, int y1, int x2, int y2, char fill)
56{ 61{
57 for (; x1 < x2; ++x1) 62 for (; x1 < x2; ++x1)
58 memset (col [x1] + y1, fill, y2 - y1); 63 memset (data [x1] + y1, fill, y2 - y1);
59} 64}
60 65
61void layout::border (char fill) 66void layout::border (char fill)
62{ 67{
63 for (int i = 0; i < w; i++) col [i][0] = col [i][h - 1] = fill; 68 for (int i = 0; i < w; i++) data [i][0] = data [i][h - 1] = fill;
64 for (int j = 0; j < h; j++) col [0][j] = col [w - 1][j] = fill; 69 for (int j = 0; j < h; j++) data [0][j] = data [w - 1][j] = fill;
65} 70}
66 71
67void 72void
68layout::fill_rand (int percent) 73layout::fill_rand (int percent)
69{ 74{
70 percent = lerp (percent, 0, 100, 0, 256); 75 percent = lerp (percent, 0, 100, 0, 256);
71 76
72 for (int x = w - 1; --x > 0; ) 77 for (int x = w - 1; --x > 0; )
73 for (int y = h - 1; --y > 0; ) 78 for (int y = h - 1; --y > 0; )
74 col [x][y] = rmg_rndm (256) > percent ? 0 : '#'; 79 data [x][y] = rmg_rndm (256) > percent ? 0 : '#';
75} 80}
76 81
77///////////////////////////////////////////////////////////////////////////// 82/////////////////////////////////////////////////////////////////////////////
78 83
79// erode by cellular automata 84// erode by cellular automata
104 for (int i = array_length (dds); i--; ) 109 for (int i = array_length (dds); i--; )
105 { 110 {
106 int nx = x + dds [i][0]; 111 int nx = x + dds [i][0];
107 int ny = y + dds [i][1]; 112 int ny = y + dds [i][1];
108 113
109 if (!IN_RANGE_EXC (nx, 0, w) || !IN_RANGE_EXC (ny, 0, h) || !col [nx][ny]) 114 if (!IN_RANGE_EXC (nx, 0, w) || !IN_RANGE_EXC (ny, 0, h) || !data [nx][ny])
110 { 115 {
111 n1 += dds [i][2]; 116 n1 += dds [i][2];
112 n2++; 117 n2++;
113 } 118 }
114 } 119 }
128{ 133{
129 for (int y = 0; y < h; y++) 134 for (int y = 0; y < h; y++)
130 { 135 {
131 for (int x = 0; x < w; x++) 136 for (int x = 0; x < w; x++)
132 { 137 {
133 U8 c = (U8)col [x][y]; 138 U8 c = (U8)data [x][y];
134 139
135 if (!c) 140 if (!c)
136 c = ' '; 141 c = ' ';
137 else if (c < 10) 142 else if (c < 10)
138 c += '0'; 143 c += '0';
229 int x, y; 234 int x, y;
230 235
231 for (int i = 0; i < w; ++i) 236 for (int i = 0; i < w; ++i)
232 for (int j = 0; j < h; ++j) 237 for (int j = 0; j < h; ++j)
233 { 238 {
234 if (col [i][j] == '#') 239 if (data [i][j] == '#')
235 dist [i][j] = U8 (255); 240 dist [i][j] = U8 (255);
236 else 241 else
237 { 242 {
238 dist [i][j] = 0; 243 dist [i][j] = 0;
239 if (!rmg_rndm (++cnt)) 244 if (!rmg_rndm (++cnt))
293 } 298 }
294 299
295 // now copy the tunnels over 300 // now copy the tunnels over
296 for (int x = 0; x < w; ++x) 301 for (int x = 0; x < w; ++x)
297 for (int y = 0; y < h; ++y) 302 for (int y = 0; y < h; ++y)
298 if (col [x][y] == '#' && dist [x][y] == 1) 303 if (data [x][y] == '#' && dist [x][y] == 1)
299 col [x][y] = 0; 304 data [x][y] = 0;
300} 305}
301 306
302///////////////////////////////////////////////////////////////////////////// 307/////////////////////////////////////////////////////////////////////////////
303 308
304// inspired mostly by http://www.jimrandomh.org/misc/caves.txt 309// inspired mostly by http://www.jimrandomh.org/misc/caves.txt
374 int j = doorlist_y [di]; 379 int j = doorlist_y [di];
375 int sindex = surround_flag (*this, i, j); 380 int sindex = surround_flag (*this, i, j);
376 381
377 if (sindex == 3 || sindex == 12) /* these are possible door sindex */ 382 if (sindex == 3 || sindex == 12) /* these are possible door sindex */
378 { 383 {
379 col [i][j] = 'D'; 384 data [i][j] = 'D';
380 ndoors--; 385 ndoors--;
381 } 386 }
382 387
383 /* reduce the size of the list */ 388 /* reduce the size of the list */
384 doorlocs--; 389 doorlocs--;
407 if (symmetry == SYMMETRY_X) 412 if (symmetry == SYMMETRY_X)
408 for (int i = 0; i < sym_layout.w / 2 + 1; i++) 413 for (int i = 0; i < sym_layout.w / 2 + 1; i++)
409 for (int j = 0; j < sym_layout.h; j++) 414 for (int j = 0; j < sym_layout.h; j++)
410 { 415 {
411 sym_layout[i ][j] = 416 sym_layout[i ][j] =
412 sym_layout[sym_layout.w - i - 1][j] = col[i][j]; 417 sym_layout[sym_layout.w - i - 1][j] = data [i][j];
413 } 418 }
414 419
415 if (symmetry == SYMMETRY_Y) 420 if (symmetry == SYMMETRY_Y)
416 for (int i = 0; i < sym_layout.w; i++) 421 for (int i = 0; i < sym_layout.w; i++)
417 for (int j = 0; j < sym_layout.h / 2 + 1; j++) 422 for (int j = 0; j < sym_layout.h / 2 + 1; j++)
418 { 423 {
419 sym_layout[i][j ] = 424 sym_layout[i][j ] =
420 sym_layout[i][sym_layout.h - j - 1] = col[i][j]; 425 sym_layout[i][sym_layout.h - j - 1] = data [i][j];
421 } 426 }
422 427
423 if (symmetry == SYMMETRY_XY) 428 if (symmetry == SYMMETRY_XY)
424 for (int i = 0; i < sym_layout.w / 2 + 1; i++) 429 for (int i = 0; i < sym_layout.w / 2 + 1; i++)
425 for (int j = 0; j < sym_layout.h / 2 + 1; j++) 430 for (int j = 0; j < sym_layout.h / 2 + 1; j++)
426 { 431 {
427 sym_layout[i ][j ] = 432 sym_layout[i ][j ] =
428 sym_layout[i ][sym_layout.h - j - 1] = 433 sym_layout[i ][sym_layout.h - j - 1] =
429 sym_layout[sym_layout.w - i - 1][j ] = 434 sym_layout[sym_layout.w - i - 1][j ] =
430 sym_layout[sym_layout.w - i - 1][sym_layout.h - j - 1] = col[i][j]; 435 sym_layout[sym_layout.w - i - 1][sym_layout.h - j - 1] = data [i][j];
431 } 436 }
432 437
433 /* need to run the isolation remover for some layouts */ 438 /* need to run the isolation remover for some layouts */
434#if 0 439#if 0
435 switch (RP->map_layout_style) 440 switch (RP->map_layout_style)
461 { 466 {
462 layout new_layout (w, h); 467 layout new_layout (w, h);
463 468
464 for (int i = 0; i < w; i++) /* copy a reflection back */ 469 for (int i = 0; i < w; i++) /* copy a reflection back */
465 for (int j = 0; j < h; j++) 470 for (int j = 0; j < h; j++)
466 new_layout [i][j] = col [w - i - 1][h - j - 1]; 471 new_layout [i][j] = data [w - i - 1][h - j - 1];
467 472
468 swap (new_layout); 473 swap (new_layout);
469 } 474 }
470 break; 475 break;
471 476
475 layout new_layout (h, w); 480 layout new_layout (h, w);
476 481
477 if (rotation == 1) /* swap x and y */ 482 if (rotation == 1) /* swap x and y */
478 for (int i = 0; i < w; i++) 483 for (int i = 0; i < w; i++)
479 for (int j = 0; j < h; j++) 484 for (int j = 0; j < h; j++)
480 new_layout [j][i] = col [i][j]; 485 new_layout [j][i] = data [i][j];
481 486
482 if (rotation == 3) /* swap x and y */ 487 if (rotation == 3) /* swap x and y */
483 for (int i = 0; i < w; i++) 488 for (int i = 0; i < w; i++)
484 for (int j = 0; j < h; j++) 489 for (int j = 0; j < h; j++)
485 new_layout [j][i] = col [w - i - 1][h - j - 1]; 490 new_layout [j][i] = data [w - i - 1][h - j - 1];
486 491
487 swap (new_layout); 492 swap (new_layout);
488 } 493 }
489 break; 494 break;
490 } 495 }
616 621
617 new_layout.clear (); 622 new_layout.clear ();
618 623
619 for (int i = 0; i < w; i++) 624 for (int i = 0; i < w; i++)
620 for (int j = 0; j < h; j++) 625 for (int j = 0; j < h; j++)
621 switch (col [i][j]) 626 switch (data [i][j])
622 { 627 {
623 case '#': expand_wall (new_layout, i, j, *this); break; 628 case '#': expand_wall (new_layout, i, j, *this); break;
624 case 'D': expand_door (new_layout, i, j, *this); break; 629 case 'D': expand_door (new_layout, i, j, *this); break;
625 default: expand_misc (new_layout, i, j, *this); break; 630 default: expand_misc (new_layout, i, j, *this); break;
626 } 631 }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines