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->y_min = -MAX_MAP_GENERATION_LIMIT;
50 b->y_max = MAX_MAP_GENERATION_LIMIT;
52 b->humidity_point = 0.0;
53 b->vertical_blend = 0;
55 b->m_nodenames.emplace_back("mapgen_stone");
56 b->m_nodenames.emplace_back("mapgen_stone");
57 b->m_nodenames.emplace_back("mapgen_stone");
58 b->m_nodenames.emplace_back("mapgen_water_source");
59 b->m_nodenames.emplace_back("mapgen_water_source");
60 b->m_nodenames.emplace_back("mapgen_river_water_source");
61 b->m_nodenames.emplace_back("mapgen_stone");
62 b->m_nodenames.emplace_back("ignore");
63 m_ndef->pendNodeResolve(b);
69 void BiomeManager::clear()
71 EmergeManager *emerge = m_server->getEmergeManager();
73 // Remove all dangling references in Decorations
74 DecorationManager *decomgr = emerge->decomgr;
75 for (size_t i = 0; i != decomgr->getNumObjects(); i++) {
76 Decoration *deco = (Decoration *)decomgr->getRaw(i);
80 // Don't delete the first biome
81 for (size_t i = 1; i < m_objects.size(); i++)
82 delete (Biome *)m_objects[i];
88 // For BiomeGen type 'BiomeGenOriginal'
89 float BiomeManager::getHeatAtPosOriginal(v3s16 pos, NoiseParams &np_heat,
90 NoiseParams &np_heat_blend, u64 seed)
93 NoisePerlin2D(&np_heat, pos.X, pos.Z, seed) +
94 NoisePerlin2D(&np_heat_blend, pos.X, pos.Z, seed);
98 // For BiomeGen type 'BiomeGenOriginal'
99 float BiomeManager::getHumidityAtPosOriginal(v3s16 pos, NoiseParams &np_humidity,
100 NoiseParams &np_humidity_blend, u64 seed)
103 NoisePerlin2D(&np_humidity, pos.X, pos.Z, seed) +
104 NoisePerlin2D(&np_humidity_blend, pos.X, pos.Z, seed);
108 // For BiomeGen type 'BiomeGenOriginal'
109 Biome *BiomeManager::getBiomeFromNoiseOriginal(float heat, float humidity, s16 y)
111 Biome *biome_closest = nullptr;
112 Biome *biome_closest_blend = nullptr;
113 float dist_min = FLT_MAX;
114 float dist_min_blend = FLT_MAX;
116 for (size_t i = 1; i < getNumObjects(); i++) {
117 Biome *b = (Biome *)getRaw(i);
118 if (!b || y > b->y_max + b->vertical_blend || y < b->y_min)
121 float d_heat = heat - b->heat_point;
122 float d_humidity = humidity - b->humidity_point;
123 float dist = (d_heat * d_heat) + (d_humidity * d_humidity);
125 if (y <= b->y_max) { // Within y limits of biome b
126 if (dist < dist_min) {
130 } else if (dist < dist_min_blend) { // Blend area above biome b
131 dist_min_blend = dist;
132 biome_closest_blend = b;
136 mysrand(y + (heat + humidity) / 2);
137 if (biome_closest_blend &&
138 myrand_range(0, biome_closest_blend->vertical_blend) >=
139 y - biome_closest_blend->y_max)
140 return biome_closest_blend;
142 return (biome_closest) ? biome_closest : (Biome *)getRaw(BIOME_NONE);
146 ////////////////////////////////////////////////////////////////////////////////
148 void BiomeParamsOriginal::readParams(const Settings *settings)
150 settings->getNoiseParams("mg_biome_np_heat", np_heat);
151 settings->getNoiseParams("mg_biome_np_heat_blend", np_heat_blend);
152 settings->getNoiseParams("mg_biome_np_humidity", np_humidity);
153 settings->getNoiseParams("mg_biome_np_humidity_blend", np_humidity_blend);
157 void BiomeParamsOriginal::writeParams(Settings *settings) const
159 settings->setNoiseParams("mg_biome_np_heat", np_heat);
160 settings->setNoiseParams("mg_biome_np_heat_blend", np_heat_blend);
161 settings->setNoiseParams("mg_biome_np_humidity", np_humidity);
162 settings->setNoiseParams("mg_biome_np_humidity_blend", np_humidity_blend);
166 ////////////////////////////////////////////////////////////////////////////////
168 BiomeGenOriginal::BiomeGenOriginal(BiomeManager *biomemgr,
169 BiomeParamsOriginal *params, v3s16 chunksize)
175 noise_heat = new Noise(¶ms->np_heat,
176 params->seed, m_csize.X, m_csize.Z);
177 noise_humidity = new Noise(¶ms->np_humidity,
178 params->seed, m_csize.X, m_csize.Z);
179 noise_heat_blend = new Noise(¶ms->np_heat_blend,
180 params->seed, m_csize.X, m_csize.Z);
181 noise_humidity_blend = new Noise(¶ms->np_humidity_blend,
182 params->seed, m_csize.X, m_csize.Z);
184 heatmap = noise_heat->result;
185 humidmap = noise_humidity->result;
186 biomemap = new biome_t[m_csize.X * m_csize.Z];
189 BiomeGenOriginal::~BiomeGenOriginal()
194 delete noise_humidity;
195 delete noise_heat_blend;
196 delete noise_humidity_blend;
199 // Only usable in a mapgen thread
200 Biome *BiomeGenOriginal::calcBiomeAtPoint(v3s16 pos) const
203 NoisePerlin2D(&m_params->np_heat, pos.X, pos.Z, m_params->seed) +
204 NoisePerlin2D(&m_params->np_heat_blend, pos.X, pos.Z, m_params->seed);
206 NoisePerlin2D(&m_params->np_humidity, pos.X, pos.Z, m_params->seed) +
207 NoisePerlin2D(&m_params->np_humidity_blend, pos.X, pos.Z, m_params->seed);
209 return calcBiomeFromNoise(heat, humidity, pos.Y);
213 void BiomeGenOriginal::calcBiomeNoise(v3s16 pmin)
217 noise_heat->perlinMap2D(pmin.X, pmin.Z);
218 noise_humidity->perlinMap2D(pmin.X, pmin.Z);
219 noise_heat_blend->perlinMap2D(pmin.X, pmin.Z);
220 noise_humidity_blend->perlinMap2D(pmin.X, pmin.Z);
222 for (s32 i = 0; i < m_csize.X * m_csize.Z; i++) {
223 noise_heat->result[i] += noise_heat_blend->result[i];
224 noise_humidity->result[i] += noise_humidity_blend->result[i];
229 biome_t *BiomeGenOriginal::getBiomes(s16 *heightmap)
231 for (s32 i = 0; i != m_csize.X * m_csize.Z; i++) {
232 Biome *biome = calcBiomeFromNoise(
233 noise_heat->result[i],
234 noise_humidity->result[i],
237 biomemap[i] = biome->index;
244 Biome *BiomeGenOriginal::getBiomeAtPoint(v3s16 pos) const
246 return getBiomeAtIndex(
247 (pos.Z - m_pmin.Z) * m_csize.X + (pos.X - m_pmin.X),
252 Biome *BiomeGenOriginal::getBiomeAtIndex(size_t index, s16 y) const
254 return calcBiomeFromNoise(
255 noise_heat->result[index],
256 noise_humidity->result[index],
261 Biome *BiomeGenOriginal::calcBiomeFromNoise(float heat, float humidity, s16 y) const
263 Biome *biome_closest = nullptr;
264 Biome *biome_closest_blend = nullptr;
265 float dist_min = FLT_MAX;
266 float dist_min_blend = FLT_MAX;
268 for (size_t i = 1; i < m_bmgr->getNumObjects(); i++) {
269 Biome *b = (Biome *)m_bmgr->getRaw(i);
270 if (!b || y > b->y_max + b->vertical_blend || y < b->y_min)
273 float d_heat = heat - b->heat_point;
274 float d_humidity = humidity - b->humidity_point;
275 float dist = (d_heat * d_heat) + (d_humidity * d_humidity);
277 if (y <= b->y_max) { // Within y limits of biome b
278 if (dist < dist_min) {
282 } else if (dist < dist_min_blend) { // Blend area above biome b
283 dist_min_blend = dist;
284 biome_closest_blend = b;
288 // Carefully tune pseudorandom seed variation to avoid single node dither
289 // and create larger scale blending patterns similar to horizontal biome
291 mysrand(y + (heat + humidity) / 2);
293 if (biome_closest_blend &&
294 myrand_range(0, biome_closest_blend->vertical_blend) >=
295 y - biome_closest_blend->y_max)
296 return biome_closest_blend;
298 return (biome_closest) ? biome_closest : (Biome *)m_bmgr->getRaw(BIOME_NONE);
302 ////////////////////////////////////////////////////////////////////////////////
304 void Biome::resolveNodeNames()
306 getIdFromNrBacklog(&c_top, "mapgen_stone", CONTENT_AIR);
307 getIdFromNrBacklog(&c_filler, "mapgen_stone", CONTENT_AIR);
308 getIdFromNrBacklog(&c_stone, "mapgen_stone", CONTENT_AIR);
309 getIdFromNrBacklog(&c_water_top, "mapgen_water_source", CONTENT_AIR);
310 getIdFromNrBacklog(&c_water, "mapgen_water_source", CONTENT_AIR);
311 getIdFromNrBacklog(&c_river_water, "mapgen_river_water_source", CONTENT_AIR);
312 getIdFromNrBacklog(&c_riverbed, "mapgen_stone", CONTENT_AIR);
313 getIdFromNrBacklog(&c_dust, "ignore", CONTENT_IGNORE);