]> git.lizzy.rs Git - dragonfireclient.git/blob - src/mg_biome.cpp
Document zoom_fov in settingtypes.txt and minetest.conf.example
[dragonfireclient.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 "mg_decoration.h"
22 #include "emerge.h"
23 #include "gamedef.h"
24 #include "nodedef.h"
25 #include "map.h" //for MMVManip
26 #include "log.h"
27 #include "util/numeric.h"
28 #include "util/mathconstants.h"
29 #include "porting.h"
30 #include "settings.h"
31
32
33 ///////////////////////////////////////////////////////////////////////////////
34
35
36 BiomeManager::BiomeManager(IGameDef *gamedef) :
37         ObjDefManager(gamedef, OBJDEF_BIOME)
38 {
39         m_gamedef = gamedef;
40
41         // Create default biome to be used in case none exist
42         Biome *b = new Biome;
43
44         b->name            = "Default";
45         b->flags           = 0;
46         b->depth_top       = 0;
47         b->depth_filler    = -MAX_MAP_GENERATION_LIMIT;
48         b->depth_water_top = 0;
49         b->depth_riverbed  = 0;
50         b->y_min           = -MAX_MAP_GENERATION_LIMIT;
51         b->y_max           = MAX_MAP_GENERATION_LIMIT;
52         b->heat_point      = 0.0;
53         b->humidity_point  = 0.0;
54
55         b->m_nodenames.push_back("mapgen_stone");
56         b->m_nodenames.push_back("mapgen_stone");
57         b->m_nodenames.push_back("mapgen_stone");
58         b->m_nodenames.push_back("mapgen_water_source");
59         b->m_nodenames.push_back("mapgen_water_source");
60         b->m_nodenames.push_back("mapgen_river_water_source");
61         b->m_nodenames.push_back("mapgen_stone");
62         b->m_nodenames.push_back("ignore");
63         m_ndef->pendNodeResolve(b);
64
65         add(b);
66 }
67
68
69 BiomeManager::~BiomeManager()
70 {
71 }
72
73
74 void BiomeManager::clear()
75 {
76         EmergeManager *emerge = m_gamedef->getEmergeManager();
77
78         // Remove all dangling references in Decorations
79         DecorationManager *decomgr = emerge->decomgr;
80         for (size_t i = 0; i != decomgr->getNumObjects(); i++) {
81                 Decoration *deco = (Decoration *)decomgr->getRaw(i);
82                 deco->biomes.clear();
83         }
84
85         // Don't delete the first biome
86         for (size_t i = 1; i < m_objects.size(); i++)
87                 delete (Biome *)m_objects[i];
88
89         m_objects.resize(1);
90 }
91
92 ////////////////////////////////////////////////////////////////////////////////
93
94
95 void BiomeParamsOriginal::readParams(const Settings *settings)
96 {
97         settings->getNoiseParams("mg_biome_np_heat",           np_heat);
98         settings->getNoiseParams("mg_biome_np_heat_blend",     np_heat_blend);
99         settings->getNoiseParams("mg_biome_np_humidity",       np_humidity);
100         settings->getNoiseParams("mg_biome_np_humidity_blend", np_humidity_blend);
101 }
102
103
104 void BiomeParamsOriginal::writeParams(Settings *settings) const
105 {
106         settings->setNoiseParams("mg_biome_np_heat",           np_heat);
107         settings->setNoiseParams("mg_biome_np_heat_blend",     np_heat_blend);
108         settings->setNoiseParams("mg_biome_np_humidity",       np_humidity);
109         settings->setNoiseParams("mg_biome_np_humidity_blend", np_humidity_blend);
110 }
111
112
113 ////////////////////////////////////////////////////////////////////////////////
114
115 BiomeGenOriginal::BiomeGenOriginal(BiomeManager *biomemgr,
116         BiomeParamsOriginal *params, v3s16 chunksize)
117 {
118         m_bmgr   = biomemgr;
119         m_params = params;
120         m_csize  = chunksize;
121
122         noise_heat           = new Noise(&params->np_heat,
123                                                                         params->seed, m_csize.X, m_csize.Z);
124         noise_humidity       = new Noise(&params->np_humidity,
125                                                                         params->seed, m_csize.X, m_csize.Z);
126         noise_heat_blend     = new Noise(&params->np_heat_blend,
127                                                                         params->seed, m_csize.X, m_csize.Z);
128         noise_humidity_blend = new Noise(&params->np_humidity_blend,
129                                                                         params->seed, m_csize.X, m_csize.Z);
130
131         heatmap  = noise_heat->result;
132         humidmap = noise_humidity->result;
133         biomemap = new biome_t[m_csize.X * m_csize.Z];
134 }
135
136 BiomeGenOriginal::~BiomeGenOriginal()
137 {
138         delete []biomemap;
139
140         delete noise_heat;
141         delete noise_humidity;
142         delete noise_heat_blend;
143         delete noise_humidity_blend;
144 }
145
146
147 Biome *BiomeGenOriginal::calcBiomeAtPoint(v3s16 pos) const
148 {
149         float heat =
150                 NoisePerlin2D(&m_params->np_heat,       pos.X, pos.Z, m_params->seed) +
151                 NoisePerlin2D(&m_params->np_heat_blend, pos.X, pos.Z, m_params->seed);
152         float humidity =
153                 NoisePerlin2D(&m_params->np_humidity,       pos.X, pos.Z, m_params->seed) +
154                 NoisePerlin2D(&m_params->np_humidity_blend, pos.X, pos.Z, m_params->seed);
155
156         return calcBiomeFromNoise(heat, humidity, pos.Y);
157 }
158
159
160 void BiomeGenOriginal::calcBiomeNoise(v3s16 pmin)
161 {
162         m_pmin = pmin;
163
164         noise_heat->perlinMap2D(pmin.X, pmin.Z);
165         noise_humidity->perlinMap2D(pmin.X, pmin.Z);
166         noise_heat_blend->perlinMap2D(pmin.X, pmin.Z);
167         noise_humidity_blend->perlinMap2D(pmin.X, pmin.Z);
168
169         for (s32 i = 0; i < m_csize.X * m_csize.Z; i++) {
170                 noise_heat->result[i]     += noise_heat_blend->result[i];
171                 noise_humidity->result[i] += noise_humidity_blend->result[i];
172         }
173 }
174
175
176 biome_t *BiomeGenOriginal::getBiomes(s16 *heightmap)
177 {
178         for (s32 i = 0; i != m_csize.X * m_csize.Z; i++) {
179                 Biome *biome = calcBiomeFromNoise(
180                         noise_heat->result[i],
181                         noise_humidity->result[i],
182                         heightmap[i]);
183
184                 biomemap[i] = biome->index;
185         }
186
187         return biomemap;
188 }
189
190
191 Biome *BiomeGenOriginal::getBiomeAtPoint(v3s16 pos) const
192 {
193         return getBiomeAtIndex(
194                 (pos.Z - m_pmin.Z) * m_csize.X + (pos.X - m_pmin.X),
195                 pos.Y);
196 }
197
198
199 Biome *BiomeGenOriginal::getBiomeAtIndex(size_t index, s16 y) const
200 {
201         return calcBiomeFromNoise(
202                 noise_heat->result[index],
203                 noise_humidity->result[index],
204                 y);
205 }
206
207
208 Biome *BiomeGenOriginal::calcBiomeFromNoise(float heat, float humidity, s16 y) const
209 {
210         Biome *b, *biome_closest = NULL;
211         float dist_min = FLT_MAX;
212
213         for (size_t i = 1; i < m_bmgr->getNumObjects(); i++) {
214                 b = (Biome *)m_bmgr->getRaw(i);
215                 if (!b || y > b->y_max || y < b->y_min)
216                         continue;
217
218                 float d_heat     = heat     - b->heat_point;
219                 float d_humidity = humidity - b->humidity_point;
220                 float dist = (d_heat * d_heat) +
221                                          (d_humidity * d_humidity);
222                 if (dist < dist_min) {
223                         dist_min = dist;
224                         biome_closest = b;
225                 }
226         }
227
228         return biome_closest ? biome_closest : (Biome *)m_bmgr->getRaw(BIOME_NONE);
229 }
230
231
232 ////////////////////////////////////////////////////////////////////////////////
233
234 void Biome::resolveNodeNames()
235 {
236         getIdFromNrBacklog(&c_top,         "mapgen_stone",              CONTENT_AIR);
237         getIdFromNrBacklog(&c_filler,      "mapgen_stone",              CONTENT_AIR);
238         getIdFromNrBacklog(&c_stone,       "mapgen_stone",              CONTENT_AIR);
239         getIdFromNrBacklog(&c_water_top,   "mapgen_water_source",       CONTENT_AIR);
240         getIdFromNrBacklog(&c_water,       "mapgen_water_source",       CONTENT_AIR);
241         getIdFromNrBacklog(&c_river_water, "mapgen_river_water_source", CONTENT_AIR);
242         getIdFromNrBacklog(&c_riverbed,    "mapgen_stone",              CONTENT_AIR);
243         getIdFromNrBacklog(&c_dust,        "ignore",                    CONTENT_IGNORE);
244 }