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