3 Copyright (C) 2014-2018 kwolekr, Ryan Kwolek <kwolekr@minetest.net>
4 Copyright (C) 2014-2018 paramat
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.
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.
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.
22 #include "mg_decoration.h"
26 #include "map.h" //for MMVManip
27 #include "util/numeric.h"
32 ///////////////////////////////////////////////////////////////////////////////
35 BiomeManager::BiomeManager(Server *server) :
36 ObjDefManager(server, OBJDEF_BIOME)
40 // Create default biome to be used in case none exist
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);
54 b->humidity_point = 0.0;
55 b->vertical_blend = 0;
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 m_ndef->pendNodeResolve(b);
72 void BiomeManager::clear()
74 EmergeManager *emerge = m_server->getEmergeManager();
76 // Remove all dangling references in Decorations
77 DecorationManager *decomgr = emerge->decomgr;
78 for (size_t i = 0; i != decomgr->getNumObjects(); i++) {
79 Decoration *deco = (Decoration *)decomgr->getRaw(i);
83 // Don't delete the first biome
84 for (size_t i = 1; i < m_objects.size(); i++)
85 delete (Biome *)m_objects[i];
91 // For BiomeGen type 'BiomeGenOriginal'
92 float BiomeManager::getHeatAtPosOriginal(v3s16 pos, NoiseParams &np_heat,
93 NoiseParams &np_heat_blend, u64 seed)
96 NoisePerlin2D(&np_heat, pos.X, pos.Z, seed) +
97 NoisePerlin2D(&np_heat_blend, pos.X, pos.Z, seed);
101 // For BiomeGen type 'BiomeGenOriginal'
102 float BiomeManager::getHumidityAtPosOriginal(v3s16 pos, NoiseParams &np_humidity,
103 NoiseParams &np_humidity_blend, u64 seed)
106 NoisePerlin2D(&np_humidity, pos.X, pos.Z, seed) +
107 NoisePerlin2D(&np_humidity_blend, pos.X, pos.Z, seed);
111 // For BiomeGen type 'BiomeGenOriginal'
112 Biome *BiomeManager::getBiomeFromNoiseOriginal(float heat, float humidity, v3s16 pos)
114 Biome *biome_closest = nullptr;
115 Biome *biome_closest_blend = nullptr;
116 float dist_min = FLT_MAX;
117 float dist_min_blend = FLT_MAX;
119 for (size_t i = 1; i < getNumObjects(); i++) {
120 Biome *b = (Biome *)getRaw(i);
122 pos.Y < b->min_pos.Y || pos.Y > b->max_pos.Y + b->vertical_blend ||
123 pos.X < b->min_pos.X || pos.X > b->max_pos.X ||
124 pos.Z < b->min_pos.Z || pos.Z > b->max_pos.Z)
127 float d_heat = heat - b->heat_point;
128 float d_humidity = humidity - b->humidity_point;
129 float dist = (d_heat * d_heat) + (d_humidity * d_humidity);
131 if (pos.Y <= b->max_pos.Y) { // Within y limits of biome b
132 if (dist < dist_min) {
136 } else if (dist < dist_min_blend) { // Blend area above biome b
137 dist_min_blend = dist;
138 biome_closest_blend = b;
142 mysrand(pos.Y + (heat + humidity) / 2);
143 if (biome_closest_blend && dist_min_blend <= dist_min &&
144 myrand_range(0, biome_closest_blend->vertical_blend) >=
145 pos.Y - biome_closest_blend->max_pos.Y)
146 return biome_closest_blend;
148 return (biome_closest) ? biome_closest : (Biome *)getRaw(BIOME_NONE);
152 ////////////////////////////////////////////////////////////////////////////////
154 void BiomeParamsOriginal::readParams(const Settings *settings)
156 settings->getNoiseParams("mg_biome_np_heat", np_heat);
157 settings->getNoiseParams("mg_biome_np_heat_blend", np_heat_blend);
158 settings->getNoiseParams("mg_biome_np_humidity", np_humidity);
159 settings->getNoiseParams("mg_biome_np_humidity_blend", np_humidity_blend);
163 void BiomeParamsOriginal::writeParams(Settings *settings) const
165 settings->setNoiseParams("mg_biome_np_heat", np_heat);
166 settings->setNoiseParams("mg_biome_np_heat_blend", np_heat_blend);
167 settings->setNoiseParams("mg_biome_np_humidity", np_humidity);
168 settings->setNoiseParams("mg_biome_np_humidity_blend", np_humidity_blend);
172 ////////////////////////////////////////////////////////////////////////////////
174 BiomeGenOriginal::BiomeGenOriginal(BiomeManager *biomemgr,
175 BiomeParamsOriginal *params, v3s16 chunksize)
181 noise_heat = new Noise(¶ms->np_heat,
182 params->seed, m_csize.X, m_csize.Z);
183 noise_humidity = new Noise(¶ms->np_humidity,
184 params->seed, m_csize.X, m_csize.Z);
185 noise_heat_blend = new Noise(¶ms->np_heat_blend,
186 params->seed, m_csize.X, m_csize.Z);
187 noise_humidity_blend = new Noise(¶ms->np_humidity_blend,
188 params->seed, m_csize.X, m_csize.Z);
190 heatmap = noise_heat->result;
191 humidmap = noise_humidity->result;
192 biomemap = new biome_t[m_csize.X * m_csize.Z];
195 BiomeGenOriginal::~BiomeGenOriginal()
200 delete noise_humidity;
201 delete noise_heat_blend;
202 delete noise_humidity_blend;
205 // Only usable in a mapgen thread
206 Biome *BiomeGenOriginal::calcBiomeAtPoint(v3s16 pos) const
209 NoisePerlin2D(&m_params->np_heat, pos.X, pos.Z, m_params->seed) +
210 NoisePerlin2D(&m_params->np_heat_blend, pos.X, pos.Z, m_params->seed);
212 NoisePerlin2D(&m_params->np_humidity, pos.X, pos.Z, m_params->seed) +
213 NoisePerlin2D(&m_params->np_humidity_blend, pos.X, pos.Z, m_params->seed);
215 return calcBiomeFromNoise(heat, humidity, pos);
219 void BiomeGenOriginal::calcBiomeNoise(v3s16 pmin)
223 noise_heat->perlinMap2D(pmin.X, pmin.Z);
224 noise_humidity->perlinMap2D(pmin.X, pmin.Z);
225 noise_heat_blend->perlinMap2D(pmin.X, pmin.Z);
226 noise_humidity_blend->perlinMap2D(pmin.X, pmin.Z);
228 for (s32 i = 0; i < m_csize.X * m_csize.Z; i++) {
229 noise_heat->result[i] += noise_heat_blend->result[i];
230 noise_humidity->result[i] += noise_humidity_blend->result[i];
235 biome_t *BiomeGenOriginal::getBiomes(s16 *heightmap, v3s16 pmin)
237 for (s16 zr = 0; zr < m_csize.Z; zr++)
238 for (s16 xr = 0; xr < m_csize.X; xr++) {
239 s32 i = zr * m_csize.X + xr;
240 Biome *biome = calcBiomeFromNoise(
241 noise_heat->result[i],
242 noise_humidity->result[i],
243 v3s16(pmin.X + xr, heightmap[i], pmin.Z + zr));
245 biomemap[i] = biome->index;
252 Biome *BiomeGenOriginal::getBiomeAtPoint(v3s16 pos) const
254 return getBiomeAtIndex(
255 (pos.Z - m_pmin.Z) * m_csize.X + (pos.X - m_pmin.X),
260 Biome *BiomeGenOriginal::getBiomeAtIndex(size_t index, v3s16 pos) const
262 return calcBiomeFromNoise(
263 noise_heat->result[index],
264 noise_humidity->result[index],
269 Biome *BiomeGenOriginal::calcBiomeFromNoise(float heat, float humidity, v3s16 pos) const
271 Biome *biome_closest = nullptr;
272 Biome *biome_closest_blend = nullptr;
273 float dist_min = FLT_MAX;
274 float dist_min_blend = FLT_MAX;
276 for (size_t i = 1; i < m_bmgr->getNumObjects(); i++) {
277 Biome *b = (Biome *)m_bmgr->getRaw(i);
279 pos.Y < b->min_pos.Y || pos.Y > b->max_pos.Y + b->vertical_blend ||
280 pos.X < b->min_pos.X || pos.X > b->max_pos.X ||
281 pos.Z < b->min_pos.Z || pos.Z > b->max_pos.Z)
284 float d_heat = heat - b->heat_point;
285 float d_humidity = humidity - b->humidity_point;
286 float dist = (d_heat * d_heat) + (d_humidity * d_humidity);
288 if (pos.Y <= b->max_pos.Y) { // Within y limits of biome b
289 if (dist < dist_min) {
293 } else if (dist < dist_min_blend) { // Blend area above biome b
294 dist_min_blend = dist;
295 biome_closest_blend = b;
299 // Carefully tune pseudorandom seed variation to avoid single node dither
300 // and create larger scale blending patterns similar to horizontal biome
302 mysrand(pos.Y + (heat + humidity) / 2);
304 if (biome_closest_blend && dist_min_blend <= dist_min &&
305 myrand_range(0, biome_closest_blend->vertical_blend) >=
306 pos.Y - biome_closest_blend->max_pos.Y)
307 return biome_closest_blend;
309 return (biome_closest) ? biome_closest : (Biome *)m_bmgr->getRaw(BIOME_NONE);
313 ////////////////////////////////////////////////////////////////////////////////
315 void Biome::resolveNodeNames()
317 getIdFromNrBacklog(&c_top, "mapgen_stone", CONTENT_AIR);
318 getIdFromNrBacklog(&c_filler, "mapgen_stone", CONTENT_AIR);
319 getIdFromNrBacklog(&c_stone, "mapgen_stone", CONTENT_AIR);
320 getIdFromNrBacklog(&c_water_top, "mapgen_water_source", CONTENT_AIR);
321 getIdFromNrBacklog(&c_water, "mapgen_water_source", CONTENT_AIR);
322 getIdFromNrBacklog(&c_river_water, "mapgen_river_water_source", CONTENT_AIR);
323 getIdFromNrBacklog(&c_riverbed, "mapgen_stone", CONTENT_AIR);
324 getIdFromNrBacklog(&c_dust, "ignore", CONTENT_IGNORE);
325 getIdFromNrBacklog(&c_cave_liquid, "ignore", CONTENT_IGNORE);