Cavegen/Mgv5/Mgv7: Add optional giant caverns
[oweals/minetest.git] / src / mapgen_v5.cpp
1 /*
2 Minetest
3 Copyright (C) 2010-2015 kwolekr, Ryan Kwolek <kwolekr@minetest.net>
4 Copyright (C) 2010-2015 paramat, Matt Gregory
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20
21
22 #include "mapgen.h"
23 #include "voxel.h"
24 #include "noise.h"
25 #include "mapblock.h"
26 #include "mapnode.h"
27 #include "map.h"
28 #include "content_sao.h"
29 #include "nodedef.h"
30 #include "voxelalgorithms.h"
31 //#include "profiler.h" // For TimeTaker
32 #include "settings.h" // For g_settings
33 #include "emerge.h"
34 #include "dungeongen.h"
35 #include "cavegen.h"
36 #include "treegen.h"
37 #include "mg_biome.h"
38 #include "mg_ore.h"
39 #include "mg_decoration.h"
40 #include "mapgen_v5.h"
41
42
43 FlagDesc flagdesc_mapgen_v5[] = {
44         {"caverns", MGV5_CAVERNS},
45         {NULL,      0}
46 };
47
48
49 MapgenV5::MapgenV5(int mapgenid, MapgenV5Params *params, EmergeManager *emerge)
50         : MapgenBasic(mapgenid, params, emerge)
51 {
52         this->spflags          = params->spflags;
53         this->cave_width       = params->cave_width;
54         this->cavern_limit     = params->cavern_limit;
55         this->cavern_taper     = params->cavern_taper;
56         this->cavern_threshold = params->cavern_threshold;
57
58         // Terrain noise
59         noise_filler_depth = new Noise(&params->np_filler_depth, seed, csize.X, csize.Z);
60         noise_factor       = new Noise(&params->np_factor,       seed, csize.X, csize.Z);
61         noise_height       = new Noise(&params->np_height,       seed, csize.X, csize.Z);
62
63         // 3D terrain noise
64         // 1-up 1-down overgeneration
65         noise_ground = new Noise(&params->np_ground, seed, csize.X, csize.Y + 2, csize.Z);
66         // 1 down overgeneration
67         MapgenBasic::np_cave1  = params->np_cave1;
68         MapgenBasic::np_cave2  = params->np_cave2;
69         MapgenBasic::np_cavern = params->np_cavern;
70 }
71
72
73 MapgenV5::~MapgenV5()
74 {
75         delete noise_filler_depth;
76         delete noise_factor;
77         delete noise_height;
78         delete noise_ground;
79 }
80
81
82 MapgenV5Params::MapgenV5Params()
83 {
84         spflags          = MGV5_CAVERNS;
85         cave_width       = 0.125;
86         cavern_limit     = -256;
87         cavern_taper     = 256;
88         cavern_threshold = 0.7;
89
90         np_filler_depth = NoiseParams(0, 1,  v3f(150, 150, 150), 261,    4, 0.7,  2.0);
91         np_factor       = NoiseParams(0, 1,  v3f(250, 250, 250), 920381, 3, 0.45, 2.0);
92         np_height       = NoiseParams(0, 10, v3f(250, 250, 250), 84174,  4, 0.5,  2.0);
93         np_ground       = NoiseParams(0, 40, v3f(80,  80,  80),  983240, 4, 0.55, 2.0, NOISE_FLAG_EASED);
94         np_cave1        = NoiseParams(0, 12, v3f(50,  50,  50),  52534,  4, 0.5,  2.0);
95         np_cave2        = NoiseParams(0, 12, v3f(50,  50,  50),  10325,  4, 0.5,  2.0);
96         np_cavern       = NoiseParams(0, 1,  v3f(384, 128, 384), 723,    5, 0.63, 2.0);
97 }
98
99
100 void MapgenV5Params::readParams(const Settings *settings)
101 {
102         settings->getFlagStrNoEx("mgv5_spflags",        spflags, flagdesc_mapgen_v5);
103         settings->getFloatNoEx("mgv5_cave_width",       cave_width);
104         settings->getS16NoEx("mgv5_cavern_limit",       cavern_limit);
105         settings->getS16NoEx("mgv5_cavern_taper",       cavern_taper);
106         settings->getFloatNoEx("mgv5_cavern_threshold", cavern_threshold);
107
108         settings->getNoiseParams("mgv5_np_filler_depth", np_filler_depth);
109         settings->getNoiseParams("mgv5_np_factor",       np_factor);
110         settings->getNoiseParams("mgv5_np_height",       np_height);
111         settings->getNoiseParams("mgv5_np_ground",       np_ground);
112         settings->getNoiseParams("mgv5_np_cave1",        np_cave1);
113         settings->getNoiseParams("mgv5_np_cave2",        np_cave2);
114         settings->getNoiseParams("mgv5_np_cavern",       np_cavern);
115 }
116
117
118 void MapgenV5Params::writeParams(Settings *settings) const
119 {
120         settings->setFlagStr("mgv5_spflags",        spflags, flagdesc_mapgen_v5, U32_MAX);
121         settings->setFloat("mgv5_cave_width",       cave_width);
122         settings->setS16("mgv5_cavern_limit",       cavern_limit);
123         settings->setS16("mgv5_cavern_taper",       cavern_taper);
124         settings->setFloat("mgv5_cavern_threshold", cavern_threshold);
125
126         settings->setNoiseParams("mgv5_np_filler_depth", np_filler_depth);
127         settings->setNoiseParams("mgv5_np_factor",       np_factor);
128         settings->setNoiseParams("mgv5_np_height",       np_height);
129         settings->setNoiseParams("mgv5_np_ground",       np_ground);
130         settings->setNoiseParams("mgv5_np_cave1",        np_cave1);
131         settings->setNoiseParams("mgv5_np_cave2",        np_cave2);
132         settings->setNoiseParams("mgv5_np_cavern",       np_cavern);
133 }
134
135
136 int MapgenV5::getSpawnLevelAtPoint(v2s16 p)
137 {
138         //TimeTaker t("getGroundLevelAtPoint", NULL, PRECISION_MICRO);
139
140         float f = 0.55 + NoisePerlin2D(&noise_factor->np, p.X, p.Y, seed);
141         if (f < 0.01)
142                 f = 0.01;
143         else if (f >= 1.0)
144                 f *= 1.6;
145         float h = NoisePerlin2D(&noise_height->np, p.X, p.Y, seed);
146
147         for (s16 y = 128; y >= -128; y--) {
148                 float n_ground = NoisePerlin3D(&noise_ground->np, p.X, y, p.Y, seed);
149
150                 if (n_ground * f > y - h) {  // If solid
151                         // If either top 2 nodes of search are solid this is inside a
152                         // mountain or floatland with possibly no space for the player to spawn.
153                         if (y >= 127) {
154                                 return MAX_MAP_GENERATION_LIMIT;  // Unsuitable spawn point
155                         } else {  // Ground below at least 2 nodes of empty space
156                                 if (y <= water_level || y > water_level + 16)
157                                         return MAX_MAP_GENERATION_LIMIT;  // Unsuitable spawn point
158                                 else
159                                         return y;
160                         }
161                 }
162         }
163
164         //printf("getGroundLevelAtPoint: %dus\n", t.stop());
165         return MAX_MAP_GENERATION_LIMIT;  // Unsuitable spawn position, no ground found
166 }
167
168
169 void MapgenV5::makeChunk(BlockMakeData *data)
170 {
171         // Pre-conditions
172         assert(data->vmanip);
173         assert(data->nodedef);
174         assert(data->blockpos_requested.X >= data->blockpos_min.X &&
175                 data->blockpos_requested.Y >= data->blockpos_min.Y &&
176                 data->blockpos_requested.Z >= data->blockpos_min.Z);
177         assert(data->blockpos_requested.X <= data->blockpos_max.X &&
178                 data->blockpos_requested.Y <= data->blockpos_max.Y &&
179                 data->blockpos_requested.Z <= data->blockpos_max.Z);
180
181         this->generating = true;
182         this->vm   = data->vmanip;
183         this->ndef = data->nodedef;
184         //TimeTaker t("makeChunk");
185
186         v3s16 blockpos_min = data->blockpos_min;
187         v3s16 blockpos_max = data->blockpos_max;
188         node_min = blockpos_min * MAP_BLOCKSIZE;
189         node_max = (blockpos_max + v3s16(1, 1, 1)) * MAP_BLOCKSIZE - v3s16(1, 1, 1);
190         full_node_min = (blockpos_min - 1) * MAP_BLOCKSIZE;
191         full_node_max = (blockpos_max + 2) * MAP_BLOCKSIZE - v3s16(1, 1, 1);
192
193         // Create a block-specific seed
194         blockseed = getBlockSeed2(full_node_min, seed);
195
196         // Generate base terrain
197         s16 stone_surface_max_y = generateBaseTerrain();
198
199         // Create heightmap
200         updateHeightmap(node_min, node_max);
201
202         // Init biome generator, place biome-specific nodes, and build biomemap
203         biomegen->calcBiomeNoise(node_min);
204         MgStoneType stone_type = generateBiomes();
205
206         // Generate caverns, tunnels and classic caves
207         if (flags & MG_CAVES) {
208                 bool has_cavern = false;
209                 // Generate caverns
210                 if (spflags & MGV5_CAVERNS)
211                         has_cavern = generateCaverns(stone_surface_max_y);
212                 // Generate tunnels and classic caves
213                 if (has_cavern)
214                         // Disable classic caves in this mapchunk by setting
215                         // 'large cave depth' to world base. Avoids excessive liquid in
216                         // large caverns and floating blobs of overgenerated liquid.
217                         generateCaves(stone_surface_max_y, -MAX_MAP_GENERATION_LIMIT);
218                 else
219                         generateCaves(stone_surface_max_y, MGV5_LARGE_CAVE_DEPTH);
220         }
221
222         // Generate dungeons and desert temples
223         if (flags & MG_DUNGEONS)
224                 generateDungeons(stone_surface_max_y, stone_type);
225
226         // Generate the registered decorations
227         if (flags & MG_DECORATIONS)
228                 m_emerge->decomgr->placeAllDecos(this, blockseed, node_min, node_max);
229
230         // Generate the registered ores
231         m_emerge->oremgr->placeAllOres(this, blockseed, node_min, node_max);
232
233         // Sprinkle some dust on top after everything else was generated
234         dustTopNodes();
235
236         //printf("makeChunk: %dms\n", t.stop());
237
238         // Add top and bottom side of water to transforming_liquid queue
239         updateLiquid(&data->transforming_liquid, full_node_min, full_node_max);
240
241         // Calculate lighting
242         if (flags & MG_LIGHT) {
243                 calcLighting(node_min - v3s16(0, 1, 0), node_max + v3s16(0, 1, 0),
244                         full_node_min, full_node_max);
245         }
246
247         this->generating = false;
248 }
249
250
251 int MapgenV5::generateBaseTerrain()
252 {
253         u32 index = 0;
254         u32 index2d = 0;
255         int stone_surface_max_y = -MAX_MAP_GENERATION_LIMIT;
256
257         noise_factor->perlinMap2D(node_min.X, node_min.Z);
258         noise_height->perlinMap2D(node_min.X, node_min.Z);
259         noise_ground->perlinMap3D(node_min.X, node_min.Y - 1, node_min.Z);
260
261         for (s16 z=node_min.Z; z<=node_max.Z; z++) {
262                 for (s16 y=node_min.Y - 1; y<=node_max.Y + 1; y++) {
263                         u32 vi = vm->m_area.index(node_min.X, y, z);
264                         for (s16 x=node_min.X; x<=node_max.X; x++, vi++, index++, index2d++) {
265                                 if (vm->m_data[vi].getContent() != CONTENT_IGNORE)
266                                         continue;
267
268                                 float f = 0.55 + noise_factor->result[index2d];
269                                 if (f < 0.01)
270                                         f = 0.01;
271                                 else if (f >= 1.0)
272                                         f *= 1.6;
273                                 float h = noise_height->result[index2d];
274
275                                 if (noise_ground->result[index] * f < y - h) {
276                                         if (y <= water_level)
277                                                 vm->m_data[vi] = MapNode(c_water_source);
278                                         else
279                                                 vm->m_data[vi] = MapNode(CONTENT_AIR);
280                                 } else {
281                                         vm->m_data[vi] = MapNode(c_stone);
282                                         if (y > stone_surface_max_y)
283                                                 stone_surface_max_y = y;
284                                 }
285                         }
286                         index2d -= ystride;
287                 }
288                 index2d += ystride;
289         }
290
291         return stone_surface_max_y;
292 }