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.9 by root, Fri Jul 2 17:18:04 2010 UTC vs.
Revision 1.21 by root, Sun Jul 4 01:01:42 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)
88{
89 --x2;
90
91 memset (data [x1] + y1, fill, y2 - y1);
92 memset (data [x2] + y1, fill, y2 - y1);
93
94 while (++x1 < x2)
95 data [x1][y1] = data [x1][y2 - 1] = fill;
96}
97
98void
99layout::fill_rect (int x1, int y1, int x2, int y2, char fill)
76{ 100{
77 for (; x1 < x2; ++x1) 101 for (; x1 < x2; ++x1)
78 memset (data [x1] + y1, fill, y2 - y1); 102 memset (data [x1] + y1, fill, y2 - y1);
79} 103}
80 104
81void layout::border (char fill) 105void layout::border (char fill)
82{ 106{
83 for (int i = 0; i < w; i++) data [i][0] = data [i][h - 1] = fill; 107 rect (0, 0, w, h, fill);
84 for (int j = 0; j < h; j++) data [0][j] = data [w - 1][j] = fill;
85} 108}
86 109
87void 110void
88layout::fill_rand (int percent) 111layout::fill_rand (int percent)
89{ 112{
171///////////////////////////////////////////////////////////////////////////// 194/////////////////////////////////////////////////////////////////////////////
172// isolation remover - ensures single connected area 195// isolation remover - ensures single connected area
173 196
174typedef fixed_stack<point> pointlist; 197typedef fixed_stack<point> pointlist;
175 198
176static noinline void 199static void noinline
177push_flood_fill (layout &dist, pointlist &seeds, int x, int y) 200push_flood_fill (layout &dist, pointlist &seeds, int x, int y)
178{ 201{
179 if (dist [x][y]) 202 if (dist [x][y])
180 return; 203 return;
181 204
192 ++y; 215 ++y;
193 } 216 }
194 217
195 while (--y >= y0) 218 while (--y >= y0)
196 { 219 {
197 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);
198 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);
199 } 222 }
200} 223}
201 224
202static inline void 225static inline void
203make_tunnel (layout &dist, pointlist &seeds, int x, int y, U8 d) 226make_tunnel (layout &dist, pointlist &seeds, int x, int y, U8 d, int perturb)
204{ 227{
205 for (;;) 228 for (;;)
206 { 229 {
230 point neigh[4];
231 int ncnt = 0;
232
233 d += perturb > 1;
234
207 if (x > 1 && U8 (dist [x - 1][y]) < d && dist [x - 1][y] > 1) 235 if (x > 0 && U8 (dist [x - 1][y]) < d && dist [x - 1][y] > 1) neigh [ncnt++] = point (x - 1, y);
208 --x;
209 else if (x < dist.w - 2 && U8 (dist [x + 1][y]) < d && dist [x + 1][y] > 1) 236 if (x < dist.w - 1 && U8 (dist [x + 1][y]) < d && dist [x + 1][y] > 1) neigh [ncnt++] = point (x + 1, y);
210 ++x;
211 else if (y > 1 && U8 (dist [x][y - 1]) < d && dist [x][y - 1] > 1) 237 if (y > 0 && U8 (dist [x][y - 1]) < d && dist [x][y - 1] > 1) neigh [ncnt++] = point (x, y - 1);
212 --y;
213 else if (y < dist.h - 2 && U8 (dist [x][y + 1]) < d && dist [x][y + 1] > 1) 238 if (y < dist.h - 1 && U8 (dist [x][y + 1]) < d && dist [x][y + 1] > 1) neigh [ncnt++] = point (x, y + 1);
214 ++y; 239
215 else 240 if (!ncnt)
216 break; 241 return;
242
243 point p = neigh [perturb ? rmg_rndm (ncnt) : 0];
244
245 seeds.push (p);
246
247 x = p.x;
248 y = p.y;
217 249
218 d = dist [x][y]; 250 d = dist [x][y];
219 dist [x][y] = 1; 251 dist [x][y] = 1;
220 seeds.push (point (x, y));
221 } 252 }
222} 253}
223 254
224static void inline 255static void inline
225maybe_push (layout &dist, pointlist &seeds, int x, int y, U8 d) 256maybe_push (layout &dist, pointlist &seeds, int x, int y, U8 d)
232 return; 263 return;
233 264
234 seeds.push (point (x, y)); 265 seeds.push (point (x, y));
235} 266}
236 267
237void 268// isolation remover, works on a "distance" map
238layout::isolation_remover (bool dirty) 269// the map must be initialised with 0 == rooms, 255 = walls
270static void noinline
271isolation_remover (layout &dist, unsigned int perturb = 2)
239{ 272{
240 layout dist (w, h);
241
242 // dist contains 273 // dist contains
243 // 0 == invisited rooms 274 // 0 == invisited rooms
244 // 1 == visited rooms 275 // 1 == visited rooms
245 // 2+ shortest distance to random near room 276 // 2+ shortest distance to random near room
246 277
247 // phase 1, initialise dist array, find seed 278 clamp_it (perturb, 0, 2);
279
280 // phase 1, find seed
248 int cnt = 0; 281 int cnt = 0;
249 int x, y; 282 int x, y;
250 283
251 for (int i = 0; i < w; ++i) 284 for (int i = 0; i < dist.w; ++i)
252 for (int j = 0; j < h; ++j) 285 for (int j = 0; j < dist.h; ++j)
253 { 286 if (!dist [i][j] && !rmg_rndm (++cnt))
254 if (data [i][j] == '#')
255 dist [i][j] = U8 (255);
256 else
257 {
258 dist [i][j] = 0;
259 if (!rmg_rndm (++cnt))
260 x = i, y = j; 287 x = i, y = j;
261 }
262 }
263 288
264 if (!cnt) 289 if (!cnt)
265 { 290 {
266 // map is completely massive, this is not good, 291 // map is completely massive, this is not good,
267 // so make it empty instead. 292 // so make it empty instead.
268 clear (); 293 dist.fill (1);
269 border ();
270 return; 294 return;
271 } 295 }
272 296
273 fixed_stack<point> seeds (w * h * 5); 297 fixed_stack<point> seeds (dist.w * dist.h * 5);
274 298
275 // found first free space - picking the first one gives 299 // found first free space - picking the first one gives
276 // us a slight bias for tunnels, but usually you won't 300 // us a slight bias for tunnels, but usually you won't
277 // notice that in-game 301 // notice that in-game
278 seeds.push (point (x, y)); 302 seeds.push (point (x, y));
290 y = p.y; 314 y = p.y;
291 315
292 if (!dist [x][y]) 316 if (!dist [x][y])
293 { 317 {
294 // found new isolated area, make tunnel 318 // found new isolated area, make tunnel
295 if (!dirty)
296 push_flood_fill (dist, seeds, x, y); 319 push_flood_fill (dist, seeds, x, y);
297
298 make_tunnel (dist, seeds, x, y, 255); 320 make_tunnel (dist, seeds, x, y, 254, perturb);
299
300 if (dirty)
301 push_flood_fill (dist, seeds, x, y);
302 } 321 }
303 else 322 else
304 { 323 {
305 // nothing here, continue to expand 324 // nothing here, continue to expand
306 U8 d = U8 (dist [x][y]) + 1; 325 U8 d = U8 (dist [x][y]) + 1;
309 if (x > 0) maybe_push (dist, seeds, x - 1, y, d); 328 if (x > 0) maybe_push (dist, seeds, x - 1, y, d);
310 if (y < dist.h - 1) maybe_push (dist, seeds, x, y + 1, d); 329 if (y < dist.h - 1) maybe_push (dist, seeds, x, y + 1, d);
311 if (y > 0) maybe_push (dist, seeds, x, y - 1, d); 330 if (y > 0) maybe_push (dist, seeds, x, y - 1, d);
312 } 331 }
313 } 332 }
333}
334
335void
336layout::isolation_remover (int perturb)
337{
338 layout dist (w - 2, h - 2); // map without border
339
340 for (int x = 1; x < w - 1; ++x)
341 for (int y = 1; y < h - 1; ++y)
342 dist [x - 1][y - 1] = data [x][y] == '#' ? U8 (255) : 0;
343
344 ::isolation_remover (dist, perturb);
314 345
315 // now copy the tunnels over 346 // now copy the tunnels over
316 for (int x = 0; x < w; ++x) 347 for (int x = 1; x < w - 1; ++x)
317 for (int y = 0; y < h; ++y) 348 for (int y = 1; y < h - 1; ++y)
318 if (data [x][y] == '#' && dist [x][y] == 1) 349 if (data [x][y] == '#' && dist [x - 1][y - 1] == 1)
319 data [x][y] = 0; 350 data [x][y] = 0;
320} 351}
321 352
322///////////////////////////////////////////////////////////////////////////// 353/////////////////////////////////////////////////////////////////////////////
323 354
324// inspired mostly by http://www.jimrandomh.org/misc/caves.txt
325void
326layout::gen_cave (int subtype)
327{
328 switch (subtype)
329 {
330 // a rough cave
331 case 0:
332 fill_rand (rmg_rndm (80, 95));
333 break;
334
335 // corridors
336 case 1:
337 fill_rand (rmg_rndm (5, 40));
338 erode_1_2 (5, 2, 10);
339 erode_1_2 (5, -1, 10);
340 erode_1_2 (5, 2, 1);
341 break;
342
343 // somewhat open, roundish
344 case 2:
345 fill_rand (45);
346 erode_1_2 (5, 0, 5);
347 erode_1_2 (5, 1, 1);
348 break;
349
350 // wide open, some room-like structures
351 case 3:
352 fill_rand (45);
353 erode_1_2 (5, 2, 4);
354 erode_1_2 (5, -1, 3);
355 break;
356 }
357
358 border ();
359 isolation_remover ();
360}
361
362/////////////////////////////////////////////////////////////////////////////
363
364//+GPL 355//+GPL
365 356
366/* puts doors at appropriate locations in a maze. */ 357/* puts doors at appropriate locations in a maze. */
367void 358void
368layout::doorify () 359layout::doorify ()
369{ 360{
370 int ndoors = w * h / 60; /* reasonable number of doors. */ 361 int ndoors = w * h / 60; /* reasonable number of doors. */
371 int doorlocs = 0; /* # of available doorlocations */
372 362
373 uint16 *doorlist_x = salloc<uint16> (w * h); 363 fixed_stack<point> doorloc (w * h);
374 uint16 *doorlist_y = salloc<uint16> (w * h);
375 364
376 /* make a list of possible door locations */ 365 /* make a list of possible door locations */
377 for (int i = 1; i < w - 1; i++) 366 for (int i = 1; i < w - 1; i++)
378 for (int j = 1; j < h - 1; j++) 367 for (int j = 1; j < h - 1; j++)
379 { 368 {
380 int sindex = surround_flag (*this, i, j); 369 int sindex = surround_flag (*this, i, j);
381 370
382 if (sindex == 3 || sindex == 12) /* these are possible door sindex */ 371 if (sindex == 3 || sindex == 12) /* these are possible door sindex */
383 { 372 doorloc.push (point (i, j));
384 doorlist_x [doorlocs] = i;
385 doorlist_y [doorlocs] = j;
386 doorlocs++;
387 }
388 } 373 }
389 374
390 while (ndoors > 0 && doorlocs > 0) 375 while (ndoors && doorloc.size)
391 { 376 {
392 int di = rmg_rndm (doorlocs); 377 point p = doorloc.remove (rmg_rndm (doorloc.size));
393 int i = doorlist_x [di]; 378
394 int j = doorlist_y [di];
395 int sindex = surround_flag (*this, i, j); 379 int sindex = surround_flag (*this, p.x, p.y);
396 380
397 if (sindex == 3 || sindex == 12) /* these are possible door sindex */ 381 if (sindex == 3 || sindex == 12) /* these are possible door sindex */
398 { 382 {
399 data [i][j] = 'D'; 383 data [p.x][p.y] = 'D';
400 ndoors--; 384 --ndoors;
401 } 385 }
402
403 /* reduce the size of the list */
404 doorlocs--;
405 doorlist_x[di] = doorlist_x [doorlocs];
406 doorlist_y[di] = doorlist_y [doorlocs];
407 } 386 }
408
409 sfree (doorlist_x, w * h);
410 sfree (doorlist_y, w * h);
411} 387}
412 388
413/* takes a map and makes it symmetric: adjusts Xsize and 389/* takes a map and makes it symmetric: adjusts Xsize and
414 * Ysize to produce a symmetric map. 390 * Ysize to produce a symmetric map.
415 */ 391 */
784 else 760 else
785 make_wall (*this, dx, dy, 1); 761 make_wall (*this, dx, dy, 1);
786 } 762 }
787} 763}
788 764
765//-GPL
766
789///////////////////////////////////////////////////////////////////////////// 767/////////////////////////////////////////////////////////////////////////////
768
769// inspired mostly by http://www.jimrandomh.org/misc/caves.txt
770void
771layout::gen_cave (int subtype)
772{
773 switch (subtype)
774 {
775 // a rough cave
776 case 0:
777 fill_rand (rmg_rndm (85, 97));
778 break;
779
780 // corridors
781 case 1:
782 fill_rand (rmg_rndm (5, 40));
783 erode_1_2 (5, 2, 10);
784 erode_1_2 (5, -1, 10);
785 erode_1_2 (5, 2, 1);
786 break;
787
788 // somewhat open, some room-like structures
789 case 2:
790 fill_rand (45);
791 erode_1_2 (5, 2, 4);
792 erode_1_2 (5, -1, 3);
793 break;
794
795 // wide open, roundish
796 case 3:
797 fill_rand (45);
798 erode_1_2 (5, 0, 5);
799 erode_1_2 (5, 1, 1);
800 break;
801 }
802
803 border ();
804 isolation_remover (1);
805}
806
807void
808layout::gen_castle ()
809{
810 fill ('#');
811
812 for (int n = w * h / 30 + 1; n--; )
813 {
814 int rw = rmg_rndm (6, 10);
815 int rh = rmg_rndm (6, 10);
816
817 if (rw > w || rh > h)
818 continue;
819
820 int rx = rmg_rndm (0, w - rw);
821 int ry = rmg_rndm (0, h - rh);
822
823 rect (rx, ry, rx + rw, ry + rh, '#');
824 fill_rect (rx + 1, ry + 1, rx + rw - 1, ry + rh - 1, 0);
825 }
826
827 border ();
828 isolation_remover (0);
829}
830
831static void
832gen_mixed_ (layout &maze, random_map_params *RP)
833{
834 int dir;
835
836 if (maze.w < 20 && maze.h < 20 && !rmg_rndm (3))
837 dir = 2; // stop recursion randomly
838 else
839 dir = maze.w > maze.h;
840
841 if (dir == 0 && maze.w > 16)
842 {
843 int m = rmg_rndm (8, maze.w - 8);
844
845 layout m1 (maze, 0, 0, m , maze.h); gen_mixed_ (m1, RP);
846 layout m2 (maze, m, 0, maze.w, maze.h); gen_mixed_ (m2, RP);
847 }
848 else if (dir == 1 && maze.h > 16)
849 {
850 int m = rmg_rndm (8, maze.h - 8);
851
852 layout m1 (maze, 0, 0, maze.w, m ); gen_mixed_ (m1, RP);
853 layout m2 (maze, 0, m, maze.w, maze.h); gen_mixed_ (m2, RP);
854 }
855 else
856 {
857 RP->map_layout_style = rmg_rndm (NROFLAYOUTS - 2) + 1;
858
859 if (RP->map_layout_style == LAYOUT_MULTIPLE)
860 ++RP->map_layout_style;
861
862 maze.generate (RP);
863 }
864}
865
866// recursive subdivision with random sublayouts
867static void
868gen_mixed (layout &maze, random_map_params *RP)
869{
870 random_map_params &rp = *new random_map_params (RP);
871 gen_mixed_ (maze, &rp);
872 delete &rp;
873
874 maze.border ();
875 maze.isolation_remover (0);
876}
877
878//+GPL
790 879
791/* function selects the maze function and gives it whatever 880/* function selects the maze function and gives it whatever
792 arguments it needs. */ 881 arguments it needs. */
793void 882void
794layout::generate (random_map_params *RP) 883layout::generate (random_map_params *RP)
854 if (rmg_rndm (2)) 943 if (rmg_rndm (2))
855 doorify (); 944 doorify ();
856 945
857 break; 946 break;
858 947
948 case LAYOUT_CASTLE:
949 gen_castle ();
950
951 if (rmg_rndm (2))
952 doorify ();
953
954 break;
955
956 case LAYOUT_MULTIPLE:
957 gen_mixed (*this, RP);
958 break;
959
859 default: 960 default:
860 abort (); 961 abort ();
861 } 962 }
963}
862 964
863 /* rotate the maze randomly */ 965//-GPL
864 rotate (rmg_rndm (4));
865
866 symmetrize (RP->symmetry_used);
867 966
868#if 0 967#if 0
869 print ();//D 968static void
870#endif 969gen_village (layout &maze)
970{
971 maze.clear ();
972 maze.border ();
871 973
872 if (RP->expand2x) 974 for (int n = maze.w * maze.h / 200 + 1; n--; )
873 expand2x (); 975 {
874} 976 int rw = rmg_rndm (6, 10);
977 int rh = rmg_rndm (6, 10);
875 978
876//-GPL 979 int rx = rmg_rndm (2, maze.w - rw - 2);
980 int ry = rmg_rndm (2, maze.h - rh - 2);
877 981
878#if 0 982 maze.rect (rx, ry, rx + rw, ry + rh, '#');
983 }
984
985 maze.border ();
986 maze.isolation_remover (2);
987}
988
879static struct demo 989static struct demo
880{ 990{
881 demo () 991 demo ()
882 { 992 {
883 rmg_rndm.seed (time (0)); 993 rmg_rndm.seed (time (0));
884 994
885 for(int i=1;i<10;i) 995 for(int i=1;i<100;i++)
886 { 996 {
887 layout maze (10, 10); 997 layout maze (40, 30);
888 maze.fill_rand (90); 998 gen_village (maze);
889 maze.border ();
890 maze.isolation_remover ();
891 maze.doorify (); 999 maze.doorify ();
892 maze.symmetrize (rmg_rndm (2, 4));
893 maze.rotate (rmg_rndm (4));
894 maze.expand2x ();
895 maze.print (); 1000 maze.print ();
1001 exit(0);
896 } 1002 }
897 1003
898 exit (1); 1004 exit (1);
899 } 1005 }
900} demo; 1006} demo;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines