… | |
… | |
34 | this->h = h; |
34 | this->h = h; |
35 | |
35 | |
36 | // we store the layout in a single contiguous memory layout |
36 | // we store the layout in a single contiguous memory layout |
37 | // first part consists of pointers to each column, followed |
37 | // first part consists of pointers to each column, followed |
38 | // by the actual columns (not rows!) |
38 | // by the actual columns (not rows!) |
39 | int size = (sizeof (cell *) + sizeof (cell) * h) * w; |
39 | size = (sizeof (cell *) + sizeof (cell) * h) * w; |
40 | |
|
|
41 | data = (cell **)salloc<char> (size); |
40 | data = (cell **)salloc<char> (size); |
42 | |
41 | |
43 | cell *p = (cell *)(data + w); |
42 | cell *p = (cell *)(data + w); |
44 | |
43 | |
45 | for (int x = w; x--; ) |
44 | for (int x = 0; x < w; ++x) |
46 | data [x] = p + x * h; |
45 | data [x] = p + x * h; |
47 | } |
46 | } |
48 | |
47 | |
49 | layout::layout (int w, int h) |
48 | layout::layout (int w, int h) |
50 | { |
49 | { |
… | |
… | |
56 | alloc (copy.w, copy.h); |
55 | alloc (copy.w, copy.h); |
57 | |
56 | |
58 | memcpy (data [0], copy.data [0], sizeof (cell) * h * w); |
57 | memcpy (data [0], copy.data [0], sizeof (cell) * h * w); |
59 | } |
58 | } |
60 | |
59 | |
|
|
60 | layout::layout (layout &orig, int x1, int y1, int x2, int y2) |
|
|
61 | { |
|
|
62 | w = x2 - x1; |
|
|
63 | h = y2 - y1; |
|
|
64 | |
|
|
65 | // we only allocate space for the pointers |
|
|
66 | size = sizeof (cell *) * w; |
|
|
67 | data = (cell **)salloc<char> (size); |
|
|
68 | |
|
|
69 | // and now we point back into the original layout |
|
|
70 | for (int x = 0; x < w; ++x) |
|
|
71 | data [x] = orig.data [x + x1] + y1; |
|
|
72 | } |
|
|
73 | |
61 | layout::~layout () |
74 | layout::~layout () |
62 | { |
75 | { |
63 | int size = (sizeof (cell *) + sizeof (cell) * h) * w; |
|
|
64 | |
|
|
65 | sfree ((char *)data, size); |
76 | sfree ((char *)data, size); |
66 | } |
77 | } |
67 | |
78 | |
68 | void |
79 | void |
69 | layout::fill (char fill) |
80 | layout::fill (char fill) |
70 | { |
81 | { |
71 | memset (data [0], fill, w * h); |
82 | //memset (data [0], fill, w * h); // only when contiguous :/ |
|
|
83 | fill_rect (0, 0, w, h, fill); |
72 | } |
84 | } |
73 | |
85 | |
74 | void |
86 | void |
75 | layout::rect (int x1, int y1, int x2, int y2, char fill) |
87 | layout::rect (int x1, int y1, int x2, int y2, char fill) |
|
|
88 | { |
|
|
89 | --x2; |
|
|
90 | |
|
|
91 | memset (data [x1] + y1, fill, y2 - y1); |
|
|
92 | memset (data [x2] + y1, fill, y2 - y1); |
|
|
93 | |
|
|
94 | while (++x1 < x2) |
|
|
95 | data [x1][y1] = data [x1][y2 - 1] = fill; |
|
|
96 | } |
|
|
97 | |
|
|
98 | void |
|
|
99 | layout::fill_rect (int x1, int y1, int x2, int y2, char fill) |
76 | { |
100 | { |
77 | for (; x1 < x2; ++x1) |
101 | for (; x1 < x2; ++x1) |
78 | memset (data [x1] + y1, fill, y2 - y1); |
102 | memset (data [x1] + y1, fill, y2 - y1); |
79 | } |
103 | } |
80 | |
104 | |
81 | void layout::border (char fill) |
105 | void layout::border (char fill) |
82 | { |
106 | { |
83 | for (int i = 0; i < w; i++) data [i][0] = data [i][h - 1] = fill; |
107 | rect (0, 0, w, h, fill); |
84 | for (int j = 0; j < h; j++) data [0][j] = data [w - 1][j] = fill; |
|
|
85 | } |
108 | } |
86 | |
109 | |
87 | void |
110 | void |
88 | layout::fill_rand (int percent) |
111 | layout::fill_rand (int percent) |
89 | { |
112 | { |
… | |
… | |
171 | ///////////////////////////////////////////////////////////////////////////// |
194 | ///////////////////////////////////////////////////////////////////////////// |
172 | // isolation remover - ensures single connected area |
195 | // isolation remover - ensures single connected area |
173 | |
196 | |
174 | typedef fixed_stack<point> pointlist; |
197 | typedef fixed_stack<point> pointlist; |
175 | |
198 | |
176 | static noinline void |
199 | static void noinline |
177 | push_flood_fill (layout &dist, pointlist &seeds, int x, int y) |
200 | push_flood_fill (layout &dist, pointlist &seeds, int x, int y) |
178 | { |
201 | { |
179 | if (dist [x][y]) |
202 | if (dist [x][y]) |
180 | return; |
203 | return; |
181 | |
204 | |
… | |
… | |
192 | ++y; |
215 | ++y; |
193 | } |
216 | } |
194 | |
217 | |
195 | while (--y >= y0) |
218 | while (--y >= y0) |
196 | { |
219 | { |
197 | if (x > 0) push_flood_fill (dist, seeds, x - 1, y); |
220 | if (x > 0 && !dist [x - 1][y]) push_flood_fill (dist, seeds, x - 1, y); |
198 | if (x < dist.w - 1) 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); |
199 | } |
222 | } |
200 | } |
223 | } |
201 | |
224 | |
202 | static inline void |
225 | static inline void |
203 | 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) |
204 | { |
227 | { |
205 | for (;;) |
228 | for (;;) |
206 | { |
229 | { |
|
|
230 | point neigh[4]; |
|
|
231 | int ncnt = 0; |
|
|
232 | |
207 | if (x > 1 && U8 (dist [x - 1][y]) < d && dist [x - 1][y] > 1) |
233 | if (x > 0 && U8 (dist [x - 1][y]) <= d && dist [x - 1][y] > 1) neigh [ncnt++] = point (x - 1, y); |
208 | --x; |
|
|
209 | else if (x < dist.w - 2 && U8 (dist [x + 1][y]) < d && dist [x + 1][y] > 1) |
234 | if (x < dist.w - 1 && U8 (dist [x + 1][y]) <= d && dist [x + 1][y] > 1) neigh [ncnt++] = point (x + 1, y); |
210 | ++x; |
|
|
211 | else if (y > 1 && U8 (dist [x][y - 1]) < d && dist [x][y - 1] > 1) |
235 | if (y > 0 && U8 (dist [x][y - 1]) <= d && dist [x][y - 1] > 1) neigh [ncnt++] = point (x, y - 1); |
212 | --y; |
|
|
213 | else if (y < dist.h - 2 && U8 (dist [x][y + 1]) < d && dist [x][y + 1] > 1) |
236 | if (y < dist.h - 1 && U8 (dist [x][y + 1]) <= d && dist [x][y + 1] > 1) neigh [ncnt++] = point (x, y + 1); |
214 | ++y; |
237 | |
215 | else |
238 | if (!ncnt) |
216 | break; |
239 | return; |
|
|
240 | |
|
|
241 | point &p = neigh [rmg_rndm (ncnt)]; |
|
|
242 | |
|
|
243 | seeds.push (p); |
|
|
244 | |
|
|
245 | x = p.x; |
|
|
246 | y = p.y; |
217 | |
247 | |
218 | d = dist [x][y]; |
248 | d = dist [x][y]; |
219 | dist [x][y] = 1; |
249 | dist [x][y] = 1; |
220 | seeds.push (point (x, y)); |
|
|
221 | } |
250 | } |
222 | } |
251 | } |
223 | |
252 | |
224 | static void inline |
253 | static void inline |
225 | maybe_push (layout &dist, pointlist &seeds, int x, int y, U8 d) |
254 | maybe_push (layout &dist, pointlist &seeds, int x, int y, U8 d) |
… | |
… | |
232 | return; |
261 | return; |
233 | |
262 | |
234 | seeds.push (point (x, y)); |
263 | seeds.push (point (x, y)); |
235 | } |
264 | } |
236 | |
265 | |
237 | void |
266 | // isolation remover, works on a "distance" map |
238 | layout::isolation_remover (bool dirty) |
267 | // the map must be initialised with 0 == rooms, 255 = walls |
|
|
268 | static void noinline |
|
|
269 | isolation_remover (layout &dist) |
239 | { |
270 | { |
240 | layout dist (w, h); |
|
|
241 | |
|
|
242 | // dist contains |
271 | // dist contains |
243 | // 0 == invisited rooms |
272 | // 0 == invisited rooms |
244 | // 1 == visited rooms |
273 | // 1 == visited rooms |
245 | // 2+ shortest distance to random near room |
274 | // 2+ shortest distance to random near room |
246 | |
275 | |
247 | // phase 1, initialise dist array, find seed |
276 | // phase 1, find seed |
248 | int cnt = 0; |
277 | int cnt = 0; |
249 | int x, y; |
278 | int x, y; |
250 | |
279 | |
251 | for (int i = 0; i < w; ++i) |
280 | for (int i = 0; i < dist.w; ++i) |
252 | for (int j = 0; j < h; ++j) |
281 | for (int j = 0; j < dist.h; ++j) |
253 | { |
282 | if (!dist [i][j] && !rmg_rndm (++cnt)) |
254 | if (data [i][j] == '#') |
|
|
255 | dist [i][j] = U8 (255); |
|
|
256 | else |
|
|
257 | { |
|
|
258 | dist [i][j] = 0; |
|
|
259 | if (!rmg_rndm (++cnt)) |
|
|
260 | x = i, y = j; |
283 | x = i, y = j; |
261 | } |
|
|
262 | } |
|
|
263 | |
284 | |
264 | if (!cnt) |
285 | if (!cnt) |
265 | { |
286 | { |
266 | // map is completely massive, this is not good, |
287 | // map is completely massive, this is not good, |
267 | // so make it empty instead. |
288 | // so make it empty instead. |
268 | clear (); |
289 | dist.fill (1); |
269 | border (); |
|
|
270 | return; |
290 | return; |
271 | } |
291 | } |
272 | |
292 | |
273 | fixed_stack<point> seeds (w * h * 5); |
293 | fixed_stack<point> seeds (dist.w * dist.h * 5); |
274 | |
294 | |
275 | // found first free space - picking the first one gives |
295 | // found first free space - picking the first one gives |
276 | // us a slight bias for tunnels, but usually you won't |
296 | // us a slight bias for tunnels, but usually you won't |
277 | // notice that in-game |
297 | // notice that in-game |
278 | seeds.push (point (x, y)); |
298 | seeds.push (point (x, y)); |
… | |
… | |
290 | y = p.y; |
310 | y = p.y; |
291 | |
311 | |
292 | if (!dist [x][y]) |
312 | if (!dist [x][y]) |
293 | { |
313 | { |
294 | // found new isolated area, make tunnel |
314 | // found new isolated area, make tunnel |
295 | if (!dirty) |
|
|
296 | push_flood_fill (dist, seeds, x, y); |
315 | push_flood_fill (dist, seeds, x, y); |
297 | |
|
|
298 | make_tunnel (dist, seeds, x, y, 255); |
316 | make_tunnel (dist, seeds, x, y, 255); |
299 | |
|
|
300 | if (dirty) |
|
|
301 | push_flood_fill (dist, seeds, x, y); |
|
|
302 | } |
317 | } |
303 | else |
318 | else |
304 | { |
319 | { |
305 | // nothing here, continue to expand |
320 | // nothing here, continue to expand |
306 | U8 d = U8 (dist [x][y]) + 1; |
321 | U8 d = U8 (dist [x][y]) + 1; |
… | |
… | |
309 | if (x > 0) maybe_push (dist, seeds, x - 1, y, d); |
324 | if (x > 0) maybe_push (dist, seeds, x - 1, y, d); |
310 | if (y < dist.h - 1) maybe_push (dist, seeds, x, y + 1, d); |
325 | if (y < dist.h - 1) maybe_push (dist, seeds, x, y + 1, d); |
311 | if (y > 0) maybe_push (dist, seeds, x, y - 1, d); |
326 | if (y > 0) maybe_push (dist, seeds, x, y - 1, d); |
312 | } |
327 | } |
313 | } |
328 | } |
|
|
329 | } |
|
|
330 | |
|
|
331 | void |
|
|
332 | layout::isolation_remover () |
|
|
333 | { |
|
|
334 | layout dist (w - 2, h - 2); // map without border |
|
|
335 | |
|
|
336 | for (int x = 1; x < w - 1; ++x) |
|
|
337 | for (int y = 1; y < h - 1; ++y) |
|
|
338 | dist [x - 1][y - 1] = data [x][y] == '#' ? U8 (255) : 0; |
|
|
339 | |
|
|
340 | ::isolation_remover (dist); |
314 | |
341 | |
315 | // now copy the tunnels over |
342 | // now copy the tunnels over |
316 | for (int x = 0; x < w; ++x) |
343 | for (int x = 1; x < w - 1; ++x) |
317 | for (int y = 0; y < h; ++y) |
344 | for (int y = 1; y < h - 1; ++y) |
318 | if (data [x][y] == '#' && dist [x][y] == 1) |
345 | if (data [x][y] == '#' && dist [x - 1][y - 1] == 1) |
319 | data [x][y] = 0; |
346 | data [x][y] = 0; |
320 | } |
347 | } |
321 | |
348 | |
322 | ///////////////////////////////////////////////////////////////////////////// |
349 | ///////////////////////////////////////////////////////////////////////////// |
323 | |
350 | |
… | |
… | |
327 | { |
354 | { |
328 | switch (subtype) |
355 | switch (subtype) |
329 | { |
356 | { |
330 | // a rough cave |
357 | // a rough cave |
331 | case 0: |
358 | case 0: |
332 | fill_rand (rmg_rndm (80, 95)); |
359 | fill_rand (rmg_rndm (85, 97)); |
333 | break; |
360 | break; |
334 | |
361 | |
335 | // corridors |
362 | // corridors |
336 | case 1: |
363 | case 1: |
337 | fill_rand (rmg_rndm (5, 40)); |
364 | fill_rand (rmg_rndm (5, 40)); |
… | |
… | |
785 | make_wall (*this, dx, dy, 1); |
812 | make_wall (*this, dx, dy, 1); |
786 | } |
813 | } |
787 | } |
814 | } |
788 | |
815 | |
789 | ///////////////////////////////////////////////////////////////////////////// |
816 | ///////////////////////////////////////////////////////////////////////////// |
|
|
817 | |
|
|
818 | static void |
|
|
819 | gen_mixed_ (layout &maze, random_map_params *RP, int dir) |
|
|
820 | { |
|
|
821 | if (maze.w < 20 && maze.h < 20 && !rmg_rndm (3)) |
|
|
822 | dir = 2; // stop recursion randomly |
|
|
823 | |
|
|
824 | if (dir == 0 && maze.w > 16) |
|
|
825 | { |
|
|
826 | int m = rmg_rndm (8, maze.w - 8); |
|
|
827 | |
|
|
828 | layout m1 (maze, 0, 0, m , maze.h); gen_mixed_ (m1, RP, !dir); |
|
|
829 | layout m2 (maze, m, 0, maze.w, maze.h); gen_mixed_ (m2, RP, !dir); |
|
|
830 | } |
|
|
831 | else if (dir == 1 && maze.h > 16) |
|
|
832 | { |
|
|
833 | int m = rmg_rndm (8, maze.h - 8); |
|
|
834 | |
|
|
835 | layout m1 (maze, 0, 0, maze.w, m ); gen_mixed_ (m1, RP, !dir); |
|
|
836 | layout m2 (maze, 0, m, maze.w, maze.h); gen_mixed_ (m2, RP, !dir); |
|
|
837 | } |
|
|
838 | else |
|
|
839 | { |
|
|
840 | RP->map_layout_style = rmg_rndm (NROFLAYOUTS - 2) + 1; |
|
|
841 | |
|
|
842 | if (RP->map_layout_style == LAYOUT_MULTIPLE) |
|
|
843 | ++RP->map_layout_style; |
|
|
844 | |
|
|
845 | maze.generate (RP); |
|
|
846 | } |
|
|
847 | } |
|
|
848 | |
|
|
849 | static void |
|
|
850 | gen_mixed (layout &maze, random_map_params *RP) |
|
|
851 | { |
|
|
852 | random_map_params &rp = *new random_map_params (RP); |
|
|
853 | gen_mixed_ (maze, &rp, rmg_rndm (2)); |
|
|
854 | delete &rp; |
|
|
855 | |
|
|
856 | maze.border (); |
|
|
857 | maze.isolation_remover (); |
|
|
858 | } |
790 | |
859 | |
791 | /* function selects the maze function and gives it whatever |
860 | /* function selects the maze function and gives it whatever |
792 | arguments it needs. */ |
861 | arguments it needs. */ |
793 | void |
862 | void |
794 | layout::generate (random_map_params *RP) |
863 | layout::generate (random_map_params *RP) |
… | |
… | |
854 | if (rmg_rndm (2)) |
923 | if (rmg_rndm (2)) |
855 | doorify (); |
924 | doorify (); |
856 | |
925 | |
857 | break; |
926 | break; |
858 | |
927 | |
|
|
928 | case LAYOUT_MULTIPLE: |
|
|
929 | gen_mixed (*this, RP); |
|
|
930 | break; |
|
|
931 | |
859 | default: |
932 | default: |
860 | abort (); |
933 | abort (); |
861 | } |
934 | } |
862 | |
|
|
863 | /* rotate the maze randomly */ |
|
|
864 | rotate (rmg_rndm (4)); |
|
|
865 | |
|
|
866 | symmetrize (RP->symmetry_used); |
|
|
867 | |
|
|
868 | #if 0 |
|
|
869 | print ();//D |
|
|
870 | #endif |
|
|
871 | |
|
|
872 | if (RP->expand2x) |
|
|
873 | expand2x (); |
|
|
874 | } |
935 | } |
875 | |
936 | |
876 | //-GPL |
937 | //-GPL |
877 | |
938 | |
878 | #if 0 |
939 | #if 0 |
… | |
… | |
880 | { |
941 | { |
881 | demo () |
942 | demo () |
882 | { |
943 | { |
883 | rmg_rndm.seed (time (0)); |
944 | rmg_rndm.seed (time (0)); |
884 | |
945 | |
885 | for(int i=1;i<10;i) |
946 | for(int i=1;i<100;i++) |
886 | { |
947 | { |
887 | layout maze (10, 10); |
948 | layout maze (40, 25); |
888 | maze.fill_rand (90); |
949 | maze.fill_rand (85); |
889 | maze.border (); |
950 | maze.border (); |
890 | maze.isolation_remover (); |
951 | maze.isolation_remover (); |
891 | maze.doorify (); |
|
|
892 | maze.symmetrize (rmg_rndm (2, 4)); |
|
|
893 | maze.rotate (rmg_rndm (4)); |
|
|
894 | maze.expand2x (); |
|
|
895 | maze.print (); |
952 | maze.print (); |
896 | } |
953 | } |
897 | |
954 | |
898 | exit (1); |
955 | exit (1); |
899 | } |
956 | } |