Replace PRNG assertions with PrngException
[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 #ifndef NOISE_HEADER
27 #define NOISE_HEADER
28
29 #include "irr_v3d.h"
30 #include "exceptions.h"
31 #include "util/string.h"
32
33 extern FlagDesc flagdesc_noiseparams[];
34
35 // Note: this class is not polymorphic so that its high level of
36 // optimizability may be preserved in the common use case
37 class PseudoRandom {
38 public:
39         const static u32 RANDOM_RANGE = 32767;
40
41         inline PseudoRandom(int seed=0):
42                 m_next(seed)
43         {
44         }
45
46         inline void seed(int seed)
47         {
48                 m_next = seed;
49         }
50
51         inline int next()
52         {
53                 m_next = m_next * 1103515245 + 12345;
54                 return (unsigned)(m_next / 65536) % (RANDOM_RANGE + 1);
55         }
56
57         inline int range(int min, int max)
58         {
59                 if (max < min)
60                         throw PrngException("Invalid range (max < min)");
61                 /*
62                 Here, we ensure the range is not too large relative to RANDOM_MAX,
63                 as otherwise the effects of bias would become noticable.  Unlike
64                 PcgRandom, we cannot modify this RNG's range as it would change the
65                 output of this RNG for reverse compatibility.
66                 */
67                 if ((u32)(max - min) > (RANDOM_RANGE + 1) / 10)
68                         throw PrngException("Range too large");
69
70                 return (next() % (max - min + 1)) + min;
71         }
72
73 private:
74         int m_next;
75 };
76
77 class PcgRandom {
78 public:
79         const static s32 RANDOM_MIN   = -0x7fffffff - 1;
80         const static s32 RANDOM_MAX   = 0x7fffffff;
81         const static u32 RANDOM_RANGE = 0xffffffff;
82
83         PcgRandom(u64 state=0x853c49e6748fea9bULL, u64 seq=0xda3e39cb94b95bdbULL);
84         void seed(u64 state, u64 seq=0xda3e39cb94b95bdbULL);
85         u32 next();
86         u32 range(u32 bound);
87         s32 range(s32 min, s32 max);
88         void bytes(void *out, size_t len);
89         s32 randNormalDist(s32 min, s32 max, int num_trials=6);
90
91 private:
92         u64 m_state;
93         u64 m_inc;
94 };
95
96 #define NOISE_FLAG_DEFAULTS    0x01
97 #define NOISE_FLAG_EASED       0x02
98 #define NOISE_FLAG_ABSVALUE    0x04
99
100 //// TODO(hmmmm): implement these!
101 #define NOISE_FLAG_POINTBUFFER 0x08
102 #define NOISE_FLAG_SIMPLEX     0x10
103
104 struct NoiseParams {
105         float offset;
106         float scale;
107         v3f spread;
108         s32 seed;
109         u16 octaves;
110         float persist;
111         float lacunarity;
112         u32 flags;
113
114         NoiseParams()
115         {
116                 offset     = 0.0f;
117                 scale      = 1.0f;
118                 spread     = v3f(250, 250, 250);
119                 seed       = 12345;
120                 octaves    = 3;
121                 persist    = 0.6f;
122                 lacunarity = 2.0f;
123                 flags      = NOISE_FLAG_DEFAULTS;
124         }
125
126         NoiseParams(float offset_, float scale_, v3f spread_, s32 seed_,
127                 u16 octaves_, float persist_, float lacunarity_,
128                 u32 flags_=NOISE_FLAG_DEFAULTS)
129         {
130                 offset     = offset_;
131                 scale      = scale_;
132                 spread     = spread_;
133                 seed       = seed_;
134                 octaves    = octaves_;
135                 persist    = persist_;
136                 lacunarity = lacunarity_;
137                 flags      = flags_;
138         }
139 };
140
141
142 // Convenience macros for getting/setting NoiseParams in Settings as a string
143 // WARNING:  Deprecated, use Settings::getNoiseParamsFromValue() instead
144 #define NOISEPARAMS_FMT_STR "f,f,v3,s32,u16,f"
145 //#define getNoiseParams(x, y) getStruct((x), NOISEPARAMS_FMT_STR, &(y), sizeof(y))
146 //#define setNoiseParams(x, y) setStruct((x), NOISEPARAMS_FMT_STR, &(y))
147
148 class Noise {
149 public:
150         NoiseParams np;
151         int seed;
152         int sx;
153         int sy;
154         int sz;
155         float *noise_buf;
156         float *gradient_buf;
157         float *persist_buf;
158         float *result;
159
160         Noise(NoiseParams *np, int seed, int sx, int sy, int sz=1);
161         ~Noise();
162
163         void setSize(int sx, int sy, int sz=1);
164         void setSpreadFactor(v3f spread);
165         void setOctaves(int octaves);
166
167         void gradientMap2D(
168                 float x, float y,
169                 float step_x, float step_y,
170                 int seed);
171         void gradientMap3D(
172                 float x, float y, float z,
173                 float step_x, float step_y, float step_z,
174                 int seed);
175
176         float *perlinMap2D(float x, float y, float *persistence_map=NULL);
177         float *perlinMap3D(float x, float y, float z, float *persistence_map=NULL);
178
179         inline float *perlinMap2D_PO(float x, float xoff, float y, float yoff,
180                 float *persistence_map=NULL)
181         {
182                 return perlinMap2D(
183                         x + xoff * np.spread.X,
184                         y + yoff * np.spread.Y,
185                         persistence_map);
186         }
187
188         inline float *perlinMap3D_PO(float x, float xoff, float y, float yoff,
189                 float z, float zoff, float *persistence_map=NULL)
190         {
191                 return perlinMap3D(
192                         x + xoff * np.spread.X,
193                         y + yoff * np.spread.Y,
194                         z + zoff * np.spread.Z,
195                         persistence_map);
196         }
197
198 private:
199         void allocBuffers();
200         void resizeNoiseBuf(bool is3d);
201         void updateResults(float g, float *gmap, float *persistence_map, size_t bufsize);
202
203 };
204
205 float NoisePerlin2D(NoiseParams *np, float x, float y, int seed);
206 float NoisePerlin3D(NoiseParams *np, float x, float y, float z, int seed);
207
208 inline float NoisePerlin2D_PO(NoiseParams *np, float x, float xoff,
209         float y, float yoff, int seed)
210 {
211         return NoisePerlin2D(np,
212                 x + xoff * np->spread.X,
213                 y + yoff * np->spread.Y,
214                 seed);
215 }
216
217 inline float NoisePerlin3D_PO(NoiseParams *np, float x, float xoff,
218         float y, float yoff, float z, float zoff, int seed)
219 {
220         return NoisePerlin3D(np,
221                 x + xoff * np->spread.X,
222                 y + yoff * np->spread.Y,
223                 z + zoff * np->spread.Z,
224                 seed);
225 }
226
227 // Return value: -1 ... 1
228 float noise2d(int x, int y, int seed);
229 float noise3d(int x, int y, int z, int seed);
230
231 float noise2d_gradient(float x, float y, int seed, bool eased=true);
232 float noise3d_gradient(float x, float y, float z, int seed, bool eased=false);
233
234 float noise2d_perlin(float x, float y, int seed,
235                 int octaves, float persistence, bool eased=true);
236
237 float noise2d_perlin_abs(float x, float y, int seed,
238                 int octaves, float persistence, bool eased=true);
239
240 float noise3d_perlin(float x, float y, float z, int seed,
241                 int octaves, float persistence, bool eased=false);
242
243 float noise3d_perlin_abs(float x, float y, float z, int seed,
244                 int octaves, float persistence, bool eased=false);
245
246 inline float easeCurve(float t)
247 {
248         return t * t * t * (t * (6.f * t - 15.f) + 10.f);
249 }
250
251 float contour(float v);
252
253 #endif
254