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.14 by root, Sat Jul 3 02:19:10 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
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
28layout::layout (int w, int h) 48layout::layout (int w, int h)
29: w(w), h(h)
30{ 49{
31 int size = (sizeof (char *) + sizeof (char) * h) * w; 50 alloc (w, h);
51}
32 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;
33 col = (char **)salloc<char> (size); 67 data = (cell **)salloc<char> (size);
34 68
35 char *data = (char *)(col + w); 69 // and now we point back into the original layout
36
37 for (int x = w; x--; ) 70 for (int x = 0; x < w; ++x)
38 col [x] = data + x * h; 71 data [x] = orig.data [x + x1] + y1;
39} 72}
40 73
41layout::~layout () 74layout::~layout ()
42{ 75{
43 int size = (sizeof (char *) + sizeof (char) * h) * w;
44
45 sfree ((char *)col, size); 76 sfree ((char *)data, size);
46} 77}
47 78
48void 79void
49layout::fill (char fill) 80layout::fill (char fill)
50{ 81{
51 memset (col [0], fill, w * h); 82 memset (data [0], fill, w * h);
52} 83}
53 84
54void 85void
55layout::rect (int x1, int y1, int x2, int y2, char fill) 86layout::rect (int x1, int y1, int x2, int y2, char fill)
56{ 87{
88 --x2;
89
90 memset (data [x1] + y1, fill, y2 - y1);
91 memset (data [x2] + y1, fill, y2 - y1);
92
93 while (++x1 < x2)
94 data [x1][y1] = data [x1][y2 - 1] = fill;
95}
96
97void
98layout::fill_rect (int x1, int y1, int x2, int y2, char fill)
99{
57 for (; x1 < x2; ++x1) 100 for (; x1 < x2; ++x1)
58 memset (col [x1] + y1, fill, y2 - y1); 101 memset (data [x1] + y1, fill, y2 - y1);
59} 102}
60 103
61void layout::border (char fill) 104void layout::border (char fill)
62{ 105{
63 for (int i = 0; i < w; i++) col [i][0] = col [i][h - 1] = fill; 106 rect (0, 0, w, h, fill);
64 for (int j = 0; j < h; j++) col [0][j] = col [w - 1][j] = fill;
65} 107}
66 108
67void 109void
68layout::fill_rand (int percent) 110layout::fill_rand (int percent)
69{ 111{
70 percent = lerp (percent, 0, 100, 0, 256); 112 percent = lerp (percent, 0, 100, 0, 256);
71 113
72 for (int x = w - 1; --x > 0; ) 114 for (int x = w - 1; --x > 0; )
73 for (int y = h - 1; --y > 0; ) 115 for (int y = h - 1; --y > 0; )
74 col [x][y] = rmg_rndm (256) > percent ? 0 : '#'; 116 data [x][y] = rmg_rndm (256) > percent ? 0 : '#';
75} 117}
76 118
77///////////////////////////////////////////////////////////////////////////// 119/////////////////////////////////////////////////////////////////////////////
78 120
79// erode by cellular automata 121// erode by cellular automata
104 for (int i = array_length (dds); i--; ) 146 for (int i = array_length (dds); i--; )
105 { 147 {
106 int nx = x + dds [i][0]; 148 int nx = x + dds [i][0];
107 int ny = y + dds [i][1]; 149 int ny = y + dds [i][1];
108 150
109 if (!IN_RANGE_EXC (nx, 0, w) || !IN_RANGE_EXC (ny, 0, h) || !col [nx][ny]) 151 if (!IN_RANGE_EXC (nx, 0, w) || !IN_RANGE_EXC (ny, 0, h) || !data [nx][ny])
110 { 152 {
111 n1 += dds [i][2]; 153 n1 += dds [i][2];
112 n2++; 154 n2++;
113 } 155 }
114 } 156 }
128{ 170{
129 for (int y = 0; y < h; y++) 171 for (int y = 0; y < h; y++)
130 { 172 {
131 for (int x = 0; x < w; x++) 173 for (int x = 0; x < w; x++)
132 { 174 {
133 U8 c = (U8)col [x][y]; 175 U8 c = (U8)data [x][y];
134 176
135 if (!c) 177 if (!c)
136 c = ' '; 178 c = ' ';
137 else if (c < 10) 179 else if (c < 10)
138 c += '0'; 180 c += '0';
151///////////////////////////////////////////////////////////////////////////// 193/////////////////////////////////////////////////////////////////////////////
152// isolation remover - ensures single connected area 194// isolation remover - ensures single connected area
153 195
154typedef fixed_stack<point> pointlist; 196typedef fixed_stack<point> pointlist;
155 197
156static noinline void 198static void noinline
157push_flood_fill (layout &dist, pointlist &seeds, int x, int y) 199push_flood_fill (layout &dist, pointlist &seeds, int x, int y)
158{ 200{
159 if (dist [x][y]) 201 if (dist [x][y])
160 return; 202 return;
161 203
172 ++y; 214 ++y;
173 } 215 }
174 216
175 while (--y >= y0) 217 while (--y >= y0)
176 { 218 {
177 if (x > 0) push_flood_fill (dist, seeds, x - 1, y); 219 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); 220 if (x < dist.w - 1 && !dist [x + 1][y]) push_flood_fill (dist, seeds, x + 1, y);
179 } 221 }
180} 222}
181 223
182static inline void 224static inline void
183make_tunnel (layout &dist, pointlist &seeds, int x, int y, U8 d) 225make_tunnel (layout &dist, pointlist &seeds, int x, int y, U8 d)
184{ 226{
185 for (;;) 227 for (;;)
186 { 228 {
229 point neigh[4];
230 int ncnt = 0;
231
187 if (x > 1 && U8 (dist [x - 1][y]) < d && dist [x - 1][y] > 1) 232 if (x > 0 && 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) 233 if (x < dist.w - 1 && 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) 234 if (y > 0 && 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) 235 if (y < dist.h - 1 && U8 (dist [x][y + 1]) <= d && dist [x][y + 1] > 1) neigh [ncnt++] = point (x, y + 1);
194 ++y; 236
195 else 237 if (!ncnt)
196 break; 238 return;
239
240 point &p = neigh [rmg_rndm (ncnt)];
241
242 seeds.push (p);
243
244 x = p.x;
245 y = p.y;
197 246
198 d = dist [x][y]; 247 d = dist [x][y];
199 dist [x][y] = 1; 248 dist [x][y] = 1;
200 seeds.push (point (x, y));
201 } 249 }
202} 250}
203 251
204static void inline 252static void inline
205maybe_push (layout &dist, pointlist &seeds, int x, int y, U8 d) 253maybe_push (layout &dist, pointlist &seeds, int x, int y, U8 d)
212 return; 260 return;
213 261
214 seeds.push (point (x, y)); 262 seeds.push (point (x, y));
215} 263}
216 264
217void 265// isolation remover, works on a "distance" map
218layout::isolation_remover (bool dirty) 266// the map must be initialised with 0 == rooms, 255 = walls
267static void noinline
268isolation_remover (layout &dist)
219{ 269{
220 layout dist (w, h);
221
222 // dist contains 270 // dist contains
223 // 0 == invisited rooms 271 // 0 == invisited rooms
224 // 1 == visited rooms 272 // 1 == visited rooms
225 // 2+ shortest distance to random near room 273 // 2+ shortest distance to random near room
226 274
227 // phase 1, initialise dist array, find seed 275 // phase 1, find seed
228 int cnt = 0; 276 int cnt = 0;
229 int x, y; 277 int x, y;
230 278
231 for (int i = 0; i < w; ++i) 279 for (int i = 0; i < dist.w; ++i)
232 for (int j = 0; j < h; ++j) 280 for (int j = 0; j < dist.h; ++j)
233 { 281 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; 282 x = i, y = j;
241 }
242 }
243 283
244 if (!cnt) 284 if (!cnt)
245 { 285 {
246 // map is completely massive, this is not good, 286 // map is completely massive, this is not good,
247 // so make it empty instead. 287 // so make it empty instead.
248 clear (); 288 dist.fill (1);
249 border ();
250 return; 289 return;
251 } 290 }
252 291
253 fixed_stack<point> seeds (w * h * 5); 292 fixed_stack<point> seeds (dist.w * dist.h * 5);
254 293
255 // found first free space - picking the first one gives 294 // found first free space - picking the first one gives
256 // us a slight bias for tunnels, but usually you won't 295 // us a slight bias for tunnels, but usually you won't
257 // notice that in-game 296 // notice that in-game
258 seeds.push (point (x, y)); 297 seeds.push (point (x, y));
270 y = p.y; 309 y = p.y;
271 310
272 if (!dist [x][y]) 311 if (!dist [x][y])
273 { 312 {
274 // found new isolated area, make tunnel 313 // found new isolated area, make tunnel
275 if (!dirty)
276 push_flood_fill (dist, seeds, x, y); 314 push_flood_fill (dist, seeds, x, y);
277
278 make_tunnel (dist, seeds, x, y, 255); 315 make_tunnel (dist, seeds, x, y, 255);
279
280 if (dirty)
281 push_flood_fill (dist, seeds, x, y);
282 } 316 }
283 else 317 else
284 { 318 {
285 // nothing here, continue to expand 319 // nothing here, continue to expand
286 U8 d = U8 (dist [x][y]) + 1; 320 U8 d = U8 (dist [x][y]) + 1;
289 if (x > 0) maybe_push (dist, seeds, x - 1, y, d); 323 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); 324 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); 325 if (y > 0) maybe_push (dist, seeds, x, y - 1, d);
292 } 326 }
293 } 327 }
328}
329
330void
331layout::isolation_remover ()
332{
333 layout dist (w - 2, h - 2); // map without border
334
335 for (int x = 1; x < w - 1; ++x)
336 for (int y = 1; y < h - 1; ++y)
337 dist [x - 1][y - 1] = data [x][y] == '#' ? U8 (255) : 0;
338
339 ::isolation_remover (dist);
294 340
295 // now copy the tunnels over 341 // now copy the tunnels over
296 for (int x = 0; x < w; ++x) 342 for (int x = 1; x < w - 1; ++x)
297 for (int y = 0; y < h; ++y) 343 for (int y = 1; y < h - 1; ++y)
298 if (col [x][y] == '#' && dist [x][y] == 1) 344 if (data [x][y] == '#' && dist [x - 1][y - 1] == 1)
299 col [x][y] = 0; 345 data [x][y] = 0;
300} 346}
301 347
302///////////////////////////////////////////////////////////////////////////// 348/////////////////////////////////////////////////////////////////////////////
303 349
304// inspired mostly by http://www.jimrandomh.org/misc/caves.txt 350// inspired mostly by http://www.jimrandomh.org/misc/caves.txt
307{ 353{
308 switch (subtype) 354 switch (subtype)
309 { 355 {
310 // a rough cave 356 // a rough cave
311 case 0: 357 case 0:
312 fill_rand (rmg_rndm (80, 95)); 358 fill_rand (rmg_rndm (85, 97));
313 break; 359 break;
314 360
315 // corridors 361 // corridors
316 case 1: 362 case 1:
317 fill_rand (rmg_rndm (5, 40)); 363 fill_rand (rmg_rndm (5, 40));
374 int j = doorlist_y [di]; 420 int j = doorlist_y [di];
375 int sindex = surround_flag (*this, i, j); 421 int sindex = surround_flag (*this, i, j);
376 422
377 if (sindex == 3 || sindex == 12) /* these are possible door sindex */ 423 if (sindex == 3 || sindex == 12) /* these are possible door sindex */
378 { 424 {
379 col [i][j] = 'D'; 425 data [i][j] = 'D';
380 ndoors--; 426 ndoors--;
381 } 427 }
382 428
383 /* reduce the size of the list */ 429 /* reduce the size of the list */
384 doorlocs--; 430 doorlocs--;
407 if (symmetry == SYMMETRY_X) 453 if (symmetry == SYMMETRY_X)
408 for (int i = 0; i < sym_layout.w / 2 + 1; i++) 454 for (int i = 0; i < sym_layout.w / 2 + 1; i++)
409 for (int j = 0; j < sym_layout.h; j++) 455 for (int j = 0; j < sym_layout.h; j++)
410 { 456 {
411 sym_layout[i ][j] = 457 sym_layout[i ][j] =
412 sym_layout[sym_layout.w - i - 1][j] = col[i][j]; 458 sym_layout[sym_layout.w - i - 1][j] = data [i][j];
413 } 459 }
414 460
415 if (symmetry == SYMMETRY_Y) 461 if (symmetry == SYMMETRY_Y)
416 for (int i = 0; i < sym_layout.w; i++) 462 for (int i = 0; i < sym_layout.w; i++)
417 for (int j = 0; j < sym_layout.h / 2 + 1; j++) 463 for (int j = 0; j < sym_layout.h / 2 + 1; j++)
418 { 464 {
419 sym_layout[i][j ] = 465 sym_layout[i][j ] =
420 sym_layout[i][sym_layout.h - j - 1] = col[i][j]; 466 sym_layout[i][sym_layout.h - j - 1] = data [i][j];
421 } 467 }
422 468
423 if (symmetry == SYMMETRY_XY) 469 if (symmetry == SYMMETRY_XY)
424 for (int i = 0; i < sym_layout.w / 2 + 1; i++) 470 for (int i = 0; i < sym_layout.w / 2 + 1; i++)
425 for (int j = 0; j < sym_layout.h / 2 + 1; j++) 471 for (int j = 0; j < sym_layout.h / 2 + 1; j++)
426 { 472 {
427 sym_layout[i ][j ] = 473 sym_layout[i ][j ] =
428 sym_layout[i ][sym_layout.h - j - 1] = 474 sym_layout[i ][sym_layout.h - j - 1] =
429 sym_layout[sym_layout.w - i - 1][j ] = 475 sym_layout[sym_layout.w - i - 1][j ] =
430 sym_layout[sym_layout.w - i - 1][sym_layout.h - j - 1] = col[i][j]; 476 sym_layout[sym_layout.w - i - 1][sym_layout.h - j - 1] = data [i][j];
431 } 477 }
432 478
433 /* need to run the isolation remover for some layouts */ 479 /* need to run the isolation remover for some layouts */
434#if 0 480#if 0
435 switch (RP->map_layout_style) 481 switch (RP->map_layout_style)
461 { 507 {
462 layout new_layout (w, h); 508 layout new_layout (w, h);
463 509
464 for (int i = 0; i < w; i++) /* copy a reflection back */ 510 for (int i = 0; i < w; i++) /* copy a reflection back */
465 for (int j = 0; j < h; j++) 511 for (int j = 0; j < h; j++)
466 new_layout [i][j] = col [w - i - 1][h - j - 1]; 512 new_layout [i][j] = data [w - i - 1][h - j - 1];
467 513
468 swap (new_layout); 514 swap (new_layout);
469 } 515 }
470 break; 516 break;
471 517
475 layout new_layout (h, w); 521 layout new_layout (h, w);
476 522
477 if (rotation == 1) /* swap x and y */ 523 if (rotation == 1) /* swap x and y */
478 for (int i = 0; i < w; i++) 524 for (int i = 0; i < w; i++)
479 for (int j = 0; j < h; j++) 525 for (int j = 0; j < h; j++)
480 new_layout [j][i] = col [i][j]; 526 new_layout [j][i] = data [i][j];
481 527
482 if (rotation == 3) /* swap x and y */ 528 if (rotation == 3) /* swap x and y */
483 for (int i = 0; i < w; i++) 529 for (int i = 0; i < w; i++)
484 for (int j = 0; j < h; j++) 530 for (int j = 0; j < h; j++)
485 new_layout [j][i] = col [w - i - 1][h - j - 1]; 531 new_layout [j][i] = data [w - i - 1][h - j - 1];
486 532
487 swap (new_layout); 533 swap (new_layout);
488 } 534 }
489 break; 535 break;
490 } 536 }
616 662
617 new_layout.clear (); 663 new_layout.clear ();
618 664
619 for (int i = 0; i < w; i++) 665 for (int i = 0; i < w; i++)
620 for (int j = 0; j < h; j++) 666 for (int j = 0; j < h; j++)
621 switch (col [i][j]) 667 switch (data [i][j])
622 { 668 {
623 case '#': expand_wall (new_layout, i, j, *this); break; 669 case '#': expand_wall (new_layout, i, j, *this); break;
624 case 'D': expand_door (new_layout, i, j, *this); break; 670 case 'D': expand_door (new_layout, i, j, *this); break;
625 default: expand_misc (new_layout, i, j, *this); break; 671 default: expand_misc (new_layout, i, j, *this); break;
626 } 672 }
860{ 906{
861 demo () 907 demo ()
862 { 908 {
863 rmg_rndm.seed (time (0)); 909 rmg_rndm.seed (time (0));
864 910
865 for(int i=1;i<10;i) 911 for(int i=1;i<100;i++)
866 { 912 {
867 layout maze (10, 10); 913 layout maze (40, 25);
868 maze.fill_rand (90); 914 maze.fill_rand (85);
869 maze.border (); 915 maze.border ();
870 maze.isolation_remover (); 916 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 (); 917 maze.print ();
876 } 918 }
877 919
878 exit (1); 920 exit (1);
879 } 921 }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines