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.11 by root, Sat Jul 3 01:12:44 2010 UTC vs.
Revision 1.16 by root, Sat Jul 3 11:52:11 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{
216 for (;;) 228 for (;;)
217 { 229 {
218 point neigh[4]; 230 point neigh[4];
219 int ncnt = 0; 231 int ncnt = 0;
220 232
221 if (x > 1 && U8 (dist [x - 1][y]) <= d && dist [x - 1][y] > 1) neigh [ncnt++] = point (x - 1, y); 233 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); 234 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); 235 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); 236 if (y < dist.h - 1 && U8 (dist [x][y + 1]) <= d && dist [x][y + 1] > 1) neigh [ncnt++] = point (x, y + 1);
225 237
226 if (!ncnt) 238 if (!ncnt)
227 return; 239 return;
228 240
229 point &p = neigh [rmg_rndm (ncnt)]; 241 point &p = neigh [rmg_rndm (ncnt)];
272 284
273 if (!cnt) 285 if (!cnt)
274 { 286 {
275 // map is completely massive, this is not good, 287 // map is completely massive, this is not good,
276 // so make it empty instead. 288 // so make it empty instead.
277 dist.clear (); 289 dist.fill (1);
278 dist.border (255);
279 return; 290 return;
280 } 291 }
281 292
282 fixed_stack<point> seeds (dist.w * dist.h * 5); 293 fixed_stack<point> seeds (dist.w * dist.h * 5);
283 294
318} 329}
319 330
320void 331void
321layout::isolation_remover () 332layout::isolation_remover ()
322{ 333{
323 layout dist (w, h); 334 layout dist (w - 2, h - 2); // map without border
324 335
325 for (int x = 0; x < w; ++x) 336 for (int x = 1; x < w - 1; ++x)
326 for (int y = 0; y < h; ++y) 337 for (int y = 1; y < h - 1; ++y)
327 dist [x][y] = data [x][y] == '#' ? U8 (255) : 0; 338 dist [x - 1][y - 1] = data [x][y] == '#' ? U8 (255) : 0;
328 339
329 ::isolation_remover (dist); 340 ::isolation_remover (dist);
330 341
331 // now copy the tunnels over 342 // now copy the tunnels over
332 for (int x = 0; x < w; ++x) 343 for (int x = 1; x < w - 1; ++x)
333 for (int y = 0; y < h; ++y) 344 for (int y = 1; y < h - 1; ++y)
334 if (data [x][y] == '#' && dist [x][y] == 1) 345 if (data [x][y] == '#' && dist [x - 1][y - 1] == 1)
335 data [x][y] = 0; 346 data [x][y] = 0;
336}
337
338/////////////////////////////////////////////////////////////////////////////
339
340// inspired mostly by http://www.jimrandomh.org/misc/caves.txt
341void
342layout::gen_cave (int subtype)
343{
344 switch (subtype)
345 {
346 // a rough cave
347 case 0:
348 fill_rand (rmg_rndm (85, 97));
349 break;
350
351 // corridors
352 case 1:
353 fill_rand (rmg_rndm (5, 40));
354 erode_1_2 (5, 2, 10);
355 erode_1_2 (5, -1, 10);
356 erode_1_2 (5, 2, 1);
357 break;
358
359 // somewhat open, roundish
360 case 2:
361 fill_rand (45);
362 erode_1_2 (5, 0, 5);
363 erode_1_2 (5, 1, 1);
364 break;
365
366 // wide open, some room-like structures
367 case 3:
368 fill_rand (45);
369 erode_1_2 (5, 2, 4);
370 erode_1_2 (5, -1, 3);
371 break;
372 }
373
374 border ();
375 isolation_remover ();
376} 347}
377 348
378///////////////////////////////////////////////////////////////////////////// 349/////////////////////////////////////////////////////////////////////////////
379 350
380//+GPL 351//+GPL
801 make_wall (*this, dx, dy, 1); 772 make_wall (*this, dx, dy, 1);
802 } 773 }
803} 774}
804 775
805///////////////////////////////////////////////////////////////////////////// 776/////////////////////////////////////////////////////////////////////////////
777
778// inspired mostly by http://www.jimrandomh.org/misc/caves.txt
779void
780layout::gen_cave (int subtype)
781{
782 switch (subtype)
783 {
784 // a rough cave
785 case 0:
786 fill_rand (rmg_rndm (85, 97));
787 break;
788
789 // corridors
790 case 1:
791 fill_rand (rmg_rndm (5, 40));
792 erode_1_2 (5, 2, 10);
793 erode_1_2 (5, -1, 10);
794 erode_1_2 (5, 2, 1);
795 break;
796
797 // somewhat open, roundish
798 case 2:
799 fill_rand (45);
800 erode_1_2 (5, 0, 5);
801 erode_1_2 (5, 1, 1);
802 break;
803
804 // wide open, some room-like structures
805 case 3:
806 fill_rand (45);
807 erode_1_2 (5, 2, 4);
808 erode_1_2 (5, -1, 3);
809 break;
810 }
811
812 border ();
813 isolation_remover ();
814}
815
816static void
817gen_mixed_ (layout &maze, random_map_params *RP, int dir)
818{
819 if (maze.w < 20 && maze.h < 20 && !rmg_rndm (3))
820 dir = 2; // stop recursion randomly
821
822 if (dir == 0 && maze.w > 16)
823 {
824 int m = rmg_rndm (8, maze.w - 8);
825
826 layout m1 (maze, 0, 0, m , maze.h); gen_mixed_ (m1, RP, !dir);
827 layout m2 (maze, m, 0, maze.w, maze.h); gen_mixed_ (m2, RP, !dir);
828 }
829 else if (dir == 1 && maze.h > 16)
830 {
831 int m = rmg_rndm (8, maze.h - 8);
832
833 layout m1 (maze, 0, 0, maze.w, m ); gen_mixed_ (m1, RP, !dir);
834 layout m2 (maze, 0, m, maze.w, maze.h); gen_mixed_ (m2, RP, !dir);
835 }
836 else
837 {
838 RP->map_layout_style = rmg_rndm (NROFLAYOUTS - 2) + 1;
839
840 if (RP->map_layout_style == LAYOUT_MULTIPLE)
841 ++RP->map_layout_style;
842
843 maze.generate (RP);
844 }
845}
846
847// recursive subdivision with random sublayouts
848static void
849gen_mixed (layout &maze, random_map_params *RP)
850{
851 random_map_params &rp = *new random_map_params (RP);
852 gen_mixed_ (maze, &rp, rmg_rndm (2));
853 delete &rp;
854
855 maze.border ();
856 maze.isolation_remover ();
857}
806 858
807/* function selects the maze function and gives it whatever 859/* function selects the maze function and gives it whatever
808 arguments it needs. */ 860 arguments it needs. */
809void 861void
810layout::generate (random_map_params *RP) 862layout::generate (random_map_params *RP)
870 if (rmg_rndm (2)) 922 if (rmg_rndm (2))
871 doorify (); 923 doorify ();
872 924
873 break; 925 break;
874 926
927 case LAYOUT_MULTIPLE:
928 gen_mixed (*this, RP);
929 break;
930
875 default: 931 default:
876 abort (); 932 abort ();
877 } 933 }
878
879 /* rotate the maze randomly */
880 rotate (rmg_rndm (4));
881
882 symmetrize (RP->symmetry_used);
883
884#if 0
885 print ();//D
886#endif
887
888 if (RP->expand2x)
889 expand2x ();
890} 934}
891 935
892//-GPL 936//-GPL
893 937
894#if 0 938#if 0

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines