… | |
… | |
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) |
… | |
… | |
203 | ++y; |
214 | ++y; |
204 | } |
215 | } |
205 | |
216 | |
206 | while (--y >= y0) |
217 | while (--y >= y0) |
207 | { |
218 | { |
208 | 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); |
209 | 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); |
210 | } |
221 | } |
211 | } |
222 | } |
212 | |
223 | |
213 | static inline void |
224 | static inline void |
214 | 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) |
… | |
… | |
216 | for (;;) |
227 | for (;;) |
217 | { |
228 | { |
218 | point neigh[4]; |
229 | point neigh[4]; |
219 | int ncnt = 0; |
230 | int ncnt = 0; |
220 | |
231 | |
221 | if (x > 1 && U8 (dist [x - 1][y]) <= d && dist [x - 1][y] > 1) neigh [ncnt++] = point (x - 1, y); |
232 | if (x > 0 && U8 (dist [x - 1][y]) <= d && dist [x - 1][y] > 1) neigh [ncnt++] = point (x - 1, y); |
222 | if (x < dist.w - 2 && U8 (dist [x + 1][y]) <= d && dist [x + 1][y] > 1) neigh [ncnt++] = point (x + 1, y); |
233 | if (x < dist.w - 1 && U8 (dist [x + 1][y]) <= d && dist [x + 1][y] > 1) neigh [ncnt++] = point (x + 1, y); |
223 | if (y > 1 && U8 (dist [x][y - 1]) <= d && dist [x][y - 1] > 1) neigh [ncnt++] = point (x, y - 1); |
234 | if (y > 0 && U8 (dist [x][y - 1]) <= d && dist [x][y - 1] > 1) neigh [ncnt++] = point (x, y - 1); |
224 | if (y < dist.h - 2 && U8 (dist [x][y + 1]) <= d && dist [x][y + 1] > 1) neigh [ncnt++] = point (x, y + 1); |
235 | if (y < dist.h - 1 && U8 (dist [x][y + 1]) <= d && dist [x][y + 1] > 1) neigh [ncnt++] = point (x, y + 1); |
225 | |
236 | |
226 | if (!ncnt) |
237 | if (!ncnt) |
227 | return; |
238 | return; |
228 | |
239 | |
229 | point &p = neigh [rmg_rndm (ncnt)]; |
240 | point &p = neigh [rmg_rndm (ncnt)]; |
… | |
… | |
272 | |
283 | |
273 | if (!cnt) |
284 | if (!cnt) |
274 | { |
285 | { |
275 | // map is completely massive, this is not good, |
286 | // map is completely massive, this is not good, |
276 | // so make it empty instead. |
287 | // so make it empty instead. |
277 | dist.clear (); |
288 | dist.fill (1); |
278 | dist.border (255); |
|
|
279 | return; |
289 | return; |
280 | } |
290 | } |
281 | |
291 | |
282 | fixed_stack<point> seeds (dist.w * dist.h * 5); |
292 | fixed_stack<point> seeds (dist.w * dist.h * 5); |
283 | |
293 | |
… | |
… | |
318 | } |
328 | } |
319 | |
329 | |
320 | void |
330 | void |
321 | layout::isolation_remover () |
331 | layout::isolation_remover () |
322 | { |
332 | { |
323 | layout dist (w, h); |
333 | layout dist (w - 2, h - 2); // map without border |
324 | |
334 | |
325 | for (int x = 0; x < w; ++x) |
335 | for (int x = 1; x < w - 1; ++x) |
326 | for (int y = 0; y < h; ++y) |
336 | for (int y = 1; y < h - 1; ++y) |
327 | dist [x][y] = data [x][y] == '#' ? U8 (255) : 0; |
337 | dist [x - 1][y - 1] = data [x][y] == '#' ? U8 (255) : 0; |
328 | |
338 | |
329 | ::isolation_remover (dist); |
339 | ::isolation_remover (dist); |
330 | |
340 | |
331 | // now copy the tunnels over |
341 | // now copy the tunnels over |
332 | for (int x = 0; x < w; ++x) |
342 | for (int x = 1; x < w - 1; ++x) |
333 | for (int y = 0; y < h; ++y) |
343 | for (int y = 1; y < h - 1; ++y) |
334 | if (data [x][y] == '#' && dist [x][y] == 1) |
344 | if (data [x][y] == '#' && dist [x - 1][y - 1] == 1) |
335 | data [x][y] = 0; |
345 | data [x][y] = 0; |
336 | } |
346 | } |
337 | |
347 | |
338 | ///////////////////////////////////////////////////////////////////////////// |
348 | ///////////////////////////////////////////////////////////////////////////// |
339 | |
349 | |