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.13 by root, Sat Jul 3 01:52:52 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 > 0 && 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 - 1 && 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 > 0 && 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 - 1 && 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 249
226 if (!ncnt) 250 if (!ncnt)
227 return; 251 return;
228 252
229 point &p = neigh [rmg_rndm (ncnt)]; 253 point p = neigh [perturb ? rmg_rndm (ncnt) : 0];
230 254
231 seeds.push (p); 255 seeds.push (p);
232 256
233 x = p.x; 257 x = p.x;
234 y = p.y; 258 y = p.y;
252} 276}
253 277
254// isolation remover, works on a "distance" map 278// isolation remover, works on a "distance" map
255// the map must be initialised with 0 == rooms, 255 = walls 279// the map must be initialised with 0 == rooms, 255 = walls
256static void noinline 280static void noinline
257isolation_remover (layout &dist) 281isolation_remover (layout &dist, unsigned int perturb = 2)
258{ 282{
259 // dist contains 283 // dist contains
260 // 0 == invisited rooms 284 // 0 == invisited rooms
261 // 1 == visited rooms 285 // 1 == visited rooms
262 // 2+ shortest distance to random near room 286 // 2+ shortest distance to random near room
263 287
288 clamp_it (perturb, 0, 2);
289
264 // phase 1, find seed 290 // phase 1, find seed
265 int cnt = 0; 291 int cnt = 0;
266 int x, y; 292 int x, y;
267 293
268 for (int i = 0; i < dist.w; ++i) 294 for (int i = 0; i < dist.w; ++i)
299 325
300 if (!dist [x][y]) 326 if (!dist [x][y])
301 { 327 {
302 // found new isolated area, make tunnel 328 // found new isolated area, make tunnel
303 push_flood_fill (dist, seeds, x, y); 329 push_flood_fill (dist, seeds, x, y);
304 make_tunnel (dist, seeds, x, y, 255); 330 make_tunnel (dist, seeds, x, y, 254, perturb);
305 } 331 }
306 else 332 else
307 { 333 {
308 // nothing here, continue to expand 334 // nothing here, continue to expand
309 U8 d = U8 (dist [x][y]) + 1; 335 U8 d = U8 (dist [x][y]) + 1;
315 } 341 }
316 } 342 }
317} 343}
318 344
319void 345void
320layout::isolation_remover () 346layout::isolation_remover (int perturb)
321{ 347{
322 layout dist (w - 2, h - 2); // map without border 348 layout dist (w - 2, h - 2); // map without border
323 349
324 for (int x = 1; x < w - 1; ++x) 350 for (int x = 1; x < w - 1; ++x)
325 for (int y = 1; y < h - 1; ++y) 351 for (int y = 1; y < h - 1; ++y)
326 dist [x - 1][y - 1] = data [x][y] == '#' ? U8 (255) : 0; 352 dist [x - 1][y - 1] = data [x][y] == '#' ? U8 (255) : 0;
327 353
328 ::isolation_remover (dist); 354 ::isolation_remover (dist, perturb);
329 355
330 // now copy the tunnels over 356 // now copy the tunnels over
331 for (int x = 1; x < w - 1; ++x) 357 for (int x = 1; x < w - 1; ++x)
332 for (int y = 1; y < h - 1; ++y) 358 for (int y = 1; y < h - 1; ++y)
333 if (data [x][y] == '#' && dist [x - 1][y - 1] == 1) 359 if (data [x][y] == '#' && dist [x - 1][y - 1] == 1)
334 data [x][y] = 0; 360 data [x][y] = 0;
335} 361}
336 362
337///////////////////////////////////////////////////////////////////////////// 363/////////////////////////////////////////////////////////////////////////////
338 364
339// inspired mostly by http://www.jimrandomh.org/misc/caves.txt
340void
341layout::gen_cave (int subtype)
342{
343 switch (subtype)
344 {
345 // a rough cave
346 case 0:
347 fill_rand (rmg_rndm (85, 97));
348 break;
349
350 // corridors
351 case 1:
352 fill_rand (rmg_rndm (5, 40));
353 erode_1_2 (5, 2, 10);
354 erode_1_2 (5, -1, 10);
355 erode_1_2 (5, 2, 1);
356 break;
357
358 // somewhat open, roundish
359 case 2:
360 fill_rand (45);
361 erode_1_2 (5, 0, 5);
362 erode_1_2 (5, 1, 1);
363 break;
364
365 // wide open, some room-like structures
366 case 3:
367 fill_rand (45);
368 erode_1_2 (5, 2, 4);
369 erode_1_2 (5, -1, 3);
370 break;
371 }
372
373 border ();
374 isolation_remover ();
375}
376
377/////////////////////////////////////////////////////////////////////////////
378
379//+GPL 365//+GPL
380 366
381/* puts doors at appropriate locations in a maze. */ 367/* puts doors at appropriate locations in a maze. */
382void 368void
383layout::doorify () 369layout::doorify ()
384{ 370{
385 int ndoors = w * h / 60; /* reasonable number of doors. */ 371 int ndoors = w * h / 60; /* reasonable number of doors. */
386 int doorlocs = 0; /* # of available doorlocations */
387 372
388 uint16 *doorlist_x = salloc<uint16> (w * h); 373 coroapi::cede_to_tick ();
389 uint16 *doorlist_y = salloc<uint16> (w * h); 374
375 fixed_stack<point> doorloc (w * h);
390 376
391 /* make a list of possible door locations */ 377 /* make a list of possible door locations */
392 for (int i = 1; i < w - 1; i++) 378 for (int i = 1; i < w - 1; i++)
393 for (int j = 1; j < h - 1; j++) 379 for (int j = 1; j < h - 1; j++)
394 { 380 {
395 int sindex = surround_flag (*this, i, j); 381 int sindex = surround_flag (*this, i, j);
396 382
397 if (sindex == 3 || sindex == 12) /* these are possible door sindex */ 383 if (sindex == 3 || sindex == 12) /* these are possible door sindex */
398 { 384 doorloc.push (point (i, j));
399 doorlist_x [doorlocs] = i;
400 doorlist_y [doorlocs] = j;
401 doorlocs++;
402 }
403 } 385 }
404 386
405 while (ndoors > 0 && doorlocs > 0) 387 while (ndoors && doorloc.size)
406 { 388 {
407 int di = rmg_rndm (doorlocs); 389 point p = doorloc.remove (rmg_rndm (doorloc.size));
408 int i = doorlist_x [di]; 390
409 int j = doorlist_y [di];
410 int sindex = surround_flag (*this, i, j); 391 int sindex = surround_flag (*this, p.x, p.y);
411 392
412 if (sindex == 3 || sindex == 12) /* these are possible door sindex */ 393 if (sindex == 3 || sindex == 12) /* these are possible door sindex */
413 { 394 {
414 data [i][j] = 'D'; 395 data [p.x][p.y] = 'D';
415 ndoors--; 396 --ndoors;
416 } 397 }
417
418 /* reduce the size of the list */
419 doorlocs--;
420 doorlist_x[di] = doorlist_x [doorlocs];
421 doorlist_y[di] = doorlist_y [doorlocs];
422 } 398 }
423
424 sfree (doorlist_x, w * h);
425 sfree (doorlist_y, w * h);
426} 399}
427 400
428/* takes a map and makes it symmetric: adjusts Xsize and 401/* takes a map and makes it symmetric: adjusts Xsize and
429 * Ysize to produce a symmetric map. 402 * Ysize to produce a symmetric map.
430 */ 403 */
488//-GPL 461//-GPL
489 462
490void 463void
491layout::rotate (int rotation) 464layout::rotate (int rotation)
492{ 465{
466 coroapi::cede_to_tick ();
467
493 switch (rotation & 3) 468 switch (rotation & 3)
494 { 469 {
495 case 2: /* a reflection */ 470 case 2: /* a reflection */
496 { 471 {
497 layout new_layout (w, h); 472 layout new_layout (w, h);
649{ 624{
650 layout new_layout (w * 2 - 1, h * 2 - 1); 625 layout new_layout (w * 2 - 1, h * 2 - 1);
651 626
652 new_layout.clear (); 627 new_layout.clear ();
653 628
629 coroapi::cede_to_tick ();
630
654 for (int i = 0; i < w; i++) 631 for (int i = 0; i < w; i++)
655 for (int j = 0; j < h; j++) 632 for (int j = 0; j < h; j++)
656 switch (data [i][j]) 633 switch (data [i][j])
657 { 634 {
658 case '#': expand_wall (new_layout, i, j, *this); break; 635 case '#': expand_wall (new_layout, i, j, *this); break;
740 717
741 return -1; 718 return -1;
742} 719}
743 720
744int 721int
745make_wall (char **maze, int x, int y, int dir) 722make_wall (layout &maze, int x, int y, int dir)
746{ 723{
747 maze[x][y] = 'D'; /* mark a door */ 724 maze[x][y] = 'D'; /* mark a door */
748 725
749 switch (dir) 726 switch (dir)
750 { 727 {
767 744
768void 745void
769layout::roomify () 746layout::roomify ()
770{ 747{
771 int tries = w * h / 30; 748 int tries = w * h / 30;
749
750 coroapi::cede_to_tick ();
772 751
773 for (int ti = 0; ti < tries; ti++) 752 for (int ti = 0; ti < tries; ti++)
774 { 753 {
775 /* starting location for looking at creating a door */ 754 /* starting location for looking at creating a door */
776 int dx = rmg_rndm (w); 755 int dx = rmg_rndm (w);
799 else 778 else
800 make_wall (*this, dx, dy, 1); 779 make_wall (*this, dx, dy, 1);
801 } 780 }
802} 781}
803 782
783//-GPL
784
804///////////////////////////////////////////////////////////////////////////// 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
805 897
806/* function selects the maze function and gives it whatever 898/* function selects the maze function and gives it whatever
807 arguments it needs. */ 899 arguments it needs. */
808void 900void
809layout::generate (random_map_params *RP) 901layout::generate (random_map_params *RP)
869 if (rmg_rndm (2)) 961 if (rmg_rndm (2))
870 doorify (); 962 doorify ();
871 963
872 break; 964 break;
873 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
874 default: 978 default:
875 abort (); 979 abort ();
876 } 980 }
981}
877 982
878 /* rotate the maze randomly */ 983//-GPL
879 rotate (rmg_rndm (4));
880
881 symmetrize (RP->symmetry_used);
882 984
883#if 0 985#if 0
884 print ();//D 986static void
885#endif 987gen_village (layout &maze)
988{
989 maze.clear ();
990 maze.border ();
886 991
887 if (RP->expand2x) 992 for (int n = maze.w * maze.h / 200 + 1; n--; )
888 expand2x (); 993 {
889} 994 int rw = rmg_rndm (6, 10);
995 int rh = rmg_rndm (6, 10);
890 996
891//-GPL 997 int rx = rmg_rndm (2, maze.w - rw - 2);
998 int ry = rmg_rndm (2, maze.h - rh - 2);
892 999
893#if 0 1000 maze.rect (rx, ry, rx + rw, ry + rh, '#');
1001 }
1002
1003 maze.border ();
1004 maze.isolation_remover (2);
1005}
1006
894static struct demo 1007static struct demo
895{ 1008{
896 demo () 1009 demo ()
897 { 1010 {
898 rmg_rndm.seed (time (0)); 1011 rmg_rndm.seed (time (0));
899 1012
900 for(int i=1;i<100;i++) 1013 for(int i=1;i<100;i++)
901 { 1014 {
902 layout maze (40, 25); 1015 layout maze (40, 30);
903 maze.fill_rand (85); 1016 gen_village (maze);
904 maze.border (); 1017 maze.doorify ();
905 maze.isolation_remover ();
906 maze.print (); 1018 maze.print ();
1019 exit(0);
907 } 1020 }
908 1021
909 exit (1); 1022 exit (1);
910 } 1023 }
911} demo; 1024} demo;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines