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