… | |
… | |
23 | |
23 | |
24 | #include <global.h> |
24 | #include <global.h> |
25 | #include <random_map.h> |
25 | #include <random_map.h> |
26 | #include <rproto.h> |
26 | #include <rproto.h> |
27 | |
27 | |
|
|
28 | void noinline |
|
|
29 | layout::alloc (int w, int h) |
|
|
30 | { |
|
|
31 | assert (sizeof (cell) == 1); |
|
|
32 | |
|
|
33 | this->w = w; |
|
|
34 | this->h = h; |
|
|
35 | |
|
|
36 | // we store the layout in a single contiguous memory layout |
|
|
37 | // first part consists of pointers to each column, followed |
|
|
38 | // by the actual columns (not rows!) |
|
|
39 | size = (sizeof (cell *) + sizeof (cell) * h) * w; |
|
|
40 | data = (cell **)salloc<char> (size); |
|
|
41 | |
|
|
42 | cell *p = (cell *)(data + w); |
|
|
43 | |
|
|
44 | for (int x = 0; x < w; ++x) |
|
|
45 | data [x] = p + x * h; |
|
|
46 | } |
|
|
47 | |
28 | layout::layout (int w, int h) |
48 | layout::layout (int w, int h) |
29 | : w(w), h(h) |
|
|
30 | { |
49 | { |
31 | int size = (sizeof (char *) + sizeof (char) * h) * w; |
50 | alloc (w, h); |
|
|
51 | } |
32 | |
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; |
33 | col = (char **)salloc<char> (size); |
67 | data = (cell **)salloc<char> (size); |
34 | |
68 | |
35 | char *data = (char *)(col + w); |
69 | // and now we point back into the original layout |
36 | |
|
|
37 | for (int x = w; x--; ) |
70 | for (int x = 0; x < w; ++x) |
38 | col [x] = data + x * h; |
71 | data [x] = orig.data [x + x1] + y1; |
39 | } |
72 | } |
40 | |
73 | |
41 | layout::~layout () |
74 | layout::~layout () |
42 | { |
75 | { |
43 | int size = (sizeof (char *) + sizeof (char) * h) * w; |
|
|
44 | |
|
|
45 | sfree ((char *)col, size); |
76 | sfree ((char *)data, size); |
|
|
77 | } |
|
|
78 | |
|
|
79 | void noinline |
|
|
80 | layout::fill (char fill) |
|
|
81 | { |
|
|
82 | //memset (data [0], fill, w * h); // only when contiguous :/ |
|
|
83 | fill_rect (0, 0, w, h, fill); |
|
|
84 | } |
|
|
85 | |
|
|
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 |
|
|
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) |
|
|
109 | { |
|
|
110 | for (; x1 < x2; ++x1) |
|
|
111 | memset (data [x1] + y1, fill, y2 - y1); |
46 | } |
112 | } |
47 | |
113 | |
48 | void |
114 | void |
49 | layout::fill (char fill) |
|
|
50 | { |
|
|
51 | memset (col [0], fill, w * h); |
|
|
52 | } |
|
|
53 | |
|
|
54 | void |
|
|
55 | layout::rect (int x1, int y1, int x2, int y2, char fill) |
|
|
56 | { |
|
|
57 | for (; x1 < x2; ++x1) |
|
|
58 | memset (col [x1] + y1, fill, y2 - y1); |
|
|
59 | } |
|
|
60 | |
|
|
61 | void layout::border (char fill) |
115 | layout::border (char fill) |
62 | { |
116 | { |
63 | for (int i = 0; i < w; i++) col [i][0] = col [i][h - 1] = fill; |
117 | rect (0, 0, w, h, fill); |
64 | for (int j = 0; j < h; j++) col [0][j] = col [w - 1][j] = fill; |
|
|
65 | } |
118 | } |
66 | |
119 | |
67 | void |
120 | void noinline |
68 | layout::fill_rand (int percent) |
121 | layout::fill_rand (int percent) |
69 | { |
122 | { |
70 | percent = lerp (percent, 0, 100, 0, 256); |
123 | percent = lerp (percent, 0, 100, 0, 256); |
71 | |
124 | |
72 | for (int x = w - 1; --x > 0; ) |
125 | for (int x = 0; x < w; ++x) |
73 | for (int y = h - 1; --y > 0; ) |
126 | for (int y = 0; y < h; ++y) |
74 | col [x][y] = rmg_rndm (256) > percent ? 0 : '#'; |
127 | data [x][y] = rmg_rndm (256) > percent ? 0 : '#'; |
75 | } |
128 | } |
76 | |
129 | |
77 | ///////////////////////////////////////////////////////////////////////////// |
130 | ///////////////////////////////////////////////////////////////////////////// |
78 | |
131 | |
79 | // erode by cellular automata |
132 | // erode by cellular automata |
80 | void |
133 | void noinline |
81 | layout::erode_1_2 (int c1, int c2, int repeat) |
134 | layout::erode_1_2 (int c1, int c2, int repeat) |
82 | { |
135 | { |
83 | layout neu (w, h); |
136 | layout neu (w, h); |
84 | |
137 | |
85 | while (repeat--) |
138 | while (repeat--) |
… | |
… | |
104 | for (int i = array_length (dds); i--; ) |
157 | for (int i = array_length (dds); i--; ) |
105 | { |
158 | { |
106 | int nx = x + dds [i][0]; |
159 | int nx = x + dds [i][0]; |
107 | int ny = y + dds [i][1]; |
160 | int ny = y + dds [i][1]; |
108 | |
161 | |
109 | if (!IN_RANGE_EXC (nx, 0, w) || !IN_RANGE_EXC (ny, 0, h) || !col [nx][ny]) |
162 | if (!IN_RANGE_EXC (nx, 0, w) || !IN_RANGE_EXC (ny, 0, h) || !data [nx][ny]) |
110 | { |
163 | { |
111 | n1 += dds [i][2]; |
164 | n1 += dds [i][2]; |
112 | n2++; |
165 | n2++; |
113 | } |
166 | } |
114 | } |
167 | } |
… | |
… | |
128 | { |
181 | { |
129 | for (int y = 0; y < h; y++) |
182 | for (int y = 0; y < h; y++) |
130 | { |
183 | { |
131 | for (int x = 0; x < w; x++) |
184 | for (int x = 0; x < w; x++) |
132 | { |
185 | { |
133 | U8 c = (U8)col [x][y]; |
186 | U8 c = (U8)data [x][y]; |
134 | |
187 | |
135 | if (!c) |
188 | if (!c) |
136 | c = ' '; |
189 | c = ' '; |
137 | else if (c < 10) |
190 | else if (c < 10) |
138 | c += '0'; |
191 | c += '0'; |
… | |
… | |
151 | ///////////////////////////////////////////////////////////////////////////// |
204 | ///////////////////////////////////////////////////////////////////////////// |
152 | // isolation remover - ensures single connected area |
205 | // isolation remover - ensures single connected area |
153 | |
206 | |
154 | typedef fixed_stack<point> pointlist; |
207 | typedef fixed_stack<point> pointlist; |
155 | |
208 | |
156 | static noinline void |
209 | static void noinline |
157 | push_flood_fill (layout &dist, pointlist &seeds, int x, int y) |
210 | push_flood_fill (layout &dist, pointlist &seeds, int x, int y) |
158 | { |
211 | { |
159 | if (dist [x][y]) |
212 | if (dist [x][y]) |
160 | return; |
213 | return; |
161 | |
214 | |
… | |
… | |
172 | ++y; |
225 | ++y; |
173 | } |
226 | } |
174 | |
227 | |
175 | while (--y >= y0) |
228 | while (--y >= y0) |
176 | { |
229 | { |
177 | 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); |
178 | 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); |
179 | } |
232 | } |
180 | } |
233 | } |
181 | |
234 | |
182 | static inline void |
235 | static void inline |
183 | 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) |
184 | { |
237 | { |
185 | for (;;) |
238 | for (;;) |
186 | { |
239 | { |
|
|
240 | point neigh[4]; |
|
|
241 | int ncnt = 0; |
|
|
242 | |
|
|
243 | d += perturb > 1; |
|
|
244 | |
187 | 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); |
188 | --x; |
|
|
189 | 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); |
190 | ++x; |
|
|
191 | 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); |
192 | --y; |
|
|
193 | 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); |
194 | ++y; |
249 | |
195 | else |
250 | if (!ncnt) |
196 | 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; |
197 | |
259 | |
198 | d = dist [x][y]; |
260 | d = dist [x][y]; |
199 | dist [x][y] = 1; |
261 | dist [x][y] = 1; |
200 | seeds.push (point (x, y)); |
|
|
201 | } |
262 | } |
202 | } |
263 | } |
203 | |
264 | |
204 | static void inline |
265 | static void inline |
205 | 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) |
… | |
… | |
212 | return; |
273 | return; |
213 | |
274 | |
214 | seeds.push (point (x, y)); |
275 | seeds.push (point (x, y)); |
215 | } |
276 | } |
216 | |
277 | |
217 | void |
278 | // isolation remover, works on a "distance" map |
218 | 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) |
219 | { |
282 | { |
220 | layout dist (w, h); |
|
|
221 | |
|
|
222 | // dist contains |
283 | // dist contains |
223 | // 0 == invisited rooms |
284 | // 0 == invisited rooms |
224 | // 1 == visited rooms |
285 | // 1 == visited rooms |
225 | // 2+ shortest distance to random near room |
286 | // 2+ shortest distance to random near room |
226 | |
287 | |
227 | // phase 1, initialise dist array, find seed |
288 | clamp_it (perturb, 0, 2); |
|
|
289 | |
|
|
290 | // phase 1, find seed |
228 | int cnt = 0; |
291 | int cnt = 0; |
229 | int x, y; |
292 | int x, y; |
230 | |
293 | |
231 | for (int i = 0; i < w; ++i) |
294 | for (int i = 0; i < dist.w; ++i) |
232 | for (int j = 0; j < h; ++j) |
295 | for (int j = 0; j < dist.h; ++j) |
233 | { |
296 | if (!dist [i][j] && !rmg_rndm (++cnt)) |
234 | if (col [i][j] == '#') |
|
|
235 | dist [i][j] = U8 (255); |
|
|
236 | else |
|
|
237 | { |
|
|
238 | dist [i][j] = 0; |
|
|
239 | if (!rmg_rndm (++cnt)) |
|
|
240 | x = i, y = j; |
297 | x = i, y = j; |
241 | } |
|
|
242 | } |
|
|
243 | |
298 | |
244 | if (!cnt) |
299 | if (!cnt) |
245 | { |
300 | { |
246 | // map is completely massive, this is not good, |
301 | // map is completely massive, this is not good, |
247 | // so make it empty instead. |
302 | // so make it empty instead. |
248 | clear (); |
303 | dist.fill (1); |
249 | border (); |
|
|
250 | return; |
304 | return; |
251 | } |
305 | } |
252 | |
306 | |
253 | fixed_stack<point> seeds (w * h * 5); |
307 | fixed_stack<point> seeds (dist.w * dist.h * 5); |
254 | |
308 | |
255 | // found first free space - picking the first one gives |
309 | // found first free space - picking the first one gives |
256 | // us a slight bias for tunnels, but usually you won't |
310 | // us a slight bias for tunnels, but usually you won't |
257 | // notice that in-game |
311 | // notice that in-game |
258 | seeds.push (point (x, y)); |
312 | seeds.push (point (x, y)); |
… | |
… | |
270 | y = p.y; |
324 | y = p.y; |
271 | |
325 | |
272 | if (!dist [x][y]) |
326 | if (!dist [x][y]) |
273 | { |
327 | { |
274 | // found new isolated area, make tunnel |
328 | // found new isolated area, make tunnel |
275 | if (!dirty) |
|
|
276 | push_flood_fill (dist, seeds, x, y); |
329 | push_flood_fill (dist, seeds, x, y); |
277 | |
|
|
278 | make_tunnel (dist, seeds, x, y, 255); |
330 | make_tunnel (dist, seeds, x, y, 254, perturb); |
279 | |
|
|
280 | if (dirty) |
|
|
281 | push_flood_fill (dist, seeds, x, y); |
|
|
282 | } |
331 | } |
283 | else |
332 | else |
284 | { |
333 | { |
285 | // nothing here, continue to expand |
334 | // nothing here, continue to expand |
286 | U8 d = U8 (dist [x][y]) + 1; |
335 | U8 d = U8 (dist [x][y]) + 1; |
… | |
… | |
289 | if (x > 0) maybe_push (dist, seeds, x - 1, y, d); |
338 | if (x > 0) maybe_push (dist, seeds, x - 1, y, d); |
290 | if (y < dist.h - 1) maybe_push (dist, seeds, x, y + 1, d); |
339 | if (y < dist.h - 1) maybe_push (dist, seeds, x, y + 1, d); |
291 | if (y > 0) maybe_push (dist, seeds, x, y - 1, d); |
340 | if (y > 0) maybe_push (dist, seeds, x, y - 1, d); |
292 | } |
341 | } |
293 | } |
342 | } |
|
|
343 | } |
|
|
344 | |
|
|
345 | void |
|
|
346 | layout::isolation_remover (int perturb) |
|
|
347 | { |
|
|
348 | layout dist (w - 2, h - 2); // map without border |
|
|
349 | |
|
|
350 | for (int x = 1; x < w - 1; ++x) |
|
|
351 | for (int y = 1; y < h - 1; ++y) |
|
|
352 | dist [x - 1][y - 1] = data [x][y] == '#' ? U8 (255) : 0; |
|
|
353 | |
|
|
354 | ::isolation_remover (dist, perturb); |
294 | |
355 | |
295 | // now copy the tunnels over |
356 | // now copy the tunnels over |
296 | for (int x = 0; x < w; ++x) |
357 | for (int x = 1; x < w - 1; ++x) |
297 | for (int y = 0; y < h; ++y) |
358 | for (int y = 1; y < h - 1; ++y) |
298 | if (col [x][y] == '#' && dist [x][y] == 1) |
359 | if (data [x][y] == '#' && dist [x - 1][y - 1] == 1) |
299 | col [x][y] = 0; |
360 | data [x][y] = 0; |
300 | } |
|
|
301 | |
|
|
302 | ///////////////////////////////////////////////////////////////////////////// |
|
|
303 | |
|
|
304 | // inspired mostly by http://www.jimrandomh.org/misc/caves.txt |
|
|
305 | void |
|
|
306 | layout::gen_cave (int subtype) |
|
|
307 | { |
|
|
308 | switch (subtype) |
|
|
309 | { |
|
|
310 | // a rough cave |
|
|
311 | case 0: |
|
|
312 | fill_rand (rmg_rndm (80, 95)); |
|
|
313 | break; |
|
|
314 | |
|
|
315 | // corridors |
|
|
316 | case 1: |
|
|
317 | fill_rand (rmg_rndm (5, 40)); |
|
|
318 | erode_1_2 (5, 2, 10); |
|
|
319 | erode_1_2 (5, -1, 10); |
|
|
320 | erode_1_2 (5, 2, 1); |
|
|
321 | break; |
|
|
322 | |
|
|
323 | // somewhat open, roundish |
|
|
324 | case 2: |
|
|
325 | fill_rand (45); |
|
|
326 | erode_1_2 (5, 0, 5); |
|
|
327 | erode_1_2 (5, 1, 1); |
|
|
328 | break; |
|
|
329 | |
|
|
330 | // wide open, some room-like structures |
|
|
331 | case 3: |
|
|
332 | fill_rand (45); |
|
|
333 | erode_1_2 (5, 2, 4); |
|
|
334 | erode_1_2 (5, -1, 3); |
|
|
335 | break; |
|
|
336 | } |
|
|
337 | |
|
|
338 | border (); |
|
|
339 | isolation_remover (); |
|
|
340 | } |
361 | } |
341 | |
362 | |
342 | ///////////////////////////////////////////////////////////////////////////// |
363 | ///////////////////////////////////////////////////////////////////////////// |
343 | |
364 | |
344 | //+GPL |
365 | //+GPL |
… | |
… | |
346 | /* puts doors at appropriate locations in a maze. */ |
367 | /* puts doors at appropriate locations in a maze. */ |
347 | void |
368 | void |
348 | layout::doorify () |
369 | layout::doorify () |
349 | { |
370 | { |
350 | int ndoors = w * h / 60; /* reasonable number of doors. */ |
371 | int ndoors = w * h / 60; /* reasonable number of doors. */ |
351 | int doorlocs = 0; /* # of available doorlocations */ |
|
|
352 | |
372 | |
353 | uint16 *doorlist_x = salloc<uint16> (w * h); |
373 | coroapi::cede_to_tick (); |
354 | uint16 *doorlist_y = salloc<uint16> (w * h); |
374 | |
|
|
375 | fixed_stack<point> doorloc (w * h); |
355 | |
376 | |
356 | /* make a list of possible door locations */ |
377 | /* make a list of possible door locations */ |
357 | for (int i = 1; i < w - 1; i++) |
378 | for (int i = 1; i < w - 1; i++) |
358 | for (int j = 1; j < h - 1; j++) |
379 | for (int j = 1; j < h - 1; j++) |
359 | { |
380 | { |
360 | int sindex = surround_flag (*this, i, j); |
381 | int sindex = surround_flag (*this, i, j); |
361 | |
382 | |
362 | if (sindex == 3 || sindex == 12) /* these are possible door sindex */ |
383 | if (sindex == 3 || sindex == 12) /* these are possible door sindex */ |
363 | { |
384 | doorloc.push (point (i, j)); |
364 | doorlist_x [doorlocs] = i; |
|
|
365 | doorlist_y [doorlocs] = j; |
|
|
366 | doorlocs++; |
|
|
367 | } |
|
|
368 | } |
385 | } |
369 | |
386 | |
370 | while (ndoors > 0 && doorlocs > 0) |
387 | while (ndoors && doorloc.size) |
371 | { |
388 | { |
372 | int di = rmg_rndm (doorlocs); |
389 | point p = doorloc.remove (rmg_rndm (doorloc.size)); |
373 | int i = doorlist_x [di]; |
390 | |
374 | int j = doorlist_y [di]; |
|
|
375 | int sindex = surround_flag (*this, i, j); |
391 | int sindex = surround_flag (*this, p.x, p.y); |
376 | |
392 | |
377 | if (sindex == 3 || sindex == 12) /* these are possible door sindex */ |
393 | if (sindex == 3 || sindex == 12) /* these are possible door sindex */ |
378 | { |
394 | { |
379 | col [i][j] = 'D'; |
395 | data [p.x][p.y] = 'D'; |
380 | ndoors--; |
396 | --ndoors; |
381 | } |
397 | } |
382 | |
|
|
383 | /* reduce the size of the list */ |
|
|
384 | doorlocs--; |
|
|
385 | doorlist_x[di] = doorlist_x [doorlocs]; |
|
|
386 | doorlist_y[di] = doorlist_y [doorlocs]; |
|
|
387 | } |
398 | } |
388 | |
|
|
389 | sfree (doorlist_x, w * h); |
|
|
390 | sfree (doorlist_y, w * h); |
|
|
391 | } |
399 | } |
392 | |
400 | |
393 | /* takes a map and makes it symmetric: adjusts Xsize and |
401 | /* takes a map and makes it symmetric: adjusts Xsize and |
394 | * Ysize to produce a symmetric map. |
402 | * Ysize to produce a symmetric map. |
395 | */ |
403 | */ |
… | |
… | |
407 | if (symmetry == SYMMETRY_X) |
415 | if (symmetry == SYMMETRY_X) |
408 | for (int i = 0; i < sym_layout.w / 2 + 1; i++) |
416 | for (int i = 0; i < sym_layout.w / 2 + 1; i++) |
409 | for (int j = 0; j < sym_layout.h; j++) |
417 | for (int j = 0; j < sym_layout.h; j++) |
410 | { |
418 | { |
411 | sym_layout[i ][j] = |
419 | sym_layout[i ][j] = |
412 | sym_layout[sym_layout.w - i - 1][j] = col[i][j]; |
420 | sym_layout[sym_layout.w - i - 1][j] = data [i][j]; |
413 | } |
421 | } |
414 | |
422 | |
415 | if (symmetry == SYMMETRY_Y) |
423 | if (symmetry == SYMMETRY_Y) |
416 | for (int i = 0; i < sym_layout.w; i++) |
424 | for (int i = 0; i < sym_layout.w; i++) |
417 | for (int j = 0; j < sym_layout.h / 2 + 1; j++) |
425 | for (int j = 0; j < sym_layout.h / 2 + 1; j++) |
418 | { |
426 | { |
419 | sym_layout[i][j ] = |
427 | sym_layout[i][j ] = |
420 | sym_layout[i][sym_layout.h - j - 1] = col[i][j]; |
428 | sym_layout[i][sym_layout.h - j - 1] = data [i][j]; |
421 | } |
429 | } |
422 | |
430 | |
423 | if (symmetry == SYMMETRY_XY) |
431 | if (symmetry == SYMMETRY_XY) |
424 | for (int i = 0; i < sym_layout.w / 2 + 1; i++) |
432 | for (int i = 0; i < sym_layout.w / 2 + 1; i++) |
425 | for (int j = 0; j < sym_layout.h / 2 + 1; j++) |
433 | for (int j = 0; j < sym_layout.h / 2 + 1; j++) |
426 | { |
434 | { |
427 | sym_layout[i ][j ] = |
435 | sym_layout[i ][j ] = |
428 | sym_layout[i ][sym_layout.h - j - 1] = |
436 | sym_layout[i ][sym_layout.h - j - 1] = |
429 | sym_layout[sym_layout.w - i - 1][j ] = |
437 | sym_layout[sym_layout.w - i - 1][j ] = |
430 | sym_layout[sym_layout.w - i - 1][sym_layout.h - j - 1] = col[i][j]; |
438 | sym_layout[sym_layout.w - i - 1][sym_layout.h - j - 1] = data [i][j]; |
431 | } |
439 | } |
432 | |
440 | |
433 | /* need to run the isolation remover for some layouts */ |
441 | /* need to run the isolation remover for some layouts */ |
434 | #if 0 |
442 | #if 0 |
435 | switch (RP->map_layout_style) |
443 | switch (RP->map_layout_style) |
… | |
… | |
453 | //-GPL |
461 | //-GPL |
454 | |
462 | |
455 | void |
463 | void |
456 | layout::rotate (int rotation) |
464 | layout::rotate (int rotation) |
457 | { |
465 | { |
|
|
466 | coroapi::cede_to_tick (); |
|
|
467 | |
458 | switch (rotation & 3) |
468 | switch (rotation & 3) |
459 | { |
469 | { |
460 | case 2: /* a reflection */ |
470 | case 2: /* a reflection */ |
461 | { |
471 | { |
462 | layout new_layout (w, h); |
472 | layout new_layout (w, h); |
463 | |
473 | |
464 | for (int i = 0; i < w; i++) /* copy a reflection back */ |
474 | for (int i = 0; i < w; i++) /* copy a reflection back */ |
465 | for (int j = 0; j < h; j++) |
475 | for (int j = 0; j < h; j++) |
466 | new_layout [i][j] = col [w - i - 1][h - j - 1]; |
476 | new_layout [i][j] = data [w - i - 1][h - j - 1]; |
467 | |
477 | |
468 | swap (new_layout); |
478 | swap (new_layout); |
469 | } |
479 | } |
470 | break; |
480 | break; |
471 | |
481 | |
… | |
… | |
475 | layout new_layout (h, w); |
485 | layout new_layout (h, w); |
476 | |
486 | |
477 | if (rotation == 1) /* swap x and y */ |
487 | if (rotation == 1) /* swap x and y */ |
478 | for (int i = 0; i < w; i++) |
488 | for (int i = 0; i < w; i++) |
479 | for (int j = 0; j < h; j++) |
489 | for (int j = 0; j < h; j++) |
480 | new_layout [j][i] = col [i][j]; |
490 | new_layout [j][i] = data [i][j]; |
481 | |
491 | |
482 | if (rotation == 3) /* swap x and y */ |
492 | if (rotation == 3) /* swap x and y */ |
483 | for (int i = 0; i < w; i++) |
493 | for (int i = 0; i < w; i++) |
484 | for (int j = 0; j < h; j++) |
494 | for (int j = 0; j < h; j++) |
485 | new_layout [j][i] = col [w - i - 1][h - j - 1]; |
495 | new_layout [j][i] = data [w - i - 1][h - j - 1]; |
486 | |
496 | |
487 | swap (new_layout); |
497 | swap (new_layout); |
488 | } |
498 | } |
489 | break; |
499 | break; |
490 | } |
500 | } |
… | |
… | |
614 | { |
624 | { |
615 | layout new_layout (w * 2 - 1, h * 2 - 1); |
625 | layout new_layout (w * 2 - 1, h * 2 - 1); |
616 | |
626 | |
617 | new_layout.clear (); |
627 | new_layout.clear (); |
618 | |
628 | |
|
|
629 | coroapi::cede_to_tick (); |
|
|
630 | |
619 | for (int i = 0; i < w; i++) |
631 | for (int i = 0; i < w; i++) |
620 | for (int j = 0; j < h; j++) |
632 | for (int j = 0; j < h; j++) |
621 | switch (col [i][j]) |
633 | switch (data [i][j]) |
622 | { |
634 | { |
623 | case '#': expand_wall (new_layout, i, j, *this); break; |
635 | case '#': expand_wall (new_layout, i, j, *this); break; |
624 | case 'D': expand_door (new_layout, i, j, *this); break; |
636 | case 'D': expand_door (new_layout, i, j, *this); break; |
625 | default: expand_misc (new_layout, i, j, *this); break; |
637 | default: expand_misc (new_layout, i, j, *this); break; |
626 | } |
638 | } |
… | |
… | |
705 | |
717 | |
706 | return -1; |
718 | return -1; |
707 | } |
719 | } |
708 | |
720 | |
709 | int |
721 | int |
710 | make_wall (char **maze, int x, int y, int dir) |
722 | make_wall (layout &maze, int x, int y, int dir) |
711 | { |
723 | { |
712 | maze[x][y] = 'D'; /* mark a door */ |
724 | maze[x][y] = 'D'; /* mark a door */ |
713 | |
725 | |
714 | switch (dir) |
726 | switch (dir) |
715 | { |
727 | { |
… | |
… | |
732 | |
744 | |
733 | void |
745 | void |
734 | layout::roomify () |
746 | layout::roomify () |
735 | { |
747 | { |
736 | int tries = w * h / 30; |
748 | int tries = w * h / 30; |
|
|
749 | |
|
|
750 | coroapi::cede_to_tick (); |
737 | |
751 | |
738 | for (int ti = 0; ti < tries; ti++) |
752 | for (int ti = 0; ti < tries; ti++) |
739 | { |
753 | { |
740 | /* starting location for looking at creating a door */ |
754 | /* starting location for looking at creating a door */ |
741 | int dx = rmg_rndm (w); |
755 | int dx = rmg_rndm (w); |
… | |
… | |
764 | else |
778 | else |
765 | make_wall (*this, dx, dy, 1); |
779 | make_wall (*this, dx, dy, 1); |
766 | } |
780 | } |
767 | } |
781 | } |
768 | |
782 | |
|
|
783 | //-GPL |
|
|
784 | |
769 | ///////////////////////////////////////////////////////////////////////////// |
785 | ///////////////////////////////////////////////////////////////////////////// |
|
|
786 | |
|
|
787 | // inspired mostly by http://www.jimrandomh.org/misc/caves.txt |
|
|
788 | void |
|
|
789 | layout::gen_cave (int subtype) |
|
|
790 | { |
|
|
791 | switch (subtype) |
|
|
792 | { |
|
|
793 | // a rough cave |
|
|
794 | case 0: |
|
|
795 | fill_rand (rmg_rndm (85, 97)); |
|
|
796 | break; |
|
|
797 | |
|
|
798 | // corridors |
|
|
799 | case 1: |
|
|
800 | fill_rand (rmg_rndm (5, 40)); |
|
|
801 | erode_1_2 (5, 2, 10); |
|
|
802 | erode_1_2 (5, -1, 10); |
|
|
803 | erode_1_2 (5, 2, 1); |
|
|
804 | break; |
|
|
805 | |
|
|
806 | // somewhat open, some room-like structures |
|
|
807 | case 2: |
|
|
808 | fill_rand (45); |
|
|
809 | erode_1_2 (5, 2, 4); |
|
|
810 | erode_1_2 (5, -1, 3); |
|
|
811 | break; |
|
|
812 | |
|
|
813 | // wide open, roundish |
|
|
814 | case 3: |
|
|
815 | fill_rand (45); |
|
|
816 | erode_1_2 (5, 0, 5); |
|
|
817 | erode_1_2 (5, 1, 1); |
|
|
818 | break; |
|
|
819 | } |
|
|
820 | |
|
|
821 | border (); |
|
|
822 | isolation_remover (1); |
|
|
823 | } |
|
|
824 | |
|
|
825 | void |
|
|
826 | layout::gen_castle () |
|
|
827 | { |
|
|
828 | fill ('#'); |
|
|
829 | |
|
|
830 | for (int n = w * h / 30 + 1; n--; ) |
|
|
831 | { |
|
|
832 | int rw = rmg_rndm (6, 10); |
|
|
833 | int rh = rmg_rndm (6, 10); |
|
|
834 | |
|
|
835 | if (rw > w || rh > h) |
|
|
836 | continue; |
|
|
837 | |
|
|
838 | int rx = rmg_rndm (0, w - rw); |
|
|
839 | int ry = rmg_rndm (0, h - rh); |
|
|
840 | |
|
|
841 | rect (rx, ry, rx + rw, ry + rh, '#'); |
|
|
842 | fill_rect (rx + 1, ry + 1, rx + rw - 1, ry + rh - 1, 0); |
|
|
843 | } |
|
|
844 | |
|
|
845 | border (); |
|
|
846 | isolation_remover (0); |
|
|
847 | } |
|
|
848 | |
|
|
849 | static void |
|
|
850 | gen_mixed_ (layout &maze, random_map_params *RP) |
|
|
851 | { |
|
|
852 | if (maze.w > maze.h && maze.w > 16) |
|
|
853 | { |
|
|
854 | int m = rmg_rndm (8, maze.w - 8); |
|
|
855 | |
|
|
856 | layout m1 (maze, 0, 0, m , maze.h); gen_mixed_ (m1, RP); |
|
|
857 | layout m2 (maze, m, 0, maze.w, maze.h); gen_mixed_ (m2, RP); |
|
|
858 | } |
|
|
859 | else if (maze.h > 16) |
|
|
860 | { |
|
|
861 | int m = rmg_rndm (8, maze.h - 8); |
|
|
862 | |
|
|
863 | layout m1 (maze, 0, 0, maze.w, m ); gen_mixed_ (m1, RP); |
|
|
864 | layout m2 (maze, 0, m, maze.w, maze.h); gen_mixed_ (m2, RP); |
|
|
865 | } |
|
|
866 | else |
|
|
867 | { |
|
|
868 | RP->map_layout_style = rmg_rndm (NROFLAYOUTS - 2) + 1; |
|
|
869 | |
|
|
870 | if (RP->map_layout_style == LAYOUT_MULTIPLE) |
|
|
871 | ++RP->map_layout_style; |
|
|
872 | |
|
|
873 | maze.generate (RP); |
|
|
874 | } |
|
|
875 | |
|
|
876 | coroapi::cede_to_tick (); |
|
|
877 | } |
|
|
878 | |
|
|
879 | // recursive subdivision with random sublayouts |
|
|
880 | static void |
|
|
881 | gen_mixed (layout &maze, random_map_params *RP) |
|
|
882 | { |
|
|
883 | random_map_params &rp = *new random_map_params (RP); |
|
|
884 | gen_mixed_ (maze, &rp); |
|
|
885 | delete &rp; |
|
|
886 | |
|
|
887 | maze.border (); |
|
|
888 | |
|
|
889 | // exits currently do not work so well, as they |
|
|
890 | // are currently often found together, so nuke entrances |
|
|
891 | maze.replace ('<', ' '); |
|
|
892 | |
|
|
893 | maze.isolation_remover (0); |
|
|
894 | } |
|
|
895 | |
|
|
896 | //+GPL |
770 | |
897 | |
771 | /* function selects the maze function and gives it whatever |
898 | /* function selects the maze function and gives it whatever |
772 | arguments it needs. */ |
899 | arguments it needs. */ |
773 | void |
900 | void |
774 | layout::generate (random_map_params *RP) |
901 | layout::generate (random_map_params *RP) |
… | |
… | |
834 | if (rmg_rndm (2)) |
961 | if (rmg_rndm (2)) |
835 | doorify (); |
962 | doorify (); |
836 | |
963 | |
837 | break; |
964 | break; |
838 | |
965 | |
|
|
966 | case LAYOUT_CASTLE: |
|
|
967 | gen_castle (); |
|
|
968 | |
|
|
969 | if (rmg_rndm (2)) |
|
|
970 | doorify (); |
|
|
971 | |
|
|
972 | break; |
|
|
973 | |
|
|
974 | case LAYOUT_MULTIPLE: |
|
|
975 | gen_mixed (*this, RP); |
|
|
976 | break; |
|
|
977 | |
839 | default: |
978 | default: |
840 | abort (); |
979 | abort (); |
841 | } |
980 | } |
|
|
981 | } |
842 | |
982 | |
843 | /* rotate the maze randomly */ |
983 | //-GPL |
844 | rotate (rmg_rndm (4)); |
|
|
845 | |
|
|
846 | symmetrize (RP->symmetry_used); |
|
|
847 | |
984 | |
848 | #if 0 |
985 | #if 0 |
849 | print ();//D |
986 | static void |
850 | #endif |
987 | gen_village (layout &maze) |
|
|
988 | { |
|
|
989 | maze.clear (); |
|
|
990 | maze.border (); |
851 | |
991 | |
852 | if (RP->expand2x) |
992 | for (int n = maze.w * maze.h / 200 + 1; n--; ) |
853 | expand2x (); |
993 | { |
854 | } |
994 | int rw = rmg_rndm (6, 10); |
|
|
995 | int rh = rmg_rndm (6, 10); |
855 | |
996 | |
856 | //-GPL |
997 | int rx = rmg_rndm (2, maze.w - rw - 2); |
|
|
998 | int ry = rmg_rndm (2, maze.h - rh - 2); |
857 | |
999 | |
858 | #if 0 |
1000 | maze.rect (rx, ry, rx + rw, ry + rh, '#'); |
|
|
1001 | } |
|
|
1002 | |
|
|
1003 | maze.border (); |
|
|
1004 | maze.isolation_remover (2); |
|
|
1005 | } |
|
|
1006 | |
859 | static struct demo |
1007 | static struct demo |
860 | { |
1008 | { |
861 | demo () |
1009 | demo () |
862 | { |
1010 | { |
863 | rmg_rndm.seed (time (0)); |
1011 | rmg_rndm.seed (time (0)); |
864 | |
1012 | |
865 | for(int i=1;i<10;i) |
1013 | for(int i=1;i<100;i++) |
866 | { |
1014 | { |
867 | layout maze (10, 10); |
1015 | layout maze (40, 30); |
868 | maze.fill_rand (90); |
1016 | gen_village (maze); |
869 | maze.border (); |
|
|
870 | maze.isolation_remover (); |
|
|
871 | maze.doorify (); |
1017 | maze.doorify (); |
872 | maze.symmetrize (rmg_rndm (2, 4)); |
|
|
873 | maze.rotate (rmg_rndm (4)); |
|
|
874 | maze.expand2x (); |
|
|
875 | maze.print (); |
1018 | maze.print (); |
|
|
1019 | exit(0); |
876 | } |
1020 | } |
877 | |
1021 | |
878 | exit (1); |
1022 | exit (1); |
879 | } |
1023 | } |
880 | } demo; |
1024 | } demo; |