3 Copyright (C) 2010-2015 kwolekr, Ryan Kwolek <kwolekr@minetest.net>
4 Copyright (C) 2010-2015 paramat, Matt Gregory
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"
39 #include "mg_decoration.h"
40 #include "mapgen_v5.h"
43 FlagDesc flagdesc_mapgen_v5[] = {
44 {"caverns", MGV5_CAVERNS},
49 MapgenV5::MapgenV5(int mapgenid, MapgenV5Params *params, EmergeManager *emerge)
50 : MapgenBasic(mapgenid, params, emerge)
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;
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 spflags = MGV5_CAVERNS;
88 cavern_threshold = 0.7;
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);
100 void MapgenV5Params::readParams(const Settings *settings)
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);
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);
118 void MapgenV5Params::writeParams(Settings *settings) const
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);
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);
136 int MapgenV5::getSpawnLevelAtPoint(v2s16 p)
138 //TimeTaker t("getGroundLevelAtPoint", NULL, PRECISION_MICRO);
140 float f = 0.55 + NoisePerlin2D(&noise_factor->np, p.X, p.Y, seed);
145 float h = NoisePerlin2D(&noise_height->np, p.X, p.Y, seed);
147 for (s16 y = 128; y >= -128; y--) {
148 float n_ground = NoisePerlin3D(&noise_ground->np, p.X, y, p.Y, seed);
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.
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
164 //printf("getGroundLevelAtPoint: %dus\n", t.stop());
165 return MAX_MAP_GENERATION_LIMIT; // Unsuitable spawn position, no ground found
169 void MapgenV5::makeChunk(BlockMakeData *data)
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);
181 this->generating = true;
182 this->vm = data->vmanip;
183 this->ndef = data->nodedef;
184 //TimeTaker t("makeChunk");
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);
193 // Create a block-specific seed
194 blockseed = getBlockSeed2(full_node_min, seed);
196 // Generate base terrain
197 s16 stone_surface_max_y = generateBaseTerrain();
200 updateHeightmap(node_min, node_max);
202 // Init biome generator, place biome-specific nodes, and build biomemap
203 biomegen->calcBiomeNoise(node_min);
204 MgStoneType stone_type = generateBiomes();
206 // Generate caverns, tunnels and classic caves
207 if (flags & MG_CAVES) {
208 bool has_cavern = false;
210 if (spflags & MGV5_CAVERNS)
211 has_cavern = generateCaverns(stone_surface_max_y);
212 // Generate tunnels and classic caves
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);
219 generateCaves(stone_surface_max_y, MGV5_LARGE_CAVE_DEPTH);
222 // Generate dungeons and desert temples
223 if (flags & MG_DUNGEONS)
224 generateDungeons(stone_surface_max_y, stone_type);
226 // Generate the registered decorations
227 if (flags & MG_DECORATIONS)
228 m_emerge->decomgr->placeAllDecos(this, blockseed, node_min, node_max);
230 // Generate the registered ores
231 m_emerge->oremgr->placeAllOres(this, blockseed, node_min, node_max);
233 // Sprinkle some dust on top after everything else was generated
236 //printf("makeChunk: %dms\n", t.stop());
238 // Add top and bottom side of water to transforming_liquid queue
239 updateLiquid(&data->transforming_liquid, full_node_min, full_node_max);
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);
247 this->generating = false;
251 int MapgenV5::generateBaseTerrain()
255 int stone_surface_max_y = -MAX_MAP_GENERATION_LIMIT;
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);
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)
268 float f = 0.55 + noise_factor->result[index2d];
273 float h = noise_height->result[index2d];
275 if (noise_ground->result[index] * f < y - h) {
276 if (y <= water_level)
277 vm->m_data[vi] = MapNode(c_water_source);
279 vm->m_data[vi] = MapNode(CONTENT_AIR);
281 vm->m_data[vi] = MapNode(c_stone);
282 if (y > stone_surface_max_y)
283 stone_surface_max_y = y;
291 return stone_surface_max_y;