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"
40 #include "mapgen_v7.h"
43 /////////////////// Mapgen V7 perlin noise default values
44 NoiseParams nparams_v7_def_terrain_base =
45 {0, 80.0, v3f(250.0, 250.0, 250.0), 82341, 5, 0.6};
46 NoiseParams nparams_v7_def_terrain_alt =
47 {0, 20.0, v3f(250.0, 250.0, 250.0), 5934, 5, 0.6};
48 NoiseParams nparams_v7_def_terrain_mod =
49 {0, 1.0, v3f(350.0, 350.0, 350.0), 85039, 5, 0.6};
50 NoiseParams nparams_v7_def_terrain_persist =
51 {0, 1.0, v3f(500.0, 500.0, 500.0), 539, 3, 0.6};
52 NoiseParams nparams_v7_def_height_select =
53 {0.5, 0.5, v3f(250.0, 250.0, 250.0), 4213, 5, 0.69};
54 NoiseParams nparams_v7_def_ridge =
55 {0, 1.0, v3f(100.0, 100.0, 100.0), 6467, 4, 0.75};
57 NoiseParams nparams_v6_def_beach =
58 {0.0, 1.0, v3f(250.0, 250.0, 250.0), 59420, 3, 0.50};
59 NoiseParams nparams_v6_def_cave =
60 {6.0, 6.0, v3f(250.0, 250.0, 250.0), 34329, 3, 0.50};
61 NoiseParams nparams_v6_def_humidity =
62 {0.5, 0.5, v3f(500.0, 500.0, 500.0), 72384, 4, 0.66};
63 NoiseParams nparams_v6_def_trees =
64 {0.0, 1.0, v3f(125.0, 125.0, 125.0), 2, 4, 0.66};
65 NoiseParams nparams_v6_def_apple_trees =
66 {0.0, 1.0, v3f(100.0, 100.0, 100.0), 342902, 3, 0.45};
68 ///////////////////////////////////////////////////////////////////////////////
71 MapgenV7::MapgenV7(int mapgenid, MapgenV7Params *params, EmergeManager *emerge) {
72 this->generating = false;
74 this->emerge = emerge;
75 this->bmgr = emerge->biomedef;
77 this->seed = (int)params->seed;
78 this->water_level = params->water_level;
79 this->flags = params->flags;
83 this->csize = v3s16(1, 1, 1) * params->chunksize * MAP_BLOCKSIZE;
84 this->ystride = csize.X; //////fix this
86 this->biomemap = new u8[csize.X * csize.Z];
87 this->heightmap = new s16[csize.X * csize.Z];
88 this->ridge_heightmap = new s16[csize.X * csize.Z];
91 noise_terrain_base = new Noise(¶ms->np_terrain_base, seed, csize.X, csize.Z);
92 noise_terrain_alt = new Noise(¶ms->np_terrain_alt, seed, csize.X, csize.Z);
93 noise_terrain_mod = new Noise(¶ms->np_terrain_mod, seed, csize.X, csize.Z);
94 noise_terrain_persist = new Noise(¶ms->np_terrain_persist, seed, csize.X, csize.Z);
95 noise_height_select = new Noise(¶ms->np_height_select, seed, csize.X, csize.Z);
96 noise_ridge = new Noise(¶ms->np_ridge, seed, csize.X, csize.Y, csize.Z);
99 noise_heat = new Noise(bmgr->np_heat, seed, csize.X, csize.Z);
100 noise_humidity = new Noise(bmgr->np_humidity, seed, csize.X, csize.Z);
104 MapgenV7::~MapgenV7() {
105 delete noise_terrain_base;
106 delete noise_terrain_mod;
107 delete noise_terrain_persist;
108 delete noise_height_select;
109 delete noise_terrain_alt;
112 delete noise_humidity;
114 delete[] ridge_heightmap;
120 int MapgenV7::getGroundLevelAtPoint(v2s16 p) {
121 s16 groundlevel = baseTerrainLevelAtPoint(p.X, p.Y);
122 float heat = NoisePerlin2D(bmgr->np_heat, p.X, p.Y, seed);
123 float humidity = NoisePerlin2D(bmgr->np_humidity, p.X, p.Y, seed);
124 Biome *b = bmgr->getBiome(heat, humidity, groundlevel);
127 int iters = 1024; // don't even bother iterating more than 64 times..
129 if (y <= water_level)
132 float ridgenoise = NoisePerlin3D(noise_ridge->np, p.X, y, p.Y, seed);
133 if (ridgenoise * (float)(y * y) < 15.0)
139 return y + b->top_depth;
143 void MapgenV7::makeChunk(BlockMakeData *data) {
144 assert(data->vmanip);
145 assert(data->nodedef);
146 assert(data->blockpos_requested.X >= data->blockpos_min.X &&
147 data->blockpos_requested.Y >= data->blockpos_min.Y &&
148 data->blockpos_requested.Z >= data->blockpos_min.Z);
149 assert(data->blockpos_requested.X <= data->blockpos_max.X &&
150 data->blockpos_requested.Y <= data->blockpos_max.Y &&
151 data->blockpos_requested.Z <= data->blockpos_max.Z);
153 this->generating = true;
154 this->vm = data->vmanip;
155 this->ndef = data->nodedef;
156 //TimeTaker t("makeChunk");
158 v3s16 blockpos_min = data->blockpos_min;
159 v3s16 blockpos_max = data->blockpos_max;
160 node_min = blockpos_min * MAP_BLOCKSIZE;
161 node_max = (blockpos_max + v3s16(1, 1, 1)) * MAP_BLOCKSIZE - v3s16(1, 1, 1);
162 full_node_min = (blockpos_min - 1) * MAP_BLOCKSIZE;
163 full_node_max = (blockpos_max + 2) * MAP_BLOCKSIZE - v3s16(1, 1, 1);
165 blockseed = emerge->getBlockSeed(full_node_min); //////use getBlockSeed2()!
170 // Calculate height map
171 s16 stone_surface_max_y = calcHeightMap();
174 BiomeNoiseInput binput;
175 binput.mapsize = v2s16(csize.X, csize.Z);
176 binput.heat_map = noise_heat->result;
177 binput.humidity_map = noise_humidity->result;
178 binput.height_map = heightmap;
179 bmgr->calcBiomes(&binput, biomemap);
181 c_stone = ndef->getId("mapgen_stone");
182 c_dirt = ndef->getId("mapgen_dirt");
183 c_dirt_with_grass = ndef->getId("mapgen_dirt_with_grass");
184 c_sand = ndef->getId("mapgen_sand");
185 c_water_source = ndef->getId("mapgen_water_source");
186 c_lava_source = ndef->getId("mapgen_lava_source");
192 if (flags & MG_CAVES)
193 generateCaves(stone_surface_max_y);
197 updateHeightmap(node_min, node_max);
199 if (flags & MG_DUNGEONS) {
200 DungeonGen dgen(ndef, data->seed, water_level);
201 dgen.generate(vm, blockseed, full_node_min, full_node_max);
204 for (size_t i = 0; i != emerge->decorations.size(); i++) {
205 Decoration *deco = emerge->decorations[i];
206 deco->placeDeco(this, blockseed + i, node_min, node_max);
209 for (size_t i = 0; i != emerge->ores.size(); i++) {
210 Ore *ore = emerge->ores[i];
211 ore->placeOre(this, blockseed + i, node_min, node_max);
214 //printf("makeChunk: %dms\n", t.stop());
216 updateLiquid(&data->transforming_liquid, full_node_min, full_node_max);
219 calcLighting(node_min - v3s16(1, 0, 1) * MAP_BLOCKSIZE,
220 node_max + v3s16(1, 0, 1) * MAP_BLOCKSIZE);
221 //setLighting(node_min - v3s16(1, 0, 1) * MAP_BLOCKSIZE,
222 // node_max + v3s16(1, 0, 1) * MAP_BLOCKSIZE, 0xFF);
224 this->generating = false;
228 void MapgenV7::calculateNoise() {
229 //TimeTaker t("calculateNoise", NULL, PRECISION_MICRO);
234 noise_terrain_mod->perlinMap2D(x, z);
236 noise_height_select->perlinMap2D(x, z);
237 noise_height_select->transformNoiseMap();
239 noise_terrain_persist->perlinMap2D(x, z);
240 float *persistmap = noise_terrain_persist->result;
241 for (int i = 0; i != csize.X * csize.Z; i++)
242 persistmap[i] = abs(persistmap[i]);
244 noise_terrain_base->perlinMap2DModulated(x, z, persistmap);
245 noise_terrain_base->transformNoiseMap();
247 noise_terrain_alt->perlinMap2DModulated(x, z, persistmap);
248 noise_terrain_alt->transformNoiseMap();
250 noise_ridge->perlinMap3D(x, y, z);
252 noise_heat->perlinMap2D(x, z);
254 noise_humidity->perlinMap2D(x, z);
256 //printf("calculateNoise: %dus\n", t.stop());
260 Biome *MapgenV7::getBiomeAtPoint(v3s16 p) {
261 float heat = NoisePerlin2D(bmgr->np_heat, p.X, p.Z, seed);
262 float humidity = NoisePerlin2D(bmgr->np_humidity, p.X, p.Z, seed);
263 s16 groundlevel = baseTerrainLevelAtPoint(p.X, p.Z);
265 return bmgr->getBiome(heat, humidity, groundlevel);
269 float MapgenV7::baseTerrainLevelAtPoint(int x, int z) {
270 float terrain_mod = NoisePerlin2DNoTxfm(noise_terrain_mod->np, x, z, seed);
271 float hselect = NoisePerlin2D(noise_height_select->np, x, z, seed);
272 float persist = abs(NoisePerlin2DNoTxfm(noise_terrain_persist->np, x, z, seed));
274 noise_terrain_base->np->persist = persist;
275 float terrain_base = NoisePerlin2D(noise_terrain_base->np, x, z, seed);
276 float height_base = terrain_base * terrain_mod;
278 noise_terrain_alt->np->persist = persist;
279 float height_alt = NoisePerlin2D(noise_terrain_alt->np, x, z, seed);
281 return (height_base * hselect) + (height_alt * (1.0 - hselect));
285 float MapgenV7::baseTerrainLevelFromMap(int index) {
286 float terrain_mod = noise_terrain_mod->result[index];
287 float hselect = noise_height_select->result[index];
288 float terrain_base = noise_terrain_base->result[index];
289 float height_base = terrain_base * terrain_mod;
290 float height_alt = noise_terrain_alt->result[index];
292 return (height_base * hselect) + (height_alt * (1.0 - hselect));
297 // Crap code to test log rivers as a proof-of-concept. Didn't work out too well.
298 void MapgenV7::carveRivers() {
299 MapNode n_air(CONTENT_AIR), n_water_source(c_water_source);
300 MapNode n_stone(c_stone);
305 for (s16 z = node_min.Z; z <= node_max.Z; z++)
306 for (s16 x = node_min.X; x <= node_max.X; x++, index++) {
307 float terrain_mod = noise_terrain_mod->result[index];
308 NoiseParams *np = noise_terrain_river->np;
309 np->persist = noise_terrain_persist->result[index];
310 float terrain_river = NoisePerlin2DNoTxfm(np, x, z, seed);
311 float height = terrain_river * (1 - abs(terrain_mod)) *
312 noise_terrain_river->np->scale;
313 height = log(height * height); //log(h^3) is pretty interesting for terrain
315 s16 y = heightmap[index];
316 if (height < 1.0 && y > river_depth &&
317 y - river_depth >= node_min.Y && y <= node_max.Y) {
319 for (s16 ry = y; ry != y - river_depth; ry--) {
320 u32 vi = vm->m_area.index(x, ry, z);
321 vm->m_data[vi] = n_air;
324 u32 vi = vm->m_area.index(x, y - river_depth, z);
325 vm->m_data[vi] = n_water_source;
332 int MapgenV7::calcHeightMap() {
333 int stone_surface_max_y = -MAP_GENERATION_LIMIT;
336 for (s16 z = node_min.Z; z <= node_max.Z; z++)
337 for (s16 x = node_min.X; x <= node_max.X; x++, index++) {
338 float surface_height = baseTerrainLevelFromMap(index);
339 s16 surface_y = (s16)surface_height;
341 heightmap[index] = surface_y;
342 ridge_heightmap[index] = surface_y;
344 if (surface_y > stone_surface_max_y)
345 stone_surface_max_y = surface_y;
348 return stone_surface_max_y;
352 void MapgenV7::generateTerrain() {
353 MapNode n_air(CONTENT_AIR), n_water_source(c_water_source);
354 MapNode n_stone(c_stone);
356 v3s16 em = vm->m_area.getExtent();
359 for (s16 z = node_min.Z; z <= node_max.Z; z++)
360 for (s16 x = node_min.X; x <= node_max.X; x++, index++) {
361 s16 surface_y = heightmap[index];
362 Biome *biome = bmgr->biomes[biomemap[index]];
364 u32 i = vm->m_area.index(x, node_min.Y, z);
365 for (s16 y = node_min.Y; y <= node_max.Y; y++) {
366 if (vm->m_data[i].getContent() == CONTENT_IGNORE) {
367 if (y <= surface_y) {
368 vm->m_data[i] = (y > water_level + biome->filler_height) ?
369 MapNode(biome->c_filler) : n_stone;
370 } else if (y <= water_level) {
371 vm->m_data[i] = n_water_source;
373 vm->m_data[i] = n_air;
376 vm->m_area.add_y(em, i, 1);
382 void MapgenV7::carveRidges() {
383 if (node_max.Y <= water_level)
386 MapNode n_air(CONTENT_AIR);
389 for (s16 z = node_min.Z; z <= node_max.Z; z++)
390 for (s16 y = node_min.Y; y <= node_max.Y; y++) {
391 u32 vi = vm->m_area.index(node_min.X, y, z);
392 for (s16 x = node_min.X; x <= node_max.X; x++, index++, vi++) {
393 // Removing this check will create huge underwater caverns,
394 // which are interesting but not desirable for gameplay
395 if (y <= water_level)
398 if (noise_ridge->result[index] * (float)(y * y) < 15.0)
401 int j = (z - node_min.Z) * csize.Z + (x - node_min.X); //////obviously just temporary
402 if (y < ridge_heightmap[j])
403 ridge_heightmap[j] = y - 1;
405 vm->m_data[vi] = n_air;
411 void MapgenV7::testBiomes() {
414 for (s16 z = node_min.Z; z <= node_min.Z; z++)
415 for (s16 x = node_min.X; x <= node_min.X; x++) {;
416 Biome *b = bmgr->getBiome(heat, humidity, 0);
418 // make an 80x80 grid with axes heat/humidity as a voroni diagram for biomes
419 // clear out y space for it first with air
420 // use absolute positioning, each chunk will be a +1 height
424 void MapgenV7::addTopNodes() {
425 v3s16 em = vm->m_area.getExtent();
429 for (s16 z = node_min.Z; z <= node_max.Z; z++)
430 for (s16 x = node_min.X; x <= node_max.X; x++, index++) {
431 Biome *biome = bmgr->biomes[biomemap[index]];
433 //////////////////// First, add top nodes below the ridge
434 s16 y = ridge_heightmap[index];
436 // This cutoff is good enough, but not perfect.
437 // It will cut off potentially placed top nodes at chunk boundaries
440 if (y > node_max.Y) {
441 y = node_max.Y; // Let's see if we can still go downward anyway
442 u32 vi = vm->m_area.index(x, y, z);
443 content_t c = vm->m_data[vi].getContent();
444 if (ndef->get(c).walkable)
448 // N.B. It is necessary to search downward since ridge_heightmap[i]
449 // might not be the actual height, just the lowest part in the chunk
450 // where a ridge had been carved
451 u32 i = vm->m_area.index(x, y, z);
452 for (; y >= node_min.Y; y--) {
453 content_t c = vm->m_data[i].getContent();
454 if (ndef->get(c).walkable)
456 vm->m_area.add_y(em, i, -1);
459 if (y != node_min.Y - 1 && y >= water_level) {
460 ridge_heightmap[index] = y; //update ridgeheight
461 ntopnodes = biome->top_depth;
462 for (; y <= node_max.Y && ntopnodes; y++) {
464 vm->m_data[i] = MapNode(biome->c_top);
465 vm->m_area.add_y(em, i, 1);
467 // If dirt, grow grass on it.
468 if (y > water_level - 10 &&
469 vm->m_data[i].getContent() == CONTENT_AIR) {
470 vm->m_area.add_y(em, i, -1);
471 if (vm->m_data[i].getContent() == c_dirt)
472 vm->m_data[i] = MapNode(c_dirt_with_grass);
476 //////////////////// Now, add top nodes on top of the ridge
477 y = heightmap[index];
478 if (y > node_max.Y) {
479 y = node_max.Y; // Let's see if we can still go downward anyway
480 u32 vi = vm->m_area.index(x, y, z);
481 content_t c = vm->m_data[vi].getContent();
482 if (ndef->get(c).walkable)
486 i = vm->m_area.index(x, y, z);
487 for (; y >= node_min.Y; y--) {
488 content_t c = vm->m_data[i].getContent();
489 if (ndef->get(c).walkable)
491 vm->m_area.add_y(em, i, -1);
494 if (y != node_min.Y - 1) {
495 ntopnodes = biome->top_depth;
496 // Let's see if we've already added it...
497 if (y == ridge_heightmap[index] + ntopnodes - 1)
500 for (; y <= node_max.Y && ntopnodes; y++) {
502 vm->m_data[i] = MapNode(biome->c_top);
503 vm->m_area.add_y(em, i, 1);
505 // If dirt, grow grass on it.
506 if (y > water_level - 10 &&
507 vm->m_data[i].getContent() == CONTENT_AIR) {
508 vm->m_area.add_y(em, i, -1);
509 if (vm->m_data[i].getContent() == c_dirt)
510 vm->m_data[i] = MapNode(c_dirt_with_grass);
517 #include "mapgen_v6.h"
518 void MapgenV7::generateCaves(int max_stone_y) {
519 PseudoRandom ps(blockseed + 21343);
521 int volume_nodes = (node_max.X - node_min.X + 1) *
522 (node_max.Y - node_min.Y + 1) *
523 (node_max.Z - node_min.Z + 1);
524 float cave_amount = NoisePerlin2D(&nparams_v6_def_cave,
525 node_min.X, node_min.Y, seed);
527 u32 caves_count = MYMAX(0.0, cave_amount) * volume_nodes / 250000;
528 for (u32 i = 0; i < caves_count; i++) {
529 CaveV7 cave(this, &ps, false);
530 cave.makeCave(node_min, node_max, max_stone_y);
533 u32 bruises_count = (ps.range(1, 8) == 1) ? ps.range(0, ps.range(0, 2)) : 1;
534 for (u32 i = 0; i < bruises_count; i++) {
535 CaveV7 cave(this, &ps, true);
536 cave.makeCave(node_min, node_max, max_stone_y);