3 Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
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.
26 //#include "serverobject.h"
27 #include "content_sao.h"
29 #include "content_mapnode.h" // For content_mapnode_get_new_name
30 #include "voxelalgorithms.h"
32 #include "settings.h" // For g_settings
33 #include "main.h" // For g_profiler
35 #include "mapgen_v6.h"
37 /////////////////// Mapgen V6 perlin noise default values
38 NoiseParams nparams_v6_def_terrain_base =
39 {-AVERAGE_MUD_AMOUNT, 20.0, v3f(250.0, 250.0, 250.0), 82341, 5, 0.6};
40 NoiseParams nparams_v6_def_terrain_higher =
41 {20.0, 16.0, v3f(500.0, 500.0, 500.0), 85039, 5, 0.6};
42 NoiseParams nparams_v6_def_steepness =
43 {0.85, 0.5, v3f(125.0, 125.0, 125.0), -932, 5, 0.7};
44 NoiseParams nparams_v6_def_height_select =
45 {0.5, 1.0, v3f(250.0, 250.0, 250.0), 4213, 5, 0.69};
46 NoiseParams nparams_v6_def_trees =
47 {0.0, 1.0, v3f(125.0, 125.0, 125.0), 2, 4, 0.66};
48 NoiseParams nparams_v6_def_mud =
49 {AVERAGE_MUD_AMOUNT, 2.0, v3f(200.0, 200.0, 200.0), 91013, 3, 0.55};
50 NoiseParams nparams_v6_def_beach =
51 {0.0, 1.0, v3f(250.0, 250.0, 250.0), 59420, 3, 0.50};
52 NoiseParams nparams_v6_def_biome =
53 {0.0, 1.0, v3f(250.0, 250.0, 250.0), 9130, 3, 0.50};
54 NoiseParams nparams_v6_def_cave =
55 {6.0, 6.0, v3f(250.0, 250.0, 250.0), 34329, 3, 0.50};
58 ///////////////////////////////////////////////////////////////////////////////
61 MapgenV6::MapgenV6(int mapgenid, MapgenV6Params *params) {
62 this->generating = false;
65 this->seed = (int)params->seed;
66 this->water_level = params->water_level;
67 this->flags = params->flags;
68 this->csize = v3s16(1, 1, 1) * params->chunksize * MAP_BLOCKSIZE;
70 this->freq_desert = params->freq_desert;
71 this->freq_beach = params->freq_beach;
73 this->ystride = csize.X; //////fix this
75 np_cave = params->np_cave;
77 noise_terrain_base = new Noise(params->np_terrain_base, seed, csize.X, csize.Y);
78 noise_terrain_higher = new Noise(params->np_terrain_higher, seed, csize.X, csize.Y);
79 noise_steepness = new Noise(params->np_steepness, seed, csize.X, csize.Y);
80 noise_height_select = new Noise(params->np_height_select, seed, csize.X, csize.Y);
81 noise_trees = new Noise(params->np_trees, seed, csize.X, csize.Y);
82 noise_mud = new Noise(params->np_mud, seed, csize.X, csize.Y);
83 noise_beach = new Noise(params->np_beach, seed, csize.X, csize.Y);
84 noise_biome = new Noise(params->np_biome, seed, csize.X, csize.Y);
86 map_terrain_base = noise_terrain_base->result;
87 map_terrain_higher = noise_terrain_higher->result;
88 map_steepness = noise_steepness->result;
89 map_height_select = noise_height_select->result;
90 map_trees = noise_trees->result;
91 map_mud = noise_mud->result;
92 map_beach = noise_beach->result;
93 map_biome = noise_biome->result;
97 MapgenV6::~MapgenV6() {
98 delete noise_terrain_base;
99 delete noise_terrain_higher;
100 delete noise_steepness;
101 delete noise_height_select;
110 Some helper functions for the map generator
114 // Returns Y one under area minimum if not found
115 s16 MapgenV6::find_ground_level(VoxelManipulator &vmanip, v2s16 p2d,
116 INodeDefManager *ndef)
118 v3s16 em = vmanip.m_area.getExtent();
119 s16 y_nodes_max = vmanip.m_area.MaxEdge.Y;
120 s16 y_nodes_min = vmanip.m_area.MinEdge.Y;
121 u32 i = vmanip.m_area.index(v3s16(p2d.X, y_nodes_max, p2d.Y));
123 for(y=y_nodes_max; y>=y_nodes_min; y--)
125 MapNode &n = vmanip.m_data[i];
126 if(ndef->get(n).walkable)
129 vmanip.m_area.add_y(em, i, -1);
134 return y_nodes_min - 1;
137 // Returns Y one under area minimum if not found
138 s16 MapgenV6::find_stone_level(VoxelManipulator &vmanip, v2s16 p2d,
139 INodeDefManager *ndef)
141 v3s16 em = vmanip.m_area.getExtent();
142 s16 y_nodes_max = vmanip.m_area.MaxEdge.Y;
143 s16 y_nodes_min = vmanip.m_area.MinEdge.Y;
144 u32 i = vmanip.m_area.index(v3s16(p2d.X, y_nodes_max, p2d.Y));
146 content_t c_stone = ndef->getId("mapgen_stone");
147 content_t c_desert_stone = ndef->getId("mapgen_desert_stone");
148 for(y=y_nodes_max; y>=y_nodes_min; y--)
150 MapNode &n = vmanip.m_data[i];
151 content_t c = n.getContent();
152 if(c != CONTENT_IGNORE && (
153 c == c_stone || c == c_desert_stone))
156 vmanip.m_area.add_y(em, i, -1);
161 return y_nodes_min - 1;
165 void MapgenV6::make_tree(ManualMapVoxelManipulator &vmanip, v3s16 p0,
166 bool is_apple_tree, INodeDefManager *ndef)
168 MapNode treenode(ndef->getId("mapgen_tree"));
169 MapNode leavesnode(ndef->getId("mapgen_leaves"));
170 MapNode applenode(ndef->getId("mapgen_apple"));
172 s16 trunk_h = myrand_range(4, 5);
174 for(s16 ii=0; ii<trunk_h; ii++)
176 if(vmanip.m_area.contains(p1))
177 vmanip.m_data[vmanip.m_area.index(p1)] = treenode;
181 // p1 is now the last piece of the trunk
184 VoxelArea leaves_a(v3s16(-2,-1,-2), v3s16(2,2,2));
185 //SharedPtr<u8> leaves_d(new u8[leaves_a.getVolume()]);
186 Buffer<u8> leaves_d(leaves_a.getVolume());
187 for(s32 i=0; i<leaves_a.getVolume(); i++)
190 // Force leaves at near the end of the trunk
193 for(s16 z=-d; z<=d; z++)
194 for(s16 y=-d; y<=d; y++)
195 for(s16 x=-d; x<=d; x++)
197 leaves_d[leaves_a.index(v3s16(x,y,z))] = 1;
201 // Add leaves randomly
202 for(u32 iii=0; iii<7; iii++)
207 myrand_range(leaves_a.MinEdge.X, leaves_a.MaxEdge.X-d),
208 myrand_range(leaves_a.MinEdge.Y, leaves_a.MaxEdge.Y-d),
209 myrand_range(leaves_a.MinEdge.Z, leaves_a.MaxEdge.Z-d)
212 for(s16 z=0; z<=d; z++)
213 for(s16 y=0; y<=d; y++)
214 for(s16 x=0; x<=d; x++)
216 leaves_d[leaves_a.index(p+v3s16(x,y,z))] = 1;
220 // Blit leaves to vmanip
221 for(s16 z=leaves_a.MinEdge.Z; z<=leaves_a.MaxEdge.Z; z++)
222 for(s16 y=leaves_a.MinEdge.Y; y<=leaves_a.MaxEdge.Y; y++)
223 for(s16 x=leaves_a.MinEdge.X; x<=leaves_a.MaxEdge.X; x++)
227 if(vmanip.m_area.contains(p) == false)
229 u32 vi = vmanip.m_area.index(p);
230 if(vmanip.m_data[vi].getContent() != CONTENT_AIR
231 && vmanip.m_data[vi].getContent() != CONTENT_IGNORE)
233 u32 i = leaves_a.index(x,y,z);
234 if(leaves_d[i] == 1) {
235 bool is_apple = myrand_range(0,99) < 10;
236 if(is_apple_tree && is_apple) {
237 vmanip.m_data[vi] = applenode;
239 vmanip.m_data[vi] = leavesnode;
247 Noise functions. Make sure seed is mangled differently in each one.
251 // Amount of trees per area in nodes
252 double MapgenV6::tree_amount_2d(u64 seed, v2s16 p)
254 /*double noise = noise2d_perlin(
255 0.5+(float)p.X/125, 0.5+(float)p.Y/125,
257 double noise = map_trees[(p.Y - node_min.Z) * ystride + (p.X - node_min.X)];
258 double zeroval = -0.39;
262 return 0.04 * (noise-zeroval) / (1.0-zeroval);
265 // Required by mapgen.h
266 bool MapgenV6::block_is_underground(u64 seed, v3s16 blockpos)
268 /*s16 minimum_groundlevel = (s16)get_sector_minimum_ground_level(
269 seed, v2s16(blockpos.X, blockpos.Z));*/
270 // Nah, this is just a heuristic, just return something
271 s16 minimum_groundlevel = water_level;
273 if(blockpos.Y*MAP_BLOCKSIZE + MAP_BLOCKSIZE <= minimum_groundlevel)
280 double MapgenV6::base_rock_level_2d(u64 seed, v2s16 p)
285 int index = (p.Y - node_min.Z) * ystride + (p.X - node_min.X);
287 // The base ground level
288 /*double base = (double)WATER_LEVEL - (double)AVERAGE_MUD_AMOUNT
289 + 20. * noise2d_perlin(
290 0.5+(float)p.X/250., 0.5+(float)p.Y/250.,
291 seed+82341, 5, 0.6);*/
292 double base = water_level + map_terrain_base[index];
294 // Higher ground level
295 /*double higher = (double)WATER_LEVEL + 20. + 16. * noise2d_perlin(
296 0.5+(float)p.X/500., 0.5+(float)p.Y/500.,
297 seed+85039, 5, 0.6);*/
298 double higher = water_level + map_terrain_higher[index];
300 // Limit higher to at least base
304 // Steepness factor of cliffs
305 /*double b = 0.85 + 0.5 * noise2d_perlin(
306 0.5+(float)p.X/125., 0.5+(float)p.Y/125.,
308 double b = map_steepness[index];
309 b = rangelim(b, 0.0, 1000.0);
312 b = rangelim(b, 0.5, 1000.0);
314 // Values 1.5...100 give quite horrible looking slopes
315 if(b > 1.5 && b < 100.0){
322 // Offset to more low
323 double a_off = -0.20;
326 /*double a = (double)0.5 + b * (a_off + noise2d_perlin(
327 0.5+(float)p.X/250., 0.5+(float)p.Y/250.,
328 seed+4213, 5, 0.69));*/
329 double a = 0.5 + b * (a_off + map_height_select[index]);
332 a = rangelim(a, 0.0, 1.0);
334 double h = base*(1.0-a) + higher*a;
339 double MapgenV6::baseRockLevelFromNoise(v2s16 p) {
343 double base = water_level +
344 NoisePerlin2DPosOffset(noise_terrain_base->np, p.X, 0.5, p.Y, 0.5, seed);
345 double higher = water_level +
346 NoisePerlin2DPosOffset(noise_terrain_higher->np, p.X, 0.5, p.Y, 0.5, seed);
351 double b = NoisePerlin2DPosOffset(noise_steepness->np, p.X, 0.5, p.Y, 0.5, seed);
352 b = rangelim(b, 0.0, 1000.0);
355 b = rangelim(b, 0.5, 1000.0);
357 if(b > 1.5 && b < 100.0){
364 double a_off = -0.20;
365 double a = 0.5 + b * (a_off + NoisePerlin2DNoTxfmPosOffset(
366 noise_height_select->np, p.X, 0.5, p.Y, 0.5, seed));
367 a = rangelim(a, 0.0, 1.0);
369 return base * (1.0 - a) + higher * a;
373 s16 MapgenV6::find_ground_level_from_noise(u64 seed, v2s16 p2d, s16 precision)
375 return baseRockLevelFromNoise(p2d) + AVERAGE_MUD_AMOUNT;
378 double MapgenV6::get_mud_add_amount(u64 seed, v2s16 p)
381 return AVERAGE_MUD_AMOUNT;
383 /*return ((float)AVERAGE_MUD_AMOUNT + 2.0 * noise2d_perlin(
384 0.5+(float)p.X/200, 0.5+(float)p.Y/200,
385 seed+91013, 3, 0.55));*/
386 int index = (p.Y - node_min.Z) * ystride + (p.X - node_min.X);
387 return map_mud[index];
390 bool MapgenV6::get_have_beach(u64 seed, v2s16 p2d)
392 // Determine whether to have sand here
393 /*double sandnoise = noise2d_perlin(
394 0.2+(float)p2d.X/250, 0.7+(float)p2d.Y/250,
395 seed+59420, 3, 0.50);*/
396 int index = (p2d.Y - node_min.Z) * ystride + (p2d.X - node_min.X);
397 double sandnoise = map_beach[index];
399 return (sandnoise > freq_beach);
402 BiomeType MapgenV6::get_biome(u64 seed, v2s16 p2d)
404 // Just do something very simple as for now
405 /*double d = noise2d_perlin(
406 0.6+(float)p2d.X/250, 0.2+(float)p2d.Y/250,
407 seed+9130, 3, 0.50);*/
408 int index = (p2d.Y - node_min.Z) * ystride + (p2d.X - node_min.X);
409 double d = map_biome[index];
412 if (flags & MGV6_BIOME_BLEND) {
413 if(d > freq_desert - 0.10 &&
414 (noise2d(p2d.X, p2d.Y, seed) + 1.0) > (freq_desert - d) * 20.0)
420 u32 MapgenV6::get_blockseed(u64 seed, v3s16 p)
422 s32 x=p.X, y=p.Y, z=p.Z;
423 return (u32)(seed%0x100000000ULL) + z*38134234 + y*42123 + x*23;
427 int MapgenV6::getGroundLevelAtPoint(v2s16 p) {
428 return baseRockLevelFromNoise(p) + AVERAGE_MUD_AMOUNT;
431 #define VMANIP_FLAG_CAVE VOXELFLAG_CHECKED1
433 void MapgenV6::makeChunk(BlockMakeData *data)
437 //dstream<<"makeBlock: no-op"<<std::endl;
441 this->generating = true;
443 assert(data->vmanip);
444 assert(data->nodedef);
445 assert(data->blockpos_requested.X >= data->blockpos_min.X &&
446 data->blockpos_requested.Y >= data->blockpos_min.Y &&
447 data->blockpos_requested.Z >= data->blockpos_min.Z);
448 assert(data->blockpos_requested.X <= data->blockpos_max.X &&
449 data->blockpos_requested.Y <= data->blockpos_max.Y &&
450 data->blockpos_requested.Z <= data->blockpos_max.Z);
452 INodeDefManager *ndef = data->nodedef;
454 // Hack: use minimum block coordinates for old code that assumes
456 v3s16 blockpos = data->blockpos_requested;
458 /*dstream<<"makeBlock(): ("<<blockpos.X<<","<<blockpos.Y<<","
459 <<blockpos.Z<<")"<<std::endl;*/
461 v3s16 blockpos_min = data->blockpos_min;
462 v3s16 blockpos_max = data->blockpos_max;
463 v3s16 blockpos_full_min = blockpos_min - v3s16(1,1,1);
464 v3s16 blockpos_full_max = blockpos_max + v3s16(1,1,1);
466 ManualMapVoxelManipulator &vmanip = *(data->vmanip);
467 // Area of central chunk
468 node_min = blockpos_min*MAP_BLOCKSIZE;
469 node_max = (blockpos_max+v3s16(1,1,1))*MAP_BLOCKSIZE-v3s16(1,1,1);
470 // Full allocated area
471 v3s16 full_node_min = (blockpos_min-1)*MAP_BLOCKSIZE;
472 v3s16 full_node_max = (blockpos_max+2)*MAP_BLOCKSIZE-v3s16(1,1,1);
474 v3s16 central_area_size = node_max - node_min + v3s16(1,1,1);
476 const s16 max_spread_amount = MAP_BLOCKSIZE;
478 int volume_blocks = (blockpos_max.X - blockpos_min.X + 1)
479 * (blockpos_max.Y - blockpos_min.Y + 1)
480 * (blockpos_max.Z - blockpos_max.Z + 1);
482 int volume_nodes = volume_blocks *
483 MAP_BLOCKSIZE*MAP_BLOCKSIZE*MAP_BLOCKSIZE;
485 // Generated surface area
486 //double gen_area_nodes = MAP_BLOCKSIZE*MAP_BLOCKSIZE * rel_volume;
488 // Horribly wrong heuristic, but better than nothing
489 bool block_is_underground = (water_level > node_max.Y);
492 Create a block-specific seed
494 u32 blockseed = get_blockseed(data->seed, full_node_min);
503 // Need to adjust for the original implementation's +.5 offset...
504 if (!(flags & MG_FLAT)) {
505 noise_terrain_base->perlinMap2D(
506 x + 0.5 * noise_terrain_base->np->spread.X,
507 z + 0.5 * noise_terrain_base->np->spread.Z);
508 noise_terrain_base->transformNoiseMap();
510 noise_terrain_higher->perlinMap2D(
511 x + 0.5 * noise_terrain_higher->np->spread.X,
512 z + 0.5 * noise_terrain_higher->np->spread.Z);
513 noise_terrain_higher->transformNoiseMap();
515 noise_steepness->perlinMap2D(
516 x + 0.5 * noise_steepness->np->spread.X,
517 z + 0.5 * noise_steepness->np->spread.Z);
518 noise_steepness->transformNoiseMap();
520 noise_height_select->perlinMap2D(
521 x + 0.5 * noise_height_select->np->spread.X,
522 z + 0.5 * noise_height_select->np->spread.Z);
525 noise_trees->perlinMap2D(
526 x + 0.5 * noise_trees->np->spread.X,
527 z + 0.5 * noise_trees->np->spread.Z);
529 if (!(flags & MG_FLAT)) {
530 noise_mud->perlinMap2D(
531 x + 0.5 * noise_mud->np->spread.X,
532 z + 0.5 * noise_mud->np->spread.Z);
533 noise_mud->transformNoiseMap();
535 noise_beach->perlinMap2D(
536 x + 0.2 * noise_beach->np->spread.X,
537 z + 0.7 * noise_beach->np->spread.Z);
539 noise_biome->perlinMap2D(
540 x + 0.6 * noise_biome->np->spread.X,
541 z + 0.2 * noise_biome->np->spread.Z);
546 Cache some ground type values for speed
549 // Creates variables c_name=id and n_name=node
550 #define CONTENT_VARIABLE(ndef, name)\
551 content_t c_##name = ndef->getId("mapgen_" #name);\
552 MapNode n_##name(c_##name);
553 // Default to something else if was CONTENT_IGNORE
554 #define CONTENT_VARIABLE_FALLBACK(name, dname)\
555 if(c_##name == CONTENT_IGNORE){\
556 c_##name = c_##dname;\
557 n_##name = n_##dname;\
560 CONTENT_VARIABLE(ndef, stone);
561 CONTENT_VARIABLE(ndef, air);
562 CONTENT_VARIABLE(ndef, water_source);
563 CONTENT_VARIABLE(ndef, dirt);
564 CONTENT_VARIABLE(ndef, sand);
565 CONTENT_VARIABLE(ndef, gravel);
566 CONTENT_VARIABLE(ndef, clay);
567 CONTENT_VARIABLE(ndef, lava_source);
568 CONTENT_VARIABLE(ndef, cobble);
569 CONTENT_VARIABLE(ndef, mossycobble);
570 CONTENT_VARIABLE(ndef, dirt_with_grass);
571 CONTENT_VARIABLE(ndef, junglegrass);
572 CONTENT_VARIABLE(ndef, stone_with_coal);
573 CONTENT_VARIABLE(ndef, stone_with_iron);
574 CONTENT_VARIABLE(ndef, mese);
575 CONTENT_VARIABLE(ndef, desert_sand);
576 CONTENT_VARIABLE_FALLBACK(desert_sand, sand);
577 CONTENT_VARIABLE(ndef, desert_stone);
578 CONTENT_VARIABLE_FALLBACK(desert_stone, stone);
580 // Maximum height of the stone surface and obstacles.
581 // This is used to guide the cave generation
582 s16 stone_surface_max_y = 0;
585 Generate general ground level to full area
589 TimeTaker timer1("Generating ground level");
591 for(s16 x=node_min.X; x<=node_max.X; x++)
592 for(s16 z=node_min.Z; z<=node_max.Z; z++)
595 v2s16 p2d = v2s16(x,z);
598 Skip of already generated
601 v3s16 p(p2d.X, node_min.Y, p2d.Y);
602 if(vmanip.m_data[vmanip.m_area.index(p)].d != CONTENT_AIR)
606 // Ground height at this point
607 float surface_y_f = 0.0;
609 // Use perlin noise for ground height
610 surface_y_f = base_rock_level_2d(data->seed, p2d);
612 /*// Experimental stuff
614 float a = highlands_level_2d(data->seed, p2d);
619 // Convert to integer
620 s16 surface_y = (s16)surface_y_f;
623 if(surface_y > stone_surface_max_y)
624 stone_surface_max_y = surface_y;
626 BiomeType bt = get_biome(data->seed, p2d);
628 Fill ground with stone
631 // Use fast index incrementing
632 v3s16 em = vmanip.m_area.getExtent();
633 u32 i = vmanip.m_area.index(v3s16(p2d.X, node_min.Y, p2d.Y));
634 for(s16 y=node_min.Y; y<=node_max.Y; y++)
636 if(vmanip.m_data[i].getContent() == CONTENT_IGNORE){
638 if(y > water_level && bt == BT_DESERT)
639 vmanip.m_data[i] = n_desert_stone;
641 vmanip.m_data[i] = n_stone;
642 } else if(y <= water_level){
643 vmanip.m_data[i] = MapNode(c_water_source);
645 vmanip.m_data[i] = MapNode(c_air);
648 vmanip.m_area.add_y(em, i, 1);
656 // Limit dirt flow area by 1 because mud is flown into neighbors.
657 assert(central_area_size.X == central_area_size.Z);
658 s16 mudflow_minpos = 0-max_spread_amount+1;
659 s16 mudflow_maxpos = central_area_size.X+max_spread_amount-2;
662 Loop this part, it will make stuff look older and newer nicely
665 /*double cave_amount = 6.0 + 6.0 * noise2d_perlin(
666 0.5+(double)node_min.X/250, 0.5+(double)node_min.Y/250,
667 data->seed+34329, 3, 0.50);*/
669 double cave_amount = NoisePerlin2D(np_cave, node_min.X, node_min.Y, data->seed);
671 const u32 age_loops = 2;
672 for(u32 i_age=0; i_age<age_loops; i_age++)
674 /******************************
675 BEGINNING OF AGING LOOP
676 ******************************/
681 //TimeTaker timer1("caves");
684 Make caves (this code is relatively horrible)
686 cave_amount = MYMAX(0.0, cave_amount);
687 u32 caves_count = cave_amount * volume_nodes / 50000;
688 u32 bruises_count = 1;
689 PseudoRandom ps(blockseed+21343);
690 PseudoRandom ps2(blockseed+1032);
691 if(ps.range(1, 6) == 1)
692 bruises_count = ps.range(0, ps.range(0, 2));
693 if(get_biome(data->seed, v2s16(node_min.X, node_min.Z)) == BT_DESERT){
697 for(u32 jj=0; jj<caves_count+bruises_count; jj++)
699 if (!(flags & MG_CAVES))
702 /*int avg_height = (int)
703 ((base_rock_level_2d(data->seed, v2s16(node_min.X, node_min.Z)) +
704 base_rock_level_2d(data->seed, v2s16(node_max.X, node_max.Z))) / 2);
705 if ((node_max.Y + node_min.Y) / 2 > avg_height)
708 bool large_cave = (jj >= caves_count);
709 s16 min_tunnel_diameter = 2;
710 s16 max_tunnel_diameter = ps.range(2,6);
711 int dswitchint = ps.range(1,14);
712 u16 tunnel_routepoints = 0;
713 int part_max_length_rs = 0;
715 part_max_length_rs = ps.range(2,4);
716 tunnel_routepoints = ps.range(5, ps.range(15,30));
717 min_tunnel_diameter = 5;
718 max_tunnel_diameter = ps.range(7, ps.range(8,24));
720 part_max_length_rs = ps.range(2,9);
721 tunnel_routepoints = ps.range(10, ps.range(15,30));
723 bool large_cave_is_flat = (ps.range(0,1) == 0);
725 v3f main_direction(0,0,0);
727 // Allowed route area size in nodes
728 v3s16 ar = central_area_size;
730 // Area starting point in nodes
734 //(this should be more than the maximum radius of the tunnel)
736 s16 more = max_spread_amount - max_tunnel_diameter/2 - insure;
737 ar += v3s16(1,0,1) * more * 2;
738 of -= v3s16(1,0,1) * more;
741 // Allow half a diameter + 7 over stone surface
742 s16 route_y_max = -of.Y + stone_surface_max_y + max_tunnel_diameter/2 + 7;
744 // Limit maximum to area
745 route_y_max = rangelim(route_y_max, 0, ar.Y-1);
750 if(node_min.Y < water_level && node_max.Y > water_level)
752 min = water_level - max_tunnel_diameter/3 - of.Y;
753 route_y_max = water_level + max_tunnel_diameter/3 - of.Y;
755 route_y_min = ps.range(min, min + max_tunnel_diameter);
756 route_y_min = rangelim(route_y_min, 0, route_y_max);
759 s16 route_start_y_min = route_y_min;
760 s16 route_start_y_max = route_y_max;
762 route_start_y_min = rangelim(route_start_y_min, 0, ar.Y-1);
763 route_start_y_max = rangelim(route_start_y_max, route_start_y_min, ar.Y-1);
765 // Randomize starting position
767 (float)(ps.next()%ar.X)+0.5,
768 (float)(ps.range(route_start_y_min, route_start_y_max))+0.5,
769 (float)(ps.next()%ar.Z)+0.5
772 v3s16 startp(orp.X, orp.Y, orp.Z);
775 MapNode airnode(CONTENT_AIR);
776 MapNode waternode(c_water_source);
777 MapNode lavanode(c_lava_source);
780 Generate some tunnel starting from orp
783 for(u16 j=0; j<tunnel_routepoints; j++)
785 if(j%dswitchint==0 && large_cave == false)
787 main_direction = v3f(
788 ((float)(ps.next()%20)-(float)10)/10,
789 ((float)(ps.next()%20)-(float)10)/30,
790 ((float)(ps.next()%20)-(float)10)/10
792 main_direction *= (float)ps.range(0, 10)/10;
796 s16 min_d = min_tunnel_diameter;
797 s16 max_d = max_tunnel_diameter;
798 s16 rs = ps.range(min_d, max_d);
800 // Every second section is rough
801 bool randomize_xz = (ps2.range(1,2) == 1);
807 rs*part_max_length_rs,
808 rs*part_max_length_rs/2,
809 rs*part_max_length_rs
815 rs*part_max_length_rs,
816 ps.range(1, rs*part_max_length_rs),
817 rs*part_max_length_rs
824 (float)(ps.next()%(maxlen.X*1))-(float)maxlen.X/2,
825 (float)(ps.next()%(maxlen.Y*1))-(float)maxlen.Y/2,
826 (float)(ps.next()%(maxlen.Z*1))-(float)maxlen.Z/2
829 // Jump downward sometimes
830 if(!large_cave && ps.range(0,12) == 0)
833 (float)(ps.next()%(maxlen.X*1))-(float)maxlen.X/2,
834 (float)(ps.next()%(maxlen.Y*2))-(float)maxlen.Y*2/2,
835 (float)(ps.next()%(maxlen.Z*1))-(float)maxlen.Z/2
841 s16 h = find_ground_level_clever(vmanip,
842 v2s16(p.X, p.Z), ndef);
843 route_y_min = h - rs/3;
844 route_y_max = h + rs;
847 vec += main_direction;
852 else if(rp.X >= ar.X)
854 if(rp.Y < route_y_min)
856 else if(rp.Y >= route_y_max)
857 rp.Y = route_y_max-1;
860 else if(rp.Z >= ar.Z)
864 for(float f=0; f<1.0; f+=1.0/vec.getLength())
866 v3f fp = orp + vec * f;
867 fp.X += 0.1*ps.range(-10,10);
868 fp.Z += 0.1*ps.range(-10,10);
869 v3s16 cp(fp.X, fp.Y, fp.Z);
874 d0 += ps.range(-1,1);
875 d1 += ps.range(-1,1);
877 for(s16 z0=d0; z0<=d1; z0++)
879 s16 si = rs/2 - MYMAX(0, abs(z0)-rs/7-1);
880 for(s16 x0=-si-ps.range(0,1); x0<=si-1+ps.range(0,1); x0++)
882 s16 maxabsxz = MYMAX(abs(x0), abs(z0));
883 s16 si2 = rs/2 - MYMAX(0, maxabsxz-rs/7-1);
884 for(s16 y0=-si2; y0<=si2; y0++)
886 /*// Make better floors in small caves
887 if(y0 <= -rs/2 && rs<=7)
889 if(large_cave_is_flat){
890 // Make large caves not so tall
891 if(rs > 7 && abs(y0) >= rs/3)
901 if(vmanip.m_area.contains(p) == false)
904 u32 i = vmanip.m_area.index(p);
908 if(full_node_min.Y < water_level &&
909 full_node_max.Y > water_level){
910 if(p.Y <= water_level)
911 vmanip.m_data[i] = waternode;
913 vmanip.m_data[i] = airnode;
914 } else if(full_node_max.Y < water_level){
915 if(p.Y < startp.Y - 2)
916 vmanip.m_data[i] = lavanode;
918 vmanip.m_data[i] = airnode;
920 vmanip.m_data[i] = airnode;
923 // Don't replace air or water or lava or ignore
924 if(vmanip.m_data[i].getContent() == CONTENT_IGNORE ||
925 vmanip.m_data[i].getContent() == CONTENT_AIR ||
926 vmanip.m_data[i].getContent() == c_water_source ||
927 vmanip.m_data[i].getContent() == c_lava_source)
930 vmanip.m_data[i] = airnode;
933 vmanip.m_flags[i] |= VMANIP_FLAG_CAVE;
951 TimeTaker timer1("add mud");
954 Add mud to the central chunk
957 for(s16 x=node_min.X; x<=node_max.X; x++)
958 for(s16 z=node_min.Z; z<=node_max.Z; z++)
960 // Node position in 2d
961 v2s16 p2d = v2s16(x,z);
963 // Randomize mud amount
964 s16 mud_add_amount = get_mud_add_amount(data->seed, p2d) / 2.0 + 0.5;
967 s16 surface_y = find_stone_level(vmanip, p2d, ndef);
968 // Handle area not found
969 if(surface_y == vmanip.m_area.MinEdge.Y - 1)
972 MapNode addnode(c_dirt);
973 BiomeType bt = get_biome(data->seed, p2d);
976 addnode = MapNode(c_desert_sand);
978 if(bt == BT_DESERT && surface_y + mud_add_amount <= water_level+1){
979 addnode = MapNode(c_sand);
980 } else if(mud_add_amount <= 0){
981 mud_add_amount = 1 - mud_add_amount;
982 addnode = MapNode(c_gravel);
983 } else if(bt == BT_NORMAL && get_have_beach(data->seed, p2d) &&
984 surface_y + mud_add_amount <= water_level+2){
985 addnode = MapNode(c_sand);
990 mud_add_amount = MYMAX(0, mud_add_amount - (surface_y - 20)/5);
995 If topmost node is grass, change it to mud.
996 It might be if it was flown to there from a neighboring
997 chunk and then converted.
1000 u32 i = vmanip.m_area.index(v3s16(p2d.X, surface_y, p2d.Y));
1001 MapNode *n = &vmanip.m_data[i];
1002 if(n->getContent() == c_dirt_with_grass)
1003 *n = MapNode(c_dirt);
1011 v3s16 em = vmanip.m_area.getExtent();
1012 s16 y_start = surface_y+1;
1013 u32 i = vmanip.m_area.index(v3s16(p2d.X, y_start, p2d.Y));
1014 for(s16 y=y_start; y<=node_max.Y; y++)
1016 if(mudcount >= mud_add_amount)
1019 MapNode &n = vmanip.m_data[i];
1023 vmanip.m_area.add_y(em, i, 1);
1033 Add blobs of dirt and gravel underground
1035 if(get_biome(data->seed, v2s16(node_min.X, node_min.Z)) == BT_NORMAL)
1037 PseudoRandom pr(blockseed+983);
1038 for(int i=0; i<volume_nodes/10/10/10; i++)
1040 bool only_fill_cave = (myrand_range(0,1) != 0);
1047 pr.range(node_min.X, node_max.X)-size.X/2,
1048 pr.range(node_min.Y, node_max.Y)-size.Y/2,
1049 pr.range(node_min.Z, node_max.Z)-size.Z/2
1052 if(p0.Y > -32 && pr.range(0,1) == 0)
1053 n1 = MapNode(c_dirt);
1055 n1 = MapNode(c_gravel);
1056 for(int x1=0; x1<size.X; x1++)
1057 for(int y1=0; y1<size.Y; y1++)
1058 for(int z1=0; z1<size.Z; z1++)
1060 v3s16 p = p0 + v3s16(x1,y1,z1);
1061 u32 i = vmanip.m_area.index(p);
1062 if(!vmanip.m_area.contains(i))
1064 // Cancel if not stone and not cave air
1065 if(vmanip.m_data[i].getContent() != c_stone &&
1066 !(vmanip.m_flags[i] & VMANIP_FLAG_CAVE))
1068 if(only_fill_cave && !(vmanip.m_flags[i] & VMANIP_FLAG_CAVE))
1070 vmanip.m_data[i] = n1;
1078 TimeTaker timer1("flow mud");
1081 Flow mud away from steep edges
1084 // Iterate a few times
1085 for(s16 k=0; k<3; k++)
1088 for(s16 x=mudflow_minpos; x<=mudflow_maxpos; x++)
1089 for(s16 z=mudflow_minpos; z<=mudflow_maxpos; z++)
1091 // Invert coordinates every 2nd iteration
1094 x = mudflow_maxpos - (x-mudflow_minpos);
1095 z = mudflow_maxpos - (z-mudflow_minpos);
1098 // Node position in 2d
1099 v2s16 p2d = v2s16(node_min.X, node_min.Z) + v2s16(x,z);
1101 v3s16 em = vmanip.m_area.getExtent();
1102 u32 i = vmanip.m_area.index(v3s16(p2d.X, node_max.Y, p2d.Y));
1105 while(y >= node_min.Y)
1112 for(; y>=node_min.Y; y--)
1114 n = &vmanip.m_data[i];
1115 //if(content_walkable(n->d))
1117 if(n->getContent() == c_dirt ||
1118 n->getContent() == c_dirt_with_grass ||
1119 n->getContent() == c_gravel)
1122 vmanip.m_area.add_y(em, i, -1);
1125 // Stop if out of area
1126 //if(vmanip.m_area.contains(i) == false)
1130 /*// If not mud, do nothing to it
1131 MapNode *n = &vmanip.m_data[i];
1132 if(n->d != CONTENT_MUD && n->d != CONTENT_GRASS)
1135 if(n->getContent() == c_dirt ||
1136 n->getContent() == c_dirt_with_grass)
1138 // Make it exactly mud
1139 n->setContent(c_dirt);
1142 Don't flow it if the stuff under it is not mud
1146 vmanip.m_area.add_y(em, i2, -1);
1147 // Cancel if out of area
1148 if(vmanip.m_area.contains(i2) == false)
1150 MapNode *n2 = &vmanip.m_data[i2];
1151 if(n2->getContent() != c_dirt &&
1152 n2->getContent() != c_dirt_with_grass)
1157 /*s16 recurse_count = 0;
1161 v3s16(0,0,1), // back
1162 v3s16(1,0,0), // right
1163 v3s16(0,0,-1), // front
1164 v3s16(-1,0,0), // left
1167 // Theck that upper is air or doesn't exist.
1168 // Cancel dropping if upper keeps it in place
1170 vmanip.m_area.add_y(em, i3, 1);
1171 if(vmanip.m_area.contains(i3) == true
1172 && ndef->get(vmanip.m_data[i3]).walkable)
1179 for(u32 di=0; di<4; di++)
1181 v3s16 dirp = dirs4[di];
1184 vmanip.m_area.add_p(em, i2, dirp);
1185 // Fail if out of area
1186 if(vmanip.m_area.contains(i2) == false)
1188 // Check that side is air
1189 MapNode *n2 = &vmanip.m_data[i2];
1190 if(ndef->get(*n2).walkable)
1192 // Check that under side is air
1193 vmanip.m_area.add_y(em, i2, -1);
1194 if(vmanip.m_area.contains(i2) == false)
1196 n2 = &vmanip.m_data[i2];
1197 if(ndef->get(*n2).walkable)
1199 /*// Check that under that is air (need a drop of 2)
1200 vmanip.m_area.add_y(em, i2, -1);
1201 if(vmanip.m_area.contains(i2) == false)
1203 n2 = &vmanip.m_data[i2];
1204 if(content_walkable(n2->d))
1206 // Loop further down until not air
1207 bool dropped_to_unknown = false;
1209 vmanip.m_area.add_y(em, i2, -1);
1210 n2 = &vmanip.m_data[i2];
1211 // if out of known area
1212 if(vmanip.m_area.contains(i2) == false
1213 || n2->getContent() == CONTENT_IGNORE){
1214 dropped_to_unknown = true;
1217 }while(ndef->get(*n2).walkable == false);
1218 // Loop one up so that we're in air
1219 vmanip.m_area.add_y(em, i2, 1);
1220 n2 = &vmanip.m_data[i2];
1222 bool old_is_water = (n->getContent() == c_water_source);
1223 // Move mud to new place
1224 if(!dropped_to_unknown) {
1226 // Set old place to be air (or water)
1228 *n = MapNode(c_water_source);
1230 *n = MapNode(CONTENT_AIR);
1246 /***********************
1248 ************************/
1251 Add top and bottom side of water to transforming_liquid queue
1254 for(s16 x=full_node_min.X; x<=full_node_max.X; x++)
1255 for(s16 z=full_node_min.Z; z<=full_node_max.Z; z++)
1260 bool water_found = false;
1261 // Use fast index incrementing
1262 v3s16 em = vmanip.m_area.getExtent();
1263 u32 i = vmanip.m_area.index(v3s16(p2d.X, full_node_max.Y, p2d.Y));
1264 for(s16 y=full_node_max.Y; y>=full_node_min.Y; y--)
1266 if(y == full_node_max.Y){
1268 (vmanip.m_data[i].getContent() == c_water_source ||
1269 vmanip.m_data[i].getContent() == c_lava_source);
1271 else if(water_found == false)
1273 if(vmanip.m_data[i].getContent() == c_water_source ||
1274 vmanip.m_data[i].getContent() == c_lava_source)
1276 v3s16 p = v3s16(p2d.X, y, p2d.Y);
1277 data->transforming_liquid.push_back(p);
1283 // This can be done because water_found can only
1284 // turn to true and end up here after going through
1286 if(vmanip.m_data[i+1].getContent() != c_water_source ||
1287 vmanip.m_data[i+1].getContent() != c_lava_source)
1289 v3s16 p = v3s16(p2d.X, y+1, p2d.Y);
1290 data->transforming_liquid.push_back(p);
1291 water_found = false;
1295 vmanip.m_area.add_y(em, i, -1);
1304 for(s16 x=full_node_min.X; x<=full_node_max.X; x++)
1305 for(s16 z=full_node_min.Z; z<=full_node_max.Z; z++)
1307 // Node position in 2d
1308 v2s16 p2d = v2s16(x,z);
1311 Find the lowest surface to which enough light ends up
1314 Basically just wait until not air and not leaves.
1318 v3s16 em = vmanip.m_area.getExtent();
1319 u32 i = vmanip.m_area.index(v3s16(p2d.X, node_max.Y, p2d.Y));
1321 // Go to ground level
1322 for(y=node_max.Y; y>=full_node_min.Y; y--)
1324 MapNode &n = vmanip.m_data[i];
1325 if(ndef->get(n).param_type != CPT_LIGHT
1326 || ndef->get(n).liquid_type != LIQUID_NONE)
1328 vmanip.m_area.add_y(em, i, -1);
1330 if(y >= full_node_min.Y)
1333 surface_y = full_node_min.Y;
1336 u32 i = vmanip.m_area.index(p2d.X, surface_y, p2d.Y);
1337 MapNode *n = &vmanip.m_data[i];
1338 if(n->getContent() == c_dirt){
1339 // Well yeah, this can't be overground...
1340 if(surface_y < water_level - 20)
1342 n->setContent(c_dirt_with_grass);
1349 assert(central_area_size.X == central_area_size.Z);
1350 if (flags & MG_TREES) {
1351 // Divide area into parts
1353 s16 sidelen = central_area_size.X / div;
1354 double area = sidelen * sidelen;
1355 for(s16 x0=0; x0<div; x0++)
1356 for(s16 z0=0; z0<div; z0++)
1358 // Center position of part of division
1360 node_min.X + sidelen/2 + sidelen*x0,
1361 node_min.Z + sidelen/2 + sidelen*z0
1363 // Minimum edge of part of division
1365 node_min.X + sidelen*x0,
1366 node_min.Z + sidelen*z0
1368 // Maximum edge of part of division
1370 node_min.X + sidelen + sidelen*x0 - 1,
1371 node_min.Z + sidelen + sidelen*z0 - 1
1374 u32 tree_count = area * tree_amount_2d(data->seed, p2d_center);
1375 // Put trees in random places on part of division
1376 for(u32 i=0; i<tree_count; i++)
1378 s16 x = myrand_range(p2d_min.X, p2d_max.X);
1379 s16 z = myrand_range(p2d_min.Y, p2d_max.Y);
1380 s16 y = find_ground_level(vmanip, v2s16(x,z), ndef);
1381 // Don't make a tree under water level
1384 // Don't make a tree so high that it doesn't fit
1385 if(y > node_max.Y - 6)
1389 Trees grow only on mud and grass
1392 u32 i = vmanip.m_area.index(v3s16(p));
1393 MapNode *n = &vmanip.m_data[i];
1394 if(n->getContent() != c_dirt
1395 && n->getContent() != c_dirt_with_grass)
1400 make_tree(vmanip, p, false, ndef);
1410 ScopeProfiler sp(g_profiler, "EmergeThread: mapgen lighting update",
1412 //VoxelArea a(node_min, node_max);
1413 VoxelArea a(node_min-v3s16(1,0,1)*MAP_BLOCKSIZE,
1414 node_max+v3s16(1,0,1)*MAP_BLOCKSIZE);
1415 /*VoxelArea a(node_min-v3s16(1,0,1)*MAP_BLOCKSIZE/2,
1416 node_max+v3s16(1,0,1)*MAP_BLOCKSIZE/2);*/
1417 enum LightBank banks[2] = {LIGHTBANK_DAY, LIGHTBANK_NIGHT};
1418 for(int i=0; i<2; i++)
1420 enum LightBank bank = banks[i];
1422 core::map<v3s16, bool> light_sources;
1423 core::map<v3s16, u8> unlight_from;
1425 voxalgo::clearLightAndCollectSources(vmanip, a, bank, ndef,
1426 light_sources, unlight_from);
1428 bool inexistent_top_provides_sunlight = !block_is_underground;
1429 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
1430 vmanip, a, inexistent_top_provides_sunlight,
1431 light_sources, ndef);
1432 // TODO: Do stuff according to bottom_sunlight_valid
1434 vmanip.unspreadLight(bank, unlight_from, light_sources, ndef);
1436 vmanip.spreadLight(bank, light_sources, ndef);