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