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.10 by root, Sat Jul 3 00:39:57 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{
203 ++y; 215 ++y;
204 } 216 }
205 217
206 while (--y >= y0) 218 while (--y >= y0)
207 { 219 {
208 if (x > 0) push_flood_fill (dist, seeds, x - 1, y); 220 if (x > 0 && !dist [x - 1][y]) push_flood_fill (dist, seeds, x - 1, y);
209 if (x < dist.w - 1) 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)
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} 347}
337 348
338///////////////////////////////////////////////////////////////////////////// 349/////////////////////////////////////////////////////////////////////////////
339 350
801 make_wall (*this, dx, dy, 1); 812 make_wall (*this, dx, dy, 1);
802 } 813 }
803} 814}
804 815
805///////////////////////////////////////////////////////////////////////////// 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}
806 859
807/* function selects the maze function and gives it whatever 860/* function selects the maze function and gives it whatever
808 arguments it needs. */ 861 arguments it needs. */
809void 862void
810layout::generate (random_map_params *RP) 863layout::generate (random_map_params *RP)
870 if (rmg_rndm (2)) 923 if (rmg_rndm (2))
871 doorify (); 924 doorify ();
872 925
873 break; 926 break;
874 927
928 case LAYOUT_MULTIPLE:
929 gen_mixed (*this, RP);
930 break;
931
875 default: 932 default:
876 abort (); 933 abort ();
877 } 934 }
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} 935}
891 936
892//-GPL 937//-GPL
893 938
894#if 0 939#if 0

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines