3 Copyright (C) 2010-2011 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
36 ///////////////////////////////////////////////////////////////////////////////
39 MapgenV6::MapgenV6(int mapgenid, MapgenV6Params *params) {
40 this->generating = false;
43 this->seed = (int)params->seed;
44 this->water_level = params->water_level;
45 this->flags = params->flags;
46 this->csize = v3s16(1, 1, 1) * params->chunksize * MAP_BLOCKSIZE;
48 this->freq_desert = params->freq_desert;
49 this->freq_beach = params->freq_beach;
51 this->ystride = csize.X; //////fix this
53 np_cave = params->np_cave;
55 noise_terrain_base = new Noise(params->np_terrain_base, seed, csize.X, csize.Y);
56 noise_terrain_higher = new Noise(params->np_terrain_higher, seed, csize.X, csize.Y);
57 noise_steepness = new Noise(params->np_steepness, seed, csize.X, csize.Y);
58 noise_height_select = new Noise(params->np_height_select, seed, csize.X, csize.Y);
59 noise_trees = new Noise(params->np_trees, seed, csize.X, csize.Y);
60 noise_mud = new Noise(params->np_mud, seed, csize.X, csize.Y);
61 noise_beach = new Noise(params->np_beach, seed, csize.X, csize.Y);
62 noise_biome = new Noise(params->np_biome, seed, csize.X, csize.Y);
64 map_terrain_base = noise_terrain_base->result;
65 map_terrain_higher = noise_terrain_higher->result;
66 map_steepness = noise_steepness->result;
67 map_height_select = noise_height_select->result;
68 map_trees = noise_trees->result;
69 map_mud = noise_mud->result;
70 map_beach = noise_beach->result;
71 map_biome = noise_biome->result;
75 MapgenV6::~MapgenV6() {
76 delete noise_terrain_base;
77 delete noise_terrain_higher;
78 delete noise_steepness;
79 delete noise_height_select;
88 Some helper functions for the map generator
92 // Returns Y one under area minimum if not found
93 s16 MapgenV6::find_ground_level(VoxelManipulator &vmanip, v2s16 p2d,
94 INodeDefManager *ndef)
96 v3s16 em = vmanip.m_area.getExtent();
97 s16 y_nodes_max = vmanip.m_area.MaxEdge.Y;
98 s16 y_nodes_min = vmanip.m_area.MinEdge.Y;
99 u32 i = vmanip.m_area.index(v3s16(p2d.X, y_nodes_max, p2d.Y));
101 for(y=y_nodes_max; y>=y_nodes_min; y--)
103 MapNode &n = vmanip.m_data[i];
104 if(ndef->get(n).walkable)
107 vmanip.m_area.add_y(em, i, -1);
112 return y_nodes_min - 1;
115 // Returns Y one under area minimum if not found
116 s16 MapgenV6::find_stone_level(VoxelManipulator &vmanip, v2s16 p2d,
117 INodeDefManager *ndef)
119 v3s16 em = vmanip.m_area.getExtent();
120 s16 y_nodes_max = vmanip.m_area.MaxEdge.Y;
121 s16 y_nodes_min = vmanip.m_area.MinEdge.Y;
122 u32 i = vmanip.m_area.index(v3s16(p2d.X, y_nodes_max, p2d.Y));
124 content_t c_stone = ndef->getId("mapgen_stone");
125 content_t c_desert_stone = ndef->getId("mapgen_desert_stone");
126 for(y=y_nodes_max; y>=y_nodes_min; y--)
128 MapNode &n = vmanip.m_data[i];
129 content_t c = n.getContent();
130 if(c != CONTENT_IGNORE && (
131 c == c_stone || c == c_desert_stone))
134 vmanip.m_area.add_y(em, i, -1);
139 return y_nodes_min - 1;
143 void MapgenV6::make_tree(ManualMapVoxelManipulator &vmanip, v3s16 p0,
144 bool is_apple_tree, INodeDefManager *ndef)
146 MapNode treenode(ndef->getId("mapgen_tree"));
147 MapNode leavesnode(ndef->getId("mapgen_leaves"));
148 MapNode applenode(ndef->getId("mapgen_apple"));
150 s16 trunk_h = myrand_range(4, 5);
152 for(s16 ii=0; ii<trunk_h; ii++)
154 if(vmanip.m_area.contains(p1))
155 vmanip.m_data[vmanip.m_area.index(p1)] = treenode;
159 // p1 is now the last piece of the trunk
162 VoxelArea leaves_a(v3s16(-2,-1,-2), v3s16(2,2,2));
163 //SharedPtr<u8> leaves_d(new u8[leaves_a.getVolume()]);
164 Buffer<u8> leaves_d(leaves_a.getVolume());
165 for(s32 i=0; i<leaves_a.getVolume(); i++)
168 // Force leaves at near the end of the trunk
171 for(s16 z=-d; z<=d; z++)
172 for(s16 y=-d; y<=d; y++)
173 for(s16 x=-d; x<=d; x++)
175 leaves_d[leaves_a.index(v3s16(x,y,z))] = 1;
179 // Add leaves randomly
180 for(u32 iii=0; iii<7; iii++)
185 myrand_range(leaves_a.MinEdge.X, leaves_a.MaxEdge.X-d),
186 myrand_range(leaves_a.MinEdge.Y, leaves_a.MaxEdge.Y-d),
187 myrand_range(leaves_a.MinEdge.Z, leaves_a.MaxEdge.Z-d)
190 for(s16 z=0; z<=d; z++)
191 for(s16 y=0; y<=d; y++)
192 for(s16 x=0; x<=d; x++)
194 leaves_d[leaves_a.index(p+v3s16(x,y,z))] = 1;
198 // Blit leaves to vmanip
199 for(s16 z=leaves_a.MinEdge.Z; z<=leaves_a.MaxEdge.Z; z++)
200 for(s16 y=leaves_a.MinEdge.Y; y<=leaves_a.MaxEdge.Y; y++)
201 for(s16 x=leaves_a.MinEdge.X; x<=leaves_a.MaxEdge.X; x++)
205 if(vmanip.m_area.contains(p) == false)
207 u32 vi = vmanip.m_area.index(p);
208 if(vmanip.m_data[vi].getContent() != CONTENT_AIR
209 && vmanip.m_data[vi].getContent() != CONTENT_IGNORE)
211 u32 i = leaves_a.index(x,y,z);
212 if(leaves_d[i] == 1) {
213 bool is_apple = myrand_range(0,99) < 10;
214 if(is_apple_tree && is_apple) {
215 vmanip.m_data[vi] = applenode;
217 vmanip.m_data[vi] = leavesnode;
225 Noise functions. Make sure seed is mangled differently in each one.
229 // Amount of trees per area in nodes
230 double MapgenV6::tree_amount_2d(u64 seed, v2s16 p)
232 /*double noise = noise2d_perlin(
233 0.5+(float)p.X/125, 0.5+(float)p.Y/125,
235 double noise = map_trees[(p.Y - node_min.Z) * ystride + (p.X - node_min.X)];
236 double zeroval = -0.39;
240 return 0.04 * (noise-zeroval) / (1.0-zeroval);
243 // Required by mapgen.h
244 bool MapgenV6::block_is_underground(u64 seed, v3s16 blockpos)
246 /*s16 minimum_groundlevel = (s16)get_sector_minimum_ground_level(
247 seed, v2s16(blockpos.X, blockpos.Z));*/
248 // Nah, this is just a heuristic, just return something
249 s16 minimum_groundlevel = water_level;
251 if(blockpos.Y*MAP_BLOCKSIZE + MAP_BLOCKSIZE <= minimum_groundlevel)
258 double MapgenV6::base_rock_level_2d(u64 seed, v2s16 p)
260 int index = (p.Y - node_min.Z) * ystride + (p.X - node_min.X);
262 // The base ground level
263 /*double base = (double)WATER_LEVEL - (double)AVERAGE_MUD_AMOUNT
264 + 20. * noise2d_perlin(
265 0.5+(float)p.X/250., 0.5+(float)p.Y/250.,
266 seed+82341, 5, 0.6);*/
267 double base = water_level + map_terrain_base[index];
269 // Higher ground level
270 /*double higher = (double)WATER_LEVEL + 20. + 16. * noise2d_perlin(
271 0.5+(float)p.X/500., 0.5+(float)p.Y/500.,
272 seed+85039, 5, 0.6);*/
273 double higher = water_level + map_terrain_higher[index];
275 // Limit higher to at least base
279 // Steepness factor of cliffs
280 /*double b = 0.85 + 0.5 * noise2d_perlin(
281 0.5+(float)p.X/125., 0.5+(float)p.Y/125.,
283 double b = map_steepness[index];
284 b = rangelim(b, 0.0, 1000.0);
287 b = rangelim(b, 0.5, 1000.0);
289 // Values 1.5...100 give quite horrible looking slopes
290 if(b > 1.5 && b < 100.0){
297 // Offset to more low
298 double a_off = -0.20;
301 /*double a = (double)0.5 + b * (a_off + noise2d_perlin(
302 0.5+(float)p.X/250., 0.5+(float)p.Y/250.,
303 seed+4213, 5, 0.69));*/
304 double a = 0.5 + b * (a_off + map_height_select[index]);
307 a = rangelim(a, 0.0, 1.0);
309 double h = base*(1.0-a) + higher*a;
314 s16 MapgenV6::find_ground_level_from_noise(u64 seed, v2s16 p2d, s16 precision)
316 return base_rock_level_2d(seed, p2d) + AVERAGE_MUD_AMOUNT;
319 double MapgenV6::get_mud_add_amount(u64 seed, v2s16 p)
321 /*return ((float)AVERAGE_MUD_AMOUNT + 2.0 * noise2d_perlin(
322 0.5+(float)p.X/200, 0.5+(float)p.Y/200,
323 seed+91013, 3, 0.55));*/
324 int index = (p.Y - node_min.Z) * ystride + (p.X - node_min.X);
325 return map_mud[index];
328 bool MapgenV6::get_have_beach(u64 seed, v2s16 p2d)
330 // Determine whether to have sand here
331 /*double sandnoise = noise2d_perlin(
332 0.2+(float)p2d.X/250, 0.7+(float)p2d.Y/250,
333 seed+59420, 3, 0.50);*/
334 int index = (p2d.Y - node_min.Z) * ystride + (p2d.X - node_min.X);
335 double sandnoise = map_beach[index];
337 return (sandnoise > freq_beach);
340 BiomeType MapgenV6::get_biome(u64 seed, v2s16 p2d)
342 // Just do something very simple as for now
343 /*double d = noise2d_perlin(
344 0.6+(float)p2d.X/250, 0.2+(float)p2d.Y/250,
345 seed+9130, 3, 0.50);*/
346 int index = (p2d.Y - node_min.Z) * ystride + (p2d.X - node_min.X);
347 double d = map_biome[index];
350 if (flags & MGV6_BIOME_BLEND) {
351 if(d > freq_desert - 0.10 &&
352 (noise2d(p2d.X, p2d.Y, seed) + 1.0) > (freq_desert - d) * 20.0)
358 u32 MapgenV6::get_blockseed(u64 seed, v3s16 p)
360 s32 x=p.X, y=p.Y, z=p.Z;
361 return (u32)(seed%0x100000000ULL) + z*38134234 + y*42123 + x*23;
365 int MapgenV6::getGroundLevelAtPoint(v2s16 p) {
366 return base_rock_level_2d(seed, p) + AVERAGE_MUD_AMOUNT;
370 #define VMANIP_FLAG_CAVE VOXELFLAG_CHECKED1
372 void MapgenV6::makeChunk(BlockMakeData *data)
376 //dstream<<"makeBlock: no-op"<<std::endl;
380 this->generating = true;
382 assert(data->vmanip);
383 assert(data->nodedef);
384 assert(data->blockpos_requested.X >= data->blockpos_min.X &&
385 data->blockpos_requested.Y >= data->blockpos_min.Y &&
386 data->blockpos_requested.Z >= data->blockpos_min.Z);
387 assert(data->blockpos_requested.X <= data->blockpos_max.X &&
388 data->blockpos_requested.Y <= data->blockpos_max.Y &&
389 data->blockpos_requested.Z <= data->blockpos_max.Z);
391 INodeDefManager *ndef = data->nodedef;
393 // Hack: use minimum block coordinates for old code that assumes
395 v3s16 blockpos = data->blockpos_requested;
397 /*dstream<<"makeBlock(): ("<<blockpos.X<<","<<blockpos.Y<<","
398 <<blockpos.Z<<")"<<std::endl;*/
400 v3s16 blockpos_min = data->blockpos_min;
401 v3s16 blockpos_max = data->blockpos_max;
402 v3s16 blockpos_full_min = blockpos_min - v3s16(1,1,1);
403 v3s16 blockpos_full_max = blockpos_max + v3s16(1,1,1);
405 ManualMapVoxelManipulator &vmanip = *(data->vmanip);
406 // Area of central chunk
407 node_min = blockpos_min*MAP_BLOCKSIZE;
408 node_max = (blockpos_max+v3s16(1,1,1))*MAP_BLOCKSIZE-v3s16(1,1,1);
409 // Full allocated area
410 v3s16 full_node_min = (blockpos_min-1)*MAP_BLOCKSIZE;
411 v3s16 full_node_max = (blockpos_max+2)*MAP_BLOCKSIZE-v3s16(1,1,1);
413 v3s16 central_area_size = node_max - node_min + v3s16(1,1,1);
415 const s16 max_spread_amount = MAP_BLOCKSIZE;
417 int volume_blocks = (blockpos_max.X - blockpos_min.X + 1)
418 * (blockpos_max.Y - blockpos_min.Y + 1)
419 * (blockpos_max.Z - blockpos_max.Z + 1);
421 int volume_nodes = volume_blocks *
422 MAP_BLOCKSIZE*MAP_BLOCKSIZE*MAP_BLOCKSIZE;
424 // Generated surface area
425 //double gen_area_nodes = MAP_BLOCKSIZE*MAP_BLOCKSIZE * rel_volume;
427 // Horribly wrong heuristic, but better than nothing
428 bool block_is_underground = (water_level > node_max.Y);
431 Create a block-specific seed
433 u32 blockseed = get_blockseed(data->seed, full_node_min);
442 // Need to adjust for the original implementation's +.5 offset...
443 noise_terrain_base->perlinMap2D(
444 x + 0.5 * noise_terrain_base->np->spread.X,
445 z + 0.5 * noise_terrain_base->np->spread.Z);
446 noise_terrain_base->transformNoiseMap();
448 noise_terrain_higher->perlinMap2D(
449 x + 0.5 * noise_terrain_higher->np->spread.X,
450 z + 0.5 * noise_terrain_higher->np->spread.Z);
451 noise_terrain_higher->transformNoiseMap();
453 noise_steepness->perlinMap2D(
454 x + 0.5 * noise_steepness->np->spread.X,
455 z + 0.5 * noise_steepness->np->spread.Z);
456 noise_steepness->transformNoiseMap();
458 noise_height_select->perlinMap2D(
459 x + 0.5 * noise_height_select->np->spread.X,
460 z + 0.5 * noise_height_select->np->spread.Z);
462 noise_trees->perlinMap2D(
463 x + 0.5 * noise_trees->np->spread.X,
464 z + 0.5 * noise_trees->np->spread.Z);
466 noise_mud->perlinMap2D(
467 x + 0.5 * noise_mud->np->spread.X,
468 z + 0.5 * noise_mud->np->spread.Z);
469 noise_mud->transformNoiseMap();
471 noise_beach->perlinMap2D(
472 x + 0.2 * noise_beach->np->spread.X,
473 z + 0.7 * noise_beach->np->spread.Z);
475 noise_biome->perlinMap2D(
476 x + 0.6 * noise_biome->np->spread.X,
477 z + 0.2 * noise_biome->np->spread.Z);
482 Cache some ground type values for speed
485 // Creates variables c_name=id and n_name=node
486 #define CONTENT_VARIABLE(ndef, name)\
487 content_t c_##name = ndef->getId("mapgen_" #name);\
488 MapNode n_##name(c_##name);
489 // Default to something else if was CONTENT_IGNORE
490 #define CONTENT_VARIABLE_FALLBACK(name, dname)\
491 if(c_##name == CONTENT_IGNORE){\
492 c_##name = c_##dname;\
493 n_##name = n_##dname;\
496 CONTENT_VARIABLE(ndef, stone);
497 CONTENT_VARIABLE(ndef, air);
498 CONTENT_VARIABLE(ndef, water_source);
499 CONTENT_VARIABLE(ndef, dirt);
500 CONTENT_VARIABLE(ndef, sand);
501 CONTENT_VARIABLE(ndef, gravel);
502 CONTENT_VARIABLE(ndef, clay);
503 CONTENT_VARIABLE(ndef, lava_source);
504 CONTENT_VARIABLE(ndef, cobble);
505 CONTENT_VARIABLE(ndef, mossycobble);
506 CONTENT_VARIABLE(ndef, dirt_with_grass);
507 CONTENT_VARIABLE(ndef, junglegrass);
508 CONTENT_VARIABLE(ndef, stone_with_coal);
509 CONTENT_VARIABLE(ndef, stone_with_iron);
510 CONTENT_VARIABLE(ndef, mese);
511 CONTENT_VARIABLE(ndef, desert_sand);
512 CONTENT_VARIABLE_FALLBACK(desert_sand, sand);
513 CONTENT_VARIABLE(ndef, desert_stone);
514 CONTENT_VARIABLE_FALLBACK(desert_stone, stone);
516 // Maximum height of the stone surface and obstacles.
517 // This is used to guide the cave generation
518 s16 stone_surface_max_y = 0;
521 Generate general ground level to full area
525 TimeTaker timer1("Generating ground level");
527 for(s16 x=node_min.X; x<=node_max.X; x++)
528 for(s16 z=node_min.Z; z<=node_max.Z; z++)
531 v2s16 p2d = v2s16(x,z);
534 Skip of already generated
537 v3s16 p(p2d.X, node_min.Y, p2d.Y);
538 if(vmanip.m_data[vmanip.m_area.index(p)].d != CONTENT_AIR)
542 // Ground height at this point
543 float surface_y_f = 0.0;
545 // Use perlin noise for ground height
546 surface_y_f = base_rock_level_2d(data->seed, p2d);
548 /*// Experimental stuff
550 float a = highlands_level_2d(data->seed, p2d);
555 // Convert to integer
556 s16 surface_y = (s16)surface_y_f;
559 if(surface_y > stone_surface_max_y)
560 stone_surface_max_y = surface_y;
562 BiomeType bt = get_biome(data->seed, p2d);
564 Fill ground with stone
567 // Use fast index incrementing
568 v3s16 em = vmanip.m_area.getExtent();
569 u32 i = vmanip.m_area.index(v3s16(p2d.X, node_min.Y, p2d.Y));
570 for(s16 y=node_min.Y; y<=node_max.Y; y++)
572 if(vmanip.m_data[i].getContent() == CONTENT_IGNORE){
574 if(y > water_level && bt == BT_DESERT)
575 vmanip.m_data[i] = n_desert_stone;
577 vmanip.m_data[i] = n_stone;
578 } else if(y <= water_level){
579 vmanip.m_data[i] = MapNode(c_water_source);
581 vmanip.m_data[i] = MapNode(c_air);
584 vmanip.m_area.add_y(em, i, 1);
592 // Limit dirt flow area by 1 because mud is flown into neighbors.
593 assert(central_area_size.X == central_area_size.Z);
594 s16 mudflow_minpos = 0-max_spread_amount+1;
595 s16 mudflow_maxpos = central_area_size.X+max_spread_amount-2;
598 Loop this part, it will make stuff look older and newer nicely
601 /*double cave_amount = 6.0 + 6.0 * noise2d_perlin(
602 0.5+(double)node_min.X/250, 0.5+(double)node_min.Y/250,
603 data->seed+34329, 3, 0.50);*/
605 double cave_amount = NoisePerlin2D(np_cave, node_min.X, node_min.Y, data->seed);
607 const u32 age_loops = 2;
608 for(u32 i_age=0; i_age<age_loops; i_age++)
610 /******************************
611 BEGINNING OF AGING LOOP
612 ******************************/
617 //TimeTaker timer1("caves");
620 Make caves (this code is relatively horrible)
622 cave_amount = MYMAX(0.0, cave_amount);
623 u32 caves_count = cave_amount * volume_nodes / 50000;
624 u32 bruises_count = 1;
625 PseudoRandom ps(blockseed+21343);
626 PseudoRandom ps2(blockseed+1032);
627 if(ps.range(1, 6) == 1)
628 bruises_count = ps.range(0, ps.range(0, 2));
629 if(get_biome(data->seed, v2s16(node_min.X, node_min.Z)) == BT_DESERT){
633 for(u32 jj=0; jj<caves_count+bruises_count; jj++)
635 if (!(flags & MG_CAVES))
638 /*int avg_height = (int)
639 ((base_rock_level_2d(data->seed, v2s16(node_min.X, node_min.Z)) +
640 base_rock_level_2d(data->seed, v2s16(node_max.X, node_max.Z))) / 2);
641 if ((node_max.Y + node_min.Y) / 2 > avg_height)
644 bool large_cave = (jj >= caves_count);
645 s16 min_tunnel_diameter = 2;
646 s16 max_tunnel_diameter = ps.range(2,6);
647 int dswitchint = ps.range(1,14);
648 u16 tunnel_routepoints = 0;
649 int part_max_length_rs = 0;
651 part_max_length_rs = ps.range(2,4);
652 tunnel_routepoints = ps.range(5, ps.range(15,30));
653 min_tunnel_diameter = 5;
654 max_tunnel_diameter = ps.range(7, ps.range(8,24));
656 part_max_length_rs = ps.range(2,9);
657 tunnel_routepoints = ps.range(10, ps.range(15,30));
659 bool large_cave_is_flat = (ps.range(0,1) == 0);
661 v3f main_direction(0,0,0);
663 // Allowed route area size in nodes
664 v3s16 ar = central_area_size;
666 // Area starting point in nodes
670 //(this should be more than the maximum radius of the tunnel)
672 s16 more = max_spread_amount - max_tunnel_diameter/2 - insure;
673 ar += v3s16(1,0,1) * more * 2;
674 of -= v3s16(1,0,1) * more;
677 // Allow half a diameter + 7 over stone surface
678 s16 route_y_max = -of.Y + stone_surface_max_y + max_tunnel_diameter/2 + 7;
680 // Limit maximum to area
681 route_y_max = rangelim(route_y_max, 0, ar.Y-1);
686 if(node_min.Y < water_level && node_max.Y > water_level)
688 min = water_level - max_tunnel_diameter/3 - of.Y;
689 route_y_max = water_level + max_tunnel_diameter/3 - of.Y;
691 route_y_min = ps.range(min, min + max_tunnel_diameter);
692 route_y_min = rangelim(route_y_min, 0, route_y_max);
695 s16 route_start_y_min = route_y_min;
696 s16 route_start_y_max = route_y_max;
698 route_start_y_min = rangelim(route_start_y_min, 0, ar.Y-1);
699 route_start_y_max = rangelim(route_start_y_max, route_start_y_min, ar.Y-1);
701 // Randomize starting position
703 (float)(ps.next()%ar.X)+0.5,
704 (float)(ps.range(route_start_y_min, route_start_y_max))+0.5,
705 (float)(ps.next()%ar.Z)+0.5
708 v3s16 startp(orp.X, orp.Y, orp.Z);
711 MapNode airnode(CONTENT_AIR);
712 MapNode waternode(c_water_source);
713 MapNode lavanode(c_lava_source);
716 Generate some tunnel starting from orp
719 for(u16 j=0; j<tunnel_routepoints; j++)
721 if(j%dswitchint==0 && large_cave == false)
723 main_direction = v3f(
724 ((float)(ps.next()%20)-(float)10)/10,
725 ((float)(ps.next()%20)-(float)10)/30,
726 ((float)(ps.next()%20)-(float)10)/10
728 main_direction *= (float)ps.range(0, 10)/10;
732 s16 min_d = min_tunnel_diameter;
733 s16 max_d = max_tunnel_diameter;
734 s16 rs = ps.range(min_d, max_d);
736 // Every second section is rough
737 bool randomize_xz = (ps2.range(1,2) == 1);
743 rs*part_max_length_rs,
744 rs*part_max_length_rs/2,
745 rs*part_max_length_rs
751 rs*part_max_length_rs,
752 ps.range(1, rs*part_max_length_rs),
753 rs*part_max_length_rs
760 (float)(ps.next()%(maxlen.X*1))-(float)maxlen.X/2,
761 (float)(ps.next()%(maxlen.Y*1))-(float)maxlen.Y/2,
762 (float)(ps.next()%(maxlen.Z*1))-(float)maxlen.Z/2
765 // Jump downward sometimes
766 if(!large_cave && ps.range(0,12) == 0)
769 (float)(ps.next()%(maxlen.X*1))-(float)maxlen.X/2,
770 (float)(ps.next()%(maxlen.Y*2))-(float)maxlen.Y*2/2,
771 (float)(ps.next()%(maxlen.Z*1))-(float)maxlen.Z/2
777 s16 h = find_ground_level_clever(vmanip,
778 v2s16(p.X, p.Z), ndef);
779 route_y_min = h - rs/3;
780 route_y_max = h + rs;
783 vec += main_direction;
788 else if(rp.X >= ar.X)
790 if(rp.Y < route_y_min)
792 else if(rp.Y >= route_y_max)
793 rp.Y = route_y_max-1;
796 else if(rp.Z >= ar.Z)
800 for(float f=0; f<1.0; f+=1.0/vec.getLength())
802 v3f fp = orp + vec * f;
803 fp.X += 0.1*ps.range(-10,10);
804 fp.Z += 0.1*ps.range(-10,10);
805 v3s16 cp(fp.X, fp.Y, fp.Z);
810 d0 += ps.range(-1,1);
811 d1 += ps.range(-1,1);
813 for(s16 z0=d0; z0<=d1; z0++)
815 s16 si = rs/2 - MYMAX(0, abs(z0)-rs/7-1);
816 for(s16 x0=-si-ps.range(0,1); x0<=si-1+ps.range(0,1); x0++)
818 s16 maxabsxz = MYMAX(abs(x0), abs(z0));
819 s16 si2 = rs/2 - MYMAX(0, maxabsxz-rs/7-1);
820 for(s16 y0=-si2; y0<=si2; y0++)
822 /*// Make better floors in small caves
823 if(y0 <= -rs/2 && rs<=7)
825 if(large_cave_is_flat){
826 // Make large caves not so tall
827 if(rs > 7 && abs(y0) >= rs/3)
837 if(vmanip.m_area.contains(p) == false)
840 u32 i = vmanip.m_area.index(p);
844 if(full_node_min.Y < water_level &&
845 full_node_max.Y > water_level){
846 if(p.Y <= water_level)
847 vmanip.m_data[i] = waternode;
849 vmanip.m_data[i] = airnode;
850 } else if(full_node_max.Y < water_level){
851 if(p.Y < startp.Y - 2)
852 vmanip.m_data[i] = lavanode;
854 vmanip.m_data[i] = airnode;
856 vmanip.m_data[i] = airnode;
859 // Don't replace air or water or lava or ignore
860 if(vmanip.m_data[i].getContent() == CONTENT_IGNORE ||
861 vmanip.m_data[i].getContent() == CONTENT_AIR ||
862 vmanip.m_data[i].getContent() == c_water_source ||
863 vmanip.m_data[i].getContent() == c_lava_source)
866 vmanip.m_data[i] = airnode;
869 vmanip.m_flags[i] |= VMANIP_FLAG_CAVE;
887 TimeTaker timer1("add mud");
890 Add mud to the central chunk
893 for(s16 x=node_min.X; x<=node_max.X; x++)
894 for(s16 z=node_min.Z; z<=node_max.Z; z++)
896 // Node position in 2d
897 v2s16 p2d = v2s16(x,z);
899 // Randomize mud amount
900 s16 mud_add_amount = get_mud_add_amount(data->seed, p2d) / 2.0 + 0.5;
903 s16 surface_y = find_stone_level(vmanip, p2d, ndef);
904 // Handle area not found
905 if(surface_y == vmanip.m_area.MinEdge.Y - 1)
908 MapNode addnode(c_dirt);
909 BiomeType bt = get_biome(data->seed, p2d);
912 addnode = MapNode(c_desert_sand);
914 if(bt == BT_DESERT && surface_y + mud_add_amount <= water_level+1){
915 addnode = MapNode(c_sand);
916 } else if(mud_add_amount <= 0){
917 mud_add_amount = 1 - mud_add_amount;
918 addnode = MapNode(c_gravel);
919 } else if(bt == BT_NORMAL && get_have_beach(data->seed, p2d) &&
920 surface_y + mud_add_amount <= water_level+2){
921 addnode = MapNode(c_sand);
926 mud_add_amount = MYMAX(0, mud_add_amount - (surface_y - 20)/5);
931 If topmost node is grass, change it to mud.
932 It might be if it was flown to there from a neighboring
933 chunk and then converted.
936 u32 i = vmanip.m_area.index(v3s16(p2d.X, surface_y, p2d.Y));
937 MapNode *n = &vmanip.m_data[i];
938 if(n->getContent() == c_dirt_with_grass)
939 *n = MapNode(c_dirt);
947 v3s16 em = vmanip.m_area.getExtent();
948 s16 y_start = surface_y+1;
949 u32 i = vmanip.m_area.index(v3s16(p2d.X, y_start, p2d.Y));
950 for(s16 y=y_start; y<=node_max.Y; y++)
952 if(mudcount >= mud_add_amount)
955 MapNode &n = vmanip.m_data[i];
959 vmanip.m_area.add_y(em, i, 1);
969 Add blobs of dirt and gravel underground
971 if(get_biome(data->seed, v2s16(node_min.X, node_min.Z)) == BT_NORMAL)
973 PseudoRandom pr(blockseed+983);
974 for(int i=0; i<volume_nodes/10/10/10; i++)
976 bool only_fill_cave = (myrand_range(0,1) != 0);
983 pr.range(node_min.X, node_max.X)-size.X/2,
984 pr.range(node_min.Y, node_max.Y)-size.Y/2,
985 pr.range(node_min.Z, node_max.Z)-size.Z/2
988 if(p0.Y > -32 && pr.range(0,1) == 0)
989 n1 = MapNode(c_dirt);
991 n1 = MapNode(c_gravel);
992 for(int x1=0; x1<size.X; x1++)
993 for(int y1=0; y1<size.Y; y1++)
994 for(int z1=0; z1<size.Z; z1++)
996 v3s16 p = p0 + v3s16(x1,y1,z1);
997 u32 i = vmanip.m_area.index(p);
998 if(!vmanip.m_area.contains(i))
1000 // Cancel if not stone and not cave air
1001 if(vmanip.m_data[i].getContent() != c_stone &&
1002 !(vmanip.m_flags[i] & VMANIP_FLAG_CAVE))
1004 if(only_fill_cave && !(vmanip.m_flags[i] & VMANIP_FLAG_CAVE))
1006 vmanip.m_data[i] = n1;
1014 TimeTaker timer1("flow mud");
1017 Flow mud away from steep edges
1020 // Iterate a few times
1021 for(s16 k=0; k<3; k++)
1024 for(s16 x=mudflow_minpos; x<=mudflow_maxpos; x++)
1025 for(s16 z=mudflow_minpos; z<=mudflow_maxpos; z++)
1027 // Invert coordinates every 2nd iteration
1030 x = mudflow_maxpos - (x-mudflow_minpos);
1031 z = mudflow_maxpos - (z-mudflow_minpos);
1034 // Node position in 2d
1035 v2s16 p2d = v2s16(node_min.X, node_min.Z) + v2s16(x,z);
1037 v3s16 em = vmanip.m_area.getExtent();
1038 u32 i = vmanip.m_area.index(v3s16(p2d.X, node_max.Y, p2d.Y));
1041 while(y >= node_min.Y)
1048 for(; y>=node_min.Y; y--)
1050 n = &vmanip.m_data[i];
1051 //if(content_walkable(n->d))
1053 if(n->getContent() == c_dirt ||
1054 n->getContent() == c_dirt_with_grass ||
1055 n->getContent() == c_gravel)
1058 vmanip.m_area.add_y(em, i, -1);
1061 // Stop if out of area
1062 //if(vmanip.m_area.contains(i) == false)
1066 /*// If not mud, do nothing to it
1067 MapNode *n = &vmanip.m_data[i];
1068 if(n->d != CONTENT_MUD && n->d != CONTENT_GRASS)
1071 if(n->getContent() == c_dirt ||
1072 n->getContent() == c_dirt_with_grass)
1074 // Make it exactly mud
1075 n->setContent(c_dirt);
1078 Don't flow it if the stuff under it is not mud
1082 vmanip.m_area.add_y(em, i2, -1);
1083 // Cancel if out of area
1084 if(vmanip.m_area.contains(i2) == false)
1086 MapNode *n2 = &vmanip.m_data[i2];
1087 if(n2->getContent() != c_dirt &&
1088 n2->getContent() != c_dirt_with_grass)
1093 /*s16 recurse_count = 0;
1097 v3s16(0,0,1), // back
1098 v3s16(1,0,0), // right
1099 v3s16(0,0,-1), // front
1100 v3s16(-1,0,0), // left
1103 // Theck that upper is air or doesn't exist.
1104 // Cancel dropping if upper keeps it in place
1106 vmanip.m_area.add_y(em, i3, 1);
1107 if(vmanip.m_area.contains(i3) == true
1108 && ndef->get(vmanip.m_data[i3]).walkable)
1115 for(u32 di=0; di<4; di++)
1117 v3s16 dirp = dirs4[di];
1120 vmanip.m_area.add_p(em, i2, dirp);
1121 // Fail if out of area
1122 if(vmanip.m_area.contains(i2) == false)
1124 // Check that side is air
1125 MapNode *n2 = &vmanip.m_data[i2];
1126 if(ndef->get(*n2).walkable)
1128 // Check that under side is air
1129 vmanip.m_area.add_y(em, i2, -1);
1130 if(vmanip.m_area.contains(i2) == false)
1132 n2 = &vmanip.m_data[i2];
1133 if(ndef->get(*n2).walkable)
1135 /*// Check that under that is air (need a drop of 2)
1136 vmanip.m_area.add_y(em, i2, -1);
1137 if(vmanip.m_area.contains(i2) == false)
1139 n2 = &vmanip.m_data[i2];
1140 if(content_walkable(n2->d))
1142 // Loop further down until not air
1143 bool dropped_to_unknown = false;
1145 vmanip.m_area.add_y(em, i2, -1);
1146 n2 = &vmanip.m_data[i2];
1147 // if out of known area
1148 if(vmanip.m_area.contains(i2) == false
1149 || n2->getContent() == CONTENT_IGNORE){
1150 dropped_to_unknown = true;
1153 }while(ndef->get(*n2).walkable == false);
1154 // Loop one up so that we're in air
1155 vmanip.m_area.add_y(em, i2, 1);
1156 n2 = &vmanip.m_data[i2];
1158 bool old_is_water = (n->getContent() == c_water_source);
1159 // Move mud to new place
1160 if(!dropped_to_unknown) {
1162 // Set old place to be air (or water)
1164 *n = MapNode(c_water_source);
1166 *n = MapNode(CONTENT_AIR);
1182 /***********************
1184 ************************/
1187 Add top and bottom side of water to transforming_liquid queue
1190 for(s16 x=full_node_min.X; x<=full_node_max.X; x++)
1191 for(s16 z=full_node_min.Z; z<=full_node_max.Z; z++)
1196 bool water_found = false;
1197 // Use fast index incrementing
1198 v3s16 em = vmanip.m_area.getExtent();
1199 u32 i = vmanip.m_area.index(v3s16(p2d.X, full_node_max.Y, p2d.Y));
1200 for(s16 y=full_node_max.Y; y>=full_node_min.Y; y--)
1202 if(y == full_node_max.Y){
1204 (vmanip.m_data[i].getContent() == c_water_source ||
1205 vmanip.m_data[i].getContent() == c_lava_source);
1207 else if(water_found == false)
1209 if(vmanip.m_data[i].getContent() == c_water_source ||
1210 vmanip.m_data[i].getContent() == c_lava_source)
1212 v3s16 p = v3s16(p2d.X, y, p2d.Y);
1213 data->transforming_liquid.push_back(p);
1219 // This can be done because water_found can only
1220 // turn to true and end up here after going through
1222 if(vmanip.m_data[i+1].getContent() != c_water_source ||
1223 vmanip.m_data[i+1].getContent() != c_lava_source)
1225 v3s16 p = v3s16(p2d.X, y+1, p2d.Y);
1226 data->transforming_liquid.push_back(p);
1227 water_found = false;
1231 vmanip.m_area.add_y(em, i, -1);
1240 for(s16 x=full_node_min.X; x<=full_node_max.X; x++)
1241 for(s16 z=full_node_min.Z; z<=full_node_max.Z; z++)
1243 // Node position in 2d
1244 v2s16 p2d = v2s16(x,z);
1247 Find the lowest surface to which enough light ends up
1250 Basically just wait until not air and not leaves.
1254 v3s16 em = vmanip.m_area.getExtent();
1255 u32 i = vmanip.m_area.index(v3s16(p2d.X, node_max.Y, p2d.Y));
1257 // Go to ground level
1258 for(y=node_max.Y; y>=full_node_min.Y; y--)
1260 MapNode &n = vmanip.m_data[i];
1261 if(ndef->get(n).param_type != CPT_LIGHT
1262 || ndef->get(n).liquid_type != LIQUID_NONE)
1264 vmanip.m_area.add_y(em, i, -1);
1266 if(y >= full_node_min.Y)
1269 surface_y = full_node_min.Y;
1272 u32 i = vmanip.m_area.index(p2d.X, surface_y, p2d.Y);
1273 MapNode *n = &vmanip.m_data[i];
1274 if(n->getContent() == c_dirt){
1275 // Well yeah, this can't be overground...
1276 if(surface_y < water_level - 20)
1278 n->setContent(c_dirt_with_grass);
1285 assert(central_area_size.X == central_area_size.Z);
1286 if (flags & MG_TREES) {
1287 // Divide area into parts
1289 s16 sidelen = central_area_size.X / div;
1290 double area = sidelen * sidelen;
1291 for(s16 x0=0; x0<div; x0++)
1292 for(s16 z0=0; z0<div; z0++)
1294 // Center position of part of division
1296 node_min.X + sidelen/2 + sidelen*x0,
1297 node_min.Z + sidelen/2 + sidelen*z0
1299 // Minimum edge of part of division
1301 node_min.X + sidelen*x0,
1302 node_min.Z + sidelen*z0
1304 // Maximum edge of part of division
1306 node_min.X + sidelen + sidelen*x0 - 1,
1307 node_min.Z + sidelen + sidelen*z0 - 1
1310 u32 tree_count = area * tree_amount_2d(data->seed, p2d_center);
1311 // Put trees in random places on part of division
1312 for(u32 i=0; i<tree_count; i++)
1314 s16 x = myrand_range(p2d_min.X, p2d_max.X);
1315 s16 z = myrand_range(p2d_min.Y, p2d_max.Y);
1316 s16 y = find_ground_level(vmanip, v2s16(x,z), ndef);
1317 // Don't make a tree under water level
1320 // Don't make a tree so high that it doesn't fit
1321 if(y > node_max.Y - 6)
1325 Trees grow only on mud and grass
1328 u32 i = vmanip.m_area.index(v3s16(p));
1329 MapNode *n = &vmanip.m_data[i];
1330 if(n->getContent() != c_dirt
1331 && n->getContent() != c_dirt_with_grass)
1336 make_tree(vmanip, p, false, ndef);
1346 ScopeProfiler sp(g_profiler, "EmergeThread: mapgen lighting update",
1348 //VoxelArea a(node_min, node_max);
1349 VoxelArea a(node_min-v3s16(1,0,1)*MAP_BLOCKSIZE,
1350 node_max+v3s16(1,0,1)*MAP_BLOCKSIZE);
1351 /*VoxelArea a(node_min-v3s16(1,0,1)*MAP_BLOCKSIZE/2,
1352 node_max+v3s16(1,0,1)*MAP_BLOCKSIZE/2);*/
1353 enum LightBank banks[2] = {LIGHTBANK_DAY, LIGHTBANK_NIGHT};
1354 for(int i=0; i<2; i++)
1356 enum LightBank bank = banks[i];
1358 core::map<v3s16, bool> light_sources;
1359 core::map<v3s16, u8> unlight_from;
1361 voxalgo::clearLightAndCollectSources(vmanip, a, bank, ndef,
1362 light_sources, unlight_from);
1364 bool inexistent_top_provides_sunlight = !block_is_underground;
1365 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
1366 vmanip, a, inexistent_top_provides_sunlight,
1367 light_sources, ndef);
1368 // TODO: Do stuff according to bottom_sunlight_valid
1370 vmanip.unspreadLight(bank, unlight_from, light_sources, ndef);
1372 vmanip.spreadLight(bank, light_sources, ndef);