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.21 by root, Sun Jul 4 01:01:42 2010 UTC

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
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
75layout::rect (int x1, int y1, int x2, int y2, char fill) 87layout::rect (int x1, int y1, int x2, int y2, char fill)
76{ 88{
209 if (x < dist.w - 1 && !dist [x + 1][y]) push_flood_fill (dist, seeds, x + 1, y); 221 if (x < dist.w - 1 && !dist [x + 1][y]) push_flood_fill (dist, seeds, x + 1, y);
210 } 222 }
211} 223}
212 224
213static inline void 225static inline void
214make_tunnel (layout &dist, pointlist &seeds, int x, int y, U8 d) 226make_tunnel (layout &dist, pointlist &seeds, int x, int y, U8 d, int perturb)
215{ 227{
216 for (;;) 228 for (;;)
217 { 229 {
218 point neigh[4]; 230 point neigh[4];
219 int ncnt = 0; 231 int ncnt = 0;
220 232
233 d += perturb > 1;
234
221 if (x > 0 && U8 (dist [x - 1][y]) <= d && dist [x - 1][y] > 1) neigh [ncnt++] = point (x - 1, y); 235 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); 236 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); 237 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); 238 if (y < dist.h - 1 && U8 (dist [x][y + 1]) < d && dist [x][y + 1] > 1) neigh [ncnt++] = point (x, y + 1);
225 239
226 if (!ncnt) 240 if (!ncnt)
227 return; 241 return;
228 242
229 point &p = neigh [rmg_rndm (ncnt)]; 243 point p = neigh [perturb ? rmg_rndm (ncnt) : 0];
230 244
231 seeds.push (p); 245 seeds.push (p);
232 246
233 x = p.x; 247 x = p.x;
234 y = p.y; 248 y = p.y;
252} 266}
253 267
254// isolation remover, works on a "distance" map 268// isolation remover, works on a "distance" map
255// the map must be initialised with 0 == rooms, 255 = walls 269// the map must be initialised with 0 == rooms, 255 = walls
256static void noinline 270static void noinline
257isolation_remover (layout &dist) 271isolation_remover (layout &dist, unsigned int perturb = 2)
258{ 272{
259 // dist contains 273 // dist contains
260 // 0 == invisited rooms 274 // 0 == invisited rooms
261 // 1 == visited rooms 275 // 1 == visited rooms
262 // 2+ shortest distance to random near room 276 // 2+ shortest distance to random near room
263 277
278 clamp_it (perturb, 0, 2);
279
264 // phase 1, find seed 280 // phase 1, find seed
265 int cnt = 0; 281 int cnt = 0;
266 int x, y; 282 int x, y;
267 283
268 for (int i = 0; i < dist.w; ++i) 284 for (int i = 0; i < dist.w; ++i)
299 315
300 if (!dist [x][y]) 316 if (!dist [x][y])
301 { 317 {
302 // found new isolated area, make tunnel 318 // found new isolated area, make tunnel
303 push_flood_fill (dist, seeds, x, y); 319 push_flood_fill (dist, seeds, x, y);
304 make_tunnel (dist, seeds, x, y, 255); 320 make_tunnel (dist, seeds, x, y, 254, perturb);
305 } 321 }
306 else 322 else
307 { 323 {
308 // nothing here, continue to expand 324 // nothing here, continue to expand
309 U8 d = U8 (dist [x][y]) + 1; 325 U8 d = U8 (dist [x][y]) + 1;
315 } 331 }
316 } 332 }
317} 333}
318 334
319void 335void
320layout::isolation_remover () 336layout::isolation_remover (int perturb)
321{ 337{
322 layout dist (w - 2, h - 2); // map without border 338 layout dist (w - 2, h - 2); // map without border
323 339
324 for (int x = 1; x < w - 1; ++x) 340 for (int x = 1; x < w - 1; ++x)
325 for (int y = 1; y < h - 1; ++y) 341 for (int y = 1; y < h - 1; ++y)
326 dist [x - 1][y - 1] = data [x][y] == '#' ? U8 (255) : 0; 342 dist [x - 1][y - 1] = data [x][y] == '#' ? U8 (255) : 0;
327 343
328 ::isolation_remover (dist); 344 ::isolation_remover (dist, perturb);
329 345
330 // now copy the tunnels over 346 // now copy the tunnels over
331 for (int x = 1; x < w - 1; ++x) 347 for (int x = 1; x < w - 1; ++x)
332 for (int y = 1; y < h - 1; ++y) 348 for (int y = 1; y < h - 1; ++y)
333 if (data [x][y] == '#' && dist [x - 1][y - 1] == 1) 349 if (data [x][y] == '#' && dist [x - 1][y - 1] == 1)
334 data [x][y] = 0; 350 data [x][y] = 0;
335} 351}
336 352
337///////////////////////////////////////////////////////////////////////////// 353/////////////////////////////////////////////////////////////////////////////
338 354
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 355//+GPL
380 356
381/* puts doors at appropriate locations in a maze. */ 357/* puts doors at appropriate locations in a maze. */
382void 358void
383layout::doorify () 359layout::doorify ()
384{ 360{
385 int ndoors = w * h / 60; /* reasonable number of doors. */ 361 int ndoors = w * h / 60; /* reasonable number of doors. */
386 int doorlocs = 0; /* # of available doorlocations */
387 362
388 uint16 *doorlist_x = salloc<uint16> (w * h); 363 fixed_stack<point> doorloc (w * h);
389 uint16 *doorlist_y = salloc<uint16> (w * h);
390 364
391 /* make a list of possible door locations */ 365 /* make a list of possible door locations */
392 for (int i = 1; i < w - 1; i++) 366 for (int i = 1; i < w - 1; i++)
393 for (int j = 1; j < h - 1; j++) 367 for (int j = 1; j < h - 1; j++)
394 { 368 {
395 int sindex = surround_flag (*this, i, j); 369 int sindex = surround_flag (*this, i, j);
396 370
397 if (sindex == 3 || sindex == 12) /* these are possible door sindex */ 371 if (sindex == 3 || sindex == 12) /* these are possible door sindex */
398 { 372 doorloc.push (point (i, j));
399 doorlist_x [doorlocs] = i;
400 doorlist_y [doorlocs] = j;
401 doorlocs++;
402 }
403 } 373 }
404 374
405 while (ndoors > 0 && doorlocs > 0) 375 while (ndoors && doorloc.size)
406 { 376 {
407 int di = rmg_rndm (doorlocs); 377 point p = doorloc.remove (rmg_rndm (doorloc.size));
408 int i = doorlist_x [di]; 378
409 int j = doorlist_y [di];
410 int sindex = surround_flag (*this, i, j); 379 int sindex = surround_flag (*this, p.x, p.y);
411 380
412 if (sindex == 3 || sindex == 12) /* these are possible door sindex */ 381 if (sindex == 3 || sindex == 12) /* these are possible door sindex */
413 { 382 {
414 data [i][j] = 'D'; 383 data [p.x][p.y] = 'D';
415 ndoors--; 384 --ndoors;
416 } 385 }
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 } 386 }
423
424 sfree (doorlist_x, w * h);
425 sfree (doorlist_y, w * h);
426} 387}
427 388
428/* takes a map and makes it symmetric: adjusts Xsize and 389/* takes a map and makes it symmetric: adjusts Xsize and
429 * Ysize to produce a symmetric map. 390 * Ysize to produce a symmetric map.
430 */ 391 */
799 else 760 else
800 make_wall (*this, dx, dy, 1); 761 make_wall (*this, dx, dy, 1);
801 } 762 }
802} 763}
803 764
765//-GPL
766
804///////////////////////////////////////////////////////////////////////////// 767/////////////////////////////////////////////////////////////////////////////
768
769// inspired mostly by http://www.jimrandomh.org/misc/caves.txt
770void
771layout::gen_cave (int subtype)
772{
773 switch (subtype)
774 {
775 // a rough cave
776 case 0:
777 fill_rand (rmg_rndm (85, 97));
778 break;
779
780 // corridors
781 case 1:
782 fill_rand (rmg_rndm (5, 40));
783 erode_1_2 (5, 2, 10);
784 erode_1_2 (5, -1, 10);
785 erode_1_2 (5, 2, 1);
786 break;
787
788 // somewhat open, some room-like structures
789 case 2:
790 fill_rand (45);
791 erode_1_2 (5, 2, 4);
792 erode_1_2 (5, -1, 3);
793 break;
794
795 // wide open, roundish
796 case 3:
797 fill_rand (45);
798 erode_1_2 (5, 0, 5);
799 erode_1_2 (5, 1, 1);
800 break;
801 }
802
803 border ();
804 isolation_remover (1);
805}
806
807void
808layout::gen_castle ()
809{
810 fill ('#');
811
812 for (int n = w * h / 30 + 1; n--; )
813 {
814 int rw = rmg_rndm (6, 10);
815 int rh = rmg_rndm (6, 10);
816
817 if (rw > w || rh > h)
818 continue;
819
820 int rx = rmg_rndm (0, w - rw);
821 int ry = rmg_rndm (0, h - rh);
822
823 rect (rx, ry, rx + rw, ry + rh, '#');
824 fill_rect (rx + 1, ry + 1, rx + rw - 1, ry + rh - 1, 0);
825 }
826
827 border ();
828 isolation_remover (0);
829}
830
831static void
832gen_mixed_ (layout &maze, random_map_params *RP)
833{
834 int dir;
835
836 if (maze.w < 20 && maze.h < 20 && !rmg_rndm (3))
837 dir = 2; // stop recursion randomly
838 else
839 dir = maze.w > maze.h;
840
841 if (dir == 0 && maze.w > 16)
842 {
843 int m = rmg_rndm (8, maze.w - 8);
844
845 layout m1 (maze, 0, 0, m , maze.h); gen_mixed_ (m1, RP);
846 layout m2 (maze, m, 0, maze.w, maze.h); gen_mixed_ (m2, RP);
847 }
848 else if (dir == 1 && maze.h > 16)
849 {
850 int m = rmg_rndm (8, maze.h - 8);
851
852 layout m1 (maze, 0, 0, maze.w, m ); gen_mixed_ (m1, RP);
853 layout m2 (maze, 0, m, maze.w, maze.h); gen_mixed_ (m2, RP);
854 }
855 else
856 {
857 RP->map_layout_style = rmg_rndm (NROFLAYOUTS - 2) + 1;
858
859 if (RP->map_layout_style == LAYOUT_MULTIPLE)
860 ++RP->map_layout_style;
861
862 maze.generate (RP);
863 }
864}
865
866// recursive subdivision with random sublayouts
867static void
868gen_mixed (layout &maze, random_map_params *RP)
869{
870 random_map_params &rp = *new random_map_params (RP);
871 gen_mixed_ (maze, &rp);
872 delete &rp;
873
874 maze.border ();
875 maze.isolation_remover (0);
876}
877
878//+GPL
805 879
806/* function selects the maze function and gives it whatever 880/* function selects the maze function and gives it whatever
807 arguments it needs. */ 881 arguments it needs. */
808void 882void
809layout::generate (random_map_params *RP) 883layout::generate (random_map_params *RP)
869 if (rmg_rndm (2)) 943 if (rmg_rndm (2))
870 doorify (); 944 doorify ();
871 945
872 break; 946 break;
873 947
948 case LAYOUT_CASTLE:
949 gen_castle ();
950
951 if (rmg_rndm (2))
952 doorify ();
953
954 break;
955
956 case LAYOUT_MULTIPLE:
957 gen_mixed (*this, RP);
958 break;
959
874 default: 960 default:
875 abort (); 961 abort ();
876 } 962 }
963}
877 964
878 /* rotate the maze randomly */ 965//-GPL
879 rotate (rmg_rndm (4));
880
881 symmetrize (RP->symmetry_used);
882 966
883#if 0 967#if 0
884 print ();//D 968static void
885#endif 969gen_village (layout &maze)
970{
971 maze.clear ();
972 maze.border ();
886 973
887 if (RP->expand2x) 974 for (int n = maze.w * maze.h / 200 + 1; n--; )
888 expand2x (); 975 {
889} 976 int rw = rmg_rndm (6, 10);
977 int rh = rmg_rndm (6, 10);
890 978
891//-GPL 979 int rx = rmg_rndm (2, maze.w - rw - 2);
980 int ry = rmg_rndm (2, maze.h - rh - 2);
892 981
893#if 0 982 maze.rect (rx, ry, rx + rw, ry + rh, '#');
983 }
984
985 maze.border ();
986 maze.isolation_remover (2);
987}
988
894static struct demo 989static struct demo
895{ 990{
896 demo () 991 demo ()
897 { 992 {
898 rmg_rndm.seed (time (0)); 993 rmg_rndm.seed (time (0));
899 994
900 for(int i=1;i<100;i++) 995 for(int i=1;i<100;i++)
901 { 996 {
902 layout maze (40, 25); 997 layout maze (40, 30);
903 maze.fill_rand (85); 998 gen_village (maze);
904 maze.border (); 999 maze.doorify ();
905 maze.isolation_remover ();
906 maze.print (); 1000 maze.print ();
1001 exit(0);
907 } 1002 }
908 1003
909 exit (1); 1004 exit (1);
910 } 1005 }
911} demo; 1006} demo;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines