… | |
… | |
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) |
… | |
… | |
71 | memset (data [0], fill, w * h); |
82 | memset (data [0], fill, w * h); |
72 | } |
83 | } |
73 | |
84 | |
74 | void |
85 | void |
75 | layout::rect (int x1, int y1, int x2, int y2, char fill) |
86 | layout::rect (int x1, int y1, int x2, int y2, char fill) |
|
|
87 | { |
|
|
88 | --x2; |
|
|
89 | |
|
|
90 | memset (data [x1] + y1, fill, y2 - y1); |
|
|
91 | memset (data [x2] + y1, fill, y2 - y1); |
|
|
92 | |
|
|
93 | while (++x1 < x2) |
|
|
94 | data [x1][y1] = data [x1][y2 - 1] = fill; |
|
|
95 | } |
|
|
96 | |
|
|
97 | void |
|
|
98 | layout::fill_rect (int x1, int y1, int x2, int y2, char fill) |
76 | { |
99 | { |
77 | for (; x1 < x2; ++x1) |
100 | for (; x1 < x2; ++x1) |
78 | memset (data [x1] + y1, fill, y2 - y1); |
101 | memset (data [x1] + y1, fill, y2 - y1); |
79 | } |
102 | } |
80 | |
103 | |
81 | void layout::border (char fill) |
104 | void layout::border (char fill) |
82 | { |
105 | { |
83 | for (int i = 0; i < w; i++) data [i][0] = data [i][h - 1] = fill; |
106 | rect (0, 0, w, h, fill); |
84 | for (int j = 0; j < h; j++) data [0][j] = data [w - 1][j] = fill; |
|
|
85 | } |
107 | } |
86 | |
108 | |
87 | void |
109 | void |
88 | layout::fill_rand (int percent) |
110 | layout::fill_rand (int percent) |
89 | { |
111 | { |
… | |
… | |
171 | ///////////////////////////////////////////////////////////////////////////// |
193 | ///////////////////////////////////////////////////////////////////////////// |
172 | // isolation remover - ensures single connected area |
194 | // isolation remover - ensures single connected area |
173 | |
195 | |
174 | typedef fixed_stack<point> pointlist; |
196 | typedef fixed_stack<point> pointlist; |
175 | |
197 | |
176 | static noinline void |
198 | static void noinline |
177 | push_flood_fill (layout &dist, pointlist &seeds, int x, int y) |
199 | push_flood_fill (layout &dist, pointlist &seeds, int x, int y) |
178 | { |
200 | { |
179 | if (dist [x][y]) |
201 | if (dist [x][y]) |
180 | return; |
202 | return; |
181 | |
203 | |
… | |
… | |
192 | ++y; |
214 | ++y; |
193 | } |
215 | } |
194 | |
216 | |
195 | while (--y >= y0) |
217 | while (--y >= y0) |
196 | { |
218 | { |
197 | if (x > 0) push_flood_fill (dist, seeds, x - 1, y); |
219 | 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); |
220 | if (x < dist.w - 1 && !dist [x + 1][y]) push_flood_fill (dist, seeds, x + 1, y); |
199 | } |
221 | } |
200 | } |
222 | } |
201 | |
223 | |
202 | static inline void |
224 | static inline void |
203 | make_tunnel (layout &dist, pointlist &seeds, int x, int y, U8 d) |
225 | make_tunnel (layout &dist, pointlist &seeds, int x, int y, U8 d) |
204 | { |
226 | { |
205 | for (;;) |
227 | for (;;) |
206 | { |
228 | { |
|
|
229 | point neigh[4]; |
|
|
230 | int ncnt = 0; |
|
|
231 | |
207 | if (x > 1 && U8 (dist [x - 1][y]) < d && dist [x - 1][y] > 1) |
232 | 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) |
233 | 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) |
234 | 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) |
235 | if (y < dist.h - 1 && U8 (dist [x][y + 1]) <= d && dist [x][y + 1] > 1) neigh [ncnt++] = point (x, y + 1); |
214 | ++y; |
236 | |
215 | else |
237 | if (!ncnt) |
216 | break; |
238 | return; |
|
|
239 | |
|
|
240 | point &p = neigh [rmg_rndm (ncnt)]; |
|
|
241 | |
|
|
242 | seeds.push (p); |
|
|
243 | |
|
|
244 | x = p.x; |
|
|
245 | y = p.y; |
217 | |
246 | |
218 | d = dist [x][y]; |
247 | d = dist [x][y]; |
219 | dist [x][y] = 1; |
248 | dist [x][y] = 1; |
220 | seeds.push (point (x, y)); |
|
|
221 | } |
249 | } |
222 | } |
250 | } |
223 | |
251 | |
224 | static void inline |
252 | static void inline |
225 | maybe_push (layout &dist, pointlist &seeds, int x, int y, U8 d) |
253 | maybe_push (layout &dist, pointlist &seeds, int x, int y, U8 d) |
… | |
… | |
232 | return; |
260 | return; |
233 | |
261 | |
234 | seeds.push (point (x, y)); |
262 | seeds.push (point (x, y)); |
235 | } |
263 | } |
236 | |
264 | |
237 | void |
265 | // isolation remover, works on a "distance" map |
238 | layout::isolation_remover (bool dirty) |
266 | // the map must be initialised with 0 == rooms, 255 = walls |
|
|
267 | static void noinline |
|
|
268 | isolation_remover (layout &dist) |
239 | { |
269 | { |
240 | layout dist (w, h); |
|
|
241 | |
|
|
242 | // dist contains |
270 | // dist contains |
243 | // 0 == invisited rooms |
271 | // 0 == invisited rooms |
244 | // 1 == visited rooms |
272 | // 1 == visited rooms |
245 | // 2+ shortest distance to random near room |
273 | // 2+ shortest distance to random near room |
246 | |
274 | |
247 | // phase 1, initialise dist array, find seed |
275 | // phase 1, find seed |
248 | int cnt = 0; |
276 | int cnt = 0; |
249 | int x, y; |
277 | int x, y; |
250 | |
278 | |
251 | for (int i = 0; i < w; ++i) |
279 | for (int i = 0; i < dist.w; ++i) |
252 | for (int j = 0; j < h; ++j) |
280 | for (int j = 0; j < dist.h; ++j) |
253 | { |
281 | 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; |
282 | x = i, y = j; |
261 | } |
|
|
262 | } |
|
|
263 | |
283 | |
264 | if (!cnt) |
284 | if (!cnt) |
265 | { |
285 | { |
266 | // map is completely massive, this is not good, |
286 | // map is completely massive, this is not good, |
267 | // so make it empty instead. |
287 | // so make it empty instead. |
268 | clear (); |
288 | dist.fill (1); |
269 | border (); |
|
|
270 | return; |
289 | return; |
271 | } |
290 | } |
272 | |
291 | |
273 | fixed_stack<point> seeds (w * h * 5); |
292 | fixed_stack<point> seeds (dist.w * dist.h * 5); |
274 | |
293 | |
275 | // found first free space - picking the first one gives |
294 | // found first free space - picking the first one gives |
276 | // us a slight bias for tunnels, but usually you won't |
295 | // us a slight bias for tunnels, but usually you won't |
277 | // notice that in-game |
296 | // notice that in-game |
278 | seeds.push (point (x, y)); |
297 | seeds.push (point (x, y)); |
… | |
… | |
290 | y = p.y; |
309 | y = p.y; |
291 | |
310 | |
292 | if (!dist [x][y]) |
311 | if (!dist [x][y]) |
293 | { |
312 | { |
294 | // found new isolated area, make tunnel |
313 | // found new isolated area, make tunnel |
295 | if (!dirty) |
|
|
296 | push_flood_fill (dist, seeds, x, y); |
314 | push_flood_fill (dist, seeds, x, y); |
297 | |
|
|
298 | make_tunnel (dist, seeds, x, y, 255); |
315 | make_tunnel (dist, seeds, x, y, 255); |
299 | |
|
|
300 | if (dirty) |
|
|
301 | push_flood_fill (dist, seeds, x, y); |
|
|
302 | } |
316 | } |
303 | else |
317 | else |
304 | { |
318 | { |
305 | // nothing here, continue to expand |
319 | // nothing here, continue to expand |
306 | U8 d = U8 (dist [x][y]) + 1; |
320 | U8 d = U8 (dist [x][y]) + 1; |
… | |
… | |
309 | if (x > 0) maybe_push (dist, seeds, x - 1, y, d); |
323 | 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); |
324 | 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); |
325 | if (y > 0) maybe_push (dist, seeds, x, y - 1, d); |
312 | } |
326 | } |
313 | } |
327 | } |
|
|
328 | } |
|
|
329 | |
|
|
330 | void |
|
|
331 | layout::isolation_remover () |
|
|
332 | { |
|
|
333 | layout dist (w - 2, h - 2); // map without border |
|
|
334 | |
|
|
335 | for (int x = 1; x < w - 1; ++x) |
|
|
336 | for (int y = 1; y < h - 1; ++y) |
|
|
337 | dist [x - 1][y - 1] = data [x][y] == '#' ? U8 (255) : 0; |
|
|
338 | |
|
|
339 | ::isolation_remover (dist); |
314 | |
340 | |
315 | // now copy the tunnels over |
341 | // now copy the tunnels over |
316 | for (int x = 0; x < w; ++x) |
342 | for (int x = 1; x < w - 1; ++x) |
317 | for (int y = 0; y < h; ++y) |
343 | for (int y = 1; y < h - 1; ++y) |
318 | if (data [x][y] == '#' && dist [x][y] == 1) |
344 | if (data [x][y] == '#' && dist [x - 1][y - 1] == 1) |
319 | data [x][y] = 0; |
345 | data [x][y] = 0; |
320 | } |
346 | } |
321 | |
347 | |
322 | ///////////////////////////////////////////////////////////////////////////// |
348 | ///////////////////////////////////////////////////////////////////////////// |
323 | |
349 | |
… | |
… | |
327 | { |
353 | { |
328 | switch (subtype) |
354 | switch (subtype) |
329 | { |
355 | { |
330 | // a rough cave |
356 | // a rough cave |
331 | case 0: |
357 | case 0: |
332 | fill_rand (rmg_rndm (80, 95)); |
358 | fill_rand (rmg_rndm (85, 97)); |
333 | break; |
359 | break; |
334 | |
360 | |
335 | // corridors |
361 | // corridors |
336 | case 1: |
362 | case 1: |
337 | fill_rand (rmg_rndm (5, 40)); |
363 | fill_rand (rmg_rndm (5, 40)); |
… | |
… | |
880 | { |
906 | { |
881 | demo () |
907 | demo () |
882 | { |
908 | { |
883 | rmg_rndm.seed (time (0)); |
909 | rmg_rndm.seed (time (0)); |
884 | |
910 | |
885 | for(int i=1;i<10;i) |
911 | for(int i=1;i<100;i++) |
886 | { |
912 | { |
887 | layout maze (10, 10); |
913 | layout maze (40, 25); |
888 | maze.fill_rand (90); |
914 | maze.fill_rand (85); |
889 | maze.border (); |
915 | maze.border (); |
890 | maze.isolation_remover (); |
916 | 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 (); |
917 | maze.print (); |
896 | } |
918 | } |
897 | |
919 | |
898 | exit (1); |
920 | exit (1); |
899 | } |
921 | } |