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 double MapgenV6::baseRockLevelFromNoise(v2s16 p) {
315 double base = water_level +
316 NoisePerlin2DPosOffset(noise_terrain_base->np, p.X, 0.5, p.Y, 0.5, seed);
317 double higher = water_level +
318 NoisePerlin2DPosOffset(noise_terrain_higher->np, p.X, 0.5, p.Y, 0.5, seed);
323 double b = NoisePerlin2DPosOffset(noise_steepness->np, p.X, 0.5, p.Y, 0.5, seed);
324 b = rangelim(b, 0.0, 1000.0);
327 b = rangelim(b, 0.5, 1000.0);
329 if(b > 1.5 && b < 100.0){
336 double a_off = -0.20;
337 double a = 0.5 + b * (a_off + NoisePerlin2DNoTxfmPosOffset(
338 noise_height_select->np, p.X, 0.5, p.Y, 0.5, seed));
339 a = rangelim(a, 0.0, 1.0);
341 return base * (1.0 - a) + higher * a;
345 s16 MapgenV6::find_ground_level_from_noise(u64 seed, v2s16 p2d, s16 precision)
347 return baseRockLevelFromNoise(p2d) + AVERAGE_MUD_AMOUNT;
350 double MapgenV6::get_mud_add_amount(u64 seed, v2s16 p)
352 /*return ((float)AVERAGE_MUD_AMOUNT + 2.0 * noise2d_perlin(
353 0.5+(float)p.X/200, 0.5+(float)p.Y/200,
354 seed+91013, 3, 0.55));*/
355 int index = (p.Y - node_min.Z) * ystride + (p.X - node_min.X);
356 return map_mud[index];
359 bool MapgenV6::get_have_beach(u64 seed, v2s16 p2d)
361 // Determine whether to have sand here
362 /*double sandnoise = noise2d_perlin(
363 0.2+(float)p2d.X/250, 0.7+(float)p2d.Y/250,
364 seed+59420, 3, 0.50);*/
365 int index = (p2d.Y - node_min.Z) * ystride + (p2d.X - node_min.X);
366 double sandnoise = map_beach[index];
368 return (sandnoise > freq_beach);
371 BiomeType MapgenV6::get_biome(u64 seed, v2s16 p2d)
373 // Just do something very simple as for now
374 /*double d = noise2d_perlin(
375 0.6+(float)p2d.X/250, 0.2+(float)p2d.Y/250,
376 seed+9130, 3, 0.50);*/
377 int index = (p2d.Y - node_min.Z) * ystride + (p2d.X - node_min.X);
378 double d = map_biome[index];
381 if (flags & MGV6_BIOME_BLEND) {
382 if(d > freq_desert - 0.10 &&
383 (noise2d(p2d.X, p2d.Y, seed) + 1.0) > (freq_desert - d) * 20.0)
389 u32 MapgenV6::get_blockseed(u64 seed, v3s16 p)
391 s32 x=p.X, y=p.Y, z=p.Z;
392 return (u32)(seed%0x100000000ULL) + z*38134234 + y*42123 + x*23;
396 int MapgenV6::getGroundLevelAtPoint(v2s16 p) {
397 return baseRockLevelFromNoise(p) + AVERAGE_MUD_AMOUNT;
401 #define VMANIP_FLAG_CAVE VOXELFLAG_CHECKED1
403 void MapgenV6::makeChunk(BlockMakeData *data)
407 //dstream<<"makeBlock: no-op"<<std::endl;
411 this->generating = true;
413 assert(data->vmanip);
414 assert(data->nodedef);
415 assert(data->blockpos_requested.X >= data->blockpos_min.X &&
416 data->blockpos_requested.Y >= data->blockpos_min.Y &&
417 data->blockpos_requested.Z >= data->blockpos_min.Z);
418 assert(data->blockpos_requested.X <= data->blockpos_max.X &&
419 data->blockpos_requested.Y <= data->blockpos_max.Y &&
420 data->blockpos_requested.Z <= data->blockpos_max.Z);
422 INodeDefManager *ndef = data->nodedef;
424 // Hack: use minimum block coordinates for old code that assumes
426 v3s16 blockpos = data->blockpos_requested;
428 /*dstream<<"makeBlock(): ("<<blockpos.X<<","<<blockpos.Y<<","
429 <<blockpos.Z<<")"<<std::endl;*/
431 v3s16 blockpos_min = data->blockpos_min;
432 v3s16 blockpos_max = data->blockpos_max;
433 v3s16 blockpos_full_min = blockpos_min - v3s16(1,1,1);
434 v3s16 blockpos_full_max = blockpos_max + v3s16(1,1,1);
436 ManualMapVoxelManipulator &vmanip = *(data->vmanip);
437 // Area of central chunk
438 node_min = blockpos_min*MAP_BLOCKSIZE;
439 node_max = (blockpos_max+v3s16(1,1,1))*MAP_BLOCKSIZE-v3s16(1,1,1);
440 // Full allocated area
441 v3s16 full_node_min = (blockpos_min-1)*MAP_BLOCKSIZE;
442 v3s16 full_node_max = (blockpos_max+2)*MAP_BLOCKSIZE-v3s16(1,1,1);
444 v3s16 central_area_size = node_max - node_min + v3s16(1,1,1);
446 const s16 max_spread_amount = MAP_BLOCKSIZE;
448 int volume_blocks = (blockpos_max.X - blockpos_min.X + 1)
449 * (blockpos_max.Y - blockpos_min.Y + 1)
450 * (blockpos_max.Z - blockpos_max.Z + 1);
452 int volume_nodes = volume_blocks *
453 MAP_BLOCKSIZE*MAP_BLOCKSIZE*MAP_BLOCKSIZE;
455 // Generated surface area
456 //double gen_area_nodes = MAP_BLOCKSIZE*MAP_BLOCKSIZE * rel_volume;
458 // Horribly wrong heuristic, but better than nothing
459 bool block_is_underground = (water_level > node_max.Y);
462 Create a block-specific seed
464 u32 blockseed = get_blockseed(data->seed, full_node_min);
473 // Need to adjust for the original implementation's +.5 offset...
474 noise_terrain_base->perlinMap2D(
475 x + 0.5 * noise_terrain_base->np->spread.X,
476 z + 0.5 * noise_terrain_base->np->spread.Z);
477 noise_terrain_base->transformNoiseMap();
479 noise_terrain_higher->perlinMap2D(
480 x + 0.5 * noise_terrain_higher->np->spread.X,
481 z + 0.5 * noise_terrain_higher->np->spread.Z);
482 noise_terrain_higher->transformNoiseMap();
484 noise_steepness->perlinMap2D(
485 x + 0.5 * noise_steepness->np->spread.X,
486 z + 0.5 * noise_steepness->np->spread.Z);
487 noise_steepness->transformNoiseMap();
489 noise_height_select->perlinMap2D(
490 x + 0.5 * noise_height_select->np->spread.X,
491 z + 0.5 * noise_height_select->np->spread.Z);
493 noise_trees->perlinMap2D(
494 x + 0.5 * noise_trees->np->spread.X,
495 z + 0.5 * noise_trees->np->spread.Z);
497 noise_mud->perlinMap2D(
498 x + 0.5 * noise_mud->np->spread.X,
499 z + 0.5 * noise_mud->np->spread.Z);
500 noise_mud->transformNoiseMap();
502 noise_beach->perlinMap2D(
503 x + 0.2 * noise_beach->np->spread.X,
504 z + 0.7 * noise_beach->np->spread.Z);
506 noise_biome->perlinMap2D(
507 x + 0.6 * noise_biome->np->spread.X,
508 z + 0.2 * noise_biome->np->spread.Z);
513 Cache some ground type values for speed
516 // Creates variables c_name=id and n_name=node
517 #define CONTENT_VARIABLE(ndef, name)\
518 content_t c_##name = ndef->getId("mapgen_" #name);\
519 MapNode n_##name(c_##name);
520 // Default to something else if was CONTENT_IGNORE
521 #define CONTENT_VARIABLE_FALLBACK(name, dname)\
522 if(c_##name == CONTENT_IGNORE){\
523 c_##name = c_##dname;\
524 n_##name = n_##dname;\
527 CONTENT_VARIABLE(ndef, stone);
528 CONTENT_VARIABLE(ndef, air);
529 CONTENT_VARIABLE(ndef, water_source);
530 CONTENT_VARIABLE(ndef, dirt);
531 CONTENT_VARIABLE(ndef, sand);
532 CONTENT_VARIABLE(ndef, gravel);
533 CONTENT_VARIABLE(ndef, clay);
534 CONTENT_VARIABLE(ndef, lava_source);
535 CONTENT_VARIABLE(ndef, cobble);
536 CONTENT_VARIABLE(ndef, mossycobble);
537 CONTENT_VARIABLE(ndef, dirt_with_grass);
538 CONTENT_VARIABLE(ndef, junglegrass);
539 CONTENT_VARIABLE(ndef, stone_with_coal);
540 CONTENT_VARIABLE(ndef, stone_with_iron);
541 CONTENT_VARIABLE(ndef, mese);
542 CONTENT_VARIABLE(ndef, desert_sand);
543 CONTENT_VARIABLE_FALLBACK(desert_sand, sand);
544 CONTENT_VARIABLE(ndef, desert_stone);
545 CONTENT_VARIABLE_FALLBACK(desert_stone, stone);
547 // Maximum height of the stone surface and obstacles.
548 // This is used to guide the cave generation
549 s16 stone_surface_max_y = 0;
552 Generate general ground level to full area
556 TimeTaker timer1("Generating ground level");
558 for(s16 x=node_min.X; x<=node_max.X; x++)
559 for(s16 z=node_min.Z; z<=node_max.Z; z++)
562 v2s16 p2d = v2s16(x,z);
565 Skip of already generated
568 v3s16 p(p2d.X, node_min.Y, p2d.Y);
569 if(vmanip.m_data[vmanip.m_area.index(p)].d != CONTENT_AIR)
573 // Ground height at this point
574 float surface_y_f = 0.0;
576 // Use perlin noise for ground height
577 surface_y_f = base_rock_level_2d(data->seed, p2d);
579 /*// Experimental stuff
581 float a = highlands_level_2d(data->seed, p2d);
586 // Convert to integer
587 s16 surface_y = (s16)surface_y_f;
590 if(surface_y > stone_surface_max_y)
591 stone_surface_max_y = surface_y;
593 BiomeType bt = get_biome(data->seed, p2d);
595 Fill ground with stone
598 // Use fast index incrementing
599 v3s16 em = vmanip.m_area.getExtent();
600 u32 i = vmanip.m_area.index(v3s16(p2d.X, node_min.Y, p2d.Y));
601 for(s16 y=node_min.Y; y<=node_max.Y; y++)
603 if(vmanip.m_data[i].getContent() == CONTENT_IGNORE){
605 if(y > water_level && bt == BT_DESERT)
606 vmanip.m_data[i] = n_desert_stone;
608 vmanip.m_data[i] = n_stone;
609 } else if(y <= water_level){
610 vmanip.m_data[i] = MapNode(c_water_source);
612 vmanip.m_data[i] = MapNode(c_air);
615 vmanip.m_area.add_y(em, i, 1);
623 // Limit dirt flow area by 1 because mud is flown into neighbors.
624 assert(central_area_size.X == central_area_size.Z);
625 s16 mudflow_minpos = 0-max_spread_amount+1;
626 s16 mudflow_maxpos = central_area_size.X+max_spread_amount-2;
629 Loop this part, it will make stuff look older and newer nicely
632 /*double cave_amount = 6.0 + 6.0 * noise2d_perlin(
633 0.5+(double)node_min.X/250, 0.5+(double)node_min.Y/250,
634 data->seed+34329, 3, 0.50);*/
636 double cave_amount = NoisePerlin2D(np_cave, node_min.X, node_min.Y, data->seed);
638 const u32 age_loops = 2;
639 for(u32 i_age=0; i_age<age_loops; i_age++)
641 /******************************
642 BEGINNING OF AGING LOOP
643 ******************************/
648 //TimeTaker timer1("caves");
651 Make caves (this code is relatively horrible)
653 cave_amount = MYMAX(0.0, cave_amount);
654 u32 caves_count = cave_amount * volume_nodes / 50000;
655 u32 bruises_count = 1;
656 PseudoRandom ps(blockseed+21343);
657 PseudoRandom ps2(blockseed+1032);
658 if(ps.range(1, 6) == 1)
659 bruises_count = ps.range(0, ps.range(0, 2));
660 if(get_biome(data->seed, v2s16(node_min.X, node_min.Z)) == BT_DESERT){
664 for(u32 jj=0; jj<caves_count+bruises_count; jj++)
666 if (!(flags & MG_CAVES))
669 /*int avg_height = (int)
670 ((base_rock_level_2d(data->seed, v2s16(node_min.X, node_min.Z)) +
671 base_rock_level_2d(data->seed, v2s16(node_max.X, node_max.Z))) / 2);
672 if ((node_max.Y + node_min.Y) / 2 > avg_height)
675 bool large_cave = (jj >= caves_count);
676 s16 min_tunnel_diameter = 2;
677 s16 max_tunnel_diameter = ps.range(2,6);
678 int dswitchint = ps.range(1,14);
679 u16 tunnel_routepoints = 0;
680 int part_max_length_rs = 0;
682 part_max_length_rs = ps.range(2,4);
683 tunnel_routepoints = ps.range(5, ps.range(15,30));
684 min_tunnel_diameter = 5;
685 max_tunnel_diameter = ps.range(7, ps.range(8,24));
687 part_max_length_rs = ps.range(2,9);
688 tunnel_routepoints = ps.range(10, ps.range(15,30));
690 bool large_cave_is_flat = (ps.range(0,1) == 0);
692 v3f main_direction(0,0,0);
694 // Allowed route area size in nodes
695 v3s16 ar = central_area_size;
697 // Area starting point in nodes
701 //(this should be more than the maximum radius of the tunnel)
703 s16 more = max_spread_amount - max_tunnel_diameter/2 - insure;
704 ar += v3s16(1,0,1) * more * 2;
705 of -= v3s16(1,0,1) * more;
708 // Allow half a diameter + 7 over stone surface
709 s16 route_y_max = -of.Y + stone_surface_max_y + max_tunnel_diameter/2 + 7;
711 // Limit maximum to area
712 route_y_max = rangelim(route_y_max, 0, ar.Y-1);
717 if(node_min.Y < water_level && node_max.Y > water_level)
719 min = water_level - max_tunnel_diameter/3 - of.Y;
720 route_y_max = water_level + max_tunnel_diameter/3 - of.Y;
722 route_y_min = ps.range(min, min + max_tunnel_diameter);
723 route_y_min = rangelim(route_y_min, 0, route_y_max);
726 s16 route_start_y_min = route_y_min;
727 s16 route_start_y_max = route_y_max;
729 route_start_y_min = rangelim(route_start_y_min, 0, ar.Y-1);
730 route_start_y_max = rangelim(route_start_y_max, route_start_y_min, ar.Y-1);
732 // Randomize starting position
734 (float)(ps.next()%ar.X)+0.5,
735 (float)(ps.range(route_start_y_min, route_start_y_max))+0.5,
736 (float)(ps.next()%ar.Z)+0.5
739 v3s16 startp(orp.X, orp.Y, orp.Z);
742 MapNode airnode(CONTENT_AIR);
743 MapNode waternode(c_water_source);
744 MapNode lavanode(c_lava_source);
747 Generate some tunnel starting from orp
750 for(u16 j=0; j<tunnel_routepoints; j++)
752 if(j%dswitchint==0 && large_cave == false)
754 main_direction = v3f(
755 ((float)(ps.next()%20)-(float)10)/10,
756 ((float)(ps.next()%20)-(float)10)/30,
757 ((float)(ps.next()%20)-(float)10)/10
759 main_direction *= (float)ps.range(0, 10)/10;
763 s16 min_d = min_tunnel_diameter;
764 s16 max_d = max_tunnel_diameter;
765 s16 rs = ps.range(min_d, max_d);
767 // Every second section is rough
768 bool randomize_xz = (ps2.range(1,2) == 1);
774 rs*part_max_length_rs,
775 rs*part_max_length_rs/2,
776 rs*part_max_length_rs
782 rs*part_max_length_rs,
783 ps.range(1, rs*part_max_length_rs),
784 rs*part_max_length_rs
791 (float)(ps.next()%(maxlen.X*1))-(float)maxlen.X/2,
792 (float)(ps.next()%(maxlen.Y*1))-(float)maxlen.Y/2,
793 (float)(ps.next()%(maxlen.Z*1))-(float)maxlen.Z/2
796 // Jump downward sometimes
797 if(!large_cave && ps.range(0,12) == 0)
800 (float)(ps.next()%(maxlen.X*1))-(float)maxlen.X/2,
801 (float)(ps.next()%(maxlen.Y*2))-(float)maxlen.Y*2/2,
802 (float)(ps.next()%(maxlen.Z*1))-(float)maxlen.Z/2
808 s16 h = find_ground_level_clever(vmanip,
809 v2s16(p.X, p.Z), ndef);
810 route_y_min = h - rs/3;
811 route_y_max = h + rs;
814 vec += main_direction;
819 else if(rp.X >= ar.X)
821 if(rp.Y < route_y_min)
823 else if(rp.Y >= route_y_max)
824 rp.Y = route_y_max-1;
827 else if(rp.Z >= ar.Z)
831 for(float f=0; f<1.0; f+=1.0/vec.getLength())
833 v3f fp = orp + vec * f;
834 fp.X += 0.1*ps.range(-10,10);
835 fp.Z += 0.1*ps.range(-10,10);
836 v3s16 cp(fp.X, fp.Y, fp.Z);
841 d0 += ps.range(-1,1);
842 d1 += ps.range(-1,1);
844 for(s16 z0=d0; z0<=d1; z0++)
846 s16 si = rs/2 - MYMAX(0, abs(z0)-rs/7-1);
847 for(s16 x0=-si-ps.range(0,1); x0<=si-1+ps.range(0,1); x0++)
849 s16 maxabsxz = MYMAX(abs(x0), abs(z0));
850 s16 si2 = rs/2 - MYMAX(0, maxabsxz-rs/7-1);
851 for(s16 y0=-si2; y0<=si2; y0++)
853 /*// Make better floors in small caves
854 if(y0 <= -rs/2 && rs<=7)
856 if(large_cave_is_flat){
857 // Make large caves not so tall
858 if(rs > 7 && abs(y0) >= rs/3)
868 if(vmanip.m_area.contains(p) == false)
871 u32 i = vmanip.m_area.index(p);
875 if(full_node_min.Y < water_level &&
876 full_node_max.Y > water_level){
877 if(p.Y <= water_level)
878 vmanip.m_data[i] = waternode;
880 vmanip.m_data[i] = airnode;
881 } else if(full_node_max.Y < water_level){
882 if(p.Y < startp.Y - 2)
883 vmanip.m_data[i] = lavanode;
885 vmanip.m_data[i] = airnode;
887 vmanip.m_data[i] = airnode;
890 // Don't replace air or water or lava or ignore
891 if(vmanip.m_data[i].getContent() == CONTENT_IGNORE ||
892 vmanip.m_data[i].getContent() == CONTENT_AIR ||
893 vmanip.m_data[i].getContent() == c_water_source ||
894 vmanip.m_data[i].getContent() == c_lava_source)
897 vmanip.m_data[i] = airnode;
900 vmanip.m_flags[i] |= VMANIP_FLAG_CAVE;
918 TimeTaker timer1("add mud");
921 Add mud to the central chunk
924 for(s16 x=node_min.X; x<=node_max.X; x++)
925 for(s16 z=node_min.Z; z<=node_max.Z; z++)
927 // Node position in 2d
928 v2s16 p2d = v2s16(x,z);
930 // Randomize mud amount
931 s16 mud_add_amount = get_mud_add_amount(data->seed, p2d) / 2.0 + 0.5;
934 s16 surface_y = find_stone_level(vmanip, p2d, ndef);
935 // Handle area not found
936 if(surface_y == vmanip.m_area.MinEdge.Y - 1)
939 MapNode addnode(c_dirt);
940 BiomeType bt = get_biome(data->seed, p2d);
943 addnode = MapNode(c_desert_sand);
945 if(bt == BT_DESERT && surface_y + mud_add_amount <= water_level+1){
946 addnode = MapNode(c_sand);
947 } else if(mud_add_amount <= 0){
948 mud_add_amount = 1 - mud_add_amount;
949 addnode = MapNode(c_gravel);
950 } else if(bt == BT_NORMAL && get_have_beach(data->seed, p2d) &&
951 surface_y + mud_add_amount <= water_level+2){
952 addnode = MapNode(c_sand);
957 mud_add_amount = MYMAX(0, mud_add_amount - (surface_y - 20)/5);
962 If topmost node is grass, change it to mud.
963 It might be if it was flown to there from a neighboring
964 chunk and then converted.
967 u32 i = vmanip.m_area.index(v3s16(p2d.X, surface_y, p2d.Y));
968 MapNode *n = &vmanip.m_data[i];
969 if(n->getContent() == c_dirt_with_grass)
970 *n = MapNode(c_dirt);
978 v3s16 em = vmanip.m_area.getExtent();
979 s16 y_start = surface_y+1;
980 u32 i = vmanip.m_area.index(v3s16(p2d.X, y_start, p2d.Y));
981 for(s16 y=y_start; y<=node_max.Y; y++)
983 if(mudcount >= mud_add_amount)
986 MapNode &n = vmanip.m_data[i];
990 vmanip.m_area.add_y(em, i, 1);
1000 Add blobs of dirt and gravel underground
1002 if(get_biome(data->seed, v2s16(node_min.X, node_min.Z)) == BT_NORMAL)
1004 PseudoRandom pr(blockseed+983);
1005 for(int i=0; i<volume_nodes/10/10/10; i++)
1007 bool only_fill_cave = (myrand_range(0,1) != 0);
1014 pr.range(node_min.X, node_max.X)-size.X/2,
1015 pr.range(node_min.Y, node_max.Y)-size.Y/2,
1016 pr.range(node_min.Z, node_max.Z)-size.Z/2
1019 if(p0.Y > -32 && pr.range(0,1) == 0)
1020 n1 = MapNode(c_dirt);
1022 n1 = MapNode(c_gravel);
1023 for(int x1=0; x1<size.X; x1++)
1024 for(int y1=0; y1<size.Y; y1++)
1025 for(int z1=0; z1<size.Z; z1++)
1027 v3s16 p = p0 + v3s16(x1,y1,z1);
1028 u32 i = vmanip.m_area.index(p);
1029 if(!vmanip.m_area.contains(i))
1031 // Cancel if not stone and not cave air
1032 if(vmanip.m_data[i].getContent() != c_stone &&
1033 !(vmanip.m_flags[i] & VMANIP_FLAG_CAVE))
1035 if(only_fill_cave && !(vmanip.m_flags[i] & VMANIP_FLAG_CAVE))
1037 vmanip.m_data[i] = n1;
1045 TimeTaker timer1("flow mud");
1048 Flow mud away from steep edges
1051 // Iterate a few times
1052 for(s16 k=0; k<3; k++)
1055 for(s16 x=mudflow_minpos; x<=mudflow_maxpos; x++)
1056 for(s16 z=mudflow_minpos; z<=mudflow_maxpos; z++)
1058 // Invert coordinates every 2nd iteration
1061 x = mudflow_maxpos - (x-mudflow_minpos);
1062 z = mudflow_maxpos - (z-mudflow_minpos);
1065 // Node position in 2d
1066 v2s16 p2d = v2s16(node_min.X, node_min.Z) + v2s16(x,z);
1068 v3s16 em = vmanip.m_area.getExtent();
1069 u32 i = vmanip.m_area.index(v3s16(p2d.X, node_max.Y, p2d.Y));
1072 while(y >= node_min.Y)
1079 for(; y>=node_min.Y; y--)
1081 n = &vmanip.m_data[i];
1082 //if(content_walkable(n->d))
1084 if(n->getContent() == c_dirt ||
1085 n->getContent() == c_dirt_with_grass ||
1086 n->getContent() == c_gravel)
1089 vmanip.m_area.add_y(em, i, -1);
1092 // Stop if out of area
1093 //if(vmanip.m_area.contains(i) == false)
1097 /*// If not mud, do nothing to it
1098 MapNode *n = &vmanip.m_data[i];
1099 if(n->d != CONTENT_MUD && n->d != CONTENT_GRASS)
1102 if(n->getContent() == c_dirt ||
1103 n->getContent() == c_dirt_with_grass)
1105 // Make it exactly mud
1106 n->setContent(c_dirt);
1109 Don't flow it if the stuff under it is not mud
1113 vmanip.m_area.add_y(em, i2, -1);
1114 // Cancel if out of area
1115 if(vmanip.m_area.contains(i2) == false)
1117 MapNode *n2 = &vmanip.m_data[i2];
1118 if(n2->getContent() != c_dirt &&
1119 n2->getContent() != c_dirt_with_grass)
1124 /*s16 recurse_count = 0;
1128 v3s16(0,0,1), // back
1129 v3s16(1,0,0), // right
1130 v3s16(0,0,-1), // front
1131 v3s16(-1,0,0), // left
1134 // Theck that upper is air or doesn't exist.
1135 // Cancel dropping if upper keeps it in place
1137 vmanip.m_area.add_y(em, i3, 1);
1138 if(vmanip.m_area.contains(i3) == true
1139 && ndef->get(vmanip.m_data[i3]).walkable)
1146 for(u32 di=0; di<4; di++)
1148 v3s16 dirp = dirs4[di];
1151 vmanip.m_area.add_p(em, i2, dirp);
1152 // Fail if out of area
1153 if(vmanip.m_area.contains(i2) == false)
1155 // Check that side is air
1156 MapNode *n2 = &vmanip.m_data[i2];
1157 if(ndef->get(*n2).walkable)
1159 // Check that under side is air
1160 vmanip.m_area.add_y(em, i2, -1);
1161 if(vmanip.m_area.contains(i2) == false)
1163 n2 = &vmanip.m_data[i2];
1164 if(ndef->get(*n2).walkable)
1166 /*// Check that under that is air (need a drop of 2)
1167 vmanip.m_area.add_y(em, i2, -1);
1168 if(vmanip.m_area.contains(i2) == false)
1170 n2 = &vmanip.m_data[i2];
1171 if(content_walkable(n2->d))
1173 // Loop further down until not air
1174 bool dropped_to_unknown = false;
1176 vmanip.m_area.add_y(em, i2, -1);
1177 n2 = &vmanip.m_data[i2];
1178 // if out of known area
1179 if(vmanip.m_area.contains(i2) == false
1180 || n2->getContent() == CONTENT_IGNORE){
1181 dropped_to_unknown = true;
1184 }while(ndef->get(*n2).walkable == false);
1185 // Loop one up so that we're in air
1186 vmanip.m_area.add_y(em, i2, 1);
1187 n2 = &vmanip.m_data[i2];
1189 bool old_is_water = (n->getContent() == c_water_source);
1190 // Move mud to new place
1191 if(!dropped_to_unknown) {
1193 // Set old place to be air (or water)
1195 *n = MapNode(c_water_source);
1197 *n = MapNode(CONTENT_AIR);
1213 /***********************
1215 ************************/
1218 Add top and bottom side of water to transforming_liquid queue
1221 for(s16 x=full_node_min.X; x<=full_node_max.X; x++)
1222 for(s16 z=full_node_min.Z; z<=full_node_max.Z; z++)
1227 bool water_found = false;
1228 // Use fast index incrementing
1229 v3s16 em = vmanip.m_area.getExtent();
1230 u32 i = vmanip.m_area.index(v3s16(p2d.X, full_node_max.Y, p2d.Y));
1231 for(s16 y=full_node_max.Y; y>=full_node_min.Y; y--)
1233 if(y == full_node_max.Y){
1235 (vmanip.m_data[i].getContent() == c_water_source ||
1236 vmanip.m_data[i].getContent() == c_lava_source);
1238 else if(water_found == false)
1240 if(vmanip.m_data[i].getContent() == c_water_source ||
1241 vmanip.m_data[i].getContent() == c_lava_source)
1243 v3s16 p = v3s16(p2d.X, y, p2d.Y);
1244 data->transforming_liquid.push_back(p);
1250 // This can be done because water_found can only
1251 // turn to true and end up here after going through
1253 if(vmanip.m_data[i+1].getContent() != c_water_source ||
1254 vmanip.m_data[i+1].getContent() != c_lava_source)
1256 v3s16 p = v3s16(p2d.X, y+1, p2d.Y);
1257 data->transforming_liquid.push_back(p);
1258 water_found = false;
1262 vmanip.m_area.add_y(em, i, -1);
1271 for(s16 x=full_node_min.X; x<=full_node_max.X; x++)
1272 for(s16 z=full_node_min.Z; z<=full_node_max.Z; z++)
1274 // Node position in 2d
1275 v2s16 p2d = v2s16(x,z);
1278 Find the lowest surface to which enough light ends up
1281 Basically just wait until not air and not leaves.
1285 v3s16 em = vmanip.m_area.getExtent();
1286 u32 i = vmanip.m_area.index(v3s16(p2d.X, node_max.Y, p2d.Y));
1288 // Go to ground level
1289 for(y=node_max.Y; y>=full_node_min.Y; y--)
1291 MapNode &n = vmanip.m_data[i];
1292 if(ndef->get(n).param_type != CPT_LIGHT
1293 || ndef->get(n).liquid_type != LIQUID_NONE)
1295 vmanip.m_area.add_y(em, i, -1);
1297 if(y >= full_node_min.Y)
1300 surface_y = full_node_min.Y;
1303 u32 i = vmanip.m_area.index(p2d.X, surface_y, p2d.Y);
1304 MapNode *n = &vmanip.m_data[i];
1305 if(n->getContent() == c_dirt){
1306 // Well yeah, this can't be overground...
1307 if(surface_y < water_level - 20)
1309 n->setContent(c_dirt_with_grass);
1316 assert(central_area_size.X == central_area_size.Z);
1317 if (flags & MG_TREES) {
1318 // Divide area into parts
1320 s16 sidelen = central_area_size.X / div;
1321 double area = sidelen * sidelen;
1322 for(s16 x0=0; x0<div; x0++)
1323 for(s16 z0=0; z0<div; z0++)
1325 // Center position of part of division
1327 node_min.X + sidelen/2 + sidelen*x0,
1328 node_min.Z + sidelen/2 + sidelen*z0
1330 // Minimum edge of part of division
1332 node_min.X + sidelen*x0,
1333 node_min.Z + sidelen*z0
1335 // Maximum edge of part of division
1337 node_min.X + sidelen + sidelen*x0 - 1,
1338 node_min.Z + sidelen + sidelen*z0 - 1
1341 u32 tree_count = area * tree_amount_2d(data->seed, p2d_center);
1342 // Put trees in random places on part of division
1343 for(u32 i=0; i<tree_count; i++)
1345 s16 x = myrand_range(p2d_min.X, p2d_max.X);
1346 s16 z = myrand_range(p2d_min.Y, p2d_max.Y);
1347 s16 y = find_ground_level(vmanip, v2s16(x,z), ndef);
1348 // Don't make a tree under water level
1351 // Don't make a tree so high that it doesn't fit
1352 if(y > node_max.Y - 6)
1356 Trees grow only on mud and grass
1359 u32 i = vmanip.m_area.index(v3s16(p));
1360 MapNode *n = &vmanip.m_data[i];
1361 if(n->getContent() != c_dirt
1362 && n->getContent() != c_dirt_with_grass)
1367 make_tree(vmanip, p, false, ndef);
1377 ScopeProfiler sp(g_profiler, "EmergeThread: mapgen lighting update",
1379 //VoxelArea a(node_min, node_max);
1380 VoxelArea a(node_min-v3s16(1,0,1)*MAP_BLOCKSIZE,
1381 node_max+v3s16(1,0,1)*MAP_BLOCKSIZE);
1382 /*VoxelArea a(node_min-v3s16(1,0,1)*MAP_BLOCKSIZE/2,
1383 node_max+v3s16(1,0,1)*MAP_BLOCKSIZE/2);*/
1384 enum LightBank banks[2] = {LIGHTBANK_DAY, LIGHTBANK_NIGHT};
1385 for(int i=0; i<2; i++)
1387 enum LightBank bank = banks[i];
1389 core::map<v3s16, bool> light_sources;
1390 core::map<v3s16, u8> unlight_from;
1392 voxalgo::clearLightAndCollectSources(vmanip, a, bank, ndef,
1393 light_sources, unlight_from);
1395 bool inexistent_top_provides_sunlight = !block_is_underground;
1396 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
1397 vmanip, a, inexistent_top_provides_sunlight,
1398 light_sources, ndef);
1399 // TODO: Do stuff according to bottom_sunlight_valid
1401 vmanip.unspreadLight(bank, unlight_from, light_sources, ndef);
1403 vmanip.spreadLight(bank, light_sources, ndef);