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.17 by root, Sat Jul 3 13:14:35 2010 UTC vs.
Revision 1.34 by root, Sat Nov 17 23:40:02 2018 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 (©) 2017,2018 Marc Alexander Lehmann / the Deliantra team
5 * Copyright (©) 2010,2011,2012,2013,2014,2015,2016 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
5 * Copyright (©) Crossfire Development Team (restored, original file without copyright notice) 6 * Copyright (©) 1994-2004 Crossfire Development Team (restored, original file without copyright notice)
6 * 7 *
7 * Deliantra is free software: you can redistribute it and/or modify it under 8 * 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 9 * 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 10 * Free Software Foundation, either version 3 of the License, or (at your
10 * option) any later version. 11 * option) any later version.
11 * 12 *
12 * This program is distributed in the hope that it will be useful, 13 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details. 16 * GNU General Public License for more details.
16 * 17 *
17 * You should have received a copy of the Affero GNU General Public License 18 * You should have received a copy of the Affero GNU General Public License
18 * and the GNU General Public License along with this program. If not, see 19 * and the GNU General Public License along with this program. If not, see
19 * <http://www.gnu.org/licenses/>. 20 * <http://www.gnu.org/licenses/>.
20 * 21 *
21 * The authors can be reached via e-mail to <support@deliantra.net> 22 * The authors can be reached via e-mail to <support@deliantra.net>
22 */ 23 */
23 24
24#include <global.h> 25#include <global.h>
25#include <random_map.h> 26#include <rmg.h>
26#include <rproto.h> 27#include <rproto.h>
27 28
28void 29ecb_noinline void
29layout::alloc (int w, int h) 30layout::alloc (int w, int h)
30{ 31{
31 assert (sizeof (cell) == 1); 32 assert (sizeof (cell) == 1);
32 33
33 this->w = w; 34 this->w = w;
74layout::~layout () 75layout::~layout ()
75{ 76{
76 sfree ((char *)data, size); 77 sfree ((char *)data, size);
77} 78}
78 79
79void 80ecb_noinline void
80layout::fill (char fill) 81layout::fill (char fill)
81{ 82{
82 //memset (data [0], fill, w * h); // only when contiguous :/ 83 //memset (data [0], fill, w * h); // only when contiguous :/
83 fill_rect (0, 0, w, h, fill); 84 fill_rect (0, 0, w, h, fill);
84} 85}
85 86
86void 87ecb_noinline void
88layout::replace (char from, char to)
89{
90 for (int x = 0; x < w; ++x)
91 for (int y = 0; y < h; ++y)
92 if (data [x][y] == from)
93 data [x][y] = to;
94}
95
96ecb_noinline void
87layout::rect (int x1, int y1, int x2, int y2, char fill) 97layout::rect (int x1, int y1, int x2, int y2, char fill)
88{ 98{
89 --x2; 99 --x2;
90 100
91 memset (data [x1] + y1, fill, y2 - y1); 101 memset (data [x1] + y1, fill, y2 - y1);
93 103
94 while (++x1 < x2) 104 while (++x1 < x2)
95 data [x1][y1] = data [x1][y2 - 1] = fill; 105 data [x1][y1] = data [x1][y2 - 1] = fill;
96} 106}
97 107
98void 108ecb_noinline void
99layout::fill_rect (int x1, int y1, int x2, int y2, char fill) 109layout::fill_rect (int x1, int y1, int x2, int y2, char fill)
100{ 110{
101 for (; x1 < x2; ++x1) 111 for (; x1 < x2; ++x1)
102 memset (data [x1] + y1, fill, y2 - y1); 112 memset (data [x1] + y1, fill, y2 - y1);
103} 113}
104 114
115void
105void layout::border (char fill) 116layout::border (char fill)
106{ 117{
107 rect (0, 0, w, h, fill); 118 rect (0, 0, w, h, fill);
108} 119}
109 120
110void 121ecb_noinline void
111layout::fill_rand (int percent) 122layout::fill_rand (int percent)
112{ 123{
113 percent = lerp (percent, 0, 100, 0, 256); 124 percent = lerp (percent, 0, 100, 0, 256);
114 125
115 for (int x = w - 1; --x > 0; ) 126 for (int x = 0; x < w; ++x)
116 for (int y = h - 1; --y > 0; ) 127 for (int y = 0; y < h; ++y)
117 data [x][y] = rmg_rndm (256) > percent ? 0 : '#'; 128 data [x][y] = rmg_rndm (256) > percent ? 0 : '#';
118} 129}
119 130
120///////////////////////////////////////////////////////////////////////////// 131/////////////////////////////////////////////////////////////////////////////
121 132
122// erode by cellular automata 133// erode by cellular automata
123void 134ecb_noinline void
124layout::erode_1_2 (int c1, int c2, int repeat) 135layout::erode_1_2 (int c1, int c2, int repeat)
125{ 136{
126 layout neu (w, h); 137 layout neu (w, h);
127 138
128 while (repeat--) 139 while (repeat--)
194///////////////////////////////////////////////////////////////////////////// 205/////////////////////////////////////////////////////////////////////////////
195// isolation remover - ensures single connected area 206// isolation remover - ensures single connected area
196 207
197typedef fixed_stack<point> pointlist; 208typedef fixed_stack<point> pointlist;
198 209
199static void noinline 210ecb_noinline static void
200push_flood_fill (layout &dist, pointlist &seeds, int x, int y) 211push_flood_fill (layout &dist, pointlist &seeds, int x, int y)
201{ 212{
202 if (dist [x][y]) 213 if (dist [x][y])
203 return; 214 return;
204 215
220 if (x > 0 && !dist [x - 1][y]) push_flood_fill (dist, seeds, x - 1, y); 231 if (x > 0 && !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); 232 if (x < dist.w - 1 && !dist [x + 1][y]) push_flood_fill (dist, seeds, x + 1, y);
222 } 233 }
223} 234}
224 235
225static inline void 236static void inline
226make_tunnel (layout &dist, pointlist &seeds, int x, int y, U8 d, int perturb) 237make_tunnel (layout &dist, pointlist &seeds, int x, int y, U8 d, int perturb)
227{ 238{
228 for (;;) 239 for (;;)
229 { 240 {
230 point neigh[4]; 241 point neigh[4];
238 if (y < dist.h - 1 && U8 (dist [x][y + 1]) < d && dist [x][y + 1] > 1) neigh [ncnt++] = point (x, y + 1); 249 if (y < dist.h - 1 && U8 (dist [x][y + 1]) < d && dist [x][y + 1] > 1) neigh [ncnt++] = point (x, y + 1);
239 250
240 if (!ncnt) 251 if (!ncnt)
241 return; 252 return;
242 253
243 point &p = neigh [perturb ? rmg_rndm (ncnt) : 0]; 254 point p = neigh [perturb ? rmg_rndm (ncnt) : 0];
244 255
245 seeds.push (p); 256 seeds.push (p);
246 257
247 x = p.x; 258 x = p.x;
248 y = p.y; 259 y = p.y;
265 seeds.push (point (x, y)); 276 seeds.push (point (x, y));
266} 277}
267 278
268// isolation remover, works on a "distance" map 279// isolation remover, works on a "distance" map
269// the map must be initialised with 0 == rooms, 255 = walls 280// the map must be initialised with 0 == rooms, 255 = walls
270static void noinline 281ecb_noinline static void
271isolation_remover (layout &dist, unsigned int perturb = 2) 282isolation_remover (layout &dist, unsigned int perturb = 2)
272{ 283{
273 // dist contains 284 // dist contains
274 // 0 == invisited rooms 285 // 0 == invisited rooms
275 // 1 == visited rooms 286 // 1 == visited rooms
276 // 2+ shortest distance to random near room 287 // 2+ shortest distance to random near room
277 288
278 max_it (perturb, 0); 289 clamp_it (perturb, 0, 2);
279 min_it (perturb, 2);
280 290
281 // phase 1, find seed 291 // phase 1, find seed
282 int cnt = 0; 292 int cnt = 0;
283 int x, y; 293 int x, y;
284 294
303 seeds.push (point (x, y)); 313 seeds.push (point (x, y));
304 314
305 // phase 2, while we have seeds, if 315 // phase 2, while we have seeds, if
306 // seed is empty, floodfill, else grow 316 // seed is empty, floodfill, else grow
307 317
318 int rem_index = 0; // used to remove "somewhat ordered"
319
308 while (seeds.size) 320 while (seeds.size)
309 { 321 {
310 coroapi::cede_to_tick (); 322 coroapi::cede_to_tick ();
311 323
324 int i = perturb
325 ? rmg_rndm (max (0, seeds.size - 8), seeds.size - 1)
326 : rem_index ++ % seeds.size;
327
312 point p = seeds.remove (rmg_rndm (seeds.size)); 328 point p = seeds.remove (i);
313 329
314 x = p.x; 330 x = p.x;
315 y = p.y; 331 y = p.y;
316 332
317 if (!dist [x][y]) 333 if (!dist [x][y])
358/* puts doors at appropriate locations in a maze. */ 374/* puts doors at appropriate locations in a maze. */
359void 375void
360layout::doorify () 376layout::doorify ()
361{ 377{
362 int ndoors = w * h / 60; /* reasonable number of doors. */ 378 int ndoors = w * h / 60; /* reasonable number of doors. */
363 int doorlocs = 0; /* # of available doorlocations */
364 379
365 uint16 *doorlist_x = salloc<uint16> (w * h); 380 coroapi::cede_to_tick ();
366 uint16 *doorlist_y = salloc<uint16> (w * h); 381
382 fixed_stack<point> doorloc (w * h);
367 383
368 /* make a list of possible door locations */ 384 /* make a list of possible door locations */
369 for (int i = 1; i < w - 1; i++) 385 for (int i = 1; i < w - 1; i++)
370 for (int j = 1; j < h - 1; j++) 386 for (int j = 1; j < h - 1; j++)
371 { 387 {
372 int sindex = surround_flag (*this, i, j); 388 int sindex = surround_flag (*this, i, j);
373 389
374 if (sindex == 3 || sindex == 12) /* these are possible door sindex */ 390 if (sindex == 3 || sindex == 12) /* these are possible door sindex */
375 { 391 doorloc.push (point (i, j));
376 doorlist_x [doorlocs] = i;
377 doorlist_y [doorlocs] = j;
378 doorlocs++;
379 }
380 } 392 }
381 393
382 while (ndoors > 0 && doorlocs > 0) 394 while (ndoors && doorloc.size)
383 { 395 {
384 int di = rmg_rndm (doorlocs); 396 point p = doorloc.remove (rmg_rndm (doorloc.size));
385 int i = doorlist_x [di]; 397
386 int j = doorlist_y [di];
387 int sindex = surround_flag (*this, i, j); 398 int sindex = surround_flag (*this, p.x, p.y);
388 399
389 if (sindex == 3 || sindex == 12) /* these are possible door sindex */ 400 if (sindex == 3 || sindex == 12) /* these are possible door sindex */
390 { 401 {
391 data [i][j] = 'D'; 402 data [p.x][p.y] = 'D';
392 ndoors--; 403 --ndoors;
393 } 404 }
394
395 /* reduce the size of the list */
396 doorlocs--;
397 doorlist_x[di] = doorlist_x [doorlocs];
398 doorlist_y[di] = doorlist_y [doorlocs];
399 } 405 }
400
401 sfree (doorlist_x, w * h);
402 sfree (doorlist_y, w * h);
403} 406}
404 407
405/* takes a map and makes it symmetric: adjusts Xsize and 408/* takes a map and makes it symmetric: adjusts Xsize and
406 * Ysize to produce a symmetric map. 409 * Ysize to produce a symmetric map.
407 */ 410 */
465//-GPL 468//-GPL
466 469
467void 470void
468layout::rotate (int rotation) 471layout::rotate (int rotation)
469{ 472{
473 coroapi::cede_to_tick ();
474
470 switch (rotation & 3) 475 switch (rotation & 3)
471 { 476 {
472 case 2: /* a reflection */ 477 case 2: /* a reflection */
473 { 478 {
474 layout new_layout (w, h); 479 layout new_layout (w, h);
529 * 1 match on (i+1, j) 534 * 1 match on (i+1, j)
530 * 2 match on (i, j+1) 535 * 2 match on (i, j+1)
531 * 4 match on (i+1, j+1) 536 * 4 match on (i+1, j+1)
532 * and the possible combinations thereof. 537 * and the possible combinations thereof.
533 */ 538 */
534static int noinline 539ecb_noinline static int
535calc_pattern (char ch, layout &maze, int i, int j) 540calc_pattern (char ch, layout &maze, int i, int j)
536{ 541{
537 int pattern = 0; 542 int pattern = 0;
538 543
539 if (i + 1 < maze.w && maze[i + 1][j] == ch) 544 if (i + 1 < maze.w && maze[i + 1][j] == ch)
626{ 631{
627 layout new_layout (w * 2 - 1, h * 2 - 1); 632 layout new_layout (w * 2 - 1, h * 2 - 1);
628 633
629 new_layout.clear (); 634 new_layout.clear ();
630 635
636 coroapi::cede_to_tick ();
637
631 for (int i = 0; i < w; i++) 638 for (int i = 0; i < w; i++)
632 for (int j = 0; j < h; j++) 639 for (int j = 0; j < h; j++)
633 switch (data [i][j]) 640 switch (data [i][j])
634 { 641 {
635 case '#': expand_wall (new_layout, i, j, *this); break; 642 case '#': expand_wall (new_layout, i, j, *this); break;
636 case 'D': expand_door (new_layout, i, j, *this); break; 643 case 'D': expand_door (new_layout, i, j, *this); break;
637 default: expand_misc (new_layout, i, j, *this); break; 644 default: expand_misc (new_layout, i, j, *this); break;
638 } 645 }
639 646
640 swap (new_layout); 647 swap (new_layout);
641} 648}
717 724
718 return -1; 725 return -1;
719} 726}
720 727
721int 728int
722make_wall (char **maze, int x, int y, int dir) 729make_wall (layout &maze, int x, int y, int dir)
723{ 730{
724 maze[x][y] = 'D'; /* mark a door */ 731 maze[x][y] = 'D'; /* mark a door */
725 732
726 switch (dir) 733 switch (dir)
727 { 734 {
744 751
745void 752void
746layout::roomify () 753layout::roomify ()
747{ 754{
748 int tries = w * h / 30; 755 int tries = w * h / 30;
756
757 coroapi::cede_to_tick ();
749 758
750 for (int ti = 0; ti < tries; ti++) 759 for (int ti = 0; ti < tries; ti++)
751 { 760 {
752 /* starting location for looking at creating a door */ 761 /* starting location for looking at creating a door */
753 int dx = rmg_rndm (w); 762 int dx = rmg_rndm (w);
775 make_wall (*this, dx, dy, 0); 784 make_wall (*this, dx, dy, 0);
776 else 785 else
777 make_wall (*this, dx, dy, 1); 786 make_wall (*this, dx, dy, 1);
778 } 787 }
779} 788}
789
790//-GPL
780 791
781///////////////////////////////////////////////////////////////////////////// 792/////////////////////////////////////////////////////////////////////////////
782 793
783// inspired mostly by http://www.jimrandomh.org/misc/caves.txt 794// inspired mostly by http://www.jimrandomh.org/misc/caves.txt
784void 795void
797 erode_1_2 (5, 2, 10); 808 erode_1_2 (5, 2, 10);
798 erode_1_2 (5, -1, 10); 809 erode_1_2 (5, -1, 10);
799 erode_1_2 (5, 2, 1); 810 erode_1_2 (5, 2, 1);
800 break; 811 break;
801 812
802 // somewhat open, roundish 813 // somewhat open, some room-like structures
803 case 2: 814 case 2:
815 fill_rand (45);
816 erode_1_2 (5, 2, 4);
817 erode_1_2 (5, -1, 3);
818 break;
819
820 // wide open, roundish
821 case 3:
804 fill_rand (45); 822 fill_rand (45);
805 erode_1_2 (5, 0, 5); 823 erode_1_2 (5, 0, 5);
806 erode_1_2 (5, 1, 1); 824 erode_1_2 (5, 1, 1);
807 break; 825 break;
808
809 // wide open, some room-like structures
810 case 3:
811 fill_rand (45);
812 erode_1_2 (5, 2, 4);
813 erode_1_2 (5, -1, 3);
814 break;
815 } 826 }
816 827
817 border (); 828 border ();
818 isolation_remover (); 829 isolation_remover (1);
819} 830}
820 831
821void 832void
822layout::gen_castle () 833layout::gen_castle ()
823{ 834{
826 for (int n = w * h / 30 + 1; n--; ) 837 for (int n = w * h / 30 + 1; n--; )
827 { 838 {
828 int rw = rmg_rndm (6, 10); 839 int rw = rmg_rndm (6, 10);
829 int rh = rmg_rndm (6, 10); 840 int rh = rmg_rndm (6, 10);
830 841
842 if (rw > w || rh > h)
843 continue;
844
831 int rx = rmg_rndm (0, w - rw); 845 int rx = rmg_rndm (0, w - rw);
832 int ry = rmg_rndm (0, h - rh); 846 int ry = rmg_rndm (0, h - rh);
833 847
834 rect (rx, ry, rx + rw, ry + rh, '#'); 848 rect (rx, ry, rx + rw, ry + rh, '#');
835 fill_rect (rx + 1, ry + 1, rx + rw - 1, ry + rh - 1, 0); 849 fill_rect (rx + 1, ry + 1, rx + rw - 1, ry + rh - 1, 0);
838 border (); 852 border ();
839 isolation_remover (0); 853 isolation_remover (0);
840} 854}
841 855
842static void 856static void
843gen_mixed_ (layout &maze, random_map_params *RP, int dir) 857gen_mixed_ (layout &maze, random_map_params *RP)
844{ 858{
845 if (maze.w < 20 && maze.h < 20 && !rmg_rndm (3)) 859 if (maze.w > maze.h && maze.w > 16)
846 dir = 2; // stop recursion randomly
847
848 if (dir == 0 && maze.w > 16)
849 { 860 {
850 int m = rmg_rndm (8, maze.w - 8); 861 int m = rmg_rndm (8, maze.w - 8);
851 862
852 layout m1 (maze, 0, 0, m , maze.h); gen_mixed_ (m1, RP, !dir); 863 layout m1 (maze, 0, 0, m , maze.h); gen_mixed_ (m1, RP);
853 layout m2 (maze, m, 0, maze.w, maze.h); gen_mixed_ (m2, RP, !dir); 864 layout m2 (maze, m, 0, maze.w, maze.h); gen_mixed_ (m2, RP);
854 } 865 }
855 else if (dir == 1 && maze.h > 16) 866 else if (maze.h > 16)
856 { 867 {
857 int m = rmg_rndm (8, maze.h - 8); 868 int m = rmg_rndm (8, maze.h - 8);
858 869
859 layout m1 (maze, 0, 0, maze.w, m ); gen_mixed_ (m1, RP, !dir); 870 layout m1 (maze, 0, 0, maze.w, m ); gen_mixed_ (m1, RP);
860 layout m2 (maze, 0, m, maze.w, maze.h); gen_mixed_ (m2, RP, !dir); 871 layout m2 (maze, 0, m, maze.w, maze.h); gen_mixed_ (m2, RP);
861 } 872 }
862 else 873 else
863 { 874 {
864 RP->map_layout_style = rmg_rndm (NROFLAYOUTS - 2) + 1; 875 RP->map_layout_style = rmg_rndm (NROFLAYOUTS - 2) + 1;
865 876
866 if (RP->map_layout_style == LAYOUT_MULTIPLE) 877 if (RP->map_layout_style == LAYOUT_MULTIPLE)
867 ++RP->map_layout_style; 878 ++RP->map_layout_style;
868 879
869 maze.generate (RP); 880 maze.generate (RP);
870 } 881 }
882
883 coroapi::cede_to_tick ();
871} 884}
872 885
873// recursive subdivision with random sublayouts 886// recursive subdivision with random sublayouts
874static void 887static void
875gen_mixed (layout &maze, random_map_params *RP) 888gen_mixed (layout &maze, random_map_params *RP)
876{ 889{
877 random_map_params &rp = *new random_map_params (RP); 890 random_map_params &rp = *new random_map_params (RP);
878 gen_mixed_ (maze, &rp, rmg_rndm (2)); 891 gen_mixed_ (maze, &rp);
879 delete &rp; 892 delete &rp;
880 893
881 maze.border (); 894 maze.border ();
895
896 // exits currently do not work so well, as they
897 // are currently often found together, so nuke entrances
898 maze.replace ('<', ' ');
899
882 maze.isolation_remover (); 900 maze.isolation_remover (0);
883} 901}
902
903//+GPL
884 904
885/* function selects the maze function and gives it whatever 905/* function selects the maze function and gives it whatever
886 arguments it needs. */ 906 arguments it needs. */
887void 907void
888layout::generate (random_map_params *RP) 908layout::generate (random_map_params *RP)
968} 988}
969 989
970//-GPL 990//-GPL
971 991
972#if 0 992#if 0
993static void
994gen_village (layout &maze)
995{
996 maze.clear ();
997 maze.border ();
998
999 for (int n = maze.w * maze.h / 200 + 1; n--; )
1000 {
1001 int rw = rmg_rndm (6, 10);
1002 int rh = rmg_rndm (6, 10);
1003
1004 int rx = rmg_rndm (2, maze.w - rw - 2);
1005 int ry = rmg_rndm (2, maze.h - rh - 2);
1006
1007 maze.rect (rx, ry, rx + rw, ry + rh, '#');
1008 }
1009
1010 maze.border ();
1011 maze.isolation_remover (2);
1012}
1013
973static struct demo 1014static struct demo
974{ 1015{
975 demo () 1016 demo ()
976 { 1017 {
977 rmg_rndm.seed (time (0)); 1018 rmg_rndm.seed (time (0));
1019 extern void hack();hack ();
978 1020
979 for(int i=1;i<100;i++) 1021 for(int i=1;i<100;i++)
980 { 1022 {
981 layout maze (40, 25); 1023 layout maze (40, 30);
982 maze.gen_castle (); 1024 maze.fill_rand (99);
983 maze.doorify (); 1025 maze.border ();
1026 maze.isolation_remover (2);
984 maze.print (); 1027 maze.print ();
985 exit(0);
986 } 1028 }
987 1029
988 exit (1); 1030 exit (1);
989 } 1031 }
990} demo; 1032} demo;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines