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