]> git.lizzy.rs Git - dragonfireclient.git/blob - src/mapgen/mg_biome.cpp
mapgen: drop mapgen id from child mapgens.
[dragonfireclient.git] / src / mapgen / mg_biome.cpp
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 #include "mg_biome.h"
22 #include "mg_decoration.h"
23 #include "emerge.h"
24 #include "server.h"
25 #include "nodedef.h"
26 #include "map.h" //for MMVManip
27 #include "util/numeric.h"
28 #include "porting.h"
29 #include "settings.h"
30
31
32 ///////////////////////////////////////////////////////////////////////////////
33
34
35 BiomeManager::BiomeManager(Server *server) :
36         ObjDefManager(server, OBJDEF_BIOME)
37 {
38         m_server = server;
39
40         // Create default biome to be used in case none exist
41         Biome *b = new Biome;
42
43         b->name            = "default";
44         b->flags           = 0;
45         b->depth_top       = 0;
46         b->depth_filler    = -MAX_MAP_GENERATION_LIMIT;
47         b->depth_water_top = 0;
48         b->depth_riverbed  = 0;
49         b->min_pos         = v3s16(-MAX_MAP_GENERATION_LIMIT,
50                         -MAX_MAP_GENERATION_LIMIT, -MAX_MAP_GENERATION_LIMIT);
51         b->max_pos         = v3s16(MAX_MAP_GENERATION_LIMIT,
52                         MAX_MAP_GENERATION_LIMIT, MAX_MAP_GENERATION_LIMIT);
53         b->heat_point      = 0.0;
54         b->humidity_point  = 0.0;
55         b->vertical_blend  = 0;
56
57         b->m_nodenames.emplace_back("mapgen_stone");
58         b->m_nodenames.emplace_back("mapgen_stone");
59         b->m_nodenames.emplace_back("mapgen_stone");
60         b->m_nodenames.emplace_back("mapgen_water_source");
61         b->m_nodenames.emplace_back("mapgen_water_source");
62         b->m_nodenames.emplace_back("mapgen_river_water_source");
63         b->m_nodenames.emplace_back("mapgen_stone");
64         b->m_nodenames.emplace_back("ignore");
65         b->m_nodenames.emplace_back("ignore");
66         b->m_nodenames.emplace_back("ignore");
67         b->m_nodenames.emplace_back("ignore");
68         b->m_nodenames.emplace_back("ignore");
69         m_ndef->pendNodeResolve(b);
70
71         add(b);
72 }
73
74
75 void BiomeManager::clear()
76 {
77         EmergeManager *emerge = m_server->getEmergeManager();
78
79         // Remove all dangling references in Decorations
80         DecorationManager *decomgr = emerge->decomgr;
81         for (size_t i = 0; i != decomgr->getNumObjects(); i++) {
82                 Decoration *deco = (Decoration *)decomgr->getRaw(i);
83                 deco->biomes.clear();
84         }
85
86         // Don't delete the first biome
87         for (size_t i = 1; i < m_objects.size(); i++)
88                 delete (Biome *)m_objects[i];
89
90         m_objects.resize(1);
91 }
92
93
94 // For BiomeGen type 'BiomeGenOriginal'
95 float BiomeManager::getHeatAtPosOriginal(v3s16 pos, NoiseParams &np_heat,
96         NoiseParams &np_heat_blend, u64 seed)
97 {
98         return
99                 NoisePerlin2D(&np_heat,       pos.X, pos.Z, seed) +
100                 NoisePerlin2D(&np_heat_blend, pos.X, pos.Z, seed);
101 }
102
103
104 // For BiomeGen type 'BiomeGenOriginal'
105 float BiomeManager::getHumidityAtPosOriginal(v3s16 pos, NoiseParams &np_humidity,
106         NoiseParams &np_humidity_blend, u64 seed)
107 {
108         return
109                 NoisePerlin2D(&np_humidity,       pos.X, pos.Z, seed) +
110                 NoisePerlin2D(&np_humidity_blend, pos.X, pos.Z, seed);
111 }
112
113
114 // For BiomeGen type 'BiomeGenOriginal'
115 Biome *BiomeManager::getBiomeFromNoiseOriginal(float heat, float humidity, v3s16 pos)
116 {
117         Biome *biome_closest = nullptr;
118         Biome *biome_closest_blend = nullptr;
119         float dist_min = FLT_MAX;
120         float dist_min_blend = FLT_MAX;
121
122         for (size_t i = 1; i < getNumObjects(); i++) {
123                 Biome *b = (Biome *)getRaw(i);
124                 if (!b ||
125                                 pos.Y < b->min_pos.Y || pos.Y > b->max_pos.Y + b->vertical_blend ||
126                                 pos.X < b->min_pos.X || pos.X > b->max_pos.X ||
127                                 pos.Z < b->min_pos.Z || pos.Z > b->max_pos.Z)
128                         continue;
129
130                 float d_heat = heat - b->heat_point;
131                 float d_humidity = humidity - b->humidity_point;
132                 float dist = (d_heat * d_heat) + (d_humidity * d_humidity);
133
134                 if (pos.Y <= b->max_pos.Y) { // Within y limits of biome b
135                         if (dist < dist_min) {
136                                 dist_min = dist;
137                                 biome_closest = b;
138                         }
139                 } else if (dist < dist_min_blend) { // Blend area above biome b
140                         dist_min_blend = dist;
141                         biome_closest_blend = b;
142                 }
143         }
144
145         mysrand(pos.Y + (heat + humidity) * 0.9f);
146         if (biome_closest_blend && dist_min_blend <= dist_min &&
147                         myrand_range(0, biome_closest_blend->vertical_blend) >=
148                         pos.Y - biome_closest_blend->max_pos.Y)
149                 return biome_closest_blend;
150
151         return (biome_closest) ? biome_closest : (Biome *)getRaw(BIOME_NONE);
152 }
153
154
155 ////////////////////////////////////////////////////////////////////////////////
156
157 void BiomeParamsOriginal::readParams(const Settings *settings)
158 {
159         settings->getNoiseParams("mg_biome_np_heat",           np_heat);
160         settings->getNoiseParams("mg_biome_np_heat_blend",     np_heat_blend);
161         settings->getNoiseParams("mg_biome_np_humidity",       np_humidity);
162         settings->getNoiseParams("mg_biome_np_humidity_blend", np_humidity_blend);
163 }
164
165
166 void BiomeParamsOriginal::writeParams(Settings *settings) const
167 {
168         settings->setNoiseParams("mg_biome_np_heat",           np_heat);
169         settings->setNoiseParams("mg_biome_np_heat_blend",     np_heat_blend);
170         settings->setNoiseParams("mg_biome_np_humidity",       np_humidity);
171         settings->setNoiseParams("mg_biome_np_humidity_blend", np_humidity_blend);
172 }
173
174
175 ////////////////////////////////////////////////////////////////////////////////
176
177 BiomeGenOriginal::BiomeGenOriginal(BiomeManager *biomemgr,
178         BiomeParamsOriginal *params, v3s16 chunksize)
179 {
180         m_bmgr   = biomemgr;
181         m_params = params;
182         m_csize  = chunksize;
183
184         noise_heat           = new Noise(&params->np_heat,
185                                                                         params->seed, m_csize.X, m_csize.Z);
186         noise_humidity       = new Noise(&params->np_humidity,
187                                                                         params->seed, m_csize.X, m_csize.Z);
188         noise_heat_blend     = new Noise(&params->np_heat_blend,
189                                                                         params->seed, m_csize.X, m_csize.Z);
190         noise_humidity_blend = new Noise(&params->np_humidity_blend,
191                                                                         params->seed, m_csize.X, m_csize.Z);
192
193         heatmap  = noise_heat->result;
194         humidmap = noise_humidity->result;
195
196         biomemap = new biome_t[m_csize.X * m_csize.Z];
197         // Initialise with the ID of 'BIOME_NONE' so that cavegen can get the
198         // fallback biome when biome generation (which calculates the biomemap IDs)
199         // is disabled.
200         memset(biomemap, 0, sizeof(biome_t) * m_csize.X * m_csize.Z);
201 }
202
203 BiomeGenOriginal::~BiomeGenOriginal()
204 {
205         delete []biomemap;
206
207         delete noise_heat;
208         delete noise_humidity;
209         delete noise_heat_blend;
210         delete noise_humidity_blend;
211 }
212
213 // Only usable in a mapgen thread
214 Biome *BiomeGenOriginal::calcBiomeAtPoint(v3s16 pos) const
215 {
216         float heat =
217                 NoisePerlin2D(&m_params->np_heat,       pos.X, pos.Z, m_params->seed) +
218                 NoisePerlin2D(&m_params->np_heat_blend, pos.X, pos.Z, m_params->seed);
219         float humidity =
220                 NoisePerlin2D(&m_params->np_humidity,       pos.X, pos.Z, m_params->seed) +
221                 NoisePerlin2D(&m_params->np_humidity_blend, pos.X, pos.Z, m_params->seed);
222
223         return calcBiomeFromNoise(heat, humidity, pos);
224 }
225
226
227 void BiomeGenOriginal::calcBiomeNoise(v3s16 pmin)
228 {
229         m_pmin = pmin;
230
231         noise_heat->perlinMap2D(pmin.X, pmin.Z);
232         noise_humidity->perlinMap2D(pmin.X, pmin.Z);
233         noise_heat_blend->perlinMap2D(pmin.X, pmin.Z);
234         noise_humidity_blend->perlinMap2D(pmin.X, pmin.Z);
235
236         for (s32 i = 0; i < m_csize.X * m_csize.Z; i++) {
237                 noise_heat->result[i]     += noise_heat_blend->result[i];
238                 noise_humidity->result[i] += noise_humidity_blend->result[i];
239         }
240 }
241
242
243 biome_t *BiomeGenOriginal::getBiomes(s16 *heightmap, v3s16 pmin)
244 {
245         for (s16 zr = 0; zr < m_csize.Z; zr++)
246         for (s16 xr = 0; xr < m_csize.X; xr++) {
247                 s32 i = zr * m_csize.X + xr;
248                 Biome *biome = calcBiomeFromNoise(
249                         noise_heat->result[i],
250                         noise_humidity->result[i],
251                         v3s16(pmin.X + xr, heightmap[i], pmin.Z + zr));
252
253                 biomemap[i] = biome->index;
254         }
255
256         return biomemap;
257 }
258
259
260 Biome *BiomeGenOriginal::getBiomeAtPoint(v3s16 pos) const
261 {
262         return getBiomeAtIndex(
263                 (pos.Z - m_pmin.Z) * m_csize.X + (pos.X - m_pmin.X),
264                 pos);
265 }
266
267
268 Biome *BiomeGenOriginal::getBiomeAtIndex(size_t index, v3s16 pos) const
269 {
270         return calcBiomeFromNoise(
271                 noise_heat->result[index],
272                 noise_humidity->result[index],
273                 pos);
274 }
275
276
277 Biome *BiomeGenOriginal::calcBiomeFromNoise(float heat, float humidity, v3s16 pos) const
278 {
279         Biome *biome_closest = nullptr;
280         Biome *biome_closest_blend = nullptr;
281         float dist_min = FLT_MAX;
282         float dist_min_blend = FLT_MAX;
283
284         for (size_t i = 1; i < m_bmgr->getNumObjects(); i++) {
285                 Biome *b = (Biome *)m_bmgr->getRaw(i);
286                 if (!b ||
287                                 pos.Y < b->min_pos.Y || pos.Y > b->max_pos.Y + b->vertical_blend ||
288                                 pos.X < b->min_pos.X || pos.X > b->max_pos.X ||
289                                 pos.Z < b->min_pos.Z || pos.Z > b->max_pos.Z)
290                         continue;
291
292                 float d_heat = heat - b->heat_point;
293                 float d_humidity = humidity - b->humidity_point;
294                 float dist = (d_heat * d_heat) + (d_humidity * d_humidity);
295
296                 if (pos.Y <= b->max_pos.Y) { // Within y limits of biome b
297                         if (dist < dist_min) {
298                                 dist_min = dist;
299                                 biome_closest = b;
300                         }
301                 } else if (dist < dist_min_blend) { // Blend area above biome b
302                         dist_min_blend = dist;
303                         biome_closest_blend = b;
304                 }
305         }
306
307         // Carefully tune pseudorandom seed variation to avoid single node dither
308         // and create larger scale blending patterns similar to horizontal biome
309         // blend.
310         mysrand(pos.Y + (heat + humidity) * 0.9f);
311
312         if (biome_closest_blend && dist_min_blend <= dist_min &&
313                         myrand_range(0, biome_closest_blend->vertical_blend) >=
314                         pos.Y - biome_closest_blend->max_pos.Y)
315                 return biome_closest_blend;
316
317         return (biome_closest) ? biome_closest : (Biome *)m_bmgr->getRaw(BIOME_NONE);   
318 }
319
320
321 ////////////////////////////////////////////////////////////////////////////////
322
323 void Biome::resolveNodeNames()
324 {
325         getIdFromNrBacklog(&c_top,           "mapgen_stone",              CONTENT_AIR,    false);
326         getIdFromNrBacklog(&c_filler,        "mapgen_stone",              CONTENT_AIR,    false);
327         getIdFromNrBacklog(&c_stone,         "mapgen_stone",              CONTENT_AIR,    false);
328         getIdFromNrBacklog(&c_water_top,     "mapgen_water_source",       CONTENT_AIR,    false);
329         getIdFromNrBacklog(&c_water,         "mapgen_water_source",       CONTENT_AIR,    false);
330         getIdFromNrBacklog(&c_river_water,   "mapgen_river_water_source", CONTENT_AIR,    false);
331         getIdFromNrBacklog(&c_riverbed,      "mapgen_stone",              CONTENT_AIR,    false);
332         getIdFromNrBacklog(&c_dust,          "ignore",                    CONTENT_IGNORE, false);
333         getIdFromNrBacklog(&c_cave_liquid,   "ignore",                    CONTENT_IGNORE, false);
334         getIdFromNrBacklog(&c_dungeon,       "ignore",                    CONTENT_IGNORE, false);
335         getIdFromNrBacklog(&c_dungeon_alt,   "ignore",                    CONTENT_IGNORE, false);
336         getIdFromNrBacklog(&c_dungeon_stair, "ignore",                    CONTENT_IGNORE, false);
337 }