1 | /* |
1 | /* |
2 | * This file is part of Deliantra, the Roguelike Realtime MMORPG. |
2 | * This file is part of Deliantra, the Roguelike Realtime MMORPG. |
3 | * |
3 | * |
4 | * Copyright (©) 2010 Marc Alexander Lehmann / Robin Redeker / the Deliantra team |
4 | * Copyright (©) 2010 Marc Alexander Lehmann / Robin Redeker / the Deliantra team |
|
|
5 | * Copyright (©) Crossfire Development Team (restored, original file without copyright notice) |
5 | * |
6 | * |
6 | * Deliantra is free software: you can redistribute it and/or modify it under |
7 | * Deliantra is free software: you can redistribute it and/or modify it under |
7 | * the terms of the Affero GNU General Public License as published by the |
8 | * the terms of the Affero GNU General Public License as published by the |
8 | * Free Software Foundation, either version 3 of the License, or (at your |
9 | * Free Software Foundation, either version 3 of the License, or (at your |
9 | * option) any later version. |
10 | * option) any later version. |
… | |
… | |
20 | * The authors can be reached via e-mail to <support@deliantra.net> |
21 | * The authors can be reached via e-mail to <support@deliantra.net> |
21 | */ |
22 | */ |
22 | |
23 | |
23 | #include <global.h> |
24 | #include <global.h> |
24 | #include <random_map.h> |
25 | #include <random_map.h> |
|
|
26 | #include <rproto.h> |
25 | |
27 | |
26 | Layout::Layout (int w, int h) |
28 | layout::layout (int w, int h) |
27 | : w(w), h(h) |
29 | : w(w), h(h) |
28 | { |
30 | { |
29 | int size = (sizeof (char *) + sizeof (char) * h) * w; |
31 | int size = (sizeof (char *) + sizeof (char) * h) * w; |
30 | |
32 | |
31 | col = (char **)salloc<char> (size); |
33 | col = (char **)salloc<char> (size); |
… | |
… | |
34 | |
36 | |
35 | for (int x = w; x--; ) |
37 | for (int x = w; x--; ) |
36 | col [x] = data + x * h; |
38 | col [x] = data + x * h; |
37 | } |
39 | } |
38 | |
40 | |
39 | Layout::~Layout () |
41 | layout::~layout () |
40 | { |
42 | { |
41 | int size = (sizeof (char *) + sizeof (char) * h) * w; |
43 | int size = (sizeof (char *) + sizeof (char) * h) * w; |
42 | |
44 | |
43 | sfree ((char *)col, size); |
45 | sfree ((char *)col, size); |
44 | } |
46 | } |
45 | |
47 | |
46 | void |
48 | void |
47 | Layout::fill (char fill) |
49 | layout::fill (char fill) |
48 | { |
50 | { |
49 | memset (col [0], fill, w * h); |
51 | memset (col [0], fill, w * h); |
50 | } |
52 | } |
51 | |
53 | |
52 | void |
54 | void |
53 | Layout::rect (int x1, int y1, int x2, int y2, char fill) |
55 | layout::rect (int x1, int y1, int x2, int y2, char fill) |
54 | { |
56 | { |
55 | for (; x1 < x2; ++x1) |
57 | for (; x1 < x2; ++x1) |
56 | memset (col [x1] + y1, fill, y2 - y1); |
58 | memset (col [x1] + y1, fill, y2 - y1); |
57 | } |
59 | } |
58 | |
60 | |
59 | void Layout::border (char fill) |
61 | void layout::border (char fill) |
60 | { |
62 | { |
61 | for (int i = 0; i < w; i++) col [i][0] = col [i][h - 1] = fill; |
63 | for (int i = 0; i < w; i++) col [i][0] = col [i][h - 1] = fill; |
62 | for (int j = 0; j < h; j++) col [0][j] = col [w - 1][j] = fill; |
64 | for (int j = 0; j < h; j++) col [0][j] = col [w - 1][j] = fill; |
63 | } |
65 | } |
64 | |
66 | |
65 | void |
67 | void |
66 | Layout::fill_rand (int percent) |
68 | layout::fill_rand (int percent) |
67 | { |
69 | { |
68 | percent = lerp (percent, 0, 100, 0, 256); |
70 | percent = lerp (percent, 0, 100, 0, 256); |
69 | |
71 | |
70 | for (int x = w - 1; --x > 0; ) |
72 | for (int x = w - 1; --x > 0; ) |
71 | for (int y = h - 1; --y > 0; ) |
73 | for (int y = h - 1; --y > 0; ) |
… | |
… | |
74 | |
76 | |
75 | ///////////////////////////////////////////////////////////////////////////// |
77 | ///////////////////////////////////////////////////////////////////////////// |
76 | |
78 | |
77 | // erode by cellular automata |
79 | // erode by cellular automata |
78 | void |
80 | void |
79 | Layout::erode_1_2 (int c1, int c2, int repeat) |
81 | layout::erode_1_2 (int c1, int c2, int repeat) |
80 | { |
82 | { |
81 | Layout neu (w, h); |
83 | layout neu (w, h); |
82 | |
84 | |
83 | while (repeat--) |
85 | while (repeat--) |
84 | { |
86 | { |
85 | for (int x = 0; x < w; ++x) |
87 | for (int x = 0; x < w; ++x) |
86 | { |
88 | { |
… | |
… | |
120 | } |
122 | } |
121 | |
123 | |
122 | ///////////////////////////////////////////////////////////////////////////// |
124 | ///////////////////////////////////////////////////////////////////////////// |
123 | |
125 | |
124 | void |
126 | void |
125 | Layout::print () |
127 | layout::print () const |
126 | { |
128 | { |
127 | for (int y = 0; y < h; y++) |
129 | for (int y = 0; y < h; y++) |
128 | { |
130 | { |
129 | for (int x = 0; x < w; x++) |
131 | for (int x = 0; x < w; x++) |
130 | { |
132 | { |
… | |
… | |
150 | // isolation remover - ensures single connected area |
152 | // isolation remover - ensures single connected area |
151 | |
153 | |
152 | typedef fixed_stack<point> pointlist; |
154 | typedef fixed_stack<point> pointlist; |
153 | |
155 | |
154 | static noinline void |
156 | static noinline void |
155 | push_flood_fill (Layout &dist, pointlist &seeds, int x, int y) |
157 | push_flood_fill (layout &dist, pointlist &seeds, int x, int y) |
156 | { |
158 | { |
157 | if (dist [x][y]) |
159 | if (dist [x][y]) |
158 | return; |
160 | return; |
159 | |
161 | |
160 | while (y > 0 && !dist [x][y - 1]) |
162 | while (y > 0 && !dist [x][y - 1]) |
… | |
… | |
176 | if (x < dist.w - 1) push_flood_fill (dist, seeds, x + 1, y); |
178 | if (x < dist.w - 1) push_flood_fill (dist, seeds, x + 1, y); |
177 | } |
179 | } |
178 | } |
180 | } |
179 | |
181 | |
180 | static inline void |
182 | static inline void |
181 | make_tunnel (Layout &dist, pointlist &seeds, int x, int y, U8 d) |
183 | make_tunnel (layout &dist, pointlist &seeds, int x, int y, U8 d) |
182 | { |
184 | { |
183 | for (;;) |
185 | for (;;) |
184 | { |
186 | { |
185 | if (x > 1 && U8 (dist [x - 1][y]) < d && dist [x - 1][y] > 1) |
187 | if (x > 1 && U8 (dist [x - 1][y]) < d && dist [x - 1][y] > 1) |
186 | --x; |
188 | --x; |
… | |
… | |
198 | seeds.push (point (x, y)); |
200 | seeds.push (point (x, y)); |
199 | } |
201 | } |
200 | } |
202 | } |
201 | |
203 | |
202 | static void inline |
204 | static void inline |
203 | maybe_push (Layout &dist, pointlist &seeds, int x, int y, U8 d) |
205 | maybe_push (layout &dist, pointlist &seeds, int x, int y, U8 d) |
204 | { |
206 | { |
205 | char &D = dist [x][y]; |
207 | char &D = dist [x][y]; |
206 | |
208 | |
207 | if (U8 (D) > d) // if wall and higher distance, lower distance |
209 | if (U8 (D) > d) // if wall and higher distance, lower distance |
208 | D = d; |
210 | D = d; |
… | |
… | |
210 | return; |
212 | return; |
211 | |
213 | |
212 | seeds.push (point (x, y)); |
214 | seeds.push (point (x, y)); |
213 | } |
215 | } |
214 | |
216 | |
215 | static void |
217 | void |
216 | isolation_remover (Layout &maze, bool dirty) |
218 | layout::isolation_remover (bool dirty) |
217 | { |
219 | { |
218 | Layout dist (maze.w, maze.h); |
220 | layout dist (w, h); |
219 | |
221 | |
220 | // dist contains |
222 | // dist contains |
221 | // 0 == invisited rooms |
223 | // 0 == invisited rooms |
222 | // 1 == visited rooms |
224 | // 1 == visited rooms |
223 | // 2+ shortest distance to random near room |
225 | // 2+ shortest distance to random near room |
224 | |
226 | |
225 | // phase 1, initialise dist array, find seed |
227 | // phase 1, initialise dist array, find seed |
226 | int cnt = 0; |
228 | int cnt = 0; |
227 | int x, y; |
229 | int x, y; |
228 | |
230 | |
229 | for (int i = 0; i < maze.w; ++i) |
231 | for (int i = 0; i < w; ++i) |
230 | for (int j = 0; j < maze.h; ++j) |
232 | for (int j = 0; j < h; ++j) |
231 | { |
233 | { |
232 | if (maze [i][j] == '#') |
234 | if (col [i][j] == '#') |
233 | dist [i][j] = U8 (255); |
235 | dist [i][j] = U8 (255); |
234 | else |
236 | else |
235 | { |
237 | { |
236 | dist [i][j] = 0; |
238 | dist [i][j] = 0; |
237 | if (!rmg_rndm (++cnt)) |
239 | if (!rmg_rndm (++cnt)) |
238 | x = i, y = j; |
240 | x = i, y = j; |
239 | } |
241 | } |
240 | } |
242 | } |
241 | |
243 | |
242 | if (!cnt) |
244 | if (!cnt) |
|
|
245 | { |
|
|
246 | // map is completely massive, this is not good, |
|
|
247 | // so make it empty instead. |
|
|
248 | clear (); |
|
|
249 | border (); |
243 | return; |
250 | return; |
|
|
251 | } |
244 | |
252 | |
245 | fixed_stack<point> seeds (maze.w * maze.h * 5); |
253 | fixed_stack<point> seeds (w * h * 5); |
246 | |
254 | |
247 | // found first free space - picking the first one gives |
255 | // found first free space - picking the first one gives |
248 | // us a slight bias for tunnels, but usually you won't |
256 | // us a slight bias for tunnels, but usually you won't |
249 | // notice that in-game |
257 | // notice that in-game |
250 | seeds.push (point (x, y)); |
258 | seeds.push (point (x, y)); |
… | |
… | |
283 | if (y > 0) maybe_push (dist, seeds, x, y - 1, d); |
291 | if (y > 0) maybe_push (dist, seeds, x, y - 1, d); |
284 | } |
292 | } |
285 | } |
293 | } |
286 | |
294 | |
287 | // now copy the tunnels over |
295 | // now copy the tunnels over |
288 | for (int x = 0; x < maze.w; ++x) |
296 | for (int x = 0; x < w; ++x) |
289 | for (int y = 0; y < maze.h; ++y) |
297 | for (int y = 0; y < h; ++y) |
290 | if (maze [x][y] == '#' && dist [x][y] == 1) |
298 | if (col [x][y] == '#' && dist [x][y] == 1) |
291 | maze [x][y] = 0; |
299 | col [x][y] = 0; |
292 | } |
|
|
293 | |
|
|
294 | void |
|
|
295 | Layout::isolation_remover (bool dirty) |
|
|
296 | { |
|
|
297 | ::isolation_remover (*this, dirty); |
|
|
298 | } |
300 | } |
299 | |
301 | |
300 | ///////////////////////////////////////////////////////////////////////////// |
302 | ///////////////////////////////////////////////////////////////////////////// |
301 | |
303 | |
302 | // inspired mostly by http://www.jimrandomh.org/misc/caves.txt |
304 | // inspired mostly by http://www.jimrandomh.org/misc/caves.txt |
303 | void |
305 | void |
304 | Layout::gen_cave (int subtype) |
306 | layout::gen_cave (int subtype) |
305 | { |
307 | { |
306 | switch (subtype) |
308 | switch (subtype) |
307 | { |
309 | { |
308 | // a rough cave |
310 | // a rough cave |
309 | case 0: |
311 | case 0: |
… | |
… | |
335 | |
337 | |
336 | border (); |
338 | border (); |
337 | isolation_remover (); |
339 | isolation_remover (); |
338 | } |
340 | } |
339 | |
341 | |
|
|
342 | ///////////////////////////////////////////////////////////////////////////// |
|
|
343 | |
|
|
344 | //+GPL |
|
|
345 | |
|
|
346 | /* puts doors at appropriate locations in a maze. */ |
|
|
347 | void |
|
|
348 | layout::doorify () |
|
|
349 | { |
|
|
350 | int ndoors = w * h / 60; /* reasonable number of doors. */ |
|
|
351 | int doorlocs = 0; /* # of available doorlocations */ |
|
|
352 | |
|
|
353 | uint16 *doorlist_x = salloc<uint16> (w * h); |
|
|
354 | uint16 *doorlist_y = salloc<uint16> (w * h); |
|
|
355 | |
|
|
356 | /* make a list of possible door locations */ |
|
|
357 | for (int i = 1; i < w - 1; i++) |
|
|
358 | for (int j = 1; j < h - 1; j++) |
|
|
359 | { |
|
|
360 | int sindex = surround_flag (*this, i, j); |
|
|
361 | |
|
|
362 | if (sindex == 3 || sindex == 12) /* these are possible door sindex */ |
|
|
363 | { |
|
|
364 | doorlist_x [doorlocs] = i; |
|
|
365 | doorlist_y [doorlocs] = j; |
|
|
366 | doorlocs++; |
|
|
367 | } |
|
|
368 | } |
|
|
369 | |
|
|
370 | while (ndoors > 0 && doorlocs > 0) |
|
|
371 | { |
|
|
372 | int di = rmg_rndm (doorlocs); |
|
|
373 | int i = doorlist_x [di]; |
|
|
374 | int j = doorlist_y [di]; |
|
|
375 | int sindex = surround_flag (*this, i, j); |
|
|
376 | |
|
|
377 | if (sindex == 3 || sindex == 12) /* these are possible door sindex */ |
|
|
378 | { |
|
|
379 | col [i][j] = 'D'; |
|
|
380 | ndoors--; |
|
|
381 | } |
|
|
382 | |
|
|
383 | /* reduce the size of the list */ |
|
|
384 | doorlocs--; |
|
|
385 | doorlist_x[di] = doorlist_x [doorlocs]; |
|
|
386 | doorlist_y[di] = doorlist_y [doorlocs]; |
|
|
387 | } |
|
|
388 | |
|
|
389 | sfree (doorlist_x, w * h); |
|
|
390 | sfree (doorlist_y, w * h); |
|
|
391 | } |
|
|
392 | |
|
|
393 | /* takes a map and makes it symmetric: adjusts Xsize and |
|
|
394 | * Ysize to produce a symmetric map. |
|
|
395 | */ |
|
|
396 | void |
|
|
397 | layout::symmetrize (int symmetry) |
|
|
398 | { |
|
|
399 | if (symmetry == SYMMETRY_NONE) |
|
|
400 | return; |
|
|
401 | |
|
|
402 | layout sym_layout ( |
|
|
403 | symmetry == SYMMETRY_X || symmetry == SYMMETRY_XY ? w * 2 - 3 : w, |
|
|
404 | symmetry == SYMMETRY_Y || symmetry == SYMMETRY_XY ? h * 2 - 3 : h |
|
|
405 | ); |
|
|
406 | |
|
|
407 | if (symmetry == SYMMETRY_X) |
|
|
408 | for (int i = 0; i < sym_layout.w / 2 + 1; i++) |
|
|
409 | for (int j = 0; j < sym_layout.h; j++) |
|
|
410 | { |
|
|
411 | sym_layout[i ][j] = |
|
|
412 | sym_layout[sym_layout.w - i - 1][j] = col[i][j]; |
|
|
413 | } |
|
|
414 | |
|
|
415 | if (symmetry == SYMMETRY_Y) |
|
|
416 | for (int i = 0; i < sym_layout.w; i++) |
|
|
417 | for (int j = 0; j < sym_layout.h / 2 + 1; j++) |
|
|
418 | { |
|
|
419 | sym_layout[i][j ] = |
|
|
420 | sym_layout[i][sym_layout.h - j - 1] = col[i][j]; |
|
|
421 | } |
|
|
422 | |
|
|
423 | if (symmetry == SYMMETRY_XY) |
|
|
424 | for (int i = 0; i < sym_layout.w / 2 + 1; i++) |
|
|
425 | for (int j = 0; j < sym_layout.h / 2 + 1; j++) |
|
|
426 | { |
|
|
427 | sym_layout[i ][j ] = |
|
|
428 | sym_layout[i ][sym_layout.h - j - 1] = |
|
|
429 | sym_layout[sym_layout.w - i - 1][j ] = |
|
|
430 | sym_layout[sym_layout.w - i - 1][sym_layout.h - j - 1] = col[i][j]; |
|
|
431 | } |
|
|
432 | |
|
|
433 | /* need to run the isolation remover for some layouts */ |
|
|
434 | #if 0 |
|
|
435 | switch (RP->map_layout_style) |
|
|
436 | { |
|
|
437 | case LAYOUT_ONION: |
|
|
438 | case LAYOUT_SNAKE: |
|
|
439 | case LAYOUT_SQUARE_SPIRAL: |
|
|
440 | // safe |
|
|
441 | break; |
|
|
442 | |
|
|
443 | default: |
|
|
444 | sym_layout.isolation_remover (); |
|
|
445 | break; |
|
|
446 | } |
|
|
447 | #endif |
|
|
448 | sym_layout.isolation_remover (); |
|
|
449 | |
|
|
450 | swap (sym_layout); |
|
|
451 | } |
|
|
452 | |
|
|
453 | //-GPL |
|
|
454 | |
|
|
455 | void |
|
|
456 | layout::rotate (int rotation) |
|
|
457 | { |
|
|
458 | switch (rotation & 3) |
|
|
459 | { |
|
|
460 | case 2: /* a reflection */ |
|
|
461 | { |
|
|
462 | layout new_layout (w, h); |
|
|
463 | |
|
|
464 | for (int i = 0; i < w; i++) /* copy a reflection back */ |
|
|
465 | for (int j = 0; j < h; j++) |
|
|
466 | new_layout [i][j] = col [w - i - 1][h - j - 1]; |
|
|
467 | |
|
|
468 | swap (new_layout); |
|
|
469 | } |
|
|
470 | break; |
|
|
471 | |
|
|
472 | case 1: |
|
|
473 | case 3: |
|
|
474 | { |
|
|
475 | layout new_layout (h, w); |
|
|
476 | |
|
|
477 | if (rotation == 1) /* swap x and y */ |
|
|
478 | for (int i = 0; i < w; i++) |
|
|
479 | for (int j = 0; j < h; j++) |
|
|
480 | new_layout [j][i] = col [i][j]; |
|
|
481 | |
|
|
482 | if (rotation == 3) /* swap x and y */ |
|
|
483 | for (int i = 0; i < w; i++) |
|
|
484 | for (int j = 0; j < h; j++) |
|
|
485 | new_layout [j][i] = col [w - i - 1][h - j - 1]; |
|
|
486 | |
|
|
487 | swap (new_layout); |
|
|
488 | } |
|
|
489 | break; |
|
|
490 | } |
|
|
491 | } |
|
|
492 | |
|
|
493 | ///////////////////////////////////////////////////////////////////////////// |
|
|
494 | |
|
|
495 | //+GPL |
|
|
496 | |
|
|
497 | /* |
|
|
498 | * Expands a maze by 2x in each dimension. |
|
|
499 | * H. S. Teoh |
|
|
500 | */ |
|
|
501 | |
|
|
502 | /* 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: |
|
|
504 | * X ---> X \0 |
|
|
505 | * \0 \0 |
|
|
506 | */ |
|
|
507 | static void inline |
|
|
508 | expand_misc (layout &newlayout, int i, int j, layout &maze) |
|
|
509 | { |
|
|
510 | 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 |
|
|
512 | * for us.) */ |
|
|
513 | } |
|
|
514 | |
|
|
515 | /* Returns a bitmap that represents which squares on the right and bottom |
|
|
516 | * edges of a square (i,j) match the given character: |
|
|
517 | * 1 match on (i+1, j) |
|
|
518 | * 2 match on (i, j+1) |
|
|
519 | * 4 match on (i+1, j+1) |
|
|
520 | * and the possible combinations thereof. |
|
|
521 | */ |
|
|
522 | static int noinline |
|
|
523 | calc_pattern (char ch, layout &maze, int i, int j) |
|
|
524 | { |
|
|
525 | int pattern = 0; |
|
|
526 | |
|
|
527 | if (i + 1 < maze.w && maze[i + 1][j] == ch) |
|
|
528 | pattern |= 1; |
|
|
529 | |
|
|
530 | if (j + 1 < maze.h) |
|
|
531 | { |
|
|
532 | if (maze[i][j + 1] == ch) |
|
|
533 | pattern |= 2; |
|
|
534 | |
|
|
535 | if (i + 1 < maze.w && maze[i + 1][j + 1] == ch) |
|
|
536 | pattern |= 4; |
|
|
537 | } |
|
|
538 | |
|
|
539 | return pattern; |
|
|
540 | } |
|
|
541 | |
|
|
542 | /* 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 |
|
|
544 | * walls. |
|
|
545 | */ |
|
|
546 | static void inline |
|
|
547 | expand_wall (layout &newlayout, int i, int j, layout &maze) |
|
|
548 | { |
|
|
549 | int wall_pattern = calc_pattern ('#', maze, i, j); |
|
|
550 | int door_pattern = calc_pattern ('D', maze, i, j); |
|
|
551 | int both_pattern = wall_pattern | door_pattern; |
|
|
552 | |
|
|
553 | newlayout[i * 2][j * 2] = '#'; |
|
|
554 | |
|
|
555 | if (i + 1 < maze.w) |
|
|
556 | { |
|
|
557 | if (both_pattern & 1) |
|
|
558 | { /* join walls/doors to the right */ |
|
|
559 | /* newlayout[i*2+1][j*2] = '#'; */ |
|
|
560 | newlayout[i * 2 + 1][j * 2] = maze[i + 1][j]; |
|
|
561 | } |
|
|
562 | } |
|
|
563 | |
|
|
564 | if (j + 1 < maze.h) |
|
|
565 | { |
|
|
566 | if (both_pattern & 2) |
|
|
567 | { /* join walls/doors to the bottom */ |
|
|
568 | /* newlayout[i*2][j*2+1] = '#'; */ |
|
|
569 | newlayout[i * 2][j * 2 + 1] = maze[i][j + 1]; |
|
|
570 | } |
|
|
571 | |
|
|
572 | if (wall_pattern == 7) |
|
|
573 | { /* if orig maze is a 2x2 wall block, |
|
|
574 | * we fill the result with walls. */ |
|
|
575 | newlayout[i * 2 + 1][j * 2 + 1] = '#'; |
|
|
576 | } |
|
|
577 | } |
|
|
578 | } |
|
|
579 | |
|
|
580 | /* 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 |
|
|
582 | * that it doesn't know how to correctly expand. |
|
|
583 | */ |
|
|
584 | static void inline |
|
|
585 | expand_door (layout &newlayout, int i, int j, layout &maze) |
|
|
586 | { |
|
|
587 | int wall_pattern = calc_pattern ('#', maze, i, j); |
|
|
588 | int door_pattern = calc_pattern ('D', maze, i, j); |
|
|
589 | int join_pattern; |
|
|
590 | |
|
|
591 | /* 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 |
|
|
593 | * disconnect from the other door. */ |
|
|
594 | if (wall_pattern & 3) |
|
|
595 | join_pattern = wall_pattern; |
|
|
596 | else |
|
|
597 | join_pattern = door_pattern; |
|
|
598 | |
|
|
599 | newlayout[i * 2][j * 2] = 'D'; |
|
|
600 | |
|
|
601 | if (i + 1 < maze.w) |
|
|
602 | if (join_pattern & 1) |
|
|
603 | /* there is a door/wall to the right */ |
|
|
604 | newlayout[i * 2 + 1][j * 2] = 'D'; |
|
|
605 | |
|
|
606 | if (j + 1 < maze.h) |
|
|
607 | if (join_pattern & 2) |
|
|
608 | /* there is a door/wall below */ |
|
|
609 | newlayout[i * 2][j * 2 + 1] = 'D'; |
|
|
610 | } |
|
|
611 | |
|
|
612 | void |
|
|
613 | layout::expand2x () |
|
|
614 | { |
|
|
615 | layout new_layout (w * 2 - 1, h * 2 - 1); |
|
|
616 | |
|
|
617 | new_layout.clear (); |
|
|
618 | |
|
|
619 | for (int i = 0; i < w; i++) |
|
|
620 | for (int j = 0; j < h; j++) |
|
|
621 | switch (col [i][j]) |
|
|
622 | { |
|
|
623 | case '#': expand_wall (new_layout, i, j, *this); break; |
|
|
624 | case 'D': expand_door (new_layout, i, j, *this); break; |
|
|
625 | default: expand_misc (new_layout, i, j, *this); break; |
|
|
626 | } |
|
|
627 | |
|
|
628 | swap (new_layout); |
|
|
629 | } |
|
|
630 | |
|
|
631 | ///////////////////////////////////////////////////////////////////////////// |
|
|
632 | |
|
|
633 | /* checks the maze to see if I can stick a horizontal(dir = 0) wall |
|
|
634 | (or vertical, dir == 1) |
|
|
635 | here which ends up on other walls sensibly. */ |
|
|
636 | static int |
|
|
637 | can_make_wall (const layout &maze, int dx, int dy, int dir) |
|
|
638 | { |
|
|
639 | int i1; |
|
|
640 | int length = 0; |
|
|
641 | |
|
|
642 | /* dont make walls if we're on the edge. */ |
|
|
643 | if (dx == 0 || dx == (maze.w - 1) || dy == 0 || dy == (maze.h - 1)) |
|
|
644 | return -1; |
|
|
645 | |
|
|
646 | /* don't make walls if we're ON a wall. */ |
|
|
647 | if (maze [dx][dy] != 0) |
|
|
648 | return -1; |
|
|
649 | |
|
|
650 | if (dir == 0) /* horizontal */ |
|
|
651 | { |
|
|
652 | int y = dy; |
|
|
653 | |
|
|
654 | for (i1 = dx - 1; i1 > 0; i1--) |
|
|
655 | { |
|
|
656 | int sindex = surround_flag2 (maze, i1, y); |
|
|
657 | |
|
|
658 | if (sindex == 1) break; |
|
|
659 | if (sindex != 0) return -1; /* can't make horiz. wall here */ |
|
|
660 | if (maze[i1][y] != 0) return -1; /* can't make horiz. wall here */ |
|
|
661 | |
|
|
662 | length++; |
|
|
663 | } |
|
|
664 | |
|
|
665 | for (i1 = dx + 1; i1 < maze.w - 1; i1++) |
|
|
666 | { |
|
|
667 | int sindex = surround_flag2 (maze, i1, y); |
|
|
668 | |
|
|
669 | if (sindex == 2) break; |
|
|
670 | if (sindex != 0) return -1; /* can't make horiz. wall here */ |
|
|
671 | if (maze[i1][y] != 0) return -1; /* can't make horiz. wall here */ |
|
|
672 | |
|
|
673 | length++; |
|
|
674 | } |
|
|
675 | return length; |
|
|
676 | } |
|
|
677 | else |
|
|
678 | { /* vertical */ |
|
|
679 | int x = dx; |
|
|
680 | |
|
|
681 | for (i1 = dy - 1; i1 > 0; i1--) |
|
|
682 | { |
|
|
683 | int sindex = surround_flag2 (maze, x, i1); |
|
|
684 | |
|
|
685 | if (sindex == 4) break; |
|
|
686 | if (sindex != 0) return -1; /* can't make vert. wall here */ |
|
|
687 | if (maze[x][i1] != 0) return -1; /* can't make horiz. wall here */ |
|
|
688 | |
|
|
689 | length++; |
|
|
690 | } |
|
|
691 | |
|
|
692 | for (i1 = dy + 1; i1 < maze.h - 1; i1++) |
|
|
693 | { |
|
|
694 | int sindex = surround_flag2 (maze, x, i1); |
|
|
695 | |
|
|
696 | if (sindex == 8) break; |
|
|
697 | if (sindex != 0) return -1; /* can't make verti. wall here */ |
|
|
698 | if (maze[x][i1] != 0) return -1; /* can't make horiz. wall here */ |
|
|
699 | |
|
|
700 | length++; |
|
|
701 | } |
|
|
702 | |
|
|
703 | return length; |
|
|
704 | } |
|
|
705 | |
|
|
706 | return -1; |
|
|
707 | } |
|
|
708 | |
|
|
709 | int |
|
|
710 | make_wall (char **maze, int x, int y, int dir) |
|
|
711 | { |
|
|
712 | maze[x][y] = 'D'; /* mark a door */ |
|
|
713 | |
|
|
714 | switch (dir) |
|
|
715 | { |
|
|
716 | case 0: /* horizontal */ |
|
|
717 | { |
|
|
718 | for (int i1 = x - 1; maze[i1][y] == 0; --i1) maze[i1][y] = '#'; |
|
|
719 | for (int i1 = x + 1; maze[i1][y] == 0; ++i1) maze[i1][y] = '#'; |
|
|
720 | break; |
|
|
721 | } |
|
|
722 | case 1: /* vertical */ |
|
|
723 | { |
|
|
724 | for (int i1 = y - 1; maze[x][i1] == 0; --i1) maze[x][i1] = '#'; |
|
|
725 | for (int i1 = y + 1; maze[x][i1] == 0; ++i1) maze[x][i1] = '#'; |
|
|
726 | break; |
|
|
727 | } |
|
|
728 | } |
|
|
729 | |
|
|
730 | return 0; |
|
|
731 | } |
|
|
732 | |
|
|
733 | void |
|
|
734 | layout::roomify () |
|
|
735 | { |
|
|
736 | int tries = w * h / 30; |
|
|
737 | |
|
|
738 | for (int ti = 0; ti < tries; ti++) |
|
|
739 | { |
|
|
740 | /* starting location for looking at creating a door */ |
|
|
741 | int dx = rmg_rndm (w); |
|
|
742 | int dy = rmg_rndm (h); |
|
|
743 | |
|
|
744 | /* results of checking on creating walls. */ |
|
|
745 | int cx = can_make_wall (*this, dx, dy, 0); /* horizontal */ |
|
|
746 | int cy = can_make_wall (*this, dx, dy, 1); /* vertical */ |
|
|
747 | |
|
|
748 | if (cx == -1) |
|
|
749 | { |
|
|
750 | if (cy != -1) |
|
|
751 | make_wall (*this, dx, dy, 1); |
|
|
752 | |
|
|
753 | continue; |
|
|
754 | } |
|
|
755 | |
|
|
756 | if (cy == -1) |
|
|
757 | { |
|
|
758 | make_wall (*this, dx, dy, 0); |
|
|
759 | continue; |
|
|
760 | } |
|
|
761 | |
|
|
762 | if (cx < cy) |
|
|
763 | make_wall (*this, dx, dy, 0); |
|
|
764 | else |
|
|
765 | make_wall (*this, dx, dy, 1); |
|
|
766 | } |
|
|
767 | } |
|
|
768 | |
|
|
769 | ///////////////////////////////////////////////////////////////////////////// |
|
|
770 | |
|
|
771 | /* function selects the maze function and gives it whatever |
|
|
772 | arguments it needs. */ |
|
|
773 | void |
|
|
774 | layout::generate (random_map_params *RP) |
|
|
775 | { |
|
|
776 | switch (RP->map_layout_style) |
|
|
777 | { |
|
|
778 | case LAYOUT_ONION: |
|
|
779 | map_gen_onion (*this, RP->layoutoptions1, RP->layoutoptions2); |
|
|
780 | |
|
|
781 | if (!(rmg_rndm (3)) && !(RP->layoutoptions1 & (RMOPT_WALLS_ONLY | RMOPT_WALL_OFF))) |
|
|
782 | roomify (); |
|
|
783 | |
|
|
784 | break; |
|
|
785 | |
|
|
786 | case LAYOUT_MAZE: |
|
|
787 | maze_gen (*this, RP->get_iv ("maze_type", rmg_rndm (4))); |
|
|
788 | |
|
|
789 | if (rmg_rndm (2)) |
|
|
790 | doorify (); |
|
|
791 | |
|
|
792 | break; |
|
|
793 | |
|
|
794 | case LAYOUT_SPIRAL: |
|
|
795 | map_gen_spiral (*this, RP->layoutoptions1); |
|
|
796 | |
|
|
797 | if (rmg_rndm (2)) |
|
|
798 | doorify (); |
|
|
799 | |
|
|
800 | break; |
|
|
801 | |
|
|
802 | case LAYOUT_ROGUELIKE: |
|
|
803 | /* Don't put symmetry in rogue maps. There isn't much reason to |
|
|
804 | * do so in the first place (doesn't make it any more interesting), |
|
|
805 | * but more importantly, the symmetry code presumes we are symmetrizing |
|
|
806 | * spirals, or maps with lots of passages - making a symmetric rogue |
|
|
807 | * map fails because its likely that the passages the symmetry process |
|
|
808 | * creates may not connect the rooms. |
|
|
809 | */ |
|
|
810 | RP->symmetry_used = SYMMETRY_NONE; |
|
|
811 | roguelike_layout_gen (*this, RP->layoutoptions1); |
|
|
812 | /* no doorifying... done already */ |
|
|
813 | break; |
|
|
814 | |
|
|
815 | case LAYOUT_SNAKE: |
|
|
816 | make_snake_layout (*this, RP->layoutoptions1); |
|
|
817 | |
|
|
818 | if (rmg_rndm (2)) |
|
|
819 | roomify (); |
|
|
820 | |
|
|
821 | break; |
|
|
822 | |
|
|
823 | case LAYOUT_SQUARE_SPIRAL: |
|
|
824 | make_square_spiral_layout (*this, RP->layoutoptions1); |
|
|
825 | |
|
|
826 | if (rmg_rndm (2)) |
|
|
827 | roomify (); |
|
|
828 | |
|
|
829 | break; |
|
|
830 | |
|
|
831 | case LAYOUT_CAVE: |
|
|
832 | gen_cave (RP->get_iv ("cave_type", rmg_rndm (4))); |
|
|
833 | |
|
|
834 | if (rmg_rndm (2)) |
|
|
835 | doorify (); |
|
|
836 | |
|
|
837 | break; |
|
|
838 | |
|
|
839 | default: |
|
|
840 | abort (); |
|
|
841 | } |
|
|
842 | |
|
|
843 | /* rotate the maze 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 | } |
|
|
855 | |
|
|
856 | //-GPL |
|
|
857 | |
340 | #if 0 |
858 | #if 0 |
341 | static struct demo |
859 | static struct demo |
342 | { |
860 | { |
343 | demo () |
861 | demo () |
344 | { |
862 | { |
345 | Layout maze (40, 25); |
|
|
346 | rmg_rndm.seed (time (0)); |
863 | rmg_rndm.seed (time (0)); |
347 | |
864 | |
348 | for(int i=1;i<10;i) |
865 | for(int i=1;i<10;i) |
349 | { |
866 | { |
|
|
867 | layout maze (10, 10); |
350 | maze.fill_rand (97); |
868 | maze.fill_rand (90); |
351 | maze.border (); |
869 | maze.border (); |
352 | maze.isolation_remover (); |
870 | maze.isolation_remover (); |
|
|
871 | maze.doorify (); |
|
|
872 | maze.symmetrize (rmg_rndm (2, 4)); |
|
|
873 | maze.rotate (rmg_rndm (4)); |
|
|
874 | maze.expand2x (); |
353 | maze.print (); |
875 | maze.print (); |
354 | } |
876 | } |
355 | |
877 | |
356 | exit (1); |
878 | exit (1); |
357 | } |
879 | } |