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"
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->cavern_limit = params->cavern_limit;
54 this->cavern_taper = params->cavern_taper;
55 this->cavern_threshold = params->cavern_threshold;
58 noise_filler_depth = new Noise(¶ms->np_filler_depth, seed, csize.X, csize.Z);
59 noise_factor = new Noise(¶ms->np_factor, seed, csize.X, csize.Z);
60 noise_height = new Noise(¶ms->np_height, seed, csize.X, csize.Z);
63 // 1-up 1-down overgeneration
64 noise_ground = new Noise(¶ms->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;
74 delete noise_filler_depth;
81 MapgenV5Params::MapgenV5Params()
83 spflags = MGV5_CAVERNS;
87 cavern_threshold = 0.7;
89 np_filler_depth = NoiseParams(0, 1, v3f(150, 150, 150), 261, 4, 0.7, 2.0);
90 np_factor = NoiseParams(0, 1, v3f(250, 250, 250), 920381, 3, 0.45, 2.0);
91 np_height = NoiseParams(0, 10, v3f(250, 250, 250), 84174, 4, 0.5, 2.0);
92 np_ground = NoiseParams(0, 40, v3f(80, 80, 80), 983240, 4, 0.55, 2.0, NOISE_FLAG_EASED);
93 np_cave1 = NoiseParams(0, 12, v3f(50, 50, 50), 52534, 4, 0.5, 2.0);
94 np_cave2 = NoiseParams(0, 12, v3f(50, 50, 50), 10325, 4, 0.5, 2.0);
95 np_cavern = NoiseParams(0, 1, v3f(384, 128, 384), 723, 5, 0.63, 2.0);
99 void MapgenV5Params::readParams(const Settings *settings)
101 settings->getFlagStrNoEx("mgv5_spflags", spflags, flagdesc_mapgen_v5);
102 settings->getFloatNoEx("mgv5_cave_width", cave_width);
103 settings->getS16NoEx("mgv5_cavern_limit", cavern_limit);
104 settings->getS16NoEx("mgv5_cavern_taper", cavern_taper);
105 settings->getFloatNoEx("mgv5_cavern_threshold", cavern_threshold);
107 settings->getNoiseParams("mgv5_np_filler_depth", np_filler_depth);
108 settings->getNoiseParams("mgv5_np_factor", np_factor);
109 settings->getNoiseParams("mgv5_np_height", np_height);
110 settings->getNoiseParams("mgv5_np_ground", np_ground);
111 settings->getNoiseParams("mgv5_np_cave1", np_cave1);
112 settings->getNoiseParams("mgv5_np_cave2", np_cave2);
113 settings->getNoiseParams("mgv5_np_cavern", np_cavern);
117 void MapgenV5Params::writeParams(Settings *settings) const
119 settings->setFlagStr("mgv5_spflags", spflags, flagdesc_mapgen_v5, U32_MAX);
120 settings->setFloat("mgv5_cave_width", cave_width);
121 settings->setS16("mgv5_cavern_limit", cavern_limit);
122 settings->setS16("mgv5_cavern_taper", cavern_taper);
123 settings->setFloat("mgv5_cavern_threshold", cavern_threshold);
125 settings->setNoiseParams("mgv5_np_filler_depth", np_filler_depth);
126 settings->setNoiseParams("mgv5_np_factor", np_factor);
127 settings->setNoiseParams("mgv5_np_height", np_height);
128 settings->setNoiseParams("mgv5_np_ground", np_ground);
129 settings->setNoiseParams("mgv5_np_cave1", np_cave1);
130 settings->setNoiseParams("mgv5_np_cave2", np_cave2);
131 settings->setNoiseParams("mgv5_np_cavern", np_cavern);
135 int MapgenV5::getSpawnLevelAtPoint(v2s16 p)
137 //TimeTaker t("getGroundLevelAtPoint", NULL, PRECISION_MICRO);
139 float f = 0.55 + NoisePerlin2D(&noise_factor->np, p.X, p.Y, seed);
144 float h = NoisePerlin2D(&noise_height->np, p.X, p.Y, seed);
146 for (s16 y = 128; y >= -128; y--) {
147 float n_ground = NoisePerlin3D(&noise_ground->np, p.X, y, p.Y, seed);
149 if (n_ground * f > y - h) { // If solid
150 // If either top 2 nodes of search are solid this is inside a
151 // mountain or floatland with possibly no space for the player to spawn.
153 return MAX_MAP_GENERATION_LIMIT; // Unsuitable spawn point
154 } else { // Ground below at least 2 nodes of empty space
155 if (y <= water_level || y > water_level + 16)
156 return MAX_MAP_GENERATION_LIMIT; // Unsuitable spawn point
163 //printf("getGroundLevelAtPoint: %dus\n", t.stop());
164 return MAX_MAP_GENERATION_LIMIT; // Unsuitable spawn position, no ground found
168 void MapgenV5::makeChunk(BlockMakeData *data)
171 assert(data->vmanip);
172 assert(data->nodedef);
173 assert(data->blockpos_requested.X >= data->blockpos_min.X &&
174 data->blockpos_requested.Y >= data->blockpos_min.Y &&
175 data->blockpos_requested.Z >= data->blockpos_min.Z);
176 assert(data->blockpos_requested.X <= data->blockpos_max.X &&
177 data->blockpos_requested.Y <= data->blockpos_max.Y &&
178 data->blockpos_requested.Z <= data->blockpos_max.Z);
180 this->generating = true;
181 this->vm = data->vmanip;
182 this->ndef = data->nodedef;
183 //TimeTaker t("makeChunk");
185 v3s16 blockpos_min = data->blockpos_min;
186 v3s16 blockpos_max = data->blockpos_max;
187 node_min = blockpos_min * MAP_BLOCKSIZE;
188 node_max = (blockpos_max + v3s16(1, 1, 1)) * MAP_BLOCKSIZE - v3s16(1, 1, 1);
189 full_node_min = (blockpos_min - 1) * MAP_BLOCKSIZE;
190 full_node_max = (blockpos_max + 2) * MAP_BLOCKSIZE - v3s16(1, 1, 1);
192 // Create a block-specific seed
193 blockseed = getBlockSeed2(full_node_min, seed);
195 // Generate base terrain
196 s16 stone_surface_max_y = generateBaseTerrain();
199 updateHeightmap(node_min, node_max);
201 // Init biome generator, place biome-specific nodes, and build biomemap
202 biomegen->calcBiomeNoise(node_min);
203 MgStoneType stone_type = generateBiomes();
205 // Generate caverns, tunnels and classic caves
206 if (flags & MG_CAVES) {
207 bool near_cavern = false;
209 if (spflags & MGV5_CAVERNS)
210 near_cavern = generateCaverns(stone_surface_max_y);
211 // Generate tunnels and classic caves
213 // Disable classic caves in this mapchunk by setting
214 // 'large cave depth' to world base. Avoids excessive liquid in
215 // large caverns and floating blobs of overgenerated liquid.
216 generateCaves(stone_surface_max_y, -MAX_MAP_GENERATION_LIMIT);
218 generateCaves(stone_surface_max_y, MGV5_LARGE_CAVE_DEPTH);
221 // Generate dungeons and desert temples
222 if (flags & MG_DUNGEONS)
223 generateDungeons(stone_surface_max_y, stone_type);
225 // Generate the registered decorations
226 if (flags & MG_DECORATIONS)
227 m_emerge->decomgr->placeAllDecos(this, blockseed, node_min, node_max);
229 // Generate the registered ores
230 m_emerge->oremgr->placeAllOres(this, blockseed, node_min, node_max);
232 // Sprinkle some dust on top after everything else was generated
235 //printf("makeChunk: %dms\n", t.stop());
237 // Add top and bottom side of water to transforming_liquid queue
238 updateLiquid(&data->transforming_liquid, full_node_min, full_node_max);
240 // Calculate lighting
241 if (flags & MG_LIGHT) {
242 calcLighting(node_min - v3s16(0, 1, 0), node_max + v3s16(0, 1, 0),
243 full_node_min, full_node_max);
246 this->generating = false;
250 int MapgenV5::generateBaseTerrain()
254 int stone_surface_max_y = -MAX_MAP_GENERATION_LIMIT;
256 noise_factor->perlinMap2D(node_min.X, node_min.Z);
257 noise_height->perlinMap2D(node_min.X, node_min.Z);
258 noise_ground->perlinMap3D(node_min.X, node_min.Y - 1, node_min.Z);
260 for (s16 z=node_min.Z; z<=node_max.Z; z++) {
261 for (s16 y=node_min.Y - 1; y<=node_max.Y + 1; y++) {
262 u32 vi = vm->m_area.index(node_min.X, y, z);
263 for (s16 x=node_min.X; x<=node_max.X; x++, vi++, index++, index2d++) {
264 if (vm->m_data[vi].getContent() != CONTENT_IGNORE)
267 float f = 0.55 + noise_factor->result[index2d];
272 float h = noise_height->result[index2d];
274 if (noise_ground->result[index] * f < y - h) {
275 if (y <= water_level)
276 vm->m_data[vi] = MapNode(c_water_source);
278 vm->m_data[vi] = MapNode(CONTENT_AIR);
280 vm->m_data[vi] = MapNode(c_stone);
281 if (y > stone_surface_max_y)
282 stone_surface_max_y = y;
290 return stone_surface_max_y;