… | |
… | |
21 | */ |
21 | */ |
22 | |
22 | |
23 | #include <global.h> |
23 | #include <global.h> |
24 | #include <random_map.h> |
24 | #include <random_map.h> |
25 | |
25 | |
26 | LayoutData::LayoutData (int w, int h) |
26 | Layout::Layout (int w, int h) |
27 | : w(w), h(h) |
27 | : w(w), h(h) |
28 | { |
28 | { |
29 | int size = (sizeof (char *) + sizeof (char) * h) * w; |
29 | int size = (sizeof (char *) + sizeof (char) * h) * w; |
30 | |
30 | |
31 | col = (char **)salloc<char> (size); |
31 | col = (char **)salloc<char> (size); |
… | |
… | |
34 | |
34 | |
35 | for (int x = w; x--; ) |
35 | for (int x = w; x--; ) |
36 | col [x] = data + x * h; |
36 | col [x] = data + x * h; |
37 | } |
37 | } |
38 | |
38 | |
39 | LayoutData::~LayoutData () |
39 | Layout::~Layout () |
40 | { |
40 | { |
41 | int size = (sizeof (char *) + sizeof (char) * h) * w; |
41 | int size = (sizeof (char *) + sizeof (char) * h) * w; |
42 | |
42 | |
43 | sfree ((char *)col, size); |
43 | sfree ((char *)col, size); |
44 | } |
44 | } |
45 | |
45 | |
46 | void |
46 | void |
47 | LayoutData::fill (char fill) |
47 | Layout::fill (char fill) |
48 | { |
48 | { |
49 | memset (col [0], fill, w * h); |
49 | memset (col [0], fill, w * h); |
50 | } |
50 | } |
51 | |
51 | |
52 | void |
52 | void |
53 | LayoutData::rect (int x1, int y1, int x2, int y2, char fill) |
53 | Layout::rect (int x1, int y1, int x2, int y2, char fill) |
54 | { |
54 | { |
55 | for (; x1 < x2; ++x1) |
55 | for (; x1 < x2; ++x1) |
56 | memset (col [x1] + y1, fill, y2 - y1); |
56 | memset (col [x1] + y1, fill, y2 - y1); |
57 | } |
57 | } |
58 | |
58 | |
59 | void LayoutData::border (char fill) |
59 | void Layout::border (char fill) |
60 | { |
60 | { |
61 | for (int i = 0; i < w; i++) col [i][0] = col [i][h - 1] = fill; |
61 | for (int i = 0; i < w; i++) col [i][0] = col [i][h - 1] = fill; |
62 | for (int j = 0; j < h; j++) col [0][j] = col [w - 1][j] = fill; |
62 | for (int j = 0; j < h; j++) col [0][j] = col [w - 1][j] = fill; |
63 | } |
63 | } |
64 | |
64 | |
65 | void |
65 | void |
66 | LayoutData::fill_rand (int percent) |
66 | Layout::fill_rand (int percent) |
67 | { |
67 | { |
68 | percent = lerp (percent, 0, 100, 0, 256); |
68 | percent = lerp (percent, 0, 100, 0, 256); |
69 | |
69 | |
70 | for (int x = w - 1; --x > 0; ) |
70 | for (int x = w - 1; --x > 0; ) |
71 | for (int y = h - 1; --y > 0; ) |
71 | for (int y = h - 1; --y > 0; ) |
… | |
… | |
74 | |
74 | |
75 | ///////////////////////////////////////////////////////////////////////////// |
75 | ///////////////////////////////////////////////////////////////////////////// |
76 | |
76 | |
77 | // erode by cellular automata |
77 | // erode by cellular automata |
78 | void |
78 | void |
79 | LayoutData::erode_1_2 (int c1, int c2, int repeat) |
79 | Layout::erode_1_2 (int c1, int c2, int repeat) |
80 | { |
80 | { |
81 | LayoutData neu (w, h); |
81 | Layout neu (w, h); |
82 | |
82 | |
83 | while (repeat--) |
83 | while (repeat--) |
84 | { |
84 | { |
85 | for (int x = 0; x < w; ++x) |
85 | for (int x = 0; x < w; ++x) |
86 | { |
86 | { |
… | |
… | |
140 | } |
140 | } |
141 | |
141 | |
142 | ///////////////////////////////////////////////////////////////////////////// |
142 | ///////////////////////////////////////////////////////////////////////////// |
143 | |
143 | |
144 | void |
144 | void |
145 | LayoutData::print () |
145 | Layout::print () |
146 | { |
146 | { |
147 | for (int y = 0; y < h; y++) |
147 | for (int y = 0; y < h; y++) |
148 | { |
148 | { |
149 | for (int x = 0; x < w; x++) |
149 | for (int x = 0; x < w; x++) |
150 | { |
150 | { |
… | |
… | |
170 | // isolation remover - ensures single connected area |
170 | // isolation remover - ensures single connected area |
171 | |
171 | |
172 | typedef fixed_stack<point> pointlist; |
172 | typedef fixed_stack<point> pointlist; |
173 | |
173 | |
174 | static noinline void |
174 | static noinline void |
175 | push_flood_fill (LayoutData &maze, LayoutData &dist, pointlist &seeds, int x, int y) |
175 | push_flood_fill (Layout &dist, pointlist &seeds, int x, int y) |
176 | { |
176 | { |
177 | if (maze [x][y]) |
177 | if (dist [x][y]) |
178 | return; |
178 | return; |
179 | |
179 | |
180 | while (y > 0 && !maze [x][y - 1]) |
180 | while (y > 0 && !dist [x][y - 1]) |
181 | --y; |
181 | --y; |
182 | |
182 | |
183 | int y0 = y; |
183 | int y0 = y; |
184 | |
184 | |
185 | while (y < maze.h && !maze [x][y]) |
185 | while (y < dist.h && !dist [x][y]) |
186 | { |
186 | { |
187 | seeds.push (point (x, y)); |
187 | seeds.push (point (x, y)); |
188 | |
188 | |
189 | maze [x][y] = 1; |
|
|
190 | dist [x][y] = 1; |
189 | dist [x][y] = 1; |
191 | ++y; |
190 | ++y; |
192 | } |
191 | } |
193 | |
192 | |
194 | while (--y >= y0) |
193 | while (--y >= y0) |
195 | { |
194 | { |
196 | if (x > 0) push_flood_fill (maze, dist, seeds, x - 1, y); |
195 | if (x > 0) push_flood_fill (dist, seeds, x - 1, y); |
197 | if (x < maze.w - 1) push_flood_fill (maze, dist, seeds, x + 1, y); |
196 | if (x < dist.w - 1) push_flood_fill (dist, seeds, x + 1, y); |
198 | } |
197 | } |
199 | } |
198 | } |
200 | |
199 | |
201 | static inline void |
200 | static inline void |
202 | make_tunnel (LayoutData &maze, LayoutData &dist, pointlist &seeds, int x, int y, U8 d) |
201 | make_tunnel (Layout &dist, pointlist &seeds, int x, int y, U8 d) |
203 | { |
202 | { |
204 | for (;;) |
203 | for (;;) |
205 | { |
204 | { |
206 | seeds.push (point (x, y)); |
|
|
207 | |
|
|
208 | maze [x][y] = 1; |
|
|
209 | dist [x][y] = 1; |
|
|
210 | |
|
|
211 | if (x > 1 && U8 (dist [x - 1][y]) < d && dist [x - 1][y] > 1) |
205 | if (x > 1 && U8 (dist [x - 1][y]) < d && dist [x - 1][y] > 1) |
212 | --x; |
206 | --x; |
213 | else if (x < maze.w - 2 && U8 (dist [x + 1][y]) < d && dist [x + 1][y] > 1) |
207 | else if (x < dist.w - 2 && U8 (dist [x + 1][y]) < d && dist [x + 1][y] > 1) |
214 | ++x; |
208 | ++x; |
215 | else if (y > 1 && U8 (dist [x][y - 1]) < d && dist [x][y - 1] > 1) |
209 | else if (y > 1 && U8 (dist [x][y - 1]) < d && dist [x][y - 1] > 1) |
216 | --y; |
210 | --y; |
217 | else if (y < maze.h - 2 && U8 (dist [x][y + 1]) < d && dist [x][y + 1] > 1) |
211 | else if (y < dist.h - 2 && U8 (dist [x][y + 1]) < d && dist [x][y + 1] > 1) |
218 | ++y; |
212 | ++y; |
219 | else |
213 | else |
220 | break; |
214 | break; |
221 | |
215 | |
222 | d = dist [x][y]; |
216 | d = dist [x][y]; |
|
|
217 | dist [x][y] = 1; |
|
|
218 | seeds.push (point (x, y)); |
223 | } |
219 | } |
|
|
220 | } |
|
|
221 | |
|
|
222 | static void inline |
|
|
223 | maybe_push (Layout &dist, pointlist &seeds, int x, int y, U8 d) |
|
|
224 | { |
|
|
225 | char &D = dist [x][y]; |
|
|
226 | |
|
|
227 | if (U8 (D) > d) // if wall and higher distance, lower distance |
|
|
228 | D = d; |
|
|
229 | else if (D) // otherwise, if it's no room, this space is uninteresting |
|
|
230 | return; |
|
|
231 | |
|
|
232 | seeds.push (point (x, y)); |
224 | } |
233 | } |
225 | |
234 | |
226 | static void |
235 | static void |
227 | isolation_remover (LayoutData &maze, bool dirty) |
236 | isolation_remover (Layout &maze, bool dirty) |
228 | { |
237 | { |
229 | LayoutData dist (maze.w, maze.h); |
238 | Layout dist (maze.w, maze.h); |
230 | |
239 | |
231 | dist.fill (255); |
240 | // dist contains |
|
|
241 | // 0 == invisited rooms |
|
|
242 | // 1 == visited rooms |
|
|
243 | // 2+ shortest distance to random near room |
232 | |
244 | |
|
|
245 | // phase 1, initialise dist array, find seed |
|
|
246 | int cnt = 0; |
|
|
247 | int x, y; |
|
|
248 | |
|
|
249 | for (int i = 0; i < maze.w; ++i) |
|
|
250 | for (int j = 0; j < maze.h; ++j) |
|
|
251 | { |
|
|
252 | if (maze [i][j] == '#') |
|
|
253 | dist [i][j] = U8 (255); |
|
|
254 | else |
|
|
255 | { |
|
|
256 | dist [i][j] = 0; |
|
|
257 | if (!rmg_rndm (++cnt)) |
|
|
258 | x = i, y = j; |
|
|
259 | } |
|
|
260 | } |
|
|
261 | |
|
|
262 | if (!cnt) |
|
|
263 | return; |
|
|
264 | |
233 | fixed_stack<point> seeds (maze.w * maze.h * 4); |
265 | fixed_stack<point> seeds (maze.w * maze.h * 5); |
234 | |
266 | |
235 | // phase 1, find seed |
267 | // found first free space - picking the first one gives |
236 | for (int x = 1; x < maze.w; ++x) |
268 | // us a slight bias for tunnels, but usually you won't |
237 | for (int y = 1; y < maze.h; ++y) |
269 | // notice that in-game |
|
|
270 | seeds.push (point (x, y)); |
|
|
271 | |
|
|
272 | // phase 2, while we have seeds, if |
|
|
273 | // seed is empty, floodfill, else grow |
|
|
274 | |
|
|
275 | while (seeds.size) |
|
|
276 | { |
|
|
277 | coroapi::cede_to_tick (); |
|
|
278 | |
|
|
279 | point p = seeds.remove (rmg_rndm (seeds.size)); |
|
|
280 | |
|
|
281 | x = p.x; |
|
|
282 | y = p.y; |
|
|
283 | |
238 | if (!maze [x][y]) |
284 | if (!dist [x][y]) |
239 | { |
285 | { |
240 | // found first free space - picking the first one gives |
|
|
241 | // us a slight bias for tunnels, but usually you won't |
|
|
242 | // notice that in-game |
|
|
243 | seeds.push (point (x, y)); |
|
|
244 | |
|
|
245 | // phase 2, while we have seeds, if |
|
|
246 | // seed is empty, floodfill, else grow |
|
|
247 | |
|
|
248 | while (seeds.size) |
|
|
249 | { |
|
|
250 | coroapi::cede_to_tick (); |
|
|
251 | |
|
|
252 | point p = seeds.remove (rmg_rndm (seeds.size)); |
|
|
253 | |
|
|
254 | x = p.x; |
|
|
255 | y = p.y; |
|
|
256 | |
|
|
257 | if (!maze [x][y]) |
|
|
258 | { |
|
|
259 | // found new isolated area, make tunnel? |
286 | // found new isolated area, make tunnel |
260 | if (!dirty) |
287 | if (!dirty) |
261 | push_flood_fill (maze, dist, seeds, x, y); |
288 | push_flood_fill (dist, seeds, x, y); |
262 | |
289 | |
263 | make_tunnel (maze, dist, seeds, x, y, 255); |
290 | make_tunnel (dist, seeds, x, y, 255); |
264 | |
291 | |
265 | if (dirty) |
292 | if (dirty) |
266 | push_flood_fill (maze, dist, seeds, x, y); |
293 | push_flood_fill (dist, seeds, x, y); |
267 | } |
|
|
268 | else |
|
|
269 | { |
|
|
270 | U8 d = U8 (dist [x][y]) + 1; |
|
|
271 | |
|
|
272 | if (x < maze.w - 1 && U8 (dist [x + 1][y]) > d) |
|
|
273 | { |
|
|
274 | dist [x + 1][y] = d; |
|
|
275 | seeds.push (point (x + 1, y)); |
|
|
276 | } |
|
|
277 | |
|
|
278 | if (x > 0 && U8 (dist [x - 1][y]) > d) |
|
|
279 | { |
|
|
280 | dist [x - 1][y] = d; |
|
|
281 | seeds.push (point (x - 1, y)); |
|
|
282 | } |
|
|
283 | |
|
|
284 | if (y < maze.h - 1 && U8 (dist [x][y + 1]) > d) |
|
|
285 | { |
|
|
286 | dist [x][y + 1] = d; |
|
|
287 | seeds.push (point (x, y + 1)); |
|
|
288 | } |
|
|
289 | |
|
|
290 | if (y > 0 && U8 (dist [x][y - 1]) > d) |
|
|
291 | { |
|
|
292 | dist [x][y - 1] = d; |
|
|
293 | seeds.push (point (x, y - 1)); |
|
|
294 | } |
|
|
295 | } |
|
|
296 | } |
|
|
297 | |
|
|
298 | goto success; |
|
|
299 | } |
294 | } |
|
|
295 | else |
|
|
296 | { |
|
|
297 | // nothing here, continue to expand |
|
|
298 | U8 d = U8 (dist [x][y]) + 1; |
300 | |
299 | |
301 | success: |
300 | if (x < dist.w - 1) maybe_push (dist, seeds, x + 1, y, d); |
|
|
301 | if (x > 0) maybe_push (dist, seeds, x - 1, y, d); |
|
|
302 | if (y < dist.h - 1) maybe_push (dist, seeds, x, y + 1, d); |
|
|
303 | if (y > 0) maybe_push (dist, seeds, x, y - 1, d); |
|
|
304 | } |
|
|
305 | } |
302 | |
306 | |
303 | // we mark free but visited floors as 1, undo this here |
307 | // now copy the tunnels over |
304 | for (int x = 0; x < maze.w; ++x) |
308 | for (int x = 0; x < maze.w; ++x) |
305 | for (int y = 0; y < maze.h; ++y) |
309 | for (int y = 0; y < maze.h; ++y) |
306 | if (maze [x][y] == 1) |
310 | if (maze [x][y] == '#' && dist [x][y] == 1) |
307 | maze [x][y] = 0; |
311 | maze [x][y] = 0; |
308 | } |
312 | } |
309 | |
313 | |
310 | void |
314 | void |
311 | LayoutData::isolation_remover (bool dirty) |
315 | Layout::isolation_remover (bool dirty) |
312 | { |
316 | { |
313 | ::isolation_remover (*this, dirty); |
317 | ::isolation_remover (*this, dirty); |
314 | } |
318 | } |
315 | |
319 | |
316 | ///////////////////////////////////////////////////////////////////////////// |
320 | ///////////////////////////////////////////////////////////////////////////// |
317 | |
321 | |
318 | // inspired mostly by http://www.jimrandomh.org/misc/caves.txt |
322 | // inspired mostly by http://www.jimrandomh.org/misc/caves.txt |
319 | void |
323 | void |
320 | LayoutData::gen_cave (int subtype) |
324 | Layout::gen_cave (int subtype) |
321 | { |
325 | { |
322 | switch (subtype) |
326 | switch (subtype) |
323 | { |
327 | { |
324 | // a rough cave |
328 | // a rough cave |
325 | case 0: |
329 | case 0: |
… | |
… | |
361 | Layout maze (40, 25); |
365 | Layout maze (40, 25); |
362 | rmg_rndm.seed (time (0)); |
366 | rmg_rndm.seed (time (0)); |
363 | |
367 | |
364 | for(int i=1;i<10;i) |
368 | for(int i=1;i<10;i) |
365 | { |
369 | { |
366 | maze->gen_cave (3); |
370 | maze.fill_rand (97); |
|
|
371 | maze.border (); |
|
|
372 | maze.isolation_remover (); |
367 | maze->print (); |
373 | maze.print (); |
368 | } |
374 | } |
369 | |
375 | |
370 | exit (1); |
376 | exit (1); |
371 | } |
377 | } |
372 | } demo; |
378 | } demo; |