]> git.lizzy.rs Git - dragonfireclient.git/blob - src/mg_biome.h
Move biome calculation to BiomeGen
[dragonfireclient.git] / src / mg_biome.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 MG_BIOME_HEADER
21 #define MG_BIOME_HEADER
22
23 #include "objdef.h"
24 #include "nodedef.h"
25 #include "noise.h"
26
27 class Settings;
28 class BiomeManager;
29
30 ////
31 //// Biome
32 ////
33
34 #define BIOME_NONE ((u8)0)
35
36 enum BiomeType
37 {
38         BIOME_NORMAL,
39         BIOME_LIQUID,
40         BIOME_NETHER,
41         BIOME_AETHER,
42         BIOME_FLAT
43 };
44
45 class Biome : public ObjDef, public NodeResolver {
46 public:
47         u32 flags;
48
49         content_t c_top;
50         content_t c_filler;
51         content_t c_stone;
52         content_t c_water_top;
53         content_t c_water;
54         content_t c_river_water;
55         content_t c_dust;
56
57         s16 depth_top;
58         s16 depth_filler;
59         s16 depth_water_top;
60
61         s16 y_min;
62         s16 y_max;
63         float heat_point;
64         float humidity_point;
65
66         virtual void resolveNodeNames();
67 };
68
69
70 ////
71 //// BiomeGen
72 ////
73
74 enum BiomeGenType {
75         BIOMEGEN_ORIGINAL,
76 };
77
78 struct BiomeParams {
79         virtual void readParams(const Settings *settings) = 0;
80         virtual void writeParams(Settings *settings) const = 0;
81         virtual ~BiomeParams() {}
82
83         int seed;
84 };
85
86 class BiomeGen {
87 public:
88         virtual ~BiomeGen() {}
89         virtual BiomeGenType getType() const = 0;
90
91         // Calculates the biome at the exact position provided.  This function can
92         // be called at any time, but may be less efficient than the latter methods,
93         // depending on implementation.
94         virtual Biome *calcBiomeAtPoint(v3s16 pos) const = 0;
95
96         // Computes any intermediate results needed for biome generation.  Must be
97         // called before using any of: getBiomes, getBiomeAtPoint, or getBiomeAtIndex.
98         // Calling this invalidates the previous results stored in biomemap.
99         virtual void calcBiomeNoise(v3s16 pmin) = 0;
100
101         // Gets all biomes in current chunk using each corresponding element of
102         // heightmap as the y position, then stores the results by biome index in
103         // biomemap (also returned)
104         virtual u8 *getBiomes(s16 *heightmap) = 0;
105
106         // Gets a single biome at the specified position, which must be contained
107         // in the region formed by m_pmin and (m_pmin + m_csize - 1).
108         virtual Biome *getBiomeAtPoint(v3s16 pos) const = 0;
109
110         // Same as above, but uses a raw numeric index correlating to the (x,z) position.
111         virtual Biome *getBiomeAtIndex(size_t index, s16 y) const = 0;
112
113         // Result of calcBiomes bulk computation.
114         u8 *biomemap;
115
116 protected:
117         BiomeManager *m_bmgr;
118         v3s16 m_pmin;
119         v3s16 m_csize;
120 };
121
122
123 ////
124 //// BiomeGen implementations
125 ////
126
127 //
128 // Original biome algorithm (Whittaker's classification + surface height)
129 //
130
131 struct BiomeParamsOriginal : public BiomeParams {
132         BiomeParamsOriginal() :
133                 np_heat(50, 50, v3f(750.0, 750.0, 750.0), 5349, 3, 0.5, 2.0),
134                 np_humidity(50, 50, v3f(750.0, 750.0, 750.0), 842, 3, 0.5, 2.0),
135                 np_heat_blend(0, 1.5, v3f(8.0, 8.0, 8.0), 13, 2, 1.0, 2.0),
136                 np_humidity_blend(0, 1.5, v3f(8.0, 8.0, 8.0), 90003, 2, 1.0, 2.0)
137         {
138         }
139
140         virtual void readParams(const Settings *settings);
141         virtual void writeParams(Settings *settings) const;
142
143         NoiseParams np_heat;
144         NoiseParams np_humidity;
145         NoiseParams np_heat_blend;
146         NoiseParams np_humidity_blend;
147 };
148
149 class BiomeGenOriginal : public BiomeGen {
150 public:
151         BiomeGenOriginal(BiomeManager *biomemgr,
152                 BiomeParamsOriginal *params, v3s16 chunksize);
153         virtual ~BiomeGenOriginal();
154
155         BiomeGenType getType() const { return BIOMEGEN_ORIGINAL; }
156
157         Biome *calcBiomeAtPoint(v3s16 pos) const;
158         void calcBiomeNoise(v3s16 pmin);
159
160         u8 *getBiomes(s16 *heightmap);
161         Biome *getBiomeAtPoint(v3s16 pos) const;
162         Biome *getBiomeAtIndex(size_t index, s16 y) const;
163
164         Biome *calcBiomeFromNoise(float heat, float humidity, s16 y) const;
165
166         float *heatmap;
167         float *humidmap;
168
169 private:
170         BiomeParamsOriginal *m_params;
171
172         Noise *noise_heat;
173         Noise *noise_humidity;
174         Noise *noise_heat_blend;
175         Noise *noise_humidity_blend;
176 };
177
178
179 ////
180 //// BiomeManager
181 ////
182
183 class BiomeManager : public ObjDefManager {
184 public:
185         BiomeManager(IGameDef *gamedef);
186         virtual ~BiomeManager();
187
188         const char *getObjectTitle() const
189         {
190                 return "biome";
191         }
192
193         static Biome *create(BiomeType type)
194         {
195                 return new Biome;
196         }
197
198         BiomeGen *createBiomeGen(BiomeGenType type, BiomeParams *params, v3s16 chunksize)
199         {
200                 switch (type) {
201                 case BIOMEGEN_ORIGINAL:
202                         return new BiomeGenOriginal(this,
203                                 (BiomeParamsOriginal *)params, chunksize);
204                 default:
205                         return NULL;
206                 }
207         }
208
209         static BiomeParams *createBiomeParams(BiomeGenType type)
210         {
211                 switch (type) {
212                 case BIOMEGEN_ORIGINAL:
213                         return new BiomeParamsOriginal;
214                 default:
215                         return NULL;
216                 }
217         }
218
219         virtual void clear();
220
221         // Looks for pos in the biome cache, and if non-existent, looks up by noise
222         u8 getBiomeAtPoint(v3s16 pos);
223
224 private:
225         IGameDef *m_gamedef;
226
227 };
228
229
230 #endif