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 | |
|
|
29 | ecb_noinline void |
28 | layout::layout (int w, int h) |
30 | layout::alloc (int w, int h) |
29 | : w(w), h(h) |
|
|
30 | { |
31 | { |
31 | assert (sizeof (cell) == 1); |
32 | assert (sizeof (cell) == 1); |
|
|
33 | |
|
|
34 | this->w = w; |
|
|
35 | this->h = h; |
32 | |
36 | |
33 | // we store the layout in a single contiguous memory layout |
37 | // we store the layout in a single contiguous memory layout |
34 | // first part consists of pointers to each column, followed |
38 | // first part consists of pointers to each column, followed |
35 | // by the actual columns (not rows!) |
39 | // by the actual columns (not rows!) |
36 | int size = (sizeof (cell *) + sizeof (cell) * h) * w; |
40 | size = (sizeof (cell *) + sizeof (cell) * h) * w; |
37 | |
|
|
38 | data = (cell **)salloc<char> (size); |
41 | data = (cell **)salloc<char> (size); |
39 | |
42 | |
40 | cell *p = (cell *)(data + w); |
43 | cell *p = (cell *)(data + w); |
41 | |
44 | |
42 | for (int x = w; x--; ) |
45 | for (int x = 0; x < w; ++x) |
43 | data [x] = p + x * h; |
46 | data [x] = p + x * h; |
44 | } |
47 | } |
45 | |
48 | |
|
|
49 | layout::layout (int w, int h) |
|
|
50 | { |
|
|
51 | alloc (w, h); |
|
|
52 | } |
|
|
53 | |
|
|
54 | layout::layout (layout ©) |
|
|
55 | { |
|
|
56 | alloc (copy.w, copy.h); |
|
|
57 | |
|
|
58 | memcpy (data [0], copy.data [0], sizeof (cell) * h * w); |
|
|
59 | } |
|
|
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 | |
46 | layout::~layout () |
75 | layout::~layout () |
47 | { |
76 | { |
48 | int size = (sizeof (cell *) + sizeof (cell) * h) * w; |
|
|
49 | |
|
|
50 | sfree ((char *)data, size); |
77 | sfree ((char *)data, size); |
51 | } |
78 | } |
52 | |
79 | |
53 | void |
80 | ecb_noinline void |
54 | layout::fill (char fill) |
81 | layout::fill (char fill) |
55 | { |
82 | { |
56 | memset (data [0], fill, w * h); |
83 | //memset (data [0], fill, w * h); // only when contiguous :/ |
|
|
84 | fill_rect (0, 0, w, h, fill); |
57 | } |
85 | } |
58 | |
86 | |
59 | 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 |
60 | 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) |
|
|
98 | { |
|
|
99 | --x2; |
|
|
100 | |
|
|
101 | memset (data [x1] + y1, fill, y2 - y1); |
|
|
102 | memset (data [x2] + y1, fill, y2 - y1); |
|
|
103 | |
|
|
104 | while (++x1 < x2) |
|
|
105 | data [x1][y1] = data [x1][y2 - 1] = fill; |
|
|
106 | } |
|
|
107 | |
|
|
108 | ecb_noinline void |
|
|
109 | layout::fill_rect (int x1, int y1, int x2, int y2, char fill) |
61 | { |
110 | { |
62 | for (; x1 < x2; ++x1) |
111 | for (; x1 < x2; ++x1) |
63 | memset (data [x1] + y1, fill, y2 - y1); |
112 | memset (data [x1] + y1, fill, y2 - y1); |
64 | } |
113 | } |
65 | |
114 | |
66 | void layout::border (char fill) |
|
|
67 | { |
|
|
68 | for (int i = 0; i < w; i++) data [i][0] = data [i][h - 1] = fill; |
|
|
69 | for (int j = 0; j < h; j++) data [0][j] = data [w - 1][j] = fill; |
|
|
70 | } |
|
|
71 | |
|
|
72 | void |
115 | void |
|
|
116 | layout::border (char fill) |
|
|
117 | { |
|
|
118 | rect (0, 0, w, h, fill); |
|
|
119 | } |
|
|
120 | |
|
|
121 | ecb_noinline void |
73 | layout::fill_rand (int percent) |
122 | layout::fill_rand (int percent) |
74 | { |
123 | { |
75 | percent = lerp (percent, 0, 100, 0, 256); |
124 | percent = lerp (percent, 0, 100, 0, 256); |
76 | |
125 | |
77 | for (int x = w - 1; --x > 0; ) |
126 | for (int x = 0; x < w; ++x) |
78 | for (int y = h - 1; --y > 0; ) |
127 | for (int y = 0; y < h; ++y) |
79 | data [x][y] = rmg_rndm (256) > percent ? 0 : '#'; |
128 | data [x][y] = rmg_rndm (256) > percent ? 0 : '#'; |
80 | } |
129 | } |
81 | |
130 | |
82 | ///////////////////////////////////////////////////////////////////////////// |
131 | ///////////////////////////////////////////////////////////////////////////// |
83 | |
132 | |
84 | // erode by cellular automata |
133 | // erode by cellular automata |
85 | void |
134 | ecb_noinline void |
86 | layout::erode_1_2 (int c1, int c2, int repeat) |
135 | layout::erode_1_2 (int c1, int c2, int repeat) |
87 | { |
136 | { |
88 | layout neu (w, h); |
137 | layout neu (w, h); |
89 | |
138 | |
90 | while (repeat--) |
139 | while (repeat--) |
… | |
… | |
104 | { 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 }, |
105 | { 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 }, |
106 | { 2, -1, 0 }, { 2, 0, 0 }, { 2, 1, 0 }, |
155 | { 2, -1, 0 }, { 2, 0, 0 }, { 2, 1, 0 }, |
107 | }; |
156 | }; |
108 | |
157 | |
109 | for (int i = array_length (dds); i--; ) |
158 | for (int i = ecb_array_length (dds); i--; ) |
110 | { |
159 | { |
111 | int nx = x + dds [i][0]; |
160 | int nx = x + dds [i][0]; |
112 | int ny = y + dds [i][1]; |
161 | int ny = y + dds [i][1]; |
113 | |
162 | |
114 | 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]) |
… | |
… | |
156 | ///////////////////////////////////////////////////////////////////////////// |
205 | ///////////////////////////////////////////////////////////////////////////// |
157 | // isolation remover - ensures single connected area |
206 | // isolation remover - ensures single connected area |
158 | |
207 | |
159 | typedef fixed_stack<point> pointlist; |
208 | typedef fixed_stack<point> pointlist; |
160 | |
209 | |
161 | static noinline void |
210 | ecb_noinline static void |
162 | push_flood_fill (layout &dist, pointlist &seeds, int x, int y) |
211 | push_flood_fill (layout &dist, pointlist &seeds, int x, int y) |
163 | { |
212 | { |
164 | if (dist [x][y]) |
213 | if (dist [x][y]) |
165 | return; |
214 | return; |
166 | |
215 | |
… | |
… | |
177 | ++y; |
226 | ++y; |
178 | } |
227 | } |
179 | |
228 | |
180 | while (--y >= y0) |
229 | while (--y >= y0) |
181 | { |
230 | { |
182 | 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); |
183 | 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); |
184 | } |
233 | } |
185 | } |
234 | } |
186 | |
235 | |
187 | static inline void |
236 | static void inline |
188 | 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) |
189 | { |
238 | { |
190 | for (;;) |
239 | for (;;) |
191 | { |
240 | { |
|
|
241 | point neigh[4]; |
|
|
242 | int ncnt = 0; |
|
|
243 | |
|
|
244 | d += perturb > 1; |
|
|
245 | |
192 | if (x > 1 && U8 (dist [x - 1][y]) < d && dist [x - 1][y] > 1) |
246 | if (x > 0 && U8 (dist [x - 1][y]) < d && dist [x - 1][y] > 1) neigh [ncnt++] = point (x - 1, y); |
193 | --x; |
|
|
194 | else if (x < dist.w - 2 && U8 (dist [x + 1][y]) < d && dist [x + 1][y] > 1) |
247 | if (x < dist.w - 1 && U8 (dist [x + 1][y]) < d && dist [x + 1][y] > 1) neigh [ncnt++] = point (x + 1, y); |
195 | ++x; |
|
|
196 | else if (y > 1 && U8 (dist [x][y - 1]) < d && dist [x][y - 1] > 1) |
248 | if (y > 0 && U8 (dist [x][y - 1]) < d && dist [x][y - 1] > 1) neigh [ncnt++] = point (x, y - 1); |
197 | --y; |
|
|
198 | else if (y < dist.h - 2 && U8 (dist [x][y + 1]) < d && dist [x][y + 1] > 1) |
249 | if (y < dist.h - 1 && U8 (dist [x][y + 1]) < d && dist [x][y + 1] > 1) neigh [ncnt++] = point (x, y + 1); |
199 | ++y; |
250 | |
200 | else |
251 | if (!ncnt) |
201 | break; |
252 | return; |
|
|
253 | |
|
|
254 | point p = neigh [perturb ? rmg_rndm (ncnt) : 0]; |
|
|
255 | |
|
|
256 | seeds.push (p); |
|
|
257 | |
|
|
258 | x = p.x; |
|
|
259 | y = p.y; |
202 | |
260 | |
203 | d = dist [x][y]; |
261 | d = dist [x][y]; |
204 | dist [x][y] = 1; |
262 | dist [x][y] = 1; |
205 | seeds.push (point (x, y)); |
|
|
206 | } |
263 | } |
207 | } |
264 | } |
208 | |
265 | |
209 | static void inline |
266 | static void inline |
210 | maybe_push (layout &dist, pointlist &seeds, int x, int y, U8 d) |
267 | maybe_push (layout &dist, pointlist &seeds, int x, int y, U8 d) |
… | |
… | |
217 | return; |
274 | return; |
218 | |
275 | |
219 | seeds.push (point (x, y)); |
276 | seeds.push (point (x, y)); |
220 | } |
277 | } |
221 | |
278 | |
222 | void |
279 | // isolation remover, works on a "distance" map |
223 | layout::isolation_remover (bool dirty) |
280 | // the map must be initialised with 0 == rooms, 255 = walls |
|
|
281 | ecb_noinline static void |
|
|
282 | isolation_remover (layout &dist, unsigned int perturb = 2) |
224 | { |
283 | { |
225 | layout dist (w, h); |
|
|
226 | |
|
|
227 | // dist contains |
284 | // dist contains |
228 | // 0 == invisited rooms |
285 | // 0 == invisited rooms |
229 | // 1 == visited rooms |
286 | // 1 == visited rooms |
230 | // 2+ shortest distance to random near room |
287 | // 2+ shortest distance to random near room |
231 | |
288 | |
232 | // phase 1, initialise dist array, find seed |
289 | clamp_it (perturb, 0, 2); |
|
|
290 | |
|
|
291 | // phase 1, find seed |
233 | int cnt = 0; |
292 | int cnt = 0; |
234 | int x, y; |
293 | int x, y; |
235 | |
294 | |
236 | for (int i = 0; i < w; ++i) |
295 | for (int i = 0; i < dist.w; ++i) |
237 | for (int j = 0; j < h; ++j) |
296 | for (int j = 0; j < dist.h; ++j) |
238 | { |
297 | if (!dist [i][j] && !rmg_rndm (++cnt)) |
239 | if (data [i][j] == '#') |
|
|
240 | dist [i][j] = U8 (255); |
|
|
241 | else |
|
|
242 | { |
|
|
243 | dist [i][j] = 0; |
|
|
244 | if (!rmg_rndm (++cnt)) |
|
|
245 | x = i, y = j; |
298 | x = i, y = j; |
246 | } |
|
|
247 | } |
|
|
248 | |
299 | |
249 | if (!cnt) |
300 | if (!cnt) |
250 | { |
301 | { |
251 | // map is completely massive, this is not good, |
302 | // map is completely massive, this is not good, |
252 | // so make it empty instead. |
303 | // so make it empty instead. |
253 | clear (); |
304 | dist.fill (1); |
254 | border (); |
|
|
255 | return; |
305 | return; |
256 | } |
306 | } |
257 | |
307 | |
258 | fixed_stack<point> seeds (w * h * 5); |
308 | fixed_stack<point> seeds (dist.w * dist.h * 5); |
259 | |
309 | |
260 | // found first free space - picking the first one gives |
310 | // found first free space - picking the first one gives |
261 | // us a slight bias for tunnels, but usually you won't |
311 | // us a slight bias for tunnels, but usually you won't |
262 | // notice that in-game |
312 | // notice that in-game |
263 | seeds.push (point (x, y)); |
313 | seeds.push (point (x, y)); |
264 | |
314 | |
265 | // phase 2, while we have seeds, if |
315 | // phase 2, while we have seeds, if |
266 | // seed is empty, floodfill, else grow |
316 | // seed is empty, floodfill, else grow |
267 | |
317 | |
|
|
318 | int rem_index = 0; // used to remove "somewhat ordered" |
|
|
319 | |
268 | while (seeds.size) |
320 | while (seeds.size) |
269 | { |
321 | { |
270 | coroapi::cede_to_tick (); |
322 | coroapi::cede_to_tick (); |
271 | |
323 | |
|
|
324 | int i = perturb |
|
|
325 | ? rmg_rndm (max (0, seeds.size - 8), seeds.size - 1) |
|
|
326 | : rem_index ++ % seeds.size; |
|
|
327 | |
272 | point p = seeds.remove (rmg_rndm (seeds.size)); |
328 | point p = seeds.remove (i); |
273 | |
329 | |
274 | x = p.x; |
330 | x = p.x; |
275 | y = p.y; |
331 | y = p.y; |
276 | |
332 | |
277 | if (!dist [x][y]) |
333 | if (!dist [x][y]) |
278 | { |
334 | { |
279 | // found new isolated area, make tunnel |
335 | // found new isolated area, make tunnel |
280 | if (!dirty) |
|
|
281 | push_flood_fill (dist, seeds, x, y); |
336 | push_flood_fill (dist, seeds, x, y); |
282 | |
|
|
283 | make_tunnel (dist, seeds, x, y, 255); |
337 | make_tunnel (dist, seeds, x, y, 254, perturb); |
284 | |
|
|
285 | if (dirty) |
|
|
286 | push_flood_fill (dist, seeds, x, y); |
|
|
287 | } |
338 | } |
288 | else |
339 | else |
289 | { |
340 | { |
290 | // nothing here, continue to expand |
341 | // nothing here, continue to expand |
291 | U8 d = U8 (dist [x][y]) + 1; |
342 | U8 d = U8 (dist [x][y]) + 1; |
… | |
… | |
294 | if (x > 0) maybe_push (dist, seeds, x - 1, y, d); |
345 | if (x > 0) maybe_push (dist, seeds, x - 1, y, d); |
295 | if (y < dist.h - 1) maybe_push (dist, seeds, x, y + 1, d); |
346 | if (y < dist.h - 1) maybe_push (dist, seeds, x, y + 1, d); |
296 | if (y > 0) maybe_push (dist, seeds, x, y - 1, d); |
347 | if (y > 0) maybe_push (dist, seeds, x, y - 1, d); |
297 | } |
348 | } |
298 | } |
349 | } |
|
|
350 | } |
|
|
351 | |
|
|
352 | void |
|
|
353 | layout::isolation_remover (int perturb) |
|
|
354 | { |
|
|
355 | layout dist (w - 2, h - 2); // map without border |
|
|
356 | |
|
|
357 | for (int x = 1; x < w - 1; ++x) |
|
|
358 | for (int y = 1; y < h - 1; ++y) |
|
|
359 | dist [x - 1][y - 1] = data [x][y] == '#' ? U8 (255) : 0; |
|
|
360 | |
|
|
361 | ::isolation_remover (dist, perturb); |
299 | |
362 | |
300 | // now copy the tunnels over |
363 | // now copy the tunnels over |
301 | for (int x = 0; x < w; ++x) |
364 | for (int x = 1; x < w - 1; ++x) |
302 | for (int y = 0; y < h; ++y) |
365 | for (int y = 1; y < h - 1; ++y) |
303 | if (data [x][y] == '#' && dist [x][y] == 1) |
366 | if (data [x][y] == '#' && dist [x - 1][y - 1] == 1) |
304 | data [x][y] = 0; |
367 | data [x][y] = 0; |
305 | } |
|
|
306 | |
|
|
307 | ///////////////////////////////////////////////////////////////////////////// |
|
|
308 | |
|
|
309 | // inspired mostly by http://www.jimrandomh.org/misc/caves.txt |
|
|
310 | void |
|
|
311 | layout::gen_cave (int subtype) |
|
|
312 | { |
|
|
313 | switch (subtype) |
|
|
314 | { |
|
|
315 | // a rough cave |
|
|
316 | case 0: |
|
|
317 | fill_rand (rmg_rndm (80, 95)); |
|
|
318 | break; |
|
|
319 | |
|
|
320 | // corridors |
|
|
321 | case 1: |
|
|
322 | fill_rand (rmg_rndm (5, 40)); |
|
|
323 | erode_1_2 (5, 2, 10); |
|
|
324 | erode_1_2 (5, -1, 10); |
|
|
325 | erode_1_2 (5, 2, 1); |
|
|
326 | break; |
|
|
327 | |
|
|
328 | // somewhat open, roundish |
|
|
329 | case 2: |
|
|
330 | fill_rand (45); |
|
|
331 | erode_1_2 (5, 0, 5); |
|
|
332 | erode_1_2 (5, 1, 1); |
|
|
333 | break; |
|
|
334 | |
|
|
335 | // wide open, some room-like structures |
|
|
336 | case 3: |
|
|
337 | fill_rand (45); |
|
|
338 | erode_1_2 (5, 2, 4); |
|
|
339 | erode_1_2 (5, -1, 3); |
|
|
340 | break; |
|
|
341 | } |
|
|
342 | |
|
|
343 | border (); |
|
|
344 | isolation_remover (); |
|
|
345 | } |
368 | } |
346 | |
369 | |
347 | ///////////////////////////////////////////////////////////////////////////// |
370 | ///////////////////////////////////////////////////////////////////////////// |
348 | |
371 | |
349 | //+GPL |
372 | //+GPL |
… | |
… | |
351 | /* puts doors at appropriate locations in a maze. */ |
374 | /* puts doors at appropriate locations in a maze. */ |
352 | void |
375 | void |
353 | layout::doorify () |
376 | layout::doorify () |
354 | { |
377 | { |
355 | int ndoors = w * h / 60; /* reasonable number of doors. */ |
378 | int ndoors = w * h / 60; /* reasonable number of doors. */ |
356 | int doorlocs = 0; /* # of available doorlocations */ |
|
|
357 | |
379 | |
358 | uint16 *doorlist_x = salloc<uint16> (w * h); |
380 | coroapi::cede_to_tick (); |
359 | uint16 *doorlist_y = salloc<uint16> (w * h); |
381 | |
|
|
382 | fixed_stack<point> doorloc (w * h); |
360 | |
383 | |
361 | /* make a list of possible door locations */ |
384 | /* make a list of possible door locations */ |
362 | for (int i = 1; i < w - 1; i++) |
385 | for (int i = 1; i < w - 1; i++) |
363 | for (int j = 1; j < h - 1; j++) |
386 | for (int j = 1; j < h - 1; j++) |
364 | { |
387 | { |
365 | int sindex = surround_flag (*this, i, j); |
388 | int sindex = surround_flag (*this, i, j); |
366 | |
389 | |
367 | if (sindex == 3 || sindex == 12) /* these are possible door sindex */ |
390 | if (sindex == 3 || sindex == 12) /* these are possible door sindex */ |
368 | { |
391 | doorloc.push (point (i, j)); |
369 | doorlist_x [doorlocs] = i; |
|
|
370 | doorlist_y [doorlocs] = j; |
|
|
371 | doorlocs++; |
|
|
372 | } |
|
|
373 | } |
392 | } |
374 | |
393 | |
375 | while (ndoors > 0 && doorlocs > 0) |
394 | while (ndoors && doorloc.size) |
376 | { |
395 | { |
377 | int di = rmg_rndm (doorlocs); |
396 | point p = doorloc.remove (rmg_rndm (doorloc.size)); |
378 | int i = doorlist_x [di]; |
397 | |
379 | int j = doorlist_y [di]; |
|
|
380 | int sindex = surround_flag (*this, i, j); |
398 | int sindex = surround_flag (*this, p.x, p.y); |
381 | |
399 | |
382 | if (sindex == 3 || sindex == 12) /* these are possible door sindex */ |
400 | if (sindex == 3 || sindex == 12) /* these are possible door sindex */ |
383 | { |
401 | { |
384 | data [i][j] = 'D'; |
402 | data [p.x][p.y] = 'D'; |
385 | ndoors--; |
403 | --ndoors; |
386 | } |
404 | } |
387 | |
|
|
388 | /* reduce the size of the list */ |
|
|
389 | doorlocs--; |
|
|
390 | doorlist_x[di] = doorlist_x [doorlocs]; |
|
|
391 | doorlist_y[di] = doorlist_y [doorlocs]; |
|
|
392 | } |
405 | } |
393 | |
|
|
394 | sfree (doorlist_x, w * h); |
|
|
395 | sfree (doorlist_y, w * h); |
|
|
396 | } |
406 | } |
397 | |
407 | |
398 | /* takes a map and makes it symmetric: adjusts Xsize and |
408 | /* takes a map and makes it symmetric: adjusts Xsize and |
399 | * Ysize to produce a symmetric map. |
409 | * Ysize to produce a symmetric map. |
400 | */ |
410 | */ |
… | |
… | |
458 | //-GPL |
468 | //-GPL |
459 | |
469 | |
460 | void |
470 | void |
461 | layout::rotate (int rotation) |
471 | layout::rotate (int rotation) |
462 | { |
472 | { |
|
|
473 | coroapi::cede_to_tick (); |
|
|
474 | |
463 | switch (rotation & 3) |
475 | switch (rotation & 3) |
464 | { |
476 | { |
465 | case 2: /* a reflection */ |
477 | case 2: /* a reflection */ |
466 | { |
478 | { |
467 | layout new_layout (w, h); |
479 | layout new_layout (w, h); |
… | |
… | |
522 | * 1 match on (i+1, j) |
534 | * 1 match on (i+1, j) |
523 | * 2 match on (i, j+1) |
535 | * 2 match on (i, j+1) |
524 | * 4 match on (i+1, j+1) |
536 | * 4 match on (i+1, j+1) |
525 | * and the possible combinations thereof. |
537 | * and the possible combinations thereof. |
526 | */ |
538 | */ |
527 | static int noinline |
539 | ecb_noinline static int |
528 | calc_pattern (char ch, layout &maze, int i, int j) |
540 | calc_pattern (char ch, layout &maze, int i, int j) |
529 | { |
541 | { |
530 | int pattern = 0; |
542 | int pattern = 0; |
531 | |
543 | |
532 | if (i + 1 < maze.w && maze[i + 1][j] == ch) |
544 | if (i + 1 < maze.w && maze[i + 1][j] == ch) |
… | |
… | |
619 | { |
631 | { |
620 | layout new_layout (w * 2 - 1, h * 2 - 1); |
632 | layout new_layout (w * 2 - 1, h * 2 - 1); |
621 | |
633 | |
622 | new_layout.clear (); |
634 | new_layout.clear (); |
623 | |
635 | |
|
|
636 | coroapi::cede_to_tick (); |
|
|
637 | |
624 | for (int i = 0; i < w; i++) |
638 | for (int i = 0; i < w; i++) |
625 | for (int j = 0; j < h; j++) |
639 | for (int j = 0; j < h; j++) |
626 | switch (data [i][j]) |
640 | switch (data [i][j]) |
627 | { |
641 | { |
628 | case '#': expand_wall (new_layout, i, j, *this); break; |
642 | case '#': expand_wall (new_layout, i, j, *this); break; |
629 | case 'D': expand_door (new_layout, i, j, *this); break; |
643 | case 'D': expand_door (new_layout, i, j, *this); break; |
630 | default: expand_misc (new_layout, i, j, *this); break; |
644 | default: expand_misc (new_layout, i, j, *this); break; |
631 | } |
645 | } |
632 | |
646 | |
633 | swap (new_layout); |
647 | swap (new_layout); |
634 | } |
648 | } |
… | |
… | |
710 | |
724 | |
711 | return -1; |
725 | return -1; |
712 | } |
726 | } |
713 | |
727 | |
714 | int |
728 | int |
715 | make_wall (char **maze, int x, int y, int dir) |
729 | make_wall (layout &maze, int x, int y, int dir) |
716 | { |
730 | { |
717 | maze[x][y] = 'D'; /* mark a door */ |
731 | maze[x][y] = 'D'; /* mark a door */ |
718 | |
732 | |
719 | switch (dir) |
733 | switch (dir) |
720 | { |
734 | { |
… | |
… | |
737 | |
751 | |
738 | void |
752 | void |
739 | layout::roomify () |
753 | layout::roomify () |
740 | { |
754 | { |
741 | int tries = w * h / 30; |
755 | int tries = w * h / 30; |
|
|
756 | |
|
|
757 | coroapi::cede_to_tick (); |
742 | |
758 | |
743 | for (int ti = 0; ti < tries; ti++) |
759 | for (int ti = 0; ti < tries; ti++) |
744 | { |
760 | { |
745 | /* starting location for looking at creating a door */ |
761 | /* starting location for looking at creating a door */ |
746 | int dx = rmg_rndm (w); |
762 | int dx = rmg_rndm (w); |
… | |
… | |
769 | else |
785 | else |
770 | make_wall (*this, dx, dy, 1); |
786 | make_wall (*this, dx, dy, 1); |
771 | } |
787 | } |
772 | } |
788 | } |
773 | |
789 | |
|
|
790 | //-GPL |
|
|
791 | |
774 | ///////////////////////////////////////////////////////////////////////////// |
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 |
775 | |
904 | |
776 | /* function selects the maze function and gives it whatever |
905 | /* function selects the maze function and gives it whatever |
777 | arguments it needs. */ |
906 | arguments it needs. */ |
778 | void |
907 | void |
779 | layout::generate (random_map_params *RP) |
908 | layout::generate (random_map_params *RP) |
… | |
… | |
839 | if (rmg_rndm (2)) |
968 | if (rmg_rndm (2)) |
840 | doorify (); |
969 | doorify (); |
841 | |
970 | |
842 | break; |
971 | break; |
843 | |
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 | |
844 | default: |
985 | default: |
845 | abort (); |
986 | abort (); |
846 | } |
987 | } |
|
|
988 | } |
847 | |
989 | |
848 | /* rotate the maze randomly */ |
990 | //-GPL |
849 | rotate (rmg_rndm (4)); |
|
|
850 | |
|
|
851 | symmetrize (RP->symmetry_used); |
|
|
852 | |
991 | |
853 | #if 0 |
992 | #if 0 |
854 | print ();//D |
993 | static void |
855 | #endif |
994 | gen_village (layout &maze) |
|
|
995 | { |
|
|
996 | maze.clear (); |
|
|
997 | maze.border (); |
856 | |
998 | |
857 | if (RP->expand2x) |
999 | for (int n = maze.w * maze.h / 200 + 1; n--; ) |
858 | expand2x (); |
1000 | { |
859 | } |
1001 | int rw = rmg_rndm (6, 10); |
|
|
1002 | int rh = rmg_rndm (6, 10); |
860 | |
1003 | |
861 | //-GPL |
1004 | int rx = rmg_rndm (2, maze.w - rw - 2); |
|
|
1005 | int ry = rmg_rndm (2, maze.h - rh - 2); |
862 | |
1006 | |
863 | #if 0 |
1007 | maze.rect (rx, ry, rx + rw, ry + rh, '#'); |
|
|
1008 | } |
|
|
1009 | |
|
|
1010 | maze.border (); |
|
|
1011 | maze.isolation_remover (2); |
|
|
1012 | } |
|
|
1013 | |
864 | static struct demo |
1014 | static struct demo |
865 | { |
1015 | { |
866 | demo () |
1016 | demo () |
867 | { |
1017 | { |
868 | rmg_rndm.seed (time (0)); |
1018 | rmg_rndm.seed (time (0)); |
|
|
1019 | extern void hack();hack (); |
869 | |
1020 | |
870 | for(int i=1;i<10;i) |
1021 | for(int i=1;i<100;i++) |
871 | { |
1022 | { |
872 | layout maze (10, 10); |
1023 | layout maze (40, 30); |
873 | maze.fill_rand (90); |
1024 | maze.fill_rand (99); |
874 | maze.border (); |
1025 | maze.border (); |
875 | maze.isolation_remover (); |
1026 | maze.isolation_remover (2); |
876 | maze.doorify (); |
|
|
877 | maze.symmetrize (rmg_rndm (2, 4)); |
|
|
878 | maze.rotate (rmg_rndm (4)); |
|
|
879 | maze.expand2x (); |
|
|
880 | maze.print (); |
1027 | maze.print (); |
881 | } |
1028 | } |
882 | |
1029 | |
883 | exit (1); |
1030 | exit (1); |
884 | } |
1031 | } |