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
35 #define AVERAGE_MUD_AMOUNT 4
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), 85309, 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 NoiseParams *np_terrain_base = &nparams_v6_def_terrain_base;
59 NoiseParams *np_terrain_higher = &nparams_v6_def_terrain_higher;
60 NoiseParams *np_steepness = &nparams_v6_def_steepness;
61 NoiseParams *np_height_select = &nparams_v6_def_height_select;
62 NoiseParams *np_trees = &nparams_v6_def_trees;
63 NoiseParams *np_mud = &nparams_v6_def_mud;
64 NoiseParams *np_beach = &nparams_v6_def_beach;
65 NoiseParams *np_biome = &nparams_v6_def_biome;
66 //NoiseParams *np_cave = &nparams_v6_def_cave;
69 MapgenV6::MapgenV6(int mapgenid, u64 seed) {
70 this->generating = false;
72 this->seed = (int)seed;
74 this->water_level = 1;
76 this->csize = v3s16(5, 5, 5) * MAP_BLOCKSIZE; /////////////////get this from config!
77 this->ystride = csize.X;
79 this->use_smooth_biome_trans = g_settings->getBool("mgv6_use_smooth_biome_trans");
81 noise_terrain_base = new Noise(np_terrain_base, seed, csize.X, csize.Y);
82 noise_terrain_higher = new Noise(np_terrain_higher, seed, csize.X, csize.Y);
83 noise_steepness = new Noise(np_steepness, seed, csize.X, csize.Y);
84 noise_height_select = new Noise(np_height_select, seed, csize.X, csize.Y);
85 noise_trees = new Noise(np_trees, seed, csize.X, csize.Y);
86 noise_mud = new Noise(np_mud, seed, csize.X, csize.Y);
87 noise_beach = new Noise(np_beach, seed, csize.X, csize.Y);
88 noise_biome = new Noise(np_biome, seed, csize.X, csize.Y);
89 //noise_cave = new Noise(np_cave, seed, csize.X, csize.Y);
91 map_terrain_base = noise_terrain_base->result;
92 map_terrain_higher = noise_terrain_higher->result;
93 map_steepness = noise_steepness->result;
94 map_height_select = noise_height_select->result;
95 map_trees = noise_trees->result;
96 map_mud = noise_mud->result;
97 map_beach = noise_beach->result;
98 map_biome = noise_biome->result;
99 //map_cave = noise_cave->result;
103 MapgenV6::~MapgenV6() {
104 delete noise_terrain_base;
105 delete noise_terrain_higher;
106 delete noise_steepness;
107 delete noise_height_select;
117 Some helper functions for the map generator
121 // Returns Y one under area minimum if not found
122 s16 MapgenV6::find_ground_level(VoxelManipulator &vmanip, v2s16 p2d,
123 INodeDefManager *ndef)
125 v3s16 em = vmanip.m_area.getExtent();
126 s16 y_nodes_max = vmanip.m_area.MaxEdge.Y;
127 s16 y_nodes_min = vmanip.m_area.MinEdge.Y;
128 u32 i = vmanip.m_area.index(v3s16(p2d.X, y_nodes_max, p2d.Y));
130 for(y=y_nodes_max; y>=y_nodes_min; y--)
132 MapNode &n = vmanip.m_data[i];
133 if(ndef->get(n).walkable)
136 vmanip.m_area.add_y(em, i, -1);
141 return y_nodes_min - 1;
144 // Returns Y one under area minimum if not found
145 s16 MapgenV6::find_stone_level(VoxelManipulator &vmanip, v2s16 p2d,
146 INodeDefManager *ndef)
148 v3s16 em = vmanip.m_area.getExtent();
149 s16 y_nodes_max = vmanip.m_area.MaxEdge.Y;
150 s16 y_nodes_min = vmanip.m_area.MinEdge.Y;
151 u32 i = vmanip.m_area.index(v3s16(p2d.X, y_nodes_max, p2d.Y));
153 content_t c_stone = ndef->getId("mapgen_stone");
154 content_t c_desert_stone = ndef->getId("mapgen_desert_stone");
155 for(y=y_nodes_max; y>=y_nodes_min; y--)
157 MapNode &n = vmanip.m_data[i];
158 content_t c = n.getContent();
159 if(c != CONTENT_IGNORE && (
160 c == c_stone || c == c_desert_stone))
163 vmanip.m_area.add_y(em, i, -1);
168 return y_nodes_min - 1;
172 void MapgenV6::make_tree(ManualMapVoxelManipulator &vmanip, v3s16 p0,
173 bool is_apple_tree, INodeDefManager *ndef)
175 MapNode treenode(ndef->getId("mapgen_tree"));
176 MapNode leavesnode(ndef->getId("mapgen_leaves"));
177 MapNode applenode(ndef->getId("mapgen_apple"));
179 s16 trunk_h = myrand_range(4, 5);
181 for(s16 ii=0; ii<trunk_h; ii++)
183 if(vmanip.m_area.contains(p1))
184 vmanip.m_data[vmanip.m_area.index(p1)] = treenode;
188 // p1 is now the last piece of the trunk
191 VoxelArea leaves_a(v3s16(-2,-1,-2), v3s16(2,2,2));
192 //SharedPtr<u8> leaves_d(new u8[leaves_a.getVolume()]);
193 Buffer<u8> leaves_d(leaves_a.getVolume());
194 for(s32 i=0; i<leaves_a.getVolume(); i++)
197 // Force leaves at near the end of the trunk
200 for(s16 z=-d; z<=d; z++)
201 for(s16 y=-d; y<=d; y++)
202 for(s16 x=-d; x<=d; x++)
204 leaves_d[leaves_a.index(v3s16(x,y,z))] = 1;
208 // Add leaves randomly
209 for(u32 iii=0; iii<7; iii++)
214 myrand_range(leaves_a.MinEdge.X, leaves_a.MaxEdge.X-d),
215 myrand_range(leaves_a.MinEdge.Y, leaves_a.MaxEdge.Y-d),
216 myrand_range(leaves_a.MinEdge.Z, leaves_a.MaxEdge.Z-d)
219 for(s16 z=0; z<=d; z++)
220 for(s16 y=0; y<=d; y++)
221 for(s16 x=0; x<=d; x++)
223 leaves_d[leaves_a.index(p+v3s16(x,y,z))] = 1;
227 // Blit leaves to vmanip
228 for(s16 z=leaves_a.MinEdge.Z; z<=leaves_a.MaxEdge.Z; z++)
229 for(s16 y=leaves_a.MinEdge.Y; y<=leaves_a.MaxEdge.Y; y++)
230 for(s16 x=leaves_a.MinEdge.X; x<=leaves_a.MaxEdge.X; x++)
234 if(vmanip.m_area.contains(p) == false)
236 u32 vi = vmanip.m_area.index(p);
237 if(vmanip.m_data[vi].getContent() != CONTENT_AIR
238 && vmanip.m_data[vi].getContent() != CONTENT_IGNORE)
240 u32 i = leaves_a.index(x,y,z);
241 if(leaves_d[i] == 1) {
242 bool is_apple = myrand_range(0,99) < 10;
243 if(is_apple_tree && is_apple) {
244 vmanip.m_data[vi] = applenode;
246 vmanip.m_data[vi] = leavesnode;
254 Noise functions. Make sure seed is mangled differently in each one.
258 // Amount of trees per area in nodes
259 double MapgenV6::tree_amount_2d(u64 seed, v2s16 p)
261 /*double noise = noise2d_perlin(
262 0.5+(float)p.X/125, 0.5+(float)p.Y/125,
264 double noise = map_trees[(p.Y - node_min.Z) * ystride + (p.X - node_min.X)];
265 double zeroval = -0.39;
269 return 0.04 * (noise-zeroval) / (1.0-zeroval);
272 // Required by mapgen.h
273 bool MapgenV6::block_is_underground(u64 seed, v3s16 blockpos)
275 /*s16 minimum_groundlevel = (s16)get_sector_minimum_ground_level(
276 seed, v2s16(blockpos.X, blockpos.Z));*/
277 // Nah, this is just a heuristic, just return something
278 s16 minimum_groundlevel = water_level;
280 if(blockpos.Y*MAP_BLOCKSIZE + MAP_BLOCKSIZE <= minimum_groundlevel)
287 double MapgenV6::base_rock_level_2d(u64 seed, v2s16 p)
289 int index = (p.Y - node_min.Z) * ystride + (p.X - node_min.X);
291 // The base ground level
292 /*double base = (double)WATER_LEVEL - (double)AVERAGE_MUD_AMOUNT
293 + 20. * noise2d_perlin(
294 0.5+(float)p.X/250., 0.5+(float)p.Y/250.,
295 seed+82341, 5, 0.6);*/
296 double base = water_level + map_terrain_base[index];
298 //printf("%f ", base);
300 // Higher ground level
301 /*double higher = (double)WATER_LEVEL + 20. + 16. * noise2d_perlin(
302 0.5+(float)p.X/500., 0.5+(float)p.Y/500.,
303 seed+85039, 5, 0.6);*/
304 double higher = water_level + map_terrain_higher[index];
306 // Limit higher to at least base
310 // Steepness factor of cliffs
311 /*double b = 0.85 + 0.5 * noise2d_perlin(
312 0.5+(float)p.X/125., 0.5+(float)p.Y/125.,
314 double b = map_steepness[index];
315 b = rangelim(b, 0.0, 1000.0);
318 b = rangelim(b, 0.5, 1000.0);
319 // Values 1.5...100 give quite horrible looking slopes
320 if(b > 1.5 && b < 100.0){
326 //dstream<<"b="<<b<<std::endl;
330 // Offset to more low
331 double a_off = -0.20;
333 /*double a = (double)0.5 + b * (a_off + noise2d_perlin(
334 0.5+(float)p.X/250., 0.5+(float)p.Y/250.,
335 seed+4213, 5, 0.69));*/
336 double a = 0.5 + b * (a_off + map_height_select[index]);
338 a = rangelim(a, 0.0, 1.0);
340 //dstream<<"a="<<a<<std::endl;
342 double h = base*(1.0-a) + higher*a;
347 s16 MapgenV6::find_ground_level_from_noise(u64 seed, v2s16 p2d, s16 precision)
349 return base_rock_level_2d(seed, p2d) + AVERAGE_MUD_AMOUNT;
352 double MapgenV6::get_mud_add_amount(u64 seed, v2s16 p)
354 /*return ((float)AVERAGE_MUD_AMOUNT + 2.0 * noise2d_perlin(
355 0.5+(float)p.X/200, 0.5+(float)p.Y/200,
356 seed+91013, 3, 0.55));*/
357 int index = (p.Y - node_min.Z) * ystride + (p.X - node_min.X);
358 return map_mud[index];
361 bool MapgenV6::get_have_beach(u64 seed, v2s16 p2d)
363 // Determine whether to have sand here
364 /*double sandnoise = noise2d_perlin(
365 0.2+(float)p2d.X/250, 0.7+(float)p2d.Y/250,
366 seed+59420, 3, 0.50);*/
367 int index = (p2d.Y - node_min.Z) * ystride + (p2d.X - node_min.X);
368 double sandnoise = map_beach[index];
370 return (sandnoise > 0.15);
373 BiomeType MapgenV6::get_biome(u64 seed, v2s16 p2d)
375 // Just do something very simple as for now
376 /*double d = noise2d_perlin(
377 0.6+(float)p2d.X/250, 0.2+(float)p2d.Y/250,
378 seed+9130, 3, 0.50);*/
379 int index = (p2d.Y - node_min.Z) * ystride + (p2d.X - node_min.X);
380 double d = map_biome[index];
383 if (use_smooth_biome_trans) {
384 if(d > 0.35 && (noise2d( p2d.X, p2d.Y, int(seed) ) + 1.0) > ( 0.45 - d ) * 20.0 )
390 u32 MapgenV6::get_blockseed(u64 seed, v3s16 p)
392 s32 x=p.X, y=p.Y, z=p.Z;
393 return (u32)(seed%0x100000000ULL) + z*38134234 + y*42123 + x*23;
396 #define VMANIP_FLAG_CAVE VOXELFLAG_CHECKED1
398 void MapgenV6::makeChunk(BlockMakeData *data)
402 //dstream<<"makeBlock: no-op"<<std::endl;
406 this->generating = true;
408 assert(data->vmanip);
409 assert(data->nodedef);
410 assert(data->blockpos_requested.X >= data->blockpos_min.X &&
411 data->blockpos_requested.Y >= data->blockpos_min.Y &&
412 data->blockpos_requested.Z >= data->blockpos_min.Z);
413 assert(data->blockpos_requested.X <= data->blockpos_max.X &&
414 data->blockpos_requested.Y <= data->blockpos_max.Y &&
415 data->blockpos_requested.Z <= data->blockpos_max.Z);
417 INodeDefManager *ndef = data->nodedef;
419 // Hack: use minimum block coordinates for old code that assumes
421 v3s16 blockpos = data->blockpos_requested;
423 /*dstream<<"makeBlock(): ("<<blockpos.X<<","<<blockpos.Y<<","
424 <<blockpos.Z<<")"<<std::endl;*/
426 v3s16 blockpos_min = data->blockpos_min;
427 v3s16 blockpos_max = data->blockpos_max;
428 v3s16 blockpos_full_min = blockpos_min - v3s16(1,1,1);
429 v3s16 blockpos_full_max = blockpos_max + v3s16(1,1,1);
431 ManualMapVoxelManipulator &vmanip = *(data->vmanip);
432 // Area of central chunk
433 node_min = blockpos_min*MAP_BLOCKSIZE;
434 node_max = (blockpos_max+v3s16(1,1,1))*MAP_BLOCKSIZE-v3s16(1,1,1);
435 // Full allocated area
436 v3s16 full_node_min = (blockpos_min-1)*MAP_BLOCKSIZE;
437 v3s16 full_node_max = (blockpos_max+2)*MAP_BLOCKSIZE-v3s16(1,1,1);
439 v3s16 central_area_size = node_max - node_min + v3s16(1,1,1);
441 const s16 max_spread_amount = MAP_BLOCKSIZE;
443 int volume_blocks = (blockpos_max.X - blockpos_min.X + 1)
444 * (blockpos_max.Y - blockpos_min.Y + 1)
445 * (blockpos_max.Z - blockpos_max.Z + 1);
447 int volume_nodes = volume_blocks *
448 MAP_BLOCKSIZE*MAP_BLOCKSIZE*MAP_BLOCKSIZE;
450 // Generated surface area
451 //double gen_area_nodes = MAP_BLOCKSIZE*MAP_BLOCKSIZE * rel_volume;
453 // Horribly wrong heuristic, but better than nothing
454 bool block_is_underground = (water_level > node_max.Y);
457 Create a block-specific seed
459 u32 blockseed = get_blockseed(data->seed, full_node_min);
468 // Need to adjust for the original implementation's +.5 offset...
469 noise_terrain_base->perlinMap2D(
470 x + 0.5 * noise_terrain_base->np->spread.X,
471 z + 0.5 * noise_terrain_base->np->spread.Z);
472 noise_terrain_base->transformNoiseMap();
474 noise_terrain_higher->perlinMap2D(
475 x + 0.5 * noise_terrain_higher->np->spread.X,
476 z + 0.5 * noise_terrain_higher->np->spread.Z);
477 noise_terrain_higher->transformNoiseMap();
479 noise_steepness->perlinMap2D(
480 x + 0.5 * noise_steepness->np->spread.X,
481 z + 0.5 * noise_steepness->np->spread.Z);
482 noise_steepness->transformNoiseMap();
484 noise_height_select->perlinMap2D(
485 x + 0.5 * noise_height_select->np->spread.X,
486 z + 0.5 * noise_height_select->np->spread.Z);
488 noise_mud->perlinMap2D(
489 x + 0.5 * noise_mud->np->spread.X,
490 z + 0.5 * noise_mud->np->spread.Z);
491 noise_mud->transformNoiseMap();
493 noise_beach->perlinMap2D(
494 x + 0.2 * noise_beach->np->spread.X,
495 z + 0.7 * noise_beach->np->spread.Z);
497 noise_biome->perlinMap2D(
498 x + 0.6 * noise_biome->np->spread.X,
499 z + 0.2 * noise_biome->np->spread.Z);
501 /* noise_cave->perlinMap2D(
502 x + 0.5 * noise_cave->np->spread.X,
503 z + 0.5 * noise_cave->np->spread.Z);
504 noise_cave->transformNoiseMap();*/
509 Cache some ground type values for speed
512 // Creates variables c_name=id and n_name=node
513 #define CONTENT_VARIABLE(ndef, name)\
514 content_t c_##name = ndef->getId("mapgen_" #name);\
515 MapNode n_##name(c_##name);
516 // Default to something else if was CONTENT_IGNORE
517 #define CONTENT_VARIABLE_FALLBACK(name, dname)\
518 if(c_##name == CONTENT_IGNORE){\
519 c_##name = c_##dname;\
520 n_##name = n_##dname;\
523 CONTENT_VARIABLE(ndef, stone);
524 CONTENT_VARIABLE(ndef, air);
525 CONTENT_VARIABLE(ndef, water_source);
526 CONTENT_VARIABLE(ndef, dirt);
527 CONTENT_VARIABLE(ndef, sand);
528 CONTENT_VARIABLE(ndef, gravel);
529 CONTENT_VARIABLE(ndef, clay);
530 CONTENT_VARIABLE(ndef, lava_source);
531 CONTENT_VARIABLE(ndef, cobble);
532 CONTENT_VARIABLE(ndef, mossycobble);
533 CONTENT_VARIABLE(ndef, dirt_with_grass);
534 CONTENT_VARIABLE(ndef, junglegrass);
535 CONTENT_VARIABLE(ndef, stone_with_coal);
536 CONTENT_VARIABLE(ndef, stone_with_iron);
537 CONTENT_VARIABLE(ndef, mese);
538 CONTENT_VARIABLE(ndef, desert_sand);
539 CONTENT_VARIABLE_FALLBACK(desert_sand, sand);
540 CONTENT_VARIABLE(ndef, desert_stone);
541 CONTENT_VARIABLE_FALLBACK(desert_stone, stone);
543 // Maximum height of the stone surface and obstacles.
544 // This is used to guide the cave generation
545 s16 stone_surface_max_y = 0;
548 Generate general ground level to full area
552 TimeTaker timer1("Generating ground level");
554 for(s16 x=node_min.X; x<=node_max.X; x++)
555 for(s16 z=node_min.Z; z<=node_max.Z; z++)
558 v2s16 p2d = v2s16(x,z);
561 Skip of already generated
564 v3s16 p(p2d.X, node_min.Y, p2d.Y);
565 if(vmanip.m_data[vmanip.m_area.index(p)].d != CONTENT_AIR)
569 // Ground height at this point
570 float surface_y_f = 0.0;
572 // Use perlin noise for ground height
573 surface_y_f = base_rock_level_2d(data->seed, p2d);
575 /*// Experimental stuff
577 float a = highlands_level_2d(data->seed, p2d);
582 // Convert to integer
583 s16 surface_y = (s16)surface_y_f;
586 if(surface_y > stone_surface_max_y)
587 stone_surface_max_y = surface_y;
589 BiomeType bt = get_biome(data->seed, p2d);
591 Fill ground with stone
594 // Use fast index incrementing
595 v3s16 em = vmanip.m_area.getExtent();
596 u32 i = vmanip.m_area.index(v3s16(p2d.X, node_min.Y, p2d.Y));
597 for(s16 y=node_min.Y; y<=node_max.Y; y++)
599 if(vmanip.m_data[i].getContent() == CONTENT_IGNORE){
601 if(y > water_level && bt == BT_DESERT)
602 vmanip.m_data[i] = n_desert_stone;
604 vmanip.m_data[i] = n_stone;
605 } else if(y <= water_level){
606 vmanip.m_data[i] = MapNode(c_water_source);
608 vmanip.m_data[i] = MapNode(c_air);
611 vmanip.m_area.add_y(em, i, 1);
619 // Limit dirt flow area by 1 because mud is flown into neighbors.
620 assert(central_area_size.X == central_area_size.Z);
621 s16 mudflow_minpos = 0-max_spread_amount+1;
622 s16 mudflow_maxpos = central_area_size.X+max_spread_amount-2;
625 Loop this part, it will make stuff look older and newer nicely
628 double cave_amount = 6.0 + 6.0 * noise2d_perlin(
629 0.5+(double)node_min.X/250, 0.5+(double)node_min.Y/250,
630 data->seed+34329, 3, 0.50);
632 const u32 age_loops = 2;
633 for(u32 i_age=0; i_age<age_loops; i_age++)
635 /******************************
636 BEGINNING OF AGING LOOP
637 ******************************/
642 //TimeTaker timer1("caves");
645 Make caves (this code is relatively horrible)
647 cave_amount = MYMAX(0.0, cave_amount);
648 u32 caves_count = cave_amount * volume_nodes / 50000;
649 u32 bruises_count = 1;
650 PseudoRandom ps(blockseed+21343);
651 PseudoRandom ps2(blockseed+1032);
652 if(ps.range(1, 6) == 1)
653 bruises_count = ps.range(0, ps.range(0, 2));
654 if(get_biome(data->seed, v2s16(node_min.X, node_min.Y)) == BT_DESERT){
658 for(u32 jj=0; jj<caves_count+bruises_count; jj++)
660 bool large_cave = (jj >= caves_count);
661 s16 min_tunnel_diameter = 2;
662 s16 max_tunnel_diameter = ps.range(2,6);
663 int dswitchint = ps.range(1,14);
664 u16 tunnel_routepoints = 0;
665 int part_max_length_rs = 0;
667 part_max_length_rs = ps.range(2,4);
668 tunnel_routepoints = ps.range(5, ps.range(15,30));
669 min_tunnel_diameter = 5;
670 max_tunnel_diameter = ps.range(7, ps.range(8,24));
672 part_max_length_rs = ps.range(2,9);
673 tunnel_routepoints = ps.range(10, ps.range(15,30));
675 bool large_cave_is_flat = (ps.range(0,1) == 0);
677 v3f main_direction(0,0,0);
679 // Allowed route area size in nodes
680 v3s16 ar = central_area_size;
682 // Area starting point in nodes
686 //(this should be more than the maximum radius of the tunnel)
688 s16 more = max_spread_amount - max_tunnel_diameter/2 - insure;
689 ar += v3s16(1,0,1) * more * 2;
690 of -= v3s16(1,0,1) * more;
693 // Allow half a diameter + 7 over stone surface
694 s16 route_y_max = -of.Y + stone_surface_max_y + max_tunnel_diameter/2 + 7;
696 // Limit maximum to area
697 route_y_max = rangelim(route_y_max, 0, ar.Y-1);
702 if(node_min.Y < water_level && node_max.Y > water_level)
704 min = water_level - max_tunnel_diameter/3 - of.Y;
705 route_y_max = water_level + max_tunnel_diameter/3 - of.Y;
707 route_y_min = ps.range(min, min + max_tunnel_diameter);
708 route_y_min = rangelim(route_y_min, 0, route_y_max);
711 s16 route_start_y_min = route_y_min;
712 s16 route_start_y_max = route_y_max;
714 route_start_y_min = rangelim(route_start_y_min, 0, ar.Y-1);
715 route_start_y_max = rangelim(route_start_y_max, route_start_y_min, ar.Y-1);
717 // Randomize starting position
719 (float)(ps.next()%ar.X)+0.5,
720 (float)(ps.range(route_start_y_min, route_start_y_max))+0.5,
721 (float)(ps.next()%ar.Z)+0.5
724 v3s16 startp(orp.X, orp.Y, orp.Z);
727 MapNode airnode(CONTENT_AIR);
728 MapNode waternode(c_water_source);
729 MapNode lavanode(c_lava_source);
732 Generate some tunnel starting from orp
735 for(u16 j=0; j<tunnel_routepoints; j++)
737 if(j%dswitchint==0 && large_cave == false)
739 main_direction = v3f(
740 ((float)(ps.next()%20)-(float)10)/10,
741 ((float)(ps.next()%20)-(float)10)/30,
742 ((float)(ps.next()%20)-(float)10)/10
744 main_direction *= (float)ps.range(0, 10)/10;
748 s16 min_d = min_tunnel_diameter;
749 s16 max_d = max_tunnel_diameter;
750 s16 rs = ps.range(min_d, max_d);
752 // Every second section is rough
753 bool randomize_xz = (ps2.range(1,2) == 1);
759 rs*part_max_length_rs,
760 rs*part_max_length_rs/2,
761 rs*part_max_length_rs
767 rs*part_max_length_rs,
768 ps.range(1, rs*part_max_length_rs),
769 rs*part_max_length_rs
776 (float)(ps.next()%(maxlen.X*1))-(float)maxlen.X/2,
777 (float)(ps.next()%(maxlen.Y*1))-(float)maxlen.Y/2,
778 (float)(ps.next()%(maxlen.Z*1))-(float)maxlen.Z/2
781 // Jump downward sometimes
782 if(!large_cave && ps.range(0,12) == 0)
785 (float)(ps.next()%(maxlen.X*1))-(float)maxlen.X/2,
786 (float)(ps.next()%(maxlen.Y*2))-(float)maxlen.Y*2/2,
787 (float)(ps.next()%(maxlen.Z*1))-(float)maxlen.Z/2
793 s16 h = find_ground_level_clever(vmanip,
794 v2s16(p.X, p.Z), ndef);
795 route_y_min = h - rs/3;
796 route_y_max = h + rs;
799 vec += main_direction;
804 else if(rp.X >= ar.X)
806 if(rp.Y < route_y_min)
808 else if(rp.Y >= route_y_max)
809 rp.Y = route_y_max-1;
812 else if(rp.Z >= ar.Z)
816 for(float f=0; f<1.0; f+=1.0/vec.getLength())
818 v3f fp = orp + vec * f;
819 fp.X += 0.1*ps.range(-10,10);
820 fp.Z += 0.1*ps.range(-10,10);
821 v3s16 cp(fp.X, fp.Y, fp.Z);
826 d0 += ps.range(-1,1);
827 d1 += ps.range(-1,1);
829 for(s16 z0=d0; z0<=d1; z0++)
831 s16 si = rs/2 - MYMAX(0, abs(z0)-rs/7-1);
832 for(s16 x0=-si-ps.range(0,1); x0<=si-1+ps.range(0,1); x0++)
834 s16 maxabsxz = MYMAX(abs(x0), abs(z0));
835 s16 si2 = rs/2 - MYMAX(0, maxabsxz-rs/7-1);
836 for(s16 y0=-si2; y0<=si2; y0++)
838 /*// Make better floors in small caves
839 if(y0 <= -rs/2 && rs<=7)
841 if(large_cave_is_flat){
842 // Make large caves not so tall
843 if(rs > 7 && abs(y0) >= rs/3)
853 if(vmanip.m_area.contains(p) == false)
856 u32 i = vmanip.m_area.index(p);
860 if(full_node_min.Y < water_level &&
861 full_node_max.Y > water_level){
862 if(p.Y <= water_level)
863 vmanip.m_data[i] = waternode;
865 vmanip.m_data[i] = airnode;
866 } else if(full_node_max.Y < water_level){
867 if(p.Y < startp.Y - 2)
868 vmanip.m_data[i] = lavanode;
870 vmanip.m_data[i] = airnode;
872 vmanip.m_data[i] = airnode;
875 // Don't replace air or water or lava or ignore
876 if(vmanip.m_data[i].getContent() == CONTENT_IGNORE ||
877 vmanip.m_data[i].getContent() == CONTENT_AIR ||
878 vmanip.m_data[i].getContent() == c_water_source ||
879 vmanip.m_data[i].getContent() == c_lava_source)
882 vmanip.m_data[i] = airnode;
885 vmanip.m_flags[i] |= VMANIP_FLAG_CAVE;
903 TimeTaker timer1("add mud");
906 Add mud to the central chunk
909 for(s16 x=node_min.X; x<=node_max.X; x++)
910 for(s16 z=node_min.Z; z<=node_max.Z; z++)
912 // Node position in 2d
913 v2s16 p2d = v2s16(x,z);
915 // Randomize mud amount
916 s16 mud_add_amount = get_mud_add_amount(data->seed, p2d) / 2.0 + 0.5;
919 s16 surface_y = find_stone_level(vmanip, p2d, ndef);
920 // Handle area not found
921 if(surface_y == vmanip.m_area.MinEdge.Y - 1)
924 MapNode addnode(c_dirt);
925 BiomeType bt = get_biome(data->seed, p2d);
928 addnode = MapNode(c_desert_sand);
930 if(bt == BT_DESERT && surface_y + mud_add_amount <= water_level+1){
931 addnode = MapNode(c_sand);
932 } else if(mud_add_amount <= 0){
933 mud_add_amount = 1 - mud_add_amount;
934 addnode = MapNode(c_gravel);
935 } else if(bt == BT_NORMAL && get_have_beach(data->seed, p2d) &&
936 surface_y + mud_add_amount <= water_level+2){
937 addnode = MapNode(c_sand);
942 mud_add_amount = MYMAX(0, mud_add_amount - (surface_y - 20)/5);
947 If topmost node is grass, change it to mud.
948 It might be if it was flown to there from a neighboring
949 chunk and then converted.
952 u32 i = vmanip.m_area.index(v3s16(p2d.X, surface_y, p2d.Y));
953 MapNode *n = &vmanip.m_data[i];
954 if(n->getContent() == c_dirt_with_grass)
955 *n = MapNode(c_dirt);
963 v3s16 em = vmanip.m_area.getExtent();
964 s16 y_start = surface_y+1;
965 u32 i = vmanip.m_area.index(v3s16(p2d.X, y_start, p2d.Y));
966 for(s16 y=y_start; y<=node_max.Y; y++)
968 if(mudcount >= mud_add_amount)
971 MapNode &n = vmanip.m_data[i];
975 vmanip.m_area.add_y(em, i, 1);
985 Add blobs of dirt and gravel underground
987 if(get_biome(data->seed, v2s16(node_min.X, node_min.Y)) == BT_NORMAL)
989 PseudoRandom pr(blockseed+983);
990 for(int i=0; i<volume_nodes/10/10/10; i++)
992 bool only_fill_cave = (myrand_range(0,1) != 0);
999 pr.range(node_min.X, node_max.X)-size.X/2,
1000 pr.range(node_min.Y, node_max.Y)-size.Y/2,
1001 pr.range(node_min.Z, node_max.Z)-size.Z/2
1004 if(p0.Y > -32 && pr.range(0,1) == 0)
1005 n1 = MapNode(c_dirt);
1007 n1 = MapNode(c_gravel);
1008 for(int x1=0; x1<size.X; x1++)
1009 for(int y1=0; y1<size.Y; y1++)
1010 for(int z1=0; z1<size.Z; z1++)
1012 v3s16 p = p0 + v3s16(x1,y1,z1);
1013 u32 i = vmanip.m_area.index(p);
1014 if(!vmanip.m_area.contains(i))
1016 // Cancel if not stone and not cave air
1017 if(vmanip.m_data[i].getContent() != c_stone &&
1018 !(vmanip.m_flags[i] & VMANIP_FLAG_CAVE))
1020 if(only_fill_cave && !(vmanip.m_flags[i] & VMANIP_FLAG_CAVE))
1022 vmanip.m_data[i] = n1;
1030 TimeTaker timer1("flow mud");
1033 Flow mud away from steep edges
1036 // Iterate a few times
1037 for(s16 k=0; k<3; k++)
1040 for(s16 x=mudflow_minpos; x<=mudflow_maxpos; x++)
1041 for(s16 z=mudflow_minpos; z<=mudflow_maxpos; z++)
1043 // Invert coordinates every 2nd iteration
1046 x = mudflow_maxpos - (x-mudflow_minpos);
1047 z = mudflow_maxpos - (z-mudflow_minpos);
1050 // Node position in 2d
1051 v2s16 p2d = v2s16(node_min.X, node_min.Z) + v2s16(x,z);
1053 v3s16 em = vmanip.m_area.getExtent();
1054 u32 i = vmanip.m_area.index(v3s16(p2d.X, node_max.Y, p2d.Y));
1057 while(y >= node_min.Y)
1064 for(; y>=node_min.Y; y--)
1066 n = &vmanip.m_data[i];
1067 //if(content_walkable(n->d))
1069 if(n->getContent() == c_dirt ||
1070 n->getContent() == c_dirt_with_grass ||
1071 n->getContent() == c_gravel)
1074 vmanip.m_area.add_y(em, i, -1);
1077 // Stop if out of area
1078 //if(vmanip.m_area.contains(i) == false)
1082 /*// If not mud, do nothing to it
1083 MapNode *n = &vmanip.m_data[i];
1084 if(n->d != CONTENT_MUD && n->d != CONTENT_GRASS)
1087 if(n->getContent() == c_dirt ||
1088 n->getContent() == c_dirt_with_grass)
1090 // Make it exactly mud
1091 n->setContent(c_dirt);
1094 Don't flow it if the stuff under it is not mud
1098 vmanip.m_area.add_y(em, i2, -1);
1099 // Cancel if out of area
1100 if(vmanip.m_area.contains(i2) == false)
1102 MapNode *n2 = &vmanip.m_data[i2];
1103 if(n2->getContent() != c_dirt &&
1104 n2->getContent() != c_dirt_with_grass)
1109 /*s16 recurse_count = 0;
1113 v3s16(0,0,1), // back
1114 v3s16(1,0,0), // right
1115 v3s16(0,0,-1), // front
1116 v3s16(-1,0,0), // left
1119 // Theck that upper is air or doesn't exist.
1120 // Cancel dropping if upper keeps it in place
1122 vmanip.m_area.add_y(em, i3, 1);
1123 if(vmanip.m_area.contains(i3) == true
1124 && ndef->get(vmanip.m_data[i3]).walkable)
1131 for(u32 di=0; di<4; di++)
1133 v3s16 dirp = dirs4[di];
1136 vmanip.m_area.add_p(em, i2, dirp);
1137 // Fail if out of area
1138 if(vmanip.m_area.contains(i2) == false)
1140 // Check that side is air
1141 MapNode *n2 = &vmanip.m_data[i2];
1142 if(ndef->get(*n2).walkable)
1144 // Check that under side is air
1145 vmanip.m_area.add_y(em, i2, -1);
1146 if(vmanip.m_area.contains(i2) == false)
1148 n2 = &vmanip.m_data[i2];
1149 if(ndef->get(*n2).walkable)
1151 /*// Check that under that is air (need a drop of 2)
1152 vmanip.m_area.add_y(em, i2, -1);
1153 if(vmanip.m_area.contains(i2) == false)
1155 n2 = &vmanip.m_data[i2];
1156 if(content_walkable(n2->d))
1158 // Loop further down until not air
1159 bool dropped_to_unknown = false;
1161 vmanip.m_area.add_y(em, i2, -1);
1162 n2 = &vmanip.m_data[i2];
1163 // if out of known area
1164 if(vmanip.m_area.contains(i2) == false
1165 || n2->getContent() == CONTENT_IGNORE){
1166 dropped_to_unknown = true;
1169 }while(ndef->get(*n2).walkable == false);
1170 // Loop one up so that we're in air
1171 vmanip.m_area.add_y(em, i2, 1);
1172 n2 = &vmanip.m_data[i2];
1174 bool old_is_water = (n->getContent() == c_water_source);
1175 // Move mud to new place
1176 if(!dropped_to_unknown) {
1178 // Set old place to be air (or water)
1180 *n = MapNode(c_water_source);
1182 *n = MapNode(CONTENT_AIR);
1198 /***********************
1200 ************************/
1203 Add top and bottom side of water to transforming_liquid queue
1206 for(s16 x=full_node_min.X; x<=full_node_max.X; x++)
1207 for(s16 z=full_node_min.Z; z<=full_node_max.Z; z++)
1212 bool water_found = false;
1213 // Use fast index incrementing
1214 v3s16 em = vmanip.m_area.getExtent();
1215 u32 i = vmanip.m_area.index(v3s16(p2d.X, full_node_max.Y, p2d.Y));
1216 for(s16 y=full_node_max.Y; y>=full_node_min.Y; y--)
1218 if(y == full_node_max.Y){
1220 (vmanip.m_data[i].getContent() == c_water_source ||
1221 vmanip.m_data[i].getContent() == c_lava_source);
1223 else if(water_found == false)
1225 if(vmanip.m_data[i].getContent() == c_water_source ||
1226 vmanip.m_data[i].getContent() == c_lava_source)
1228 v3s16 p = v3s16(p2d.X, y, p2d.Y);
1229 data->transforming_liquid.push_back(p);
1235 // This can be done because water_found can only
1236 // turn to true and end up here after going through
1238 if(vmanip.m_data[i+1].getContent() != c_water_source ||
1239 vmanip.m_data[i+1].getContent() != c_lava_source)
1241 v3s16 p = v3s16(p2d.X, y+1, p2d.Y);
1242 data->transforming_liquid.push_back(p);
1243 water_found = false;
1247 vmanip.m_area.add_y(em, i, -1);
1256 for(s16 x=full_node_min.X; x<=full_node_max.X; x++)
1257 for(s16 z=full_node_min.Z; z<=full_node_max.Z; z++)
1259 // Node position in 2d
1260 v2s16 p2d = v2s16(x,z);
1263 Find the lowest surface to which enough light ends up
1266 Basically just wait until not air and not leaves.
1270 v3s16 em = vmanip.m_area.getExtent();
1271 u32 i = vmanip.m_area.index(v3s16(p2d.X, node_max.Y, p2d.Y));
1273 // Go to ground level
1274 for(y=node_max.Y; y>=full_node_min.Y; y--)
1276 MapNode &n = vmanip.m_data[i];
1277 if(ndef->get(n).param_type != CPT_LIGHT
1278 || ndef->get(n).liquid_type != LIQUID_NONE)
1280 vmanip.m_area.add_y(em, i, -1);
1282 if(y >= full_node_min.Y)
1285 surface_y = full_node_min.Y;
1288 u32 i = vmanip.m_area.index(p2d.X, surface_y, p2d.Y);
1289 MapNode *n = &vmanip.m_data[i];
1290 if(n->getContent() == c_dirt){
1291 // Well yeah, this can't be overground...
1292 if(surface_y < water_level - 20)
1294 n->setContent(c_dirt_with_grass);
1301 assert(central_area_size.X == central_area_size.Z);
1303 // Divide area into parts
1305 s16 sidelen = central_area_size.X / div;
1306 double area = sidelen * sidelen;
1307 for(s16 x0=0; x0<div; x0++)
1308 for(s16 z0=0; z0<div; z0++)
1310 // Center position of part of division
1312 node_min.X + sidelen/2 + sidelen*x0,
1313 node_min.Z + sidelen/2 + sidelen*z0
1315 // Minimum edge of part of division
1317 node_min.X + sidelen*x0,
1318 node_min.Z + sidelen*z0
1320 // Maximum edge of part of division
1322 node_min.X + sidelen + sidelen*x0 - 1,
1323 node_min.Z + sidelen + sidelen*z0 - 1
1326 u32 tree_count = area * tree_amount_2d(data->seed, p2d_center);
1327 // Put trees in random places on part of division
1328 for(u32 i=0; i<tree_count; i++)
1330 s16 x = myrand_range(p2d_min.X, p2d_max.X);
1331 s16 z = myrand_range(p2d_min.Y, p2d_max.Y);
1332 s16 y = find_ground_level(vmanip, v2s16(x,z), ndef);
1333 // Don't make a tree under water level
1336 // Don't make a tree so high that it doesn't fit
1337 if(y > node_max.Y - 6)
1341 Trees grow only on mud and grass
1344 u32 i = vmanip.m_area.index(v3s16(p));
1345 MapNode *n = &vmanip.m_data[i];
1346 if(n->getContent() != c_dirt
1347 && n->getContent() != c_dirt_with_grass)
1352 make_tree(vmanip, p, false, ndef);
1362 ScopeProfiler sp(g_profiler, "EmergeThread: mapgen lighting update",
1364 //VoxelArea a(node_min, node_max);
1365 VoxelArea a(node_min-v3s16(1,0,1)*MAP_BLOCKSIZE,
1366 node_max+v3s16(1,0,1)*MAP_BLOCKSIZE);
1367 /*VoxelArea a(node_min-v3s16(1,0,1)*MAP_BLOCKSIZE/2,
1368 node_max+v3s16(1,0,1)*MAP_BLOCKSIZE/2);*/
1369 enum LightBank banks[2] = {LIGHTBANK_DAY, LIGHTBANK_NIGHT};
1370 for(int i=0; i<2; i++)
1372 enum LightBank bank = banks[i];
1374 core::map<v3s16, bool> light_sources;
1375 core::map<v3s16, u8> unlight_from;
1377 voxalgo::clearLightAndCollectSources(vmanip, a, bank, ndef,
1378 light_sources, unlight_from);
1380 bool inexistent_top_provides_sunlight = !block_is_underground;
1381 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
1382 vmanip, a, inexistent_top_provides_sunlight,
1383 light_sources, ndef);
1384 // TODO: Do stuff according to bottom_sunlight_valid
1386 vmanip.unspreadLight(bank, unlight_from, light_sources, ndef);
1388 vmanip.spreadLight(bank, light_sources, ndef);