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
BiomeManager(Server * server)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_nnlistsizes.push_back(1);
67 b->m_nodenames.emplace_back("ignore");
68 b->m_nodenames.emplace_back("ignore");
69 b->m_nodenames.emplace_back("ignore");
70 m_ndef->pendNodeResolve(b);
71
72 add(b);
73 }
74
75
clear()76 void BiomeManager::clear()
77 {
78 EmergeManager *emerge = m_server->getEmergeManager();
79
80 // Remove all dangling references in Decorations
81 DecorationManager *decomgr = emerge->getWritableDecorationManager();
82 for (size_t i = 0; i != decomgr->getNumObjects(); i++) {
83 Decoration *deco = (Decoration *)decomgr->getRaw(i);
84 deco->biomes.clear();
85 }
86
87 // Don't delete the first biome
88 for (size_t i = 1; i < m_objects.size(); i++)
89 delete (Biome *)m_objects[i];
90
91 m_objects.resize(1);
92 }
93
94
clone() const95 BiomeManager *BiomeManager::clone() const
96 {
97 auto mgr = new BiomeManager();
98 assert(mgr);
99 ObjDefManager::cloneTo(mgr);
100 mgr->m_server = m_server;
101 return mgr;
102 }
103
104
105 // For BiomeGen type 'BiomeGenOriginal'
getHeatAtPosOriginal(v3s16 pos,NoiseParams & np_heat,NoiseParams & np_heat_blend,u64 seed) const106 float BiomeManager::getHeatAtPosOriginal(v3s16 pos, NoiseParams &np_heat,
107 NoiseParams &np_heat_blend, u64 seed) const
108 {
109 return
110 NoisePerlin2D(&np_heat, pos.X, pos.Z, seed) +
111 NoisePerlin2D(&np_heat_blend, pos.X, pos.Z, seed);
112 }
113
114
115 // For BiomeGen type 'BiomeGenOriginal'
getHumidityAtPosOriginal(v3s16 pos,NoiseParams & np_humidity,NoiseParams & np_humidity_blend,u64 seed) const116 float BiomeManager::getHumidityAtPosOriginal(v3s16 pos, NoiseParams &np_humidity,
117 NoiseParams &np_humidity_blend, u64 seed) const
118 {
119 return
120 NoisePerlin2D(&np_humidity, pos.X, pos.Z, seed) +
121 NoisePerlin2D(&np_humidity_blend, pos.X, pos.Z, seed);
122 }
123
124
125 // For BiomeGen type 'BiomeGenOriginal'
getBiomeFromNoiseOriginal(float heat,float humidity,v3s16 pos) const126 const Biome *BiomeManager::getBiomeFromNoiseOriginal(float heat,
127 float humidity, v3s16 pos) const
128 {
129 Biome *biome_closest = nullptr;
130 Biome *biome_closest_blend = nullptr;
131 float dist_min = FLT_MAX;
132 float dist_min_blend = FLT_MAX;
133
134 for (size_t i = 1; i < getNumObjects(); i++) {
135 Biome *b = (Biome *)getRaw(i);
136 if (!b ||
137 pos.Y < b->min_pos.Y || pos.Y > b->max_pos.Y + b->vertical_blend ||
138 pos.X < b->min_pos.X || pos.X > b->max_pos.X ||
139 pos.Z < b->min_pos.Z || pos.Z > b->max_pos.Z)
140 continue;
141
142 float d_heat = heat - b->heat_point;
143 float d_humidity = humidity - b->humidity_point;
144 float dist = (d_heat * d_heat) + (d_humidity * d_humidity);
145
146 if (pos.Y <= b->max_pos.Y) { // Within y limits of biome b
147 if (dist < dist_min) {
148 dist_min = dist;
149 biome_closest = b;
150 }
151 } else if (dist < dist_min_blend) { // Blend area above biome b
152 dist_min_blend = dist;
153 biome_closest_blend = b;
154 }
155 }
156
157 const u64 seed = pos.Y + (heat + humidity) * 0.9f;
158 PcgRandom rng(seed);
159
160 if (biome_closest_blend && dist_min_blend <= dist_min &&
161 rng.range(0, biome_closest_blend->vertical_blend) >=
162 pos.Y - biome_closest_blend->max_pos.Y)
163 return biome_closest_blend;
164
165 return (biome_closest) ? biome_closest : (Biome *)getRaw(BIOME_NONE);
166 }
167
168
169 ////////////////////////////////////////////////////////////////////////////////
170
readParams(const Settings * settings)171 void BiomeParamsOriginal::readParams(const Settings *settings)
172 {
173 settings->getNoiseParams("mg_biome_np_heat", np_heat);
174 settings->getNoiseParams("mg_biome_np_heat_blend", np_heat_blend);
175 settings->getNoiseParams("mg_biome_np_humidity", np_humidity);
176 settings->getNoiseParams("mg_biome_np_humidity_blend", np_humidity_blend);
177 }
178
179
writeParams(Settings * settings) const180 void BiomeParamsOriginal::writeParams(Settings *settings) const
181 {
182 settings->setNoiseParams("mg_biome_np_heat", np_heat);
183 settings->setNoiseParams("mg_biome_np_heat_blend", np_heat_blend);
184 settings->setNoiseParams("mg_biome_np_humidity", np_humidity);
185 settings->setNoiseParams("mg_biome_np_humidity_blend", np_humidity_blend);
186 }
187
188
189 ////////////////////////////////////////////////////////////////////////////////
190
BiomeGenOriginal(BiomeManager * biomemgr,BiomeParamsOriginal * params,v3s16 chunksize)191 BiomeGenOriginal::BiomeGenOriginal(BiomeManager *biomemgr,
192 BiomeParamsOriginal *params, v3s16 chunksize)
193 {
194 m_bmgr = biomemgr;
195 m_params = params;
196 m_csize = chunksize;
197
198 noise_heat = new Noise(¶ms->np_heat,
199 params->seed, m_csize.X, m_csize.Z);
200 noise_humidity = new Noise(¶ms->np_humidity,
201 params->seed, m_csize.X, m_csize.Z);
202 noise_heat_blend = new Noise(¶ms->np_heat_blend,
203 params->seed, m_csize.X, m_csize.Z);
204 noise_humidity_blend = new Noise(¶ms->np_humidity_blend,
205 params->seed, m_csize.X, m_csize.Z);
206
207 heatmap = noise_heat->result;
208 humidmap = noise_humidity->result;
209
210 biomemap = new biome_t[m_csize.X * m_csize.Z];
211 // Initialise with the ID of 'BIOME_NONE' so that cavegen can get the
212 // fallback biome when biome generation (which calculates the biomemap IDs)
213 // is disabled.
214 memset(biomemap, 0, sizeof(biome_t) * m_csize.X * m_csize.Z);
215 }
216
~BiomeGenOriginal()217 BiomeGenOriginal::~BiomeGenOriginal()
218 {
219 delete []biomemap;
220
221 delete noise_heat;
222 delete noise_humidity;
223 delete noise_heat_blend;
224 delete noise_humidity_blend;
225 }
226
227 // Only usable in a mapgen thread
calcBiomeAtPoint(v3s16 pos) const228 Biome *BiomeGenOriginal::calcBiomeAtPoint(v3s16 pos) const
229 {
230 float heat =
231 NoisePerlin2D(&m_params->np_heat, pos.X, pos.Z, m_params->seed) +
232 NoisePerlin2D(&m_params->np_heat_blend, pos.X, pos.Z, m_params->seed);
233 float humidity =
234 NoisePerlin2D(&m_params->np_humidity, pos.X, pos.Z, m_params->seed) +
235 NoisePerlin2D(&m_params->np_humidity_blend, pos.X, pos.Z, m_params->seed);
236
237 return calcBiomeFromNoise(heat, humidity, pos);
238 }
239
240
calcBiomeNoise(v3s16 pmin)241 void BiomeGenOriginal::calcBiomeNoise(v3s16 pmin)
242 {
243 m_pmin = pmin;
244
245 noise_heat->perlinMap2D(pmin.X, pmin.Z);
246 noise_humidity->perlinMap2D(pmin.X, pmin.Z);
247 noise_heat_blend->perlinMap2D(pmin.X, pmin.Z);
248 noise_humidity_blend->perlinMap2D(pmin.X, pmin.Z);
249
250 for (s32 i = 0; i < m_csize.X * m_csize.Z; i++) {
251 noise_heat->result[i] += noise_heat_blend->result[i];
252 noise_humidity->result[i] += noise_humidity_blend->result[i];
253 }
254 }
255
256
getBiomes(s16 * heightmap,v3s16 pmin)257 biome_t *BiomeGenOriginal::getBiomes(s16 *heightmap, v3s16 pmin)
258 {
259 for (s16 zr = 0; zr < m_csize.Z; zr++)
260 for (s16 xr = 0; xr < m_csize.X; xr++) {
261 s32 i = zr * m_csize.X + xr;
262 Biome *biome = calcBiomeFromNoise(
263 noise_heat->result[i],
264 noise_humidity->result[i],
265 v3s16(pmin.X + xr, heightmap[i], pmin.Z + zr));
266
267 biomemap[i] = biome->index;
268 }
269
270 return biomemap;
271 }
272
273
getBiomeAtPoint(v3s16 pos) const274 Biome *BiomeGenOriginal::getBiomeAtPoint(v3s16 pos) const
275 {
276 return getBiomeAtIndex(
277 (pos.Z - m_pmin.Z) * m_csize.X + (pos.X - m_pmin.X),
278 pos);
279 }
280
281
getBiomeAtIndex(size_t index,v3s16 pos) const282 Biome *BiomeGenOriginal::getBiomeAtIndex(size_t index, v3s16 pos) const
283 {
284 return calcBiomeFromNoise(
285 noise_heat->result[index],
286 noise_humidity->result[index],
287 pos);
288 }
289
290
calcBiomeFromNoise(float heat,float humidity,v3s16 pos) const291 Biome *BiomeGenOriginal::calcBiomeFromNoise(float heat, float humidity, v3s16 pos) const
292 {
293 Biome *biome_closest = nullptr;
294 Biome *biome_closest_blend = nullptr;
295 float dist_min = FLT_MAX;
296 float dist_min_blend = FLT_MAX;
297
298 for (size_t i = 1; i < m_bmgr->getNumObjects(); i++) {
299 Biome *b = (Biome *)m_bmgr->getRaw(i);
300 if (!b ||
301 pos.Y < b->min_pos.Y || pos.Y > b->max_pos.Y + b->vertical_blend ||
302 pos.X < b->min_pos.X || pos.X > b->max_pos.X ||
303 pos.Z < b->min_pos.Z || pos.Z > b->max_pos.Z)
304 continue;
305
306 float d_heat = heat - b->heat_point;
307 float d_humidity = humidity - b->humidity_point;
308 float dist = (d_heat * d_heat) + (d_humidity * d_humidity);
309
310 if (pos.Y <= b->max_pos.Y) { // Within y limits of biome b
311 if (dist < dist_min) {
312 dist_min = dist;
313 biome_closest = b;
314 }
315 } else if (dist < dist_min_blend) { // Blend area above biome b
316 dist_min_blend = dist;
317 biome_closest_blend = b;
318 }
319 }
320
321 // Carefully tune pseudorandom seed variation to avoid single node dither
322 // and create larger scale blending patterns similar to horizontal biome
323 // blend.
324 const u64 seed = pos.Y + (heat + humidity) * 0.9f;
325 PcgRandom rng(seed);
326
327 if (biome_closest_blend && dist_min_blend <= dist_min &&
328 rng.range(0, biome_closest_blend->vertical_blend) >=
329 pos.Y - biome_closest_blend->max_pos.Y)
330 return biome_closest_blend;
331
332 return (biome_closest) ? biome_closest : (Biome *)m_bmgr->getRaw(BIOME_NONE);
333 }
334
335
336 ////////////////////////////////////////////////////////////////////////////////
337
clone() const338 ObjDef *Biome::clone() const
339 {
340 auto obj = new Biome();
341 ObjDef::cloneTo(obj);
342 NodeResolver::cloneTo(obj);
343
344 obj->flags = flags;
345
346 obj->c_top = c_top;
347 obj->c_filler = c_filler;
348 obj->c_stone = c_stone;
349 obj->c_water_top = c_water_top;
350 obj->c_water = c_water;
351 obj->c_river_water = c_river_water;
352 obj->c_riverbed = c_riverbed;
353 obj->c_dust = c_dust;
354 obj->c_cave_liquid = c_cave_liquid;
355 obj->c_dungeon = c_dungeon;
356 obj->c_dungeon_alt = c_dungeon_alt;
357 obj->c_dungeon_stair = c_dungeon_stair;
358
359 obj->depth_top = depth_top;
360 obj->depth_filler = depth_filler;
361 obj->depth_water_top = depth_water_top;
362 obj->depth_riverbed = depth_riverbed;
363
364 obj->min_pos = min_pos;
365 obj->max_pos = max_pos;
366 obj->heat_point = heat_point;
367 obj->humidity_point = humidity_point;
368 obj->vertical_blend = vertical_blend;
369
370 return obj;
371 }
372
resolveNodeNames()373 void Biome::resolveNodeNames()
374 {
375 getIdFromNrBacklog(&c_top, "mapgen_stone", CONTENT_AIR, false);
376 getIdFromNrBacklog(&c_filler, "mapgen_stone", CONTENT_AIR, false);
377 getIdFromNrBacklog(&c_stone, "mapgen_stone", CONTENT_AIR, false);
378 getIdFromNrBacklog(&c_water_top, "mapgen_water_source", CONTENT_AIR, false);
379 getIdFromNrBacklog(&c_water, "mapgen_water_source", CONTENT_AIR, false);
380 getIdFromNrBacklog(&c_river_water, "mapgen_river_water_source", CONTENT_AIR, false);
381 getIdFromNrBacklog(&c_riverbed, "mapgen_stone", CONTENT_AIR, false);
382 getIdFromNrBacklog(&c_dust, "ignore", CONTENT_IGNORE, false);
383 getIdsFromNrBacklog(&c_cave_liquid);
384 getIdFromNrBacklog(&c_dungeon, "ignore", CONTENT_IGNORE, false);
385 getIdFromNrBacklog(&c_dungeon_alt, "ignore", CONTENT_IGNORE, false);
386 getIdFromNrBacklog(&c_dungeon_stair, "ignore", CONTENT_IGNORE, false);
387 }
388