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 (©) 2017,2018 Marc Alexander Lehmann / the Deliantra team |
|
|
5 | * 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) |
6 | * Copyright (©) 1994-2004 Crossfire Development Team (restored, original file without copyright notice) |
6 | * |
7 | * |
7 | * Deliantra is free software: you can redistribute it and/or modify it under |
8 | * 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 |
9 | * 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 |
10 | * Free Software Foundation, either version 3 of the License, or (at your |
10 | * option) any later version. |
11 | * option) any later version. |
11 | * |
12 | * |
12 | * This program is distributed in the hope that it will be useful, |
13 | * This program is distributed in the hope that it will be useful, |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 | * GNU General Public License for more details. |
16 | * GNU General Public License for more details. |
16 | * |
17 | * |
17 | * You should have received a copy of the Affero GNU General Public License |
18 | * 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 |
19 | * and the GNU General Public License along with this program. If not, see |
19 | * <http://www.gnu.org/licenses/>. |
20 | * <http://www.gnu.org/licenses/>. |
20 | * |
21 | * |
21 | * The authors can be reached via e-mail to <support@deliantra.net> |
22 | * The authors can be reached via e-mail to <support@deliantra.net> |
22 | */ |
23 | */ |
23 | |
24 | |
24 | #include <global.h> |
25 | #include <global.h> |
25 | #include <random_map.h> |
26 | #include <rmg.h> |
26 | #include <rproto.h> |
27 | #include <rproto.h> |
27 | |
28 | |
28 | void |
29 | ecb_noinline void |
29 | layout::alloc (int w, int h) |
30 | layout::alloc (int w, int h) |
30 | { |
31 | { |
31 | assert (sizeof (cell) == 1); |
32 | assert (sizeof (cell) == 1); |
32 | |
33 | |
33 | this->w = w; |
34 | this->w = w; |
34 | this->h = h; |
35 | this->h = h; |
35 | |
36 | |
36 | // we store the layout in a single contiguous memory layout |
37 | // we store the layout in a single contiguous memory layout |
37 | // first part consists of pointers to each column, followed |
38 | // first part consists of pointers to each column, followed |
38 | // by the actual columns (not rows!) |
39 | // by the actual columns (not rows!) |
39 | int size = (sizeof (cell *) + sizeof (cell) * h) * w; |
40 | size = (sizeof (cell *) + sizeof (cell) * h) * w; |
40 | |
|
|
41 | data = (cell **)salloc<char> (size); |
41 | data = (cell **)salloc<char> (size); |
42 | |
42 | |
43 | cell *p = (cell *)(data + w); |
43 | cell *p = (cell *)(data + w); |
44 | |
44 | |
45 | for (int x = w; x--; ) |
45 | for (int x = 0; x < w; ++x) |
46 | data [x] = p + x * h; |
46 | data [x] = p + x * h; |
47 | } |
47 | } |
48 | |
48 | |
49 | layout::layout (int w, int h) |
49 | layout::layout (int w, int h) |
50 | { |
50 | { |
… | |
… | |
56 | alloc (copy.w, copy.h); |
56 | alloc (copy.w, copy.h); |
57 | |
57 | |
58 | memcpy (data [0], copy.data [0], sizeof (cell) * h * w); |
58 | memcpy (data [0], copy.data [0], sizeof (cell) * h * w); |
59 | } |
59 | } |
60 | |
60 | |
|
|
61 | layout::layout (layout &orig, int x1, int y1, int x2, int y2) |
|
|
62 | { |
|
|
63 | w = x2 - x1; |
|
|
64 | h = y2 - y1; |
|
|
65 | |
|
|
66 | // we only allocate space for the pointers |
|
|
67 | size = sizeof (cell *) * w; |
|
|
68 | data = (cell **)salloc<char> (size); |
|
|
69 | |
|
|
70 | // and now we point back into the original layout |
|
|
71 | for (int x = 0; x < w; ++x) |
|
|
72 | data [x] = orig.data [x + x1] + y1; |
|
|
73 | } |
|
|
74 | |
61 | layout::~layout () |
75 | layout::~layout () |
62 | { |
76 | { |
63 | int size = (sizeof (cell *) + sizeof (cell) * h) * w; |
|
|
64 | |
|
|
65 | sfree ((char *)data, size); |
77 | sfree ((char *)data, size); |
66 | } |
78 | } |
67 | |
79 | |
68 | void |
80 | ecb_noinline void |
69 | layout::fill (char fill) |
81 | layout::fill (char fill) |
70 | { |
82 | { |
71 | memset (data [0], fill, w * h); |
83 | //memset (data [0], fill, w * h); // only when contiguous :/ |
|
|
84 | fill_rect (0, 0, w, h, fill); |
72 | } |
85 | } |
73 | |
86 | |
74 | void |
87 | ecb_noinline void |
|
|
88 | layout::replace (char from, char to) |
|
|
89 | { |
|
|
90 | for (int x = 0; x < w; ++x) |
|
|
91 | for (int y = 0; y < h; ++y) |
|
|
92 | if (data [x][y] == from) |
|
|
93 | data [x][y] = to; |
|
|
94 | } |
|
|
95 | |
|
|
96 | ecb_noinline void |
75 | layout::rect (int x1, int y1, int x2, int y2, char fill) |
97 | layout::rect (int x1, int y1, int x2, int y2, char fill) |
76 | { |
98 | { |
77 | --x2; |
99 | --x2; |
78 | |
100 | |
79 | memset (data [x1] + y1, fill, y2 - y1); |
101 | memset (data [x1] + y1, fill, y2 - y1); |
… | |
… | |
81 | |
103 | |
82 | while (++x1 < x2) |
104 | while (++x1 < x2) |
83 | data [x1][y1] = data [x1][y2 - 1] = fill; |
105 | data [x1][y1] = data [x1][y2 - 1] = fill; |
84 | } |
106 | } |
85 | |
107 | |
86 | void |
108 | ecb_noinline void |
87 | layout::fill_rect (int x1, int y1, int x2, int y2, char fill) |
109 | layout::fill_rect (int x1, int y1, int x2, int y2, char fill) |
88 | { |
110 | { |
89 | for (; x1 < x2; ++x1) |
111 | for (; x1 < x2; ++x1) |
90 | memset (data [x1] + y1, fill, y2 - y1); |
112 | memset (data [x1] + y1, fill, y2 - y1); |
91 | } |
113 | } |
92 | |
114 | |
|
|
115 | void |
93 | void layout::border (char fill) |
116 | layout::border (char fill) |
94 | { |
117 | { |
95 | rect (0, 0, w, h, fill); |
118 | rect (0, 0, w, h, fill); |
96 | } |
119 | } |
97 | |
120 | |
98 | void |
121 | ecb_noinline void |
99 | layout::fill_rand (int percent) |
122 | layout::fill_rand (int percent) |
100 | { |
123 | { |
101 | percent = lerp (percent, 0, 100, 0, 256); |
124 | percent = lerp (percent, 0, 100, 0, 256); |
102 | |
125 | |
103 | for (int x = w - 1; --x > 0; ) |
126 | for (int x = 0; x < w; ++x) |
104 | for (int y = h - 1; --y > 0; ) |
127 | for (int y = 0; y < h; ++y) |
105 | data [x][y] = rmg_rndm (256) > percent ? 0 : '#'; |
128 | data [x][y] = rmg_rndm (256) > percent ? 0 : '#'; |
106 | } |
129 | } |
107 | |
130 | |
108 | ///////////////////////////////////////////////////////////////////////////// |
131 | ///////////////////////////////////////////////////////////////////////////// |
109 | |
132 | |
110 | // erode by cellular automata |
133 | // erode by cellular automata |
111 | void |
134 | ecb_noinline void |
112 | layout::erode_1_2 (int c1, int c2, int repeat) |
135 | layout::erode_1_2 (int c1, int c2, int repeat) |
113 | { |
136 | { |
114 | layout neu (w, h); |
137 | layout neu (w, h); |
115 | |
138 | |
116 | while (repeat--) |
139 | while (repeat--) |
… | |
… | |
130 | { 0, -2, 0 }, { 0, -1, 1 }, { 0, 0, 1 }, { 0, 1, 1 }, { 0, 2, 0 }, |
153 | { 0, -2, 0 }, { 0, -1, 1 }, { 0, 0, 1 }, { 0, 1, 1 }, { 0, 2, 0 }, |
131 | { 1, -2, 0 }, { 1, -1, 1 }, { 1, 0, 1 }, { 1, 1, 1 }, { 1, 2, 0 }, |
154 | { 1, -2, 0 }, { 1, -1, 1 }, { 1, 0, 1 }, { 1, 1, 1 }, { 1, 2, 0 }, |
132 | { 2, -1, 0 }, { 2, 0, 0 }, { 2, 1, 0 }, |
155 | { 2, -1, 0 }, { 2, 0, 0 }, { 2, 1, 0 }, |
133 | }; |
156 | }; |
134 | |
157 | |
135 | for (int i = array_length (dds); i--; ) |
158 | for (int i = ecb_array_length (dds); i--; ) |
136 | { |
159 | { |
137 | int nx = x + dds [i][0]; |
160 | int nx = x + dds [i][0]; |
138 | int ny = y + dds [i][1]; |
161 | int ny = y + dds [i][1]; |
139 | |
162 | |
140 | if (!IN_RANGE_EXC (nx, 0, w) || !IN_RANGE_EXC (ny, 0, h) || !data [nx][ny]) |
163 | if (!IN_RANGE_EXC (nx, 0, w) || !IN_RANGE_EXC (ny, 0, h) || !data [nx][ny]) |
… | |
… | |
182 | ///////////////////////////////////////////////////////////////////////////// |
205 | ///////////////////////////////////////////////////////////////////////////// |
183 | // isolation remover - ensures single connected area |
206 | // isolation remover - ensures single connected area |
184 | |
207 | |
185 | typedef fixed_stack<point> pointlist; |
208 | typedef fixed_stack<point> pointlist; |
186 | |
209 | |
187 | static void noinline |
210 | ecb_noinline static void |
188 | push_flood_fill (layout &dist, pointlist &seeds, int x, int y) |
211 | push_flood_fill (layout &dist, pointlist &seeds, int x, int y) |
189 | { |
212 | { |
190 | if (dist [x][y]) |
213 | if (dist [x][y]) |
191 | return; |
214 | return; |
192 | |
215 | |
… | |
… | |
203 | ++y; |
226 | ++y; |
204 | } |
227 | } |
205 | |
228 | |
206 | while (--y >= y0) |
229 | while (--y >= y0) |
207 | { |
230 | { |
208 | if (x > 0) push_flood_fill (dist, seeds, x - 1, y); |
231 | 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); |
232 | if (x < dist.w - 1 && !dist [x + 1][y]) push_flood_fill (dist, seeds, x + 1, y); |
210 | } |
233 | } |
211 | } |
234 | } |
212 | |
235 | |
213 | static inline void |
236 | static void inline |
214 | make_tunnel (layout &dist, pointlist &seeds, int x, int y, U8 d) |
237 | make_tunnel (layout &dist, pointlist &seeds, int x, int y, U8 d, int perturb) |
215 | { |
238 | { |
216 | for (;;) |
239 | for (;;) |
217 | { |
240 | { |
218 | point neigh[4]; |
241 | point neigh[4]; |
219 | int ncnt = 0; |
242 | int ncnt = 0; |
220 | |
243 | |
|
|
244 | d += perturb > 1; |
|
|
245 | |
221 | if (x > 1 && U8 (dist [x - 1][y]) <= d && dist [x - 1][y] > 1) neigh [ncnt++] = point (x - 1, y); |
246 | 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); |
247 | 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); |
248 | 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); |
249 | if (y < dist.h - 1 && U8 (dist [x][y + 1]) < d && dist [x][y + 1] > 1) neigh [ncnt++] = point (x, y + 1); |
225 | |
250 | |
226 | if (!ncnt) |
251 | if (!ncnt) |
227 | return; |
252 | return; |
228 | |
253 | |
229 | point &p = neigh [rmg_rndm (ncnt)]; |
254 | point p = neigh [perturb ? rmg_rndm (ncnt) : 0]; |
230 | |
255 | |
231 | seeds.push (p); |
256 | seeds.push (p); |
232 | |
257 | |
233 | x = p.x; |
258 | x = p.x; |
234 | y = p.y; |
259 | y = p.y; |
… | |
… | |
251 | seeds.push (point (x, y)); |
276 | seeds.push (point (x, y)); |
252 | } |
277 | } |
253 | |
278 | |
254 | // isolation remover, works on a "distance" map |
279 | // isolation remover, works on a "distance" map |
255 | // the map must be initialised with 0 == rooms, 255 = walls |
280 | // the map must be initialised with 0 == rooms, 255 = walls |
256 | static void noinline |
281 | ecb_noinline static void |
257 | isolation_remover (layout &dist) |
282 | isolation_remover (layout &dist, unsigned int perturb = 2) |
258 | { |
283 | { |
259 | // dist contains |
284 | // dist contains |
260 | // 0 == invisited rooms |
285 | // 0 == invisited rooms |
261 | // 1 == visited rooms |
286 | // 1 == visited rooms |
262 | // 2+ shortest distance to random near room |
287 | // 2+ shortest distance to random near room |
263 | |
288 | |
|
|
289 | clamp_it (perturb, 0, 2); |
|
|
290 | |
264 | // phase 1, find seed |
291 | // phase 1, find seed |
265 | int cnt = 0; |
292 | int cnt = 0; |
266 | int x, y; |
293 | int x, y; |
267 | |
294 | |
268 | for (int i = 0; i < dist.w; ++i) |
295 | for (int i = 0; i < dist.w; ++i) |
… | |
… | |
272 | |
299 | |
273 | if (!cnt) |
300 | if (!cnt) |
274 | { |
301 | { |
275 | // map is completely massive, this is not good, |
302 | // map is completely massive, this is not good, |
276 | // so make it empty instead. |
303 | // so make it empty instead. |
277 | dist.clear (); |
304 | dist.fill (1); |
278 | dist.border (255); |
|
|
279 | return; |
305 | return; |
280 | } |
306 | } |
281 | |
307 | |
282 | fixed_stack<point> seeds (dist.w * dist.h * 5); |
308 | fixed_stack<point> seeds (dist.w * dist.h * 5); |
283 | |
309 | |
… | |
… | |
287 | seeds.push (point (x, y)); |
313 | seeds.push (point (x, y)); |
288 | |
314 | |
289 | // phase 2, while we have seeds, if |
315 | // phase 2, while we have seeds, if |
290 | // seed is empty, floodfill, else grow |
316 | // seed is empty, floodfill, else grow |
291 | |
317 | |
|
|
318 | int rem_index = 0; // used to remove "somewhat ordered" |
|
|
319 | |
292 | while (seeds.size) |
320 | while (seeds.size) |
293 | { |
321 | { |
294 | coroapi::cede_to_tick (); |
322 | coroapi::cede_to_tick (); |
295 | |
323 | |
|
|
324 | int i = perturb |
|
|
325 | ? rmg_rndm (max (0, seeds.size - 8), seeds.size - 1) |
|
|
326 | : rem_index ++ % seeds.size; |
|
|
327 | |
296 | point p = seeds.remove (rmg_rndm (seeds.size)); |
328 | point p = seeds.remove (i); |
297 | |
329 | |
298 | x = p.x; |
330 | x = p.x; |
299 | y = p.y; |
331 | y = p.y; |
300 | |
332 | |
301 | if (!dist [x][y]) |
333 | if (!dist [x][y]) |
302 | { |
334 | { |
303 | // found new isolated area, make tunnel |
335 | // found new isolated area, make tunnel |
304 | push_flood_fill (dist, seeds, x, y); |
336 | push_flood_fill (dist, seeds, x, y); |
305 | make_tunnel (dist, seeds, x, y, 255); |
337 | make_tunnel (dist, seeds, x, y, 254, perturb); |
306 | } |
338 | } |
307 | else |
339 | else |
308 | { |
340 | { |
309 | // nothing here, continue to expand |
341 | // nothing here, continue to expand |
310 | U8 d = U8 (dist [x][y]) + 1; |
342 | U8 d = U8 (dist [x][y]) + 1; |
… | |
… | |
316 | } |
348 | } |
317 | } |
349 | } |
318 | } |
350 | } |
319 | |
351 | |
320 | void |
352 | void |
321 | layout::isolation_remover () |
353 | layout::isolation_remover (int perturb) |
322 | { |
354 | { |
323 | layout dist (w, h); |
355 | layout dist (w - 2, h - 2); // map without border |
324 | |
356 | |
325 | for (int x = 0; x < w; ++x) |
357 | for (int x = 1; x < w - 1; ++x) |
326 | for (int y = 0; y < h; ++y) |
358 | for (int y = 1; y < h - 1; ++y) |
327 | dist [x][y] = data [x][y] == '#' ? U8 (255) : 0; |
359 | dist [x - 1][y - 1] = data [x][y] == '#' ? U8 (255) : 0; |
328 | |
360 | |
329 | ::isolation_remover (dist); |
361 | ::isolation_remover (dist, perturb); |
330 | |
362 | |
331 | // now copy the tunnels over |
363 | // now copy the tunnels over |
332 | for (int x = 0; x < w; ++x) |
364 | for (int x = 1; x < w - 1; ++x) |
333 | for (int y = 0; y < h; ++y) |
365 | for (int y = 1; y < h - 1; ++y) |
334 | if (data [x][y] == '#' && dist [x][y] == 1) |
366 | if (data [x][y] == '#' && dist [x - 1][y - 1] == 1) |
335 | data [x][y] = 0; |
367 | data [x][y] = 0; |
336 | } |
|
|
337 | |
|
|
338 | ///////////////////////////////////////////////////////////////////////////// |
|
|
339 | |
|
|
340 | // inspired mostly by http://www.jimrandomh.org/misc/caves.txt |
|
|
341 | void |
|
|
342 | layout::gen_cave (int subtype) |
|
|
343 | { |
|
|
344 | switch (subtype) |
|
|
345 | { |
|
|
346 | // a rough cave |
|
|
347 | case 0: |
|
|
348 | fill_rand (rmg_rndm (85, 97)); |
|
|
349 | break; |
|
|
350 | |
|
|
351 | // corridors |
|
|
352 | case 1: |
|
|
353 | fill_rand (rmg_rndm (5, 40)); |
|
|
354 | erode_1_2 (5, 2, 10); |
|
|
355 | erode_1_2 (5, -1, 10); |
|
|
356 | erode_1_2 (5, 2, 1); |
|
|
357 | break; |
|
|
358 | |
|
|
359 | // somewhat open, roundish |
|
|
360 | case 2: |
|
|
361 | fill_rand (45); |
|
|
362 | erode_1_2 (5, 0, 5); |
|
|
363 | erode_1_2 (5, 1, 1); |
|
|
364 | break; |
|
|
365 | |
|
|
366 | // wide open, some room-like structures |
|
|
367 | case 3: |
|
|
368 | fill_rand (45); |
|
|
369 | erode_1_2 (5, 2, 4); |
|
|
370 | erode_1_2 (5, -1, 3); |
|
|
371 | break; |
|
|
372 | } |
|
|
373 | |
|
|
374 | border (); |
|
|
375 | isolation_remover (); |
|
|
376 | } |
368 | } |
377 | |
369 | |
378 | ///////////////////////////////////////////////////////////////////////////// |
370 | ///////////////////////////////////////////////////////////////////////////// |
379 | |
371 | |
380 | //+GPL |
372 | //+GPL |
… | |
… | |
382 | /* puts doors at appropriate locations in a maze. */ |
374 | /* puts doors at appropriate locations in a maze. */ |
383 | void |
375 | void |
384 | layout::doorify () |
376 | layout::doorify () |
385 | { |
377 | { |
386 | int ndoors = w * h / 60; /* reasonable number of doors. */ |
378 | int ndoors = w * h / 60; /* reasonable number of doors. */ |
387 | int doorlocs = 0; /* # of available doorlocations */ |
|
|
388 | |
379 | |
389 | uint16 *doorlist_x = salloc<uint16> (w * h); |
380 | coroapi::cede_to_tick (); |
390 | uint16 *doorlist_y = salloc<uint16> (w * h); |
381 | |
|
|
382 | fixed_stack<point> doorloc (w * h); |
391 | |
383 | |
392 | /* make a list of possible door locations */ |
384 | /* make a list of possible door locations */ |
393 | for (int i = 1; i < w - 1; i++) |
385 | for (int i = 1; i < w - 1; i++) |
394 | for (int j = 1; j < h - 1; j++) |
386 | for (int j = 1; j < h - 1; j++) |
395 | { |
387 | { |
396 | int sindex = surround_flag (*this, i, j); |
388 | int sindex = surround_flag (*this, i, j); |
397 | |
389 | |
398 | if (sindex == 3 || sindex == 12) /* these are possible door sindex */ |
390 | if (sindex == 3 || sindex == 12) /* these are possible door sindex */ |
399 | { |
391 | doorloc.push (point (i, j)); |
400 | doorlist_x [doorlocs] = i; |
|
|
401 | doorlist_y [doorlocs] = j; |
|
|
402 | doorlocs++; |
|
|
403 | } |
|
|
404 | } |
392 | } |
405 | |
393 | |
406 | while (ndoors > 0 && doorlocs > 0) |
394 | while (ndoors && doorloc.size) |
407 | { |
395 | { |
408 | int di = rmg_rndm (doorlocs); |
396 | point p = doorloc.remove (rmg_rndm (doorloc.size)); |
409 | int i = doorlist_x [di]; |
397 | |
410 | int j = doorlist_y [di]; |
|
|
411 | int sindex = surround_flag (*this, i, j); |
398 | int sindex = surround_flag (*this, p.x, p.y); |
412 | |
399 | |
413 | if (sindex == 3 || sindex == 12) /* these are possible door sindex */ |
400 | if (sindex == 3 || sindex == 12) /* these are possible door sindex */ |
414 | { |
401 | { |
415 | data [i][j] = 'D'; |
402 | data [p.x][p.y] = 'D'; |
416 | ndoors--; |
403 | --ndoors; |
417 | } |
404 | } |
418 | |
|
|
419 | /* reduce the size of the list */ |
|
|
420 | doorlocs--; |
|
|
421 | doorlist_x[di] = doorlist_x [doorlocs]; |
|
|
422 | doorlist_y[di] = doorlist_y [doorlocs]; |
|
|
423 | } |
405 | } |
424 | |
|
|
425 | sfree (doorlist_x, w * h); |
|
|
426 | sfree (doorlist_y, w * h); |
|
|
427 | } |
406 | } |
428 | |
407 | |
429 | /* takes a map and makes it symmetric: adjusts Xsize and |
408 | /* takes a map and makes it symmetric: adjusts Xsize and |
430 | * Ysize to produce a symmetric map. |
409 | * Ysize to produce a symmetric map. |
431 | */ |
410 | */ |
… | |
… | |
489 | //-GPL |
468 | //-GPL |
490 | |
469 | |
491 | void |
470 | void |
492 | layout::rotate (int rotation) |
471 | layout::rotate (int rotation) |
493 | { |
472 | { |
|
|
473 | coroapi::cede_to_tick (); |
|
|
474 | |
494 | switch (rotation & 3) |
475 | switch (rotation & 3) |
495 | { |
476 | { |
496 | case 2: /* a reflection */ |
477 | case 2: /* a reflection */ |
497 | { |
478 | { |
498 | layout new_layout (w, h); |
479 | layout new_layout (w, h); |
… | |
… | |
553 | * 1 match on (i+1, j) |
534 | * 1 match on (i+1, j) |
554 | * 2 match on (i, j+1) |
535 | * 2 match on (i, j+1) |
555 | * 4 match on (i+1, j+1) |
536 | * 4 match on (i+1, j+1) |
556 | * and the possible combinations thereof. |
537 | * and the possible combinations thereof. |
557 | */ |
538 | */ |
558 | static int noinline |
539 | ecb_noinline static int |
559 | calc_pattern (char ch, layout &maze, int i, int j) |
540 | calc_pattern (char ch, layout &maze, int i, int j) |
560 | { |
541 | { |
561 | int pattern = 0; |
542 | int pattern = 0; |
562 | |
543 | |
563 | if (i + 1 < maze.w && maze[i + 1][j] == ch) |
544 | if (i + 1 < maze.w && maze[i + 1][j] == ch) |
… | |
… | |
650 | { |
631 | { |
651 | layout new_layout (w * 2 - 1, h * 2 - 1); |
632 | layout new_layout (w * 2 - 1, h * 2 - 1); |
652 | |
633 | |
653 | new_layout.clear (); |
634 | new_layout.clear (); |
654 | |
635 | |
|
|
636 | coroapi::cede_to_tick (); |
|
|
637 | |
655 | for (int i = 0; i < w; i++) |
638 | for (int i = 0; i < w; i++) |
656 | for (int j = 0; j < h; j++) |
639 | for (int j = 0; j < h; j++) |
657 | switch (data [i][j]) |
640 | switch (data [i][j]) |
658 | { |
641 | { |
659 | case '#': expand_wall (new_layout, i, j, *this); break; |
642 | case '#': expand_wall (new_layout, i, j, *this); break; |
660 | case 'D': expand_door (new_layout, i, j, *this); break; |
643 | case 'D': expand_door (new_layout, i, j, *this); break; |
661 | default: expand_misc (new_layout, i, j, *this); break; |
644 | default: expand_misc (new_layout, i, j, *this); break; |
662 | } |
645 | } |
663 | |
646 | |
664 | swap (new_layout); |
647 | swap (new_layout); |
665 | } |
648 | } |
… | |
… | |
741 | |
724 | |
742 | return -1; |
725 | return -1; |
743 | } |
726 | } |
744 | |
727 | |
745 | int |
728 | int |
746 | make_wall (char **maze, int x, int y, int dir) |
729 | make_wall (layout &maze, int x, int y, int dir) |
747 | { |
730 | { |
748 | maze[x][y] = 'D'; /* mark a door */ |
731 | maze[x][y] = 'D'; /* mark a door */ |
749 | |
732 | |
750 | switch (dir) |
733 | switch (dir) |
751 | { |
734 | { |
… | |
… | |
768 | |
751 | |
769 | void |
752 | void |
770 | layout::roomify () |
753 | layout::roomify () |
771 | { |
754 | { |
772 | int tries = w * h / 30; |
755 | int tries = w * h / 30; |
|
|
756 | |
|
|
757 | coroapi::cede_to_tick (); |
773 | |
758 | |
774 | for (int ti = 0; ti < tries; ti++) |
759 | for (int ti = 0; ti < tries; ti++) |
775 | { |
760 | { |
776 | /* starting location for looking at creating a door */ |
761 | /* starting location for looking at creating a door */ |
777 | int dx = rmg_rndm (w); |
762 | int dx = rmg_rndm (w); |
… | |
… | |
800 | else |
785 | else |
801 | make_wall (*this, dx, dy, 1); |
786 | make_wall (*this, dx, dy, 1); |
802 | } |
787 | } |
803 | } |
788 | } |
804 | |
789 | |
|
|
790 | //-GPL |
|
|
791 | |
805 | ///////////////////////////////////////////////////////////////////////////// |
792 | ///////////////////////////////////////////////////////////////////////////// |
|
|
793 | |
|
|
794 | // inspired mostly by http://www.jimrandomh.org/misc/caves.txt |
|
|
795 | void |
|
|
796 | layout::gen_cave (int subtype) |
|
|
797 | { |
|
|
798 | switch (subtype) |
|
|
799 | { |
|
|
800 | // a rough cave |
|
|
801 | case 0: |
|
|
802 | fill_rand (rmg_rndm (85, 97)); |
|
|
803 | break; |
|
|
804 | |
|
|
805 | // corridors |
|
|
806 | case 1: |
|
|
807 | fill_rand (rmg_rndm (5, 40)); |
|
|
808 | erode_1_2 (5, 2, 10); |
|
|
809 | erode_1_2 (5, -1, 10); |
|
|
810 | erode_1_2 (5, 2, 1); |
|
|
811 | break; |
|
|
812 | |
|
|
813 | // somewhat open, some room-like structures |
|
|
814 | case 2: |
|
|
815 | fill_rand (45); |
|
|
816 | erode_1_2 (5, 2, 4); |
|
|
817 | erode_1_2 (5, -1, 3); |
|
|
818 | break; |
|
|
819 | |
|
|
820 | // wide open, roundish |
|
|
821 | case 3: |
|
|
822 | fill_rand (45); |
|
|
823 | erode_1_2 (5, 0, 5); |
|
|
824 | erode_1_2 (5, 1, 1); |
|
|
825 | break; |
|
|
826 | } |
|
|
827 | |
|
|
828 | border (); |
|
|
829 | isolation_remover (1); |
|
|
830 | } |
|
|
831 | |
|
|
832 | void |
|
|
833 | layout::gen_castle () |
|
|
834 | { |
|
|
835 | fill ('#'); |
|
|
836 | |
|
|
837 | for (int n = w * h / 30 + 1; n--; ) |
|
|
838 | { |
|
|
839 | int rw = rmg_rndm (6, 10); |
|
|
840 | int rh = rmg_rndm (6, 10); |
|
|
841 | |
|
|
842 | if (rw > w || rh > h) |
|
|
843 | continue; |
|
|
844 | |
|
|
845 | int rx = rmg_rndm (0, w - rw); |
|
|
846 | int ry = rmg_rndm (0, h - rh); |
|
|
847 | |
|
|
848 | rect (rx, ry, rx + rw, ry + rh, '#'); |
|
|
849 | fill_rect (rx + 1, ry + 1, rx + rw - 1, ry + rh - 1, 0); |
|
|
850 | } |
|
|
851 | |
|
|
852 | border (); |
|
|
853 | isolation_remover (0); |
|
|
854 | } |
|
|
855 | |
|
|
856 | static void |
|
|
857 | gen_mixed_ (layout &maze, random_map_params *RP) |
|
|
858 | { |
|
|
859 | if (maze.w > maze.h && maze.w > 16) |
|
|
860 | { |
|
|
861 | int m = rmg_rndm (8, maze.w - 8); |
|
|
862 | |
|
|
863 | layout m1 (maze, 0, 0, m , maze.h); gen_mixed_ (m1, RP); |
|
|
864 | layout m2 (maze, m, 0, maze.w, maze.h); gen_mixed_ (m2, RP); |
|
|
865 | } |
|
|
866 | else if (maze.h > 16) |
|
|
867 | { |
|
|
868 | int m = rmg_rndm (8, maze.h - 8); |
|
|
869 | |
|
|
870 | layout m1 (maze, 0, 0, maze.w, m ); gen_mixed_ (m1, RP); |
|
|
871 | layout m2 (maze, 0, m, maze.w, maze.h); gen_mixed_ (m2, RP); |
|
|
872 | } |
|
|
873 | else |
|
|
874 | { |
|
|
875 | RP->map_layout_style = rmg_rndm (NROFLAYOUTS - 2) + 1; |
|
|
876 | |
|
|
877 | if (RP->map_layout_style == LAYOUT_MULTIPLE) |
|
|
878 | ++RP->map_layout_style; |
|
|
879 | |
|
|
880 | maze.generate (RP); |
|
|
881 | } |
|
|
882 | |
|
|
883 | coroapi::cede_to_tick (); |
|
|
884 | } |
|
|
885 | |
|
|
886 | // recursive subdivision with random sublayouts |
|
|
887 | static void |
|
|
888 | gen_mixed (layout &maze, random_map_params *RP) |
|
|
889 | { |
|
|
890 | random_map_params &rp = *new random_map_params (RP); |
|
|
891 | gen_mixed_ (maze, &rp); |
|
|
892 | delete &rp; |
|
|
893 | |
|
|
894 | maze.border (); |
|
|
895 | |
|
|
896 | // exits currently do not work so well, as they |
|
|
897 | // are currently often found together, so nuke entrances |
|
|
898 | maze.replace ('<', ' '); |
|
|
899 | |
|
|
900 | maze.isolation_remover (0); |
|
|
901 | } |
|
|
902 | |
|
|
903 | //+GPL |
806 | |
904 | |
807 | /* function selects the maze function and gives it whatever |
905 | /* function selects the maze function and gives it whatever |
808 | arguments it needs. */ |
906 | arguments it needs. */ |
809 | void |
907 | void |
810 | layout::generate (random_map_params *RP) |
908 | layout::generate (random_map_params *RP) |
… | |
… | |
870 | if (rmg_rndm (2)) |
968 | if (rmg_rndm (2)) |
871 | doorify (); |
969 | doorify (); |
872 | |
970 | |
873 | break; |
971 | break; |
874 | |
972 | |
|
|
973 | case LAYOUT_CASTLE: |
|
|
974 | gen_castle (); |
|
|
975 | |
|
|
976 | if (rmg_rndm (2)) |
|
|
977 | doorify (); |
|
|
978 | |
|
|
979 | break; |
|
|
980 | |
|
|
981 | case LAYOUT_MULTIPLE: |
|
|
982 | gen_mixed (*this, RP); |
|
|
983 | break; |
|
|
984 | |
875 | default: |
985 | default: |
876 | abort (); |
986 | abort (); |
877 | } |
987 | } |
|
|
988 | } |
878 | |
989 | |
879 | /* rotate the maze randomly */ |
990 | //-GPL |
880 | rotate (rmg_rndm (4)); |
|
|
881 | |
|
|
882 | symmetrize (RP->symmetry_used); |
|
|
883 | |
991 | |
884 | #if 0 |
992 | #if 0 |
885 | print ();//D |
993 | static void |
886 | #endif |
994 | gen_village (layout &maze) |
|
|
995 | { |
|
|
996 | maze.clear (); |
|
|
997 | maze.border (); |
887 | |
998 | |
888 | if (RP->expand2x) |
999 | for (int n = maze.w * maze.h / 200 + 1; n--; ) |
889 | expand2x (); |
1000 | { |
890 | } |
1001 | int rw = rmg_rndm (6, 10); |
|
|
1002 | int rh = rmg_rndm (6, 10); |
891 | |
1003 | |
892 | //-GPL |
1004 | int rx = rmg_rndm (2, maze.w - rw - 2); |
|
|
1005 | int ry = rmg_rndm (2, maze.h - rh - 2); |
893 | |
1006 | |
894 | #if 0 |
1007 | maze.rect (rx, ry, rx + rw, ry + rh, '#'); |
|
|
1008 | } |
|
|
1009 | |
|
|
1010 | maze.border (); |
|
|
1011 | maze.isolation_remover (2); |
|
|
1012 | } |
|
|
1013 | |
895 | static struct demo |
1014 | static struct demo |
896 | { |
1015 | { |
897 | demo () |
1016 | demo () |
898 | { |
1017 | { |
899 | rmg_rndm.seed (time (0)); |
1018 | rmg_rndm.seed (time (0)); |
|
|
1019 | extern void hack();hack (); |
900 | |
1020 | |
901 | for(int i=1;i<100;i++) |
1021 | for(int i=1;i<100;i++) |
902 | { |
1022 | { |
903 | layout maze (40, 25); |
1023 | layout maze (40, 30); |
904 | maze.fill_rand (85); |
1024 | maze.fill_rand (99); |
905 | maze.border (); |
1025 | maze.border (); |
906 | maze.isolation_remover (); |
1026 | maze.isolation_remover (2); |
907 | maze.print (); |
1027 | maze.print (); |
908 | } |
1028 | } |
909 | |
1029 | |
910 | exit (1); |
1030 | exit (1); |
911 | } |
1031 | } |