Mapgen V7: Huge rewrite, also tweaks to cavegen et al.
[oweals/minetest.git] / src / 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 "biome.h"
21 #include "nodedef.h"
22 #include "map.h" //for ManualMapVoxelManipulator
23 #include "log.h"
24 #include "main.h"
25
26
27 NoiseParams nparams_biome_def_heat =
28         {50, 50, v3f(500.0, 500.0, 500.0), 5349, 3, 0.70};
29 NoiseParams nparams_biome_def_humidity =
30         {50, 50, v3f(500.0, 500.0, 500.0), 842, 3, 0.55};
31
32
33 BiomeDefManager::BiomeDefManager() {
34         biome_registration_finished = false;
35         np_heat     = &nparams_biome_def_heat;
36         np_humidity = &nparams_biome_def_humidity;
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
45         b->c_top         = CONTENT_AIR;
46         b->depth_top     = 0;
47         b->c_filler      = CONTENT_AIR;
48         b->depth_filler  = 0;
49
50         b->height_min     = -MAP_GENERATION_LIMIT;
51         b->height_max     = MAP_GENERATION_LIMIT;
52         b->heat_point     = 0.0;
53         b->humidity_point = 0.0;
54
55         biomes.push_back(b);
56 }
57
58
59 BiomeDefManager::~BiomeDefManager() {
60         //if (biomecache)
61         //      delete[] biomecache;
62         
63         for (size_t i = 0; i != biomes.size(); i++)
64                 delete biomes[i];
65 }
66
67
68 Biome *BiomeDefManager::createBiome(BiomeTerrainType btt) {
69         /*switch (btt) {
70                 case BIOME_TERRAIN_NORMAL:
71                         return new Biome;
72                 case BIOME_TERRAIN_LIQUID:
73                         return new BiomeLiquid;
74                 case BIOME_TERRAIN_NETHER:
75                         return new BiomeHell;
76                 case BIOME_TERRAIN_AETHER:
77                         return new BiomeSky;
78                 case BIOME_TERRAIN_FLAT:
79                         return new BiomeSuperflat;
80         }
81         return NULL;*/
82         return new Biome;
83 }
84
85
86 // just a PoC, obviously needs optimization later on (precalculate this)
87 void BiomeDefManager::calcBiomes(BiomeNoiseInput *input, u8 *biomeid_map) {
88         int i = 0;
89         for (int y = 0; y != input->mapsize.Y; y++) {
90                 for (int x = 0; x != input->mapsize.X; x++, i++) {
91                         float heat     = (input->heat_map[i] + 1) * 50;
92                         float humidity = (input->humidity_map[i] + 1) * 50;
93                         biomeid_map[i] = getBiome(heat, humidity, input->height_map[i])->id;
94                 }
95         }
96 }
97
98
99 void BiomeDefManager::resolveNodeNames(INodeDefManager *ndef) {
100         Biome *b;
101         
102         biome_registration_finished = true;
103         
104         for (size_t i = 1; i < biomes.size(); i++) {
105                 b = biomes[i];
106
107                 b->c_top = ndef->getId(b->nname_top);
108                 if (b->c_top == CONTENT_IGNORE) {
109                         errorstream << "BiomeDefManager::resolveNodeNames: node '"
110                                 << b->nname_top << "' not defined" << std::endl;
111                         b->c_top     = CONTENT_AIR;
112                         b->depth_top = 0;
113                 }
114         
115                 b->c_filler = ndef->getId(b->nname_filler);
116                 if (b->c_filler == CONTENT_IGNORE) {
117                         errorstream << "BiomeDefManager::resolveNodeNames: node '"
118                                 << b->nname_filler << "' not defined" << std::endl;
119                         b->c_filler     = CONTENT_AIR;
120                         b->depth_filler = 0;
121                 }
122                 
123                 b->c_water = ndef->getId(b->nname_water);
124                 if (b->c_water == CONTENT_IGNORE) {
125                         errorstream << "BiomeDefManager::resolveNodeNames: node '"
126                                 << b->nname_water << "' not defined" << std::endl;
127                         b->c_water = CONTENT_AIR;
128                 }
129                 
130                 b->c_dust = ndef->getId(b->nname_dust);
131                 if (b->c_dust == CONTENT_IGNORE) {
132                         errorstream << "BiomeDefManager::resolveNodeNames: node '"
133                                 << b->nname_dust << "' not defined" << std::endl;
134                 }
135                 
136                 b->c_dust_water = ndef->getId(b->nname_dust_water);
137                 if (b->c_dust_water == CONTENT_IGNORE) {
138                         errorstream << "BiomeDefManager::resolveNodeNames: node '"
139                                 << b->nname_dust_water << "' not defined" << std::endl;
140                 }
141         }
142 }
143
144
145 void BiomeDefManager::addBiome(Biome *b) {
146         if (biome_registration_finished) {
147                 errorstream << "BIomeDefManager: biome registration already finished, dropping " << b->name <<std::endl;
148                 delete b;
149                 return;
150         }
151         
152         size_t nbiomes = biomes.size();
153         if (nbiomes >= 0xFF) {
154                 errorstream << "BiomeDefManager: too many biomes, dropping " << b->name << std::endl;
155                 delete b;
156                 return;
157         }
158
159         b->id = (u8)nbiomes;
160         biomes.push_back(b);
161         verbosestream << "BiomeDefManager: added biome " << b->name << std::endl;
162 }
163
164
165 Biome *BiomeDefManager::getBiome(float heat, float humidity, s16 y) {
166         Biome *b, *biome_closest = NULL;
167         float dist_min = FLT_MAX;
168
169         for (size_t i = 1; i < biomes.size(); i++) {
170                 b = biomes[i];
171                 if (y > b->height_max || y < b->height_min)
172                         continue;
173
174                 float d_heat     = heat     - b->heat_point;
175                 float d_humidity = humidity - b->humidity_point;
176                 float dist = (d_heat * d_heat) +
177                                          (d_humidity * d_humidity);
178                 if (dist < dist_min) {
179                         dist_min = dist;
180                         biome_closest = b;
181                 }
182         }
183         
184         return biome_closest ? biome_closest : biomes[0];
185 }
186
187
188 u8 BiomeDefManager::getBiomeIdByName(const char *name) {
189         for (size_t i = 0; i != biomes.size(); i++) {
190                 if (!strcasecmp(name, biomes[i]->name.c_str()))
191                         return i;
192         }
193         
194         return 0;
195 }