… | |
… | |
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]; |
… | |
… | |
358 | void |
368 | void |
359 | layout::doorify () |
369 | layout::doorify () |
360 | { |
370 | { |
361 | int ndoors = w * h / 60; /* reasonable number of doors. */ |
371 | int ndoors = w * h / 60; /* reasonable number of doors. */ |
362 | |
372 | |
|
|
373 | coroapi::cede_to_tick (); |
|
|
374 | |
363 | fixed_stack<point> doorloc (w * h); |
375 | fixed_stack<point> doorloc (w * h); |
364 | |
376 | |
365 | /* make a list of possible door locations */ |
377 | /* make a list of possible door locations */ |
366 | for (int i = 1; i < w - 1; i++) |
378 | for (int i = 1; i < w - 1; i++) |
367 | for (int j = 1; j < h - 1; j++) |
379 | for (int j = 1; j < h - 1; j++) |
… | |
… | |
449 | //-GPL |
461 | //-GPL |
450 | |
462 | |
451 | void |
463 | void |
452 | layout::rotate (int rotation) |
464 | layout::rotate (int rotation) |
453 | { |
465 | { |
|
|
466 | coroapi::cede_to_tick (); |
|
|
467 | |
454 | switch (rotation & 3) |
468 | switch (rotation & 3) |
455 | { |
469 | { |
456 | case 2: /* a reflection */ |
470 | case 2: /* a reflection */ |
457 | { |
471 | { |
458 | layout new_layout (w, h); |
472 | layout new_layout (w, h); |
… | |
… | |
610 | { |
624 | { |
611 | layout new_layout (w * 2 - 1, h * 2 - 1); |
625 | layout new_layout (w * 2 - 1, h * 2 - 1); |
612 | |
626 | |
613 | new_layout.clear (); |
627 | new_layout.clear (); |
614 | |
628 | |
|
|
629 | coroapi::cede_to_tick (); |
|
|
630 | |
615 | for (int i = 0; i < w; i++) |
631 | for (int i = 0; i < w; i++) |
616 | for (int j = 0; j < h; j++) |
632 | for (int j = 0; j < h; j++) |
617 | switch (data [i][j]) |
633 | switch (data [i][j]) |
618 | { |
634 | { |
619 | case '#': expand_wall (new_layout, i, j, *this); break; |
635 | case '#': expand_wall (new_layout, i, j, *this); break; |
… | |
… | |
701 | |
717 | |
702 | return -1; |
718 | return -1; |
703 | } |
719 | } |
704 | |
720 | |
705 | int |
721 | int |
706 | make_wall (char **maze, int x, int y, int dir) |
722 | make_wall (layout &maze, int x, int y, int dir) |
707 | { |
723 | { |
708 | maze[x][y] = 'D'; /* mark a door */ |
724 | maze[x][y] = 'D'; /* mark a door */ |
709 | |
725 | |
710 | switch (dir) |
726 | switch (dir) |
711 | { |
727 | { |
… | |
… | |
728 | |
744 | |
729 | void |
745 | void |
730 | layout::roomify () |
746 | layout::roomify () |
731 | { |
747 | { |
732 | int tries = w * h / 30; |
748 | int tries = w * h / 30; |
|
|
749 | |
|
|
750 | coroapi::cede_to_tick (); |
733 | |
751 | |
734 | for (int ti = 0; ti < tries; ti++) |
752 | for (int ti = 0; ti < tries; ti++) |
735 | { |
753 | { |
736 | /* starting location for looking at creating a door */ |
754 | /* starting location for looking at creating a door */ |
737 | int dx = rmg_rndm (w); |
755 | int dx = rmg_rndm (w); |
… | |
… | |
812 | for (int n = w * h / 30 + 1; n--; ) |
830 | for (int n = w * h / 30 + 1; n--; ) |
813 | { |
831 | { |
814 | int rw = rmg_rndm (6, 10); |
832 | int rw = rmg_rndm (6, 10); |
815 | int rh = rmg_rndm (6, 10); |
833 | int rh = rmg_rndm (6, 10); |
816 | |
834 | |
|
|
835 | if (rw > w || rh > h) |
|
|
836 | continue; |
|
|
837 | |
817 | int rx = rmg_rndm (0, w - rw); |
838 | int rx = rmg_rndm (0, w - rw); |
818 | int ry = rmg_rndm (0, h - rh); |
839 | int ry = rmg_rndm (0, h - rh); |
819 | |
840 | |
820 | rect (rx, ry, rx + rw, ry + rh, '#'); |
841 | rect (rx, ry, rx + rw, ry + rh, '#'); |
821 | fill_rect (rx + 1, ry + 1, rx + rw - 1, ry + rh - 1, 0); |
842 | fill_rect (rx + 1, ry + 1, rx + rw - 1, ry + rh - 1, 0); |
… | |
… | |
824 | border (); |
845 | border (); |
825 | isolation_remover (0); |
846 | isolation_remover (0); |
826 | } |
847 | } |
827 | |
848 | |
828 | static void |
849 | static void |
829 | gen_mixed_ (layout &maze, random_map_params *RP, int dir) |
850 | gen_mixed_ (layout &maze, random_map_params *RP) |
830 | { |
851 | { |
831 | if (maze.w < 20 && maze.h < 20 && !rmg_rndm (3)) |
852 | if (maze.w > maze.h && maze.w > 16) |
832 | dir = 2; // stop recursion randomly |
|
|
833 | |
|
|
834 | if (dir == 0 && maze.w > 16) |
|
|
835 | { |
853 | { |
836 | int m = rmg_rndm (8, maze.w - 8); |
854 | int m = rmg_rndm (8, maze.w - 8); |
837 | |
855 | |
838 | layout m1 (maze, 0, 0, m , maze.h); gen_mixed_ (m1, RP, !dir); |
856 | layout m1 (maze, 0, 0, m , maze.h); gen_mixed_ (m1, RP); |
839 | layout m2 (maze, m, 0, maze.w, maze.h); gen_mixed_ (m2, RP, !dir); |
857 | layout m2 (maze, m, 0, maze.w, maze.h); gen_mixed_ (m2, RP); |
840 | } |
858 | } |
841 | else if (dir == 1 && maze.h > 16) |
859 | else if (maze.h > 16) |
842 | { |
860 | { |
843 | int m = rmg_rndm (8, maze.h - 8); |
861 | int m = rmg_rndm (8, maze.h - 8); |
844 | |
862 | |
845 | layout m1 (maze, 0, 0, maze.w, m ); gen_mixed_ (m1, RP, !dir); |
863 | layout m1 (maze, 0, 0, maze.w, m ); gen_mixed_ (m1, RP); |
846 | layout m2 (maze, 0, m, maze.w, maze.h); gen_mixed_ (m2, RP, !dir); |
864 | layout m2 (maze, 0, m, maze.w, maze.h); gen_mixed_ (m2, RP); |
847 | } |
865 | } |
848 | else |
866 | else |
849 | { |
867 | { |
850 | RP->map_layout_style = rmg_rndm (NROFLAYOUTS - 2) + 1; |
868 | RP->map_layout_style = rmg_rndm (NROFLAYOUTS - 2) + 1; |
851 | |
869 | |
852 | if (RP->map_layout_style == LAYOUT_MULTIPLE) |
870 | if (RP->map_layout_style == LAYOUT_MULTIPLE) |
853 | ++RP->map_layout_style; |
871 | ++RP->map_layout_style; |
854 | |
872 | |
855 | maze.generate (RP); |
873 | maze.generate (RP); |
856 | } |
874 | } |
|
|
875 | |
|
|
876 | coroapi::cede_to_tick (); |
857 | } |
877 | } |
858 | |
878 | |
859 | // recursive subdivision with random sublayouts |
879 | // recursive subdivision with random sublayouts |
860 | static void |
880 | static void |
861 | gen_mixed (layout &maze, random_map_params *RP) |
881 | gen_mixed (layout &maze, random_map_params *RP) |
862 | { |
882 | { |
863 | random_map_params &rp = *new random_map_params (RP); |
883 | random_map_params &rp = *new random_map_params (RP); |
864 | gen_mixed_ (maze, &rp, rmg_rndm (2)); |
884 | gen_mixed_ (maze, &rp); |
865 | delete &rp; |
885 | delete &rp; |
866 | |
886 | |
867 | maze.border (); |
887 | maze.border (); |
|
|
888 | |
|
|
889 | // exits currently do not work so well, as they |
|
|
890 | // are currently often found together, so nuke entrances |
|
|
891 | maze.replace ('<', ' '); |
|
|
892 | |
868 | maze.isolation_remover (0); |
893 | maze.isolation_remover (0); |
869 | } |
894 | } |
870 | |
895 | |
871 | //+GPL |
896 | //+GPL |
872 | |
897 | |