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.15 by root, Sat Jul 3 03:09:27 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
226 printf ("tunnel %d+%d ncnt %d\n", x, y, ncnt);//D
227 237
228 if (!ncnt) 238 if (!ncnt)
229 return; 239 return;
230 240
231 point &p = neigh [rmg_rndm (ncnt)]; 241 point &p = neigh [rmg_rndm (ncnt)];
275 if (!cnt) 285 if (!cnt)
276 { 286 {
277 // map is completely massive, this is not good, 287 // map is completely massive, this is not good,
278 // so make it empty instead. 288 // so make it empty instead.
279 dist.fill (1); 289 dist.fill (1);
280 dist.border (255);
281 return; 290 return;
282 } 291 }
283 292
284 fixed_stack<point> seeds (dist.w * dist.h * 5); 293 fixed_stack<point> seeds (dist.w * dist.h * 5);
285 294
309 else 318 else
310 { 319 {
311 // nothing here, continue to expand 320 // nothing here, continue to expand
312 U8 d = U8 (dist [x][y]) + 1; 321 U8 d = U8 (dist [x][y]) + 1;
313 322
314 if (x < dist.w - 2) maybe_push (dist, seeds, x + 1, y, d); 323 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); 324 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); 325 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); 326 if (y > 0) maybe_push (dist, seeds, x, y - 1, d);
318 } 327 }
319 } 328 }
320} 329}
321 330
322void 331void
323layout::isolation_remover () 332layout::isolation_remover ()
324{ 333{
325 layout dist (w, h); 334 layout dist (w - 2, h - 2); // map without border
326 335
327 for (int x = 1; x < w - 1; ++x) 336 for (int x = 1; x < w - 1; ++x)
328 for (int y = 1; y < h - 1; ++y) 337 for (int y = 1; y < h - 1; ++y)
329 dist [x][y] = data [x][y] == '#' ? U8 (255) : 0; 338 dist [x - 1][y - 1] = data [x][y] == '#' ? U8 (255) : 0;
330 339
331 ::isolation_remover (dist); 340 ::isolation_remover (dist);
332 341
333 // now copy the tunnels over 342 // now copy the tunnels over
334 for (int x = 1; x < w - 1; ++x) 343 for (int x = 1; x < w - 1; ++x)
335 for (int y = 1; y < h - 1; ++y) 344 for (int y = 1; y < h - 1; ++y)
336 if (data [x][y] == '#' && dist [x][y] == 1) 345 if (data [x][y] == '#' && dist [x - 1][y - 1] == 1)
337 data [x][y] = 0; 346 data [x][y] = 0;
338} 347}
339 348
340///////////////////////////////////////////////////////////////////////////// 349/////////////////////////////////////////////////////////////////////////////
341 350
803 make_wall (*this, dx, dy, 1); 812 make_wall (*this, dx, dy, 1);
804 } 813 }
805} 814}
806 815
807///////////////////////////////////////////////////////////////////////////// 816/////////////////////////////////////////////////////////////////////////////
817
818static void
819gen_mixed_ (layout &maze, random_map_params *RP, int dir)
820{
821 if (maze.w < 20 && maze.h < 20 && !rmg_rndm (3))
822 dir = 2; // stop recursion randomly
823
824 if (dir == 0 && maze.w > 16)
825 {
826 int m = rmg_rndm (8, maze.w - 8);
827
828 layout m1 (maze, 0, 0, m , maze.h); gen_mixed_ (m1, RP, !dir);
829 layout m2 (maze, m, 0, maze.w, maze.h); gen_mixed_ (m2, RP, !dir);
830 }
831 else if (dir == 1 && maze.h > 16)
832 {
833 int m = rmg_rndm (8, maze.h - 8);
834
835 layout m1 (maze, 0, 0, maze.w, m ); gen_mixed_ (m1, RP, !dir);
836 layout m2 (maze, 0, m, maze.w, maze.h); gen_mixed_ (m2, RP, !dir);
837 }
838 else
839 {
840 RP->map_layout_style = rmg_rndm (NROFLAYOUTS - 2) + 1;
841
842 if (RP->map_layout_style == LAYOUT_MULTIPLE)
843 ++RP->map_layout_style;
844
845 maze.generate (RP);
846 }
847}
848
849static void
850gen_mixed (layout &maze, random_map_params *RP)
851{
852 random_map_params &rp = *new random_map_params (RP);
853 gen_mixed_ (maze, &rp, rmg_rndm (2));
854 delete &rp;
855
856 maze.border ();
857 maze.isolation_remover ();
858}
808 859
809/* function selects the maze function and gives it whatever 860/* function selects the maze function and gives it whatever
810 arguments it needs. */ 861 arguments it needs. */
811void 862void
812layout::generate (random_map_params *RP) 863layout::generate (random_map_params *RP)
872 if (rmg_rndm (2)) 923 if (rmg_rndm (2))
873 doorify (); 924 doorify ();
874 925
875 break; 926 break;
876 927
928 case LAYOUT_MULTIPLE:
929 gen_mixed (*this, RP);
930 break;
931
877 default: 932 default:
878 abort (); 933 abort ();
879 } 934 }
880
881 /* rotate the maze randomly */
882 rotate (rmg_rndm (4));
883
884 symmetrize (RP->symmetry_used);
885
886#if 0
887 print ();//D
888#endif
889
890 if (RP->expand2x)
891 expand2x ();
892} 935}
893 936
894//-GPL 937//-GPL
895 938
896#if 0 939#if 0

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines