CSM: Fix crashing minetest.get_item_def()
[oweals/minetest.git] / src / noise.h
1 /*
2  * Minetest
3  * Copyright (C) 2010-2014 celeron55, Perttu Ahola <celeron55@gmail.com>
4  * Copyright (C) 2010-2014 kwolekr, Ryan Kwolek <kwolekr@minetest.net>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without modification, are
8  * permitted provided that the following conditions are met:
9  *  1. Redistributions of source code must retain the above copyright notice, this list of
10  *     conditions and the following disclaimer.
11  *  2. Redistributions in binary form must reproduce the above copyright notice, this list
12  *     of conditions and the following disclaimer in the documentation and/or other materials
13  *     provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED
16  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
17  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR
18  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
20  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
21  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
22  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
23  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #pragma once
27
28 #include "irr_v3d.h"
29 #include "exceptions.h"
30 #include "util/string.h"
31
32 extern FlagDesc flagdesc_noiseparams[];
33
34 // Note: this class is not polymorphic so that its high level of
35 // optimizability may be preserved in the common use case
36 class PseudoRandom {
37 public:
38         const static u32 RANDOM_RANGE = 32767;
39
40         inline PseudoRandom(int seed=0):
41                 m_next(seed)
42         {
43         }
44
45         inline void seed(int seed)
46         {
47                 m_next = seed;
48         }
49
50         inline int next()
51         {
52                 m_next = m_next * 1103515245 + 12345;
53                 return (unsigned)(m_next / 65536) % (RANDOM_RANGE + 1);
54         }
55
56         inline int range(int min, int max)
57         {
58                 if (max < min)
59                         throw PrngException("Invalid range (max < min)");
60                 /*
61                 Here, we ensure the range is not too large relative to RANDOM_MAX,
62                 as otherwise the effects of bias would become noticable.  Unlike
63                 PcgRandom, we cannot modify this RNG's range as it would change the
64                 output of this RNG for reverse compatibility.
65                 */
66                 if ((u32)(max - min) > (RANDOM_RANGE + 1) / 10)
67                         throw PrngException("Range too large");
68
69                 return (next() % (max - min + 1)) + min;
70         }
71
72 private:
73         int m_next;
74 };
75
76 class PcgRandom {
77 public:
78         const static s32 RANDOM_MIN   = -0x7fffffff - 1;
79         const static s32 RANDOM_MAX   = 0x7fffffff;
80         const static u32 RANDOM_RANGE = 0xffffffff;
81
82         PcgRandom(u64 state=0x853c49e6748fea9bULL, u64 seq=0xda3e39cb94b95bdbULL);
83         void seed(u64 state, u64 seq=0xda3e39cb94b95bdbULL);
84         u32 next();
85         u32 range(u32 bound);
86         s32 range(s32 min, s32 max);
87         void bytes(void *out, size_t len);
88         s32 randNormalDist(s32 min, s32 max, int num_trials=6);
89
90 private:
91         u64 m_state;
92         u64 m_inc;
93 };
94
95 #define NOISE_FLAG_DEFAULTS    0x01
96 #define NOISE_FLAG_EASED       0x02
97 #define NOISE_FLAG_ABSVALUE    0x04
98
99 //// TODO(hmmmm): implement these!
100 #define NOISE_FLAG_POINTBUFFER 0x08
101 #define NOISE_FLAG_SIMPLEX     0x10
102
103 struct NoiseParams {
104         float offset = 0.0f;
105         float scale = 1.0f;
106         v3f spread = v3f(250, 250, 250);
107         s32 seed = 12345;
108         u16 octaves = 3;
109         float persist = 0.6f;
110         float lacunarity = 2.0f;
111         u32 flags = NOISE_FLAG_DEFAULTS;
112
113         NoiseParams() = default;
114
115         NoiseParams(float offset_, float scale_, const v3f &spread_, s32 seed_,
116                 u16 octaves_, float persist_, float lacunarity_,
117                 u32 flags_=NOISE_FLAG_DEFAULTS)
118         {
119                 offset     = offset_;
120                 scale      = scale_;
121                 spread     = spread_;
122                 seed       = seed_;
123                 octaves    = octaves_;
124                 persist    = persist_;
125                 lacunarity = lacunarity_;
126                 flags      = flags_;
127         }
128 };
129
130 class Noise {
131 public:
132         NoiseParams np;
133         s32 seed;
134         u32 sx;
135         u32 sy;
136         u32 sz;
137         float *noise_buf = nullptr;
138         float *gradient_buf = nullptr;
139         float *persist_buf = nullptr;
140         float *result = nullptr;
141
142         Noise(NoiseParams *np, s32 seed, u32 sx, u32 sy, u32 sz=1);
143         ~Noise();
144
145         void setSize(u32 sx, u32 sy, u32 sz=1);
146         void setSpreadFactor(v3f spread);
147         void setOctaves(int octaves);
148
149         void gradientMap2D(
150                 float x, float y,
151                 float step_x, float step_y,
152                 s32 seed);
153         void gradientMap3D(
154                 float x, float y, float z,
155                 float step_x, float step_y, float step_z,
156                 s32 seed);
157
158         float *perlinMap2D(float x, float y, float *persistence_map=NULL);
159         float *perlinMap3D(float x, float y, float z, float *persistence_map=NULL);
160
161         inline float *perlinMap2D_PO(float x, float xoff, float y, float yoff,
162                 float *persistence_map=NULL)
163         {
164                 return perlinMap2D(
165                         x + xoff * np.spread.X,
166                         y + yoff * np.spread.Y,
167                         persistence_map);
168         }
169
170         inline float *perlinMap3D_PO(float x, float xoff, float y, float yoff,
171                 float z, float zoff, float *persistence_map=NULL)
172         {
173                 return perlinMap3D(
174                         x + xoff * np.spread.X,
175                         y + yoff * np.spread.Y,
176                         z + zoff * np.spread.Z,
177                         persistence_map);
178         }
179
180 private:
181         void allocBuffers();
182         void resizeNoiseBuf(bool is3d);
183         void updateResults(float g, float *gmap, const float *persistence_map,
184                         size_t bufsize);
185
186 };
187
188 float NoisePerlin2D(NoiseParams *np, float x, float y, s32 seed);
189 float NoisePerlin3D(NoiseParams *np, float x, float y, float z, s32 seed);
190
191 inline float NoisePerlin2D_PO(NoiseParams *np, float x, float xoff,
192         float y, float yoff, s32 seed)
193 {
194         return NoisePerlin2D(np,
195                 x + xoff * np->spread.X,
196                 y + yoff * np->spread.Y,
197                 seed);
198 }
199
200 inline float NoisePerlin3D_PO(NoiseParams *np, float x, float xoff,
201         float y, float yoff, float z, float zoff, s32 seed)
202 {
203         return NoisePerlin3D(np,
204                 x + xoff * np->spread.X,
205                 y + yoff * np->spread.Y,
206                 z + zoff * np->spread.Z,
207                 seed);
208 }
209
210 // Return value: -1 ... 1
211 float noise2d(int x, int y, s32 seed);
212 float noise3d(int x, int y, int z, s32 seed);
213
214 float noise2d_gradient(float x, float y, s32 seed, bool eased=true);
215 float noise3d_gradient(float x, float y, float z, s32 seed, bool eased=false);
216
217 float noise2d_perlin(float x, float y, s32 seed,
218                 int octaves, float persistence, bool eased=true);
219
220 float noise2d_perlin_abs(float x, float y, s32 seed,
221                 int octaves, float persistence, bool eased=true);
222
223 float noise3d_perlin(float x, float y, float z, s32 seed,
224                 int octaves, float persistence, bool eased=false);
225
226 float noise3d_perlin_abs(float x, float y, float z, s32 seed,
227                 int octaves, float persistence, bool eased=false);
228
229 inline float easeCurve(float t)
230 {
231         return t * t * t * (t * (6.f * t - 15.f) + 10.f);
232 }
233
234 float contour(float v);