… | |
… | |
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 |
|
|
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); |
46 | } |
77 | } |
47 | |
78 | |
48 | void |
79 | void |
49 | Layout::fill (char fill) |
80 | layout::fill (char fill) |
50 | { |
81 | { |
51 | memset (col [0], fill, w * h); |
82 | //memset (data [0], fill, w * h); // only when contiguous :/ |
|
|
83 | fill_rect (0, 0, w, h, fill); |
52 | } |
84 | } |
53 | |
85 | |
54 | void |
86 | void |
55 | Layout::rect (int x1, int y1, int x2, int y2, char fill) |
87 | layout::rect (int x1, int y1, int x2, int y2, char fill) |
|
|
88 | { |
|
|
89 | --x2; |
|
|
90 | |
|
|
91 | memset (data [x1] + y1, fill, y2 - y1); |
|
|
92 | memset (data [x2] + y1, fill, y2 - y1); |
|
|
93 | |
|
|
94 | while (++x1 < x2) |
|
|
95 | data [x1][y1] = data [x1][y2 - 1] = fill; |
|
|
96 | } |
|
|
97 | |
|
|
98 | void |
|
|
99 | layout::fill_rect (int x1, int y1, int x2, int y2, char fill) |
56 | { |
100 | { |
57 | for (; x1 < x2; ++x1) |
101 | for (; x1 < x2; ++x1) |
58 | memset (col [x1] + y1, fill, y2 - y1); |
102 | memset (data [x1] + y1, fill, y2 - y1); |
59 | } |
103 | } |
60 | |
104 | |
61 | void Layout::border (char fill) |
105 | void layout::border (char fill) |
62 | { |
106 | { |
63 | for (int i = 0; i < w; i++) col [i][0] = col [i][h - 1] = fill; |
107 | rect (0, 0, w, h, fill); |
64 | for (int j = 0; j < h; j++) col [0][j] = col [w - 1][j] = fill; |
|
|
65 | } |
108 | } |
66 | |
109 | |
67 | void |
110 | void |
68 | Layout::fill_rand (int percent) |
111 | layout::fill_rand (int percent) |
69 | { |
112 | { |
70 | percent = lerp (percent, 0, 100, 0, 256); |
113 | percent = lerp (percent, 0, 100, 0, 256); |
71 | |
114 | |
72 | for (int x = w - 1; --x > 0; ) |
115 | for (int x = w - 1; --x > 0; ) |
73 | for (int y = h - 1; --y > 0; ) |
116 | for (int y = h - 1; --y > 0; ) |
74 | col [x][y] = rmg_rndm (256) > percent ? 0 : '#'; |
117 | data [x][y] = rmg_rndm (256) > percent ? 0 : '#'; |
75 | } |
118 | } |
76 | |
119 | |
77 | ///////////////////////////////////////////////////////////////////////////// |
120 | ///////////////////////////////////////////////////////////////////////////// |
78 | |
121 | |
79 | // erode by cellular automata |
122 | // erode by cellular automata |
80 | void |
123 | void |
81 | Layout::erode_1_2 (int c1, int c2, int repeat) |
124 | layout::erode_1_2 (int c1, int c2, int repeat) |
82 | { |
125 | { |
83 | Layout neu (w, h); |
126 | layout neu (w, h); |
84 | |
127 | |
85 | while (repeat--) |
128 | while (repeat--) |
86 | { |
129 | { |
87 | for (int x = 0; x < w; ++x) |
130 | for (int x = 0; x < w; ++x) |
88 | { |
131 | { |
… | |
… | |
104 | for (int i = array_length (dds); i--; ) |
147 | for (int i = array_length (dds); i--; ) |
105 | { |
148 | { |
106 | int nx = x + dds [i][0]; |
149 | int nx = x + dds [i][0]; |
107 | int ny = y + dds [i][1]; |
150 | int ny = y + dds [i][1]; |
108 | |
151 | |
109 | if (!IN_RANGE_EXC (nx, 0, w) || !IN_RANGE_EXC (ny, 0, h) || !col [nx][ny]) |
152 | if (!IN_RANGE_EXC (nx, 0, w) || !IN_RANGE_EXC (ny, 0, h) || !data [nx][ny]) |
110 | { |
153 | { |
111 | n1 += dds [i][2]; |
154 | n1 += dds [i][2]; |
112 | n2++; |
155 | n2++; |
113 | } |
156 | } |
114 | } |
157 | } |
… | |
… | |
122 | } |
165 | } |
123 | |
166 | |
124 | ///////////////////////////////////////////////////////////////////////////// |
167 | ///////////////////////////////////////////////////////////////////////////// |
125 | |
168 | |
126 | void |
169 | void |
127 | Layout::print () const |
170 | layout::print () const |
128 | { |
171 | { |
129 | for (int y = 0; y < h; y++) |
172 | for (int y = 0; y < h; y++) |
130 | { |
173 | { |
131 | for (int x = 0; x < w; x++) |
174 | for (int x = 0; x < w; x++) |
132 | { |
175 | { |
133 | U8 c = (U8)col [x][y]; |
176 | U8 c = (U8)data [x][y]; |
134 | |
177 | |
135 | if (!c) |
178 | if (!c) |
136 | c = ' '; |
179 | c = ' '; |
137 | else if (c < 10) |
180 | else if (c < 10) |
138 | c += '0'; |
181 | c += '0'; |
… | |
… | |
151 | ///////////////////////////////////////////////////////////////////////////// |
194 | ///////////////////////////////////////////////////////////////////////////// |
152 | // isolation remover - ensures single connected area |
195 | // isolation remover - ensures single connected area |
153 | |
196 | |
154 | typedef fixed_stack<point> pointlist; |
197 | typedef fixed_stack<point> pointlist; |
155 | |
198 | |
156 | static noinline void |
199 | static void noinline |
157 | push_flood_fill (Layout &dist, pointlist &seeds, int x, int y) |
200 | push_flood_fill (layout &dist, pointlist &seeds, int x, int y) |
158 | { |
201 | { |
159 | if (dist [x][y]) |
202 | if (dist [x][y]) |
160 | return; |
203 | return; |
161 | |
204 | |
162 | while (y > 0 && !dist [x][y - 1]) |
205 | while (y > 0 && !dist [x][y - 1]) |
… | |
… | |
172 | ++y; |
215 | ++y; |
173 | } |
216 | } |
174 | |
217 | |
175 | while (--y >= y0) |
218 | while (--y >= y0) |
176 | { |
219 | { |
177 | if (x > 0) push_flood_fill (dist, seeds, x - 1, y); |
220 | 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); |
221 | if (x < dist.w - 1 && !dist [x + 1][y]) push_flood_fill (dist, seeds, x + 1, y); |
179 | } |
222 | } |
180 | } |
223 | } |
181 | |
224 | |
182 | static inline void |
225 | static inline void |
183 | make_tunnel (Layout &dist, pointlist &seeds, int x, int y, U8 d) |
226 | make_tunnel (layout &dist, pointlist &seeds, int x, int y, U8 d) |
184 | { |
227 | { |
185 | for (;;) |
228 | for (;;) |
186 | { |
229 | { |
|
|
230 | point neigh[4]; |
|
|
231 | int ncnt = 0; |
|
|
232 | |
187 | if (x > 1 && U8 (dist [x - 1][y]) < d && dist [x - 1][y] > 1) |
233 | 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) |
234 | 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) |
235 | 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) |
236 | if (y < dist.h - 1 && U8 (dist [x][y + 1]) <= d && dist [x][y + 1] > 1) neigh [ncnt++] = point (x, y + 1); |
194 | ++y; |
237 | |
195 | else |
238 | if (!ncnt) |
196 | break; |
239 | return; |
|
|
240 | |
|
|
241 | point &p = neigh [rmg_rndm (ncnt)]; |
|
|
242 | |
|
|
243 | seeds.push (p); |
|
|
244 | |
|
|
245 | x = p.x; |
|
|
246 | y = p.y; |
197 | |
247 | |
198 | d = dist [x][y]; |
248 | d = dist [x][y]; |
199 | dist [x][y] = 1; |
249 | dist [x][y] = 1; |
200 | seeds.push (point (x, y)); |
|
|
201 | } |
250 | } |
202 | } |
251 | } |
203 | |
252 | |
204 | static void inline |
253 | static void inline |
205 | maybe_push (Layout &dist, pointlist &seeds, int x, int y, U8 d) |
254 | maybe_push (layout &dist, pointlist &seeds, int x, int y, U8 d) |
206 | { |
255 | { |
207 | char &D = dist [x][y]; |
256 | char &D = dist [x][y]; |
208 | |
257 | |
209 | if (U8 (D) > d) // if wall and higher distance, lower distance |
258 | if (U8 (D) > d) // if wall and higher distance, lower distance |
210 | D = d; |
259 | D = d; |
… | |
… | |
212 | return; |
261 | return; |
213 | |
262 | |
214 | seeds.push (point (x, y)); |
263 | seeds.push (point (x, y)); |
215 | } |
264 | } |
216 | |
265 | |
217 | void |
266 | // isolation remover, works on a "distance" map |
218 | Layout::isolation_remover (bool dirty) |
267 | // the map must be initialised with 0 == rooms, 255 = walls |
|
|
268 | static void noinline |
|
|
269 | isolation_remover (layout &dist) |
219 | { |
270 | { |
220 | Layout dist (w, h); |
|
|
221 | |
|
|
222 | // dist contains |
271 | // dist contains |
223 | // 0 == invisited rooms |
272 | // 0 == invisited rooms |
224 | // 1 == visited rooms |
273 | // 1 == visited rooms |
225 | // 2+ shortest distance to random near room |
274 | // 2+ shortest distance to random near room |
226 | |
275 | |
227 | // phase 1, initialise dist array, find seed |
276 | // phase 1, find seed |
228 | int cnt = 0; |
277 | int cnt = 0; |
229 | int x, y; |
278 | int x, y; |
230 | |
279 | |
231 | for (int i = 0; i < w; ++i) |
280 | for (int i = 0; i < dist.w; ++i) |
232 | for (int j = 0; j < h; ++j) |
281 | for (int j = 0; j < dist.h; ++j) |
233 | { |
282 | 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; |
283 | x = i, y = j; |
241 | } |
|
|
242 | } |
|
|
243 | |
284 | |
244 | if (!cnt) |
285 | if (!cnt) |
245 | { |
286 | { |
246 | // map is completely massive, this is not good, |
287 | // map is completely massive, this is not good, |
247 | // so make it empty instead. |
288 | // so make it empty instead. |
248 | clear (); |
289 | dist.fill (1); |
249 | border (); |
|
|
250 | return; |
290 | return; |
251 | } |
291 | } |
252 | |
292 | |
253 | fixed_stack<point> seeds (w * h * 5); |
293 | fixed_stack<point> seeds (dist.w * dist.h * 5); |
254 | |
294 | |
255 | // found first free space - picking the first one gives |
295 | // found first free space - picking the first one gives |
256 | // us a slight bias for tunnels, but usually you won't |
296 | // us a slight bias for tunnels, but usually you won't |
257 | // notice that in-game |
297 | // notice that in-game |
258 | seeds.push (point (x, y)); |
298 | seeds.push (point (x, y)); |
… | |
… | |
270 | y = p.y; |
310 | y = p.y; |
271 | |
311 | |
272 | if (!dist [x][y]) |
312 | if (!dist [x][y]) |
273 | { |
313 | { |
274 | // found new isolated area, make tunnel |
314 | // found new isolated area, make tunnel |
275 | if (!dirty) |
|
|
276 | push_flood_fill (dist, seeds, x, y); |
315 | push_flood_fill (dist, seeds, x, y); |
277 | |
|
|
278 | make_tunnel (dist, seeds, x, y, 255); |
316 | make_tunnel (dist, seeds, x, y, 255); |
279 | |
|
|
280 | if (dirty) |
|
|
281 | push_flood_fill (dist, seeds, x, y); |
|
|
282 | } |
317 | } |
283 | else |
318 | else |
284 | { |
319 | { |
285 | // nothing here, continue to expand |
320 | // nothing here, continue to expand |
286 | U8 d = U8 (dist [x][y]) + 1; |
321 | U8 d = U8 (dist [x][y]) + 1; |
… | |
… | |
289 | if (x > 0) maybe_push (dist, seeds, x - 1, y, d); |
324 | 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); |
325 | 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); |
326 | if (y > 0) maybe_push (dist, seeds, x, y - 1, d); |
292 | } |
327 | } |
293 | } |
328 | } |
|
|
329 | } |
|
|
330 | |
|
|
331 | void |
|
|
332 | layout::isolation_remover () |
|
|
333 | { |
|
|
334 | layout dist (w - 2, h - 2); // map without border |
|
|
335 | |
|
|
336 | for (int x = 1; x < w - 1; ++x) |
|
|
337 | for (int y = 1; y < h - 1; ++y) |
|
|
338 | dist [x - 1][y - 1] = data [x][y] == '#' ? U8 (255) : 0; |
|
|
339 | |
|
|
340 | ::isolation_remover (dist); |
294 | |
341 | |
295 | // now copy the tunnels over |
342 | // now copy the tunnels over |
296 | for (int x = 0; x < w; ++x) |
343 | for (int x = 1; x < w - 1; ++x) |
297 | for (int y = 0; y < h; ++y) |
344 | for (int y = 1; y < h - 1; ++y) |
298 | if (col [x][y] == '#' && dist [x][y] == 1) |
345 | if (data [x][y] == '#' && dist [x - 1][y - 1] == 1) |
299 | col [x][y] = 0; |
346 | data [x][y] = 0; |
300 | } |
347 | } |
301 | |
348 | |
302 | ///////////////////////////////////////////////////////////////////////////// |
349 | ///////////////////////////////////////////////////////////////////////////// |
303 | |
350 | |
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 | } |
|
|
341 | |
|
|
342 | ///////////////////////////////////////////////////////////////////////////// |
|
|
343 | |
|
|
344 | //+GPL |
351 | //+GPL |
345 | |
352 | |
346 | /* puts doors at appropriate locations in a layout. */ |
353 | /* puts doors at appropriate locations in a maze. */ |
347 | void |
354 | void |
348 | Layout::doorify () |
355 | layout::doorify () |
349 | { |
356 | { |
350 | int ndoors = w * h / 60; /* reasonable number of doors. */ |
357 | int ndoors = w * h / 60; /* reasonable number of doors. */ |
351 | int doorlocs = 0; /* # of available doorlocations */ |
358 | int doorlocs = 0; /* # of available doorlocations */ |
352 | |
359 | |
353 | uint16 *doorlist_x = salloc<uint16> (w * h); |
360 | uint16 *doorlist_x = salloc<uint16> (w * h); |
… | |
… | |
374 | int j = doorlist_y [di]; |
381 | int j = doorlist_y [di]; |
375 | int sindex = surround_flag (*this, i, j); |
382 | int sindex = surround_flag (*this, i, j); |
376 | |
383 | |
377 | if (sindex == 3 || sindex == 12) /* these are possible door sindex */ |
384 | if (sindex == 3 || sindex == 12) /* these are possible door sindex */ |
378 | { |
385 | { |
379 | col [i][j] = 'D'; |
386 | data [i][j] = 'D'; |
380 | ndoors--; |
387 | ndoors--; |
381 | } |
388 | } |
382 | |
389 | |
383 | /* reduce the size of the list */ |
390 | /* reduce the size of the list */ |
384 | doorlocs--; |
391 | doorlocs--; |
… | |
… | |
392 | |
399 | |
393 | /* takes a map and makes it symmetric: adjusts Xsize and |
400 | /* takes a map and makes it symmetric: adjusts Xsize and |
394 | * Ysize to produce a symmetric map. |
401 | * Ysize to produce a symmetric map. |
395 | */ |
402 | */ |
396 | void |
403 | void |
397 | Layout::symmetrize (int symmetry) |
404 | layout::symmetrize (int symmetry) |
398 | { |
405 | { |
399 | if (symmetry == SYMMETRY_NONE) |
406 | if (symmetry == SYMMETRY_NONE) |
400 | return; |
407 | return; |
401 | |
408 | |
402 | Layout sym_layout ( |
409 | layout sym_layout ( |
403 | symmetry == SYMMETRY_X || symmetry == SYMMETRY_XY ? w * 2 - 3 : w, |
410 | symmetry == SYMMETRY_X || symmetry == SYMMETRY_XY ? w * 2 - 3 : w, |
404 | symmetry == SYMMETRY_Y || symmetry == SYMMETRY_XY ? h * 2 - 3 : h |
411 | symmetry == SYMMETRY_Y || symmetry == SYMMETRY_XY ? h * 2 - 3 : h |
405 | ); |
412 | ); |
406 | |
413 | |
407 | if (symmetry == SYMMETRY_X) |
414 | if (symmetry == SYMMETRY_X) |
408 | for (int i = 0; i < sym_layout.w / 2 + 1; i++) |
415 | for (int i = 0; i < sym_layout.w / 2 + 1; i++) |
409 | for (int j = 0; j < sym_layout.h; j++) |
416 | for (int j = 0; j < sym_layout.h; j++) |
410 | { |
417 | { |
411 | sym_layout[i ][j] = |
418 | sym_layout[i ][j] = |
412 | sym_layout[sym_layout.w - i - 1][j] = col[i][j]; |
419 | sym_layout[sym_layout.w - i - 1][j] = data [i][j]; |
413 | } |
420 | } |
414 | |
421 | |
415 | if (symmetry == SYMMETRY_Y) |
422 | if (symmetry == SYMMETRY_Y) |
416 | for (int i = 0; i < sym_layout.w; i++) |
423 | for (int i = 0; i < sym_layout.w; i++) |
417 | for (int j = 0; j < sym_layout.h / 2 + 1; j++) |
424 | for (int j = 0; j < sym_layout.h / 2 + 1; j++) |
418 | { |
425 | { |
419 | sym_layout[i][j ] = |
426 | sym_layout[i][j ] = |
420 | sym_layout[i][sym_layout.h - j - 1] = col[i][j]; |
427 | sym_layout[i][sym_layout.h - j - 1] = data [i][j]; |
421 | } |
428 | } |
422 | |
429 | |
423 | if (symmetry == SYMMETRY_XY) |
430 | if (symmetry == SYMMETRY_XY) |
424 | for (int i = 0; i < sym_layout.w / 2 + 1; i++) |
431 | for (int i = 0; i < sym_layout.w / 2 + 1; i++) |
425 | for (int j = 0; j < sym_layout.h / 2 + 1; j++) |
432 | for (int j = 0; j < sym_layout.h / 2 + 1; j++) |
426 | { |
433 | { |
427 | sym_layout[i ][j ] = |
434 | sym_layout[i ][j ] = |
428 | sym_layout[i ][sym_layout.h - j - 1] = |
435 | sym_layout[i ][sym_layout.h - j - 1] = |
429 | sym_layout[sym_layout.w - i - 1][j ] = |
436 | sym_layout[sym_layout.w - i - 1][j ] = |
430 | sym_layout[sym_layout.w - i - 1][sym_layout.h - j - 1] = col[i][j]; |
437 | sym_layout[sym_layout.w - i - 1][sym_layout.h - j - 1] = data [i][j]; |
431 | } |
438 | } |
432 | |
439 | |
433 | /* need to run the isolation remover for some layouts */ |
440 | /* need to run the isolation remover for some layouts */ |
434 | #if 0 |
441 | #if 0 |
435 | switch (RP->map_layout_style) |
442 | switch (RP->map_layout_style) |
… | |
… | |
451 | } |
458 | } |
452 | |
459 | |
453 | //-GPL |
460 | //-GPL |
454 | |
461 | |
455 | void |
462 | void |
456 | Layout::rotate (int rotation) |
463 | layout::rotate (int rotation) |
457 | { |
464 | { |
458 | switch (rotation & 3) |
465 | switch (rotation & 3) |
459 | { |
466 | { |
460 | case 2: /* a reflection */ |
467 | case 2: /* a reflection */ |
461 | { |
468 | { |
462 | Layout new_layout (w, h); |
469 | layout new_layout (w, h); |
463 | |
470 | |
464 | for (int i = 0; i < w; i++) /* copy a reflection back */ |
471 | for (int i = 0; i < w; i++) /* copy a reflection back */ |
465 | for (int j = 0; j < h; j++) |
472 | for (int j = 0; j < h; j++) |
466 | new_layout [i][j] = col [w - i - 1][h - j - 1]; |
473 | new_layout [i][j] = data [w - i - 1][h - j - 1]; |
467 | |
474 | |
468 | swap (new_layout); |
475 | swap (new_layout); |
469 | } |
476 | } |
470 | break; |
477 | break; |
471 | |
478 | |
472 | case 1: |
479 | case 1: |
473 | case 3: |
480 | case 3: |
474 | { |
481 | { |
475 | Layout new_layout (h, w); |
482 | layout new_layout (h, w); |
476 | |
483 | |
477 | if (rotation == 1) /* swap x and y */ |
484 | if (rotation == 1) /* swap x and y */ |
478 | for (int i = 0; i < w; i++) |
485 | for (int i = 0; i < w; i++) |
479 | for (int j = 0; j < h; j++) |
486 | for (int j = 0; j < h; j++) |
480 | new_layout [j][i] = col [i][j]; |
487 | new_layout [j][i] = data [i][j]; |
481 | |
488 | |
482 | if (rotation == 3) /* swap x and y */ |
489 | if (rotation == 3) /* swap x and y */ |
483 | for (int i = 0; i < w; i++) |
490 | for (int i = 0; i < w; i++) |
484 | for (int j = 0; j < h; j++) |
491 | for (int j = 0; j < h; j++) |
485 | new_layout [j][i] = col [w - i - 1][h - j - 1]; |
492 | new_layout [j][i] = data [w - i - 1][h - j - 1]; |
486 | |
493 | |
487 | swap (new_layout); |
494 | swap (new_layout); |
488 | } |
495 | } |
489 | break; |
496 | break; |
490 | } |
497 | } |
… | |
… | |
493 | ///////////////////////////////////////////////////////////////////////////// |
500 | ///////////////////////////////////////////////////////////////////////////// |
494 | |
501 | |
495 | //+GPL |
502 | //+GPL |
496 | |
503 | |
497 | /* |
504 | /* |
498 | * Expands a layout by 2x in each dimension. |
505 | * Expands a maze by 2x in each dimension. |
499 | * H. S. Teoh |
506 | * H. S. Teoh |
500 | */ |
507 | */ |
501 | |
508 | |
502 | /* Copy the old tile X into the new one at location (i*2, j*2) and |
509 | /* Copy the old tile X into the new one at location (i*2, j*2) and |
503 | * fill up the rest of the 2x2 result with \0: |
510 | * fill up the rest of the 2x2 result with \0: |
504 | * X ---> X \0 |
511 | * X ---> X \0 |
505 | * \0 \0 |
512 | * \0 \0 |
506 | */ |
513 | */ |
507 | static void inline |
514 | static void inline |
508 | expand_misc (Layout &newlayout, int i, int j, Layout &layout) |
515 | expand_misc (layout &newlayout, int i, int j, layout &maze) |
509 | { |
516 | { |
510 | newlayout[i * 2 + rmg_rndm (1)][j * 2 + rmg_rndm (1)] = layout[i][j]; |
517 | newlayout[i * 2 + rmg_rndm (1)][j * 2 + rmg_rndm (1)] = maze[i][j]; |
511 | /* (Note: no need to reset rest of 2x2 area to \0 because calloc does that |
518 | /* (Note: no need to reset rest of 2x2 area to \0 because calloc does that |
512 | * for us.) */ |
519 | * for us.) */ |
513 | } |
520 | } |
514 | |
521 | |
515 | /* Returns a bitmap that represents which squares on the right and bottom |
522 | /* Returns a bitmap that represents which squares on the right and bottom |
… | |
… | |
518 | * 2 match on (i, j+1) |
525 | * 2 match on (i, j+1) |
519 | * 4 match on (i+1, j+1) |
526 | * 4 match on (i+1, j+1) |
520 | * and the possible combinations thereof. |
527 | * and the possible combinations thereof. |
521 | */ |
528 | */ |
522 | static int noinline |
529 | static int noinline |
523 | calc_pattern (char ch, Layout &layout, int i, int j) |
530 | calc_pattern (char ch, layout &maze, int i, int j) |
524 | { |
531 | { |
525 | int pattern = 0; |
532 | int pattern = 0; |
526 | |
533 | |
527 | if (i + 1 < layout.w && layout[i + 1][j] == ch) |
534 | if (i + 1 < maze.w && maze[i + 1][j] == ch) |
528 | pattern |= 1; |
535 | pattern |= 1; |
529 | |
536 | |
530 | if (j + 1 < layout.h) |
537 | if (j + 1 < maze.h) |
531 | { |
538 | { |
532 | if (layout[i][j + 1] == ch) |
539 | if (maze[i][j + 1] == ch) |
533 | pattern |= 2; |
540 | pattern |= 2; |
534 | |
541 | |
535 | if (i + 1 < layout.w && layout[i + 1][j + 1] == ch) |
542 | if (i + 1 < maze.w && maze[i + 1][j + 1] == ch) |
536 | pattern |= 4; |
543 | pattern |= 4; |
537 | } |
544 | } |
538 | |
545 | |
539 | return pattern; |
546 | return pattern; |
540 | } |
547 | } |
… | |
… | |
542 | /* Expand a wall. This function will try to sensibly connect the resulting |
549 | /* Expand a wall. This function will try to sensibly connect the resulting |
543 | * wall to adjacent wall squares, so that the result won't have disconnected |
550 | * wall to adjacent wall squares, so that the result won't have disconnected |
544 | * walls. |
551 | * walls. |
545 | */ |
552 | */ |
546 | static void inline |
553 | static void inline |
547 | expand_wall (Layout &newlayout, int i, int j, Layout &layout) |
554 | expand_wall (layout &newlayout, int i, int j, layout &maze) |
548 | { |
555 | { |
549 | int wall_pattern = calc_pattern ('#', layout, i, j); |
556 | int wall_pattern = calc_pattern ('#', maze, i, j); |
550 | int door_pattern = calc_pattern ('D', layout, i, j); |
557 | int door_pattern = calc_pattern ('D', maze, i, j); |
551 | int both_pattern = wall_pattern | door_pattern; |
558 | int both_pattern = wall_pattern | door_pattern; |
552 | |
559 | |
553 | newlayout[i * 2][j * 2] = '#'; |
560 | newlayout[i * 2][j * 2] = '#'; |
554 | |
561 | |
555 | if (i + 1 < layout.w) |
562 | if (i + 1 < maze.w) |
556 | { |
563 | { |
557 | if (both_pattern & 1) |
564 | if (both_pattern & 1) |
558 | { /* join walls/doors to the right */ |
565 | { /* join walls/doors to the right */ |
559 | /* newlayout[i*2+1][j*2] = '#'; */ |
566 | /* newlayout[i*2+1][j*2] = '#'; */ |
560 | newlayout[i * 2 + 1][j * 2] = layout[i + 1][j]; |
567 | newlayout[i * 2 + 1][j * 2] = maze[i + 1][j]; |
561 | } |
568 | } |
562 | } |
569 | } |
563 | |
570 | |
564 | if (j + 1 < layout.h) |
571 | if (j + 1 < maze.h) |
565 | { |
572 | { |
566 | if (both_pattern & 2) |
573 | if (both_pattern & 2) |
567 | { /* join walls/doors to the bottom */ |
574 | { /* join walls/doors to the bottom */ |
568 | /* newlayout[i*2][j*2+1] = '#'; */ |
575 | /* newlayout[i*2][j*2+1] = '#'; */ |
569 | newlayout[i * 2][j * 2 + 1] = layout[i][j + 1]; |
576 | newlayout[i * 2][j * 2 + 1] = maze[i][j + 1]; |
570 | } |
577 | } |
571 | |
578 | |
572 | if (wall_pattern == 7) |
579 | if (wall_pattern == 7) |
573 | { /* if orig layout is a 2x2 wall block, |
580 | { /* if orig maze is a 2x2 wall block, |
574 | * we fill the result with walls. */ |
581 | * we fill the result with walls. */ |
575 | newlayout[i * 2 + 1][j * 2 + 1] = '#'; |
582 | newlayout[i * 2 + 1][j * 2 + 1] = '#'; |
576 | } |
583 | } |
577 | } |
584 | } |
578 | } |
585 | } |
… | |
… | |
580 | /* This function will try to sensibly connect doors so that they meet up with |
587 | /* This function will try to sensibly connect doors so that they meet up with |
581 | * adjacent walls. Note that it will also presumptuously delete (ignore) doors |
588 | * adjacent walls. Note that it will also presumptuously delete (ignore) doors |
582 | * that it doesn't know how to correctly expand. |
589 | * that it doesn't know how to correctly expand. |
583 | */ |
590 | */ |
584 | static void inline |
591 | static void inline |
585 | expand_door (Layout &newlayout, int i, int j, Layout &layout) |
592 | expand_door (layout &newlayout, int i, int j, layout &maze) |
586 | { |
593 | { |
587 | int wall_pattern = calc_pattern ('#', layout, i, j); |
594 | int wall_pattern = calc_pattern ('#', maze, i, j); |
588 | int door_pattern = calc_pattern ('D', layout, i, j); |
595 | int door_pattern = calc_pattern ('D', maze, i, j); |
589 | int join_pattern; |
596 | int join_pattern; |
590 | |
597 | |
591 | /* Doors "like" to connect to walls more than other doors. If there is |
598 | /* Doors "like" to connect to walls more than other doors. If there is |
592 | * a wall and another door, this door will connect to the wall and |
599 | * a wall and another door, this door will connect to the wall and |
593 | * disconnect from the other door. */ |
600 | * disconnect from the other door. */ |
… | |
… | |
596 | else |
603 | else |
597 | join_pattern = door_pattern; |
604 | join_pattern = door_pattern; |
598 | |
605 | |
599 | newlayout[i * 2][j * 2] = 'D'; |
606 | newlayout[i * 2][j * 2] = 'D'; |
600 | |
607 | |
601 | if (i + 1 < layout.w) |
608 | if (i + 1 < maze.w) |
602 | if (join_pattern & 1) |
609 | if (join_pattern & 1) |
603 | /* there is a door/wall to the right */ |
610 | /* there is a door/wall to the right */ |
604 | newlayout[i * 2 + 1][j * 2] = 'D'; |
611 | newlayout[i * 2 + 1][j * 2] = 'D'; |
605 | |
612 | |
606 | if (j + 1 < layout.h) |
613 | if (j + 1 < maze.h) |
607 | if (join_pattern & 2) |
614 | if (join_pattern & 2) |
608 | /* there is a door/wall below */ |
615 | /* there is a door/wall below */ |
609 | newlayout[i * 2][j * 2 + 1] = 'D'; |
616 | newlayout[i * 2][j * 2 + 1] = 'D'; |
610 | } |
617 | } |
611 | |
618 | |
612 | void |
619 | void |
613 | Layout::expand2x () |
620 | layout::expand2x () |
614 | { |
621 | { |
615 | Layout new_layout (w * 2 - 1, h * 2 - 1); |
622 | layout new_layout (w * 2 - 1, h * 2 - 1); |
616 | |
623 | |
617 | new_layout.clear (); |
624 | new_layout.clear (); |
618 | |
625 | |
619 | for (int i = 0; i < w; i++) |
626 | for (int i = 0; i < w; i++) |
620 | for (int j = 0; j < h; j++) |
627 | for (int j = 0; j < h; j++) |
621 | switch (col [i][j]) |
628 | switch (data [i][j]) |
622 | { |
629 | { |
623 | case '#': expand_wall (new_layout, i, j, *this); break; |
630 | case '#': expand_wall (new_layout, i, j, *this); break; |
624 | case 'D': expand_door (new_layout, i, j, *this); break; |
631 | case 'D': expand_door (new_layout, i, j, *this); break; |
625 | default: expand_misc (new_layout, i, j, *this); break; |
632 | default: expand_misc (new_layout, i, j, *this); break; |
626 | } |
633 | } |
… | |
… | |
628 | swap (new_layout); |
635 | swap (new_layout); |
629 | } |
636 | } |
630 | |
637 | |
631 | ///////////////////////////////////////////////////////////////////////////// |
638 | ///////////////////////////////////////////////////////////////////////////// |
632 | |
639 | |
633 | /* checks the layout to see if I can stick a horizontal(dir = 0) wall |
640 | /* checks the maze to see if I can stick a horizontal(dir = 0) wall |
634 | (or vertical, dir == 1) |
641 | (or vertical, dir == 1) |
635 | here which ends up on other walls sensibly. */ |
642 | here which ends up on other walls sensibly. */ |
636 | static int |
643 | static int |
637 | can_make_wall (const Layout &maze, int dx, int dy, int dir) |
644 | can_make_wall (const layout &maze, int dx, int dy, int dir) |
638 | { |
645 | { |
639 | int i1; |
646 | int i1; |
640 | int length = 0; |
647 | int length = 0; |
641 | |
648 | |
642 | /* dont make walls if we're on the edge. */ |
649 | /* dont make walls if we're on the edge. */ |
… | |
… | |
729 | |
736 | |
730 | return 0; |
737 | return 0; |
731 | } |
738 | } |
732 | |
739 | |
733 | void |
740 | void |
734 | Layout::roomify () |
741 | layout::roomify () |
735 | { |
742 | { |
736 | int tries = w * h / 30; |
743 | int tries = w * h / 30; |
737 | |
744 | |
738 | for (int ti = 0; ti < tries; ti++) |
745 | for (int ti = 0; ti < tries; ti++) |
739 | { |
746 | { |
… | |
… | |
766 | } |
773 | } |
767 | } |
774 | } |
768 | |
775 | |
769 | ///////////////////////////////////////////////////////////////////////////// |
776 | ///////////////////////////////////////////////////////////////////////////// |
770 | |
777 | |
|
|
778 | // inspired mostly by http://www.jimrandomh.org/misc/caves.txt |
|
|
779 | void |
|
|
780 | layout::gen_cave (int subtype) |
|
|
781 | { |
|
|
782 | switch (subtype) |
|
|
783 | { |
|
|
784 | // a rough cave |
|
|
785 | case 0: |
|
|
786 | fill_rand (rmg_rndm (85, 97)); |
|
|
787 | break; |
|
|
788 | |
|
|
789 | // corridors |
|
|
790 | case 1: |
|
|
791 | fill_rand (rmg_rndm (5, 40)); |
|
|
792 | erode_1_2 (5, 2, 10); |
|
|
793 | erode_1_2 (5, -1, 10); |
|
|
794 | erode_1_2 (5, 2, 1); |
|
|
795 | break; |
|
|
796 | |
|
|
797 | // somewhat open, roundish |
|
|
798 | case 2: |
|
|
799 | fill_rand (45); |
|
|
800 | erode_1_2 (5, 0, 5); |
|
|
801 | erode_1_2 (5, 1, 1); |
|
|
802 | break; |
|
|
803 | |
|
|
804 | // wide open, some room-like structures |
|
|
805 | case 3: |
|
|
806 | fill_rand (45); |
|
|
807 | erode_1_2 (5, 2, 4); |
|
|
808 | erode_1_2 (5, -1, 3); |
|
|
809 | break; |
|
|
810 | } |
|
|
811 | |
|
|
812 | border (); |
|
|
813 | isolation_remover (); |
|
|
814 | } |
|
|
815 | |
|
|
816 | static void |
|
|
817 | gen_mixed_ (layout &maze, random_map_params *RP, int dir) |
|
|
818 | { |
|
|
819 | if (maze.w < 20 && maze.h < 20 && !rmg_rndm (3)) |
|
|
820 | dir = 2; // stop recursion randomly |
|
|
821 | |
|
|
822 | if (dir == 0 && maze.w > 16) |
|
|
823 | { |
|
|
824 | int m = rmg_rndm (8, maze.w - 8); |
|
|
825 | |
|
|
826 | layout m1 (maze, 0, 0, m , maze.h); gen_mixed_ (m1, RP, !dir); |
|
|
827 | layout m2 (maze, m, 0, maze.w, maze.h); gen_mixed_ (m2, RP, !dir); |
|
|
828 | } |
|
|
829 | else if (dir == 1 && maze.h > 16) |
|
|
830 | { |
|
|
831 | int m = rmg_rndm (8, maze.h - 8); |
|
|
832 | |
|
|
833 | layout m1 (maze, 0, 0, maze.w, m ); gen_mixed_ (m1, RP, !dir); |
|
|
834 | layout m2 (maze, 0, m, maze.w, maze.h); gen_mixed_ (m2, RP, !dir); |
|
|
835 | } |
|
|
836 | else |
|
|
837 | { |
|
|
838 | RP->map_layout_style = rmg_rndm (NROFLAYOUTS - 2) + 1; |
|
|
839 | |
|
|
840 | if (RP->map_layout_style == LAYOUT_MULTIPLE) |
|
|
841 | ++RP->map_layout_style; |
|
|
842 | |
|
|
843 | maze.generate (RP); |
|
|
844 | } |
|
|
845 | } |
|
|
846 | |
|
|
847 | // recursive subdivision with random sublayouts |
|
|
848 | static void |
|
|
849 | gen_mixed (layout &maze, random_map_params *RP) |
|
|
850 | { |
|
|
851 | random_map_params &rp = *new random_map_params (RP); |
|
|
852 | gen_mixed_ (maze, &rp, rmg_rndm (2)); |
|
|
853 | delete &rp; |
|
|
854 | |
|
|
855 | maze.border (); |
|
|
856 | maze.isolation_remover (); |
|
|
857 | } |
|
|
858 | |
771 | /* function selects the layout function and gives it whatever |
859 | /* function selects the maze function and gives it whatever |
772 | arguments it needs. */ |
860 | arguments it needs. */ |
773 | void |
861 | void |
774 | Layout::generate (random_map_params *RP) |
862 | layout::generate (random_map_params *RP) |
775 | { |
863 | { |
776 | switch (RP->map_layout_style) |
864 | switch (RP->map_layout_style) |
777 | { |
865 | { |
778 | case LAYOUT_ONION: |
866 | case LAYOUT_ONION: |
779 | map_gen_onion (*this, RP->layoutoptions1, RP->layoutoptions2); |
867 | map_gen_onion (*this, RP->layoutoptions1, RP->layoutoptions2); |
… | |
… | |
834 | if (rmg_rndm (2)) |
922 | if (rmg_rndm (2)) |
835 | doorify (); |
923 | doorify (); |
836 | |
924 | |
837 | break; |
925 | break; |
838 | |
926 | |
|
|
927 | case LAYOUT_MULTIPLE: |
|
|
928 | gen_mixed (*this, RP); |
|
|
929 | break; |
|
|
930 | |
839 | default: |
931 | default: |
840 | abort (); |
932 | abort (); |
841 | } |
933 | } |
842 | |
|
|
843 | /* rotate the layout randomly */ |
|
|
844 | rotate (rmg_rndm (4)); |
|
|
845 | |
|
|
846 | symmetrize (RP->symmetry_used); |
|
|
847 | |
|
|
848 | #if 0 |
|
|
849 | print ();//D |
|
|
850 | #endif |
|
|
851 | |
|
|
852 | if (RP->expand2x) |
|
|
853 | expand2x (); |
|
|
854 | } |
934 | } |
855 | |
935 | |
856 | //-GPL |
936 | //-GPL |
857 | |
937 | |
858 | #if 0 |
938 | #if 0 |
… | |
… | |
860 | { |
940 | { |
861 | demo () |
941 | demo () |
862 | { |
942 | { |
863 | rmg_rndm.seed (time (0)); |
943 | rmg_rndm.seed (time (0)); |
864 | |
944 | |
865 | for(int i=1;i<10;i) |
945 | for(int i=1;i<100;i++) |
866 | { |
946 | { |
867 | Layout maze (10, 10); |
947 | layout maze (40, 25); |
868 | maze.fill_rand (90); |
948 | maze.fill_rand (85); |
869 | maze.border (); |
949 | maze.border (); |
870 | maze.isolation_remover (); |
950 | maze.isolation_remover (); |
871 | maze.doorify (); |
|
|
872 | maze.symmetrize (rmg_rndm (2, 4)); |
|
|
873 | maze.rotate (rmg_rndm (4)); |
|
|
874 | maze.expand2x (); |
|
|
875 | maze.print (); |
951 | maze.print (); |
876 | } |
952 | } |
877 | |
953 | |
878 | exit (1); |
954 | exit (1); |
879 | } |
955 | } |