… | |
… | |
23 | |
23 | |
24 | #include <global.h> |
24 | #include <global.h> |
25 | #include <random_map.h> |
25 | #include <random_map.h> |
26 | #include <rproto.h> |
26 | #include <rproto.h> |
27 | |
27 | |
28 | void |
28 | void noinline |
29 | layout::alloc (int w, int h) |
29 | layout::alloc (int w, int h) |
30 | { |
30 | { |
31 | assert (sizeof (cell) == 1); |
31 | assert (sizeof (cell) == 1); |
32 | |
32 | |
33 | this->w = w; |
33 | this->w = w; |
… | |
… | |
74 | layout::~layout () |
74 | layout::~layout () |
75 | { |
75 | { |
76 | sfree ((char *)data, size); |
76 | sfree ((char *)data, size); |
77 | } |
77 | } |
78 | |
78 | |
79 | void |
79 | void noinline |
80 | layout::fill (char fill) |
80 | layout::fill (char fill) |
81 | { |
81 | { |
82 | //memset (data [0], fill, w * h); // only when contiguous :/ |
82 | //memset (data [0], fill, w * h); // only when contiguous :/ |
83 | fill_rect (0, 0, w, h, fill); |
83 | fill_rect (0, 0, w, h, fill); |
84 | } |
84 | } |
85 | |
85 | |
86 | void |
86 | void noinline |
|
|
87 | layout::replace (char from, char to) |
|
|
88 | { |
|
|
89 | for (int x = 0; x < w; ++x) |
|
|
90 | for (int y = 0; y < h; ++y) |
|
|
91 | if (data [x][y] == from) |
|
|
92 | data [x][y] = to; |
|
|
93 | } |
|
|
94 | |
|
|
95 | void noinline |
87 | layout::rect (int x1, int y1, int x2, int y2, char fill) |
96 | layout::rect (int x1, int y1, int x2, int y2, char fill) |
88 | { |
97 | { |
89 | --x2; |
98 | --x2; |
90 | |
99 | |
91 | memset (data [x1] + y1, fill, y2 - y1); |
100 | memset (data [x1] + y1, fill, y2 - y1); |
… | |
… | |
93 | |
102 | |
94 | while (++x1 < x2) |
103 | while (++x1 < x2) |
95 | data [x1][y1] = data [x1][y2 - 1] = fill; |
104 | data [x1][y1] = data [x1][y2 - 1] = fill; |
96 | } |
105 | } |
97 | |
106 | |
98 | void |
107 | void noinline |
99 | layout::fill_rect (int x1, int y1, int x2, int y2, char fill) |
108 | layout::fill_rect (int x1, int y1, int x2, int y2, char fill) |
100 | { |
109 | { |
101 | for (; x1 < x2; ++x1) |
110 | for (; x1 < x2; ++x1) |
102 | memset (data [x1] + y1, fill, y2 - y1); |
111 | memset (data [x1] + y1, fill, y2 - y1); |
103 | } |
112 | } |
104 | |
113 | |
|
|
114 | void |
105 | void layout::border (char fill) |
115 | layout::border (char fill) |
106 | { |
116 | { |
107 | rect (0, 0, w, h, fill); |
117 | rect (0, 0, w, h, fill); |
108 | } |
118 | } |
109 | |
119 | |
110 | void |
120 | void noinline |
111 | layout::fill_rand (int percent) |
121 | layout::fill_rand (int percent) |
112 | { |
122 | { |
113 | percent = lerp (percent, 0, 100, 0, 256); |
123 | percent = lerp (percent, 0, 100, 0, 256); |
114 | |
124 | |
115 | for (int x = w - 1; --x > 0; ) |
125 | for (int x = 0; x < w; ++x) |
116 | for (int y = h - 1; --y > 0; ) |
126 | for (int y = 0; y < h; ++y) |
117 | data [x][y] = rmg_rndm (256) > percent ? 0 : '#'; |
127 | data [x][y] = rmg_rndm (256) > percent ? 0 : '#'; |
118 | } |
128 | } |
119 | |
129 | |
120 | ///////////////////////////////////////////////////////////////////////////// |
130 | ///////////////////////////////////////////////////////////////////////////// |
121 | |
131 | |
122 | // erode by cellular automata |
132 | // erode by cellular automata |
123 | void |
133 | void noinline |
124 | layout::erode_1_2 (int c1, int c2, int repeat) |
134 | layout::erode_1_2 (int c1, int c2, int repeat) |
125 | { |
135 | { |
126 | layout neu (w, h); |
136 | layout neu (w, h); |
127 | |
137 | |
128 | while (repeat--) |
138 | while (repeat--) |
… | |
… | |
220 | if (x > 0 && !dist [x - 1][y]) push_flood_fill (dist, seeds, x - 1, y); |
230 | if (x > 0 && !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); |
231 | if (x < dist.w - 1 && !dist [x + 1][y]) push_flood_fill (dist, seeds, x + 1, y); |
222 | } |
232 | } |
223 | } |
233 | } |
224 | |
234 | |
225 | static inline void |
235 | static void inline |
226 | make_tunnel (layout &dist, pointlist &seeds, int x, int y, U8 d, int perturb) |
236 | make_tunnel (layout &dist, pointlist &seeds, int x, int y, U8 d, int perturb) |
227 | { |
237 | { |
228 | for (;;) |
238 | for (;;) |
229 | { |
239 | { |
230 | point neigh[4]; |
240 | point neigh[4]; |
… | |
… | |
238 | if (y < dist.h - 1 && U8 (dist [x][y + 1]) < d && dist [x][y + 1] > 1) neigh [ncnt++] = point (x, y + 1); |
248 | if (y < dist.h - 1 && U8 (dist [x][y + 1]) < d && dist [x][y + 1] > 1) neigh [ncnt++] = point (x, y + 1); |
239 | |
249 | |
240 | if (!ncnt) |
250 | if (!ncnt) |
241 | return; |
251 | return; |
242 | |
252 | |
243 | point &p = neigh [perturb ? rmg_rndm (ncnt) : 0]; |
253 | point p = neigh [perturb ? rmg_rndm (ncnt) : 0]; |
244 | |
254 | |
245 | seeds.push (p); |
255 | seeds.push (p); |
246 | |
256 | |
247 | x = p.x; |
257 | x = p.x; |
248 | y = p.y; |
258 | y = p.y; |
… | |
… | |
273 | // dist contains |
283 | // dist contains |
274 | // 0 == invisited rooms |
284 | // 0 == invisited rooms |
275 | // 1 == visited rooms |
285 | // 1 == visited rooms |
276 | // 2+ shortest distance to random near room |
286 | // 2+ shortest distance to random near room |
277 | |
287 | |
278 | max_it (perturb, 0); |
288 | clamp_it (perturb, 0, 2); |
279 | min_it (perturb, 2); |
|
|
280 | |
289 | |
281 | // phase 1, find seed |
290 | // phase 1, find seed |
282 | int cnt = 0; |
291 | int cnt = 0; |
283 | int x, y; |
292 | int x, y; |
284 | |
293 | |
… | |
… | |
303 | seeds.push (point (x, y)); |
312 | seeds.push (point (x, y)); |
304 | |
313 | |
305 | // phase 2, while we have seeds, if |
314 | // phase 2, while we have seeds, if |
306 | // seed is empty, floodfill, else grow |
315 | // seed is empty, floodfill, else grow |
307 | |
316 | |
|
|
317 | int rem_index = 0; // used to remove "somewhat ordered" |
|
|
318 | |
308 | while (seeds.size) |
319 | while (seeds.size) |
309 | { |
320 | { |
310 | coroapi::cede_to_tick (); |
321 | coroapi::cede_to_tick (); |
311 | |
322 | |
|
|
323 | int i = perturb |
|
|
324 | ? rmg_rndm (max (0, seeds.size - 8), seeds.size - 1) |
|
|
325 | : rem_index ++ % seeds.size; |
|
|
326 | |
312 | point p = seeds.remove (rmg_rndm (seeds.size)); |
327 | point p = seeds.remove (i); |
313 | |
328 | |
314 | x = p.x; |
329 | x = p.x; |
315 | y = p.y; |
330 | y = p.y; |
316 | |
331 | |
317 | if (!dist [x][y]) |
332 | if (!dist [x][y]) |
… | |
… | |
358 | /* puts doors at appropriate locations in a maze. */ |
373 | /* puts doors at appropriate locations in a maze. */ |
359 | void |
374 | void |
360 | layout::doorify () |
375 | layout::doorify () |
361 | { |
376 | { |
362 | int ndoors = w * h / 60; /* reasonable number of doors. */ |
377 | int ndoors = w * h / 60; /* reasonable number of doors. */ |
363 | int doorlocs = 0; /* # of available doorlocations */ |
|
|
364 | |
378 | |
365 | uint16 *doorlist_x = salloc<uint16> (w * h); |
379 | coroapi::cede_to_tick (); |
366 | uint16 *doorlist_y = salloc<uint16> (w * h); |
380 | |
|
|
381 | fixed_stack<point> doorloc (w * h); |
367 | |
382 | |
368 | /* make a list of possible door locations */ |
383 | /* make a list of possible door locations */ |
369 | for (int i = 1; i < w - 1; i++) |
384 | for (int i = 1; i < w - 1; i++) |
370 | for (int j = 1; j < h - 1; j++) |
385 | for (int j = 1; j < h - 1; j++) |
371 | { |
386 | { |
372 | int sindex = surround_flag (*this, i, j); |
387 | int sindex = surround_flag (*this, i, j); |
373 | |
388 | |
374 | if (sindex == 3 || sindex == 12) /* these are possible door sindex */ |
389 | if (sindex == 3 || sindex == 12) /* these are possible door sindex */ |
375 | { |
390 | doorloc.push (point (i, j)); |
376 | doorlist_x [doorlocs] = i; |
|
|
377 | doorlist_y [doorlocs] = j; |
|
|
378 | doorlocs++; |
|
|
379 | } |
|
|
380 | } |
391 | } |
381 | |
392 | |
382 | while (ndoors > 0 && doorlocs > 0) |
393 | while (ndoors && doorloc.size) |
383 | { |
394 | { |
384 | int di = rmg_rndm (doorlocs); |
395 | point p = doorloc.remove (rmg_rndm (doorloc.size)); |
385 | int i = doorlist_x [di]; |
396 | |
386 | int j = doorlist_y [di]; |
|
|
387 | int sindex = surround_flag (*this, i, j); |
397 | int sindex = surround_flag (*this, p.x, p.y); |
388 | |
398 | |
389 | if (sindex == 3 || sindex == 12) /* these are possible door sindex */ |
399 | if (sindex == 3 || sindex == 12) /* these are possible door sindex */ |
390 | { |
400 | { |
391 | data [i][j] = 'D'; |
401 | data [p.x][p.y] = 'D'; |
392 | ndoors--; |
402 | --ndoors; |
393 | } |
403 | } |
394 | |
|
|
395 | /* reduce the size of the list */ |
|
|
396 | doorlocs--; |
|
|
397 | doorlist_x[di] = doorlist_x [doorlocs]; |
|
|
398 | doorlist_y[di] = doorlist_y [doorlocs]; |
|
|
399 | } |
404 | } |
400 | |
|
|
401 | sfree (doorlist_x, w * h); |
|
|
402 | sfree (doorlist_y, w * h); |
|
|
403 | } |
405 | } |
404 | |
406 | |
405 | /* takes a map and makes it symmetric: adjusts Xsize and |
407 | /* takes a map and makes it symmetric: adjusts Xsize and |
406 | * Ysize to produce a symmetric map. |
408 | * Ysize to produce a symmetric map. |
407 | */ |
409 | */ |
… | |
… | |
465 | //-GPL |
467 | //-GPL |
466 | |
468 | |
467 | void |
469 | void |
468 | layout::rotate (int rotation) |
470 | layout::rotate (int rotation) |
469 | { |
471 | { |
|
|
472 | coroapi::cede_to_tick (); |
|
|
473 | |
470 | switch (rotation & 3) |
474 | switch (rotation & 3) |
471 | { |
475 | { |
472 | case 2: /* a reflection */ |
476 | case 2: /* a reflection */ |
473 | { |
477 | { |
474 | layout new_layout (w, h); |
478 | layout new_layout (w, h); |
… | |
… | |
626 | { |
630 | { |
627 | layout new_layout (w * 2 - 1, h * 2 - 1); |
631 | layout new_layout (w * 2 - 1, h * 2 - 1); |
628 | |
632 | |
629 | new_layout.clear (); |
633 | new_layout.clear (); |
630 | |
634 | |
|
|
635 | coroapi::cede_to_tick (); |
|
|
636 | |
631 | for (int i = 0; i < w; i++) |
637 | for (int i = 0; i < w; i++) |
632 | for (int j = 0; j < h; j++) |
638 | for (int j = 0; j < h; j++) |
633 | switch (data [i][j]) |
639 | switch (data [i][j]) |
634 | { |
640 | { |
635 | case '#': expand_wall (new_layout, i, j, *this); break; |
641 | case '#': expand_wall (new_layout, i, j, *this); break; |
… | |
… | |
717 | |
723 | |
718 | return -1; |
724 | return -1; |
719 | } |
725 | } |
720 | |
726 | |
721 | int |
727 | int |
722 | make_wall (char **maze, int x, int y, int dir) |
728 | make_wall (layout &maze, int x, int y, int dir) |
723 | { |
729 | { |
724 | maze[x][y] = 'D'; /* mark a door */ |
730 | maze[x][y] = 'D'; /* mark a door */ |
725 | |
731 | |
726 | switch (dir) |
732 | switch (dir) |
727 | { |
733 | { |
… | |
… | |
744 | |
750 | |
745 | void |
751 | void |
746 | layout::roomify () |
752 | layout::roomify () |
747 | { |
753 | { |
748 | int tries = w * h / 30; |
754 | int tries = w * h / 30; |
|
|
755 | |
|
|
756 | coroapi::cede_to_tick (); |
749 | |
757 | |
750 | for (int ti = 0; ti < tries; ti++) |
758 | for (int ti = 0; ti < tries; ti++) |
751 | { |
759 | { |
752 | /* starting location for looking at creating a door */ |
760 | /* starting location for looking at creating a door */ |
753 | int dx = rmg_rndm (w); |
761 | int dx = rmg_rndm (w); |
… | |
… | |
775 | make_wall (*this, dx, dy, 0); |
783 | make_wall (*this, dx, dy, 0); |
776 | else |
784 | else |
777 | make_wall (*this, dx, dy, 1); |
785 | make_wall (*this, dx, dy, 1); |
778 | } |
786 | } |
779 | } |
787 | } |
|
|
788 | |
|
|
789 | //-GPL |
780 | |
790 | |
781 | ///////////////////////////////////////////////////////////////////////////// |
791 | ///////////////////////////////////////////////////////////////////////////// |
782 | |
792 | |
783 | // inspired mostly by http://www.jimrandomh.org/misc/caves.txt |
793 | // inspired mostly by http://www.jimrandomh.org/misc/caves.txt |
784 | void |
794 | void |
… | |
… | |
797 | erode_1_2 (5, 2, 10); |
807 | erode_1_2 (5, 2, 10); |
798 | erode_1_2 (5, -1, 10); |
808 | erode_1_2 (5, -1, 10); |
799 | erode_1_2 (5, 2, 1); |
809 | erode_1_2 (5, 2, 1); |
800 | break; |
810 | break; |
801 | |
811 | |
802 | // somewhat open, roundish |
812 | // somewhat open, some room-like structures |
803 | case 2: |
813 | case 2: |
|
|
814 | fill_rand (45); |
|
|
815 | erode_1_2 (5, 2, 4); |
|
|
816 | erode_1_2 (5, -1, 3); |
|
|
817 | break; |
|
|
818 | |
|
|
819 | // wide open, roundish |
|
|
820 | case 3: |
804 | fill_rand (45); |
821 | fill_rand (45); |
805 | erode_1_2 (5, 0, 5); |
822 | erode_1_2 (5, 0, 5); |
806 | erode_1_2 (5, 1, 1); |
823 | erode_1_2 (5, 1, 1); |
807 | break; |
824 | break; |
808 | |
|
|
809 | // wide open, some room-like structures |
|
|
810 | case 3: |
|
|
811 | fill_rand (45); |
|
|
812 | erode_1_2 (5, 2, 4); |
|
|
813 | erode_1_2 (5, -1, 3); |
|
|
814 | break; |
|
|
815 | } |
825 | } |
816 | |
826 | |
817 | border (); |
827 | border (); |
818 | isolation_remover (); |
828 | isolation_remover (1); |
819 | } |
829 | } |
820 | |
830 | |
821 | void |
831 | void |
822 | layout::gen_castle () |
832 | layout::gen_castle () |
823 | { |
833 | { |
… | |
… | |
826 | for (int n = w * h / 30 + 1; n--; ) |
836 | for (int n = w * h / 30 + 1; n--; ) |
827 | { |
837 | { |
828 | int rw = rmg_rndm (6, 10); |
838 | int rw = rmg_rndm (6, 10); |
829 | int rh = rmg_rndm (6, 10); |
839 | int rh = rmg_rndm (6, 10); |
830 | |
840 | |
|
|
841 | if (rw > w || rh > h) |
|
|
842 | continue; |
|
|
843 | |
831 | int rx = rmg_rndm (0, w - rw); |
844 | int rx = rmg_rndm (0, w - rw); |
832 | int ry = rmg_rndm (0, h - rh); |
845 | int ry = rmg_rndm (0, h - rh); |
833 | |
846 | |
834 | rect (rx, ry, rx + rw, ry + rh, '#'); |
847 | rect (rx, ry, rx + rw, ry + rh, '#'); |
835 | fill_rect (rx + 1, ry + 1, rx + rw - 1, ry + rh - 1, 0); |
848 | fill_rect (rx + 1, ry + 1, rx + rw - 1, ry + rh - 1, 0); |
… | |
… | |
838 | border (); |
851 | border (); |
839 | isolation_remover (0); |
852 | isolation_remover (0); |
840 | } |
853 | } |
841 | |
854 | |
842 | static void |
855 | static void |
843 | gen_mixed_ (layout &maze, random_map_params *RP, int dir) |
856 | gen_mixed_ (layout &maze, random_map_params *RP) |
844 | { |
857 | { |
845 | if (maze.w < 20 && maze.h < 20 && !rmg_rndm (3)) |
858 | if (maze.w > maze.h && maze.w > 16) |
846 | dir = 2; // stop recursion randomly |
|
|
847 | |
|
|
848 | if (dir == 0 && maze.w > 16) |
|
|
849 | { |
859 | { |
850 | int m = rmg_rndm (8, maze.w - 8); |
860 | int m = rmg_rndm (8, maze.w - 8); |
851 | |
861 | |
852 | layout m1 (maze, 0, 0, m , maze.h); gen_mixed_ (m1, RP, !dir); |
862 | layout m1 (maze, 0, 0, m , maze.h); gen_mixed_ (m1, RP); |
853 | layout m2 (maze, m, 0, maze.w, maze.h); gen_mixed_ (m2, RP, !dir); |
863 | layout m2 (maze, m, 0, maze.w, maze.h); gen_mixed_ (m2, RP); |
854 | } |
864 | } |
855 | else if (dir == 1 && maze.h > 16) |
865 | else if (maze.h > 16) |
856 | { |
866 | { |
857 | int m = rmg_rndm (8, maze.h - 8); |
867 | int m = rmg_rndm (8, maze.h - 8); |
858 | |
868 | |
859 | layout m1 (maze, 0, 0, maze.w, m ); gen_mixed_ (m1, RP, !dir); |
869 | layout m1 (maze, 0, 0, maze.w, m ); gen_mixed_ (m1, RP); |
860 | layout m2 (maze, 0, m, maze.w, maze.h); gen_mixed_ (m2, RP, !dir); |
870 | layout m2 (maze, 0, m, maze.w, maze.h); gen_mixed_ (m2, RP); |
861 | } |
871 | } |
862 | else |
872 | else |
863 | { |
873 | { |
864 | RP->map_layout_style = rmg_rndm (NROFLAYOUTS - 2) + 1; |
874 | RP->map_layout_style = rmg_rndm (NROFLAYOUTS - 2) + 1; |
865 | |
875 | |
866 | if (RP->map_layout_style == LAYOUT_MULTIPLE) |
876 | if (RP->map_layout_style == LAYOUT_MULTIPLE) |
867 | ++RP->map_layout_style; |
877 | ++RP->map_layout_style; |
868 | |
878 | |
869 | maze.generate (RP); |
879 | maze.generate (RP); |
870 | } |
880 | } |
|
|
881 | |
|
|
882 | coroapi::cede_to_tick (); |
871 | } |
883 | } |
872 | |
884 | |
873 | // recursive subdivision with random sublayouts |
885 | // recursive subdivision with random sublayouts |
874 | static void |
886 | static void |
875 | gen_mixed (layout &maze, random_map_params *RP) |
887 | gen_mixed (layout &maze, random_map_params *RP) |
876 | { |
888 | { |
877 | random_map_params &rp = *new random_map_params (RP); |
889 | random_map_params &rp = *new random_map_params (RP); |
878 | gen_mixed_ (maze, &rp, rmg_rndm (2)); |
890 | gen_mixed_ (maze, &rp); |
879 | delete &rp; |
891 | delete &rp; |
880 | |
892 | |
881 | maze.border (); |
893 | maze.border (); |
|
|
894 | |
|
|
895 | // exits currently do not work so well, as they |
|
|
896 | // are currently often found together, so nuke entrances |
|
|
897 | maze.replace ('<', ' '); |
|
|
898 | |
882 | maze.isolation_remover (); |
899 | maze.isolation_remover (0); |
883 | } |
900 | } |
|
|
901 | |
|
|
902 | //+GPL |
884 | |
903 | |
885 | /* function selects the maze function and gives it whatever |
904 | /* function selects the maze function and gives it whatever |
886 | arguments it needs. */ |
905 | arguments it needs. */ |
887 | void |
906 | void |
888 | layout::generate (random_map_params *RP) |
907 | layout::generate (random_map_params *RP) |
… | |
… | |
968 | } |
987 | } |
969 | |
988 | |
970 | //-GPL |
989 | //-GPL |
971 | |
990 | |
972 | #if 0 |
991 | #if 0 |
|
|
992 | static void |
|
|
993 | gen_village (layout &maze) |
|
|
994 | { |
|
|
995 | maze.clear (); |
|
|
996 | maze.border (); |
|
|
997 | |
|
|
998 | for (int n = maze.w * maze.h / 200 + 1; n--; ) |
|
|
999 | { |
|
|
1000 | int rw = rmg_rndm (6, 10); |
|
|
1001 | int rh = rmg_rndm (6, 10); |
|
|
1002 | |
|
|
1003 | int rx = rmg_rndm (2, maze.w - rw - 2); |
|
|
1004 | int ry = rmg_rndm (2, maze.h - rh - 2); |
|
|
1005 | |
|
|
1006 | maze.rect (rx, ry, rx + rw, ry + rh, '#'); |
|
|
1007 | } |
|
|
1008 | |
|
|
1009 | maze.border (); |
|
|
1010 | maze.isolation_remover (2); |
|
|
1011 | } |
|
|
1012 | |
973 | static struct demo |
1013 | static struct demo |
974 | { |
1014 | { |
975 | demo () |
1015 | demo () |
976 | { |
1016 | { |
977 | rmg_rndm.seed (time (0)); |
1017 | rmg_rndm.seed (time (0)); |
978 | |
1018 | |
979 | for(int i=1;i<100;i++) |
1019 | for(int i=1;i<100;i++) |
980 | { |
1020 | { |
981 | layout maze (40, 25); |
1021 | layout maze (40, 30); |
982 | maze.gen_castle (); |
1022 | maze.fill_rand (99); |
983 | maze.doorify (); |
1023 | maze.border (); |
|
|
1024 | maze.isolation_remover (2); |
984 | maze.print (); |
1025 | maze.print (); |
985 | exit(0); |
|
|
986 | } |
1026 | } |
987 | |
1027 | |
988 | exit (1); |
1028 | exit (1); |
989 | } |
1029 | } |
990 | } demo; |
1030 | } demo; |