Line data Source code
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 "mg_decoration.h"
22 : #include "emerge.h"
23 : #include "gamedef.h"
24 : #include "nodedef.h"
25 : #include "map.h" //for MMVManip
26 : #include "log.h"
27 : #include "util/numeric.h"
28 : #include "util/mathconstants.h"
29 : #include "porting.h"
30 :
31 :
32 : ///////////////////////////////////////////////////////////////////////////////
33 :
34 :
35 0 : BiomeManager::BiomeManager(IGameDef *gamedef) :
36 0 : ObjDefManager(gamedef, OBJDEF_BIOME)
37 : {
38 0 : m_gamedef = gamedef;
39 :
40 : // Create default biome to be used in case none exist
41 0 : Biome *b = new Biome;
42 :
43 0 : b->name = "Default";
44 0 : b->flags = 0;
45 0 : b->depth_top = 0;
46 0 : b->depth_filler = 0;
47 0 : b->depth_water_top = 0;
48 0 : b->y_min = -MAP_GENERATION_LIMIT;
49 0 : b->y_max = MAP_GENERATION_LIMIT;
50 0 : b->heat_point = 0.0;
51 0 : b->humidity_point = 0.0;
52 :
53 0 : b->m_nodenames.push_back("air");
54 0 : b->m_nodenames.push_back("air");
55 0 : b->m_nodenames.push_back("mapgen_stone");
56 0 : b->m_nodenames.push_back("mapgen_water_source");
57 0 : b->m_nodenames.push_back("mapgen_water_source");
58 0 : b->m_nodenames.push_back("mapgen_river_water_source");
59 0 : b->m_nodenames.push_back("air");
60 0 : m_ndef->pendNodeResolve(b);
61 :
62 0 : add(b);
63 0 : }
64 :
65 :
66 :
67 0 : BiomeManager::~BiomeManager()
68 : {
69 : //if (biomecache)
70 : // delete[] biomecache;
71 0 : }
72 :
73 :
74 :
75 : // just a PoC, obviously needs optimization later on (precalculate this)
76 0 : void BiomeManager::calcBiomes(s16 sx, s16 sy, float *heat_map,
77 : float *humidity_map, s16 *height_map, u8 *biomeid_map)
78 : {
79 0 : for (s32 i = 0; i != sx * sy; i++) {
80 0 : Biome *biome = getBiome(heat_map[i], humidity_map[i], height_map[i]);
81 0 : biomeid_map[i] = biome->index;
82 : }
83 0 : }
84 :
85 :
86 0 : Biome *BiomeManager::getBiome(float heat, float humidity, s16 y)
87 : {
88 0 : Biome *b, *biome_closest = NULL;
89 0 : float dist_min = FLT_MAX;
90 :
91 0 : for (size_t i = 1; i < m_objects.size(); i++) {
92 0 : b = (Biome *)m_objects[i];
93 0 : if (!b || y > b->y_max || y < b->y_min)
94 0 : continue;
95 :
96 0 : float d_heat = heat - b->heat_point;
97 0 : float d_humidity = humidity - b->humidity_point;
98 0 : float dist = (d_heat * d_heat) +
99 0 : (d_humidity * d_humidity);
100 0 : if (dist < dist_min) {
101 0 : dist_min = dist;
102 0 : biome_closest = b;
103 : }
104 : }
105 :
106 0 : return biome_closest ? biome_closest : (Biome *)m_objects[0];
107 : }
108 :
109 0 : void BiomeManager::clear()
110 : {
111 0 : EmergeManager *emerge = m_gamedef->getEmergeManager();
112 :
113 : // Remove all dangling references in Decorations
114 0 : DecorationManager *decomgr = emerge->decomgr;
115 0 : for (size_t i = 0; i != decomgr->getNumObjects(); i++) {
116 0 : Decoration *deco = (Decoration *)decomgr->getRaw(i);
117 0 : deco->biomes.clear();
118 : }
119 :
120 : // Don't delete the first biome
121 0 : for (size_t i = 1; i < m_objects.size(); i++) {
122 0 : Biome *b = (Biome *)m_objects[i];
123 0 : delete b;
124 : }
125 :
126 0 : m_objects.resize(1);
127 0 : }
128 :
129 :
130 : ///////////////////////////////////////////////////////////////////////////////
131 :
132 :
133 0 : void Biome::resolveNodeNames()
134 : {
135 0 : getIdFromNrBacklog(&c_top, "mapgen_dirt_with_grass", CONTENT_AIR);
136 0 : getIdFromNrBacklog(&c_filler, "mapgen_dirt", CONTENT_AIR);
137 0 : getIdFromNrBacklog(&c_stone, "mapgen_stone", CONTENT_AIR);
138 0 : getIdFromNrBacklog(&c_water_top, "mapgen_water_source", CONTENT_AIR);
139 0 : getIdFromNrBacklog(&c_water, "mapgen_water_source", CONTENT_AIR);
140 0 : getIdFromNrBacklog(&c_river_water, "mapgen_river_water_source", CONTENT_AIR);
141 0 : getIdFromNrBacklog(&c_dust, "air", CONTENT_IGNORE);
142 3 : }
143 :
|