Huge overhaul of the entire MapgenParams system
[oweals/minetest.git] / src / biome.cpp
1 /*
2 Minetest
3 Copyright (C) 2010-2013 kwolekr, Ryan Kwolek <kwolekr@minetest.net>
4
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.
9
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.
14
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.
18 */
19
20 #include "biome.h"
21 #include "nodedef.h"
22 #include "map.h" //for ManualMapVoxelManipulator
23 #include "log.h"
24 #include "util/numeric.h"
25 #include "main.h"
26 #include "util/mathconstants.h"
27
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);
30
31
32 BiomeDefManager::BiomeDefManager() {
33         biome_registration_finished = false;
34         np_heat     = &nparams_biome_def_heat;
35         np_humidity = &nparams_biome_def_humidity;
36
37         // Create default biome to be used in case none exist
38         Biome *b = new Biome;
39         
40         b->id    = 0;
41         b->name  = "Default";
42         b->flags = 0;
43         
44         b->depth_top    = 0;
45         b->depth_filler = 0;
46
47         b->nname_top        = "air";
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";
52
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;
58
59         b->height_min     = -MAP_GENERATION_LIMIT;
60         b->height_max     = MAP_GENERATION_LIMIT;
61         b->heat_point     = 0.0;
62         b->humidity_point = 0.0;
63
64         biomes.push_back(b);
65 }
66
67
68 BiomeDefManager::~BiomeDefManager() {
69         //if (biomecache)
70         //      delete[] biomecache;
71         
72         for (size_t i = 0; i != biomes.size(); i++)
73                 delete biomes[i];
74 }
75
76
77 Biome *BiomeDefManager::createBiome(BiomeTerrainType btt) {
78         /*switch (btt) {
79                 case BIOME_TERRAIN_NORMAL:
80                         return new Biome;
81                 case BIOME_TERRAIN_LIQUID:
82                         return new BiomeLiquid;
83                 case BIOME_TERRAIN_NETHER:
84                         return new BiomeHell;
85                 case BIOME_TERRAIN_AETHER:
86                         return new BiomeSky;
87                 case BIOME_TERRAIN_FLAT:
88                         return new BiomeSuperflat;
89         }
90         return NULL;*/
91         return new Biome;
92 }
93
94
95 // just a PoC, obviously needs optimization later on (precalculate this)
96 void BiomeDefManager::calcBiomes(BiomeNoiseInput *input, u8 *biomeid_map) {
97         int i = 0;
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;
103                 }
104         }
105 }
106
107
108 void BiomeDefManager::resolveNodeNames(INodeDefManager *ndef) {
109         Biome *b;
110         
111         biome_registration_finished = true;
112         
113         for (size_t i = 0; i < biomes.size(); i++) {
114                 b = biomes[i];
115
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;
121                         b->depth_top = 0;
122                 }
123         
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;
129                         b->depth_filler = 0;
130                 }
131                 
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;
137                 }
138                 
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;
143                 }
144                 
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;
149                 }
150         }
151 }
152
153
154 void BiomeDefManager::addBiome(Biome *b) {
155         if (biome_registration_finished) {
156                 errorstream << "BIomeDefManager: biome registration already finished, dropping " << b->name <<std::endl;
157                 delete b;
158                 return;
159         }
160         
161         size_t nbiomes = biomes.size();
162         if (nbiomes >= 0xFF) {
163                 errorstream << "BiomeDefManager: too many biomes, dropping " << b->name << std::endl;
164                 delete b;
165                 return;
166         }
167
168         b->id = (u8)nbiomes;
169         biomes.push_back(b);
170         verbosestream << "BiomeDefManager: added biome " << b->name << std::endl;
171 }
172
173
174 Biome *BiomeDefManager::getBiome(float heat, float humidity, s16 y) {
175         Biome *b, *biome_closest = NULL;
176         float dist_min = FLT_MAX;
177
178         for (size_t i = 1; i < biomes.size(); i++) {
179                 b = biomes[i];
180                 if (y > b->height_max || y < b->height_min)
181                         continue;
182
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) {
188                         dist_min = dist;
189                         biome_closest = b;
190                 }
191         }
192         
193         return biome_closest ? biome_closest : biomes[0];
194 }
195
196
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()))
200                         return i;
201         }
202         
203         return 0;
204 }
205
206
207 ///////////////////////////// Weather
208
209
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);
213
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
219
220         heat -= np_heat->offset; // -50..0..+50
221
222         // normalizing - todo REMOVE after fixed NoiseParams nparams_biome_def_heat = {50, 50, -> 20, 50,
223         if (np_heat->scale)
224                 heat /= np_heat->scale / scale; //  -20..0..+20
225
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
231
232         heat += offset; // -40..0..50
233         heat += p.Y / -333; // upper=colder, lower=hotter, 3c per 1000
234
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
237         
238         return heat;
239 }
240
241
242 s16 BiomeDefManager::calcBlockHumidity(v3s16 p, u64 seed, float timeofday, float totaltime) {
243
244         f32 humidity = NoisePerlin2D(np_humidity, p.X, p.Z, seed);
245
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);
250
251         humidity += -12 * (sin(cycle_shift(timeofday, -0.1) * M_PI) - 0.5);
252         humidity = rangelim(humidity, 0, 100);
253         
254         return humidity;
255 }