Cavegen: Re-add small caves to CavesRandomWalk
[oweals/minetest.git] / src / cavegen.h
1 /*
2 Minetest
3 Copyright (C) 2010-2013 kwolekr, Ryan Kwolek <kwolekr@minetest.net>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #ifndef CAVEGEN_HEADER
21 #define CAVEGEN_HEADER
22
23 #define VMANIP_FLAG_CAVE VOXELFLAG_CHECKED1
24 #define DEFAULT_LAVA_DEPTH (-256)
25
26 class GenerateNotifier;
27
28 /*
29         CavesRandomWalk is an implementation of a cave-digging algorithm that
30         operates on the principle of a "random walk" to approximate the stochiastic
31         activity of cavern development.
32
33         In summary, this algorithm works by carving a randomly sized tunnel in a
34         random direction a random amount of times, randomly varying in width.
35         All randomness here is uniformly distributed; alternative distributions have
36         not yet been implemented.
37
38         This algorithm is very fast, executing in less than 1ms on average for an
39         80x80x80 chunk of map on a modern processor.
40 */
41 class CavesRandomWalk {
42 public:
43         MMVManip *vm;
44         INodeDefManager *ndef;
45         GenerateNotifier *gennotify;
46         s16 *heightmap;
47
48         // configurable parameters
49         int seed;
50         int water_level;
51         int lava_depth;
52         NoiseParams *np_caveliquids;
53
54         // intermediate state variables
55         u16 ystride;
56
57         s16 min_tunnel_diameter;
58         s16 max_tunnel_diameter;
59         u16 tunnel_routepoints;
60         int dswitchint;
61         int part_max_length_rs;
62
63         bool large_cave;
64         bool large_cave_is_flat;
65         bool flooded;
66
67         s16 max_stone_y;
68         v3s16 node_min;
69         v3s16 node_max;
70
71         v3f orp;  // starting point, relative to caved space
72         v3s16 of; // absolute coordinates of caved space
73         v3s16 ar; // allowed route area
74         s16 rs;   // tunnel radius size
75         v3f main_direction;
76
77         s16 route_y_min;
78         s16 route_y_max;
79
80         PseudoRandom *ps;
81
82         content_t c_water_source;
83         content_t c_lava_source;
84
85         // ndef is a mandatory parameter.
86         // If gennotify is NULL, generation events are not logged.
87         CavesRandomWalk(INodeDefManager *ndef,
88                 GenerateNotifier *gennotify = NULL,
89                 int seed = 0,
90                 int water_level = 1,
91                 content_t water_source = CONTENT_IGNORE,
92                 content_t lava_source = CONTENT_IGNORE);
93
94         // vm and ps are mandatory parameters.
95         // If heightmap is NULL, the surface level at all points is assumed to
96         // be water_level.
97         void makeCave(MMVManip *vm, v3s16 nmin, v3s16 nmax, PseudoRandom *ps,
98                         bool is_large_cave, int max_stone_height, s16 *heightmap);
99
100 private:
101         void makeTunnel(bool dirswitch);
102         void carveRoute(v3f vec, float f, bool randomize_xz);
103
104         inline bool isPosAboveSurface(v3s16 p);
105 };
106
107 /*
108         CavesV6 is the original version of caves used with Mapgen V6.
109
110         Though it uses the same fundamental algorithm as CavesRandomWalk, it is made
111         separate to preserve the exact sequence of PseudoRandom calls - any change
112         to this ordering results in the output being radically different.
113         Because caves in Mapgen V6 are responsible for a large portion of the basic
114         terrain shape, modifying this will break our contract of reverse
115         compatibility for a 'stable' mapgen such as V6.
116
117         tl;dr,
118         *** DO NOT TOUCH THIS CLASS UNLESS YOU KNOW WHAT YOU ARE DOING ***
119 */
120 class CavesV6 {
121 public:
122         MMVManip *vm;
123         INodeDefManager *ndef;
124         GenerateNotifier *gennotify;
125         PseudoRandom *ps;
126         PseudoRandom *ps2;
127
128         // configurable parameters
129         s16 *heightmap;
130         content_t c_water_source;
131         content_t c_lava_source;
132         int water_level;
133
134         // intermediate state variables
135         u16 ystride;
136
137         s16 min_tunnel_diameter;
138         s16 max_tunnel_diameter;
139         u16 tunnel_routepoints;
140         int dswitchint;
141         int part_max_length_rs;
142
143         bool large_cave;
144         bool large_cave_is_flat;
145
146         v3s16 node_min;
147         v3s16 node_max;
148
149         v3f orp;  // starting point, relative to caved space
150         v3s16 of; // absolute coordinates of caved space
151         v3s16 ar; // allowed route area
152         s16 rs;   // tunnel radius size
153         v3f main_direction;
154
155         s16 route_y_min;
156         s16 route_y_max;
157
158         // ndef is a mandatory parameter.
159         // If gennotify is NULL, generation events are not logged.
160         CavesV6(INodeDefManager *ndef,
161                 GenerateNotifier *gennotify = NULL,
162                 int water_level = 1,
163                 content_t water_source = CONTENT_IGNORE,
164                 content_t lava_source = CONTENT_IGNORE);
165
166         // vm, ps, and ps2 are mandatory parameters.
167         // If heightmap is NULL, the surface level at all points is assumed to
168         // be water_level.
169         void makeCave(MMVManip *vm, v3s16 nmin, v3s16 nmax,
170                 PseudoRandom *ps, PseudoRandom *ps2,
171                 bool is_large_cave, int max_stone_height, s16 *heightmap = NULL);
172
173 private:
174         void makeTunnel(bool dirswitch);
175         void carveRoute(v3f vec, float f, bool randomize_xz, bool tunnel_above_ground);
176
177         inline s16 getSurfaceFromHeightmap(v3s16 p);
178 };
179
180 #endif