Cleanups
[SmugglerRL.git] / src / mapgen.d
blob507f239af4fcdfa0660c38c5f86d571dff7f5b06
1 import constants;
2 import game;
3 import std.random: uniform;
4 import util;
5 import logging;
6 import map;
7 import rng;
9 // 1/a chance of any tile being walkable
10 private Map map_random(int[] flags) {
11 Map map = new Map();
13 uint a, b;
14 a = 1;
15 b = 2;
16 if (flags.length == 1) {
17 b = flags[0];
18 if (flags[0] < 0) {
19 b = -b;
20 a = b-1;
23 if (flags.length == 2) {
24 a = flags[0];
25 b = flags[1];
28 map.for_all((ref Tile x) { x.walkable = chances(a, b); x.blocks_light = !x.walkable; });
30 return map;
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) {
45 Map map = new Map();
47 enum Direction: bool {y = true, x = false}
48 enum Sign: bool {plus = true, minus = false}
49 bool d = Direction.x;
50 bool s = Sign.plus;
52 map.for_all((ref Tile x) { x.walkable = false; x.blocks_light = true; });
54 int tmpx, tmpy;
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) {
73 if (s == Sign.plus) {
74 tmpx++;
75 } else {
76 tmpx--;
78 } else if (d == Direction.y) {
79 if (s == Sign.plus) {
80 tmpy++;
81 } else {
82 tmpy--;
87 if (uniform(0, 3)) {
88 d = !d;
90 if (uniform(0, 3)) {
91 s = !s;
95 /* if (chances(iters, 1001)) {
96 return real_map_caves(map, flags, iters--);
97 } else {*/
98 return map;
99 /*}*/
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);
109 return map;
114 /* Prototypes for different types of dungeons */
115 enum MapType {
116 random,
117 caves
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
124 * OR 5 args
127 private void veriflags(MapType type, ulong flags) {
128 bool[ulong] tmp;
129 foreach (i; argnums[type]) {
130 tmp[i] = false;
132 assert (flags in tmp);
135 Map genmap(MapType type, int[] flags = []) {
136 veriflags(type, flags.length);
138 switch(type) {
139 case MapType.random: return map_random(flags);
140 case MapType.caves: return map_caves(flags);
141 default: assert(0);