Huge overhaul of the entire MapgenParams system
[oweals/minetest.git] / src / noise.h
1 /*
2 Minetest
3 Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4 Copyright (C) 2010-2013 kwolekr, Ryan Kwolek <kwolekr@minetest.net>
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20
21 #ifndef NOISE_HEADER
22 #define NOISE_HEADER
23
24 #include "debug.h"
25 #include "irr_v3d.h"
26
27 class PseudoRandom
28 {
29 public:
30         PseudoRandom(): m_next(0)
31         {
32         }
33         PseudoRandom(int seed): m_next(seed)
34         {
35         }
36         void seed(int seed)
37         {
38                 m_next = seed;
39         }
40         // Returns 0...32767
41         int next()
42         {
43                 m_next = m_next * 1103515245 + 12345;
44                 return((unsigned)(m_next/65536) % 32768);
45         }
46         int range(int min, int max)
47         {
48                 if(max-min > 32768/10)
49                 {
50                         //dstream<<"WARNING: PseudoRandom::range: max > 32767"<<std::endl;
51                         assert(0);
52                 }
53                 if(min > max)
54                 {
55                         assert(0);
56                         return max;
57                 }
58                 return (next()%(max-min+1))+min;
59         }
60 private:
61         int m_next;
62 };
63
64 struct NoiseParams {
65         float offset;
66         float scale;
67         v3f spread;
68         int seed;
69         int octaves;
70         float persist;
71
72         NoiseParams() {}
73
74         NoiseParams(float offset_, float scale_, v3f spread_,
75                 int seed_, int octaves_, float persist_)
76         {
77                 offset  = offset_;
78                 scale   = scale_;
79                 spread  = spread_;
80                 seed    = seed_;
81                 octaves = octaves_;
82                 persist = persist_;
83         }
84 };
85
86
87 // Convenience macros for getting/setting NoiseParams in Settings
88 #define getNoiseParams(x, y) getStruct((x), "f,f,v3,s32,s32,f", &(y), sizeof(y))
89 #define setNoiseParams(x, y) setStruct((x), "f,f,v3,s32,s32,f", &(y))
90
91 class Noise {
92 public:
93         NoiseParams *np;
94         int seed;
95         int sx;
96         int sy;
97         int sz;
98         float *noisebuf;
99         float *buf;
100         float *result;
101
102         Noise(NoiseParams *np, int seed, int sx, int sy);
103         Noise(NoiseParams *np, int seed, int sx, int sy, int sz);
104         virtual ~Noise();
105
106         virtual void init(NoiseParams *np, int seed, int sx, int sy, int sz);
107         void setSize(int sx, int sy);
108         void setSize(int sx, int sy, int sz);
109         void setSpreadFactor(v3f spread);
110         void setOctaves(int octaves);
111         void resizeNoiseBuf(bool is3d);
112
113         void gradientMap2D(
114                 float x, float y,
115                 float step_x, float step_y,
116                 int seed);
117         void gradientMap3D(
118                 float x, float y, float z,
119                 float step_x, float step_y, float step_z,
120                 int seed);
121         float *perlinMap2D(float x, float y);
122         float *perlinMap2DModulated(float x, float y, float *persist_map);
123         float *perlinMap3D(float x, float y, float z);
124         void transformNoiseMap();
125 };
126
127 // Return value: -1 ... 1
128 float noise2d(int x, int y, int seed);
129 float noise3d(int x, int y, int z, int seed);
130
131 float noise2d_gradient(float x, float y, int seed);
132 float noise3d_gradient(float x, float y, float z, int seed);
133
134 float noise2d_perlin(float x, float y, int seed,
135                 int octaves, float persistence);
136
137 float noise2d_perlin_abs(float x, float y, int seed,
138                 int octaves, float persistence);
139
140 float noise3d_perlin(float x, float y, float z, int seed,
141                 int octaves, float persistence);
142
143 float noise3d_perlin_abs(float x, float y, float z, int seed,
144                 int octaves, float persistence);
145
146 inline float easeCurve(float t) {
147         return t * t * t * (t * (6.f * t - 15.f) + 10.f);
148 }
149
150 #define NoisePerlin2D(np, x, y, s) \
151                 ((np)->offset + (np)->scale * noise2d_perlin( \
152                 (float)(x) / (np)->spread.X, \
153                 (float)(y) / (np)->spread.Y, \
154                 (s) + (np)->seed, (np)->octaves, (np)->persist))
155
156 #define NoisePerlin2DNoTxfm(np, x, y, s) \
157                 (noise2d_perlin( \
158                 (float)(x) / (np)->spread.X, \
159                 (float)(y) / (np)->spread.Y, \
160                 (s) + (np)->seed, (np)->octaves, (np)->persist))
161
162 #define NoisePerlin2DPosOffset(np, x, xoff, y, yoff, s) \
163                 ((np)->offset + (np)->scale * noise2d_perlin( \
164                 (float)(xoff) + (float)(x) / (np)->spread.X, \
165                 (float)(yoff) + (float)(y) / (np)->spread.Y, \
166                 (s) + (np)->seed, (np)->octaves, (np)->persist))
167
168 #define NoisePerlin2DNoTxfmPosOffset(np, x, xoff, y, yoff, s) \
169                 (noise2d_perlin( \
170                 (float)(xoff) + (float)(x) / (np)->spread.X, \
171                 (float)(yoff) + (float)(y) / (np)->spread.Y, \
172                 (s) + (np)->seed, (np)->octaves, (np)->persist))
173
174 #define NoisePerlin3D(np, x, y, z, s) ((np)->offset + (np)->scale * \
175                 noise3d_perlin((float)(x) / (np)->spread.X, (float)(y) / (np)->spread.Y, \
176                 (float)(z) / (np)->spread.Z, (s) + (np)->seed, (np)->octaves, (np)->persist))
177
178 #endif
179