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