]> git.lizzy.rs Git - dragonfireclient.git/blob - src/cavegen.h
db3e091f6f3fd6a8fb404ae5548d9d1d2c432c2e
[dragonfireclient.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
25 class GenerateNotifier;
26
27 /*
28         CavesNoiseIntersection is a cave digging algorithm that carves smooth,
29         web-like, continuous tunnels at points where the density of the intersection
30         between two separate 3d noises is above a certain value.  This value,
31         cave_width, can be modified to set the effective width of these tunnels.
32
33         This algorithm is relatively heavyweight, taking ~80ms to generate an
34         80x80x80 chunk of map on a modern processor.  Use sparingly!
35
36         TODO(hmmmm): Remove dependency on biomes
37         TODO(hmmmm): Find alternative to overgeneration as solution for sunlight issue
38 */
39 class CavesNoiseIntersection
40 {
41 public:
42         CavesNoiseIntersection(INodeDefManager *nodedef, BiomeManager *biomemgr,
43                         v3s16 chunksize, NoiseParams *np_cave1, NoiseParams *np_cave2,
44                         s32 seed, float cave_width);
45         ~CavesNoiseIntersection();
46
47         void generateCaves(MMVManip *vm, v3s16 nmin, v3s16 nmax, u8 *biomemap);
48
49 private:
50         INodeDefManager *m_ndef;
51         BiomeManager *m_bmgr;
52
53         // configurable parameters
54         v3s16 m_csize;
55         float m_cave_width;
56
57         // intermediate state variables
58         u16 m_ystride;
59         u16 m_zstride_1d;
60
61         Noise *noise_cave1;
62         Noise *noise_cave2;
63 };
64
65 /*
66         CavernsNoise is a cave digging algorithm
67 */
68 class CavernsNoise
69 {
70 public:
71         CavernsNoise(INodeDefManager *nodedef, v3s16 chunksize, NoiseParams *np_cavern,
72                         s32 seed, float cavern_limit, float cavern_taper,
73                         float cavern_threshold);
74         ~CavernsNoise();
75
76         bool generateCaverns(MMVManip *vm, v3s16 nmin, v3s16 nmax);
77
78 private:
79         INodeDefManager *m_ndef;
80
81         // configurable parameters
82         v3s16 m_csize;
83         float m_cavern_limit;
84         float m_cavern_taper;
85         float m_cavern_threshold;
86
87         // intermediate state variables
88         u16 m_ystride;
89         u16 m_zstride_1d;
90
91         Noise *noise_cavern;
92
93         content_t c_water_source;
94         content_t c_lava_source;
95 };
96
97 /*
98         CavesRandomWalk is an implementation of a cave-digging algorithm that
99         operates on the principle of a "random walk" to approximate the stochiastic
100         activity of cavern development.
101
102         In summary, this algorithm works by carving a randomly sized tunnel in a
103         random direction a random amount of times, randomly varying in width.
104         All randomness here is uniformly distributed; alternative distributions have
105         not yet been implemented.
106
107         This algorithm is very fast, executing in less than 1ms on average for an
108         80x80x80 chunk of map on a modern processor.
109 */
110 class CavesRandomWalk
111 {
112 public:
113         MMVManip *vm;
114         INodeDefManager *ndef;
115         GenerateNotifier *gennotify;
116         s16 *heightmap;
117
118         // configurable parameters
119         s32 seed;
120         int water_level;
121         int lava_depth;
122         NoiseParams *np_caveliquids;
123
124         // intermediate state variables
125         u16 ystride;
126
127         s16 min_tunnel_diameter;
128         s16 max_tunnel_diameter;
129         u16 tunnel_routepoints;
130         int part_max_length_rs;
131
132         bool large_cave;
133         bool large_cave_is_flat;
134         bool flooded;
135
136         s16 max_stone_y;
137         v3s16 node_min;
138         v3s16 node_max;
139
140         v3f orp;  // starting point, relative to caved space
141         v3s16 of; // absolute coordinates of caved space
142         v3s16 ar; // allowed route area
143         s16 rs;   // tunnel radius size
144         v3f main_direction;
145
146         s16 route_y_min;
147         s16 route_y_max;
148
149         PseudoRandom *ps;
150
151         content_t c_water_source;
152         content_t c_lava_source;
153
154         // ndef is a mandatory parameter.
155         // If gennotify is NULL, generation events are not logged.
156         CavesRandomWalk(INodeDefManager *ndef, GenerateNotifier *gennotify = NULL,
157                         s32 seed = 0, int water_level = 1,
158                         content_t water_source = CONTENT_IGNORE,
159                         content_t lava_source = CONTENT_IGNORE, int lava_depth = -256);
160
161         // vm and ps are mandatory parameters.
162         // If heightmap is NULL, the surface level at all points is assumed to
163         // be water_level.
164         void makeCave(MMVManip *vm, v3s16 nmin, v3s16 nmax, PseudoRandom *ps,
165                         bool is_large_cave, int max_stone_height, s16 *heightmap);
166
167 private:
168         void makeTunnel(bool dirswitch);
169         void carveRoute(v3f vec, float f, bool randomize_xz);
170
171         inline bool isPosAboveSurface(v3s16 p);
172 };
173
174 /*
175         CavesV6 is the original version of caves used with Mapgen V6.
176
177         Though it uses the same fundamental algorithm as CavesRandomWalk, it is made
178         separate to preserve the exact sequence of PseudoRandom calls - any change
179         to this ordering results in the output being radically different.
180         Because caves in Mapgen V6 are responsible for a large portion of the basic
181         terrain shape, modifying this will break our contract of reverse
182         compatibility for a 'stable' mapgen such as V6.
183
184         tl;dr,
185         *** DO NOT TOUCH THIS CLASS UNLESS YOU KNOW WHAT YOU ARE DOING ***
186 */
187 class CavesV6
188 {
189 public:
190         MMVManip *vm;
191         INodeDefManager *ndef;
192         GenerateNotifier *gennotify;
193         PseudoRandom *ps;
194         PseudoRandom *ps2;
195
196         // configurable parameters
197         s16 *heightmap;
198         content_t c_water_source;
199         content_t c_lava_source;
200         int water_level;
201
202         // intermediate state variables
203         u16 ystride;
204
205         s16 min_tunnel_diameter;
206         s16 max_tunnel_diameter;
207         u16 tunnel_routepoints;
208         int part_max_length_rs;
209
210         bool large_cave;
211         bool large_cave_is_flat;
212
213         v3s16 node_min;
214         v3s16 node_max;
215
216         v3f orp;  // starting point, relative to caved space
217         v3s16 of; // absolute coordinates of caved space
218         v3s16 ar; // allowed route area
219         s16 rs;   // tunnel radius size
220         v3f main_direction;
221
222         s16 route_y_min;
223         s16 route_y_max;
224
225         // ndef is a mandatory parameter.
226         // If gennotify is NULL, generation events are not logged.
227         CavesV6(INodeDefManager *ndef, GenerateNotifier *gennotify = NULL,
228                         int water_level = 1, content_t water_source = CONTENT_IGNORE,
229                         content_t lava_source = CONTENT_IGNORE);
230
231         // vm, ps, and ps2 are mandatory parameters.
232         // If heightmap is NULL, the surface level at all points is assumed to
233         // be water_level.
234         void makeCave(MMVManip *vm, v3s16 nmin, v3s16 nmax, PseudoRandom *ps,
235                         PseudoRandom *ps2, bool is_large_cave, int max_stone_height,
236                         s16 *heightmap = NULL);
237
238 private:
239         void makeTunnel(bool dirswitch);
240         void carveRoute(v3f vec, float f, bool randomize_xz, bool tunnel_above_ground);
241
242         inline s16 getSurfaceFromHeightmap(v3s16 p);
243 };
244
245 #endif