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.5, 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;
80 this->csize = v3s16(1, 1, 1) * params->chunksize * MAP_BLOCKSIZE;
81 // this->ystride = csize.X; //////fix this
83 this->biomemap = new u8[csize.X * csize.Z];
84 this->heightmap = new s16[csize.X * csize.Z];
85 this->ridge_heightmap = new s16[csize.X * csize.Z];
88 noise_terrain_base = new Noise(params->np_terrain_base, seed, csize.X, csize.Z);
89 noise_terrain_alt = new Noise(params->np_terrain_alt, seed, csize.X, csize.Z);
90 noise_terrain_mod = new Noise(params->np_terrain_mod, seed, csize.X, csize.Z);
91 noise_terrain_persist = new Noise(params->np_terrain_persist, seed, csize.X, csize.Z);
92 noise_height_select = new Noise(params->np_height_select, seed, csize.X, csize.Z);
93 noise_ridge = new Noise(params->np_ridge, seed, csize.X, csize.Y, csize.Z);
96 noise_heat = new Noise(bmgr->np_heat, seed, csize.X, csize.Z);
97 noise_humidity = new Noise(bmgr->np_humidity, seed, csize.X, csize.Z);
101 MapgenV7::~MapgenV7() {
102 delete noise_terrain_base;
103 delete noise_terrain_mod;
104 delete noise_terrain_persist;
105 delete noise_height_select;
106 delete noise_terrain_alt;
109 delete noise_humidity;
111 delete[] ridge_heightmap;
117 int MapgenV7::getGroundLevelAtPoint(v2s16 p) {
118 s16 groundlevel = baseTerrainLevelAtPoint(p.X, p.Y);
119 float heat = NoisePerlin2D(bmgr->np_heat, p.X, p.Y, seed);
120 float humidity = NoisePerlin2D(bmgr->np_humidity, p.X, p.Y, seed);
121 Biome *b = bmgr->getBiome(heat, humidity, groundlevel);
124 if (y > water_level) {
125 int iters = 1024; // don't even bother iterating more than 1024 times..
127 float ridgenoise = NoisePerlin3D(noise_ridge->np, p.X, y, p.Y, seed);
128 if (ridgenoise * (float)(y * y) < 15.0)
134 return y + b->top_depth;
138 void MapgenV7::makeChunk(BlockMakeData *data) {
139 assert(data->vmanip);
140 assert(data->nodedef);
141 assert(data->blockpos_requested.X >= data->blockpos_min.X &&
142 data->blockpos_requested.Y >= data->blockpos_min.Y &&
143 data->blockpos_requested.Z >= data->blockpos_min.Z);
144 assert(data->blockpos_requested.X <= data->blockpos_max.X &&
145 data->blockpos_requested.Y <= data->blockpos_max.Y &&
146 data->blockpos_requested.Z <= data->blockpos_max.Z);
148 this->generating = true;
149 this->vm = data->vmanip;
150 this->ndef = data->nodedef;
151 //TimeTaker t("makeChunk");
153 v3s16 blockpos_min = data->blockpos_min;
154 v3s16 blockpos_max = data->blockpos_max;
155 v3s16 blockpos_full_min = blockpos_min - v3s16(1, 1, 1);
156 v3s16 blockpos_full_max = blockpos_max + v3s16(1, 1, 1);
157 node_min = blockpos_min * MAP_BLOCKSIZE;
158 node_max = (blockpos_max + v3s16(1, 1, 1)) * MAP_BLOCKSIZE - v3s16(1, 1, 1);
159 full_node_min = (blockpos_min - 1) * MAP_BLOCKSIZE;
160 full_node_max = (blockpos_max + 2) * MAP_BLOCKSIZE - v3s16(1, 1, 1);
162 blockseed = emerge->getBlockSeed(full_node_min); //////use getBlockSeed2()!
167 // Calculate height map
168 s16 stone_surface_max_y = calcHeightMap();
171 BiomeNoiseInput binput;
172 binput.mapsize = v2s16(csize.X, csize.Z);
173 binput.heat_map = noise_heat->result;
174 binput.humidity_map = noise_humidity->result;
175 binput.height_map = heightmap;
176 bmgr->calcBiomes(&binput, biomemap);
178 c_stone = ndef->getId("mapgen_stone");
179 c_dirt = ndef->getId("mapgen_dirt");
180 c_dirt_with_grass = ndef->getId("mapgen_dirt_with_grass");
181 c_sand = ndef->getId("mapgen_sand");
182 c_water_source = ndef->getId("mapgen_water_source");
183 c_lava_source = ndef->getId("mapgen_lava_source");
188 generateCaves(stone_surface_max_y);
190 //v3s16 central_area_size = node_max - node_min + v3s16(1,1,1);
192 if (flags & MG_DUNGEONS) {
193 DungeonGen dgen(ndef, data->seed, water_level);
194 dgen.generate(vm, blockseed, full_node_min, full_node_max);
197 for (size_t i = 0; i != emerge->ores.size(); i++) {
198 Ore *ore = emerge->ores[i];
199 ore->placeOre(this, blockseed + i, node_min, node_max);
202 //printf("makeChunk: %dms\n", t.stop());
204 updateLiquid(&data->transforming_liquid, full_node_min, full_node_max);
206 calcLighting(node_min - v3s16(1, 0, 1) * MAP_BLOCKSIZE,
207 node_max + v3s16(1, 0, 1) * MAP_BLOCKSIZE);
208 //setLighting(node_min - v3s16(1, 0, 1) * MAP_BLOCKSIZE,
209 // node_max + v3s16(1, 0, 1) * MAP_BLOCKSIZE, 0xFF);
211 this->generating = false;
215 void MapgenV7::calculateNoise() {
216 //TimeTaker t("calculateNoise", NULL, PRECISION_MICRO);
221 noise_terrain_mod->perlinMap2D(x, z);
223 noise_height_select->perlinMap2D(x, z);
224 noise_height_select->transformNoiseMap();
226 noise_terrain_persist->perlinMap2D(x, z);
227 float *persistmap = noise_terrain_persist->result;
228 for (int i = 0; i != csize.X * csize.Z; i++)
229 persistmap[i] = abs(persistmap[i]);
231 noise_terrain_base->perlinMap2DModulated(x, z, persistmap);
232 noise_terrain_base->transformNoiseMap();
234 noise_terrain_alt->perlinMap2DModulated(x, z, persistmap);
235 noise_terrain_alt->transformNoiseMap();
237 noise_ridge->perlinMap3D(x, y, z);
239 noise_heat->perlinMap2D(x, z);
241 noise_humidity->perlinMap2D(x, z);
243 //printf("calculateNoise: %dus\n", t.stop());
247 Biome *MapgenV7::getBiomeAtPoint(v3s16 p) {
248 float heat = NoisePerlin2D(bmgr->np_heat, p.X, p.Z, seed);
249 float humidity = NoisePerlin2D(bmgr->np_humidity, p.X, p.Z, seed);
250 s16 groundlevel = baseTerrainLevelAtPoint(p.X, p.Z);
252 return bmgr->getBiome(heat, humidity, groundlevel);
256 float MapgenV7::baseTerrainLevelAtPoint(int x, int z) {
257 float terrain_mod = NoisePerlin2DNoTxfm(noise_terrain_mod->np, x, z, seed);
258 float hselect = NoisePerlin2D(noise_height_select->np, x, z, seed);
259 float persist = abs(NoisePerlin2DNoTxfm(noise_terrain_persist->np, x, z, seed));
261 noise_terrain_base->np->persist = persist;
262 float terrain_base = NoisePerlin2D(noise_terrain_base->np, x, z, seed);
263 float height_base = terrain_base * terrain_mod;
265 noise_terrain_alt->np->persist = persist;
266 float height_alt = NoisePerlin2D(noise_terrain_alt->np, x, z, seed);
268 return (height_base * hselect) + (height_alt * (1.0 - hselect));
272 float MapgenV7::baseTerrainLevelFromMap(int index) {
273 float terrain_mod = noise_terrain_mod->result[index];
274 float hselect = noise_height_select->result[index];
275 float terrain_base = noise_terrain_base->result[index];
276 float height_base = terrain_base * terrain_mod;
277 float height_alt = noise_terrain_alt->result[index];
279 return (height_base * hselect) + (height_alt * (1.0 - hselect));
284 // Crap code to test log rivers as a proof-of-concept. Didn't work out too well.
285 void MapgenV7::carveRivers() {
286 MapNode n_air(CONTENT_AIR), n_water_source(c_water_source);
287 MapNode n_stone(c_stone);
292 for (s16 z = node_min.Z; z <= node_max.Z; z++)
293 for (s16 x = node_min.X; x <= node_max.X; x++, index++) {
294 float terrain_mod = noise_terrain_mod->result[index];
295 NoiseParams *np = noise_terrain_river->np;
296 np->persist = noise_terrain_persist->result[index];
297 float terrain_river = NoisePerlin2DNoTxfm(np, x, z, seed);
298 float height = terrain_river * (1 - abs(terrain_mod)) *
299 noise_terrain_river->np->scale;
300 height = log(height * height); //log(h^3) is pretty interesting for terrain
302 s16 y = heightmap[index];
303 if (height < 1.0 && y > river_depth &&
304 y - river_depth >= node_min.Y && y <= node_max.Y) {
306 for (s16 ry = y; ry != y - river_depth; ry--) {
307 u32 vi = vm->m_area.index(x, ry, z);
308 vm->m_data[vi] = n_air;
311 u32 vi = vm->m_area.index(x, y - river_depth, z);
312 vm->m_data[vi] = n_water_source;
319 int MapgenV7::calcHeightMap() {
320 int stone_surface_max_y = -MAP_GENERATION_LIMIT;
323 for (s16 z = node_min.Z; z <= node_max.Z; z++)
324 for (s16 x = node_min.X; x <= node_max.X; x++, index++) {
325 float surface_height = baseTerrainLevelFromMap(index);
326 s16 surface_y = (s16)surface_height;
328 heightmap[index] = surface_y;
329 ridge_heightmap[index] = surface_y;
331 if (surface_y > stone_surface_max_y)
332 stone_surface_max_y = surface_y;
335 return stone_surface_max_y;
339 void MapgenV7::generateTerrain() {
340 MapNode n_air(CONTENT_AIR), n_water_source(c_water_source);
341 MapNode n_stone(c_stone);
343 v3s16 em = vm->m_area.getExtent();
346 for (s16 z = node_min.Z; z <= node_max.Z; z++)
347 for (s16 x = node_min.X; x <= node_max.X; x++, index++) {
348 s16 surface_y = heightmap[index];
349 Biome *biome = bmgr->biomes[biomemap[index]];
351 u32 i = vm->m_area.index(x, node_min.Y, z);
352 for (s16 y = node_min.Y; y <= node_max.Y; y++) {
353 if (vm->m_data[i].getContent() == CONTENT_IGNORE) {
354 if (y <= surface_y) {
355 vm->m_data[i] = (y > water_level + biome->filler_height) ?
356 MapNode(biome->c_filler) : n_stone;
357 } else if (y <= water_level) {
358 vm->m_data[i] = n_water_source;
360 vm->m_data[i] = n_air;
363 vm->m_area.add_y(em, i, 1);
369 void MapgenV7::carveRidges() {
370 if (node_max.Y <= water_level)
373 MapNode n_air(CONTENT_AIR);
376 for (s16 z = node_min.Z; z <= node_max.Z; z++)
377 for (s16 y = node_min.Y; y <= node_max.Y; y++) {
378 u32 vi = vm->m_area.index(node_min.X, y, z);
379 for (s16 x = node_min.X; x <= node_max.X; x++, index++, vi++) {
380 // Removing this check will create huge underwater caverns,
381 // which are interesting but not desirable for gameplay
382 if (y <= water_level)
385 if (noise_ridge->result[index] * (float)(y * y) < 15.0)
388 int j = (z - node_min.Z) * csize.Z + (x - node_min.X); //////obviously just temporary
389 if (y < ridge_heightmap[j])
390 ridge_heightmap[j] = y - 1;
392 vm->m_data[vi] = n_air;
398 void MapgenV7::testBiomes() {
401 for (s16 z = node_min.Z; z <= node_min.Z; z++)
402 for (s16 x = node_min.X; x <= node_min.X; x++) {;
403 Biome *b = bmgr->getBiome(heat, humidity, 0);
405 // make an 80x80 grid with axes heat/humidity as a voroni diagram for biomes
406 // clear out y space for it first with air
407 // use absolute positioning, each chunk will be a +1 height
411 void MapgenV7::addTopNodes() {
412 v3s16 em = vm->m_area.getExtent();
416 for (s16 z = node_min.Z; z <= node_max.Z; z++)
417 for (s16 x = node_min.X; x <= node_max.X; x++, index++) {
418 Biome *biome = bmgr->biomes[biomemap[index]];
420 //////////////////// First, add top nodes below the ridge
421 s16 y = ridge_heightmap[index];
423 // This cutoff is good enough, but not perfect.
424 // It will cut off potentially placed top nodes at chunk boundaries
427 if (y > node_max.Y) {
428 y = node_max.Y; // Let's see if we can still go downward anyway
429 u32 vi = vm->m_area.index(x, y, z);
430 content_t c = vm->m_data[vi].getContent();
431 if (ndef->get(c).walkable)
435 // N.B. It is necessary to search downward since range_heightmap[i]
436 // might not be the actual height, just the lowest part in the chunk
437 // where a ridge had been carved
438 u32 i = vm->m_area.index(x, y, z);
439 for (; y >= node_min.Y; y--) {
440 content_t c = vm->m_data[i].getContent();
441 if (ndef->get(c).walkable)
443 vm->m_area.add_y(em, i, -1);
446 if (y != node_min.Y - 1 && y >= water_level) {
447 ridge_heightmap[index] = y; //update ridgeheight
448 ntopnodes = biome->top_depth;
449 for (; y <= node_max.Y && ntopnodes; y++) {
451 vm->m_data[i] = MapNode(biome->c_top);
452 vm->m_area.add_y(em, i, 1);
454 // If dirt, grow grass on it.
455 if (vm->m_data[i].getContent() == CONTENT_AIR) {
456 vm->m_area.add_y(em, i, -1);
457 if (vm->m_data[i].getContent() == c_dirt)
458 vm->m_data[i] = MapNode(c_dirt_with_grass);
462 //////////////////// Now, add top nodes on top of the ridge
463 y = heightmap[index];
464 if (y > node_max.Y) {
465 y = node_max.Y; // Let's see if we can still go downward anyway
466 u32 vi = vm->m_area.index(x, y, z);
467 content_t c = vm->m_data[vi].getContent();
468 if (ndef->get(c).walkable)
472 i = vm->m_area.index(x, y, z);
473 for (; y >= node_min.Y; y--) {
474 content_t c = vm->m_data[i].getContent();
475 if (ndef->get(c).walkable)
477 vm->m_area.add_y(em, i, -1);
480 if (y != node_min.Y - 1) {
481 ntopnodes = biome->top_depth;
482 // Let's see if we've already added it...
483 if (y == ridge_heightmap[index] + ntopnodes - 1)
486 for (; y <= node_max.Y && ntopnodes; y++) {
488 vm->m_data[i] = MapNode(biome->c_top);
489 vm->m_area.add_y(em, i, 1);
491 // If dirt, grow grass on it.
492 if (vm->m_data[i].getContent() == CONTENT_AIR) {
493 vm->m_area.add_y(em, i, -1);
494 if (vm->m_data[i].getContent() == c_dirt)
495 vm->m_data[i] = MapNode(c_dirt_with_grass);
502 #include "mapgen_v6.h"
503 void MapgenV7::generateCaves(int max_stone_y) {
504 PseudoRandom ps(blockseed + 21343);
505 PseudoRandom ps2(blockseed + 1032);
507 int volume_nodes = (node_max.X - node_min.X + 1) *
508 (node_max.Y - node_min.Y + 1) * MAP_BLOCKSIZE;
509 float cave_amount = NoisePerlin2D(&nparams_v6_def_cave,
510 node_min.X, node_min.Y, seed);
512 u32 caves_count = MYMAX(0.0, cave_amount) * volume_nodes / 50000;
513 for (u32 i = 0; i < caves_count; i++) {
514 CaveV6 cave(this, &ps, &ps2, false, c_water_source, c_lava_source);
515 cave.makeCave(node_min, node_max, max_stone_y);
518 u32 bruises_count = (ps.range(1, 6) == 1) ? ps.range(0, ps.range(0, 2)) : 1;
519 for (u32 i = 0; i < bruises_count; i++) {
520 CaveV6 cave(this, &ps, &ps2, true, c_water_source, c_lava_source);
521 cave.makeCave(node_min, node_max, max_stone_y);