]> git.lizzy.rs Git - minetest.git/blob - src/mg_biome.cpp
91b7290d82422f48d5fb528dfa1250a355661d25
[minetest.git] / src / mg_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 "mg_biome.h"
21 #include "gamedef.h"
22 #include "nodedef.h"
23 #include "map.h" //for ManualMapVoxelManipulator
24 #include "log.h"
25 #include "util/numeric.h"
26 #include "main.h"
27 #include "util/mathconstants.h"
28 #include "porting.h"
29
30 const char *BiomeManager::ELEMENT_TITLE = "biome";
31
32 NoiseParams nparams_biome_def_heat(50, 50, v3f(500.0, 500.0, 500.0), 5349, 3, 0.70);
33 NoiseParams nparams_biome_def_humidity(50, 50, v3f(500.0, 500.0, 500.0), 842, 3, 0.55);
34
35
36 ///////////////////////////////////////////////////////////////////////////////
37
38 BiomeManager::BiomeManager(IGameDef *gamedef)
39 {
40         m_resolver  = gamedef->getNodeDefManager()->getResolver();
41         np_heat     = &nparams_biome_def_heat;
42         np_humidity = &nparams_biome_def_humidity;
43
44         // Create default biome to be used in case none exist
45         Biome *b = new Biome;
46
47         b->id             = 0;
48         b->name           = "Default";
49         b->flags          = 0;
50         b->depth_top      = 0;
51         b->depth_filler   = 0;
52         b->height_min     = -MAP_GENERATION_LIMIT;
53         b->height_max     = MAP_GENERATION_LIMIT;
54         b->heat_point     = 0.0;
55         b->humidity_point = 0.0;
56
57         m_resolver->addNode("air",                 "", CONTENT_AIR, &b->c_top);
58         m_resolver->addNode("air",                 "", CONTENT_AIR, &b->c_filler);
59         m_resolver->addNode("mapgen_stone",        "", CONTENT_AIR, &b->c_stone);
60         m_resolver->addNode("mapgen_water_source", "", CONTENT_AIR, &b->c_water);
61         m_resolver->addNode("air",                 "", CONTENT_AIR, &b->c_dust);
62         m_resolver->addNode("mapgen_water_source", "", CONTENT_AIR, &b->c_dust_water);
63
64         add(b);
65 }
66
67
68
69 BiomeManager::~BiomeManager()
70 {
71         //if (biomecache)
72         //      delete[] biomecache;
73 }
74
75
76
77 // just a PoC, obviously needs optimization later on (precalculate this)
78 void BiomeManager::calcBiomes(s16 sx, s16 sy, float *heat_map,
79         float *humidity_map, s16 *height_map, u8 *biomeid_map)
80 {
81         int i = 0;
82         for (int y = 0; y != sy; y++) {
83                 for (int x = 0; x != sx; x++, i++) {
84                         float heat     = (heat_map[i] + 1) * 50;
85                         float humidity = (humidity_map[i] + 1) * 50;
86                         biomeid_map[i] = getBiome(heat, humidity, height_map[i])->id;
87                 }
88         }
89 }
90
91
92 Biome *BiomeManager::getBiome(float heat, float humidity, s16 y)
93 {
94         Biome *b, *biome_closest = NULL;
95         float dist_min = FLT_MAX;
96
97         for (size_t i = 1; i < m_elements.size(); i++) {
98                 b = (Biome *)m_elements[i];
99                 if (!b || y > b->height_max || y < b->height_min)
100                         continue;
101
102                 float d_heat     = heat     - b->heat_point;
103                 float d_humidity = humidity - b->humidity_point;
104                 float dist = (d_heat * d_heat) +
105                                          (d_humidity * d_humidity);
106                 if (dist < dist_min) {
107                         dist_min = dist;
108                         biome_closest = b;
109                 }
110         }
111
112         return biome_closest ? biome_closest : (Biome *)m_elements[0];
113 }
114
115 void BiomeManager::clear()
116 {
117         for (size_t i = 1; i < m_elements.size(); i++) {
118                 Biome *b = (Biome *)m_elements[i];
119                 if (!b)
120                         continue;
121
122                 m_resolver->cancelNode(&b->c_top);
123                 m_resolver->cancelNode(&b->c_filler);
124                 m_resolver->cancelNode(&b->c_stone);
125                 m_resolver->cancelNode(&b->c_water);
126                 m_resolver->cancelNode(&b->c_dust);
127                 m_resolver->cancelNode(&b->c_dust_water);
128         }
129         m_elements.resize(1);
130 }
131