]> git.lizzy.rs Git - minetest.git/blob - src/mg_biome.cpp
Move globals from main.cpp to more sane locations
[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 MMVManip
24 #include "log.h"
25 #include "util/numeric.h"
26 #include "util/mathconstants.h"
27 #include "porting.h"
28
29
30 ///////////////////////////////////////////////////////////////////////////////
31
32
33 BiomeManager::BiomeManager(IGameDef *gamedef) :
34         ObjDefManager(gamedef, OBJDEF_BIOME)
35 {
36         // Create default biome to be used in case none exist
37         Biome *b = new Biome;
38
39         b->name            = "Default";
40         b->flags           = 0;
41         b->depth_top       = 0;
42         b->depth_filler    = 0;
43         b->depth_water_top = 0;
44         b->y_min           = -MAP_GENERATION_LIMIT;
45         b->y_max           = MAP_GENERATION_LIMIT;
46         b->heat_point      = 0.0;
47         b->humidity_point  = 0.0;
48
49         NodeResolveInfo *nri = new NodeResolveInfo(b);
50         nri->nodenames.push_back("air");
51         nri->nodenames.push_back("air");
52         nri->nodenames.push_back("mapgen_stone");
53         nri->nodenames.push_back("mapgen_water_source");
54         nri->nodenames.push_back("mapgen_water_source");
55         nri->nodenames.push_back("air");
56         m_ndef->pendNodeResolve(nri);
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                 Biome *biome = getBiome(heat_map[i], humidity_map[i], height_map[i]);
77                 biomeid_map[i] = biome->index;
78         }
79 }
80
81
82 Biome *BiomeManager::getBiome(float heat, float humidity, s16 y)
83 {
84         Biome *b, *biome_closest = NULL;
85         float dist_min = FLT_MAX;
86
87         for (size_t i = 1; i < m_objects.size(); i++) {
88                 b = (Biome *)m_objects[i];
89                 if (!b || y > b->y_max || y < b->y_min)
90                         continue;
91
92                 float d_heat     = heat     - b->heat_point;
93                 float d_humidity = humidity - b->humidity_point;
94                 float dist = (d_heat * d_heat) +
95                                          (d_humidity * d_humidity);
96                 if (dist < dist_min) {
97                         dist_min = dist;
98                         biome_closest = b;
99                 }
100         }
101
102         return biome_closest ? biome_closest : (Biome *)m_objects[0];
103 }
104
105 void BiomeManager::clear()
106 {
107
108         for (size_t i = 1; i < m_objects.size(); i++) {
109                 Biome *b = (Biome *)m_objects[i];
110                 delete b;
111         }
112
113         m_objects.resize(1);
114 }
115
116
117 ///////////////////////////////////////////////////////////////////////////////
118
119
120 void Biome::resolveNodeNames(NodeResolveInfo *nri)
121 {
122         m_ndef->getIdFromResolveInfo(nri, "mapgen_dirt_with_grass", CONTENT_AIR,    c_top);
123         m_ndef->getIdFromResolveInfo(nri, "mapgen_dirt",            CONTENT_AIR,    c_filler);
124         m_ndef->getIdFromResolveInfo(nri, "mapgen_stone",           CONTENT_AIR,    c_stone);
125         m_ndef->getIdFromResolveInfo(nri, "mapgen_water_source",    CONTENT_AIR,    c_water_top);
126         m_ndef->getIdFromResolveInfo(nri, "mapgen_water_source",    CONTENT_AIR,    c_water);
127         m_ndef->getIdFromResolveInfo(nri, "air",                    CONTENT_IGNORE, c_dust);
128 }
129