3 Copyright (C) 2010-2013 kwolekr, Ryan Kwolek <kwolekr@minetest.net>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 //#include "serverobject.h"
28 #include "content_sao.h"
30 #include "content_mapnode.h" // For content_mapnode_get_new_name
31 #include "voxelalgorithms.h"
33 #include "settings.h" // For g_settings
34 #include "main.h" // For g_profiler
36 #include "dungeongen.h"
39 #include "mapgen_v7.h"
42 /////////////////// Mapgen V7 perlin noise default values
43 NoiseParams nparams_v7_def_terrain_base =
44 {0, 80.0, v3f(250.0, 250.0, 250.0), 82341, 5, 0.6};
45 NoiseParams nparams_v7_def_terrain_alt =
46 {0, 20.0, v3f(250.0, 250.0, 250.0), 5934, 5, 0.6};
47 NoiseParams nparams_v7_def_terrain_mod =
48 {0, 1.0, v3f(350.0, 350.0, 350.0), 85039, 5, 0.6};
49 NoiseParams nparams_v7_def_terrain_persist =
50 {0, 1.0, v3f(500.0, 500.0, 500.0), 539, 3, 0.6};
51 NoiseParams nparams_v7_def_height_select =
52 {0.5, 0.5, v3f(250.0, 250.0, 250.0), 4213, 5, 0.69};
53 NoiseParams nparams_v7_def_ridge =
54 {0.5, 1.0, v3f(100.0, 100.0, 100.0), 6467, 4, 0.75};
56 NoiseParams nparams_v6_def_beach =
57 {0.0, 1.0, v3f(250.0, 250.0, 250.0), 59420, 3, 0.50};
58 NoiseParams nparams_v6_def_cave =
59 {6.0, 6.0, v3f(250.0, 250.0, 250.0), 34329, 3, 0.50};
60 NoiseParams nparams_v6_def_humidity =
61 {0.5, 0.5, v3f(500.0, 500.0, 500.0), 72384, 4, 0.66};
62 NoiseParams nparams_v6_def_trees =
63 {0.0, 1.0, v3f(125.0, 125.0, 125.0), 2, 4, 0.66};
64 NoiseParams nparams_v6_def_apple_trees =
65 {0.0, 1.0, v3f(100.0, 100.0, 100.0), 342902, 3, 0.45};
67 ///////////////////////////////////////////////////////////////////////////////
70 MapgenV7::MapgenV7(int mapgenid, MapgenV7Params *params, EmergeManager *emerge) {
71 this->generating = false;
73 this->emerge = emerge;
74 this->bmgr = emerge->biomedef;
76 this->seed = (int)params->seed;
77 this->water_level = params->water_level;
78 this->flags = params->flags;
79 this->csize = v3s16(1, 1, 1) * params->chunksize * MAP_BLOCKSIZE;
80 // this->ystride = csize.X; //////fix this
82 this->biomemap = new u8[csize.X * csize.Z];
83 this->heightmap = new s16[csize.X * csize.Z];
84 this->ridge_heightmap = new s16[csize.X * csize.Z];
87 noise_terrain_base = new Noise(params->np_terrain_base, seed, csize.X, csize.Z);
88 noise_terrain_alt = new Noise(params->np_terrain_alt, seed, csize.X, csize.Z);
89 noise_terrain_mod = new Noise(params->np_terrain_mod, seed, csize.X, csize.Z);
90 noise_terrain_persist = new Noise(params->np_terrain_persist, seed, csize.X, csize.Z);
91 noise_height_select = new Noise(params->np_height_select, seed, csize.X, csize.Z);
92 noise_ridge = new Noise(params->np_ridge, seed, csize.X, csize.Y, csize.Z);
95 noise_heat = new Noise(bmgr->np_heat, seed, csize.X, csize.Z);
96 noise_humidity = new Noise(bmgr->np_humidity, seed, csize.X, csize.Z);
100 MapgenV7::~MapgenV7() {
101 delete noise_terrain_base;
102 delete noise_terrain_mod;
103 delete noise_terrain_persist;
104 delete noise_height_select;
105 delete noise_terrain_alt;
108 delete noise_humidity;
110 delete[] ridge_heightmap;
116 int MapgenV7::getGroundLevelAtPoint(v2s16 p) {
121 void MapgenV7::makeChunk(BlockMakeData *data) {
122 assert(data->vmanip);
123 assert(data->nodedef);
124 assert(data->blockpos_requested.X >= data->blockpos_min.X &&
125 data->blockpos_requested.Y >= data->blockpos_min.Y &&
126 data->blockpos_requested.Z >= data->blockpos_min.Z);
127 assert(data->blockpos_requested.X <= data->blockpos_max.X &&
128 data->blockpos_requested.Y <= data->blockpos_max.Y &&
129 data->blockpos_requested.Z <= data->blockpos_max.Z);
131 this->generating = true;
132 this->vm = data->vmanip;
133 this->ndef = data->nodedef;
134 //TimeTaker t("makeChunk");
136 v3s16 blockpos_min = data->blockpos_min;
137 v3s16 blockpos_max = data->blockpos_max;
138 v3s16 blockpos_full_min = blockpos_min - v3s16(1, 1, 1);
139 v3s16 blockpos_full_max = blockpos_max + v3s16(1, 1, 1);
140 node_min = blockpos_min * MAP_BLOCKSIZE;
141 node_max = (blockpos_max + v3s16(1, 1, 1)) * MAP_BLOCKSIZE - v3s16(1, 1, 1);
142 full_node_min = (blockpos_min - 1) * MAP_BLOCKSIZE;
143 full_node_max = (blockpos_max + 2) * MAP_BLOCKSIZE - v3s16(1, 1, 1);
145 //blockseed = emerge->getBlockSeed(full_node_min);
150 // Calculate height map
151 s16 stone_surface_max_y = calcHeightMap();
154 BiomeNoiseInput binput;
155 binput.mapsize = v2s16(csize.X, csize.Z);
156 binput.heat_map = noise_heat->result;
157 binput.humidity_map = noise_humidity->result;
158 binput.height_map = heightmap;
159 bmgr->calcBiomes(&binput, biomemap);
161 c_stone = ndef->getId("mapgen_stone");
162 c_dirt = ndef->getId("mapgen_dirt");
163 c_dirt_with_grass = ndef->getId("mapgen_dirt_with_grass");
164 c_sand = ndef->getId("mapgen_sand");
165 c_water_source = ndef->getId("mapgen_water_source");
174 //v3s16 central_area_size = node_max - node_min + v3s16(1,1,1);
176 if (flags & MG_DUNGEONS) {
177 DungeonGen dgen(ndef, data->seed, water_level);
178 dgen.generate(vm, blockseed, full_node_min, full_node_max);
181 for (size_t i = 0; i != emerge->ores.size(); i++) {
182 Ore *ore = emerge->ores[i];
183 ore->placeOre(this, blockseed + i, node_min, node_max);
186 //printf("makeChunk: %dms\n", t.stop());
188 updateLiquid(&data->transforming_liquid, full_node_min, full_node_max);
190 calcLighting(node_min - v3s16(1, 0, 1) * MAP_BLOCKSIZE,
191 node_max + v3s16(1, 0, 1) * MAP_BLOCKSIZE);
192 //setLighting(node_min, node_max, 0xFF);
194 this->generating = false;
198 void MapgenV7::calculateNoise() {
199 //TimeTaker t("calculateNoise", NULL, PRECISION_MICRO);
204 noise_terrain_mod->perlinMap2D(x, z);
206 noise_height_select->perlinMap2D(x, z);
207 noise_height_select->transformNoiseMap();
209 noise_terrain_persist->perlinMap2D(x, z);
210 float *persistmap = noise_terrain_persist->result;
211 for (int i = 0; i != csize.X * csize.Z; i++)
212 persistmap[i] = abs(persistmap[i]);
214 noise_terrain_base->perlinMap2DModulated(x, z, persistmap);
215 noise_terrain_base->transformNoiseMap();
217 noise_terrain_alt->perlinMap2DModulated(x, z, persistmap);
218 noise_terrain_alt->transformNoiseMap();
220 noise_ridge->perlinMap3D(x, y, z);
222 noise_heat->perlinMap2D(x, z);
224 noise_humidity->perlinMap2D(x, z);
226 //printf("calculateNoise: %dus\n", t.stop());
230 Biome *MapgenV7::getBiomeAtPoint(v3s16 p) {
231 float heat = NoisePerlin2D(bmgr->np_heat, p.X, p.Z, seed);
232 float humidity = NoisePerlin2D(bmgr->np_humidity, p.X, p.Z, seed);
233 s16 groundlevel = baseTerrainLevelAtPoint(p.X, p.Z);
235 return bmgr->getBiome(heat, humidity, groundlevel);
239 float MapgenV7::baseTerrainLevelAtPoint(int x, int z) {
240 float terrain_mod = NoisePerlin2DNoTxfm(noise_terrain_mod->np, x, z, seed);
241 float hselect = NoisePerlin2D(noise_height_select->np, x, z, seed);
242 float persist = abs(NoisePerlin2DNoTxfm(noise_terrain_persist->np, x, z, seed));
244 noise_terrain_base->np->persist = persist;
245 float terrain_base = NoisePerlin2D(noise_terrain_base->np, x, z, seed);
246 float height_base = terrain_base * terrain_mod;
248 noise_terrain_alt->np->persist = persist;
249 float height_alt = NoisePerlin2D(noise_terrain_alt->np, x, z, seed);
251 return (height_base * hselect) + (height_alt * (1.0 - hselect));
255 float MapgenV7::baseTerrainLevelFromMap(int index) {
256 float terrain_mod = noise_terrain_mod->result[index];
257 float hselect = noise_height_select->result[index];
258 float terrain_base = noise_terrain_base->result[index];
259 float height_base = terrain_base * terrain_mod;
260 float height_alt = noise_terrain_alt->result[index];
262 return (height_base * hselect) + (height_alt * (1.0 - hselect));
267 // Crap code to test log rivers as a proof-of-concept. Didn't work out too well.
268 void MapgenV7::carveRivers() {
269 MapNode n_air(CONTENT_AIR), n_water_source(c_water_source);
270 MapNode n_stone(c_stone);
275 for (s16 z = node_min.Z; z <= node_max.Z; z++)
276 for (s16 x = node_min.X; x <= node_max.X; x++, index++) {
277 float terrain_mod = noise_terrain_mod->result[index];
278 NoiseParams *np = noise_terrain_river->np;
279 np->persist = noise_terrain_persist->result[index];
280 float terrain_river = NoisePerlin2DNoTxfm(np, x, z, seed);
281 float height = terrain_river * (1 - abs(terrain_mod)) *
282 noise_terrain_river->np->scale;
283 height = log(height * height); //log(h^3) is pretty interesting for terrain
285 s16 y = heightmap[index];
286 if (height < 1.0 && y > river_depth &&
287 y - river_depth >= node_min.Y && y <= node_max.Y) {
289 for (s16 ry = y; ry != y - river_depth; ry--) {
290 u32 vi = vm->m_area.index(x, ry, z);
291 vm->m_data[vi] = n_air;
294 u32 vi = vm->m_area.index(x, y - river_depth, z);
295 vm->m_data[vi] = n_water_source;
302 int MapgenV7::calcHeightMap() {
303 int stone_surface_max_y = -MAP_GENERATION_LIMIT;
306 for (s16 z = node_min.Z; z <= node_max.Z; z++)
307 for (s16 x = node_min.X; x <= node_max.X; x++, index++) {
308 float surface_height = baseTerrainLevelFromMap(index);
309 s16 surface_y = (s16)surface_height;
311 heightmap[index] = surface_y;
312 ridge_heightmap[index] = surface_y;
314 if (surface_y > stone_surface_max_y)
315 stone_surface_max_y = surface_y;
318 return stone_surface_max_y;
322 void MapgenV7::generateTerrain() {
323 MapNode n_air(CONTENT_AIR), n_water_source(c_water_source);
324 MapNode n_stone(c_stone);
326 v3s16 em = vm->m_area.getExtent();
329 for (s16 z = node_min.Z; z <= node_max.Z; z++)
330 for (s16 x = node_min.X; x <= node_max.X; x++, index++) {
331 s16 surface_y = heightmap[index];
332 Biome *biome = bmgr->biomes[biomemap[index]];
334 u32 i = vm->m_area.index(x, node_min.Y, z);
335 for (s16 y = node_min.Y; y <= node_max.Y; y++) {
336 if (vm->m_data[i].getContent() == CONTENT_IGNORE) {
337 if (y <= surface_y) {
338 vm->m_data[i] = (y > water_level + biome->filler_height) ?
339 MapNode(biome->c_filler) : n_stone;
340 } else if (y <= water_level) {
341 vm->m_data[i] = n_water_source;
343 vm->m_data[i] = n_air;
346 vm->m_area.add_y(em, i, 1);
352 void MapgenV7::carveRidges() {
353 if (node_max.Y <= water_level)
356 MapNode n_air(CONTENT_AIR);
359 for (s16 z = node_min.Z; z <= node_max.Z; z++)
360 for (s16 y = node_min.Y; y <= node_max.Y; y++) {
361 u32 vi = vm->m_area.index(node_min.X, y, z);
362 for (s16 x = node_min.X; x <= node_max.X; x++, index++, vi++) {
363 // Removing this check will create huge underwater caverns,
364 // which are interesting but not desirable for gameplay
365 if (y <= water_level)
368 if (noise_ridge->result[index] * (float)(y * y) < 15.0)
371 int j = (z - node_min.Z) * csize.Z + (x - node_min.X); //////obviously just temporary
372 if (y < ridge_heightmap[j])
373 ridge_heightmap[j] = y - 1;
375 vm->m_data[vi] = n_air;
381 void MapgenV7::testBiomes() {
384 for (s16 z = node_min.Z; z <= node_min.Z; z++)
385 for (s16 x = node_min.X; x <= node_min.X; x++) {;
386 Biome *b = bmgr->getBiome(heat, humidity, 0);
388 // make an 80x80 grid with axes heat/humidity as a voroni diagram for biomes
389 // clear out y space for it first with air
390 // use absolute positioning, each chunk will be a +1 height
394 void MapgenV7::addTopNodes() {
395 v3s16 em = vm->m_area.getExtent();
399 for (s16 z = node_min.Z; z <= node_max.Z; z++)
400 for (s16 x = node_min.X; x <= node_max.X; x++, index++) {
401 // First, add top nodes below the ridge
402 s16 y = ridge_heightmap[index];
404 // This cutoff is good enough, but not perfect.
405 // It will cut off potentially placed top nodes at chunk boundaries
408 if (y > node_max.Y) {
409 y = node_max.Y; // Let's see if we can still go downward anyway
410 u32 vi = vm->m_area.index(x, y, z);
411 if (vm->m_data[vi].getContent() != CONTENT_AIR)
415 // N.B. It is necessary to search downward since range_heightmap[i]
416 // might not be the actual height, just the lowest part in the chunk
417 // where a ridge had been carved
418 u32 i = vm->m_area.index(x, y, z);
419 for (; y >= node_min.Y; y--) {
420 if (vm->m_data[i].getContent() != CONTENT_AIR)
422 vm->m_area.add_y(em, i, -1);
425 Biome *biome = bmgr->biomes[biomemap[index]];
427 if (y != node_min.Y - 1) {
428 ridge_heightmap[index] = y; //update ridgeheight
429 ntopnodes = biome->top_depth;
430 for (; y <= node_max.Y && ntopnodes; y++) {
432 vm->m_data[i] = MapNode(biome->c_top);
433 vm->m_area.add_y(em, i, 1);
435 //heightmap[index] = y;
438 // Now, add top nodes on top of the ridge
439 y = heightmap[index];
441 i = vm->m_area.index(x, y, z);
442 for (; y >= node_min.Y; y--) {
443 if (vm->m_data[i].getContent() != CONTENT_AIR)
445 vm->m_area.add_y(em, i, -1);
448 if (y != node_min.Y - 1) {
449 ntopnodes = biome->top_depth;
450 // Let's see if we've already added it...
451 if (y == ridge_heightmap[index] + ntopnodes - 1)
454 for (; y <= node_max.Y && ntopnodes; y++) {
456 vm->m_data[i] = MapNode(biome->c_top);
457 vm->m_area.add_y(em, i, 1);
464 void MapgenV7::growGrass() {
465 for (s16 z = node_min.Z; z <= node_max.Z; z++)
466 for (s16 x = node_min.X; x <= node_max.X; x++) {
467 // Find the lowest surface to which enough light ends up to make
468 // grass grow. Basically just wait until not air and not leaves.
471 v3s16 em = vm->m_area.getExtent();
472 u32 i = vm->m_area.index(x, node_max.Y, z);
474 // Go to ground level
475 for (y = node_max.Y; y >= node_min.Y; y--) {
476 MapNode &n = vm->m_data[i];
477 if (ndef->get(n).param_type != CPT_LIGHT ||
478 ndef->get(n).liquid_type != LIQUID_NONE)
480 vm->m_area.add_y(em, i, -1);
482 surface_y = (y >= node_min.Y) ? y : node_min.Y;
485 u32 i = vm->m_area.index(x, surface_y, z);
486 MapNode *n = &vm->m_data[i];
487 if (n->getContent() == c_dirt && surface_y >= water_level - 20)
488 n->setContent(c_dirt_with_grass);