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.12 by root, Sat Jul 3 01:49:18 2010 UTC vs.
Revision 1.24 by root, Mon Jul 5 00:07:21 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 28void noinline
29layout::alloc (int w, int h) 29layout::alloc (int w, int h)
30{ 30{
31 assert (sizeof (cell) == 1); 31 assert (sizeof (cell) == 1);
32 32
33 this->w = w; 33 this->w = w;
34 this->h = h; 34 this->h = h;
35 35
36 // we store the layout in a single contiguous memory layout 36 // we store the layout in a single contiguous memory layout
37 // first part consists of pointers to each column, followed 37 // first part consists of pointers to each column, followed
38 // by the actual columns (not rows!) 38 // by the actual columns (not rows!)
39 int size = (sizeof (cell *) + sizeof (cell) * h) * w; 39 size = (sizeof (cell *) + sizeof (cell) * h) * w;
40
41 data = (cell **)salloc<char> (size); 40 data = (cell **)salloc<char> (size);
42 41
43 cell *p = (cell *)(data + w); 42 cell *p = (cell *)(data + w);
44 43
45 for (int x = w; x--; ) 44 for (int x = 0; x < w; ++x)
46 data [x] = p + x * h; 45 data [x] = p + x * h;
47} 46}
48 47
49layout::layout (int w, int h) 48layout::layout (int w, int h)
50{ 49{
56 alloc (copy.w, copy.h); 55 alloc (copy.w, copy.h);
57 56
58 memcpy (data [0], copy.data [0], sizeof (cell) * h * w); 57 memcpy (data [0], copy.data [0], sizeof (cell) * h * w);
59} 58}
60 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;
67 data = (cell **)salloc<char> (size);
68
69 // and now we point back into the original layout
70 for (int x = 0; x < w; ++x)
71 data [x] = orig.data [x + x1] + y1;
72}
73
61layout::~layout () 74layout::~layout ()
62{ 75{
63 int size = (sizeof (cell *) + sizeof (cell) * h) * w;
64
65 sfree ((char *)data, size); 76 sfree ((char *)data, size);
66} 77}
67 78
68void 79void noinline
69layout::fill (char fill) 80layout::fill (char fill)
70{ 81{
71 memset (data [0], fill, w * h); 82 //memset (data [0], fill, w * h); // only when contiguous :/
83 fill_rect (0, 0, w, h, fill);
72} 84}
73 85
74void 86void noinline
87layout::replace (char from, char to)
88{
89 for (int x = 0; x < w; ++x)
90 for (int y = 0; y < h; ++y)
91 if (data [x][y] == from)
92 data [x][y] = to;
93}
94
95void noinline
75layout::rect (int x1, int y1, int x2, int y2, char fill) 96layout::rect (int x1, int y1, int x2, int y2, char fill)
76{ 97{
77 --x2; 98 --x2;
78 99
79 memset (data [x1] + y1, fill, y2 - y1); 100 memset (data [x1] + y1, fill, y2 - y1);
81 102
82 while (++x1 < x2) 103 while (++x1 < x2)
83 data [x1][y1] = data [x1][y2 - 1] = fill; 104 data [x1][y1] = data [x1][y2 - 1] = fill;
84} 105}
85 106
86void 107void noinline
87layout::fill_rect (int x1, int y1, int x2, int y2, char fill) 108layout::fill_rect (int x1, int y1, int x2, int y2, char fill)
88{ 109{
89 for (; x1 < x2; ++x1) 110 for (; x1 < x2; ++x1)
90 memset (data [x1] + y1, fill, y2 - y1); 111 memset (data [x1] + y1, fill, y2 - y1);
91} 112}
92 113
114void
93void layout::border (char fill) 115layout::border (char fill)
94{ 116{
95 rect (0, 0, w, h, fill); 117 rect (0, 0, w, h, fill);
96} 118}
97 119
98void 120void noinline
99layout::fill_rand (int percent) 121layout::fill_rand (int percent)
100{ 122{
101 percent = lerp (percent, 0, 100, 0, 256); 123 percent = lerp (percent, 0, 100, 0, 256);
102 124
103 for (int x = w - 1; --x > 0; ) 125 for (int x = 0; x < w; ++x)
104 for (int y = h - 1; --y > 0; ) 126 for (int y = 0; y < h; ++y)
105 data [x][y] = rmg_rndm (256) > percent ? 0 : '#'; 127 data [x][y] = rmg_rndm (256) > percent ? 0 : '#';
106} 128}
107 129
108///////////////////////////////////////////////////////////////////////////// 130/////////////////////////////////////////////////////////////////////////////
109 131
110// erode by cellular automata 132// erode by cellular automata
111void 133void noinline
112layout::erode_1_2 (int c1, int c2, int repeat) 134layout::erode_1_2 (int c1, int c2, int repeat)
113{ 135{
114 layout neu (w, h); 136 layout neu (w, h);
115 137
116 while (repeat--) 138 while (repeat--)
208 if (x > 0 && !dist [x - 1][y]) push_flood_fill (dist, seeds, x - 1, y); 230 if (x > 0 && !dist [x - 1][y]) 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); 231 if (x < dist.w - 1 && !dist [x + 1][y]) push_flood_fill (dist, seeds, x + 1, y);
210 } 232 }
211} 233}
212 234
213static inline void 235static void inline
214make_tunnel (layout &dist, pointlist &seeds, int x, int y, U8 d) 236make_tunnel (layout &dist, pointlist &seeds, int x, int y, U8 d, int perturb)
215{ 237{
216 for (;;) 238 for (;;)
217 { 239 {
218 point neigh[4]; 240 point neigh[4];
219 int ncnt = 0; 241 int ncnt = 0;
220 242
243 d += perturb > 1;
244
221 if (x > 1 && U8 (dist [x - 1][y]) <= d && dist [x - 1][y] > 1) neigh [ncnt++] = point (x - 1, y); 245 if (x > 0 && U8 (dist [x - 1][y]) < d && dist [x - 1][y] > 1) neigh [ncnt++] = point (x - 1, y);
222 if (x < dist.w - 2 && U8 (dist [x + 1][y]) <= d && dist [x + 1][y] > 1) neigh [ncnt++] = point (x + 1, y); 246 if (x < dist.w - 1 && U8 (dist [x + 1][y]) < d && dist [x + 1][y] > 1) neigh [ncnt++] = point (x + 1, y);
223 if (y > 1 && U8 (dist [x][y - 1]) <= d && dist [x][y - 1] > 1) neigh [ncnt++] = point (x, y - 1); 247 if (y > 0 && U8 (dist [x][y - 1]) < d && dist [x][y - 1] > 1) neigh [ncnt++] = point (x, y - 1);
224 if (y < dist.h - 2 && U8 (dist [x][y + 1]) <= d && dist [x][y + 1] > 1) neigh [ncnt++] = point (x, y + 1); 248 if (y < dist.h - 1 && U8 (dist [x][y + 1]) < d && dist [x][y + 1] > 1) neigh [ncnt++] = point (x, y + 1);
225
226 printf ("tunnel %d+%d ncnt %d\n", x, y, ncnt);//D
227 249
228 if (!ncnt) 250 if (!ncnt)
229 return; 251 return;
230 252
231 point &p = neigh [rmg_rndm (ncnt)]; 253 point p = neigh [perturb ? rmg_rndm (ncnt) : 0];
232 254
233 seeds.push (p); 255 seeds.push (p);
234 256
235 x = p.x; 257 x = p.x;
236 y = p.y; 258 y = p.y;
254} 276}
255 277
256// isolation remover, works on a "distance" map 278// isolation remover, works on a "distance" map
257// the map must be initialised with 0 == rooms, 255 = walls 279// the map must be initialised with 0 == rooms, 255 = walls
258static void noinline 280static void noinline
259isolation_remover (layout &dist) 281isolation_remover (layout &dist, unsigned int perturb = 2)
260{ 282{
261 // dist contains 283 // dist contains
262 // 0 == invisited rooms 284 // 0 == invisited rooms
263 // 1 == visited rooms 285 // 1 == visited rooms
264 // 2+ shortest distance to random near room 286 // 2+ shortest distance to random near room
265 287
288 clamp_it (perturb, 0, 2);
289
266 // phase 1, find seed 290 // phase 1, find seed
267 int cnt = 0; 291 int cnt = 0;
268 int x, y; 292 int x, y;
269 293
270 for (int i = 0; i < dist.w; ++i) 294 for (int i = 0; i < dist.w; ++i)
275 if (!cnt) 299 if (!cnt)
276 { 300 {
277 // map is completely massive, this is not good, 301 // map is completely massive, this is not good,
278 // so make it empty instead. 302 // so make it empty instead.
279 dist.fill (1); 303 dist.fill (1);
280 dist.border (255);
281 return; 304 return;
282 } 305 }
283 306
284 fixed_stack<point> seeds (dist.w * dist.h * 5); 307 fixed_stack<point> seeds (dist.w * dist.h * 5);
285 308
302 325
303 if (!dist [x][y]) 326 if (!dist [x][y])
304 { 327 {
305 // found new isolated area, make tunnel 328 // found new isolated area, make tunnel
306 push_flood_fill (dist, seeds, x, y); 329 push_flood_fill (dist, seeds, x, y);
307 make_tunnel (dist, seeds, x, y, 255); 330 make_tunnel (dist, seeds, x, y, 254, perturb);
308 } 331 }
309 else 332 else
310 { 333 {
311 // nothing here, continue to expand 334 // nothing here, continue to expand
312 U8 d = U8 (dist [x][y]) + 1; 335 U8 d = U8 (dist [x][y]) + 1;
313 336
314 if (x < dist.w - 2) maybe_push (dist, seeds, x + 1, y, d); 337 if (x < dist.w - 1) maybe_push (dist, seeds, x + 1, y, d);
315 if (x > 1) maybe_push (dist, seeds, x - 1, y, d); 338 if (x > 0) maybe_push (dist, seeds, x - 1, y, d);
316 if (y < dist.h - 2) maybe_push (dist, seeds, x, y + 1, d); 339 if (y < dist.h - 1) maybe_push (dist, seeds, x, y + 1, d);
317 if (y > 1) maybe_push (dist, seeds, x, y - 1, d); 340 if (y > 0) maybe_push (dist, seeds, x, y - 1, d);
318 } 341 }
319 } 342 }
320} 343}
321 344
322void 345void
323layout::isolation_remover () 346layout::isolation_remover (int perturb)
324{ 347{
325 layout dist (w, h); 348 layout dist (w - 2, h - 2); // map without border
326 349
327 for (int x = 1; x < w - 1; ++x) 350 for (int x = 1; x < w - 1; ++x)
328 for (int y = 1; y < h - 1; ++y) 351 for (int y = 1; y < h - 1; ++y)
329 dist [x][y] = data [x][y] == '#' ? U8 (255) : 0; 352 dist [x - 1][y - 1] = data [x][y] == '#' ? U8 (255) : 0;
330 353
331 ::isolation_remover (dist); 354 ::isolation_remover (dist, perturb);
332 355
333 // now copy the tunnels over 356 // now copy the tunnels over
334 for (int x = 1; x < w - 1; ++x) 357 for (int x = 1; x < w - 1; ++x)
335 for (int y = 1; y < h - 1; ++y) 358 for (int y = 1; y < h - 1; ++y)
336 if (data [x][y] == '#' && dist [x][y] == 1) 359 if (data [x][y] == '#' && dist [x - 1][y - 1] == 1)
337 data [x][y] = 0; 360 data [x][y] = 0;
338}
339
340/////////////////////////////////////////////////////////////////////////////
341
342// inspired mostly by http://www.jimrandomh.org/misc/caves.txt
343void
344layout::gen_cave (int subtype)
345{
346 switch (subtype)
347 {
348 // a rough cave
349 case 0:
350 fill_rand (rmg_rndm (85, 97));
351 break;
352
353 // corridors
354 case 1:
355 fill_rand (rmg_rndm (5, 40));
356 erode_1_2 (5, 2, 10);
357 erode_1_2 (5, -1, 10);
358 erode_1_2 (5, 2, 1);
359 break;
360
361 // somewhat open, roundish
362 case 2:
363 fill_rand (45);
364 erode_1_2 (5, 0, 5);
365 erode_1_2 (5, 1, 1);
366 break;
367
368 // wide open, some room-like structures
369 case 3:
370 fill_rand (45);
371 erode_1_2 (5, 2, 4);
372 erode_1_2 (5, -1, 3);
373 break;
374 }
375
376 border ();
377 isolation_remover ();
378} 361}
379 362
380///////////////////////////////////////////////////////////////////////////// 363/////////////////////////////////////////////////////////////////////////////
381 364
382//+GPL 365//+GPL
384/* puts doors at appropriate locations in a maze. */ 367/* puts doors at appropriate locations in a maze. */
385void 368void
386layout::doorify () 369layout::doorify ()
387{ 370{
388 int ndoors = w * h / 60; /* reasonable number of doors. */ 371 int ndoors = w * h / 60; /* reasonable number of doors. */
389 int doorlocs = 0; /* # of available doorlocations */
390 372
391 uint16 *doorlist_x = salloc<uint16> (w * h); 373 coroapi::cede_to_tick ();
392 uint16 *doorlist_y = salloc<uint16> (w * h); 374
375 fixed_stack<point> doorloc (w * h);
393 376
394 /* make a list of possible door locations */ 377 /* make a list of possible door locations */
395 for (int i = 1; i < w - 1; i++) 378 for (int i = 1; i < w - 1; i++)
396 for (int j = 1; j < h - 1; j++) 379 for (int j = 1; j < h - 1; j++)
397 { 380 {
398 int sindex = surround_flag (*this, i, j); 381 int sindex = surround_flag (*this, i, j);
399 382
400 if (sindex == 3 || sindex == 12) /* these are possible door sindex */ 383 if (sindex == 3 || sindex == 12) /* these are possible door sindex */
401 { 384 doorloc.push (point (i, j));
402 doorlist_x [doorlocs] = i;
403 doorlist_y [doorlocs] = j;
404 doorlocs++;
405 }
406 } 385 }
407 386
408 while (ndoors > 0 && doorlocs > 0) 387 while (ndoors && doorloc.size)
409 { 388 {
410 int di = rmg_rndm (doorlocs); 389 point p = doorloc.remove (rmg_rndm (doorloc.size));
411 int i = doorlist_x [di]; 390
412 int j = doorlist_y [di];
413 int sindex = surround_flag (*this, i, j); 391 int sindex = surround_flag (*this, p.x, p.y);
414 392
415 if (sindex == 3 || sindex == 12) /* these are possible door sindex */ 393 if (sindex == 3 || sindex == 12) /* these are possible door sindex */
416 { 394 {
417 data [i][j] = 'D'; 395 data [p.x][p.y] = 'D';
418 ndoors--; 396 --ndoors;
419 } 397 }
420
421 /* reduce the size of the list */
422 doorlocs--;
423 doorlist_x[di] = doorlist_x [doorlocs];
424 doorlist_y[di] = doorlist_y [doorlocs];
425 } 398 }
426
427 sfree (doorlist_x, w * h);
428 sfree (doorlist_y, w * h);
429} 399}
430 400
431/* takes a map and makes it symmetric: adjusts Xsize and 401/* takes a map and makes it symmetric: adjusts Xsize and
432 * Ysize to produce a symmetric map. 402 * Ysize to produce a symmetric map.
433 */ 403 */
491//-GPL 461//-GPL
492 462
493void 463void
494layout::rotate (int rotation) 464layout::rotate (int rotation)
495{ 465{
466 coroapi::cede_to_tick ();
467
496 switch (rotation & 3) 468 switch (rotation & 3)
497 { 469 {
498 case 2: /* a reflection */ 470 case 2: /* a reflection */
499 { 471 {
500 layout new_layout (w, h); 472 layout new_layout (w, h);
652{ 624{
653 layout new_layout (w * 2 - 1, h * 2 - 1); 625 layout new_layout (w * 2 - 1, h * 2 - 1);
654 626
655 new_layout.clear (); 627 new_layout.clear ();
656 628
629 coroapi::cede_to_tick ();
630
657 for (int i = 0; i < w; i++) 631 for (int i = 0; i < w; i++)
658 for (int j = 0; j < h; j++) 632 for (int j = 0; j < h; j++)
659 switch (data [i][j]) 633 switch (data [i][j])
660 { 634 {
661 case '#': expand_wall (new_layout, i, j, *this); break; 635 case '#': expand_wall (new_layout, i, j, *this); break;
743 717
744 return -1; 718 return -1;
745} 719}
746 720
747int 721int
748make_wall (char **maze, int x, int y, int dir) 722make_wall (layout &maze, int x, int y, int dir)
749{ 723{
750 maze[x][y] = 'D'; /* mark a door */ 724 maze[x][y] = 'D'; /* mark a door */
751 725
752 switch (dir) 726 switch (dir)
753 { 727 {
770 744
771void 745void
772layout::roomify () 746layout::roomify ()
773{ 747{
774 int tries = w * h / 30; 748 int tries = w * h / 30;
749
750 coroapi::cede_to_tick ();
775 751
776 for (int ti = 0; ti < tries; ti++) 752 for (int ti = 0; ti < tries; ti++)
777 { 753 {
778 /* starting location for looking at creating a door */ 754 /* starting location for looking at creating a door */
779 int dx = rmg_rndm (w); 755 int dx = rmg_rndm (w);
802 else 778 else
803 make_wall (*this, dx, dy, 1); 779 make_wall (*this, dx, dy, 1);
804 } 780 }
805} 781}
806 782
783//-GPL
784
807///////////////////////////////////////////////////////////////////////////// 785/////////////////////////////////////////////////////////////////////////////
786
787// inspired mostly by http://www.jimrandomh.org/misc/caves.txt
788void
789layout::gen_cave (int subtype)
790{
791 switch (subtype)
792 {
793 // a rough cave
794 case 0:
795 fill_rand (rmg_rndm (85, 97));
796 break;
797
798 // corridors
799 case 1:
800 fill_rand (rmg_rndm (5, 40));
801 erode_1_2 (5, 2, 10);
802 erode_1_2 (5, -1, 10);
803 erode_1_2 (5, 2, 1);
804 break;
805
806 // somewhat open, some room-like structures
807 case 2:
808 fill_rand (45);
809 erode_1_2 (5, 2, 4);
810 erode_1_2 (5, -1, 3);
811 break;
812
813 // wide open, roundish
814 case 3:
815 fill_rand (45);
816 erode_1_2 (5, 0, 5);
817 erode_1_2 (5, 1, 1);
818 break;
819 }
820
821 border ();
822 isolation_remover (1);
823}
824
825void
826layout::gen_castle ()
827{
828 fill ('#');
829
830 for (int n = w * h / 30 + 1; n--; )
831 {
832 int rw = rmg_rndm (6, 10);
833 int rh = rmg_rndm (6, 10);
834
835 if (rw > w || rh > h)
836 continue;
837
838 int rx = rmg_rndm (0, w - rw);
839 int ry = rmg_rndm (0, h - rh);
840
841 rect (rx, ry, rx + rw, ry + rh, '#');
842 fill_rect (rx + 1, ry + 1, rx + rw - 1, ry + rh - 1, 0);
843 }
844
845 border ();
846 isolation_remover (0);
847}
848
849static void
850gen_mixed_ (layout &maze, random_map_params *RP)
851{
852 if (maze.w > maze.h && maze.w > 16)
853 {
854 int m = rmg_rndm (8, maze.w - 8);
855
856 layout m1 (maze, 0, 0, m , maze.h); gen_mixed_ (m1, RP);
857 layout m2 (maze, m, 0, maze.w, maze.h); gen_mixed_ (m2, RP);
858 }
859 else if (maze.h > 16)
860 {
861 int m = rmg_rndm (8, maze.h - 8);
862
863 layout m1 (maze, 0, 0, maze.w, m ); gen_mixed_ (m1, RP);
864 layout m2 (maze, 0, m, maze.w, maze.h); gen_mixed_ (m2, RP);
865 }
866 else
867 {
868 RP->map_layout_style = rmg_rndm (NROFLAYOUTS - 2) + 1;
869
870 if (RP->map_layout_style == LAYOUT_MULTIPLE)
871 ++RP->map_layout_style;
872
873 maze.generate (RP);
874 }
875
876 coroapi::cede_to_tick ();
877}
878
879// recursive subdivision with random sublayouts
880static void
881gen_mixed (layout &maze, random_map_params *RP)
882{
883 random_map_params &rp = *new random_map_params (RP);
884 gen_mixed_ (maze, &rp);
885 delete &rp;
886
887 maze.border ();
888
889 // exits currently do not work so well, as they
890 // are currently often found together, so nuke entrances
891 maze.replace ('<', ' ');
892
893 maze.isolation_remover (0);
894}
895
896//+GPL
808 897
809/* function selects the maze function and gives it whatever 898/* function selects the maze function and gives it whatever
810 arguments it needs. */ 899 arguments it needs. */
811void 900void
812layout::generate (random_map_params *RP) 901layout::generate (random_map_params *RP)
872 if (rmg_rndm (2)) 961 if (rmg_rndm (2))
873 doorify (); 962 doorify ();
874 963
875 break; 964 break;
876 965
966 case LAYOUT_CASTLE:
967 gen_castle ();
968
969 if (rmg_rndm (2))
970 doorify ();
971
972 break;
973
974 case LAYOUT_MULTIPLE:
975 gen_mixed (*this, RP);
976 break;
977
877 default: 978 default:
878 abort (); 979 abort ();
879 } 980 }
981}
880 982
881 /* rotate the maze randomly */ 983//-GPL
882 rotate (rmg_rndm (4));
883
884 symmetrize (RP->symmetry_used);
885 984
886#if 0 985#if 0
887 print ();//D 986static void
888#endif 987gen_village (layout &maze)
988{
989 maze.clear ();
990 maze.border ();
889 991
890 if (RP->expand2x) 992 for (int n = maze.w * maze.h / 200 + 1; n--; )
891 expand2x (); 993 {
892} 994 int rw = rmg_rndm (6, 10);
995 int rh = rmg_rndm (6, 10);
893 996
894//-GPL 997 int rx = rmg_rndm (2, maze.w - rw - 2);
998 int ry = rmg_rndm (2, maze.h - rh - 2);
895 999
896#if 0 1000 maze.rect (rx, ry, rx + rw, ry + rh, '#');
1001 }
1002
1003 maze.border ();
1004 maze.isolation_remover (2);
1005}
1006
897static struct demo 1007static struct demo
898{ 1008{
899 demo () 1009 demo ()
900 { 1010 {
901 rmg_rndm.seed (time (0)); 1011 rmg_rndm.seed (time (0));
902 1012
903 for(int i=1;i<100;i++) 1013 for(int i=1;i<100;i++)
904 { 1014 {
905 layout maze (40, 25); 1015 layout maze (40, 30);
906 maze.fill_rand (85); 1016 gen_village (maze);
907 maze.border (); 1017 maze.doorify ();
908 maze.isolation_remover ();
909 maze.print (); 1018 maze.print ();
1019 exit(0);
910 } 1020 }
911 1021
912 exit (1); 1022 exit (1);
913 } 1023 }
914} demo; 1024} demo;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines