… | |
… | |
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 | int size = (sizeof (cell *) + sizeof (cell) * h) * w; |
|
|
40 | |
|
|
41 | data = (cell **)salloc<char> (size); |
|
|
42 | |
|
|
43 | cell *p = (cell *)(data + w); |
|
|
44 | |
|
|
45 | for (int x = w; x--; ) |
|
|
46 | data [x] = p + x * h; |
|
|
47 | } |
|
|
48 | |
28 | layout::layout (int w, int h) |
49 | layout::layout (int w, int h) |
29 | : w(w), h(h) |
|
|
30 | { |
50 | { |
31 | int size = (sizeof (char *) + sizeof (char) * h) * w; |
51 | alloc (w, h); |
|
|
52 | } |
32 | |
53 | |
33 | col = (char **)salloc<char> (size); |
54 | layout::layout (layout ©) |
|
|
55 | { |
|
|
56 | alloc (copy.w, copy.h); |
34 | |
57 | |
35 | char *data = (char *)(col + w); |
58 | memcpy (data [0], copy.data [0], sizeof (cell) * h * w); |
36 | |
|
|
37 | for (int x = w; x--; ) |
|
|
38 | col [x] = data + x * h; |
|
|
39 | } |
59 | } |
40 | |
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) |
56 | { |
76 | { |
57 | for (; x1 < x2; ++x1) |
77 | for (; x1 < x2; ++x1) |
58 | memset (col [x1] + y1, fill, y2 - y1); |
78 | memset (data [x1] + y1, fill, y2 - y1); |
59 | } |
79 | } |
60 | |
80 | |
61 | void layout::border (char fill) |
81 | void layout::border (char fill) |
62 | { |
82 | { |
63 | for (int i = 0; i < w; i++) col [i][0] = col [i][h - 1] = fill; |
83 | for (int i = 0; i < w; i++) data [i][0] = data [i][h - 1] = fill; |
64 | for (int j = 0; j < h; j++) col [0][j] = col [w - 1][j] = fill; |
84 | for (int j = 0; j < h; j++) data [0][j] = data [w - 1][j] = fill; |
65 | } |
85 | } |
66 | |
86 | |
67 | void |
87 | void |
68 | layout::fill_rand (int percent) |
88 | layout::fill_rand (int percent) |
69 | { |
89 | { |
70 | percent = lerp (percent, 0, 100, 0, 256); |
90 | percent = lerp (percent, 0, 100, 0, 256); |
71 | |
91 | |
72 | for (int x = w - 1; --x > 0; ) |
92 | for (int x = w - 1; --x > 0; ) |
73 | for (int y = h - 1; --y > 0; ) |
93 | for (int y = h - 1; --y > 0; ) |
74 | col [x][y] = rmg_rndm (256) > percent ? 0 : '#'; |
94 | data [x][y] = rmg_rndm (256) > percent ? 0 : '#'; |
75 | } |
95 | } |
76 | |
96 | |
77 | ///////////////////////////////////////////////////////////////////////////// |
97 | ///////////////////////////////////////////////////////////////////////////// |
78 | |
98 | |
79 | // erode by cellular automata |
99 | // erode by cellular automata |
… | |
… | |
104 | for (int i = array_length (dds); i--; ) |
124 | for (int i = array_length (dds); i--; ) |
105 | { |
125 | { |
106 | int nx = x + dds [i][0]; |
126 | int nx = x + dds [i][0]; |
107 | int ny = y + dds [i][1]; |
127 | int ny = y + dds [i][1]; |
108 | |
128 | |
109 | if (!IN_RANGE_EXC (nx, 0, w) || !IN_RANGE_EXC (ny, 0, h) || !col [nx][ny]) |
129 | if (!IN_RANGE_EXC (nx, 0, w) || !IN_RANGE_EXC (ny, 0, h) || !data [nx][ny]) |
110 | { |
130 | { |
111 | n1 += dds [i][2]; |
131 | n1 += dds [i][2]; |
112 | n2++; |
132 | n2++; |
113 | } |
133 | } |
114 | } |
134 | } |
… | |
… | |
128 | { |
148 | { |
129 | for (int y = 0; y < h; y++) |
149 | for (int y = 0; y < h; y++) |
130 | { |
150 | { |
131 | for (int x = 0; x < w; x++) |
151 | for (int x = 0; x < w; x++) |
132 | { |
152 | { |
133 | U8 c = (U8)col [x][y]; |
153 | U8 c = (U8)data [x][y]; |
134 | |
154 | |
135 | if (!c) |
155 | if (!c) |
136 | c = ' '; |
156 | c = ' '; |
137 | else if (c < 10) |
157 | else if (c < 10) |
138 | c += '0'; |
158 | c += '0'; |
… | |
… | |
229 | int x, y; |
249 | int x, y; |
230 | |
250 | |
231 | for (int i = 0; i < w; ++i) |
251 | for (int i = 0; i < w; ++i) |
232 | for (int j = 0; j < h; ++j) |
252 | for (int j = 0; j < h; ++j) |
233 | { |
253 | { |
234 | if (col [i][j] == '#') |
254 | if (data [i][j] == '#') |
235 | dist [i][j] = U8 (255); |
255 | dist [i][j] = U8 (255); |
236 | else |
256 | else |
237 | { |
257 | { |
238 | dist [i][j] = 0; |
258 | dist [i][j] = 0; |
239 | if (!rmg_rndm (++cnt)) |
259 | if (!rmg_rndm (++cnt)) |
… | |
… | |
293 | } |
313 | } |
294 | |
314 | |
295 | // now copy the tunnels over |
315 | // now copy the tunnels over |
296 | for (int x = 0; x < w; ++x) |
316 | for (int x = 0; x < w; ++x) |
297 | for (int y = 0; y < h; ++y) |
317 | for (int y = 0; y < h; ++y) |
298 | if (col [x][y] == '#' && dist [x][y] == 1) |
318 | if (data [x][y] == '#' && dist [x][y] == 1) |
299 | col [x][y] = 0; |
319 | data [x][y] = 0; |
300 | } |
320 | } |
301 | |
321 | |
302 | ///////////////////////////////////////////////////////////////////////////// |
322 | ///////////////////////////////////////////////////////////////////////////// |
303 | |
323 | |
304 | // inspired mostly by http://www.jimrandomh.org/misc/caves.txt |
324 | // inspired mostly by http://www.jimrandomh.org/misc/caves.txt |
… | |
… | |
374 | int j = doorlist_y [di]; |
394 | int j = doorlist_y [di]; |
375 | int sindex = surround_flag (*this, i, j); |
395 | int sindex = surround_flag (*this, i, j); |
376 | |
396 | |
377 | if (sindex == 3 || sindex == 12) /* these are possible door sindex */ |
397 | if (sindex == 3 || sindex == 12) /* these are possible door sindex */ |
378 | { |
398 | { |
379 | col [i][j] = 'D'; |
399 | data [i][j] = 'D'; |
380 | ndoors--; |
400 | ndoors--; |
381 | } |
401 | } |
382 | |
402 | |
383 | /* reduce the size of the list */ |
403 | /* reduce the size of the list */ |
384 | doorlocs--; |
404 | doorlocs--; |
… | |
… | |
407 | if (symmetry == SYMMETRY_X) |
427 | if (symmetry == SYMMETRY_X) |
408 | for (int i = 0; i < sym_layout.w / 2 + 1; i++) |
428 | for (int i = 0; i < sym_layout.w / 2 + 1; i++) |
409 | for (int j = 0; j < sym_layout.h; j++) |
429 | for (int j = 0; j < sym_layout.h; j++) |
410 | { |
430 | { |
411 | sym_layout[i ][j] = |
431 | sym_layout[i ][j] = |
412 | sym_layout[sym_layout.w - i - 1][j] = col[i][j]; |
432 | sym_layout[sym_layout.w - i - 1][j] = data [i][j]; |
413 | } |
433 | } |
414 | |
434 | |
415 | if (symmetry == SYMMETRY_Y) |
435 | if (symmetry == SYMMETRY_Y) |
416 | for (int i = 0; i < sym_layout.w; i++) |
436 | for (int i = 0; i < sym_layout.w; i++) |
417 | for (int j = 0; j < sym_layout.h / 2 + 1; j++) |
437 | for (int j = 0; j < sym_layout.h / 2 + 1; j++) |
418 | { |
438 | { |
419 | sym_layout[i][j ] = |
439 | sym_layout[i][j ] = |
420 | sym_layout[i][sym_layout.h - j - 1] = col[i][j]; |
440 | sym_layout[i][sym_layout.h - j - 1] = data [i][j]; |
421 | } |
441 | } |
422 | |
442 | |
423 | if (symmetry == SYMMETRY_XY) |
443 | if (symmetry == SYMMETRY_XY) |
424 | for (int i = 0; i < sym_layout.w / 2 + 1; i++) |
444 | for (int i = 0; i < sym_layout.w / 2 + 1; i++) |
425 | for (int j = 0; j < sym_layout.h / 2 + 1; j++) |
445 | for (int j = 0; j < sym_layout.h / 2 + 1; j++) |
426 | { |
446 | { |
427 | sym_layout[i ][j ] = |
447 | sym_layout[i ][j ] = |
428 | sym_layout[i ][sym_layout.h - j - 1] = |
448 | sym_layout[i ][sym_layout.h - j - 1] = |
429 | sym_layout[sym_layout.w - i - 1][j ] = |
449 | sym_layout[sym_layout.w - i - 1][j ] = |
430 | sym_layout[sym_layout.w - i - 1][sym_layout.h - j - 1] = col[i][j]; |
450 | sym_layout[sym_layout.w - i - 1][sym_layout.h - j - 1] = data [i][j]; |
431 | } |
451 | } |
432 | |
452 | |
433 | /* need to run the isolation remover for some layouts */ |
453 | /* need to run the isolation remover for some layouts */ |
434 | #if 0 |
454 | #if 0 |
435 | switch (RP->map_layout_style) |
455 | switch (RP->map_layout_style) |
… | |
… | |
461 | { |
481 | { |
462 | layout new_layout (w, h); |
482 | layout new_layout (w, h); |
463 | |
483 | |
464 | for (int i = 0; i < w; i++) /* copy a reflection back */ |
484 | for (int i = 0; i < w; i++) /* copy a reflection back */ |
465 | for (int j = 0; j < h; j++) |
485 | for (int j = 0; j < h; j++) |
466 | new_layout [i][j] = col [w - i - 1][h - j - 1]; |
486 | new_layout [i][j] = data [w - i - 1][h - j - 1]; |
467 | |
487 | |
468 | swap (new_layout); |
488 | swap (new_layout); |
469 | } |
489 | } |
470 | break; |
490 | break; |
471 | |
491 | |
… | |
… | |
475 | layout new_layout (h, w); |
495 | layout new_layout (h, w); |
476 | |
496 | |
477 | if (rotation == 1) /* swap x and y */ |
497 | if (rotation == 1) /* swap x and y */ |
478 | for (int i = 0; i < w; i++) |
498 | for (int i = 0; i < w; i++) |
479 | for (int j = 0; j < h; j++) |
499 | for (int j = 0; j < h; j++) |
480 | new_layout [j][i] = col [i][j]; |
500 | new_layout [j][i] = data [i][j]; |
481 | |
501 | |
482 | if (rotation == 3) /* swap x and y */ |
502 | if (rotation == 3) /* swap x and y */ |
483 | for (int i = 0; i < w; i++) |
503 | for (int i = 0; i < w; i++) |
484 | for (int j = 0; j < h; j++) |
504 | for (int j = 0; j < h; j++) |
485 | new_layout [j][i] = col [w - i - 1][h - j - 1]; |
505 | new_layout [j][i] = data [w - i - 1][h - j - 1]; |
486 | |
506 | |
487 | swap (new_layout); |
507 | swap (new_layout); |
488 | } |
508 | } |
489 | break; |
509 | break; |
490 | } |
510 | } |
… | |
… | |
616 | |
636 | |
617 | new_layout.clear (); |
637 | new_layout.clear (); |
618 | |
638 | |
619 | for (int i = 0; i < w; i++) |
639 | for (int i = 0; i < w; i++) |
620 | for (int j = 0; j < h; j++) |
640 | for (int j = 0; j < h; j++) |
621 | switch (col [i][j]) |
641 | switch (data [i][j]) |
622 | { |
642 | { |
623 | case '#': expand_wall (new_layout, i, j, *this); break; |
643 | case '#': expand_wall (new_layout, i, j, *this); break; |
624 | case 'D': expand_door (new_layout, i, j, *this); break; |
644 | case 'D': expand_door (new_layout, i, j, *this); break; |
625 | default: expand_misc (new_layout, i, j, *this); break; |
645 | default: expand_misc (new_layout, i, j, *this); break; |
626 | } |
646 | } |