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