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.20 by root, Sun Jul 4 00:27:14 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, int dir)
818{ 833{
851 random_map_params &rp = *new random_map_params (RP); 866 random_map_params &rp = *new random_map_params (RP);
852 gen_mixed_ (maze, &rp, rmg_rndm (2)); 867 gen_mixed_ (maze, &rp, rmg_rndm (2));
853 delete &rp; 868 delete &rp;
854 869
855 maze.border (); 870 maze.border ();
856 maze.isolation_remover (); 871 maze.isolation_remover (0);
857} 872}
873
874//+GPL
858 875
859/* function selects the maze function and gives it whatever 876/* function selects the maze function and gives it whatever
860 arguments it needs. */ 877 arguments it needs. */
861void 878void
862layout::generate (random_map_params *RP) 879layout::generate (random_map_params *RP)
922 if (rmg_rndm (2)) 939 if (rmg_rndm (2))
923 doorify (); 940 doorify ();
924 941
925 break; 942 break;
926 943
944 case LAYOUT_CASTLE:
945 gen_castle ();
946
947 if (rmg_rndm (2))
948 doorify ();
949
950 break;
951
927 case LAYOUT_MULTIPLE: 952 case LAYOUT_MULTIPLE:
928 gen_mixed (*this, RP); 953 gen_mixed (*this, RP);
929 break; 954 break;
930 955
931 default: 956 default:
934} 959}
935 960
936//-GPL 961//-GPL
937 962
938#if 0 963#if 0
964static void
965gen_village (layout &maze)
966{
967 maze.clear ();
968 maze.border ();
969
970 for (int n = maze.w * maze.h / 200 + 1; n--; )
971 {
972 int rw = rmg_rndm (6, 10);
973 int rh = rmg_rndm (6, 10);
974
975 int rx = rmg_rndm (2, maze.w - rw - 2);
976 int ry = rmg_rndm (2, maze.h - rh - 2);
977
978 maze.rect (rx, ry, rx + rw, ry + rh, '#');
979 }
980
981 maze.border ();
982 maze.isolation_remover (2);
983}
984
939static struct demo 985static struct demo
940{ 986{
941 demo () 987 demo ()
942 { 988 {
943 rmg_rndm.seed (time (0)); 989 rmg_rndm.seed (time (0));
944 990
945 for(int i=1;i<100;i++) 991 for(int i=1;i<100;i++)
946 { 992 {
947 layout maze (40, 25); 993 layout maze (40, 30);
948 maze.fill_rand (85); 994 gen_village (maze);
949 maze.border (); 995 maze.doorify ();
950 maze.isolation_remover ();
951 maze.print (); 996 maze.print ();
997 exit(0);
952 } 998 }
953 999
954 exit (1); 1000 exit (1);
955 } 1001 }
956} demo; 1002} demo;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines