Finish and clean up mapgen configuration
[oweals/minetest.git] / src / noise.h
1 /*
2 Minetest-c55
3 Copyright (C) 2010-2011 celeron55, Perttu Ahola <celeron55@gmail.com>
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 NOISE_HEADER
21 #define NOISE_HEADER
22
23 #include "debug.h"
24 #include "irr_v3d.h"
25
26 class PseudoRandom
27 {
28 public:
29         PseudoRandom(): m_next(0)
30         {
31         }
32         PseudoRandom(int seed): m_next(seed)
33         {
34         }
35         void seed(int seed)
36         {
37                 m_next = seed;
38         }
39         // Returns 0...32767
40         int next()
41         {
42                 m_next = m_next * 1103515245 + 12345;
43                 return((unsigned)(m_next/65536) % 32768);
44         }
45         int range(int min, int max)
46         {
47                 if(max-min > 32768/10)
48                 {
49                         //dstream<<"WARNING: PseudoRandom::range: max > 32767"<<std::endl;
50                         assert(0);
51                 }
52                 if(min > max)
53                 {
54                         assert(0);
55                         return max;
56                 }
57                 return (next()%(max-min+1))+min;
58         }
59 private:
60         int m_next;
61 };
62
63 struct NoiseParams {
64         float offset;
65         float scale;
66         v3f spread;
67         int seed;
68         int octaves;
69         float persist;
70 };
71
72
73 // Convenience macros for getting/setting NoiseParams in Settings
74 #define getNoiseParams(x) getStruct<NoiseParams>((x), "f,f,v3,s32,s32,f")
75 #define setNoiseParams(x, y) setStruct((x), "f,f,v3,s32,s32,f", (y))
76
77 class Noise {
78 public:
79         NoiseParams *np;
80         int seed;
81         int sx;
82         int sy;
83         int sz;
84         float *noisebuf;
85         float *buf;
86         float *result;
87
88         Noise(NoiseParams *np, int seed, int sx, int sy);
89         Noise(NoiseParams *np, int seed, int sx, int sy, int sz);
90         ~Noise();
91
92         void init(NoiseParams *np, int seed, int sx, int sy, int sz);
93         void setSize(int sx, int sy);
94         void setSize(int sx, int sy, int sz);
95         void setSpreadFactor(v3f spread);
96         void setOctaves(int octaves);
97         void resizeNoiseBuf(bool is3d);
98
99         void gradientMap2D(
100                 float x, float y,
101                 float step_x, float step_y,
102                 int seed);
103         void gradientMap3D(
104                 float x, float y, float z,
105                 float step_x, float step_y, float step_z,
106                 int seed);
107         float *perlinMap2D(float x, float y);
108         float *perlinMap3D(float x, float y, float z);
109         void transformNoiseMap();
110 };
111
112 // Return value: -1 ... 1
113 float noise2d(int x, int y, int seed);
114 float noise3d(int x, int y, int z, int seed);
115
116 float noise2d_gradient(float x, float y, int seed);
117 float noise3d_gradient(float x, float y, float z, int seed);
118
119 float noise2d_perlin(float x, float y, int seed,
120                 int octaves, float persistence);
121
122 float noise2d_perlin_abs(float x, float y, int seed,
123                 int octaves, float persistence);
124
125 float noise3d_perlin(float x, float y, float z, int seed,
126                 int octaves, float persistence);
127
128 float noise3d_perlin_abs(float x, float y, float z, int seed,
129                 int octaves, float persistence);
130
131 inline float easeCurve(float t) {
132         return t * t * t * (t * (6.f * t - 15.f) + 10.f);
133 }
134
135 #define NoisePerlin2D(np, x, y, s) ((np)->offset + (np)->scale * \
136                 noise2d_perlin((float)(x) / (np)->spread.X, (float)(y) / (np)->spread.Y, \
137                 (s) + (np)->seed, (np)->octaves, (np)->persist))
138
139 #define NoisePerlin3D(np, x, y, z, s) ((np)->offset + (np)->scale * \
140                 noise2d_perlin((float)(x) / (np)->spread.X, (float)(y) / (np)->spread.Y, \
141                 (float)(z) / (np)->spread.Z, (s) + (np)->seed, (np)->octaves, (np)->persist))
142
143 #endif
144