3 Copyright (C) 2014-2017 paramat
4 Copyright (C) 2014-2016 kwolekr, Ryan Kwolek <kwolekr@minetest.net>
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.
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.
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.
28 #include "content_sao.h"
30 #include "voxelalgorithms.h"
31 //#include "profiler.h" // For TimeTaker
32 #include "settings.h" // For g_settings
34 #include "dungeongen.h"
38 #include "mg_decoration.h"
39 #include "mapgen_v5.h"
42 FlagDesc flagdesc_mapgen_v5[] = {
43 {"caverns", MGV5_CAVERNS},
48 MapgenV5::MapgenV5(int mapgenid, MapgenV5Params *params, EmergeManager *emerge)
49 : MapgenBasic(mapgenid, params, emerge)
51 this->spflags = params->spflags;
52 this->cave_width = params->cave_width;
53 this->large_cave_depth = params->large_cave_depth;
54 this->cavern_limit = params->cavern_limit;
55 this->cavern_taper = params->cavern_taper;
56 this->cavern_threshold = params->cavern_threshold;
59 noise_filler_depth = new Noise(¶ms->np_filler_depth, seed, csize.X, csize.Z);
60 noise_factor = new Noise(¶ms->np_factor, seed, csize.X, csize.Z);
61 noise_height = new Noise(¶ms->np_height, seed, csize.X, csize.Z);
64 // 1-up 1-down overgeneration
65 noise_ground = new Noise(¶ms->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;
75 delete noise_filler_depth;
82 MapgenV5Params::MapgenV5Params()
84 np_filler_depth = NoiseParams(0, 1, v3f(150, 150, 150), 261, 4, 0.7, 2.0);
85 np_factor = NoiseParams(0, 1, v3f(250, 250, 250), 920381, 3, 0.45, 2.0);
86 np_height = NoiseParams(0, 10, v3f(250, 250, 250), 84174, 4, 0.5, 2.0);
87 np_ground = NoiseParams(0, 40, v3f(80, 80, 80), 983240, 4, 0.55, 2.0, NOISE_FLAG_EASED);
88 np_cave1 = NoiseParams(0, 12, v3f(50, 50, 50), 52534, 4, 0.5, 2.0);
89 np_cave2 = NoiseParams(0, 12, v3f(50, 50, 50), 10325, 4, 0.5, 2.0);
90 np_cavern = NoiseParams(0, 1, v3f(384, 128, 384), 723, 5, 0.63, 2.0);
94 void MapgenV5Params::readParams(const Settings *settings)
96 settings->getFlagStrNoEx("mgv5_spflags", spflags, flagdesc_mapgen_v5);
97 settings->getFloatNoEx("mgv5_cave_width", cave_width);
98 settings->getS16NoEx("mgv5_large_cave_depth", large_cave_depth);
99 settings->getS16NoEx("mgv5_cavern_limit", cavern_limit);
100 settings->getS16NoEx("mgv5_cavern_taper", cavern_taper);
101 settings->getFloatNoEx("mgv5_cavern_threshold", cavern_threshold);
103 settings->getNoiseParams("mgv5_np_filler_depth", np_filler_depth);
104 settings->getNoiseParams("mgv5_np_factor", np_factor);
105 settings->getNoiseParams("mgv5_np_height", np_height);
106 settings->getNoiseParams("mgv5_np_ground", np_ground);
107 settings->getNoiseParams("mgv5_np_cave1", np_cave1);
108 settings->getNoiseParams("mgv5_np_cave2", np_cave2);
109 settings->getNoiseParams("mgv5_np_cavern", np_cavern);
113 void MapgenV5Params::writeParams(Settings *settings) const
115 settings->setFlagStr("mgv5_spflags", spflags, flagdesc_mapgen_v5, U32_MAX);
116 settings->setFloat("mgv5_cave_width", cave_width);
117 settings->setS16("mgv5_large_cave_depth", large_cave_depth);
118 settings->setS16("mgv5_cavern_limit", cavern_limit);
119 settings->setS16("mgv5_cavern_taper", cavern_taper);
120 settings->setFloat("mgv5_cavern_threshold", cavern_threshold);
122 settings->setNoiseParams("mgv5_np_filler_depth", np_filler_depth);
123 settings->setNoiseParams("mgv5_np_factor", np_factor);
124 settings->setNoiseParams("mgv5_np_height", np_height);
125 settings->setNoiseParams("mgv5_np_ground", np_ground);
126 settings->setNoiseParams("mgv5_np_cave1", np_cave1);
127 settings->setNoiseParams("mgv5_np_cave2", np_cave2);
128 settings->setNoiseParams("mgv5_np_cavern", np_cavern);
132 int MapgenV5::getSpawnLevelAtPoint(v2s16 p)
134 //TimeTaker t("getGroundLevelAtPoint", NULL, PRECISION_MICRO);
136 float f = 0.55 + NoisePerlin2D(&noise_factor->np, p.X, p.Y, seed);
141 float h = NoisePerlin2D(&noise_height->np, p.X, p.Y, seed);
143 for (s16 y = 128; y >= -128; y--) {
144 float n_ground = NoisePerlin3D(&noise_ground->np, p.X, y, p.Y, seed);
146 if (n_ground * f > y - h) { // If solid
147 // If either top 2 nodes of search are solid this is inside a
148 // mountain or floatland with possibly no space for the player to spawn.
150 return MAX_MAP_GENERATION_LIMIT; // Unsuitable spawn point
151 } else { // Ground below at least 2 nodes of empty space
152 if (y <= water_level || y > water_level + 16)
153 return MAX_MAP_GENERATION_LIMIT; // Unsuitable spawn point
160 //printf("getGroundLevelAtPoint: %dus\n", t.stop());
161 return MAX_MAP_GENERATION_LIMIT; // Unsuitable spawn position, no ground found
165 void MapgenV5::makeChunk(BlockMakeData *data)
168 assert(data->vmanip);
169 assert(data->nodedef);
170 assert(data->blockpos_requested.X >= data->blockpos_min.X &&
171 data->blockpos_requested.Y >= data->blockpos_min.Y &&
172 data->blockpos_requested.Z >= data->blockpos_min.Z);
173 assert(data->blockpos_requested.X <= data->blockpos_max.X &&
174 data->blockpos_requested.Y <= data->blockpos_max.Y &&
175 data->blockpos_requested.Z <= data->blockpos_max.Z);
177 this->generating = true;
178 this->vm = data->vmanip;
179 this->ndef = data->nodedef;
180 //TimeTaker t("makeChunk");
182 v3s16 blockpos_min = data->blockpos_min;
183 v3s16 blockpos_max = data->blockpos_max;
184 node_min = blockpos_min * MAP_BLOCKSIZE;
185 node_max = (blockpos_max + v3s16(1, 1, 1)) * MAP_BLOCKSIZE - v3s16(1, 1, 1);
186 full_node_min = (blockpos_min - 1) * MAP_BLOCKSIZE;
187 full_node_max = (blockpos_max + 2) * MAP_BLOCKSIZE - v3s16(1, 1, 1);
189 // Create a block-specific seed
190 blockseed = getBlockSeed2(full_node_min, seed);
192 // Generate base terrain
193 s16 stone_surface_max_y = generateBaseTerrain();
196 updateHeightmap(node_min, node_max);
198 // Init biome generator, place biome-specific nodes, and build biomemap
199 biomegen->calcBiomeNoise(node_min);
200 MgStoneType stone_type = generateBiomes();
202 // Generate caverns, tunnels and classic caves
203 if (flags & MG_CAVES) {
204 bool near_cavern = false;
206 if (spflags & MGV5_CAVERNS)
207 near_cavern = generateCaverns(stone_surface_max_y);
208 // Generate tunnels and classic caves
210 // Disable classic caves in this mapchunk by setting
211 // 'large cave depth' to world base. Avoids excessive liquid in
212 // large caverns and floating blobs of overgenerated liquid.
213 generateCaves(stone_surface_max_y, -MAX_MAP_GENERATION_LIMIT);
215 generateCaves(stone_surface_max_y, large_cave_depth);
218 // Generate dungeons and desert temples
219 if (flags & MG_DUNGEONS)
220 generateDungeons(stone_surface_max_y, stone_type);
222 // Generate the registered decorations
223 if (flags & MG_DECORATIONS)
224 m_emerge->decomgr->placeAllDecos(this, blockseed, node_min, node_max);
226 // Generate the registered ores
227 m_emerge->oremgr->placeAllOres(this, blockseed, node_min, node_max);
229 // Sprinkle some dust on top after everything else was generated
232 //printf("makeChunk: %dms\n", t.stop());
234 // Add top and bottom side of water to transforming_liquid queue
235 updateLiquid(&data->transforming_liquid, full_node_min, full_node_max);
237 // Calculate lighting
238 if (flags & MG_LIGHT) {
239 calcLighting(node_min - v3s16(0, 1, 0), node_max + v3s16(0, 1, 0),
240 full_node_min, full_node_max);
243 this->generating = false;
247 int MapgenV5::generateBaseTerrain()
251 int stone_surface_max_y = -MAX_MAP_GENERATION_LIMIT;
253 noise_factor->perlinMap2D(node_min.X, node_min.Z);
254 noise_height->perlinMap2D(node_min.X, node_min.Z);
255 noise_ground->perlinMap3D(node_min.X, node_min.Y - 1, node_min.Z);
257 for (s16 z=node_min.Z; z<=node_max.Z; z++) {
258 for (s16 y=node_min.Y - 1; y<=node_max.Y + 1; y++) {
259 u32 vi = vm->m_area.index(node_min.X, y, z);
260 for (s16 x=node_min.X; x<=node_max.X; x++, vi++, index++, index2d++) {
261 if (vm->m_data[vi].getContent() != CONTENT_IGNORE)
264 float f = 0.55 + noise_factor->result[index2d];
269 float h = noise_height->result[index2d];
271 if (noise_ground->result[index] * f < y - h) {
272 if (y <= water_level)
273 vm->m_data[vi] = MapNode(c_water_source);
275 vm->m_data[vi] = MapNode(CONTENT_AIR);
277 vm->m_data[vi] = MapNode(c_stone);
278 if (y > stone_surface_max_y)
279 stone_surface_max_y = y;
287 return stone_surface_max_y;