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)
435 this->generating = true;
437 assert(data->vmanip);
438 assert(data->nodedef);
439 assert(data->blockpos_requested.X >= data->blockpos_min.X &&
440 data->blockpos_requested.Y >= data->blockpos_min.Y &&
441 data->blockpos_requested.Z >= data->blockpos_min.Z);
442 assert(data->blockpos_requested.X <= data->blockpos_max.X &&
443 data->blockpos_requested.Y <= data->blockpos_max.Y &&
444 data->blockpos_requested.Z <= data->blockpos_max.Z);
446 INodeDefManager *ndef = data->nodedef;
448 // Hack: use minimum block coordinates for old code that assumes
450 v3s16 blockpos = data->blockpos_requested;
452 /*dstream<<"makeBlock(): ("<<blockpos.X<<","<<blockpos.Y<<","
453 <<blockpos.Z<<")"<<std::endl;*/
455 v3s16 blockpos_min = data->blockpos_min;
456 v3s16 blockpos_max = data->blockpos_max;
457 v3s16 blockpos_full_min = blockpos_min - v3s16(1,1,1);
458 v3s16 blockpos_full_max = blockpos_max + v3s16(1,1,1);
460 ManualMapVoxelManipulator &vmanip = *(data->vmanip);
461 // Area of central chunk
462 node_min = blockpos_min*MAP_BLOCKSIZE;
463 node_max = (blockpos_max+v3s16(1,1,1))*MAP_BLOCKSIZE-v3s16(1,1,1);
464 // Full allocated area
465 v3s16 full_node_min = (blockpos_min-1)*MAP_BLOCKSIZE;
466 v3s16 full_node_max = (blockpos_max+2)*MAP_BLOCKSIZE-v3s16(1,1,1);
468 v3s16 central_area_size = node_max - node_min + v3s16(1,1,1);
470 const s16 max_spread_amount = MAP_BLOCKSIZE;
472 int volume_blocks = (blockpos_max.X - blockpos_min.X + 1)
473 * (blockpos_max.Y - blockpos_min.Y + 1)
474 * (blockpos_max.Z - blockpos_max.Z + 1);
476 int volume_nodes = volume_blocks *
477 MAP_BLOCKSIZE*MAP_BLOCKSIZE*MAP_BLOCKSIZE;
479 // Generated surface area
480 //double gen_area_nodes = MAP_BLOCKSIZE*MAP_BLOCKSIZE * rel_volume;
482 // Horribly wrong heuristic, but better than nothing
483 bool block_is_underground = (water_level > node_max.Y);
486 Create a block-specific seed
488 u32 blockseed = get_blockseed(data->seed, full_node_min);
497 // Need to adjust for the original implementation's +.5 offset...
498 if (!(flags & MG_FLAT)) {
499 noise_terrain_base->perlinMap2D(
500 x + 0.5 * noise_terrain_base->np->spread.X,
501 z + 0.5 * noise_terrain_base->np->spread.Z);
502 noise_terrain_base->transformNoiseMap();
504 noise_terrain_higher->perlinMap2D(
505 x + 0.5 * noise_terrain_higher->np->spread.X,
506 z + 0.5 * noise_terrain_higher->np->spread.Z);
507 noise_terrain_higher->transformNoiseMap();
509 noise_steepness->perlinMap2D(
510 x + 0.5 * noise_steepness->np->spread.X,
511 z + 0.5 * noise_steepness->np->spread.Z);
512 noise_steepness->transformNoiseMap();
514 noise_height_select->perlinMap2D(
515 x + 0.5 * noise_height_select->np->spread.X,
516 z + 0.5 * noise_height_select->np->spread.Z);
519 noise_trees->perlinMap2D(
520 x + 0.5 * noise_trees->np->spread.X,
521 z + 0.5 * noise_trees->np->spread.Z);
523 if (!(flags & MG_FLAT)) {
524 noise_mud->perlinMap2D(
525 x + 0.5 * noise_mud->np->spread.X,
526 z + 0.5 * noise_mud->np->spread.Z);
527 noise_mud->transformNoiseMap();
529 noise_beach->perlinMap2D(
530 x + 0.2 * noise_beach->np->spread.X,
531 z + 0.7 * noise_beach->np->spread.Z);
533 noise_biome->perlinMap2D(
534 x + 0.6 * noise_biome->np->spread.X,
535 z + 0.2 * noise_biome->np->spread.Z);
540 Cache some ground type values for speed
543 // Creates variables c_name=id and n_name=node
544 #define CONTENT_VARIABLE(ndef, name)\
545 content_t c_##name = ndef->getId("mapgen_" #name);\
546 MapNode n_##name(c_##name);
547 // Default to something else if was CONTENT_IGNORE
548 #define CONTENT_VARIABLE_FALLBACK(name, dname)\
549 if(c_##name == CONTENT_IGNORE){\
550 c_##name = c_##dname;\
551 n_##name = n_##dname;\
554 CONTENT_VARIABLE(ndef, stone);
555 CONTENT_VARIABLE(ndef, air);
556 CONTENT_VARIABLE(ndef, water_source);
557 CONTENT_VARIABLE(ndef, dirt);
558 CONTENT_VARIABLE(ndef, sand);
559 CONTENT_VARIABLE(ndef, gravel);
560 CONTENT_VARIABLE(ndef, clay);
561 CONTENT_VARIABLE(ndef, lava_source);
562 CONTENT_VARIABLE(ndef, cobble);
563 CONTENT_VARIABLE(ndef, mossycobble);
564 CONTENT_VARIABLE(ndef, dirt_with_grass);
565 CONTENT_VARIABLE(ndef, junglegrass);
566 CONTENT_VARIABLE(ndef, stone_with_coal);
567 CONTENT_VARIABLE(ndef, stone_with_iron);
568 CONTENT_VARIABLE(ndef, mese);
569 CONTENT_VARIABLE(ndef, desert_sand);
570 CONTENT_VARIABLE_FALLBACK(desert_sand, sand);
571 CONTENT_VARIABLE(ndef, desert_stone);
572 CONTENT_VARIABLE_FALLBACK(desert_stone, stone);
574 // Maximum height of the stone surface and obstacles.
575 // This is used to guide the cave generation
576 s16 stone_surface_max_y = 0;
579 Generate general ground level to full area
583 TimeTaker timer1("Generating ground level");
585 for(s16 x=node_min.X; x<=node_max.X; x++)
586 for(s16 z=node_min.Z; z<=node_max.Z; z++)
589 v2s16 p2d = v2s16(x,z);
592 Skip of already generated
595 v3s16 p(p2d.X, node_min.Y, p2d.Y);
596 if(vmanip.m_data[vmanip.m_area.index(p)].d != CONTENT_AIR)
600 // Ground height at this point
601 float surface_y_f = 0.0;
603 // Use perlin noise for ground height
604 surface_y_f = base_rock_level_2d(data->seed, p2d);
606 /*// Experimental stuff
608 float a = highlands_level_2d(data->seed, p2d);
613 // Convert to integer
614 s16 surface_y = (s16)surface_y_f;
617 if(surface_y > stone_surface_max_y)
618 stone_surface_max_y = surface_y;
620 BiomeType bt = get_biome(data->seed, p2d);
622 Fill ground with stone
625 // Use fast index incrementing
626 v3s16 em = vmanip.m_area.getExtent();
627 u32 i = vmanip.m_area.index(v3s16(p2d.X, node_min.Y, p2d.Y));
628 for(s16 y=node_min.Y; y<=node_max.Y; y++)
630 if(vmanip.m_data[i].getContent() == CONTENT_IGNORE){
632 if(y > water_level && bt == BT_DESERT)
633 vmanip.m_data[i] = n_desert_stone;
635 vmanip.m_data[i] = n_stone;
636 } else if(y <= water_level){
637 vmanip.m_data[i] = MapNode(c_water_source);
639 vmanip.m_data[i] = MapNode(c_air);
642 vmanip.m_area.add_y(em, i, 1);
650 // Limit dirt flow area by 1 because mud is flown into neighbors.
651 assert(central_area_size.X == central_area_size.Z);
652 s16 mudflow_minpos = 0-max_spread_amount+1;
653 s16 mudflow_maxpos = central_area_size.X+max_spread_amount-2;
656 Loop this part, it will make stuff look older and newer nicely
659 /*double cave_amount = 6.0 + 6.0 * noise2d_perlin(
660 0.5+(double)node_min.X/250, 0.5+(double)node_min.Y/250,
661 data->seed+34329, 3, 0.50);*/
663 double cave_amount = NoisePerlin2D(np_cave, node_min.X, node_min.Y, data->seed);
665 const u32 age_loops = 2;
666 for(u32 i_age=0; i_age<age_loops; i_age++)
668 /******************************
669 BEGINNING OF AGING LOOP
670 ******************************/
675 //TimeTaker timer1("caves");
678 Make caves (this code is relatively horrible)
680 cave_amount = MYMAX(0.0, cave_amount);
681 u32 caves_count = cave_amount * volume_nodes / 50000;
682 u32 bruises_count = 1;
683 PseudoRandom ps(blockseed+21343);
684 PseudoRandom ps2(blockseed+1032);
685 if(ps.range(1, 6) == 1)
686 bruises_count = ps.range(0, ps.range(0, 2));
687 if(get_biome(data->seed, v2s16(node_min.X, node_min.Z)) == BT_DESERT){
691 for(u32 jj=0; jj<caves_count+bruises_count; jj++)
693 if (!(flags & MG_CAVES))
696 /*int avg_height = (int)
697 ((base_rock_level_2d(data->seed, v2s16(node_min.X, node_min.Z)) +
698 base_rock_level_2d(data->seed, v2s16(node_max.X, node_max.Z))) / 2);
699 if ((node_max.Y + node_min.Y) / 2 > avg_height)
702 bool large_cave = (jj >= caves_count);
703 s16 min_tunnel_diameter = 2;
704 s16 max_tunnel_diameter = ps.range(2,6);
705 int dswitchint = ps.range(1,14);
706 u16 tunnel_routepoints = 0;
707 int part_max_length_rs = 0;
709 part_max_length_rs = ps.range(2,4);
710 tunnel_routepoints = ps.range(5, ps.range(15,30));
711 min_tunnel_diameter = 5;
712 max_tunnel_diameter = ps.range(7, ps.range(8,24));
714 part_max_length_rs = ps.range(2,9);
715 tunnel_routepoints = ps.range(10, ps.range(15,30));
717 bool large_cave_is_flat = (ps.range(0,1) == 0);
719 v3f main_direction(0,0,0);
721 // Allowed route area size in nodes
722 v3s16 ar = central_area_size;
724 // Area starting point in nodes
728 //(this should be more than the maximum radius of the tunnel)
730 s16 more = max_spread_amount - max_tunnel_diameter/2 - insure;
731 ar += v3s16(1,0,1) * more * 2;
732 of -= v3s16(1,0,1) * more;
735 // Allow half a diameter + 7 over stone surface
736 s16 route_y_max = -of.Y + stone_surface_max_y + max_tunnel_diameter/2 + 7;
738 // Limit maximum to area
739 route_y_max = rangelim(route_y_max, 0, ar.Y-1);
744 if(node_min.Y < water_level && node_max.Y > water_level)
746 min = water_level - max_tunnel_diameter/3 - of.Y;
747 route_y_max = water_level + max_tunnel_diameter/3 - of.Y;
749 route_y_min = ps.range(min, min + max_tunnel_diameter);
750 route_y_min = rangelim(route_y_min, 0, route_y_max);
753 s16 route_start_y_min = route_y_min;
754 s16 route_start_y_max = route_y_max;
756 route_start_y_min = rangelim(route_start_y_min, 0, ar.Y-1);
757 route_start_y_max = rangelim(route_start_y_max, route_start_y_min, ar.Y-1);
759 // Randomize starting position
761 (float)(ps.next()%ar.X)+0.5,
762 (float)(ps.range(route_start_y_min, route_start_y_max))+0.5,
763 (float)(ps.next()%ar.Z)+0.5
766 v3s16 startp(orp.X, orp.Y, orp.Z);
769 MapNode airnode(CONTENT_AIR);
770 MapNode waternode(c_water_source);
771 MapNode lavanode(c_lava_source);
774 Generate some tunnel starting from orp
777 for(u16 j=0; j<tunnel_routepoints; j++)
779 if(j%dswitchint==0 && large_cave == false)
781 main_direction = v3f(
782 ((float)(ps.next()%20)-(float)10)/10,
783 ((float)(ps.next()%20)-(float)10)/30,
784 ((float)(ps.next()%20)-(float)10)/10
786 main_direction *= (float)ps.range(0, 10)/10;
790 s16 min_d = min_tunnel_diameter;
791 s16 max_d = max_tunnel_diameter;
792 s16 rs = ps.range(min_d, max_d);
794 // Every second section is rough
795 bool randomize_xz = (ps2.range(1,2) == 1);
801 rs*part_max_length_rs,
802 rs*part_max_length_rs/2,
803 rs*part_max_length_rs
809 rs*part_max_length_rs,
810 ps.range(1, rs*part_max_length_rs),
811 rs*part_max_length_rs
818 (float)(ps.next()%(maxlen.X*1))-(float)maxlen.X/2,
819 (float)(ps.next()%(maxlen.Y*1))-(float)maxlen.Y/2,
820 (float)(ps.next()%(maxlen.Z*1))-(float)maxlen.Z/2
823 // Jump downward sometimes
824 if(!large_cave && ps.range(0,12) == 0)
827 (float)(ps.next()%(maxlen.X*1))-(float)maxlen.X/2,
828 (float)(ps.next()%(maxlen.Y*2))-(float)maxlen.Y*2/2,
829 (float)(ps.next()%(maxlen.Z*1))-(float)maxlen.Z/2
835 s16 h = find_ground_level_clever(vmanip,
836 v2s16(p.X, p.Z), ndef);
837 route_y_min = h - rs/3;
838 route_y_max = h + rs;
841 vec += main_direction;
846 else if(rp.X >= ar.X)
848 if(rp.Y < route_y_min)
850 else if(rp.Y >= route_y_max)
851 rp.Y = route_y_max-1;
854 else if(rp.Z >= ar.Z)
858 for(float f=0; f<1.0; f+=1.0/vec.getLength())
860 v3f fp = orp + vec * f;
861 fp.X += 0.1*ps.range(-10,10);
862 fp.Z += 0.1*ps.range(-10,10);
863 v3s16 cp(fp.X, fp.Y, fp.Z);
868 d0 += ps.range(-1,1);
869 d1 += ps.range(-1,1);
871 for(s16 z0=d0; z0<=d1; z0++)
873 s16 si = rs/2 - MYMAX(0, abs(z0)-rs/7-1);
874 for(s16 x0=-si-ps.range(0,1); x0<=si-1+ps.range(0,1); x0++)
876 s16 maxabsxz = MYMAX(abs(x0), abs(z0));
877 s16 si2 = rs/2 - MYMAX(0, maxabsxz-rs/7-1);
878 for(s16 y0=-si2; y0<=si2; y0++)
880 /*// Make better floors in small caves
881 if(y0 <= -rs/2 && rs<=7)
883 if(large_cave_is_flat){
884 // Make large caves not so tall
885 if(rs > 7 && abs(y0) >= rs/3)
895 if(vmanip.m_area.contains(p) == false)
898 u32 i = vmanip.m_area.index(p);
902 if(full_node_min.Y < water_level &&
903 full_node_max.Y > water_level){
904 if(p.Y <= water_level)
905 vmanip.m_data[i] = waternode;
907 vmanip.m_data[i] = airnode;
908 } else if(full_node_max.Y < water_level){
909 if(p.Y < startp.Y - 2)
910 vmanip.m_data[i] = lavanode;
912 vmanip.m_data[i] = airnode;
914 vmanip.m_data[i] = airnode;
917 // Don't replace air or water or lava or ignore
918 if(vmanip.m_data[i].getContent() == CONTENT_IGNORE ||
919 vmanip.m_data[i].getContent() == CONTENT_AIR ||
920 vmanip.m_data[i].getContent() == c_water_source ||
921 vmanip.m_data[i].getContent() == c_lava_source)
924 vmanip.m_data[i] = airnode;
927 vmanip.m_flags[i] |= VMANIP_FLAG_CAVE;
945 TimeTaker timer1("add mud");
948 Add mud to the central chunk
951 for(s16 x=node_min.X; x<=node_max.X; x++)
952 for(s16 z=node_min.Z; z<=node_max.Z; z++)
954 // Node position in 2d
955 v2s16 p2d = v2s16(x,z);
957 // Randomize mud amount
958 s16 mud_add_amount = get_mud_add_amount(data->seed, p2d) / 2.0 + 0.5;
961 s16 surface_y = find_stone_level(vmanip, p2d, ndef);
962 // Handle area not found
963 if(surface_y == vmanip.m_area.MinEdge.Y - 1)
966 MapNode addnode(c_dirt);
967 BiomeType bt = get_biome(data->seed, p2d);
970 addnode = MapNode(c_desert_sand);
972 if(bt == BT_DESERT && surface_y + mud_add_amount <= water_level+1){
973 addnode = MapNode(c_sand);
974 } else if(mud_add_amount <= 0){
975 mud_add_amount = 1 - mud_add_amount;
976 addnode = MapNode(c_gravel);
977 } else if(bt == BT_NORMAL && get_have_beach(data->seed, p2d) &&
978 surface_y + mud_add_amount <= water_level+2){
979 addnode = MapNode(c_sand);
984 mud_add_amount = MYMAX(0, mud_add_amount - (surface_y - 20)/5);
989 If topmost node is grass, change it to mud.
990 It might be if it was flown to there from a neighboring
991 chunk and then converted.
994 u32 i = vmanip.m_area.index(v3s16(p2d.X, surface_y, p2d.Y));
995 MapNode *n = &vmanip.m_data[i];
996 if(n->getContent() == c_dirt_with_grass)
997 *n = MapNode(c_dirt);
1005 v3s16 em = vmanip.m_area.getExtent();
1006 s16 y_start = surface_y+1;
1007 u32 i = vmanip.m_area.index(v3s16(p2d.X, y_start, p2d.Y));
1008 for(s16 y=y_start; y<=node_max.Y; y++)
1010 if(mudcount >= mud_add_amount)
1013 MapNode &n = vmanip.m_data[i];
1017 vmanip.m_area.add_y(em, i, 1);
1027 Add blobs of dirt and gravel underground
1029 if(get_biome(data->seed, v2s16(node_min.X, node_min.Z)) == BT_NORMAL)
1031 PseudoRandom pr(blockseed+983);
1032 for(int i=0; i<volume_nodes/10/10/10; i++)
1034 bool only_fill_cave = (myrand_range(0,1) != 0);
1041 pr.range(node_min.X, node_max.X)-size.X/2,
1042 pr.range(node_min.Y, node_max.Y)-size.Y/2,
1043 pr.range(node_min.Z, node_max.Z)-size.Z/2
1046 if(p0.Y > -32 && pr.range(0,1) == 0)
1047 n1 = MapNode(c_dirt);
1049 n1 = MapNode(c_gravel);
1050 for(int x1=0; x1<size.X; x1++)
1051 for(int y1=0; y1<size.Y; y1++)
1052 for(int z1=0; z1<size.Z; z1++)
1054 v3s16 p = p0 + v3s16(x1,y1,z1);
1055 u32 i = vmanip.m_area.index(p);
1056 if(!vmanip.m_area.contains(i))
1058 // Cancel if not stone and not cave air
1059 if(vmanip.m_data[i].getContent() != c_stone &&
1060 !(vmanip.m_flags[i] & VMANIP_FLAG_CAVE))
1062 if(only_fill_cave && !(vmanip.m_flags[i] & VMANIP_FLAG_CAVE))
1064 vmanip.m_data[i] = n1;
1072 TimeTaker timer1("flow mud");
1075 Flow mud away from steep edges
1078 // Iterate a few times
1079 for(s16 k=0; k<3; k++)
1082 for(s16 x=mudflow_minpos; x<=mudflow_maxpos; x++)
1083 for(s16 z=mudflow_minpos; z<=mudflow_maxpos; z++)
1085 // Invert coordinates every 2nd iteration
1088 x = mudflow_maxpos - (x-mudflow_minpos);
1089 z = mudflow_maxpos - (z-mudflow_minpos);
1092 // Node position in 2d
1093 v2s16 p2d = v2s16(node_min.X, node_min.Z) + v2s16(x,z);
1095 v3s16 em = vmanip.m_area.getExtent();
1096 u32 i = vmanip.m_area.index(v3s16(p2d.X, node_max.Y, p2d.Y));
1099 while(y >= node_min.Y)
1106 for(; y>=node_min.Y; y--)
1108 n = &vmanip.m_data[i];
1109 //if(content_walkable(n->d))
1111 if(n->getContent() == c_dirt ||
1112 n->getContent() == c_dirt_with_grass ||
1113 n->getContent() == c_gravel)
1116 vmanip.m_area.add_y(em, i, -1);
1119 // Stop if out of area
1120 //if(vmanip.m_area.contains(i) == false)
1124 /*// If not mud, do nothing to it
1125 MapNode *n = &vmanip.m_data[i];
1126 if(n->d != CONTENT_MUD && n->d != CONTENT_GRASS)
1129 if(n->getContent() == c_dirt ||
1130 n->getContent() == c_dirt_with_grass)
1132 // Make it exactly mud
1133 n->setContent(c_dirt);
1136 Don't flow it if the stuff under it is not mud
1140 vmanip.m_area.add_y(em, i2, -1);
1141 // Cancel if out of area
1142 if(vmanip.m_area.contains(i2) == false)
1144 MapNode *n2 = &vmanip.m_data[i2];
1145 if(n2->getContent() != c_dirt &&
1146 n2->getContent() != c_dirt_with_grass)
1151 /*s16 recurse_count = 0;
1155 v3s16(0,0,1), // back
1156 v3s16(1,0,0), // right
1157 v3s16(0,0,-1), // front
1158 v3s16(-1,0,0), // left
1161 // Theck that upper is air or doesn't exist.
1162 // Cancel dropping if upper keeps it in place
1164 vmanip.m_area.add_y(em, i3, 1);
1165 if(vmanip.m_area.contains(i3) == true
1166 && ndef->get(vmanip.m_data[i3]).walkable)
1173 for(u32 di=0; di<4; di++)
1175 v3s16 dirp = dirs4[di];
1178 vmanip.m_area.add_p(em, i2, dirp);
1179 // Fail if out of area
1180 if(vmanip.m_area.contains(i2) == false)
1182 // Check that side is air
1183 MapNode *n2 = &vmanip.m_data[i2];
1184 if(ndef->get(*n2).walkable)
1186 // Check that under side is air
1187 vmanip.m_area.add_y(em, i2, -1);
1188 if(vmanip.m_area.contains(i2) == false)
1190 n2 = &vmanip.m_data[i2];
1191 if(ndef->get(*n2).walkable)
1193 /*// Check that under that is air (need a drop of 2)
1194 vmanip.m_area.add_y(em, i2, -1);
1195 if(vmanip.m_area.contains(i2) == false)
1197 n2 = &vmanip.m_data[i2];
1198 if(content_walkable(n2->d))
1200 // Loop further down until not air
1201 bool dropped_to_unknown = false;
1203 vmanip.m_area.add_y(em, i2, -1);
1204 n2 = &vmanip.m_data[i2];
1205 // if out of known area
1206 if(vmanip.m_area.contains(i2) == false
1207 || n2->getContent() == CONTENT_IGNORE){
1208 dropped_to_unknown = true;
1211 }while(ndef->get(*n2).walkable == false);
1212 // Loop one up so that we're in air
1213 vmanip.m_area.add_y(em, i2, 1);
1214 n2 = &vmanip.m_data[i2];
1216 bool old_is_water = (n->getContent() == c_water_source);
1217 // Move mud to new place
1218 if(!dropped_to_unknown) {
1220 // Set old place to be air (or water)
1222 *n = MapNode(c_water_source);
1224 *n = MapNode(CONTENT_AIR);
1240 /***********************
1242 ************************/
1245 Add top and bottom side of water to transforming_liquid queue
1248 for(s16 x=full_node_min.X; x<=full_node_max.X; x++)
1249 for(s16 z=full_node_min.Z; z<=full_node_max.Z; z++)
1254 bool water_found = false;
1255 // Use fast index incrementing
1256 v3s16 em = vmanip.m_area.getExtent();
1257 u32 i = vmanip.m_area.index(v3s16(p2d.X, full_node_max.Y, p2d.Y));
1258 for(s16 y=full_node_max.Y; y>=full_node_min.Y; y--)
1260 if(y == full_node_max.Y){
1262 (vmanip.m_data[i].getContent() == c_water_source ||
1263 vmanip.m_data[i].getContent() == c_lava_source);
1265 else if(water_found == false)
1267 if(vmanip.m_data[i].getContent() == c_water_source ||
1268 vmanip.m_data[i].getContent() == c_lava_source)
1270 v3s16 p = v3s16(p2d.X, y, p2d.Y);
1271 data->transforming_liquid.push_back(p);
1277 // This can be done because water_found can only
1278 // turn to true and end up here after going through
1280 if(vmanip.m_data[i+1].getContent() != c_water_source ||
1281 vmanip.m_data[i+1].getContent() != c_lava_source)
1283 v3s16 p = v3s16(p2d.X, y+1, p2d.Y);
1284 data->transforming_liquid.push_back(p);
1285 water_found = false;
1289 vmanip.m_area.add_y(em, i, -1);
1298 for(s16 x=full_node_min.X; x<=full_node_max.X; x++)
1299 for(s16 z=full_node_min.Z; z<=full_node_max.Z; z++)
1301 // Node position in 2d
1302 v2s16 p2d = v2s16(x,z);
1305 Find the lowest surface to which enough light ends up
1308 Basically just wait until not air and not leaves.
1312 v3s16 em = vmanip.m_area.getExtent();
1313 u32 i = vmanip.m_area.index(v3s16(p2d.X, node_max.Y, p2d.Y));
1315 // Go to ground level
1316 for(y=node_max.Y; y>=full_node_min.Y; y--)
1318 MapNode &n = vmanip.m_data[i];
1319 if(ndef->get(n).param_type != CPT_LIGHT
1320 || ndef->get(n).liquid_type != LIQUID_NONE)
1322 vmanip.m_area.add_y(em, i, -1);
1324 if(y >= full_node_min.Y)
1327 surface_y = full_node_min.Y;
1330 u32 i = vmanip.m_area.index(p2d.X, surface_y, p2d.Y);
1331 MapNode *n = &vmanip.m_data[i];
1332 if(n->getContent() == c_dirt){
1333 // Well yeah, this can't be overground...
1334 if(surface_y < water_level - 20)
1336 n->setContent(c_dirt_with_grass);
1343 assert(central_area_size.X == central_area_size.Z);
1344 if (flags & MG_TREES) {
1345 // Divide area into parts
1347 s16 sidelen = central_area_size.X / div;
1348 double area = sidelen * sidelen;
1349 for(s16 x0=0; x0<div; x0++)
1350 for(s16 z0=0; z0<div; z0++)
1352 // Center position of part of division
1354 node_min.X + sidelen/2 + sidelen*x0,
1355 node_min.Z + sidelen/2 + sidelen*z0
1357 // Minimum edge of part of division
1359 node_min.X + sidelen*x0,
1360 node_min.Z + sidelen*z0
1362 // Maximum edge of part of division
1364 node_min.X + sidelen + sidelen*x0 - 1,
1365 node_min.Z + sidelen + sidelen*z0 - 1
1368 u32 tree_count = area * tree_amount_2d(data->seed, p2d_center);
1369 // Put trees in random places on part of division
1370 for(u32 i=0; i<tree_count; i++)
1372 s16 x = myrand_range(p2d_min.X, p2d_max.X);
1373 s16 z = myrand_range(p2d_min.Y, p2d_max.Y);
1374 s16 y = find_ground_level(vmanip, v2s16(x,z), ndef);
1375 // Don't make a tree under water level
1378 // Don't make a tree so high that it doesn't fit
1379 if(y > node_max.Y - 6)
1383 Trees grow only on mud and grass
1386 u32 i = vmanip.m_area.index(v3s16(p));
1387 MapNode *n = &vmanip.m_data[i];
1388 if(n->getContent() != c_dirt
1389 && n->getContent() != c_dirt_with_grass)
1394 make_tree(vmanip, p, false, ndef);
1404 ScopeProfiler sp(g_profiler, "EmergeThread: mapgen lighting update",
1406 //VoxelArea a(node_min, node_max);
1407 VoxelArea a(node_min-v3s16(1,0,1)*MAP_BLOCKSIZE,
1408 node_max+v3s16(1,0,1)*MAP_BLOCKSIZE);
1409 /*VoxelArea a(node_min-v3s16(1,0,1)*MAP_BLOCKSIZE/2,
1410 node_max+v3s16(1,0,1)*MAP_BLOCKSIZE/2);*/
1411 enum LightBank banks[2] = {LIGHTBANK_DAY, LIGHTBANK_NIGHT};
1412 for(int i=0; i<2; i++)
1414 enum LightBank bank = banks[i];
1416 core::map<v3s16, bool> light_sources;
1417 core::map<v3s16, u8> unlight_from;
1419 voxalgo::clearLightAndCollectSources(vmanip, a, bank, ndef,
1420 light_sources, unlight_from);
1422 bool inexistent_top_provides_sunlight = !block_is_underground;
1423 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
1424 vmanip, a, inexistent_top_provides_sunlight,
1425 light_sources, ndef);
1426 // TODO: Do stuff according to bottom_sunlight_valid
1428 vmanip.unspreadLight(bank, unlight_from, light_sources, ndef);
1430 vmanip.spreadLight(bank, light_sources, ndef);
1433 this->generating = false;