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.14 by root, Sat Jul 3 02:19:10 2010 UTC vs.
Revision 1.28 by root, Sat Apr 23 04:56:52 2011 UTC

1/* 1/*
2 * This file is part of Deliantra, the Roguelike Realtime MMORPG. 2 * This file is part of Deliantra, the Roguelike Realtime MMORPG.
3 * 3 *
4 * Copyright (©) 2010 Marc Alexander Lehmann / Robin Redeker / the Deliantra team 4 * Copyright (©) 2010,2011 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
5 * Copyright (©) Crossfire Development Team (restored, original file without copyright notice) 5 * Copyright (©) 1994-2004 Crossfire Development Team (restored, original file without copyright notice)
6 * 6 *
7 * Deliantra is free software: you can redistribute it and/or modify it under 7 * Deliantra is free software: you can redistribute it and/or modify it under
8 * the terms of the Affero GNU General Public License as published by the 8 * the terms of the Affero GNU General Public License as published by the
9 * Free Software Foundation, either version 3 of the License, or (at your 9 * Free Software Foundation, either version 3 of the License, or (at your
10 * option) any later version. 10 * option) any later version.
20 * 20 *
21 * The authors can be reached via e-mail to <support@deliantra.net> 21 * The authors can be reached via e-mail to <support@deliantra.net>
22 */ 22 */
23 23
24#include <global.h> 24#include <global.h>
25#include <random_map.h> 25#include <rmg.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;
74layout::~layout () 74layout::~layout ()
75{ 75{
76 sfree ((char *)data, size); 76 sfree ((char *)data, size);
77} 77}
78 78
79void 79void noinline
80layout::fill (char fill) 80layout::fill (char fill)
81{ 81{
82 memset (data [0], fill, w * h); 82 //memset (data [0], fill, w * h); // only when contiguous :/
83 fill_rect (0, 0, w, h, fill);
83} 84}
84 85
85void 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
86layout::rect (int x1, int y1, int x2, int y2, char fill) 96layout::rect (int x1, int y1, int x2, int y2, char fill)
87{ 97{
88 --x2; 98 --x2;
89 99
90 memset (data [x1] + y1, fill, y2 - y1); 100 memset (data [x1] + y1, fill, y2 - y1);
92 102
93 while (++x1 < x2) 103 while (++x1 < x2)
94 data [x1][y1] = data [x1][y2 - 1] = fill; 104 data [x1][y1] = data [x1][y2 - 1] = fill;
95} 105}
96 106
97void 107void noinline
98layout::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)
99{ 109{
100 for (; x1 < x2; ++x1) 110 for (; x1 < x2; ++x1)
101 memset (data [x1] + y1, fill, y2 - y1); 111 memset (data [x1] + y1, fill, y2 - y1);
102} 112}
103 113
114void
104void layout::border (char fill) 115layout::border (char fill)
105{ 116{
106 rect (0, 0, w, h, fill); 117 rect (0, 0, w, h, fill);
107} 118}
108 119
109void 120void noinline
110layout::fill_rand (int percent) 121layout::fill_rand (int percent)
111{ 122{
112 percent = lerp (percent, 0, 100, 0, 256); 123 percent = lerp (percent, 0, 100, 0, 256);
113 124
114 for (int x = w - 1; --x > 0; ) 125 for (int x = 0; x < w; ++x)
115 for (int y = h - 1; --y > 0; ) 126 for (int y = 0; y < h; ++y)
116 data [x][y] = rmg_rndm (256) > percent ? 0 : '#'; 127 data [x][y] = rmg_rndm (256) > percent ? 0 : '#';
117} 128}
118 129
119///////////////////////////////////////////////////////////////////////////// 130/////////////////////////////////////////////////////////////////////////////
120 131
121// erode by cellular automata 132// erode by cellular automata
122void 133void noinline
123layout::erode_1_2 (int c1, int c2, int repeat) 134layout::erode_1_2 (int c1, int c2, int repeat)
124{ 135{
125 layout neu (w, h); 136 layout neu (w, h);
126 137
127 while (repeat--) 138 while (repeat--)
219 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);
220 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);
221 } 232 }
222} 233}
223 234
224static inline void 235static void inline
225make_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)
226{ 237{
227 for (;;) 238 for (;;)
228 { 239 {
229 point neigh[4]; 240 point neigh[4];
230 int ncnt = 0; 241 int ncnt = 0;
231 242
243 d += perturb > 1;
244
232 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);
233 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);
234 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);
235 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);
236 249
237 if (!ncnt) 250 if (!ncnt)
238 return; 251 return;
239 252
240 point &p = neigh [rmg_rndm (ncnt)]; 253 point p = neigh [perturb ? rmg_rndm (ncnt) : 0];
241 254
242 seeds.push (p); 255 seeds.push (p);
243 256
244 x = p.x; 257 x = p.x;
245 y = p.y; 258 y = p.y;
263} 276}
264 277
265// isolation remover, works on a "distance" map 278// isolation remover, works on a "distance" map
266// the map must be initialised with 0 == rooms, 255 = walls 279// the map must be initialised with 0 == rooms, 255 = walls
267static void noinline 280static void noinline
268isolation_remover (layout &dist) 281isolation_remover (layout &dist, unsigned int perturb = 2)
269{ 282{
270 // dist contains 283 // dist contains
271 // 0 == invisited rooms 284 // 0 == invisited rooms
272 // 1 == visited rooms 285 // 1 == visited rooms
273 // 2+ shortest distance to random near room 286 // 2+ shortest distance to random near room
274 287
288 clamp_it (perturb, 0, 2);
289
275 // phase 1, find seed 290 // phase 1, find seed
276 int cnt = 0; 291 int cnt = 0;
277 int x, y; 292 int x, y;
278 293
279 for (int i = 0; i < dist.w; ++i) 294 for (int i = 0; i < dist.w; ++i)
297 seeds.push (point (x, y)); 312 seeds.push (point (x, y));
298 313
299 // phase 2, while we have seeds, if 314 // phase 2, while we have seeds, if
300 // seed is empty, floodfill, else grow 315 // seed is empty, floodfill, else grow
301 316
317 int rem_index = 0; // used to remove "somewhat ordered"
318
302 while (seeds.size) 319 while (seeds.size)
303 { 320 {
304 coroapi::cede_to_tick (); 321 coroapi::cede_to_tick ();
305 322
323 int i = perturb
324 ? rmg_rndm (max (0, seeds.size - 8), seeds.size - 1)
325 : rem_index ++ % seeds.size;
326
306 point p = seeds.remove (rmg_rndm (seeds.size)); 327 point p = seeds.remove (i);
307 328
308 x = p.x; 329 x = p.x;
309 y = p.y; 330 y = p.y;
310 331
311 if (!dist [x][y]) 332 if (!dist [x][y])
312 { 333 {
313 // found new isolated area, make tunnel 334 // found new isolated area, make tunnel
314 push_flood_fill (dist, seeds, x, y); 335 push_flood_fill (dist, seeds, x, y);
315 make_tunnel (dist, seeds, x, y, 255); 336 make_tunnel (dist, seeds, x, y, 254, perturb);
316 } 337 }
317 else 338 else
318 { 339 {
319 // nothing here, continue to expand 340 // nothing here, continue to expand
320 U8 d = U8 (dist [x][y]) + 1; 341 U8 d = U8 (dist [x][y]) + 1;
326 } 347 }
327 } 348 }
328} 349}
329 350
330void 351void
331layout::isolation_remover () 352layout::isolation_remover (int perturb)
332{ 353{
333 layout dist (w - 2, h - 2); // map without border 354 layout dist (w - 2, h - 2); // map without border
334 355
335 for (int x = 1; x < w - 1; ++x) 356 for (int x = 1; x < w - 1; ++x)
336 for (int y = 1; y < h - 1; ++y) 357 for (int y = 1; y < h - 1; ++y)
337 dist [x - 1][y - 1] = data [x][y] == '#' ? U8 (255) : 0; 358 dist [x - 1][y - 1] = data [x][y] == '#' ? U8 (255) : 0;
338 359
339 ::isolation_remover (dist); 360 ::isolation_remover (dist, perturb);
340 361
341 // now copy the tunnels over 362 // now copy the tunnels over
342 for (int x = 1; x < w - 1; ++x) 363 for (int x = 1; x < w - 1; ++x)
343 for (int y = 1; y < h - 1; ++y) 364 for (int y = 1; y < h - 1; ++y)
344 if (data [x][y] == '#' && dist [x - 1][y - 1] == 1) 365 if (data [x][y] == '#' && dist [x - 1][y - 1] == 1)
345 data [x][y] = 0; 366 data [x][y] = 0;
346} 367}
347 368
348///////////////////////////////////////////////////////////////////////////// 369/////////////////////////////////////////////////////////////////////////////
349 370
350// inspired mostly by http://www.jimrandomh.org/misc/caves.txt
351void
352layout::gen_cave (int subtype)
353{
354 switch (subtype)
355 {
356 // a rough cave
357 case 0:
358 fill_rand (rmg_rndm (85, 97));
359 break;
360
361 // corridors
362 case 1:
363 fill_rand (rmg_rndm (5, 40));
364 erode_1_2 (5, 2, 10);
365 erode_1_2 (5, -1, 10);
366 erode_1_2 (5, 2, 1);
367 break;
368
369 // somewhat open, roundish
370 case 2:
371 fill_rand (45);
372 erode_1_2 (5, 0, 5);
373 erode_1_2 (5, 1, 1);
374 break;
375
376 // wide open, some room-like structures
377 case 3:
378 fill_rand (45);
379 erode_1_2 (5, 2, 4);
380 erode_1_2 (5, -1, 3);
381 break;
382 }
383
384 border ();
385 isolation_remover ();
386}
387
388/////////////////////////////////////////////////////////////////////////////
389
390//+GPL 371//+GPL
391 372
392/* puts doors at appropriate locations in a maze. */ 373/* puts doors at appropriate locations in a maze. */
393void 374void
394layout::doorify () 375layout::doorify ()
395{ 376{
396 int ndoors = w * h / 60; /* reasonable number of doors. */ 377 int ndoors = w * h / 60; /* reasonable number of doors. */
397 int doorlocs = 0; /* # of available doorlocations */
398 378
399 uint16 *doorlist_x = salloc<uint16> (w * h); 379 coroapi::cede_to_tick ();
400 uint16 *doorlist_y = salloc<uint16> (w * h); 380
381 fixed_stack<point> doorloc (w * h);
401 382
402 /* make a list of possible door locations */ 383 /* make a list of possible door locations */
403 for (int i = 1; i < w - 1; i++) 384 for (int i = 1; i < w - 1; i++)
404 for (int j = 1; j < h - 1; j++) 385 for (int j = 1; j < h - 1; j++)
405 { 386 {
406 int sindex = surround_flag (*this, i, j); 387 int sindex = surround_flag (*this, i, j);
407 388
408 if (sindex == 3 || sindex == 12) /* these are possible door sindex */ 389 if (sindex == 3 || sindex == 12) /* these are possible door sindex */
409 { 390 doorloc.push (point (i, j));
410 doorlist_x [doorlocs] = i;
411 doorlist_y [doorlocs] = j;
412 doorlocs++;
413 }
414 } 391 }
415 392
416 while (ndoors > 0 && doorlocs > 0) 393 while (ndoors && doorloc.size)
417 { 394 {
418 int di = rmg_rndm (doorlocs); 395 point p = doorloc.remove (rmg_rndm (doorloc.size));
419 int i = doorlist_x [di]; 396
420 int j = doorlist_y [di];
421 int sindex = surround_flag (*this, i, j); 397 int sindex = surround_flag (*this, p.x, p.y);
422 398
423 if (sindex == 3 || sindex == 12) /* these are possible door sindex */ 399 if (sindex == 3 || sindex == 12) /* these are possible door sindex */
424 { 400 {
425 data [i][j] = 'D'; 401 data [p.x][p.y] = 'D';
426 ndoors--; 402 --ndoors;
427 } 403 }
428
429 /* reduce the size of the list */
430 doorlocs--;
431 doorlist_x[di] = doorlist_x [doorlocs];
432 doorlist_y[di] = doorlist_y [doorlocs];
433 } 404 }
434
435 sfree (doorlist_x, w * h);
436 sfree (doorlist_y, w * h);
437} 405}
438 406
439/* takes a map and makes it symmetric: adjusts Xsize and 407/* takes a map and makes it symmetric: adjusts Xsize and
440 * Ysize to produce a symmetric map. 408 * Ysize to produce a symmetric map.
441 */ 409 */
499//-GPL 467//-GPL
500 468
501void 469void
502layout::rotate (int rotation) 470layout::rotate (int rotation)
503{ 471{
472 coroapi::cede_to_tick ();
473
504 switch (rotation & 3) 474 switch (rotation & 3)
505 { 475 {
506 case 2: /* a reflection */ 476 case 2: /* a reflection */
507 { 477 {
508 layout new_layout (w, h); 478 layout new_layout (w, h);
660{ 630{
661 layout new_layout (w * 2 - 1, h * 2 - 1); 631 layout new_layout (w * 2 - 1, h * 2 - 1);
662 632
663 new_layout.clear (); 633 new_layout.clear ();
664 634
635 coroapi::cede_to_tick ();
636
665 for (int i = 0; i < w; i++) 637 for (int i = 0; i < w; i++)
666 for (int j = 0; j < h; j++) 638 for (int j = 0; j < h; j++)
667 switch (data [i][j]) 639 switch (data [i][j])
668 { 640 {
669 case '#': expand_wall (new_layout, i, j, *this); break; 641 case '#': expand_wall (new_layout, i, j, *this); break;
751 723
752 return -1; 724 return -1;
753} 725}
754 726
755int 727int
756make_wall (char **maze, int x, int y, int dir) 728make_wall (layout &maze, int x, int y, int dir)
757{ 729{
758 maze[x][y] = 'D'; /* mark a door */ 730 maze[x][y] = 'D'; /* mark a door */
759 731
760 switch (dir) 732 switch (dir)
761 { 733 {
778 750
779void 751void
780layout::roomify () 752layout::roomify ()
781{ 753{
782 int tries = w * h / 30; 754 int tries = w * h / 30;
755
756 coroapi::cede_to_tick ();
783 757
784 for (int ti = 0; ti < tries; ti++) 758 for (int ti = 0; ti < tries; ti++)
785 { 759 {
786 /* starting location for looking at creating a door */ 760 /* starting location for looking at creating a door */
787 int dx = rmg_rndm (w); 761 int dx = rmg_rndm (w);
810 else 784 else
811 make_wall (*this, dx, dy, 1); 785 make_wall (*this, dx, dy, 1);
812 } 786 }
813} 787}
814 788
789//-GPL
790
815///////////////////////////////////////////////////////////////////////////// 791/////////////////////////////////////////////////////////////////////////////
792
793// inspired mostly by http://www.jimrandomh.org/misc/caves.txt
794void
795layout::gen_cave (int subtype)
796{
797 switch (subtype)
798 {
799 // a rough cave
800 case 0:
801 fill_rand (rmg_rndm (85, 97));
802 break;
803
804 // corridors
805 case 1:
806 fill_rand (rmg_rndm (5, 40));
807 erode_1_2 (5, 2, 10);
808 erode_1_2 (5, -1, 10);
809 erode_1_2 (5, 2, 1);
810 break;
811
812 // somewhat open, some room-like structures
813 case 2:
814 fill_rand (45);
815 erode_1_2 (5, 2, 4);
816 erode_1_2 (5, -1, 3);
817 break;
818
819 // wide open, roundish
820 case 3:
821 fill_rand (45);
822 erode_1_2 (5, 0, 5);
823 erode_1_2 (5, 1, 1);
824 break;
825 }
826
827 border ();
828 isolation_remover (1);
829}
830
831void
832layout::gen_castle ()
833{
834 fill ('#');
835
836 for (int n = w * h / 30 + 1; n--; )
837 {
838 int rw = rmg_rndm (6, 10);
839 int rh = rmg_rndm (6, 10);
840
841 if (rw > w || rh > h)
842 continue;
843
844 int rx = rmg_rndm (0, w - rw);
845 int ry = rmg_rndm (0, h - rh);
846
847 rect (rx, ry, rx + rw, ry + rh, '#');
848 fill_rect (rx + 1, ry + 1, rx + rw - 1, ry + rh - 1, 0);
849 }
850
851 border ();
852 isolation_remover (0);
853}
854
855static void
856gen_mixed_ (layout &maze, random_map_params *RP)
857{
858 if (maze.w > maze.h && maze.w > 16)
859 {
860 int m = rmg_rndm (8, maze.w - 8);
861
862 layout m1 (maze, 0, 0, m , maze.h); gen_mixed_ (m1, RP);
863 layout m2 (maze, m, 0, maze.w, maze.h); gen_mixed_ (m2, RP);
864 }
865 else if (maze.h > 16)
866 {
867 int m = rmg_rndm (8, maze.h - 8);
868
869 layout m1 (maze, 0, 0, maze.w, m ); gen_mixed_ (m1, RP);
870 layout m2 (maze, 0, m, maze.w, maze.h); gen_mixed_ (m2, RP);
871 }
872 else
873 {
874 RP->map_layout_style = rmg_rndm (NROFLAYOUTS - 2) + 1;
875
876 if (RP->map_layout_style == LAYOUT_MULTIPLE)
877 ++RP->map_layout_style;
878
879 maze.generate (RP);
880 }
881
882 coroapi::cede_to_tick ();
883}
884
885// recursive subdivision with random sublayouts
886static void
887gen_mixed (layout &maze, random_map_params *RP)
888{
889 random_map_params &rp = *new random_map_params (RP);
890 gen_mixed_ (maze, &rp);
891 delete &rp;
892
893 maze.border ();
894
895 // exits currently do not work so well, as they
896 // are currently often found together, so nuke entrances
897 maze.replace ('<', ' ');
898
899 maze.isolation_remover (0);
900}
901
902//+GPL
816 903
817/* function selects the maze function and gives it whatever 904/* function selects the maze function and gives it whatever
818 arguments it needs. */ 905 arguments it needs. */
819void 906void
820layout::generate (random_map_params *RP) 907layout::generate (random_map_params *RP)
880 if (rmg_rndm (2)) 967 if (rmg_rndm (2))
881 doorify (); 968 doorify ();
882 969
883 break; 970 break;
884 971
972 case LAYOUT_CASTLE:
973 gen_castle ();
974
975 if (rmg_rndm (2))
976 doorify ();
977
978 break;
979
980 case LAYOUT_MULTIPLE:
981 gen_mixed (*this, RP);
982 break;
983
885 default: 984 default:
886 abort (); 985 abort ();
887 } 986 }
987}
888 988
889 /* rotate the maze randomly */ 989//-GPL
890 rotate (rmg_rndm (4));
891
892 symmetrize (RP->symmetry_used);
893 990
894#if 0 991#if 0
895 print ();//D 992static void
896#endif 993gen_village (layout &maze)
994{
995 maze.clear ();
996 maze.border ();
897 997
898 if (RP->expand2x) 998 for (int n = maze.w * maze.h / 200 + 1; n--; )
899 expand2x (); 999 {
900} 1000 int rw = rmg_rndm (6, 10);
1001 int rh = rmg_rndm (6, 10);
901 1002
902//-GPL 1003 int rx = rmg_rndm (2, maze.w - rw - 2);
1004 int ry = rmg_rndm (2, maze.h - rh - 2);
903 1005
904#if 0 1006 maze.rect (rx, ry, rx + rw, ry + rh, '#');
1007 }
1008
1009 maze.border ();
1010 maze.isolation_remover (2);
1011}
1012
905static struct demo 1013static struct demo
906{ 1014{
907 demo () 1015 demo ()
908 { 1016 {
909 rmg_rndm.seed (time (0)); 1017 rmg_rndm.seed (time (0));
1018 extern void hack();hack ();
910 1019
911 for(int i=1;i<100;i++) 1020 for(int i=1;i<100;i++)
912 { 1021 {
913 layout maze (40, 25); 1022 layout maze (40, 30);
914 maze.fill_rand (85); 1023 maze.fill_rand (99);
915 maze.border (); 1024 maze.border ();
916 maze.isolation_remover (); 1025 maze.isolation_remover (2);
917 maze.print (); 1026 maze.print ();
918 } 1027 }
919 1028
920 exit (1); 1029 exit (1);
921 } 1030 }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines