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"
28 NoiseParams nparams_biome_def_heat =
29 {50, 50, v3f(500.0, 500.0, 500.0), 5349, 3, 0.70};
30 NoiseParams nparams_biome_def_humidity =
31 {50, 50, v3f(500.0, 500.0, 500.0), 842, 3, 0.55};
34 BiomeDefManager::BiomeDefManager() {
35 biome_registration_finished = false;
36 np_heat = &nparams_biome_def_heat;
37 np_humidity = &nparams_biome_def_humidity;
39 // Create default biome to be used in case none exist
50 b->nname_filler = "air";
51 b->nname_water = "mapgen_water_source";
52 b->nname_dust = "air";
53 b->nname_dust_water = "mapgen_water_source";
55 b->c_top = CONTENT_IGNORE;
56 b->c_filler = CONTENT_IGNORE;
57 b->c_water = CONTENT_IGNORE;
58 b->c_dust = CONTENT_IGNORE;
59 b->c_dust_water = CONTENT_IGNORE;
61 b->height_min = -MAP_GENERATION_LIMIT;
62 b->height_max = MAP_GENERATION_LIMIT;
64 b->humidity_point = 0.0;
70 BiomeDefManager::~BiomeDefManager() {
72 // delete[] biomecache;
74 for (size_t i = 0; i != biomes.size(); i++)
79 Biome *BiomeDefManager::createBiome(BiomeTerrainType btt) {
81 case BIOME_TERRAIN_NORMAL:
83 case BIOME_TERRAIN_LIQUID:
84 return new BiomeLiquid;
85 case BIOME_TERRAIN_NETHER:
87 case BIOME_TERRAIN_AETHER:
89 case BIOME_TERRAIN_FLAT:
90 return new BiomeSuperflat;
97 // just a PoC, obviously needs optimization later on (precalculate this)
98 void BiomeDefManager::calcBiomes(BiomeNoiseInput *input, u8 *biomeid_map) {
100 for (int y = 0; y != input->mapsize.Y; y++) {
101 for (int x = 0; x != input->mapsize.X; x++, i++) {
102 float heat = (input->heat_map[i] + 1) * 50;
103 float humidity = (input->humidity_map[i] + 1) * 50;
104 biomeid_map[i] = getBiome(heat, humidity, input->height_map[i])->id;
110 void BiomeDefManager::resolveNodeNames(INodeDefManager *ndef) {
113 biome_registration_finished = true;
115 for (size_t i = 0; i < biomes.size(); i++) {
118 b->c_top = ndef->getId(b->nname_top);
119 if (b->c_top == CONTENT_IGNORE) {
120 errorstream << "BiomeDefManager::resolveNodeNames: node '"
121 << b->nname_top << "' not defined" << std::endl;
122 b->c_top = CONTENT_AIR;
126 b->c_filler = ndef->getId(b->nname_filler);
127 if (b->c_filler == CONTENT_IGNORE) {
128 errorstream << "BiomeDefManager::resolveNodeNames: node '"
129 << b->nname_filler << "' not defined" << std::endl;
130 b->c_filler = CONTENT_AIR;
134 b->c_water = ndef->getId(b->nname_water);
135 if (b->c_water == CONTENT_IGNORE) {
136 errorstream << "BiomeDefManager::resolveNodeNames: node '"
137 << b->nname_water << "' not defined" << std::endl;
138 b->c_water = CONTENT_AIR;
141 b->c_dust = ndef->getId(b->nname_dust);
142 if (b->c_dust == CONTENT_IGNORE) {
143 errorstream << "BiomeDefManager::resolveNodeNames: node '"
144 << b->nname_dust << "' not defined" << std::endl;
147 b->c_dust_water = ndef->getId(b->nname_dust_water);
148 if (b->c_dust_water == CONTENT_IGNORE) {
149 errorstream << "BiomeDefManager::resolveNodeNames: node '"
150 << b->nname_dust_water << "' not defined" << std::endl;
156 void BiomeDefManager::addBiome(Biome *b) {
157 if (biome_registration_finished) {
158 errorstream << "BIomeDefManager: biome registration already finished, dropping " << b->name <<std::endl;
163 size_t nbiomes = biomes.size();
164 if (nbiomes >= 0xFF) {
165 errorstream << "BiomeDefManager: too many biomes, dropping " << b->name << std::endl;
172 verbosestream << "BiomeDefManager: added biome " << b->name << std::endl;
176 Biome *BiomeDefManager::getBiome(float heat, float humidity, s16 y) {
177 Biome *b, *biome_closest = NULL;
178 float dist_min = FLT_MAX;
180 for (size_t i = 1; i < biomes.size(); i++) {
182 if (y > b->height_max || y < b->height_min)
185 float d_heat = heat - b->heat_point;
186 float d_humidity = humidity - b->humidity_point;
187 float dist = (d_heat * d_heat) +
188 (d_humidity * d_humidity);
189 if (dist < dist_min) {
195 return biome_closest ? biome_closest : biomes[0];
199 u8 BiomeDefManager::getBiomeIdByName(const char *name) {
200 for (size_t i = 0; i != biomes.size(); i++) {
201 if (!strcasecmp(name, biomes[i]->name.c_str()))
209 ///////////////////////////// Weather
212 s16 BiomeDefManager::calcBlockHeat(v3s16 p, u64 seed, float timeofday, float totaltime) {
213 //variant 1: full random
214 //f32 heat = NoisePerlin3D(np_heat, p.X, env->getGameTime()/100, p.Z, seed);
216 //variant 2: season change based on default heat map
217 const f32 offset = 20; // = np_heat->offset
218 const f32 scale = 20; // = np_heat->scale
219 const f32 range = 20;
220 f32 heat = NoisePerlin2D(np_heat, p.X, p.Z, seed); // 0..50..100
222 heat -= np_heat->offset; // -50..0..+50
224 // normalizing - todo REMOVE after fixed NoiseParams nparams_biome_def_heat = {50, 50, -> 20, 50,
226 heat /= np_heat->scale / scale; // -20..0..+20
228 f32 seasonv = totaltime;
229 seasonv /= 86400 * g_settings->getS16("year_days"); // season change speed
230 seasonv += (f32)p.X / 3000; // you can walk to area with other season
231 seasonv = sin(seasonv * M_PI);
232 heat += (range * (heat < 0 ? 2 : 0.5)) * seasonv; // -60..0..30
234 heat += offset; // -40..0..50
235 heat += p.Y / -333; // upper=colder, lower=hotter, 3c per 1000
237 // daily change, hotter at sun +4, colder at night -4
238 heat += 8 * (sin(cycle_shift(timeofday, -0.25) * M_PI) - 0.5); //-44..20..54
244 s16 BiomeDefManager::calcBlockHumidity(v3s16 p, u64 seed, float timeofday, float totaltime) {
246 f32 humidity = NoisePerlin2D(np_humidity, p.X, p.Z, seed);
248 f32 seasonv = totaltime;
249 seasonv /= 86400 * 2; // bad weather change speed (2 days)
250 seasonv += (f32)p.Z / 300;
251 humidity += 30 * sin(seasonv * M_PI);
253 humidity += -12 * (sin(cycle_shift(timeofday, -0.1) * M_PI) - 0.5);
254 humidity = rangelim(humidity, 0, 100);