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