… | |
… | |
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; |
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 noinline |
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 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 |
75 | 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) |
76 | { |
97 | { |
77 | --x2; |
98 | --x2; |
78 | |
99 | |
79 | memset (data [x1] + y1, fill, y2 - y1); |
100 | memset (data [x1] + y1, fill, y2 - y1); |
… | |
… | |
81 | |
102 | |
82 | while (++x1 < x2) |
103 | while (++x1 < x2) |
83 | data [x1][y1] = data [x1][y2 - 1] = fill; |
104 | data [x1][y1] = data [x1][y2 - 1] = fill; |
84 | } |
105 | } |
85 | |
106 | |
86 | void |
107 | void noinline |
87 | 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) |
88 | { |
109 | { |
89 | for (; x1 < x2; ++x1) |
110 | for (; x1 < x2; ++x1) |
90 | memset (data [x1] + y1, fill, y2 - y1); |
111 | memset (data [x1] + y1, fill, y2 - y1); |
91 | } |
112 | } |
92 | |
113 | |
|
|
114 | void |
93 | void layout::border (char fill) |
115 | layout::border (char fill) |
94 | { |
116 | { |
95 | rect (0, 0, w, h, fill); |
117 | rect (0, 0, w, h, fill); |
96 | } |
118 | } |
97 | |
119 | |
98 | void |
120 | void noinline |
99 | layout::fill_rand (int percent) |
121 | layout::fill_rand (int percent) |
100 | { |
122 | { |
101 | percent = lerp (percent, 0, 100, 0, 256); |
123 | percent = lerp (percent, 0, 100, 0, 256); |
102 | |
124 | |
103 | for (int x = w - 1; --x > 0; ) |
125 | for (int x = 0; x < w; ++x) |
104 | for (int y = h - 1; --y > 0; ) |
126 | for (int y = 0; y < h; ++y) |
105 | data [x][y] = rmg_rndm (256) > percent ? 0 : '#'; |
127 | data [x][y] = rmg_rndm (256) > percent ? 0 : '#'; |
106 | } |
128 | } |
107 | |
129 | |
108 | ///////////////////////////////////////////////////////////////////////////// |
130 | ///////////////////////////////////////////////////////////////////////////// |
109 | |
131 | |
110 | // erode by cellular automata |
132 | // erode by cellular automata |
111 | void |
133 | void noinline |
112 | layout::erode_1_2 (int c1, int c2, int repeat) |
134 | layout::erode_1_2 (int c1, int c2, int repeat) |
113 | { |
135 | { |
114 | layout neu (w, h); |
136 | layout neu (w, h); |
115 | |
137 | |
116 | while (repeat--) |
138 | while (repeat--) |
… | |
… | |
208 | 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); |
209 | 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); |
210 | } |
232 | } |
211 | } |
233 | } |
212 | |
234 | |
213 | static inline void |
235 | static void inline |
214 | make_tunnel (layout &dist, pointlist &seeds, int x, int y, U8 d) |
236 | make_tunnel (layout &dist, pointlist &seeds, int x, int y, U8 d, int perturb) |
215 | { |
237 | { |
216 | for (;;) |
238 | for (;;) |
217 | { |
239 | { |
218 | point neigh[4]; |
240 | point neigh[4]; |
219 | int ncnt = 0; |
241 | int ncnt = 0; |
220 | |
242 | |
|
|
243 | d += perturb > 1; |
|
|
244 | |
221 | if (x > 1 && U8 (dist [x - 1][y]) <= d && dist [x - 1][y] > 1) neigh [ncnt++] = point (x - 1, y); |
245 | 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); |
246 | 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); |
247 | 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); |
248 | if (y < dist.h - 1 && U8 (dist [x][y + 1]) < d && dist [x][y + 1] > 1) neigh [ncnt++] = point (x, y + 1); |
225 | |
|
|
226 | printf ("tunnel %d+%d ncnt %d\n", x, y, ncnt);//D |
|
|
227 | |
249 | |
228 | if (!ncnt) |
250 | if (!ncnt) |
229 | return; |
251 | return; |
230 | |
252 | |
231 | point &p = neigh [rmg_rndm (ncnt)]; |
253 | point p = neigh [perturb ? rmg_rndm (ncnt) : 0]; |
232 | |
254 | |
233 | seeds.push (p); |
255 | seeds.push (p); |
234 | |
256 | |
235 | x = p.x; |
257 | x = p.x; |
236 | y = p.y; |
258 | y = p.y; |
… | |
… | |
254 | } |
276 | } |
255 | |
277 | |
256 | // isolation remover, works on a "distance" map |
278 | // isolation remover, works on a "distance" map |
257 | // the map must be initialised with 0 == rooms, 255 = walls |
279 | // the map must be initialised with 0 == rooms, 255 = walls |
258 | static void noinline |
280 | static void noinline |
259 | isolation_remover (layout &dist) |
281 | isolation_remover (layout &dist, unsigned int perturb = 2) |
260 | { |
282 | { |
261 | // dist contains |
283 | // dist contains |
262 | // 0 == invisited rooms |
284 | // 0 == invisited rooms |
263 | // 1 == visited rooms |
285 | // 1 == visited rooms |
264 | // 2+ shortest distance to random near room |
286 | // 2+ shortest distance to random near room |
265 | |
287 | |
|
|
288 | clamp_it (perturb, 0, 2); |
|
|
289 | |
266 | // phase 1, find seed |
290 | // phase 1, find seed |
267 | int cnt = 0; |
291 | int cnt = 0; |
268 | int x, y; |
292 | int x, y; |
269 | |
293 | |
270 | for (int i = 0; i < dist.w; ++i) |
294 | for (int i = 0; i < dist.w; ++i) |
… | |
… | |
275 | if (!cnt) |
299 | if (!cnt) |
276 | { |
300 | { |
277 | // map is completely massive, this is not good, |
301 | // map is completely massive, this is not good, |
278 | // so make it empty instead. |
302 | // so make it empty instead. |
279 | dist.fill (1); |
303 | dist.fill (1); |
280 | dist.border (255); |
|
|
281 | return; |
304 | return; |
282 | } |
305 | } |
283 | |
306 | |
284 | fixed_stack<point> seeds (dist.w * dist.h * 5); |
307 | fixed_stack<point> seeds (dist.w * dist.h * 5); |
285 | |
308 | |
… | |
… | |
289 | seeds.push (point (x, y)); |
312 | seeds.push (point (x, y)); |
290 | |
313 | |
291 | // phase 2, while we have seeds, if |
314 | // phase 2, while we have seeds, if |
292 | // seed is empty, floodfill, else grow |
315 | // seed is empty, floodfill, else grow |
293 | |
316 | |
|
|
317 | int rem_index = 0; // used to remove "somewhat ordered" |
|
|
318 | |
294 | while (seeds.size) |
319 | while (seeds.size) |
295 | { |
320 | { |
296 | coroapi::cede_to_tick (); |
321 | coroapi::cede_to_tick (); |
297 | |
322 | |
|
|
323 | int i = perturb |
|
|
324 | ? rmg_rndm (max (0, seeds.size - 8), seeds.size - 1) |
|
|
325 | : rem_index ++ % seeds.size; |
|
|
326 | |
298 | point p = seeds.remove (rmg_rndm (seeds.size)); |
327 | point p = seeds.remove (i); |
299 | |
328 | |
300 | x = p.x; |
329 | x = p.x; |
301 | y = p.y; |
330 | y = p.y; |
302 | |
331 | |
303 | if (!dist [x][y]) |
332 | if (!dist [x][y]) |
304 | { |
333 | { |
305 | // found new isolated area, make tunnel |
334 | // found new isolated area, make tunnel |
306 | push_flood_fill (dist, seeds, x, y); |
335 | push_flood_fill (dist, seeds, x, y); |
307 | make_tunnel (dist, seeds, x, y, 255); |
336 | make_tunnel (dist, seeds, x, y, 254, perturb); |
308 | } |
337 | } |
309 | else |
338 | else |
310 | { |
339 | { |
311 | // nothing here, continue to expand |
340 | // nothing here, continue to expand |
312 | U8 d = U8 (dist [x][y]) + 1; |
341 | U8 d = U8 (dist [x][y]) + 1; |
313 | |
342 | |
314 | if (x < dist.w - 2) maybe_push (dist, seeds, x + 1, y, d); |
343 | if (x < dist.w - 1) maybe_push (dist, seeds, x + 1, y, d); |
315 | if (x > 1) maybe_push (dist, seeds, x - 1, y, d); |
344 | if (x > 0) maybe_push (dist, seeds, x - 1, y, d); |
316 | if (y < dist.h - 2) maybe_push (dist, seeds, x, y + 1, d); |
345 | if (y < dist.h - 1) maybe_push (dist, seeds, x, y + 1, d); |
317 | if (y > 1) maybe_push (dist, seeds, x, y - 1, d); |
346 | if (y > 0) maybe_push (dist, seeds, x, y - 1, d); |
318 | } |
347 | } |
319 | } |
348 | } |
320 | } |
349 | } |
321 | |
350 | |
322 | void |
351 | void |
323 | layout::isolation_remover () |
352 | layout::isolation_remover (int perturb) |
324 | { |
353 | { |
325 | layout dist (w, h); |
354 | layout dist (w - 2, h - 2); // map without border |
326 | |
355 | |
327 | for (int x = 1; x < w - 1; ++x) |
356 | for (int x = 1; x < w - 1; ++x) |
328 | for (int y = 1; y < h - 1; ++y) |
357 | for (int y = 1; y < h - 1; ++y) |
329 | dist [x][y] = data [x][y] == '#' ? U8 (255) : 0; |
358 | dist [x - 1][y - 1] = data [x][y] == '#' ? U8 (255) : 0; |
330 | |
359 | |
331 | ::isolation_remover (dist); |
360 | ::isolation_remover (dist, perturb); |
332 | |
361 | |
333 | // now copy the tunnels over |
362 | // now copy the tunnels over |
334 | for (int x = 1; x < w - 1; ++x) |
363 | for (int x = 1; x < w - 1; ++x) |
335 | for (int y = 1; y < h - 1; ++y) |
364 | for (int y = 1; y < h - 1; ++y) |
336 | if (data [x][y] == '#' && dist [x][y] == 1) |
365 | if (data [x][y] == '#' && dist [x - 1][y - 1] == 1) |
337 | data [x][y] = 0; |
366 | data [x][y] = 0; |
338 | } |
|
|
339 | |
|
|
340 | ///////////////////////////////////////////////////////////////////////////// |
|
|
341 | |
|
|
342 | // inspired mostly by http://www.jimrandomh.org/misc/caves.txt |
|
|
343 | void |
|
|
344 | layout::gen_cave (int subtype) |
|
|
345 | { |
|
|
346 | switch (subtype) |
|
|
347 | { |
|
|
348 | // a rough cave |
|
|
349 | case 0: |
|
|
350 | fill_rand (rmg_rndm (85, 97)); |
|
|
351 | break; |
|
|
352 | |
|
|
353 | // corridors |
|
|
354 | case 1: |
|
|
355 | fill_rand (rmg_rndm (5, 40)); |
|
|
356 | erode_1_2 (5, 2, 10); |
|
|
357 | erode_1_2 (5, -1, 10); |
|
|
358 | erode_1_2 (5, 2, 1); |
|
|
359 | break; |
|
|
360 | |
|
|
361 | // somewhat open, roundish |
|
|
362 | case 2: |
|
|
363 | fill_rand (45); |
|
|
364 | erode_1_2 (5, 0, 5); |
|
|
365 | erode_1_2 (5, 1, 1); |
|
|
366 | break; |
|
|
367 | |
|
|
368 | // wide open, some room-like structures |
|
|
369 | case 3: |
|
|
370 | fill_rand (45); |
|
|
371 | erode_1_2 (5, 2, 4); |
|
|
372 | erode_1_2 (5, -1, 3); |
|
|
373 | break; |
|
|
374 | } |
|
|
375 | |
|
|
376 | border (); |
|
|
377 | isolation_remover (); |
|
|
378 | } |
367 | } |
379 | |
368 | |
380 | ///////////////////////////////////////////////////////////////////////////// |
369 | ///////////////////////////////////////////////////////////////////////////// |
381 | |
370 | |
382 | //+GPL |
371 | //+GPL |
… | |
… | |
384 | /* puts doors at appropriate locations in a maze. */ |
373 | /* puts doors at appropriate locations in a maze. */ |
385 | void |
374 | void |
386 | layout::doorify () |
375 | layout::doorify () |
387 | { |
376 | { |
388 | int ndoors = w * h / 60; /* reasonable number of doors. */ |
377 | int ndoors = w * h / 60; /* reasonable number of doors. */ |
389 | int doorlocs = 0; /* # of available doorlocations */ |
|
|
390 | |
378 | |
391 | uint16 *doorlist_x = salloc<uint16> (w * h); |
379 | coroapi::cede_to_tick (); |
392 | uint16 *doorlist_y = salloc<uint16> (w * h); |
380 | |
|
|
381 | fixed_stack<point> doorloc (w * h); |
393 | |
382 | |
394 | /* make a list of possible door locations */ |
383 | /* make a list of possible door locations */ |
395 | for (int i = 1; i < w - 1; i++) |
384 | for (int i = 1; i < w - 1; i++) |
396 | for (int j = 1; j < h - 1; j++) |
385 | for (int j = 1; j < h - 1; j++) |
397 | { |
386 | { |
398 | int sindex = surround_flag (*this, i, j); |
387 | int sindex = surround_flag (*this, i, j); |
399 | |
388 | |
400 | if (sindex == 3 || sindex == 12) /* these are possible door sindex */ |
389 | if (sindex == 3 || sindex == 12) /* these are possible door sindex */ |
401 | { |
390 | doorloc.push (point (i, j)); |
402 | doorlist_x [doorlocs] = i; |
|
|
403 | doorlist_y [doorlocs] = j; |
|
|
404 | doorlocs++; |
|
|
405 | } |
|
|
406 | } |
391 | } |
407 | |
392 | |
408 | while (ndoors > 0 && doorlocs > 0) |
393 | while (ndoors && doorloc.size) |
409 | { |
394 | { |
410 | int di = rmg_rndm (doorlocs); |
395 | point p = doorloc.remove (rmg_rndm (doorloc.size)); |
411 | int i = doorlist_x [di]; |
396 | |
412 | int j = doorlist_y [di]; |
|
|
413 | int sindex = surround_flag (*this, i, j); |
397 | int sindex = surround_flag (*this, p.x, p.y); |
414 | |
398 | |
415 | if (sindex == 3 || sindex == 12) /* these are possible door sindex */ |
399 | if (sindex == 3 || sindex == 12) /* these are possible door sindex */ |
416 | { |
400 | { |
417 | data [i][j] = 'D'; |
401 | data [p.x][p.y] = 'D'; |
418 | ndoors--; |
402 | --ndoors; |
419 | } |
403 | } |
420 | |
|
|
421 | /* reduce the size of the list */ |
|
|
422 | doorlocs--; |
|
|
423 | doorlist_x[di] = doorlist_x [doorlocs]; |
|
|
424 | doorlist_y[di] = doorlist_y [doorlocs]; |
|
|
425 | } |
404 | } |
426 | |
|
|
427 | sfree (doorlist_x, w * h); |
|
|
428 | sfree (doorlist_y, w * h); |
|
|
429 | } |
405 | } |
430 | |
406 | |
431 | /* takes a map and makes it symmetric: adjusts Xsize and |
407 | /* takes a map and makes it symmetric: adjusts Xsize and |
432 | * Ysize to produce a symmetric map. |
408 | * Ysize to produce a symmetric map. |
433 | */ |
409 | */ |
… | |
… | |
491 | //-GPL |
467 | //-GPL |
492 | |
468 | |
493 | void |
469 | void |
494 | layout::rotate (int rotation) |
470 | layout::rotate (int rotation) |
495 | { |
471 | { |
|
|
472 | coroapi::cede_to_tick (); |
|
|
473 | |
496 | switch (rotation & 3) |
474 | switch (rotation & 3) |
497 | { |
475 | { |
498 | case 2: /* a reflection */ |
476 | case 2: /* a reflection */ |
499 | { |
477 | { |
500 | layout new_layout (w, h); |
478 | layout new_layout (w, h); |
… | |
… | |
652 | { |
630 | { |
653 | layout new_layout (w * 2 - 1, h * 2 - 1); |
631 | layout new_layout (w * 2 - 1, h * 2 - 1); |
654 | |
632 | |
655 | new_layout.clear (); |
633 | new_layout.clear (); |
656 | |
634 | |
|
|
635 | coroapi::cede_to_tick (); |
|
|
636 | |
657 | for (int i = 0; i < w; i++) |
637 | for (int i = 0; i < w; i++) |
658 | for (int j = 0; j < h; j++) |
638 | for (int j = 0; j < h; j++) |
659 | switch (data [i][j]) |
639 | switch (data [i][j]) |
660 | { |
640 | { |
661 | case '#': expand_wall (new_layout, i, j, *this); break; |
641 | case '#': expand_wall (new_layout, i, j, *this); break; |
… | |
… | |
743 | |
723 | |
744 | return -1; |
724 | return -1; |
745 | } |
725 | } |
746 | |
726 | |
747 | int |
727 | int |
748 | make_wall (char **maze, int x, int y, int dir) |
728 | make_wall (layout &maze, int x, int y, int dir) |
749 | { |
729 | { |
750 | maze[x][y] = 'D'; /* mark a door */ |
730 | maze[x][y] = 'D'; /* mark a door */ |
751 | |
731 | |
752 | switch (dir) |
732 | switch (dir) |
753 | { |
733 | { |
… | |
… | |
770 | |
750 | |
771 | void |
751 | void |
772 | layout::roomify () |
752 | layout::roomify () |
773 | { |
753 | { |
774 | int tries = w * h / 30; |
754 | int tries = w * h / 30; |
|
|
755 | |
|
|
756 | coroapi::cede_to_tick (); |
775 | |
757 | |
776 | for (int ti = 0; ti < tries; ti++) |
758 | for (int ti = 0; ti < tries; ti++) |
777 | { |
759 | { |
778 | /* starting location for looking at creating a door */ |
760 | /* starting location for looking at creating a door */ |
779 | int dx = rmg_rndm (w); |
761 | int dx = rmg_rndm (w); |
… | |
… | |
802 | else |
784 | else |
803 | make_wall (*this, dx, dy, 1); |
785 | make_wall (*this, dx, dy, 1); |
804 | } |
786 | } |
805 | } |
787 | } |
806 | |
788 | |
|
|
789 | //-GPL |
|
|
790 | |
807 | ///////////////////////////////////////////////////////////////////////////// |
791 | ///////////////////////////////////////////////////////////////////////////// |
|
|
792 | |
|
|
793 | // inspired mostly by http://www.jimrandomh.org/misc/caves.txt |
|
|
794 | void |
|
|
795 | layout::gen_cave (int subtype) |
|
|
796 | { |
|
|
797 | switch (subtype) |
|
|
798 | { |
|
|
799 | // a rough cave |
|
|
800 | case 0: |
|
|
801 | fill_rand (rmg_rndm (85, 97)); |
|
|
802 | break; |
|
|
803 | |
|
|
804 | // corridors |
|
|
805 | case 1: |
|
|
806 | fill_rand (rmg_rndm (5, 40)); |
|
|
807 | erode_1_2 (5, 2, 10); |
|
|
808 | erode_1_2 (5, -1, 10); |
|
|
809 | erode_1_2 (5, 2, 1); |
|
|
810 | break; |
|
|
811 | |
|
|
812 | // somewhat open, some room-like structures |
|
|
813 | case 2: |
|
|
814 | fill_rand (45); |
|
|
815 | erode_1_2 (5, 2, 4); |
|
|
816 | erode_1_2 (5, -1, 3); |
|
|
817 | break; |
|
|
818 | |
|
|
819 | // wide open, roundish |
|
|
820 | case 3: |
|
|
821 | fill_rand (45); |
|
|
822 | erode_1_2 (5, 0, 5); |
|
|
823 | erode_1_2 (5, 1, 1); |
|
|
824 | break; |
|
|
825 | } |
|
|
826 | |
|
|
827 | border (); |
|
|
828 | isolation_remover (1); |
|
|
829 | } |
|
|
830 | |
|
|
831 | void |
|
|
832 | layout::gen_castle () |
|
|
833 | { |
|
|
834 | fill ('#'); |
|
|
835 | |
|
|
836 | for (int n = w * h / 30 + 1; n--; ) |
|
|
837 | { |
|
|
838 | int rw = rmg_rndm (6, 10); |
|
|
839 | int rh = rmg_rndm (6, 10); |
|
|
840 | |
|
|
841 | if (rw > w || rh > h) |
|
|
842 | continue; |
|
|
843 | |
|
|
844 | int rx = rmg_rndm (0, w - rw); |
|
|
845 | int ry = rmg_rndm (0, h - rh); |
|
|
846 | |
|
|
847 | rect (rx, ry, rx + rw, ry + rh, '#'); |
|
|
848 | fill_rect (rx + 1, ry + 1, rx + rw - 1, ry + rh - 1, 0); |
|
|
849 | } |
|
|
850 | |
|
|
851 | border (); |
|
|
852 | isolation_remover (0); |
|
|
853 | } |
|
|
854 | |
|
|
855 | static void |
|
|
856 | gen_mixed_ (layout &maze, random_map_params *RP) |
|
|
857 | { |
|
|
858 | if (maze.w > maze.h && maze.w > 16) |
|
|
859 | { |
|
|
860 | int m = rmg_rndm (8, maze.w - 8); |
|
|
861 | |
|
|
862 | layout m1 (maze, 0, 0, m , maze.h); gen_mixed_ (m1, RP); |
|
|
863 | layout m2 (maze, m, 0, maze.w, maze.h); gen_mixed_ (m2, RP); |
|
|
864 | } |
|
|
865 | else if (maze.h > 16) |
|
|
866 | { |
|
|
867 | int m = rmg_rndm (8, maze.h - 8); |
|
|
868 | |
|
|
869 | layout m1 (maze, 0, 0, maze.w, m ); gen_mixed_ (m1, RP); |
|
|
870 | layout m2 (maze, 0, m, maze.w, maze.h); gen_mixed_ (m2, RP); |
|
|
871 | } |
|
|
872 | else |
|
|
873 | { |
|
|
874 | RP->map_layout_style = rmg_rndm (NROFLAYOUTS - 2) + 1; |
|
|
875 | |
|
|
876 | if (RP->map_layout_style == LAYOUT_MULTIPLE) |
|
|
877 | ++RP->map_layout_style; |
|
|
878 | |
|
|
879 | maze.generate (RP); |
|
|
880 | } |
|
|
881 | |
|
|
882 | coroapi::cede_to_tick (); |
|
|
883 | } |
|
|
884 | |
|
|
885 | // recursive subdivision with random sublayouts |
|
|
886 | static void |
|
|
887 | gen_mixed (layout &maze, random_map_params *RP) |
|
|
888 | { |
|
|
889 | random_map_params &rp = *new random_map_params (RP); |
|
|
890 | gen_mixed_ (maze, &rp); |
|
|
891 | delete &rp; |
|
|
892 | |
|
|
893 | maze.border (); |
|
|
894 | |
|
|
895 | // exits currently do not work so well, as they |
|
|
896 | // are currently often found together, so nuke entrances |
|
|
897 | maze.replace ('<', ' '); |
|
|
898 | |
|
|
899 | maze.isolation_remover (0); |
|
|
900 | } |
|
|
901 | |
|
|
902 | //+GPL |
808 | |
903 | |
809 | /* function selects the maze function and gives it whatever |
904 | /* function selects the maze function and gives it whatever |
810 | arguments it needs. */ |
905 | arguments it needs. */ |
811 | void |
906 | void |
812 | layout::generate (random_map_params *RP) |
907 | layout::generate (random_map_params *RP) |
… | |
… | |
872 | if (rmg_rndm (2)) |
967 | if (rmg_rndm (2)) |
873 | doorify (); |
968 | doorify (); |
874 | |
969 | |
875 | break; |
970 | break; |
876 | |
971 | |
|
|
972 | case LAYOUT_CASTLE: |
|
|
973 | gen_castle (); |
|
|
974 | |
|
|
975 | if (rmg_rndm (2)) |
|
|
976 | doorify (); |
|
|
977 | |
|
|
978 | break; |
|
|
979 | |
|
|
980 | case LAYOUT_MULTIPLE: |
|
|
981 | gen_mixed (*this, RP); |
|
|
982 | break; |
|
|
983 | |
877 | default: |
984 | default: |
878 | abort (); |
985 | abort (); |
879 | } |
986 | } |
|
|
987 | } |
880 | |
988 | |
881 | /* rotate the maze randomly */ |
989 | //-GPL |
882 | rotate (rmg_rndm (4)); |
|
|
883 | |
|
|
884 | symmetrize (RP->symmetry_used); |
|
|
885 | |
990 | |
886 | #if 0 |
991 | #if 0 |
887 | print ();//D |
992 | static void |
888 | #endif |
993 | gen_village (layout &maze) |
|
|
994 | { |
|
|
995 | maze.clear (); |
|
|
996 | maze.border (); |
889 | |
997 | |
890 | if (RP->expand2x) |
998 | for (int n = maze.w * maze.h / 200 + 1; n--; ) |
891 | expand2x (); |
999 | { |
892 | } |
1000 | int rw = rmg_rndm (6, 10); |
|
|
1001 | int rh = rmg_rndm (6, 10); |
893 | |
1002 | |
894 | //-GPL |
1003 | int rx = rmg_rndm (2, maze.w - rw - 2); |
|
|
1004 | int ry = rmg_rndm (2, maze.h - rh - 2); |
895 | |
1005 | |
896 | #if 0 |
1006 | maze.rect (rx, ry, rx + rw, ry + rh, '#'); |
|
|
1007 | } |
|
|
1008 | |
|
|
1009 | maze.border (); |
|
|
1010 | maze.isolation_remover (2); |
|
|
1011 | } |
|
|
1012 | |
897 | static struct demo |
1013 | static struct demo |
898 | { |
1014 | { |
899 | demo () |
1015 | demo () |
900 | { |
1016 | { |
901 | rmg_rndm.seed (time (0)); |
1017 | rmg_rndm.seed (time (0)); |
902 | |
1018 | |
903 | for(int i=1;i<100;i++) |
1019 | for(int i=1;i<100;i++) |
904 | { |
1020 | { |
905 | layout maze (40, 25); |
1021 | layout maze (40, 30); |
906 | maze.fill_rand (85); |
1022 | maze.fill_rand (99); |
907 | maze.border (); |
1023 | maze.border (); |
908 | maze.isolation_remover (); |
1024 | maze.isolation_remover (2); |
909 | maze.print (); |
1025 | maze.print (); |
910 | } |
1026 | } |
911 | |
1027 | |
912 | exit (1); |
1028 | exit (1); |
913 | } |
1029 | } |