]> git.lizzy.rs Git - minetest.git/blob - src/biome.cpp
The new mapgen, noise functions, et al.
[minetest.git] / src / biome.cpp
1 /*
2 Minetest-c55
3 Copyright (C) 2010-2011 kwolekr, Ryan Kwolek <kwolekr2@cs.scranton.edu>
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 "main.h"
24
25 #define BT_NONE                 0
26 #define BT_OCEAN                1
27 #define BT_LAKE                 2
28 #define BT_SBEACH               3
29 #define BT_GBEACH               4
30 #define BT_PLAINS               5
31 #define BT_HILLS                6
32 #define BT_EXTREMEHILLS 7
33 #define BT_MOUNTAINS    8
34 #define BT_DESERT               9
35 #define BT_DESERTHILLS  10
36 #define BT_HELL                 11
37 #define BT_AETHER               12
38
39 #define BT_BTMASK               0x3F
40
41 #define BTF_SNOW                0x40
42 #define BTF_FOREST              0x80
43
44 #define BGFREQ_1 (           0.40)
45 #define BGFREQ_2 (BGFREQ_1 + 0.05)
46 #define BGFREQ_3 (BGFREQ_2 + 0.08)
47 #define BGFREQ_4 (BGFREQ_3 + 0.35)
48 #define BGFREQ_5 (BGFREQ_4 + 0.18)
49 //BGFREQ_5 is not checked as an upper bound; it ought to sum up to 1.00, but it's okay if it doesn't.
50
51
52 /*float bg1_temps[] = {0.0};
53 int bg1_biomes[]  = {BT_OCEAN};
54
55 float bg2_temps[] = {10.0};
56 int bg2_biomes[]  = {BT_GBEACH, BT_SBEACH};
57
58 float bg3_temps[] = {30.0, 40.0};
59 int bg3_biomes[]  = {BT_HILLS, BT_EXTREMEHILLS, BT_MOUNTAINS};
60
61 float bg4_temps[] = {25.0, 30.0, 35.0, 40.0};
62 int bg4_biomes[]  = {BT_HILLS, BT_EXTREMEHILLS, BT_MOUNTAINS, BT_DESERT, BT_DESERTHILLS};
63
64 float bg5_temps[] = {5.0, 40.0};
65 int bg5_biomes[]  = {BT_LAKE, BT_PLAINS, BT_DESERT};*/
66
67
68 BiomeDefManager::BiomeDefManager(IGameDef *gamedef) {
69         this->m_gamedef = gamedef;
70         this->ndef      = gamedef->ndef();
71
72         //addDefaultBiomes(); //can't do this in the ctor, too early
73 }
74
75
76 BiomeDefManager::~BiomeDefManager() {
77         for (int i = 0; i != bgroups.size(); i++)
78                 delete bgroups[i];
79 }
80
81
82 void BiomeDefManager::addBiome() {
83
84 }
85
86
87 NoiseParams npmtdef = {0.0, 20.0, v3f(250., 250., 250.), 82341, 5, 0.6};
88
89 void BiomeDefManager::addDefaultBiomes() {
90         std::vector<Biome *> *bgroup;
91         Biome *b;
92
93         //bgroup = new std::vector<Biome *>;
94
95         b = new Biome;
96         b->name         = "Default";
97         b->n_top        = MapNode(ndef->getId("mapgen_stone"));
98         b->n_filler     = b->n_top;
99         b->ntopnodes    = 0;
100         b->height_min   = -MAP_GENERATION_LIMIT;
101         b->height_max   = MAP_GENERATION_LIMIT;
102         b->heat_min     = FLT_MIN;
103         b->heat_max     = FLT_MAX;
104         b->humidity_min = FLT_MIN;
105         b->humidity_max = FLT_MAX;
106         b->np = &npmtdef;
107         biome_default = b;
108
109         //bgroup->push_back(b);
110         //bgroups.push_back(bgroup);
111 }
112
113
114 Biome *BiomeDefManager::getBiome(float bgfreq, float heat, float humidity) {
115         std::vector<Biome *> bgroup;
116         Biome *b;
117         int i;
118
119         int ngroups = bgroup_freqs.size();
120         if (!ngroups)
121                 return biome_default;
122         for (i = 0; (i != ngroups - 1) && (bgfreq > bgroup_freqs[i]); i++);
123         bgroup = *(bgroups[i]);
124
125         int nbiomes = bgroup.size();
126         if (!nbiomes)
127                 return biome_default;
128         for (i = 0; i != nbiomes - 1; i++) {
129                 b = bgroup[i];
130                 if (heat >= b->heat_min && heat <= b->heat_max &&
131                         humidity >= b->humidity_min && humidity <= b->humidity_max)
132                         return b;
133         }
134
135         return biome_default;
136 }
137
138
139 //////////////////////////// [ Generic biome ] ////////////////////////////////
140
141
142 int Biome::getSurfaceHeight(float noise_terrain) {
143         return np->offset + np->scale * noise_terrain;
144 }
145
146
147 void Biome::genColumn(Mapgen *mg, int x, int z, int y1, int y2) {
148         //printf("(%d, %d): %f\n", x, z, mg->map_terrain[z * mg->ystride + x]);
149
150         //int surfaceh = 4;
151         int surfaceh = np->offset + np->scale * mg->map_terrain[(z - mg->node_min.Z) * 80 /*THIS IS TEMPORARY mg->ystride*/ + (x - mg->node_min.X)];
152         //printf("gen column %f\n", );
153         int y = y1;
154         int i = mg->vmanip->m_area.index(x, y, z);  //z * mg->zstride + x + (y - mg->vmanip->m_area.MinEdge.Y) * mg->ystride;
155         for (; y <= surfaceh - ntopnodes && y <= y2; y++, i += mg->ystride)
156                 mg->vmanip->m_data[i] = n_filler;
157         for (; y <= surfaceh && y <= y2; y++, i += mg->ystride)
158                 mg->vmanip->m_data[i] = n_top;
159         for (; y <= y2; y++, i += mg->ystride)
160                 mg->vmanip->m_data[i] = mg->n_air;
161 }
162
163
164
165 ///////////////////////////// [ Ocean biome ] /////////////////////////////////
166
167
168
169 void BiomeOcean::genColumn(Mapgen *mg, int x, int z, int y1, int y2) {
170         int y, i = 0;
171
172         int surfaceh = np->offset + np->scale * mg->map_terrain[z * mg->ystride + x];
173
174         i = z * mg->zstride + x;
175         for (y = y1; y <= surfaceh - ntopnodes && y <= y2; y++, i += mg->ystride)
176                 mg->vmanip->m_data[i] = n_filler;
177         for (; y <= surfaceh && y <= y2; y++, i += mg->ystride)
178                 mg->vmanip->m_data[i] = n_top;
179         for (; y <= y2; y++, i += mg->ystride)
180                 mg->vmanip->m_data[i] = mg->n_air;
181 }
182
183
184 ///////////////////////////// [ Nether biome ] /////////////////////////////////
185
186
187 int BiomeHell::getSurfaceHeight(float noise_terrain) {
188         return np->offset + np->scale * noise_terrain;
189 }
190
191
192 void BiomeHell::genColumn(Mapgen *mg, int x, int z, int y1, int y2) {
193         int y, i = 0;
194
195         int surfaceh = np->offset + np->scale * mg->map_terrain[z * mg->ystride + x];
196
197         i = z * mg->zstride + x;
198         for (y = y1; y <= surfaceh - ntopnodes && y <= y2; y++, i += mg->ystride)
199                 mg->vmanip->m_data[i] = n_filler;
200         for (; y <= surfaceh && y <= y2; y++, i += mg->ystride)
201                 mg->vmanip->m_data[i] = n_top;
202         for (; y <= y2; y++, i += mg->ystride)
203                 mg->vmanip->m_data[i] = mg->n_air;
204 }
205
206
207 ///////////////////////////// [ Aether biome ] ////////////////////////////////
208
209 /////////////////////////// [ Superflat biome ] ///////////////////////////////
210
211
212 int BiomeSuperflat::getSurfaceHeight(float noise_terrain) {
213         return ntopnodes;
214 }
215
216
217 void BiomeSuperflat::genColumn(Mapgen *mg, int x, int z, int y1, int y2) {
218         int y, i = 0;
219
220         int surfaceh = ntopnodes;
221
222         i = z * mg->zstride + x;
223         for (y = y1; y <= surfaceh - ntopnodes && y <= y2; y++, i += mg->ystride)
224                 mg->vmanip->m_data[i] = n_filler;
225         for (; y <= surfaceh && y <= y2; y++, i += mg->ystride)
226                 mg->vmanip->m_data[i] = n_top;
227         for (; y <= y2; y++, i += mg->ystride)
228                 mg->vmanip->m_data[i] = mg->n_air;
229 }