3 Copyright (C) 2010-2013 kwolekr, Ryan Kwolek <kwolekr@minetest.net>
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.
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.
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.
23 #include "map.h" //for MMVManip
25 #include "util/numeric.h"
27 #include "util/mathconstants.h"
30 const char *BiomeManager::ELEMENT_TITLE = "biome";
33 ///////////////////////////////////////////////////////////////////////////////
35 BiomeManager::BiomeManager(IGameDef *gamedef) :
36 GenElementManager(gamedef)
38 // Create default biome to be used in case none exist
46 b->depth_water_top = 0;
47 b->y_min = -MAP_GENERATION_LIMIT;
48 b->y_max = MAP_GENERATION_LIMIT;
50 b->humidity_point = 0.0;
52 NodeResolveInfo *nri = new NodeResolveInfo(b);
53 nri->nodenames.push_back("air");
54 nri->nodenames.push_back("air");
55 nri->nodenames.push_back("mapgen_stone");
56 nri->nodenames.push_back("mapgen_water_source");
57 nri->nodenames.push_back("mapgen_water_source");
58 nri->nodenames.push_back("air");
59 m_ndef->pendNodeResolve(nri);
66 BiomeManager::~BiomeManager()
69 // delete[] biomecache;
74 // just a PoC, obviously needs optimization later on (precalculate this)
75 void BiomeManager::calcBiomes(s16 sx, s16 sy, float *heat_map,
76 float *humidity_map, s16 *height_map, u8 *biomeid_map)
78 for (s32 i = 0; i != sx * sy; i++)
79 biomeid_map[i] = getBiome(heat_map[i], humidity_map[i], height_map[i])->id;
83 Biome *BiomeManager::getBiome(float heat, float humidity, s16 y)
85 Biome *b, *biome_closest = NULL;
86 float dist_min = FLT_MAX;
88 for (size_t i = 1; i < m_elements.size(); i++) {
89 b = (Biome *)m_elements[i];
90 if (!b || y > b->y_max || y < b->y_min)
93 float d_heat = heat - b->heat_point;
94 float d_humidity = humidity - b->humidity_point;
95 float dist = (d_heat * d_heat) +
96 (d_humidity * d_humidity);
97 if (dist < dist_min) {
103 return biome_closest ? biome_closest : (Biome *)m_elements[0];
106 void BiomeManager::clear()
109 for (size_t i = 1; i < m_elements.size(); i++) {
110 Biome *b = (Biome *)m_elements[i];
114 m_elements.resize(1);
118 ///////////////////////////////////////////////////////////////////////////////
121 void Biome::resolveNodeNames(NodeResolveInfo *nri)
123 m_ndef->getIdFromResolveInfo(nri, "mapgen_dirt_with_grass", CONTENT_AIR, c_top);
124 m_ndef->getIdFromResolveInfo(nri, "mapgen_dirt", CONTENT_AIR, c_filler);
125 m_ndef->getIdFromResolveInfo(nri, "mapgen_stone", CONTENT_AIR, c_stone);
126 m_ndef->getIdFromResolveInfo(nri, "mapgen_water_source", CONTENT_AIR, c_water_top);
127 m_ndef->getIdFromResolveInfo(nri, "mapgen_water_source", CONTENT_AIR, c_water);
128 m_ndef->getIdFromResolveInfo(nri, "air", CONTENT_IGNORE, c_dust);