3 Copyright (C) 2010-2013 kwolekr, Ryan Kwolek <kwolekr@minetest.net>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include "map.h" //for ManualMapVoxelManipulator
24 #include "util/numeric.h"
26 #include "util/mathconstants.h"
28 NoiseParams nparams_biome_def_heat(50, 50, v3f(500.0, 500.0, 500.0), 5349, 3, 0.70);
29 NoiseParams nparams_biome_def_humidity(50, 50, v3f(500.0, 500.0, 500.0), 842, 3, 0.55);
32 BiomeDefManager::BiomeDefManager() {
33 biome_registration_finished = false;
34 np_heat = &nparams_biome_def_heat;
35 np_humidity = &nparams_biome_def_humidity;
37 // Create default biome to be used in case none exist
48 b->nname_filler = "air";
49 b->nname_water = "mapgen_water_source";
50 b->nname_dust = "air";
51 b->nname_dust_water = "mapgen_water_source";
53 b->c_top = CONTENT_IGNORE;
54 b->c_filler = CONTENT_IGNORE;
55 b->c_water = CONTENT_IGNORE;
56 b->c_dust = CONTENT_IGNORE;
57 b->c_dust_water = CONTENT_IGNORE;
59 b->height_min = -MAP_GENERATION_LIMIT;
60 b->height_max = MAP_GENERATION_LIMIT;
62 b->humidity_point = 0.0;
68 BiomeDefManager::~BiomeDefManager() {
70 // delete[] biomecache;
72 for (size_t i = 0; i != biomes.size(); i++)
77 Biome *BiomeDefManager::createBiome(BiomeTerrainType btt) {
79 case BIOME_TERRAIN_NORMAL:
81 case BIOME_TERRAIN_LIQUID:
82 return new BiomeLiquid;
83 case BIOME_TERRAIN_NETHER:
85 case BIOME_TERRAIN_AETHER:
87 case BIOME_TERRAIN_FLAT:
88 return new BiomeSuperflat;
95 // just a PoC, obviously needs optimization later on (precalculate this)
96 void BiomeDefManager::calcBiomes(BiomeNoiseInput *input, u8 *biomeid_map) {
98 for (int y = 0; y != input->mapsize.Y; y++) {
99 for (int x = 0; x != input->mapsize.X; x++, i++) {
100 float heat = (input->heat_map[i] + 1) * 50;
101 float humidity = (input->humidity_map[i] + 1) * 50;
102 biomeid_map[i] = getBiome(heat, humidity, input->height_map[i])->id;
108 void BiomeDefManager::resolveNodeNames(INodeDefManager *ndef) {
111 biome_registration_finished = true;
113 for (size_t i = 0; i < biomes.size(); i++) {
116 b->c_top = ndef->getId(b->nname_top);
117 if (b->c_top == CONTENT_IGNORE) {
118 errorstream << "BiomeDefManager::resolveNodeNames: node '"
119 << b->nname_top << "' not defined" << std::endl;
120 b->c_top = CONTENT_AIR;
124 b->c_filler = ndef->getId(b->nname_filler);
125 if (b->c_filler == CONTENT_IGNORE) {
126 errorstream << "BiomeDefManager::resolveNodeNames: node '"
127 << b->nname_filler << "' not defined" << std::endl;
128 b->c_filler = CONTENT_AIR;
132 b->c_water = ndef->getId(b->nname_water);
133 if (b->c_water == CONTENT_IGNORE) {
134 errorstream << "BiomeDefManager::resolveNodeNames: node '"
135 << b->nname_water << "' not defined" << std::endl;
136 b->c_water = CONTENT_AIR;
139 b->c_dust = ndef->getId(b->nname_dust);
140 if (b->c_dust == CONTENT_IGNORE) {
141 errorstream << "BiomeDefManager::resolveNodeNames: node '"
142 << b->nname_dust << "' not defined" << std::endl;
145 b->c_dust_water = ndef->getId(b->nname_dust_water);
146 if (b->c_dust_water == CONTENT_IGNORE) {
147 errorstream << "BiomeDefManager::resolveNodeNames: node '"
148 << b->nname_dust_water << "' not defined" << std::endl;
154 void BiomeDefManager::addBiome(Biome *b) {
155 if (biome_registration_finished) {
156 errorstream << "BIomeDefManager: biome registration already finished, dropping " << b->name <<std::endl;
161 size_t nbiomes = biomes.size();
162 if (nbiomes >= 0xFF) {
163 errorstream << "BiomeDefManager: too many biomes, dropping " << b->name << std::endl;
170 verbosestream << "BiomeDefManager: added biome " << b->name << std::endl;
174 Biome *BiomeDefManager::getBiome(float heat, float humidity, s16 y) {
175 Biome *b, *biome_closest = NULL;
176 float dist_min = FLT_MAX;
178 for (size_t i = 1; i < biomes.size(); i++) {
180 if (y > b->height_max || y < b->height_min)
183 float d_heat = heat - b->heat_point;
184 float d_humidity = humidity - b->humidity_point;
185 float dist = (d_heat * d_heat) +
186 (d_humidity * d_humidity);
187 if (dist < dist_min) {
193 return biome_closest ? biome_closest : biomes[0];
197 u8 BiomeDefManager::getBiomeIdByName(const char *name) {
198 for (size_t i = 0; i != biomes.size(); i++) {
199 if (!strcasecmp(name, biomes[i]->name.c_str()))
207 ///////////////////////////// Weather
210 s16 BiomeDefManager::calcBlockHeat(v3s16 p, u64 seed, float timeofday, float totaltime) {
211 //variant 1: full random
212 //f32 heat = NoisePerlin3D(np_heat, p.X, env->getGameTime()/100, p.Z, seed);
214 //variant 2: season change based on default heat map
215 const f32 offset = 20; // = np_heat->offset
216 const f32 scale = 20; // = np_heat->scale
217 const f32 range = 20;
218 f32 heat = NoisePerlin2D(np_heat, p.X, p.Z, seed); // 0..50..100
220 heat -= np_heat->offset; // -50..0..+50
222 // normalizing - todo REMOVE after fixed NoiseParams nparams_biome_def_heat = {50, 50, -> 20, 50,
224 heat /= np_heat->scale / scale; // -20..0..+20
226 f32 seasonv = totaltime;
227 seasonv /= 86400 * g_settings->getS16("year_days"); // season change speed
228 seasonv += (f32)p.X / 3000; // you can walk to area with other season
229 seasonv = sin(seasonv * M_PI);
230 heat += (range * (heat < 0 ? 2 : 0.5)) * seasonv; // -60..0..30
232 heat += offset; // -40..0..50
233 heat += p.Y / -333; // upper=colder, lower=hotter, 3c per 1000
235 // daily change, hotter at sun +4, colder at night -4
236 heat += 8 * (sin(cycle_shift(timeofday, -0.25) * M_PI) - 0.5); //-44..20..54
242 s16 BiomeDefManager::calcBlockHumidity(v3s16 p, u64 seed, float timeofday, float totaltime) {
244 f32 humidity = NoisePerlin2D(np_humidity, p.X, p.Z, seed);
246 f32 seasonv = totaltime;
247 seasonv /= 86400 * 2; // bad weather change speed (2 days)
248 seasonv += (f32)p.Z / 300;
249 humidity += 30 * sin(seasonv * M_PI);
251 humidity += -12 * (sin(cycle_shift(timeofday, -0.1) * M_PI) - 0.5);
252 humidity = rangelim(humidity, 0, 100);