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