1 | /* |
1 | /* |
2 | * This file is part of Deliantra, the Roguelike Realtime MMORPG. |
2 | * This file is part of Deliantra, the Roguelike Realtime MMORPG. |
3 | * |
3 | * |
4 | * Copyright (©) 2010 Marc Alexander Lehmann / Robin Redeker / the Deliantra team |
4 | * Copyright (©) 2010,2011,2012,2013,2014,2015,2016 Marc Alexander Lehmann / Robin Redeker / the Deliantra team |
5 | * Copyright (©) Crossfire Development Team (restored, original file without copyright notice) |
5 | * Copyright (©) 1994-2004 Crossfire Development Team (restored, original file without copyright notice) |
6 | * |
6 | * |
7 | * Deliantra is free software: you can redistribute it and/or modify it under |
7 | * Deliantra is free software: you can redistribute it and/or modify it under |
8 | * the terms of the Affero GNU General Public License as published by the |
8 | * the terms of the Affero GNU General Public License as published by the |
9 | * Free Software Foundation, either version 3 of the License, or (at your |
9 | * Free Software Foundation, either version 3 of the License, or (at your |
10 | * option) any later version. |
10 | * option) any later version. |
11 | * |
11 | * |
12 | * This program is distributed in the hope that it will be useful, |
12 | * This program is distributed in the hope that it will be useful, |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 | * GNU General Public License for more details. |
15 | * GNU General Public License for more details. |
16 | * |
16 | * |
17 | * You should have received a copy of the Affero GNU General Public License |
17 | * You should have received a copy of the Affero GNU General Public License |
18 | * and the GNU General Public License along with this program. If not, see |
18 | * and the GNU General Public License along with this program. If not, see |
19 | * <http://www.gnu.org/licenses/>. |
19 | * <http://www.gnu.org/licenses/>. |
20 | * |
20 | * |
21 | * The authors can be reached via e-mail to <support@deliantra.net> |
21 | * The authors can be reached via e-mail to <support@deliantra.net> |
22 | */ |
22 | */ |
23 | |
23 | |
24 | #include <global.h> |
24 | #include <global.h> |
25 | #include <random_map.h> |
25 | #include <rmg.h> |
26 | #include <rproto.h> |
26 | #include <rproto.h> |
27 | |
27 | |
28 | void |
28 | ecb_noinline void |
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; |
… | |
… | |
74 | layout::~layout () |
74 | layout::~layout () |
75 | { |
75 | { |
76 | sfree ((char *)data, size); |
76 | sfree ((char *)data, size); |
77 | } |
77 | } |
78 | |
78 | |
79 | void |
79 | ecb_noinline void |
80 | layout::fill (char fill) |
80 | layout::fill (char fill) |
81 | { |
81 | { |
82 | //memset (data [0], fill, w * h); // only when contiguous :/ |
82 | //memset (data [0], fill, w * h); // only when contiguous :/ |
83 | fill_rect (0, 0, w, h, fill); |
83 | fill_rect (0, 0, w, h, fill); |
84 | } |
84 | } |
85 | |
85 | |
86 | void |
86 | ecb_noinline void |
|
|
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 | ecb_noinline void |
87 | 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) |
88 | { |
97 | { |
89 | --x2; |
98 | --x2; |
90 | |
99 | |
91 | memset (data [x1] + y1, fill, y2 - y1); |
100 | memset (data [x1] + y1, fill, y2 - y1); |
… | |
… | |
93 | |
102 | |
94 | while (++x1 < x2) |
103 | while (++x1 < x2) |
95 | data [x1][y1] = data [x1][y2 - 1] = fill; |
104 | data [x1][y1] = data [x1][y2 - 1] = fill; |
96 | } |
105 | } |
97 | |
106 | |
98 | void |
107 | ecb_noinline void |
99 | 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) |
100 | { |
109 | { |
101 | for (; x1 < x2; ++x1) |
110 | for (; x1 < x2; ++x1) |
102 | memset (data [x1] + y1, fill, y2 - y1); |
111 | memset (data [x1] + y1, fill, y2 - y1); |
103 | } |
112 | } |
104 | |
113 | |
|
|
114 | void |
105 | void layout::border (char fill) |
115 | layout::border (char fill) |
106 | { |
116 | { |
107 | rect (0, 0, w, h, fill); |
117 | rect (0, 0, w, h, fill); |
108 | } |
118 | } |
109 | |
119 | |
110 | void |
120 | ecb_noinline void |
111 | layout::fill_rand (int percent) |
121 | layout::fill_rand (int percent) |
112 | { |
122 | { |
113 | percent = lerp (percent, 0, 100, 0, 256); |
123 | percent = lerp (percent, 0, 100, 0, 256); |
114 | |
124 | |
115 | for (int x = w - 1; --x > 0; ) |
125 | for (int x = 0; x < w; ++x) |
116 | for (int y = h - 1; --y > 0; ) |
126 | for (int y = 0; y < h; ++y) |
117 | data [x][y] = rmg_rndm (256) > percent ? 0 : '#'; |
127 | data [x][y] = rmg_rndm (256) > percent ? 0 : '#'; |
118 | } |
128 | } |
119 | |
129 | |
120 | ///////////////////////////////////////////////////////////////////////////// |
130 | ///////////////////////////////////////////////////////////////////////////// |
121 | |
131 | |
122 | // erode by cellular automata |
132 | // erode by cellular automata |
123 | void |
133 | ecb_noinline void |
124 | layout::erode_1_2 (int c1, int c2, int repeat) |
134 | layout::erode_1_2 (int c1, int c2, int repeat) |
125 | { |
135 | { |
126 | layout neu (w, h); |
136 | layout neu (w, h); |
127 | |
137 | |
128 | while (repeat--) |
138 | while (repeat--) |
… | |
… | |
194 | ///////////////////////////////////////////////////////////////////////////// |
204 | ///////////////////////////////////////////////////////////////////////////// |
195 | // isolation remover - ensures single connected area |
205 | // isolation remover - ensures single connected area |
196 | |
206 | |
197 | typedef fixed_stack<point> pointlist; |
207 | typedef fixed_stack<point> pointlist; |
198 | |
208 | |
199 | static void noinline |
209 | ecb_noinline static void |
200 | push_flood_fill (layout &dist, pointlist &seeds, int x, int y) |
210 | push_flood_fill (layout &dist, pointlist &seeds, int x, int y) |
201 | { |
211 | { |
202 | if (dist [x][y]) |
212 | if (dist [x][y]) |
203 | return; |
213 | return; |
204 | |
214 | |
… | |
… | |
220 | 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); |
221 | 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); |
222 | } |
232 | } |
223 | } |
233 | } |
224 | |
234 | |
225 | static inline void |
235 | static void inline |
226 | 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) |
227 | { |
237 | { |
228 | for (;;) |
238 | for (;;) |
229 | { |
239 | { |
230 | point neigh[4]; |
240 | point neigh[4]; |
231 | int ncnt = 0; |
241 | int ncnt = 0; |
232 | |
242 | |
|
|
243 | d += perturb > 1; |
|
|
244 | |
233 | if (x > 0 && 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); |
234 | if (x < dist.w - 1 && 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); |
235 | if (y > 0 && 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); |
236 | if (y < dist.h - 1 && 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); |
237 | |
249 | |
238 | if (!ncnt) |
250 | if (!ncnt) |
239 | return; |
251 | return; |
240 | |
252 | |
241 | point &p = neigh [rmg_rndm (ncnt)]; |
253 | point p = neigh [perturb ? rmg_rndm (ncnt) : 0]; |
242 | |
254 | |
243 | seeds.push (p); |
255 | seeds.push (p); |
244 | |
256 | |
245 | x = p.x; |
257 | x = p.x; |
246 | y = p.y; |
258 | y = p.y; |
… | |
… | |
263 | seeds.push (point (x, y)); |
275 | seeds.push (point (x, y)); |
264 | } |
276 | } |
265 | |
277 | |
266 | // isolation remover, works on a "distance" map |
278 | // isolation remover, works on a "distance" map |
267 | // the map must be initialised with 0 == rooms, 255 = walls |
279 | // the map must be initialised with 0 == rooms, 255 = walls |
268 | static void noinline |
280 | ecb_noinline static void |
269 | isolation_remover (layout &dist) |
281 | isolation_remover (layout &dist, unsigned int perturb = 2) |
270 | { |
282 | { |
271 | // dist contains |
283 | // dist contains |
272 | // 0 == invisited rooms |
284 | // 0 == invisited rooms |
273 | // 1 == visited rooms |
285 | // 1 == visited rooms |
274 | // 2+ shortest distance to random near room |
286 | // 2+ shortest distance to random near room |
275 | |
287 | |
|
|
288 | clamp_it (perturb, 0, 2); |
|
|
289 | |
276 | // phase 1, find seed |
290 | // phase 1, find seed |
277 | int cnt = 0; |
291 | int cnt = 0; |
278 | int x, y; |
292 | int x, y; |
279 | |
293 | |
280 | for (int i = 0; i < dist.w; ++i) |
294 | for (int i = 0; i < dist.w; ++i) |
… | |
… | |
298 | seeds.push (point (x, y)); |
312 | seeds.push (point (x, y)); |
299 | |
313 | |
300 | // phase 2, while we have seeds, if |
314 | // phase 2, while we have seeds, if |
301 | // seed is empty, floodfill, else grow |
315 | // seed is empty, floodfill, else grow |
302 | |
316 | |
|
|
317 | int rem_index = 0; // used to remove "somewhat ordered" |
|
|
318 | |
303 | while (seeds.size) |
319 | while (seeds.size) |
304 | { |
320 | { |
305 | coroapi::cede_to_tick (); |
321 | coroapi::cede_to_tick (); |
306 | |
322 | |
|
|
323 | int i = perturb |
|
|
324 | ? rmg_rndm (max (0, seeds.size - 8), seeds.size - 1) |
|
|
325 | : rem_index ++ % seeds.size; |
|
|
326 | |
307 | point p = seeds.remove (rmg_rndm (seeds.size)); |
327 | point p = seeds.remove (i); |
308 | |
328 | |
309 | x = p.x; |
329 | x = p.x; |
310 | y = p.y; |
330 | y = p.y; |
311 | |
331 | |
312 | if (!dist [x][y]) |
332 | if (!dist [x][y]) |
313 | { |
333 | { |
314 | // found new isolated area, make tunnel |
334 | // found new isolated area, make tunnel |
315 | push_flood_fill (dist, seeds, x, y); |
335 | push_flood_fill (dist, seeds, x, y); |
316 | make_tunnel (dist, seeds, x, y, 255); |
336 | make_tunnel (dist, seeds, x, y, 254, perturb); |
317 | } |
337 | } |
318 | else |
338 | else |
319 | { |
339 | { |
320 | // nothing here, continue to expand |
340 | // nothing here, continue to expand |
321 | U8 d = U8 (dist [x][y]) + 1; |
341 | U8 d = U8 (dist [x][y]) + 1; |
… | |
… | |
327 | } |
347 | } |
328 | } |
348 | } |
329 | } |
349 | } |
330 | |
350 | |
331 | void |
351 | void |
332 | layout::isolation_remover () |
352 | layout::isolation_remover (int perturb) |
333 | { |
353 | { |
334 | layout dist (w - 2, h - 2); // map without border |
354 | layout dist (w - 2, h - 2); // map without border |
335 | |
355 | |
336 | for (int x = 1; x < w - 1; ++x) |
356 | for (int x = 1; x < w - 1; ++x) |
337 | for (int y = 1; y < h - 1; ++y) |
357 | for (int y = 1; y < h - 1; ++y) |
338 | dist [x - 1][y - 1] = data [x][y] == '#' ? U8 (255) : 0; |
358 | dist [x - 1][y - 1] = data [x][y] == '#' ? U8 (255) : 0; |
339 | |
359 | |
340 | ::isolation_remover (dist); |
360 | ::isolation_remover (dist, perturb); |
341 | |
361 | |
342 | // now copy the tunnels over |
362 | // now copy the tunnels over |
343 | for (int x = 1; x < w - 1; ++x) |
363 | for (int x = 1; x < w - 1; ++x) |
344 | for (int y = 1; y < h - 1; ++y) |
364 | for (int y = 1; y < h - 1; ++y) |
345 | if (data [x][y] == '#' && dist [x - 1][y - 1] == 1) |
365 | if (data [x][y] == '#' && dist [x - 1][y - 1] == 1) |
… | |
… | |
353 | /* puts doors at appropriate locations in a maze. */ |
373 | /* puts doors at appropriate locations in a maze. */ |
354 | void |
374 | void |
355 | layout::doorify () |
375 | layout::doorify () |
356 | { |
376 | { |
357 | int ndoors = w * h / 60; /* reasonable number of doors. */ |
377 | int ndoors = w * h / 60; /* reasonable number of doors. */ |
358 | int doorlocs = 0; /* # of available doorlocations */ |
|
|
359 | |
378 | |
360 | uint16 *doorlist_x = salloc<uint16> (w * h); |
379 | coroapi::cede_to_tick (); |
361 | uint16 *doorlist_y = salloc<uint16> (w * h); |
380 | |
|
|
381 | fixed_stack<point> doorloc (w * h); |
362 | |
382 | |
363 | /* make a list of possible door locations */ |
383 | /* make a list of possible door locations */ |
364 | for (int i = 1; i < w - 1; i++) |
384 | for (int i = 1; i < w - 1; i++) |
365 | for (int j = 1; j < h - 1; j++) |
385 | for (int j = 1; j < h - 1; j++) |
366 | { |
386 | { |
367 | int sindex = surround_flag (*this, i, j); |
387 | int sindex = surround_flag (*this, i, j); |
368 | |
388 | |
369 | if (sindex == 3 || sindex == 12) /* these are possible door sindex */ |
389 | if (sindex == 3 || sindex == 12) /* these are possible door sindex */ |
370 | { |
390 | doorloc.push (point (i, j)); |
371 | doorlist_x [doorlocs] = i; |
|
|
372 | doorlist_y [doorlocs] = j; |
|
|
373 | doorlocs++; |
|
|
374 | } |
|
|
375 | } |
391 | } |
376 | |
392 | |
377 | while (ndoors > 0 && doorlocs > 0) |
393 | while (ndoors && doorloc.size) |
378 | { |
394 | { |
379 | int di = rmg_rndm (doorlocs); |
395 | point p = doorloc.remove (rmg_rndm (doorloc.size)); |
380 | int i = doorlist_x [di]; |
396 | |
381 | int j = doorlist_y [di]; |
|
|
382 | int sindex = surround_flag (*this, i, j); |
397 | int sindex = surround_flag (*this, p.x, p.y); |
383 | |
398 | |
384 | if (sindex == 3 || sindex == 12) /* these are possible door sindex */ |
399 | if (sindex == 3 || sindex == 12) /* these are possible door sindex */ |
385 | { |
400 | { |
386 | data [i][j] = 'D'; |
401 | data [p.x][p.y] = 'D'; |
387 | ndoors--; |
402 | --ndoors; |
388 | } |
403 | } |
389 | |
|
|
390 | /* reduce the size of the list */ |
|
|
391 | doorlocs--; |
|
|
392 | doorlist_x[di] = doorlist_x [doorlocs]; |
|
|
393 | doorlist_y[di] = doorlist_y [doorlocs]; |
|
|
394 | } |
404 | } |
395 | |
|
|
396 | sfree (doorlist_x, w * h); |
|
|
397 | sfree (doorlist_y, w * h); |
|
|
398 | } |
405 | } |
399 | |
406 | |
400 | /* takes a map and makes it symmetric: adjusts Xsize and |
407 | /* takes a map and makes it symmetric: adjusts Xsize and |
401 | * Ysize to produce a symmetric map. |
408 | * Ysize to produce a symmetric map. |
402 | */ |
409 | */ |
… | |
… | |
460 | //-GPL |
467 | //-GPL |
461 | |
468 | |
462 | void |
469 | void |
463 | layout::rotate (int rotation) |
470 | layout::rotate (int rotation) |
464 | { |
471 | { |
|
|
472 | coroapi::cede_to_tick (); |
|
|
473 | |
465 | switch (rotation & 3) |
474 | switch (rotation & 3) |
466 | { |
475 | { |
467 | case 2: /* a reflection */ |
476 | case 2: /* a reflection */ |
468 | { |
477 | { |
469 | layout new_layout (w, h); |
478 | layout new_layout (w, h); |
… | |
… | |
524 | * 1 match on (i+1, j) |
533 | * 1 match on (i+1, j) |
525 | * 2 match on (i, j+1) |
534 | * 2 match on (i, j+1) |
526 | * 4 match on (i+1, j+1) |
535 | * 4 match on (i+1, j+1) |
527 | * and the possible combinations thereof. |
536 | * and the possible combinations thereof. |
528 | */ |
537 | */ |
529 | static int noinline |
538 | ecb_noinline static int |
530 | calc_pattern (char ch, layout &maze, int i, int j) |
539 | calc_pattern (char ch, layout &maze, int i, int j) |
531 | { |
540 | { |
532 | int pattern = 0; |
541 | int pattern = 0; |
533 | |
542 | |
534 | if (i + 1 < maze.w && maze[i + 1][j] == ch) |
543 | if (i + 1 < maze.w && maze[i + 1][j] == ch) |
… | |
… | |
621 | { |
630 | { |
622 | layout new_layout (w * 2 - 1, h * 2 - 1); |
631 | layout new_layout (w * 2 - 1, h * 2 - 1); |
623 | |
632 | |
624 | new_layout.clear (); |
633 | new_layout.clear (); |
625 | |
634 | |
|
|
635 | coroapi::cede_to_tick (); |
|
|
636 | |
626 | for (int i = 0; i < w; i++) |
637 | for (int i = 0; i < w; i++) |
627 | for (int j = 0; j < h; j++) |
638 | for (int j = 0; j < h; j++) |
628 | switch (data [i][j]) |
639 | switch (data [i][j]) |
629 | { |
640 | { |
630 | case '#': expand_wall (new_layout, i, j, *this); break; |
641 | case '#': expand_wall (new_layout, i, j, *this); break; |
631 | case 'D': expand_door (new_layout, i, j, *this); break; |
642 | case 'D': expand_door (new_layout, i, j, *this); break; |
632 | default: expand_misc (new_layout, i, j, *this); break; |
643 | default: expand_misc (new_layout, i, j, *this); break; |
633 | } |
644 | } |
634 | |
645 | |
635 | swap (new_layout); |
646 | swap (new_layout); |
636 | } |
647 | } |
… | |
… | |
712 | |
723 | |
713 | return -1; |
724 | return -1; |
714 | } |
725 | } |
715 | |
726 | |
716 | int |
727 | int |
717 | make_wall (char **maze, int x, int y, int dir) |
728 | make_wall (layout &maze, int x, int y, int dir) |
718 | { |
729 | { |
719 | maze[x][y] = 'D'; /* mark a door */ |
730 | maze[x][y] = 'D'; /* mark a door */ |
720 | |
731 | |
721 | switch (dir) |
732 | switch (dir) |
722 | { |
733 | { |
… | |
… | |
739 | |
750 | |
740 | void |
751 | void |
741 | layout::roomify () |
752 | layout::roomify () |
742 | { |
753 | { |
743 | int tries = w * h / 30; |
754 | int tries = w * h / 30; |
|
|
755 | |
|
|
756 | coroapi::cede_to_tick (); |
744 | |
757 | |
745 | for (int ti = 0; ti < tries; ti++) |
758 | for (int ti = 0; ti < tries; ti++) |
746 | { |
759 | { |
747 | /* starting location for looking at creating a door */ |
760 | /* starting location for looking at creating a door */ |
748 | int dx = rmg_rndm (w); |
761 | int dx = rmg_rndm (w); |
… | |
… | |
770 | make_wall (*this, dx, dy, 0); |
783 | make_wall (*this, dx, dy, 0); |
771 | else |
784 | else |
772 | make_wall (*this, dx, dy, 1); |
785 | make_wall (*this, dx, dy, 1); |
773 | } |
786 | } |
774 | } |
787 | } |
|
|
788 | |
|
|
789 | //-GPL |
775 | |
790 | |
776 | ///////////////////////////////////////////////////////////////////////////// |
791 | ///////////////////////////////////////////////////////////////////////////// |
777 | |
792 | |
778 | // inspired mostly by http://www.jimrandomh.org/misc/caves.txt |
793 | // inspired mostly by http://www.jimrandomh.org/misc/caves.txt |
779 | void |
794 | void |
… | |
… | |
792 | erode_1_2 (5, 2, 10); |
807 | erode_1_2 (5, 2, 10); |
793 | erode_1_2 (5, -1, 10); |
808 | erode_1_2 (5, -1, 10); |
794 | erode_1_2 (5, 2, 1); |
809 | erode_1_2 (5, 2, 1); |
795 | break; |
810 | break; |
796 | |
811 | |
797 | // somewhat open, roundish |
812 | // somewhat open, some room-like structures |
798 | case 2: |
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: |
799 | fill_rand (45); |
821 | fill_rand (45); |
800 | erode_1_2 (5, 0, 5); |
822 | erode_1_2 (5, 0, 5); |
801 | erode_1_2 (5, 1, 1); |
823 | erode_1_2 (5, 1, 1); |
802 | break; |
824 | break; |
803 | |
|
|
804 | // wide open, some room-like structures |
|
|
805 | case 3: |
|
|
806 | fill_rand (45); |
|
|
807 | erode_1_2 (5, 2, 4); |
|
|
808 | erode_1_2 (5, -1, 3); |
|
|
809 | break; |
|
|
810 | } |
825 | } |
811 | |
826 | |
812 | border (); |
827 | border (); |
813 | isolation_remover (); |
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); |
814 | } |
853 | } |
815 | |
854 | |
816 | static void |
855 | static void |
817 | gen_mixed_ (layout &maze, random_map_params *RP, int dir) |
856 | gen_mixed_ (layout &maze, random_map_params *RP) |
818 | { |
857 | { |
819 | if (maze.w < 20 && maze.h < 20 && !rmg_rndm (3)) |
858 | if (maze.w > maze.h && maze.w > 16) |
820 | dir = 2; // stop recursion randomly |
|
|
821 | |
|
|
822 | if (dir == 0 && maze.w > 16) |
|
|
823 | { |
859 | { |
824 | int m = rmg_rndm (8, maze.w - 8); |
860 | int m = rmg_rndm (8, maze.w - 8); |
825 | |
861 | |
826 | layout m1 (maze, 0, 0, m , maze.h); gen_mixed_ (m1, RP, !dir); |
862 | layout m1 (maze, 0, 0, m , maze.h); gen_mixed_ (m1, RP); |
827 | layout m2 (maze, m, 0, maze.w, maze.h); gen_mixed_ (m2, RP, !dir); |
863 | layout m2 (maze, m, 0, maze.w, maze.h); gen_mixed_ (m2, RP); |
828 | } |
864 | } |
829 | else if (dir == 1 && maze.h > 16) |
865 | else if (maze.h > 16) |
830 | { |
866 | { |
831 | int m = rmg_rndm (8, maze.h - 8); |
867 | int m = rmg_rndm (8, maze.h - 8); |
832 | |
868 | |
833 | layout m1 (maze, 0, 0, maze.w, m ); gen_mixed_ (m1, RP, !dir); |
869 | layout m1 (maze, 0, 0, maze.w, m ); gen_mixed_ (m1, RP); |
834 | layout m2 (maze, 0, m, maze.w, maze.h); gen_mixed_ (m2, RP, !dir); |
870 | layout m2 (maze, 0, m, maze.w, maze.h); gen_mixed_ (m2, RP); |
835 | } |
871 | } |
836 | else |
872 | else |
837 | { |
873 | { |
838 | RP->map_layout_style = rmg_rndm (NROFLAYOUTS - 2) + 1; |
874 | RP->map_layout_style = rmg_rndm (NROFLAYOUTS - 2) + 1; |
839 | |
875 | |
840 | if (RP->map_layout_style == LAYOUT_MULTIPLE) |
876 | if (RP->map_layout_style == LAYOUT_MULTIPLE) |
841 | ++RP->map_layout_style; |
877 | ++RP->map_layout_style; |
842 | |
878 | |
843 | maze.generate (RP); |
879 | maze.generate (RP); |
844 | } |
880 | } |
|
|
881 | |
|
|
882 | coroapi::cede_to_tick (); |
845 | } |
883 | } |
846 | |
884 | |
847 | // recursive subdivision with random sublayouts |
885 | // recursive subdivision with random sublayouts |
848 | static void |
886 | static void |
849 | gen_mixed (layout &maze, random_map_params *RP) |
887 | gen_mixed (layout &maze, random_map_params *RP) |
850 | { |
888 | { |
851 | random_map_params &rp = *new random_map_params (RP); |
889 | random_map_params &rp = *new random_map_params (RP); |
852 | gen_mixed_ (maze, &rp, rmg_rndm (2)); |
890 | gen_mixed_ (maze, &rp); |
853 | delete &rp; |
891 | delete &rp; |
854 | |
892 | |
855 | maze.border (); |
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 | |
856 | maze.isolation_remover (); |
899 | maze.isolation_remover (0); |
857 | } |
900 | } |
|
|
901 | |
|
|
902 | //+GPL |
858 | |
903 | |
859 | /* function selects the maze function and gives it whatever |
904 | /* function selects the maze function and gives it whatever |
860 | arguments it needs. */ |
905 | arguments it needs. */ |
861 | void |
906 | void |
862 | layout::generate (random_map_params *RP) |
907 | layout::generate (random_map_params *RP) |
… | |
… | |
922 | if (rmg_rndm (2)) |
967 | if (rmg_rndm (2)) |
923 | doorify (); |
968 | doorify (); |
924 | |
969 | |
925 | break; |
970 | break; |
926 | |
971 | |
|
|
972 | case LAYOUT_CASTLE: |
|
|
973 | gen_castle (); |
|
|
974 | |
|
|
975 | if (rmg_rndm (2)) |
|
|
976 | doorify (); |
|
|
977 | |
|
|
978 | break; |
|
|
979 | |
927 | case LAYOUT_MULTIPLE: |
980 | case LAYOUT_MULTIPLE: |
928 | gen_mixed (*this, RP); |
981 | gen_mixed (*this, RP); |
929 | break; |
982 | break; |
930 | |
983 | |
931 | default: |
984 | default: |
… | |
… | |
934 | } |
987 | } |
935 | |
988 | |
936 | //-GPL |
989 | //-GPL |
937 | |
990 | |
938 | #if 0 |
991 | #if 0 |
|
|
992 | static void |
|
|
993 | gen_village (layout &maze) |
|
|
994 | { |
|
|
995 | maze.clear (); |
|
|
996 | maze.border (); |
|
|
997 | |
|
|
998 | for (int n = maze.w * maze.h / 200 + 1; n--; ) |
|
|
999 | { |
|
|
1000 | int rw = rmg_rndm (6, 10); |
|
|
1001 | int rh = rmg_rndm (6, 10); |
|
|
1002 | |
|
|
1003 | int rx = rmg_rndm (2, maze.w - rw - 2); |
|
|
1004 | int ry = rmg_rndm (2, maze.h - rh - 2); |
|
|
1005 | |
|
|
1006 | maze.rect (rx, ry, rx + rw, ry + rh, '#'); |
|
|
1007 | } |
|
|
1008 | |
|
|
1009 | maze.border (); |
|
|
1010 | maze.isolation_remover (2); |
|
|
1011 | } |
|
|
1012 | |
939 | static struct demo |
1013 | static struct demo |
940 | { |
1014 | { |
941 | demo () |
1015 | demo () |
942 | { |
1016 | { |
943 | rmg_rndm.seed (time (0)); |
1017 | rmg_rndm.seed (time (0)); |
|
|
1018 | extern void hack();hack (); |
944 | |
1019 | |
945 | for(int i=1;i<100;i++) |
1020 | for(int i=1;i<100;i++) |
946 | { |
1021 | { |
947 | layout maze (40, 25); |
1022 | layout maze (40, 30); |
948 | maze.fill_rand (85); |
1023 | maze.fill_rand (99); |
949 | maze.border (); |
1024 | maze.border (); |
950 | maze.isolation_remover (); |
1025 | maze.isolation_remover (2); |
951 | maze.print (); |
1026 | maze.print (); |
952 | } |
1027 | } |
953 | |
1028 | |
954 | exit (1); |
1029 | exit (1); |
955 | } |
1030 | } |