]> git.lizzy.rs Git - dragonfireclient.git/blob - src/mg_biome.cpp
a3a6d5128ec2ee571b8eb7aba96d4999d6f3ff8c
[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 "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
33 ///////////////////////////////////////////////////////////////////////////////
34
35 BiomeManager::BiomeManager(IGameDef *gamedef) :
36         GenElementManager(gamedef)
37 {
38         // Create default biome to be used in case none exist
39         Biome *b = new Biome;
40
41         b->id             = 0;
42         b->name           = "Default";
43         b->flags          = 0;
44         b->depth_top      = 0;
45         b->depth_filler   = 0;
46         b->height_min     = -MAP_GENERATION_LIMIT;
47         b->height_max     = MAP_GENERATION_LIMIT;
48         b->heat_point     = 0.0;
49         b->humidity_point = 0.0;
50
51         m_resolver->addNode("air",                 "", CONTENT_AIR, &b->c_top);
52         m_resolver->addNode("air",                 "", CONTENT_AIR, &b->c_filler);
53         m_resolver->addNode("mapgen_stone",        "", CONTENT_AIR, &b->c_stone);
54         m_resolver->addNode("mapgen_water_source", "", CONTENT_AIR, &b->c_water);
55         m_resolver->addNode("air",                 "", CONTENT_AIR, &b->c_dust);
56         m_resolver->addNode("mapgen_water_source", "", CONTENT_AIR, &b->c_dust_water);
57
58         add(b);
59 }
60
61
62
63 BiomeManager::~BiomeManager()
64 {
65         //if (biomecache)
66         //      delete[] biomecache;
67 }
68
69
70
71 // just a PoC, obviously needs optimization later on (precalculate this)
72 void BiomeManager::calcBiomes(s16 sx, s16 sy, float *heat_map,
73         float *humidity_map, s16 *height_map, u8 *biomeid_map)
74 {
75         for (s32 i = 0; i != sx * sy; i++)
76                 biomeid_map[i] = getBiome(heat_map[i], humidity_map[i], height_map[i])->id;
77 }
78
79
80 Biome *BiomeManager::getBiome(float heat, float humidity, s16 y)
81 {
82         Biome *b, *biome_closest = NULL;
83         float dist_min = FLT_MAX;
84
85         for (size_t i = 1; i < m_elements.size(); i++) {
86                 b = (Biome *)m_elements[i];
87                 if (!b || y > b->height_max || y < b->height_min)
88                         continue;
89
90                 float d_heat     = heat     - b->heat_point;
91                 float d_humidity = humidity - b->humidity_point;
92                 float dist = (d_heat * d_heat) +
93                                          (d_humidity * d_humidity);
94                 if (dist < dist_min) {
95                         dist_min = dist;
96                         biome_closest = b;
97                 }
98         }
99
100         return biome_closest ? biome_closest : (Biome *)m_elements[0];
101 }
102
103 void BiomeManager::clear()
104 {
105         for (size_t i = 1; i < m_elements.size(); i++) {
106                 Biome *b = (Biome *)m_elements[i];
107                 if (!b)
108                         continue;
109
110                 m_resolver->cancelNode(&b->c_top);
111                 m_resolver->cancelNode(&b->c_filler);
112                 m_resolver->cancelNode(&b->c_stone);
113                 m_resolver->cancelNode(&b->c_water);
114                 m_resolver->cancelNode(&b->c_dust);
115                 m_resolver->cancelNode(&b->c_dust_water);
116         }
117         m_elements.resize(1);
118 }
119