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.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 > 1 && 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 - 2 && 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 > 1 && 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 - 2 && 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
226 printf ("tunnel %d+%d ncnt %d\n", x, y, ncnt);//D
227 239
228 if (!ncnt) 240 if (!ncnt)
229 return; 241 return;
230 242
231 point &p = neigh [rmg_rndm (ncnt)]; 243 point p = neigh [perturb ? rmg_rndm (ncnt) : 0];
232 244
233 seeds.push (p); 245 seeds.push (p);
234 246
235 x = p.x; 247 x = p.x;
236 y = p.y; 248 y = p.y;
254} 266}
255 267
256// isolation remover, works on a "distance" map 268// isolation remover, works on a "distance" map
257// the map must be initialised with 0 == rooms, 255 = walls 269// the map must be initialised with 0 == rooms, 255 = walls
258static void noinline 270static void noinline
259isolation_remover (layout &dist) 271isolation_remover (layout &dist, unsigned int perturb = 2)
260{ 272{
261 // dist contains 273 // dist contains
262 // 0 == invisited rooms 274 // 0 == invisited rooms
263 // 1 == visited rooms 275 // 1 == visited rooms
264 // 2+ shortest distance to random near room 276 // 2+ shortest distance to random near room
265 277
278 clamp_it (perturb, 0, 2);
279
266 // phase 1, find seed 280 // phase 1, find seed
267 int cnt = 0; 281 int cnt = 0;
268 int x, y; 282 int x, y;
269 283
270 for (int i = 0; i < dist.w; ++i) 284 for (int i = 0; i < dist.w; ++i)
275 if (!cnt) 289 if (!cnt)
276 { 290 {
277 // map is completely massive, this is not good, 291 // map is completely massive, this is not good,
278 // so make it empty instead. 292 // so make it empty instead.
279 dist.fill (1); 293 dist.fill (1);
280 dist.border (255);
281 return; 294 return;
282 } 295 }
283 296
284 fixed_stack<point> seeds (dist.w * dist.h * 5); 297 fixed_stack<point> seeds (dist.w * dist.h * 5);
285 298
302 315
303 if (!dist [x][y]) 316 if (!dist [x][y])
304 { 317 {
305 // found new isolated area, make tunnel 318 // found new isolated area, make tunnel
306 push_flood_fill (dist, seeds, x, y); 319 push_flood_fill (dist, seeds, x, y);
307 make_tunnel (dist, seeds, x, y, 255); 320 make_tunnel (dist, seeds, x, y, 254, perturb);
308 } 321 }
309 else 322 else
310 { 323 {
311 // nothing here, continue to expand 324 // nothing here, continue to expand
312 U8 d = U8 (dist [x][y]) + 1; 325 U8 d = U8 (dist [x][y]) + 1;
313 326
314 if (x < dist.w - 2) maybe_push (dist, seeds, x + 1, y, d); 327 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); 328 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); 329 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); 330 if (y > 0) maybe_push (dist, seeds, x, y - 1, d);
318 } 331 }
319 } 332 }
320} 333}
321 334
322void 335void
323layout::isolation_remover () 336layout::isolation_remover (int perturb)
324{ 337{
325 layout dist (w, h); 338 layout dist (w - 2, h - 2); // map without border
326 339
327 for (int x = 1; x < w - 1; ++x) 340 for (int x = 1; x < w - 1; ++x)
328 for (int y = 1; y < h - 1; ++y) 341 for (int y = 1; y < h - 1; ++y)
329 dist [x][y] = data [x][y] == '#' ? U8 (255) : 0; 342 dist [x - 1][y - 1] = data [x][y] == '#' ? U8 (255) : 0;
330 343
331 ::isolation_remover (dist); 344 ::isolation_remover (dist, perturb);
332 345
333 // now copy the tunnels over 346 // now copy the tunnels over
334 for (int x = 1; x < w - 1; ++x) 347 for (int x = 1; x < w - 1; ++x)
335 for (int y = 1; y < h - 1; ++y) 348 for (int y = 1; y < h - 1; ++y)
336 if (data [x][y] == '#' && dist [x][y] == 1) 349 if (data [x][y] == '#' && dist [x - 1][y - 1] == 1)
337 data [x][y] = 0; 350 data [x][y] = 0;
338} 351}
339 352
340///////////////////////////////////////////////////////////////////////////// 353/////////////////////////////////////////////////////////////////////////////
341 354
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}
379
380/////////////////////////////////////////////////////////////////////////////
381
382//+GPL 355//+GPL
383 356
384/* puts doors at appropriate locations in a maze. */ 357/* puts doors at appropriate locations in a maze. */
385void 358void
386layout::doorify () 359layout::doorify ()
387{ 360{
388 int ndoors = w * h / 60; /* reasonable number of doors. */ 361 int ndoors = w * h / 60; /* reasonable number of doors. */
389 int doorlocs = 0; /* # of available doorlocations */
390 362
391 uint16 *doorlist_x = salloc<uint16> (w * h); 363 fixed_stack<point> doorloc (w * h);
392 uint16 *doorlist_y = salloc<uint16> (w * h);
393 364
394 /* make a list of possible door locations */ 365 /* make a list of possible door locations */
395 for (int i = 1; i < w - 1; i++) 366 for (int i = 1; i < w - 1; i++)
396 for (int j = 1; j < h - 1; j++) 367 for (int j = 1; j < h - 1; j++)
397 { 368 {
398 int sindex = surround_flag (*this, i, j); 369 int sindex = surround_flag (*this, i, j);
399 370
400 if (sindex == 3 || sindex == 12) /* these are possible door sindex */ 371 if (sindex == 3 || sindex == 12) /* these are possible door sindex */
401 { 372 doorloc.push (point (i, j));
402 doorlist_x [doorlocs] = i;
403 doorlist_y [doorlocs] = j;
404 doorlocs++;
405 }
406 } 373 }
407 374
408 while (ndoors > 0 && doorlocs > 0) 375 while (ndoors && doorloc.size)
409 { 376 {
410 int di = rmg_rndm (doorlocs); 377 point p = doorloc.remove (rmg_rndm (doorloc.size));
411 int i = doorlist_x [di]; 378
412 int j = doorlist_y [di];
413 int sindex = surround_flag (*this, i, j); 379 int sindex = surround_flag (*this, p.x, p.y);
414 380
415 if (sindex == 3 || sindex == 12) /* these are possible door sindex */ 381 if (sindex == 3 || sindex == 12) /* these are possible door sindex */
416 { 382 {
417 data [i][j] = 'D'; 383 data [p.x][p.y] = 'D';
418 ndoors--; 384 --ndoors;
419 } 385 }
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 } 386 }
426
427 sfree (doorlist_x, w * h);
428 sfree (doorlist_y, w * h);
429} 387}
430 388
431/* takes a map and makes it symmetric: adjusts Xsize and 389/* takes a map and makes it symmetric: adjusts Xsize and
432 * Ysize to produce a symmetric map. 390 * Ysize to produce a symmetric map.
433 */ 391 */
802 else 760 else
803 make_wall (*this, dx, dy, 1); 761 make_wall (*this, dx, dy, 1);
804 } 762 }
805} 763}
806 764
765//-GPL
766
807///////////////////////////////////////////////////////////////////////////// 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
808 879
809/* function selects the maze function and gives it whatever 880/* function selects the maze function and gives it whatever
810 arguments it needs. */ 881 arguments it needs. */
811void 882void
812layout::generate (random_map_params *RP) 883layout::generate (random_map_params *RP)
872 if (rmg_rndm (2)) 943 if (rmg_rndm (2))
873 doorify (); 944 doorify ();
874 945
875 break; 946 break;
876 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
877 default: 960 default:
878 abort (); 961 abort ();
879 } 962 }
963}
880 964
881 /* rotate the maze randomly */ 965//-GPL
882 rotate (rmg_rndm (4));
883
884 symmetrize (RP->symmetry_used);
885 966
886#if 0 967#if 0
887 print ();//D 968static void
888#endif 969gen_village (layout &maze)
970{
971 maze.clear ();
972 maze.border ();
889 973
890 if (RP->expand2x) 974 for (int n = maze.w * maze.h / 200 + 1; n--; )
891 expand2x (); 975 {
892} 976 int rw = rmg_rndm (6, 10);
977 int rh = rmg_rndm (6, 10);
893 978
894//-GPL 979 int rx = rmg_rndm (2, maze.w - rw - 2);
980 int ry = rmg_rndm (2, maze.h - rh - 2);
895 981
896#if 0 982 maze.rect (rx, ry, rx + rw, ry + rh, '#');
983 }
984
985 maze.border ();
986 maze.isolation_remover (2);
987}
988
897static struct demo 989static struct demo
898{ 990{
899 demo () 991 demo ()
900 { 992 {
901 rmg_rndm.seed (time (0)); 993 rmg_rndm.seed (time (0));
902 994
903 for(int i=1;i<100;i++) 995 for(int i=1;i<100;i++)
904 { 996 {
905 layout maze (40, 25); 997 layout maze (40, 30);
906 maze.fill_rand (85); 998 gen_village (maze);
907 maze.border (); 999 maze.doorify ();
908 maze.isolation_remover ();
909 maze.print (); 1000 maze.print ();
1001 exit(0);
910 } 1002 }
911 1003
912 exit (1); 1004 exit (1);
913 } 1005 }
914} demo; 1006} demo;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines