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