3 Copyright (C) 2014-2018 paramat
4 Copyright (C) 2014-2018 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(MapgenV5Params *params, EmergeManager *emerge)
49 : MapgenBasic(MAPGEN_V5, params, emerge)
51 spflags = params->spflags;
52 cave_width = params->cave_width;
53 large_cave_depth = params->large_cave_depth;
54 small_cave_num_min = params->small_cave_num_min;
55 small_cave_num_max = params->small_cave_num_max;
56 large_cave_num_min = params->large_cave_num_min;
57 large_cave_num_max = params->large_cave_num_max;
58 large_cave_flooded = params->large_cave_flooded;
59 cavern_limit = params->cavern_limit;
60 cavern_taper = params->cavern_taper;
61 cavern_threshold = params->cavern_threshold;
62 dungeon_ymin = params->dungeon_ymin;
63 dungeon_ymax = params->dungeon_ymax;
66 noise_filler_depth = new Noise(¶ms->np_filler_depth, seed, csize.X, csize.Z);
67 noise_factor = new Noise(¶ms->np_factor, seed, csize.X, csize.Z);
68 noise_height = new Noise(¶ms->np_height, seed, csize.X, csize.Z);
71 // 1-up 1-down overgeneration
72 noise_ground = new Noise(¶ms->np_ground, seed, csize.X, csize.Y + 2, csize.Z);
73 // 1 down overgeneration
74 MapgenBasic::np_cave1 = params->np_cave1;
75 MapgenBasic::np_cave2 = params->np_cave2;
76 MapgenBasic::np_cavern = params->np_cavern;
77 MapgenBasic::np_dungeons = params->np_dungeons;
83 delete noise_filler_depth;
90 MapgenV5Params::MapgenV5Params():
91 np_filler_depth (0, 1, v3f(150, 150, 150), 261, 4, 0.7, 2.0),
92 np_factor (0, 1, v3f(250, 250, 250), 920381, 3, 0.45, 2.0),
93 np_height (0, 10, v3f(250, 250, 250), 84174, 4, 0.5, 2.0),
94 np_ground (0, 40, v3f(80, 80, 80), 983240, 4, 0.55, 2.0, NOISE_FLAG_EASED),
95 np_cave1 (0, 12, v3f(61, 61, 61), 52534, 3, 0.5, 2.0),
96 np_cave2 (0, 12, v3f(67, 67, 67), 10325, 3, 0.5, 2.0),
97 np_cavern (0, 1, v3f(384, 128, 384), 723, 5, 0.63, 2.0),
98 np_dungeons (0.9, 0.5, v3f(500, 500, 500), 0, 2, 0.8, 2.0)
103 void MapgenV5Params::readParams(const Settings *settings)
105 settings->getFlagStrNoEx("mgv5_spflags", spflags, flagdesc_mapgen_v5);
106 settings->getFloatNoEx("mgv5_cave_width", cave_width);
107 settings->getS16NoEx("mgv5_large_cave_depth", large_cave_depth);
108 settings->getU16NoEx("mgv5_small_cave_num_min", small_cave_num_min);
109 settings->getU16NoEx("mgv5_small_cave_num_max", small_cave_num_max);
110 settings->getU16NoEx("mgv5_large_cave_num_min", large_cave_num_min);
111 settings->getU16NoEx("mgv5_large_cave_num_max", large_cave_num_max);
112 settings->getFloatNoEx("mgv5_large_cave_flooded", large_cave_flooded);
113 settings->getS16NoEx("mgv5_cavern_limit", cavern_limit);
114 settings->getS16NoEx("mgv5_cavern_taper", cavern_taper);
115 settings->getFloatNoEx("mgv5_cavern_threshold", cavern_threshold);
116 settings->getS16NoEx("mgv5_dungeon_ymin", dungeon_ymin);
117 settings->getS16NoEx("mgv5_dungeon_ymax", dungeon_ymax);
119 settings->getNoiseParams("mgv5_np_filler_depth", np_filler_depth);
120 settings->getNoiseParams("mgv5_np_factor", np_factor);
121 settings->getNoiseParams("mgv5_np_height", np_height);
122 settings->getNoiseParams("mgv5_np_ground", np_ground);
123 settings->getNoiseParams("mgv5_np_cave1", np_cave1);
124 settings->getNoiseParams("mgv5_np_cave2", np_cave2);
125 settings->getNoiseParams("mgv5_np_cavern", np_cavern);
126 settings->getNoiseParams("mgv5_np_dungeons", np_dungeons);
130 void MapgenV5Params::writeParams(Settings *settings) const
132 settings->setFlagStr("mgv5_spflags", spflags, flagdesc_mapgen_v5);
133 settings->setFloat("mgv5_cave_width", cave_width);
134 settings->setS16("mgv5_large_cave_depth", large_cave_depth);
135 settings->setU16("mgv5_small_cave_num_min", small_cave_num_min);
136 settings->setU16("mgv5_small_cave_num_max", small_cave_num_max);
137 settings->setU16("mgv5_large_cave_num_min", large_cave_num_min);
138 settings->setU16("mgv5_large_cave_num_max", large_cave_num_max);
139 settings->setFloat("mgv5_large_cave_flooded", large_cave_flooded);
140 settings->setS16("mgv5_cavern_limit", cavern_limit);
141 settings->setS16("mgv5_cavern_taper", cavern_taper);
142 settings->setFloat("mgv5_cavern_threshold", cavern_threshold);
143 settings->setS16("mgv5_dungeon_ymin", dungeon_ymin);
144 settings->setS16("mgv5_dungeon_ymax", dungeon_ymax);
146 settings->setNoiseParams("mgv5_np_filler_depth", np_filler_depth);
147 settings->setNoiseParams("mgv5_np_factor", np_factor);
148 settings->setNoiseParams("mgv5_np_height", np_height);
149 settings->setNoiseParams("mgv5_np_ground", np_ground);
150 settings->setNoiseParams("mgv5_np_cave1", np_cave1);
151 settings->setNoiseParams("mgv5_np_cave2", np_cave2);
152 settings->setNoiseParams("mgv5_np_cavern", np_cavern);
153 settings->setNoiseParams("mgv5_np_dungeons", np_dungeons);
157 void MapgenV5Params::setDefaultSettings(Settings *settings)
159 settings->setDefault("mgv5_spflags", flagdesc_mapgen_v5, MGV5_CAVERNS);
163 /////////////////////////////////////////////////////////////////
166 int MapgenV5::getSpawnLevelAtPoint(v2s16 p)
169 float f = 0.55 + NoisePerlin2D(&noise_factor->np, p.X, p.Y, seed);
174 float h = NoisePerlin2D(&noise_height->np, p.X, p.Y, seed);
176 // noise_height 'offset' is the average level of terrain. At least 50% of
177 // terrain will be below this.
178 // Raising the maximum spawn level above 'water_level + 16' is necessary
179 // for when noise_height 'offset' is set much higher than water_level.
180 s16 max_spawn_y = MYMAX(noise_height->np.offset, water_level + 16);
182 // Starting spawn search at max_spawn_y + 128 ensures 128 nodes of open
183 // space above spawn position. Avoids spawning in possibly sealed voids.
184 for (s16 y = max_spawn_y + 128; y >= water_level; y--) {
185 float n_ground = NoisePerlin3D(&noise_ground->np, p.X, y, p.Y, seed);
187 if (n_ground * f > y - h) { // If solid
188 if (y < water_level || y > max_spawn_y)
189 return MAX_MAP_GENERATION_LIMIT; // Unsuitable spawn point
191 // y + 2 because y is surface and due to biome 'dust' nodes.
195 // Unsuitable spawn position, no ground found
196 return MAX_MAP_GENERATION_LIMIT;
200 void MapgenV5::makeChunk(BlockMakeData *data)
203 assert(data->vmanip);
204 assert(data->nodedef);
205 assert(data->blockpos_requested.X >= data->blockpos_min.X &&
206 data->blockpos_requested.Y >= data->blockpos_min.Y &&
207 data->blockpos_requested.Z >= data->blockpos_min.Z);
208 assert(data->blockpos_requested.X <= data->blockpos_max.X &&
209 data->blockpos_requested.Y <= data->blockpos_max.Y &&
210 data->blockpos_requested.Z <= data->blockpos_max.Z);
212 this->generating = true;
213 this->vm = data->vmanip;
214 this->ndef = data->nodedef;
215 //TimeTaker t("makeChunk");
217 v3s16 blockpos_min = data->blockpos_min;
218 v3s16 blockpos_max = data->blockpos_max;
219 node_min = blockpos_min * MAP_BLOCKSIZE;
220 node_max = (blockpos_max + v3s16(1, 1, 1)) * MAP_BLOCKSIZE - v3s16(1, 1, 1);
221 full_node_min = (blockpos_min - 1) * MAP_BLOCKSIZE;
222 full_node_max = (blockpos_max + 2) * MAP_BLOCKSIZE - v3s16(1, 1, 1);
224 // Create a block-specific seed
225 blockseed = getBlockSeed2(full_node_min, seed);
227 // Generate base terrain
228 s16 stone_surface_max_y = generateBaseTerrain();
231 updateHeightmap(node_min, node_max);
233 // Init biome generator, place biome-specific nodes, and build biomemap
234 if (flags & MG_BIOMES) {
235 biomegen->calcBiomeNoise(node_min);
239 // Generate tunnels, caverns and large randomwalk caves
240 if (flags & MG_CAVES) {
241 // Generate tunnels first as caverns confuse them
242 generateCavesNoiseIntersection(stone_surface_max_y);
245 bool near_cavern = false;
246 if (spflags & MGV5_CAVERNS)
247 near_cavern = generateCavernsNoise(stone_surface_max_y);
249 // Generate large randomwalk caves
251 // Disable large randomwalk caves in this mapchunk by setting
252 // 'large cave depth' to world base. Avoids excessive liquid in
253 // large caverns and floating blobs of overgenerated liquid.
254 generateCavesRandomWalk(stone_surface_max_y,
255 -MAX_MAP_GENERATION_LIMIT);
257 generateCavesRandomWalk(stone_surface_max_y, large_cave_depth);
260 // Generate the registered ores
261 m_emerge->oremgr->placeAllOres(this, blockseed, node_min, node_max);
263 // Generate dungeons and desert temples
264 if (flags & MG_DUNGEONS)
265 generateDungeons(stone_surface_max_y);
267 // Generate the registered decorations
268 if (flags & MG_DECORATIONS)
269 m_emerge->decomgr->placeAllDecos(this, blockseed, node_min, node_max);
271 // Sprinkle some dust on top after everything else was generated
272 if (flags & MG_BIOMES)
275 //printf("makeChunk: %dms\n", t.stop());
277 // Add top and bottom side of water to transforming_liquid queue
278 updateLiquid(&data->transforming_liquid, full_node_min, full_node_max);
280 // Calculate lighting
281 if (flags & MG_LIGHT) {
282 calcLighting(node_min - v3s16(0, 1, 0), node_max + v3s16(0, 1, 0),
283 full_node_min, full_node_max);
286 this->generating = false;
290 int MapgenV5::generateBaseTerrain()
294 int stone_surface_max_y = -MAX_MAP_GENERATION_LIMIT;
296 noise_factor->perlinMap2D(node_min.X, node_min.Z);
297 noise_height->perlinMap2D(node_min.X, node_min.Z);
298 noise_ground->perlinMap3D(node_min.X, node_min.Y - 1, node_min.Z);
300 for (s16 z=node_min.Z; z<=node_max.Z; z++) {
301 for (s16 y=node_min.Y - 1; y<=node_max.Y + 1; y++) {
302 u32 vi = vm->m_area.index(node_min.X, y, z);
303 for (s16 x=node_min.X; x<=node_max.X; x++, vi++, index++, index2d++) {
304 if (vm->m_data[vi].getContent() != CONTENT_IGNORE)
307 float f = 0.55 + noise_factor->result[index2d];
312 float h = noise_height->result[index2d];
314 if (noise_ground->result[index] * f < y - h) {
315 if (y <= water_level)
316 vm->m_data[vi] = MapNode(c_water_source);
318 vm->m_data[vi] = MapNode(CONTENT_AIR);
320 vm->m_data[vi] = MapNode(c_stone);
321 if (y > stone_surface_max_y)
322 stone_surface_max_y = y;
330 return stone_surface_max_y;