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