3 Copyright (C) 2014-2016 kwolekr, Ryan Kwolek <kwolekr@minetest.net>
4 Copyright (C) 2014-2017 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;
54 b->m_nodenames.push_back("mapgen_stone");
55 b->m_nodenames.push_back("mapgen_stone");
56 b->m_nodenames.push_back("mapgen_stone");
57 b->m_nodenames.push_back("mapgen_water_source");
58 b->m_nodenames.push_back("mapgen_water_source");
59 b->m_nodenames.push_back("mapgen_river_water_source");
60 b->m_nodenames.push_back("mapgen_stone");
61 b->m_nodenames.push_back("ignore");
62 m_ndef->pendNodeResolve(b);
68 BiomeManager::~BiomeManager()
73 void BiomeManager::clear()
75 EmergeManager *emerge = m_server->getEmergeManager();
77 // Remove all dangling references in Decorations
78 DecorationManager *decomgr = emerge->decomgr;
79 for (size_t i = 0; i != decomgr->getNumObjects(); i++) {
80 Decoration *deco = (Decoration *)decomgr->getRaw(i);
84 // Don't delete the first biome
85 for (size_t i = 1; i < m_objects.size(); i++)
86 delete (Biome *)m_objects[i];
91 ////////////////////////////////////////////////////////////////////////////////
94 void BiomeParamsOriginal::readParams(const Settings *settings)
96 settings->getNoiseParams("mg_biome_np_heat", np_heat);
97 settings->getNoiseParams("mg_biome_np_heat_blend", np_heat_blend);
98 settings->getNoiseParams("mg_biome_np_humidity", np_humidity);
99 settings->getNoiseParams("mg_biome_np_humidity_blend", np_humidity_blend);
103 void BiomeParamsOriginal::writeParams(Settings *settings) const
105 settings->setNoiseParams("mg_biome_np_heat", np_heat);
106 settings->setNoiseParams("mg_biome_np_heat_blend", np_heat_blend);
107 settings->setNoiseParams("mg_biome_np_humidity", np_humidity);
108 settings->setNoiseParams("mg_biome_np_humidity_blend", np_humidity_blend);
112 ////////////////////////////////////////////////////////////////////////////////
114 BiomeGenOriginal::BiomeGenOriginal(BiomeManager *biomemgr,
115 BiomeParamsOriginal *params, v3s16 chunksize)
121 noise_heat = new Noise(¶ms->np_heat,
122 params->seed, m_csize.X, m_csize.Z);
123 noise_humidity = new Noise(¶ms->np_humidity,
124 params->seed, m_csize.X, m_csize.Z);
125 noise_heat_blend = new Noise(¶ms->np_heat_blend,
126 params->seed, m_csize.X, m_csize.Z);
127 noise_humidity_blend = new Noise(¶ms->np_humidity_blend,
128 params->seed, m_csize.X, m_csize.Z);
130 heatmap = noise_heat->result;
131 humidmap = noise_humidity->result;
132 biomemap = new biome_t[m_csize.X * m_csize.Z];
135 BiomeGenOriginal::~BiomeGenOriginal()
140 delete noise_humidity;
141 delete noise_heat_blend;
142 delete noise_humidity_blend;
146 Biome *BiomeGenOriginal::calcBiomeAtPoint(v3s16 pos) const
149 NoisePerlin2D(&m_params->np_heat, pos.X, pos.Z, m_params->seed) +
150 NoisePerlin2D(&m_params->np_heat_blend, pos.X, pos.Z, m_params->seed);
152 NoisePerlin2D(&m_params->np_humidity, pos.X, pos.Z, m_params->seed) +
153 NoisePerlin2D(&m_params->np_humidity_blend, pos.X, pos.Z, m_params->seed);
155 return calcBiomeFromNoise(heat, humidity, pos.Y);
159 void BiomeGenOriginal::calcBiomeNoise(v3s16 pmin)
163 noise_heat->perlinMap2D(pmin.X, pmin.Z);
164 noise_humidity->perlinMap2D(pmin.X, pmin.Z);
165 noise_heat_blend->perlinMap2D(pmin.X, pmin.Z);
166 noise_humidity_blend->perlinMap2D(pmin.X, pmin.Z);
168 for (s32 i = 0; i < m_csize.X * m_csize.Z; i++) {
169 noise_heat->result[i] += noise_heat_blend->result[i];
170 noise_humidity->result[i] += noise_humidity_blend->result[i];
175 biome_t *BiomeGenOriginal::getBiomes(s16 *heightmap)
177 for (s32 i = 0; i != m_csize.X * m_csize.Z; i++) {
178 Biome *biome = calcBiomeFromNoise(
179 noise_heat->result[i],
180 noise_humidity->result[i],
183 biomemap[i] = biome->index;
190 Biome *BiomeGenOriginal::getBiomeAtPoint(v3s16 pos) const
192 return getBiomeAtIndex(
193 (pos.Z - m_pmin.Z) * m_csize.X + (pos.X - m_pmin.X),
198 Biome *BiomeGenOriginal::getBiomeAtIndex(size_t index, s16 y) const
200 return calcBiomeFromNoise(
201 noise_heat->result[index],
202 noise_humidity->result[index],
207 Biome *BiomeGenOriginal::calcBiomeFromNoise(float heat, float humidity, s16 y) const
209 Biome *b, *biome_closest = NULL;
210 float dist_min = FLT_MAX;
212 for (size_t i = 1; i < m_bmgr->getNumObjects(); i++) {
213 b = (Biome *)m_bmgr->getRaw(i);
214 if (!b || y > b->y_max || y < b->y_min)
217 float d_heat = heat - b->heat_point;
218 float d_humidity = humidity - b->humidity_point;
219 float dist = (d_heat * d_heat) +
220 (d_humidity * d_humidity);
221 if (dist < dist_min) {
227 return biome_closest ? biome_closest : (Biome *)m_bmgr->getRaw(BIOME_NONE);
231 ////////////////////////////////////////////////////////////////////////////////
233 void Biome::resolveNodeNames()
235 getIdFromNrBacklog(&c_top, "mapgen_stone", CONTENT_AIR);
236 getIdFromNrBacklog(&c_filler, "mapgen_stone", CONTENT_AIR);
237 getIdFromNrBacklog(&c_stone, "mapgen_stone", CONTENT_AIR);
238 getIdFromNrBacklog(&c_water_top, "mapgen_water_source", CONTENT_AIR);
239 getIdFromNrBacklog(&c_water, "mapgen_water_source", CONTENT_AIR);
240 getIdFromNrBacklog(&c_river_water, "mapgen_river_water_source", CONTENT_AIR);
241 getIdFromNrBacklog(&c_riverbed, "mapgen_stone", CONTENT_AIR);
242 getIdFromNrBacklog(&c_dust, "ignore", CONTENT_IGNORE);