… | |
… | |
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 | |
225 | static inline void |
225 | static inline void |
226 | make_tunnel (layout &dist, pointlist &seeds, int x, int y, U8 d) |
226 | make_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 |
268 | static void noinline |
270 | static void noinline |
269 | isolation_remover (layout &dist) |
271 | isolation_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 | |
331 | void |
335 | void |
332 | layout::isolation_remover () |
336 | layout::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) |
346 | data [x][y] = 0; |
350 | data [x][y] = 0; |
347 | } |
351 | } |
348 | |
352 | |
349 | ///////////////////////////////////////////////////////////////////////////// |
353 | ///////////////////////////////////////////////////////////////////////////// |
350 | |
354 | |
351 | // inspired mostly by http://www.jimrandomh.org/misc/caves.txt |
|
|
352 | void |
|
|
353 | layout::gen_cave (int subtype) |
|
|
354 | { |
|
|
355 | switch (subtype) |
|
|
356 | { |
|
|
357 | // a rough cave |
|
|
358 | case 0: |
|
|
359 | fill_rand (rmg_rndm (85, 97)); |
|
|
360 | break; |
|
|
361 | |
|
|
362 | // corridors |
|
|
363 | case 1: |
|
|
364 | fill_rand (rmg_rndm (5, 40)); |
|
|
365 | erode_1_2 (5, 2, 10); |
|
|
366 | erode_1_2 (5, -1, 10); |
|
|
367 | erode_1_2 (5, 2, 1); |
|
|
368 | break; |
|
|
369 | |
|
|
370 | // somewhat open, roundish |
|
|
371 | case 2: |
|
|
372 | fill_rand (45); |
|
|
373 | erode_1_2 (5, 0, 5); |
|
|
374 | erode_1_2 (5, 1, 1); |
|
|
375 | break; |
|
|
376 | |
|
|
377 | // wide open, some room-like structures |
|
|
378 | case 3: |
|
|
379 | fill_rand (45); |
|
|
380 | erode_1_2 (5, 2, 4); |
|
|
381 | erode_1_2 (5, -1, 3); |
|
|
382 | break; |
|
|
383 | } |
|
|
384 | |
|
|
385 | border (); |
|
|
386 | isolation_remover (); |
|
|
387 | } |
|
|
388 | |
|
|
389 | ///////////////////////////////////////////////////////////////////////////// |
|
|
390 | |
|
|
391 | //+GPL |
355 | //+GPL |
392 | |
356 | |
393 | /* puts doors at appropriate locations in a maze. */ |
357 | /* puts doors at appropriate locations in a maze. */ |
394 | void |
358 | void |
395 | layout::doorify () |
359 | layout::doorify () |
396 | { |
360 | { |
397 | int ndoors = w * h / 60; /* reasonable number of doors. */ |
361 | int ndoors = w * h / 60; /* reasonable number of doors. */ |
398 | int doorlocs = 0; /* # of available doorlocations */ |
|
|
399 | |
362 | |
400 | uint16 *doorlist_x = salloc<uint16> (w * h); |
363 | fixed_stack<point> doorloc (w * h); |
401 | uint16 *doorlist_y = salloc<uint16> (w * h); |
|
|
402 | |
364 | |
403 | /* make a list of possible door locations */ |
365 | /* make a list of possible door locations */ |
404 | for (int i = 1; i < w - 1; i++) |
366 | for (int i = 1; i < w - 1; i++) |
405 | for (int j = 1; j < h - 1; j++) |
367 | for (int j = 1; j < h - 1; j++) |
406 | { |
368 | { |
407 | int sindex = surround_flag (*this, i, j); |
369 | int sindex = surround_flag (*this, i, j); |
408 | |
370 | |
409 | if (sindex == 3 || sindex == 12) /* these are possible door sindex */ |
371 | if (sindex == 3 || sindex == 12) /* these are possible door sindex */ |
410 | { |
372 | doorloc.push (point (i, j)); |
411 | doorlist_x [doorlocs] = i; |
|
|
412 | doorlist_y [doorlocs] = j; |
|
|
413 | doorlocs++; |
|
|
414 | } |
|
|
415 | } |
373 | } |
416 | |
374 | |
417 | while (ndoors > 0 && doorlocs > 0) |
375 | while (ndoors && doorloc.size) |
418 | { |
376 | { |
419 | int di = rmg_rndm (doorlocs); |
377 | point p = doorloc.remove (rmg_rndm (doorloc.size)); |
420 | int i = doorlist_x [di]; |
378 | |
421 | int j = doorlist_y [di]; |
|
|
422 | int sindex = surround_flag (*this, i, j); |
379 | int sindex = surround_flag (*this, p.x, p.y); |
423 | |
380 | |
424 | if (sindex == 3 || sindex == 12) /* these are possible door sindex */ |
381 | if (sindex == 3 || sindex == 12) /* these are possible door sindex */ |
425 | { |
382 | { |
426 | data [i][j] = 'D'; |
383 | data [p.x][p.y] = 'D'; |
427 | ndoors--; |
384 | --ndoors; |
428 | } |
385 | } |
429 | |
|
|
430 | /* reduce the size of the list */ |
|
|
431 | doorlocs--; |
|
|
432 | doorlist_x[di] = doorlist_x [doorlocs]; |
|
|
433 | doorlist_y[di] = doorlist_y [doorlocs]; |
|
|
434 | } |
386 | } |
435 | |
|
|
436 | sfree (doorlist_x, w * h); |
|
|
437 | sfree (doorlist_y, w * h); |
|
|
438 | } |
387 | } |
439 | |
388 | |
440 | /* takes a map and makes it symmetric: adjusts Xsize and |
389 | /* takes a map and makes it symmetric: adjusts Xsize and |
441 | * Ysize to produce a symmetric map. |
390 | * Ysize to produce a symmetric map. |
442 | */ |
391 | */ |
… | |
… | |
811 | else |
760 | else |
812 | make_wall (*this, dx, dy, 1); |
761 | make_wall (*this, dx, dy, 1); |
813 | } |
762 | } |
814 | } |
763 | } |
815 | |
764 | |
|
|
765 | //-GPL |
|
|
766 | |
816 | ///////////////////////////////////////////////////////////////////////////// |
767 | ///////////////////////////////////////////////////////////////////////////// |
817 | |
768 | |
|
|
769 | // inspired mostly by http://www.jimrandomh.org/misc/caves.txt |
|
|
770 | void |
|
|
771 | layout::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 | |
|
|
807 | void |
|
|
808 | layout::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 | |
818 | static void |
831 | static void |
819 | gen_mixed_ (layout &maze, random_map_params *RP, int dir) |
832 | gen_mixed_ (layout &maze, random_map_params *RP) |
820 | { |
833 | { |
|
|
834 | int dir; |
|
|
835 | |
821 | if (maze.w < 20 && maze.h < 20 && !rmg_rndm (3)) |
836 | if (maze.w < 20 && maze.h < 20 && !rmg_rndm (3)) |
822 | dir = 2; // stop recursion randomly |
837 | dir = 2; // stop recursion randomly |
|
|
838 | else |
|
|
839 | dir = maze.w > maze.h; |
823 | |
840 | |
824 | if (dir == 0 && maze.w > 16) |
841 | if (dir == 0 && maze.w > 16) |
825 | { |
842 | { |
826 | int m = rmg_rndm (8, maze.w - 8); |
843 | int m = rmg_rndm (8, maze.w - 8); |
827 | |
844 | |
828 | 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); |
829 | 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); |
830 | } |
847 | } |
831 | else if (dir == 1 && maze.h > 16) |
848 | else if (dir == 1 && maze.h > 16) |
832 | { |
849 | { |
833 | int m = rmg_rndm (8, maze.h - 8); |
850 | int m = rmg_rndm (8, maze.h - 8); |
834 | |
851 | |
835 | 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); |
836 | 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); |
837 | } |
854 | } |
838 | else |
855 | else |
839 | { |
856 | { |
840 | RP->map_layout_style = rmg_rndm (NROFLAYOUTS - 2) + 1; |
857 | RP->map_layout_style = rmg_rndm (NROFLAYOUTS - 2) + 1; |
841 | |
858 | |
… | |
… | |
844 | |
861 | |
845 | maze.generate (RP); |
862 | maze.generate (RP); |
846 | } |
863 | } |
847 | } |
864 | } |
848 | |
865 | |
|
|
866 | // recursive subdivision with random sublayouts |
849 | static void |
867 | static void |
850 | gen_mixed (layout &maze, random_map_params *RP) |
868 | gen_mixed (layout &maze, random_map_params *RP) |
851 | { |
869 | { |
852 | random_map_params &rp = *new random_map_params (RP); |
870 | random_map_params &rp = *new random_map_params (RP); |
853 | gen_mixed_ (maze, &rp, rmg_rndm (2)); |
871 | gen_mixed_ (maze, &rp); |
854 | delete &rp; |
872 | delete &rp; |
855 | |
873 | |
856 | maze.border (); |
874 | maze.border (); |
857 | maze.isolation_remover (); |
875 | maze.isolation_remover (0); |
858 | } |
876 | } |
|
|
877 | |
|
|
878 | //+GPL |
859 | |
879 | |
860 | /* function selects the maze function and gives it whatever |
880 | /* function selects the maze function and gives it whatever |
861 | arguments it needs. */ |
881 | arguments it needs. */ |
862 | void |
882 | void |
863 | layout::generate (random_map_params *RP) |
883 | layout::generate (random_map_params *RP) |
… | |
… | |
923 | if (rmg_rndm (2)) |
943 | if (rmg_rndm (2)) |
924 | doorify (); |
944 | doorify (); |
925 | |
945 | |
926 | break; |
946 | break; |
927 | |
947 | |
|
|
948 | case LAYOUT_CASTLE: |
|
|
949 | gen_castle (); |
|
|
950 | |
|
|
951 | if (rmg_rndm (2)) |
|
|
952 | doorify (); |
|
|
953 | |
|
|
954 | break; |
|
|
955 | |
928 | case LAYOUT_MULTIPLE: |
956 | case LAYOUT_MULTIPLE: |
929 | gen_mixed (*this, RP); |
957 | gen_mixed (*this, RP); |
930 | break; |
958 | break; |
931 | |
959 | |
932 | default: |
960 | default: |
… | |
… | |
935 | } |
963 | } |
936 | |
964 | |
937 | //-GPL |
965 | //-GPL |
938 | |
966 | |
939 | #if 0 |
967 | #if 0 |
|
|
968 | static void |
|
|
969 | gen_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 | |
940 | static struct demo |
989 | static struct demo |
941 | { |
990 | { |
942 | demo () |
991 | demo () |
943 | { |
992 | { |
944 | rmg_rndm.seed (time (0)); |
993 | rmg_rndm.seed (time (0)); |
945 | |
994 | |
946 | for(int i=1;i<100;i++) |
995 | for(int i=1;i<100;i++) |
947 | { |
996 | { |
948 | layout maze (40, 25); |
997 | layout maze (40, 30); |
949 | maze.fill_rand (85); |
998 | gen_village (maze); |
950 | maze.border (); |
999 | maze.doorify (); |
951 | maze.isolation_remover (); |
|
|
952 | maze.print (); |
1000 | maze.print (); |
|
|
1001 | exit(0); |
953 | } |
1002 | } |
954 | |
1003 | |
955 | exit (1); |
1004 | exit (1); |
956 | } |
1005 | } |
957 | } demo; |
1006 | } demo; |