Shorten ManualMapVoxelManipulator to MMVManip
[oweals/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 "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_shore    = 0;
47         b->depth_water_top = 0;
48         b->y_min           = -MAP_GENERATION_LIMIT;
49         b->y_max           = MAP_GENERATION_LIMIT;
50         b->heat_point      = 0.0;
51         b->humidity_point  = 0.0;
52
53         NodeResolveInfo *nri = new NodeResolveInfo(b);
54         nri->nodenames.push_back("air");
55         nri->nodenames.push_back("air");
56         nri->nodenames.push_back("air");
57         nri->nodenames.push_back("air");
58         nri->nodenames.push_back("air");
59         nri->nodenames.push_back("mapgen_stone");
60         nri->nodenames.push_back("mapgen_water_source");
61         nri->nodenames.push_back("mapgen_water_source");
62         nri->nodenames.push_back("air");
63         m_ndef->pendNodeResolve(nri);
64
65         add(b);
66 }
67
68
69
70 BiomeManager::~BiomeManager()
71 {
72         //if (biomecache)
73         //      delete[] biomecache;
74 }
75
76
77
78 // just a PoC, obviously needs optimization later on (precalculate this)
79 void BiomeManager::calcBiomes(s16 sx, s16 sy, float *heat_map,
80         float *humidity_map, s16 *height_map, u8 *biomeid_map)
81 {
82         for (s32 i = 0; i != sx * sy; i++)
83                 biomeid_map[i] = getBiome(heat_map[i], humidity_map[i], height_map[i])->id;
84 }
85
86
87 Biome *BiomeManager::getBiome(float heat, float humidity, s16 y)
88 {
89         Biome *b, *biome_closest = NULL;
90         float dist_min = FLT_MAX;
91
92         for (size_t i = 1; i < m_elements.size(); i++) {
93                 b = (Biome *)m_elements[i];
94                 if (!b || y > b->y_max || y < b->y_min)
95                         continue;
96
97                 float d_heat     = heat     - b->heat_point;
98                 float d_humidity = humidity - b->humidity_point;
99                 float dist = (d_heat * d_heat) +
100                                          (d_humidity * d_humidity);
101                 if (dist < dist_min) {
102                         dist_min = dist;
103                         biome_closest = b;
104                 }
105         }
106
107         return biome_closest ? biome_closest : (Biome *)m_elements[0];
108 }
109
110 void BiomeManager::clear()
111 {
112
113         for (size_t i = 1; i < m_elements.size(); i++) {
114                 Biome *b = (Biome *)m_elements[i];
115                 delete b;
116         }
117
118         m_elements.resize(1);
119 }
120
121
122 ///////////////////////////////////////////////////////////////////////////////
123
124
125 void Biome::resolveNodeNames(NodeResolveInfo *nri)
126 {
127         m_ndef->getIdFromResolveInfo(nri, "mapgen_dirt_with_grass", CONTENT_AIR,    c_top);
128         m_ndef->getIdFromResolveInfo(nri, "mapgen_dirt",            CONTENT_AIR,    c_filler);
129         m_ndef->getIdFromResolveInfo(nri, "mapgen_sand",            CONTENT_AIR,    c_shore_top);
130         m_ndef->getIdFromResolveInfo(nri, "mapgen_sand",            CONTENT_AIR,    c_shore_filler);
131         m_ndef->getIdFromResolveInfo(nri, "mapgen_sand",            CONTENT_AIR,    c_underwater);
132         m_ndef->getIdFromResolveInfo(nri, "mapgen_stone",           CONTENT_AIR,    c_stone);
133         m_ndef->getIdFromResolveInfo(nri, "mapgen_water_source",    CONTENT_AIR,    c_water_top);
134         m_ndef->getIdFromResolveInfo(nri, "mapgen_water_source",    CONTENT_AIR,    c_water);
135         m_ndef->getIdFromResolveInfo(nri, "air",                    CONTENT_IGNORE, c_dust);
136 }
137