3 import std
.random
: uniform
;
9 // 1/a chance of any tile being walkable
10 private Map
map_random(int[] flags
) {
16 if (flags
.length
== 1) {
23 if (flags
.length
== 2) {
28 map
.for_all((ref Tile x
) { x
.walkable
= chances(a
, b
); x
.blocks_light
= !x
.walkable
; });
33 private void mapcaves_postprocess(Map map
) {
34 foreach (y
; 0 .. map_y
-1) {
35 foreach (x
; 0 .. map_x
-1) {
36 if ((map
[y
+1, x
].walkable
&& map
[y
-1, x
].walkable
) ||
(map
[y
, x
+1].walkable
&& map
[y
, x
-1].walkable
)) {
37 map
[y
, x
].walkable
= true;
38 map
[y
, x
].blocks_light
= false;
44 private Map
real_map_caves(int[] flags
, int iters
= 100) {
47 enum Direction
: bool {y
= true, x
= false}
48 enum Sign
: bool {plus
= true, minus
= false}
52 map
.for_all((ref Tile x
) { x
.walkable
= false; x
.blocks_light
= true; });
55 // "burrow" through the cave
57 tmpy
= uniform(0, map_y
);
58 tmpx
= uniform(0, map_x
);
59 foreach (_
; 0..uniform(8000, 9000)) {
60 foreach (__
; 0..uniform(3, 20)) {
61 foreach (i
; tmpx
-uniform(-1, 3)..tmpx
+uniform(-1, 3)) {
62 map
[tmpy
, i
].walkable
= true;
63 map
[tmpy
, i
].blocks_light
= false;
65 foreach (i
; tmpy
-uniform(-1, 3)..tmpy
+uniform(-1, 3)) {
66 map
[i
, tmpx
].walkable
= true;
67 map
[i
, tmpx
].blocks_light
= false;
72 if (d
== Direction
.x
) {
78 } else if (d
== Direction
.y
) {
95 /* if (chances(iters, 1001)) {
96 return real_map_caves(map, flags, iters--);
102 private Map
map_caves(int[] flags
) {
103 Map map
= real_map_caves(flags
);
105 mapcaves_postprocess(map
);
106 mapcaves_postprocess(map
);
107 //mapcaves_postprocess(map);
114 /* Prototypes for different types of dungeons */
120 private enum int[][MapType
] argnums
= [MapType
.random
: [0, 1, 2], MapType
.caves
: [0]];
122 /* Map from a maptype to an array of ints. Those are all the different numbers
123 * of args it can take. So, for example, it might have an option of taking 4
127 private void veriflags(MapType type
, ulong flags
) {
129 foreach (i
; argnums
[type
]) {
132 assert (flags
in tmp
);
135 Map
genmap(MapType type
, int[] flags
= []) {
136 veriflags(type
, flags
.length
);
139 case MapType
.random
: return map_random(flags
);
140 case MapType
.caves
: return map_caves(flags
);