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.16 by root, Sat Jul 3 11:52:11 2010 UTC vs.
Revision 1.21 by root, Sun Jul 4 01:01:42 2010 UTC

221 if (x < dist.w - 1 && !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);
222 } 222 }
223} 223}
224 224
225static inline void 225static inline void
226make_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)
227{ 227{
228 for (;;) 228 for (;;)
229 { 229 {
230 point neigh[4]; 230 point neigh[4];
231 int ncnt = 0; 231 int ncnt = 0;
232 232
233 d += perturb > 1;
234
233 if (x > 0 && U8 (dist [x - 1][y]) <= d && dist [x - 1][y] > 1) neigh [ncnt++] = point (x - 1, y); 235 if (x > 0 && 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); 236 if (x < dist.w - 1 && U8 (dist [x + 1][y]) < d && dist [x + 1][y] > 1) neigh [ncnt++] = point (x + 1, y);
235 if (y > 0 && U8 (dist [x][y - 1]) <= d && dist [x][y - 1] > 1) neigh [ncnt++] = point (x, y - 1); 237 if (y > 0 && 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); 238 if (y < dist.h - 1 && U8 (dist [x][y + 1]) < d && dist [x][y + 1] > 1) neigh [ncnt++] = point (x, y + 1);
237 239
238 if (!ncnt) 240 if (!ncnt)
239 return; 241 return;
240 242
241 point &p = neigh [rmg_rndm (ncnt)]; 243 point p = neigh [perturb ? rmg_rndm (ncnt) : 0];
242 244
243 seeds.push (p); 245 seeds.push (p);
244 246
245 x = p.x; 247 x = p.x;
246 y = p.y; 248 y = p.y;
264} 266}
265 267
266// isolation remover, works on a "distance" map 268// isolation remover, works on a "distance" map
267// the map must be initialised with 0 == rooms, 255 = walls 269// the map must be initialised with 0 == rooms, 255 = walls
268static void noinline 270static void noinline
269isolation_remover (layout &dist) 271isolation_remover (layout &dist, unsigned int perturb = 2)
270{ 272{
271 // dist contains 273 // dist contains
272 // 0 == invisited rooms 274 // 0 == invisited rooms
273 // 1 == visited rooms 275 // 1 == visited rooms
274 // 2+ shortest distance to random near room 276 // 2+ shortest distance to random near room
275 277
278 clamp_it (perturb, 0, 2);
279
276 // phase 1, find seed 280 // phase 1, find seed
277 int cnt = 0; 281 int cnt = 0;
278 int x, y; 282 int x, y;
279 283
280 for (int i = 0; i < dist.w; ++i) 284 for (int i = 0; i < dist.w; ++i)
311 315
312 if (!dist [x][y]) 316 if (!dist [x][y])
313 { 317 {
314 // found new isolated area, make tunnel 318 // found new isolated area, make tunnel
315 push_flood_fill (dist, seeds, x, y); 319 push_flood_fill (dist, seeds, x, y);
316 make_tunnel (dist, seeds, x, y, 255); 320 make_tunnel (dist, seeds, x, y, 254, perturb);
317 } 321 }
318 else 322 else
319 { 323 {
320 // nothing here, continue to expand 324 // nothing here, continue to expand
321 U8 d = U8 (dist [x][y]) + 1; 325 U8 d = U8 (dist [x][y]) + 1;
327 } 331 }
328 } 332 }
329} 333}
330 334
331void 335void
332layout::isolation_remover () 336layout::isolation_remover (int perturb)
333{ 337{
334 layout dist (w - 2, h - 2); // map without border 338 layout dist (w - 2, h - 2); // map without border
335 339
336 for (int x = 1; x < w - 1; ++x) 340 for (int x = 1; x < w - 1; ++x)
337 for (int y = 1; y < h - 1; ++y) 341 for (int y = 1; y < h - 1; ++y)
338 dist [x - 1][y - 1] = data [x][y] == '#' ? U8 (255) : 0; 342 dist [x - 1][y - 1] = data [x][y] == '#' ? U8 (255) : 0;
339 343
340 ::isolation_remover (dist); 344 ::isolation_remover (dist, perturb);
341 345
342 // now copy the tunnels over 346 // now copy the tunnels over
343 for (int x = 1; x < w - 1; ++x) 347 for (int x = 1; x < w - 1; ++x)
344 for (int y = 1; y < h - 1; ++y) 348 for (int y = 1; y < h - 1; ++y)
345 if (data [x][y] == '#' && dist [x - 1][y - 1] == 1) 349 if (data [x][y] == '#' && dist [x - 1][y - 1] == 1)
353/* puts doors at appropriate locations in a maze. */ 357/* puts doors at appropriate locations in a maze. */
354void 358void
355layout::doorify () 359layout::doorify ()
356{ 360{
357 int ndoors = w * h / 60; /* reasonable number of doors. */ 361 int ndoors = w * h / 60; /* reasonable number of doors. */
358 int doorlocs = 0; /* # of available doorlocations */
359 362
360 uint16 *doorlist_x = salloc<uint16> (w * h); 363 fixed_stack<point> doorloc (w * h);
361 uint16 *doorlist_y = salloc<uint16> (w * h);
362 364
363 /* make a list of possible door locations */ 365 /* make a list of possible door locations */
364 for (int i = 1; i < w - 1; i++) 366 for (int i = 1; i < w - 1; i++)
365 for (int j = 1; j < h - 1; j++) 367 for (int j = 1; j < h - 1; j++)
366 { 368 {
367 int sindex = surround_flag (*this, i, j); 369 int sindex = surround_flag (*this, i, j);
368 370
369 if (sindex == 3 || sindex == 12) /* these are possible door sindex */ 371 if (sindex == 3 || sindex == 12) /* these are possible door sindex */
370 { 372 doorloc.push (point (i, j));
371 doorlist_x [doorlocs] = i;
372 doorlist_y [doorlocs] = j;
373 doorlocs++;
374 }
375 } 373 }
376 374
377 while (ndoors > 0 && doorlocs > 0) 375 while (ndoors && doorloc.size)
378 { 376 {
379 int di = rmg_rndm (doorlocs); 377 point p = doorloc.remove (rmg_rndm (doorloc.size));
380 int i = doorlist_x [di]; 378
381 int j = doorlist_y [di];
382 int sindex = surround_flag (*this, i, j); 379 int sindex = surround_flag (*this, p.x, p.y);
383 380
384 if (sindex == 3 || sindex == 12) /* these are possible door sindex */ 381 if (sindex == 3 || sindex == 12) /* these are possible door sindex */
385 { 382 {
386 data [i][j] = 'D'; 383 data [p.x][p.y] = 'D';
387 ndoors--; 384 --ndoors;
388 } 385 }
389
390 /* reduce the size of the list */
391 doorlocs--;
392 doorlist_x[di] = doorlist_x [doorlocs];
393 doorlist_y[di] = doorlist_y [doorlocs];
394 } 386 }
395
396 sfree (doorlist_x, w * h);
397 sfree (doorlist_y, w * h);
398} 387}
399 388
400/* takes a map and makes it symmetric: adjusts Xsize and 389/* takes a map and makes it symmetric: adjusts Xsize and
401 * Ysize to produce a symmetric map. 390 * Ysize to produce a symmetric map.
402 */ 391 */
771 else 760 else
772 make_wall (*this, dx, dy, 1); 761 make_wall (*this, dx, dy, 1);
773 } 762 }
774} 763}
775 764
765//-GPL
766
776///////////////////////////////////////////////////////////////////////////// 767/////////////////////////////////////////////////////////////////////////////
777 768
778// inspired mostly by http://www.jimrandomh.org/misc/caves.txt 769// inspired mostly by http://www.jimrandomh.org/misc/caves.txt
779void 770void
780layout::gen_cave (int subtype) 771layout::gen_cave (int subtype)
792 erode_1_2 (5, 2, 10); 783 erode_1_2 (5, 2, 10);
793 erode_1_2 (5, -1, 10); 784 erode_1_2 (5, -1, 10);
794 erode_1_2 (5, 2, 1); 785 erode_1_2 (5, 2, 1);
795 break; 786 break;
796 787
797 // somewhat open, roundish 788 // somewhat open, some room-like structures
798 case 2: 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:
799 fill_rand (45); 797 fill_rand (45);
800 erode_1_2 (5, 0, 5); 798 erode_1_2 (5, 0, 5);
801 erode_1_2 (5, 1, 1); 799 erode_1_2 (5, 1, 1);
802 break; 800 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 } 801 }
811 802
812 border (); 803 border ();
813 isolation_remover (); 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);
814} 829}
815 830
816static void 831static void
817gen_mixed_ (layout &maze, random_map_params *RP, int dir) 832gen_mixed_ (layout &maze, random_map_params *RP)
818{ 833{
834 int dir;
835
819 if (maze.w < 20 && maze.h < 20 && !rmg_rndm (3)) 836 if (maze.w < 20 && maze.h < 20 && !rmg_rndm (3))
820 dir = 2; // stop recursion randomly 837 dir = 2; // stop recursion randomly
838 else
839 dir = maze.w > maze.h;
821 840
822 if (dir == 0 && maze.w > 16) 841 if (dir == 0 && maze.w > 16)
823 { 842 {
824 int m = rmg_rndm (8, maze.w - 8); 843 int m = rmg_rndm (8, maze.w - 8);
825 844
826 layout m1 (maze, 0, 0, m , maze.h); gen_mixed_ (m1, RP, !dir); 845 layout m1 (maze, 0, 0, m , maze.h); gen_mixed_ (m1, RP);
827 layout m2 (maze, m, 0, maze.w, maze.h); gen_mixed_ (m2, RP, !dir); 846 layout m2 (maze, m, 0, maze.w, maze.h); gen_mixed_ (m2, RP);
828 } 847 }
829 else if (dir == 1 && maze.h > 16) 848 else if (dir == 1 && maze.h > 16)
830 { 849 {
831 int m = rmg_rndm (8, maze.h - 8); 850 int m = rmg_rndm (8, maze.h - 8);
832 851
833 layout m1 (maze, 0, 0, maze.w, m ); gen_mixed_ (m1, RP, !dir); 852 layout m1 (maze, 0, 0, maze.w, m ); gen_mixed_ (m1, RP);
834 layout m2 (maze, 0, m, maze.w, maze.h); gen_mixed_ (m2, RP, !dir); 853 layout m2 (maze, 0, m, maze.w, maze.h); gen_mixed_ (m2, RP);
835 } 854 }
836 else 855 else
837 { 856 {
838 RP->map_layout_style = rmg_rndm (NROFLAYOUTS - 2) + 1; 857 RP->map_layout_style = rmg_rndm (NROFLAYOUTS - 2) + 1;
839 858
847// recursive subdivision with random sublayouts 866// recursive subdivision with random sublayouts
848static void 867static void
849gen_mixed (layout &maze, random_map_params *RP) 868gen_mixed (layout &maze, random_map_params *RP)
850{ 869{
851 random_map_params &rp = *new random_map_params (RP); 870 random_map_params &rp = *new random_map_params (RP);
852 gen_mixed_ (maze, &rp, rmg_rndm (2)); 871 gen_mixed_ (maze, &rp);
853 delete &rp; 872 delete &rp;
854 873
855 maze.border (); 874 maze.border ();
856 maze.isolation_remover (); 875 maze.isolation_remover (0);
857} 876}
877
878//+GPL
858 879
859/* function selects the maze function and gives it whatever 880/* function selects the maze function and gives it whatever
860 arguments it needs. */ 881 arguments it needs. */
861void 882void
862layout::generate (random_map_params *RP) 883layout::generate (random_map_params *RP)
922 if (rmg_rndm (2)) 943 if (rmg_rndm (2))
923 doorify (); 944 doorify ();
924 945
925 break; 946 break;
926 947
948 case LAYOUT_CASTLE:
949 gen_castle ();
950
951 if (rmg_rndm (2))
952 doorify ();
953
954 break;
955
927 case LAYOUT_MULTIPLE: 956 case LAYOUT_MULTIPLE:
928 gen_mixed (*this, RP); 957 gen_mixed (*this, RP);
929 break; 958 break;
930 959
931 default: 960 default:
934} 963}
935 964
936//-GPL 965//-GPL
937 966
938#if 0 967#if 0
968static void
969gen_village (layout &maze)
970{
971 maze.clear ();
972 maze.border ();
973
974 for (int n = maze.w * maze.h / 200 + 1; n--; )
975 {
976 int rw = rmg_rndm (6, 10);
977 int rh = rmg_rndm (6, 10);
978
979 int rx = rmg_rndm (2, maze.w - rw - 2);
980 int ry = rmg_rndm (2, maze.h - rh - 2);
981
982 maze.rect (rx, ry, rx + rw, ry + rh, '#');
983 }
984
985 maze.border ();
986 maze.isolation_remover (2);
987}
988
939static struct demo 989static struct demo
940{ 990{
941 demo () 991 demo ()
942 { 992 {
943 rmg_rndm.seed (time (0)); 993 rmg_rndm.seed (time (0));
944 994
945 for(int i=1;i<100;i++) 995 for(int i=1;i<100;i++)
946 { 996 {
947 layout maze (40, 25); 997 layout maze (40, 30);
948 maze.fill_rand (85); 998 gen_village (maze);
949 maze.border (); 999 maze.doorify ();
950 maze.isolation_remover ();
951 maze.print (); 1000 maze.print ();
1001 exit(0);
952 } 1002 }
953 1003
954 exit (1); 1004 exit (1);
955 } 1005 }
956} demo; 1006} demo;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines