b1172a09d09f1c299bbb1336a6f48b8302cb4839
[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 "nodedef.h"
22 #include "map.h" //for ManualMapVoxelManipulator
23 #include "log.h"
24 #include "util/numeric.h"
25 #include "main.h"
26 #include "util/mathconstants.h"
27 #include "porting.h"
28
29 NoiseParams nparams_biome_def_heat(50, 50, v3f(500.0, 500.0, 500.0), 5349, 3, 0.70);
30 NoiseParams nparams_biome_def_humidity(50, 50, v3f(500.0, 500.0, 500.0), 842, 3, 0.55);
31
32
33 BiomeDefManager::BiomeDefManager(NodeResolver *resolver)
34 {
35         biome_registration_finished = false;
36         np_heat     = &nparams_biome_def_heat;
37         np_humidity = &nparams_biome_def_humidity;
38
39         // Create default biome to be used in case none exist
40         Biome *b = new Biome;
41         
42         b->id             = 0;
43         b->name           = "Default";
44         b->flags          = 0;
45         b->depth_top      = 0;
46         b->depth_filler   = 0;
47         b->height_min     = -MAP_GENERATION_LIMIT;
48         b->height_max     = MAP_GENERATION_LIMIT;
49         b->heat_point     = 0.0;
50         b->humidity_point = 0.0;
51
52         resolver->addNode("air",                 "", CONTENT_AIR, &b->c_top);
53         resolver->addNode("air",                 "", CONTENT_AIR, &b->c_filler);
54         resolver->addNode("mapgen_water_source", "", CONTENT_AIR, &b->c_water);
55         resolver->addNode("air",                 "", CONTENT_AIR, &b->c_dust);
56         resolver->addNode("mapgen_water_source", "", CONTENT_AIR, &b->c_dust_water);
57
58         biomes.push_back(b);
59 }
60
61
62 BiomeDefManager::~BiomeDefManager()
63 {
64         //if (biomecache)
65         //      delete[] biomecache;
66         
67         for (size_t i = 0; i != biomes.size(); i++)
68                 delete biomes[i];
69 }
70
71
72 Biome *BiomeDefManager::createBiome(BiomeTerrainType btt)
73 {
74         /*switch (btt) {
75                 case BIOME_TERRAIN_NORMAL:
76                         return new Biome;
77                 case BIOME_TERRAIN_LIQUID:
78                         return new BiomeLiquid;
79                 case BIOME_TERRAIN_NETHER:
80                         return new BiomeHell;
81                 case BIOME_TERRAIN_AETHER:
82                         return new BiomeSky;
83                 case BIOME_TERRAIN_FLAT:
84                         return new BiomeSuperflat;
85         }
86         return NULL;*/
87         return new Biome;
88 }
89
90
91 // just a PoC, obviously needs optimization later on (precalculate this)
92 void BiomeDefManager::calcBiomes(BiomeNoiseInput *input, u8 *biomeid_map)
93 {
94         int i = 0;
95         for (int y = 0; y != input->mapsize.Y; y++) {
96                 for (int x = 0; x != input->mapsize.X; x++, i++) {
97                         float heat     = (input->heat_map[i] + 1) * 50;
98                         float humidity = (input->humidity_map[i] + 1) * 50;
99                         biomeid_map[i] = getBiome(heat, humidity, input->height_map[i])->id;
100                 }
101         }
102 }
103
104
105 bool BiomeDefManager::addBiome(Biome *b)
106 {
107         if (biome_registration_finished) {
108                 errorstream << "BiomeDefManager: biome registration already "
109                         "finished, dropping " << b->name << std::endl;
110                 return false;
111         }
112         
113         size_t nbiomes = biomes.size();
114         if (nbiomes >= 0xFF) {
115                 errorstream << "BiomeDefManager: too many biomes, dropping "
116                         << b->name << std::endl;
117                 return false;
118         }
119
120         b->id = (u8)nbiomes;
121         biomes.push_back(b);
122         verbosestream << "BiomeDefManager: added biome " << b->name << std::endl;
123
124         return true;
125 }
126
127
128 Biome *BiomeDefManager::getBiome(float heat, float humidity, s16 y)
129 {
130         Biome *b, *biome_closest = NULL;
131         float dist_min = FLT_MAX;
132
133         for (size_t i = 1; i < biomes.size(); i++) {
134                 b = biomes[i];
135                 if (y > b->height_max || y < b->height_min)
136                         continue;
137
138                 float d_heat     = heat     - b->heat_point;
139                 float d_humidity = humidity - b->humidity_point;
140                 float dist = (d_heat * d_heat) +
141                                          (d_humidity * d_humidity);
142                 if (dist < dist_min) {
143                         dist_min = dist;
144                         biome_closest = b;
145                 }
146         }
147         
148         return biome_closest ? biome_closest : biomes[0];
149 }
150
151
152 u8 BiomeDefManager::getBiomeIdByName(const char *name)
153 {
154         for (size_t i = 0; i != biomes.size(); i++) {
155                 if (!strcasecmp(name, biomes[i]->name.c_str()))
156                         return i;
157         }
158         
159         return 0;
160 }