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.
21 #include "mg_decoration.h"
25 #include "map.h" //for MMVManip
27 #include "util/numeric.h"
28 #include "util/mathconstants.h"
32 ///////////////////////////////////////////////////////////////////////////////
35 BiomeManager::BiomeManager(IGameDef *gamedef) :
36 ObjDefManager(gamedef, OBJDEF_BIOME)
40 // Create default biome to be used in case none exist
46 b->depth_filler = -MAX_MAP_GENERATION_LIMIT;
47 b->depth_water_top = 0;
48 b->y_min = -MAX_MAP_GENERATION_LIMIT;
49 b->y_max = MAX_MAP_GENERATION_LIMIT;
51 b->humidity_point = 0.0;
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("air");
60 m_ndef->pendNodeResolve(b);
67 BiomeManager::~BiomeManager()
70 // delete[] biomecache;
75 // just a PoC, obviously needs optimization later on (precalculate this)
76 void BiomeManager::calcBiomes(s16 sx, s16 sy, float *heat_map,
77 float *humidity_map, s16 *height_map, u8 *biomeid_map)
79 for (s32 i = 0; i != sx * sy; i++) {
80 Biome *biome = getBiome(heat_map[i], humidity_map[i], height_map[i]);
81 biomeid_map[i] = biome->index;
86 Biome *BiomeManager::getBiome(float heat, float humidity, s16 y)
88 Biome *b, *biome_closest = NULL;
89 float dist_min = FLT_MAX;
91 for (size_t i = 1; i < m_objects.size(); i++) {
92 b = (Biome *)m_objects[i];
93 if (!b || y > b->y_max || y < b->y_min)
96 float d_heat = heat - b->heat_point;
97 float d_humidity = humidity - b->humidity_point;
98 float dist = (d_heat * d_heat) +
99 (d_humidity * d_humidity);
100 if (dist < dist_min) {
106 return biome_closest ? biome_closest : (Biome *)m_objects[0];
109 void BiomeManager::clear()
111 EmergeManager *emerge = m_gamedef->getEmergeManager();
113 // Remove all dangling references in Decorations
114 DecorationManager *decomgr = emerge->decomgr;
115 for (size_t i = 0; i != decomgr->getNumObjects(); i++) {
116 Decoration *deco = (Decoration *)decomgr->getRaw(i);
117 deco->biomes.clear();
120 // Don't delete the first biome
121 for (size_t i = 1; i < m_objects.size(); i++) {
122 Biome *b = (Biome *)m_objects[i];
130 ///////////////////////////////////////////////////////////////////////////////
133 void Biome::resolveNodeNames()
135 getIdFromNrBacklog(&c_top, "mapgen_stone", CONTENT_AIR);
136 getIdFromNrBacklog(&c_filler, "mapgen_stone", CONTENT_AIR);
137 getIdFromNrBacklog(&c_stone, "mapgen_stone", CONTENT_AIR);
138 getIdFromNrBacklog(&c_water_top, "mapgen_water_source", CONTENT_AIR);
139 getIdFromNrBacklog(&c_water, "mapgen_water_source", CONTENT_AIR);
140 getIdFromNrBacklog(&c_river_water, "mapgen_river_water_source", CONTENT_AIR);
141 getIdFromNrBacklog(&c_dust, "air", CONTENT_IGNORE);