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 #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 MapgenV6Params mg_def_params_v6 = {
62 MG_TREES | MG_CAVES | MGV6_BIOME_BLEND,
65 &nparams_v6_def_terrain_base,
66 &nparams_v6_def_terrain_higher,
67 &nparams_v6_def_steepness,
68 &nparams_v6_def_height_select,
69 &nparams_v6_def_trees,
71 &nparams_v6_def_beach,
72 &nparams_v6_def_biome,
77 ///////////////////////////////////////////////////////////////////////////////
80 MapgenV6::MapgenV6(int mapgenid, MapgenV6Params *params) {
81 this->generating = false;
84 this->seed = params->seed;
85 this->water_level = params->water_level;
87 this->csize = v3s16(1, 1, 1) * params->chunksize * MAP_BLOCKSIZE;
89 this->freq_desert = params->freq_desert;
90 this->freq_beach = params->freq_beach;
92 this->ystride = csize.X; //////fix this
94 noise_terrain_base = new Noise(params->np_terrain_base, seed, csize.X, csize.Y);
95 noise_terrain_higher = new Noise(params->np_terrain_higher, seed, csize.X, csize.Y);
96 noise_steepness = new Noise(params->np_steepness, seed, csize.X, csize.Y);
97 noise_height_select = new Noise(params->np_height_select, seed, csize.X, csize.Y);
98 noise_trees = new Noise(params->np_trees, seed, csize.X, csize.Y);
99 noise_mud = new Noise(params->np_mud, seed, csize.X, csize.Y);
100 noise_beach = new Noise(params->np_beach, seed, csize.X, csize.Y);
101 noise_biome = new Noise(params->np_biome, seed, csize.X, csize.Y);
103 map_terrain_base = noise_terrain_base->result;
104 map_terrain_higher = noise_terrain_higher->result;
105 map_steepness = noise_steepness->result;
106 map_height_select = noise_height_select->result;
107 map_trees = noise_trees->result;
108 map_mud = noise_mud->result;
109 map_beach = noise_beach->result;
110 map_biome = noise_biome->result;
114 MapgenV6::~MapgenV6() {
115 delete noise_terrain_base;
116 delete noise_terrain_higher;
117 delete noise_steepness;
118 delete noise_height_select;
128 Some helper functions for the map generator
132 // Returns Y one under area minimum if not found
133 s16 MapgenV6::find_ground_level(VoxelManipulator &vmanip, v2s16 p2d,
134 INodeDefManager *ndef)
136 v3s16 em = vmanip.m_area.getExtent();
137 s16 y_nodes_max = vmanip.m_area.MaxEdge.Y;
138 s16 y_nodes_min = vmanip.m_area.MinEdge.Y;
139 u32 i = vmanip.m_area.index(v3s16(p2d.X, y_nodes_max, p2d.Y));
141 for(y=y_nodes_max; y>=y_nodes_min; y--)
143 MapNode &n = vmanip.m_data[i];
144 if(ndef->get(n).walkable)
147 vmanip.m_area.add_y(em, i, -1);
152 return y_nodes_min - 1;
155 // Returns Y one under area minimum if not found
156 s16 MapgenV6::find_stone_level(VoxelManipulator &vmanip, v2s16 p2d,
157 INodeDefManager *ndef)
159 v3s16 em = vmanip.m_area.getExtent();
160 s16 y_nodes_max = vmanip.m_area.MaxEdge.Y;
161 s16 y_nodes_min = vmanip.m_area.MinEdge.Y;
162 u32 i = vmanip.m_area.index(v3s16(p2d.X, y_nodes_max, p2d.Y));
164 content_t c_stone = ndef->getId("mapgen_stone");
165 content_t c_desert_stone = ndef->getId("mapgen_desert_stone");
166 for(y=y_nodes_max; y>=y_nodes_min; y--)
168 MapNode &n = vmanip.m_data[i];
169 content_t c = n.getContent();
170 if(c != CONTENT_IGNORE && (
171 c == c_stone || c == c_desert_stone))
174 vmanip.m_area.add_y(em, i, -1);
179 return y_nodes_min - 1;
183 void MapgenV6::make_tree(ManualMapVoxelManipulator &vmanip, v3s16 p0,
184 bool is_apple_tree, INodeDefManager *ndef)
186 MapNode treenode(ndef->getId("mapgen_tree"));
187 MapNode leavesnode(ndef->getId("mapgen_leaves"));
188 MapNode applenode(ndef->getId("mapgen_apple"));
190 s16 trunk_h = myrand_range(4, 5);
192 for(s16 ii=0; ii<trunk_h; ii++)
194 if(vmanip.m_area.contains(p1))
195 vmanip.m_data[vmanip.m_area.index(p1)] = treenode;
199 // p1 is now the last piece of the trunk
202 VoxelArea leaves_a(v3s16(-2,-1,-2), v3s16(2,2,2));
203 //SharedPtr<u8> leaves_d(new u8[leaves_a.getVolume()]);
204 Buffer<u8> leaves_d(leaves_a.getVolume());
205 for(s32 i=0; i<leaves_a.getVolume(); i++)
208 // Force leaves at near the end of the trunk
211 for(s16 z=-d; z<=d; z++)
212 for(s16 y=-d; y<=d; y++)
213 for(s16 x=-d; x<=d; x++)
215 leaves_d[leaves_a.index(v3s16(x,y,z))] = 1;
219 // Add leaves randomly
220 for(u32 iii=0; iii<7; iii++)
225 myrand_range(leaves_a.MinEdge.X, leaves_a.MaxEdge.X-d),
226 myrand_range(leaves_a.MinEdge.Y, leaves_a.MaxEdge.Y-d),
227 myrand_range(leaves_a.MinEdge.Z, leaves_a.MaxEdge.Z-d)
230 for(s16 z=0; z<=d; z++)
231 for(s16 y=0; y<=d; y++)
232 for(s16 x=0; x<=d; x++)
234 leaves_d[leaves_a.index(p+v3s16(x,y,z))] = 1;
238 // Blit leaves to vmanip
239 for(s16 z=leaves_a.MinEdge.Z; z<=leaves_a.MaxEdge.Z; z++)
240 for(s16 y=leaves_a.MinEdge.Y; y<=leaves_a.MaxEdge.Y; y++)
241 for(s16 x=leaves_a.MinEdge.X; x<=leaves_a.MaxEdge.X; x++)
245 if(vmanip.m_area.contains(p) == false)
247 u32 vi = vmanip.m_area.index(p);
248 if(vmanip.m_data[vi].getContent() != CONTENT_AIR
249 && vmanip.m_data[vi].getContent() != CONTENT_IGNORE)
251 u32 i = leaves_a.index(x,y,z);
252 if(leaves_d[i] == 1) {
253 bool is_apple = myrand_range(0,99) < 10;
254 if(is_apple_tree && is_apple) {
255 vmanip.m_data[vi] = applenode;
257 vmanip.m_data[vi] = leavesnode;
265 Noise functions. Make sure seed is mangled differently in each one.
269 // Amount of trees per area in nodes
270 double MapgenV6::tree_amount_2d(u64 seed, v2s16 p)
272 /*double noise = noise2d_perlin(
273 0.5+(float)p.X/125, 0.5+(float)p.Y/125,
275 double noise = map_trees[(p.Y - node_min.Z) * ystride + (p.X - node_min.X)];
276 double zeroval = -0.39;
280 return 0.04 * (noise-zeroval) / (1.0-zeroval);
283 // Required by mapgen.h
284 bool MapgenV6::block_is_underground(u64 seed, v3s16 blockpos)
286 /*s16 minimum_groundlevel = (s16)get_sector_minimum_ground_level(
287 seed, v2s16(blockpos.X, blockpos.Z));*/
288 // Nah, this is just a heuristic, just return something
289 s16 minimum_groundlevel = water_level;
291 if(blockpos.Y*MAP_BLOCKSIZE + MAP_BLOCKSIZE <= minimum_groundlevel)
298 double MapgenV6::base_rock_level_2d(u64 seed, v2s16 p)
300 int index = (p.Y - node_min.Z) * ystride + (p.X - node_min.X);
302 // The base ground level
303 /*double base = (double)WATER_LEVEL - (double)AVERAGE_MUD_AMOUNT
304 + 20. * noise2d_perlin(
305 0.5+(float)p.X/250., 0.5+(float)p.Y/250.,
306 seed+82341, 5, 0.6);*/
307 double base = water_level + map_terrain_base[index];
309 // Higher ground level
310 /*double higher = (double)WATER_LEVEL + 20. + 16. * noise2d_perlin(
311 0.5+(float)p.X/500., 0.5+(float)p.Y/500.,
312 seed+85039, 5, 0.6);*/
313 double higher = water_level + map_terrain_higher[index];
315 // Limit higher to at least base
319 // Steepness factor of cliffs
320 /*double b = 0.85 + 0.5 * noise2d_perlin(
321 0.5+(float)p.X/125., 0.5+(float)p.Y/125.,
323 double b = map_steepness[index];
324 b = rangelim(b, 0.0, 1000.0);
327 b = rangelim(b, 0.5, 1000.0);
329 // Values 1.5...100 give quite horrible looking slopes
330 if(b > 1.5 && b < 100.0){
337 // Offset to more low
338 double a_off = -0.20;
341 /*double a = (double)0.5 + b * (a_off + noise2d_perlin(
342 0.5+(float)p.X/250., 0.5+(float)p.Y/250.,
343 seed+4213, 5, 0.69));*/
344 double a = 0.5 + b * (a_off + map_height_select[index]);
347 a = rangelim(a, 0.0, 1.0);
349 double h = base*(1.0-a) + higher*a;
354 s16 MapgenV6::find_ground_level_from_noise(u64 seed, v2s16 p2d, s16 precision)
356 return base_rock_level_2d(seed, p2d) + AVERAGE_MUD_AMOUNT;
359 double MapgenV6::get_mud_add_amount(u64 seed, v2s16 p)
361 /*return ((float)AVERAGE_MUD_AMOUNT + 2.0 * noise2d_perlin(
362 0.5+(float)p.X/200, 0.5+(float)p.Y/200,
363 seed+91013, 3, 0.55));*/
364 int index = (p.Y - node_min.Z) * ystride + (p.X - node_min.X);
365 return map_mud[index];
368 bool MapgenV6::get_have_beach(u64 seed, v2s16 p2d)
370 // Determine whether to have sand here
371 /*double sandnoise = noise2d_perlin(
372 0.2+(float)p2d.X/250, 0.7+(float)p2d.Y/250,
373 seed+59420, 3, 0.50);*/
374 int index = (p2d.Y - node_min.Z) * ystride + (p2d.X - node_min.X);
375 double sandnoise = map_beach[index];
377 return (sandnoise > freq_beach);
380 BiomeType MapgenV6::get_biome(u64 seed, v2s16 p2d)
382 // Just do something very simple as for now
383 /*double d = noise2d_perlin(
384 0.6+(float)p2d.X/250, 0.2+(float)p2d.Y/250,
385 seed+9130, 3, 0.50);*/
386 int index = (p2d.Y - node_min.Z) * ystride + (p2d.X - node_min.X);
387 double d = map_biome[index];
390 if (flags & MGV6_BIOME_BLEND) {
391 if(d > freq_desert - 0.10 &&
392 (noise2d(p2d.X, p2d.Y, seed) + 1.0) > (freq_desert - d) * 20.0)
398 u32 MapgenV6::get_blockseed(u64 seed, v3s16 p)
400 s32 x=p.X, y=p.Y, z=p.Z;
401 return (u32)(seed%0x100000000ULL) + z*38134234 + y*42123 + x*23;
404 #define VMANIP_FLAG_CAVE VOXELFLAG_CHECKED1
406 void MapgenV6::makeChunk(BlockMakeData *data)
410 //dstream<<"makeBlock: no-op"<<std::endl;
414 this->generating = true;
416 assert(data->vmanip);
417 assert(data->nodedef);
418 assert(data->blockpos_requested.X >= data->blockpos_min.X &&
419 data->blockpos_requested.Y >= data->blockpos_min.Y &&
420 data->blockpos_requested.Z >= data->blockpos_min.Z);
421 assert(data->blockpos_requested.X <= data->blockpos_max.X &&
422 data->blockpos_requested.Y <= data->blockpos_max.Y &&
423 data->blockpos_requested.Z <= data->blockpos_max.Z);
425 INodeDefManager *ndef = data->nodedef;
427 // Hack: use minimum block coordinates for old code that assumes
429 v3s16 blockpos = data->blockpos_requested;
431 /*dstream<<"makeBlock(): ("<<blockpos.X<<","<<blockpos.Y<<","
432 <<blockpos.Z<<")"<<std::endl;*/
434 v3s16 blockpos_min = data->blockpos_min;
435 v3s16 blockpos_max = data->blockpos_max;
436 v3s16 blockpos_full_min = blockpos_min - v3s16(1,1,1);
437 v3s16 blockpos_full_max = blockpos_max + v3s16(1,1,1);
439 ManualMapVoxelManipulator &vmanip = *(data->vmanip);
440 // Area of central chunk
441 node_min = blockpos_min*MAP_BLOCKSIZE;
442 node_max = (blockpos_max+v3s16(1,1,1))*MAP_BLOCKSIZE-v3s16(1,1,1);
443 // Full allocated area
444 v3s16 full_node_min = (blockpos_min-1)*MAP_BLOCKSIZE;
445 v3s16 full_node_max = (blockpos_max+2)*MAP_BLOCKSIZE-v3s16(1,1,1);
447 v3s16 central_area_size = node_max - node_min + v3s16(1,1,1);
449 const s16 max_spread_amount = MAP_BLOCKSIZE;
451 int volume_blocks = (blockpos_max.X - blockpos_min.X + 1)
452 * (blockpos_max.Y - blockpos_min.Y + 1)
453 * (blockpos_max.Z - blockpos_max.Z + 1);
455 int volume_nodes = volume_blocks *
456 MAP_BLOCKSIZE*MAP_BLOCKSIZE*MAP_BLOCKSIZE;
458 // Generated surface area
459 //double gen_area_nodes = MAP_BLOCKSIZE*MAP_BLOCKSIZE * rel_volume;
461 // Horribly wrong heuristic, but better than nothing
462 bool block_is_underground = (water_level > node_max.Y);
465 Create a block-specific seed
467 u32 blockseed = get_blockseed(data->seed, full_node_min);
476 // Need to adjust for the original implementation's +.5 offset...
477 noise_terrain_base->perlinMap2D(
478 x + 0.5 * noise_terrain_base->np->spread.X,
479 z + 0.5 * noise_terrain_base->np->spread.Z);
480 noise_terrain_base->transformNoiseMap();
482 noise_terrain_higher->perlinMap2D(
483 x + 0.5 * noise_terrain_higher->np->spread.X,
484 z + 0.5 * noise_terrain_higher->np->spread.Z);
485 noise_terrain_higher->transformNoiseMap();
487 noise_steepness->perlinMap2D(
488 x + 0.5 * noise_steepness->np->spread.X,
489 z + 0.5 * noise_steepness->np->spread.Z);
490 noise_steepness->transformNoiseMap();
492 noise_height_select->perlinMap2D(
493 x + 0.5 * noise_height_select->np->spread.X,
494 z + 0.5 * noise_height_select->np->spread.Z);
496 noise_trees->perlinMap2D(
497 x + 0.5 * noise_trees->np->spread.X,
498 z + 0.5 * noise_trees->np->spread.Z);
500 noise_mud->perlinMap2D(
501 x + 0.5 * noise_mud->np->spread.X,
502 z + 0.5 * noise_mud->np->spread.Z);
503 noise_mud->transformNoiseMap();
505 noise_beach->perlinMap2D(
506 x + 0.2 * noise_beach->np->spread.X,
507 z + 0.7 * noise_beach->np->spread.Z);
509 noise_biome->perlinMap2D(
510 x + 0.6 * noise_biome->np->spread.X,
511 z + 0.2 * noise_biome->np->spread.Z);
516 Cache some ground type values for speed
519 // Creates variables c_name=id and n_name=node
520 #define CONTENT_VARIABLE(ndef, name)\
521 content_t c_##name = ndef->getId("mapgen_" #name);\
522 MapNode n_##name(c_##name);
523 // Default to something else if was CONTENT_IGNORE
524 #define CONTENT_VARIABLE_FALLBACK(name, dname)\
525 if(c_##name == CONTENT_IGNORE){\
526 c_##name = c_##dname;\
527 n_##name = n_##dname;\
530 CONTENT_VARIABLE(ndef, stone);
531 CONTENT_VARIABLE(ndef, air);
532 CONTENT_VARIABLE(ndef, water_source);
533 CONTENT_VARIABLE(ndef, dirt);
534 CONTENT_VARIABLE(ndef, sand);
535 CONTENT_VARIABLE(ndef, gravel);
536 CONTENT_VARIABLE(ndef, clay);
537 CONTENT_VARIABLE(ndef, lava_source);
538 CONTENT_VARIABLE(ndef, cobble);
539 CONTENT_VARIABLE(ndef, mossycobble);
540 CONTENT_VARIABLE(ndef, dirt_with_grass);
541 CONTENT_VARIABLE(ndef, junglegrass);
542 CONTENT_VARIABLE(ndef, stone_with_coal);
543 CONTENT_VARIABLE(ndef, stone_with_iron);
544 CONTENT_VARIABLE(ndef, mese);
545 CONTENT_VARIABLE(ndef, desert_sand);
546 CONTENT_VARIABLE_FALLBACK(desert_sand, sand);
547 CONTENT_VARIABLE(ndef, desert_stone);
548 CONTENT_VARIABLE_FALLBACK(desert_stone, stone);
550 // Maximum height of the stone surface and obstacles.
551 // This is used to guide the cave generation
552 s16 stone_surface_max_y = 0;
555 Generate general ground level to full area
559 TimeTaker timer1("Generating ground level");
561 for(s16 x=node_min.X; x<=node_max.X; x++)
562 for(s16 z=node_min.Z; z<=node_max.Z; z++)
565 v2s16 p2d = v2s16(x,z);
568 Skip of already generated
571 v3s16 p(p2d.X, node_min.Y, p2d.Y);
572 if(vmanip.m_data[vmanip.m_area.index(p)].d != CONTENT_AIR)
576 // Ground height at this point
577 float surface_y_f = 0.0;
579 // Use perlin noise for ground height
580 surface_y_f = base_rock_level_2d(data->seed, p2d);
582 /*// Experimental stuff
584 float a = highlands_level_2d(data->seed, p2d);
589 // Convert to integer
590 s16 surface_y = (s16)surface_y_f;
593 if(surface_y > stone_surface_max_y)
594 stone_surface_max_y = surface_y;
596 BiomeType bt = get_biome(data->seed, p2d);
598 Fill ground with stone
601 // Use fast index incrementing
602 v3s16 em = vmanip.m_area.getExtent();
603 u32 i = vmanip.m_area.index(v3s16(p2d.X, node_min.Y, p2d.Y));
604 for(s16 y=node_min.Y; y<=node_max.Y; y++)
606 if(vmanip.m_data[i].getContent() == CONTENT_IGNORE){
608 if(y > water_level && bt == BT_DESERT)
609 vmanip.m_data[i] = n_desert_stone;
611 vmanip.m_data[i] = n_stone;
612 } else if(y <= water_level){
613 vmanip.m_data[i] = MapNode(c_water_source);
615 vmanip.m_data[i] = MapNode(c_air);
618 vmanip.m_area.add_y(em, i, 1);
626 // Limit dirt flow area by 1 because mud is flown into neighbors.
627 assert(central_area_size.X == central_area_size.Z);
628 s16 mudflow_minpos = 0-max_spread_amount+1;
629 s16 mudflow_maxpos = central_area_size.X+max_spread_amount-2;
632 Loop this part, it will make stuff look older and newer nicely
635 /*double cave_amount = 6.0 + 6.0 * noise2d_perlin(
636 0.5+(double)node_min.X/250, 0.5+(double)node_min.Y/250,
637 data->seed+34329, 3, 0.50);*/
639 double cave_amount = np_cave->offset + np_cave->scale * noise2d_perlin(
640 0.5 + (double)node_min.X / np_cave->spread.X,
641 0.5 + (double)node_min.Y / np_cave->spread.Y,
642 data->seed + np_cave->seed, np_cave->octaves, np_cave->persist);
644 const u32 age_loops = 2;
645 for(u32 i_age=0; i_age<age_loops; i_age++)
647 /******************************
648 BEGINNING OF AGING LOOP
649 ******************************/
654 //TimeTaker timer1("caves");
657 Make caves (this code is relatively horrible)
659 cave_amount = MYMAX(0.0, cave_amount);
660 u32 caves_count = cave_amount * volume_nodes / 50000;
661 u32 bruises_count = 1;
662 PseudoRandom ps(blockseed+21343);
663 PseudoRandom ps2(blockseed+1032);
664 if(ps.range(1, 6) == 1)
665 bruises_count = ps.range(0, ps.range(0, 2));
666 if(get_biome(data->seed, v2s16(node_min.X, node_min.Z)) == BT_DESERT){
670 for(u32 jj=0; jj<caves_count+bruises_count; jj++)
672 int avg_height = (int)
673 ((base_rock_level_2d(data->seed, v2s16(node_min.X, node_min.Z)) +
674 base_rock_level_2d(data->seed, v2s16(node_max.X, node_max.Z))) / 2);
675 if ((node_max.Y + node_min.Y) / 2 > avg_height)
677 bool large_cave = (jj >= caves_count);
678 s16 min_tunnel_diameter = 2;
679 s16 max_tunnel_diameter = ps.range(2,6);
680 int dswitchint = ps.range(1,14);
681 u16 tunnel_routepoints = 0;
682 int part_max_length_rs = 0;
684 part_max_length_rs = ps.range(2,4);
685 tunnel_routepoints = ps.range(5, ps.range(15,30));
686 min_tunnel_diameter = 5;
687 max_tunnel_diameter = ps.range(7, ps.range(8,24));
689 part_max_length_rs = ps.range(2,9);
690 tunnel_routepoints = ps.range(10, ps.range(15,30));
692 bool large_cave_is_flat = (ps.range(0,1) == 0);
694 v3f main_direction(0,0,0);
696 // Allowed route area size in nodes
697 v3s16 ar = central_area_size;
699 // Area starting point in nodes
703 //(this should be more than the maximum radius of the tunnel)
705 s16 more = max_spread_amount - max_tunnel_diameter/2 - insure;
706 ar += v3s16(1,0,1) * more * 2;
707 of -= v3s16(1,0,1) * more;
710 // Allow half a diameter + 7 over stone surface
711 s16 route_y_max = -of.Y + stone_surface_max_y + max_tunnel_diameter/2 + 7;
713 // Limit maximum to area
714 route_y_max = rangelim(route_y_max, 0, ar.Y-1);
719 if(node_min.Y < water_level && node_max.Y > water_level)
721 min = water_level - max_tunnel_diameter/3 - of.Y;
722 route_y_max = water_level + max_tunnel_diameter/3 - of.Y;
724 route_y_min = ps.range(min, min + max_tunnel_diameter);
725 route_y_min = rangelim(route_y_min, 0, route_y_max);
728 s16 route_start_y_min = route_y_min;
729 s16 route_start_y_max = route_y_max;
731 route_start_y_min = rangelim(route_start_y_min, 0, ar.Y-1);
732 route_start_y_max = rangelim(route_start_y_max, route_start_y_min, ar.Y-1);
734 // Randomize starting position
736 (float)(ps.next()%ar.X)+0.5,
737 (float)(ps.range(route_start_y_min, route_start_y_max))+0.5,
738 (float)(ps.next()%ar.Z)+0.5
741 v3s16 startp(orp.X, orp.Y, orp.Z);
744 MapNode airnode(CONTENT_AIR);
745 MapNode waternode(c_water_source);
746 MapNode lavanode(c_lava_source);
749 Generate some tunnel starting from orp
752 for(u16 j=0; j<tunnel_routepoints; j++)
754 if(j%dswitchint==0 && large_cave == false)
756 main_direction = v3f(
757 ((float)(ps.next()%20)-(float)10)/10,
758 ((float)(ps.next()%20)-(float)10)/30,
759 ((float)(ps.next()%20)-(float)10)/10
761 main_direction *= (float)ps.range(0, 10)/10;
765 s16 min_d = min_tunnel_diameter;
766 s16 max_d = max_tunnel_diameter;
767 s16 rs = ps.range(min_d, max_d);
769 // Every second section is rough
770 bool randomize_xz = (ps2.range(1,2) == 1);
776 rs*part_max_length_rs,
777 rs*part_max_length_rs/2,
778 rs*part_max_length_rs
784 rs*part_max_length_rs,
785 ps.range(1, rs*part_max_length_rs),
786 rs*part_max_length_rs
793 (float)(ps.next()%(maxlen.X*1))-(float)maxlen.X/2,
794 (float)(ps.next()%(maxlen.Y*1))-(float)maxlen.Y/2,
795 (float)(ps.next()%(maxlen.Z*1))-(float)maxlen.Z/2
798 // Jump downward sometimes
799 if(!large_cave && ps.range(0,12) == 0)
802 (float)(ps.next()%(maxlen.X*1))-(float)maxlen.X/2,
803 (float)(ps.next()%(maxlen.Y*2))-(float)maxlen.Y*2/2,
804 (float)(ps.next()%(maxlen.Z*1))-(float)maxlen.Z/2
810 s16 h = find_ground_level_clever(vmanip,
811 v2s16(p.X, p.Z), ndef);
812 route_y_min = h - rs/3;
813 route_y_max = h + rs;
816 vec += main_direction;
821 else if(rp.X >= ar.X)
823 if(rp.Y < route_y_min)
825 else if(rp.Y >= route_y_max)
826 rp.Y = route_y_max-1;
829 else if(rp.Z >= ar.Z)
833 for(float f=0; f<1.0; f+=1.0/vec.getLength())
835 v3f fp = orp + vec * f;
836 fp.X += 0.1*ps.range(-10,10);
837 fp.Z += 0.1*ps.range(-10,10);
838 v3s16 cp(fp.X, fp.Y, fp.Z);
843 d0 += ps.range(-1,1);
844 d1 += ps.range(-1,1);
846 for(s16 z0=d0; z0<=d1; z0++)
848 s16 si = rs/2 - MYMAX(0, abs(z0)-rs/7-1);
849 for(s16 x0=-si-ps.range(0,1); x0<=si-1+ps.range(0,1); x0++)
851 s16 maxabsxz = MYMAX(abs(x0), abs(z0));
852 s16 si2 = rs/2 - MYMAX(0, maxabsxz-rs/7-1);
853 for(s16 y0=-si2; y0<=si2; y0++)
855 /*// Make better floors in small caves
856 if(y0 <= -rs/2 && rs<=7)
858 if(large_cave_is_flat){
859 // Make large caves not so tall
860 if(rs > 7 && abs(y0) >= rs/3)
870 if(vmanip.m_area.contains(p) == false)
873 u32 i = vmanip.m_area.index(p);
877 if(full_node_min.Y < water_level &&
878 full_node_max.Y > water_level){
879 if(p.Y <= water_level)
880 vmanip.m_data[i] = waternode;
882 vmanip.m_data[i] = airnode;
883 } else if(full_node_max.Y < water_level){
884 if(p.Y < startp.Y - 2)
885 vmanip.m_data[i] = lavanode;
887 vmanip.m_data[i] = airnode;
889 vmanip.m_data[i] = airnode;
892 // Don't replace air or water or lava or ignore
893 if(vmanip.m_data[i].getContent() == CONTENT_IGNORE ||
894 vmanip.m_data[i].getContent() == CONTENT_AIR ||
895 vmanip.m_data[i].getContent() == c_water_source ||
896 vmanip.m_data[i].getContent() == c_lava_source)
899 vmanip.m_data[i] = airnode;
902 vmanip.m_flags[i] |= VMANIP_FLAG_CAVE;
920 TimeTaker timer1("add mud");
923 Add mud to the central chunk
926 for(s16 x=node_min.X; x<=node_max.X; x++)
927 for(s16 z=node_min.Z; z<=node_max.Z; z++)
929 // Node position in 2d
930 v2s16 p2d = v2s16(x,z);
932 // Randomize mud amount
933 s16 mud_add_amount = get_mud_add_amount(data->seed, p2d) / 2.0 + 0.5;
936 s16 surface_y = find_stone_level(vmanip, p2d, ndef);
937 // Handle area not found
938 if(surface_y == vmanip.m_area.MinEdge.Y - 1)
941 MapNode addnode(c_dirt);
942 BiomeType bt = get_biome(data->seed, p2d);
945 addnode = MapNode(c_desert_sand);
947 if(bt == BT_DESERT && surface_y + mud_add_amount <= water_level+1){
948 addnode = MapNode(c_sand);
949 } else if(mud_add_amount <= 0){
950 mud_add_amount = 1 - mud_add_amount;
951 addnode = MapNode(c_gravel);
952 } else if(bt == BT_NORMAL && get_have_beach(data->seed, p2d) &&
953 surface_y + mud_add_amount <= water_level+2){
954 addnode = MapNode(c_sand);
959 mud_add_amount = MYMAX(0, mud_add_amount - (surface_y - 20)/5);
964 If topmost node is grass, change it to mud.
965 It might be if it was flown to there from a neighboring
966 chunk and then converted.
969 u32 i = vmanip.m_area.index(v3s16(p2d.X, surface_y, p2d.Y));
970 MapNode *n = &vmanip.m_data[i];
971 if(n->getContent() == c_dirt_with_grass)
972 *n = MapNode(c_dirt);
980 v3s16 em = vmanip.m_area.getExtent();
981 s16 y_start = surface_y+1;
982 u32 i = vmanip.m_area.index(v3s16(p2d.X, y_start, p2d.Y));
983 for(s16 y=y_start; y<=node_max.Y; y++)
985 if(mudcount >= mud_add_amount)
988 MapNode &n = vmanip.m_data[i];
992 vmanip.m_area.add_y(em, i, 1);
1002 Add blobs of dirt and gravel underground
1004 if(get_biome(data->seed, v2s16(node_min.X, node_min.Z)) == BT_NORMAL)
1006 PseudoRandom pr(blockseed+983);
1007 for(int i=0; i<volume_nodes/10/10/10; i++)
1009 bool only_fill_cave = (myrand_range(0,1) != 0);
1016 pr.range(node_min.X, node_max.X)-size.X/2,
1017 pr.range(node_min.Y, node_max.Y)-size.Y/2,
1018 pr.range(node_min.Z, node_max.Z)-size.Z/2
1021 if(p0.Y > -32 && pr.range(0,1) == 0)
1022 n1 = MapNode(c_dirt);
1024 n1 = MapNode(c_gravel);
1025 for(int x1=0; x1<size.X; x1++)
1026 for(int y1=0; y1<size.Y; y1++)
1027 for(int z1=0; z1<size.Z; z1++)
1029 v3s16 p = p0 + v3s16(x1,y1,z1);
1030 u32 i = vmanip.m_area.index(p);
1031 if(!vmanip.m_area.contains(i))
1033 // Cancel if not stone and not cave air
1034 if(vmanip.m_data[i].getContent() != c_stone &&
1035 !(vmanip.m_flags[i] & VMANIP_FLAG_CAVE))
1037 if(only_fill_cave && !(vmanip.m_flags[i] & VMANIP_FLAG_CAVE))
1039 vmanip.m_data[i] = n1;
1047 TimeTaker timer1("flow mud");
1050 Flow mud away from steep edges
1053 // Iterate a few times
1054 for(s16 k=0; k<3; k++)
1057 for(s16 x=mudflow_minpos; x<=mudflow_maxpos; x++)
1058 for(s16 z=mudflow_minpos; z<=mudflow_maxpos; z++)
1060 // Invert coordinates every 2nd iteration
1063 x = mudflow_maxpos - (x-mudflow_minpos);
1064 z = mudflow_maxpos - (z-mudflow_minpos);
1067 // Node position in 2d
1068 v2s16 p2d = v2s16(node_min.X, node_min.Z) + v2s16(x,z);
1070 v3s16 em = vmanip.m_area.getExtent();
1071 u32 i = vmanip.m_area.index(v3s16(p2d.X, node_max.Y, p2d.Y));
1074 while(y >= node_min.Y)
1081 for(; y>=node_min.Y; y--)
1083 n = &vmanip.m_data[i];
1084 //if(content_walkable(n->d))
1086 if(n->getContent() == c_dirt ||
1087 n->getContent() == c_dirt_with_grass ||
1088 n->getContent() == c_gravel)
1091 vmanip.m_area.add_y(em, i, -1);
1094 // Stop if out of area
1095 //if(vmanip.m_area.contains(i) == false)
1099 /*// If not mud, do nothing to it
1100 MapNode *n = &vmanip.m_data[i];
1101 if(n->d != CONTENT_MUD && n->d != CONTENT_GRASS)
1104 if(n->getContent() == c_dirt ||
1105 n->getContent() == c_dirt_with_grass)
1107 // Make it exactly mud
1108 n->setContent(c_dirt);
1111 Don't flow it if the stuff under it is not mud
1115 vmanip.m_area.add_y(em, i2, -1);
1116 // Cancel if out of area
1117 if(vmanip.m_area.contains(i2) == false)
1119 MapNode *n2 = &vmanip.m_data[i2];
1120 if(n2->getContent() != c_dirt &&
1121 n2->getContent() != c_dirt_with_grass)
1126 /*s16 recurse_count = 0;
1130 v3s16(0,0,1), // back
1131 v3s16(1,0,0), // right
1132 v3s16(0,0,-1), // front
1133 v3s16(-1,0,0), // left
1136 // Theck that upper is air or doesn't exist.
1137 // Cancel dropping if upper keeps it in place
1139 vmanip.m_area.add_y(em, i3, 1);
1140 if(vmanip.m_area.contains(i3) == true
1141 && ndef->get(vmanip.m_data[i3]).walkable)
1148 for(u32 di=0; di<4; di++)
1150 v3s16 dirp = dirs4[di];
1153 vmanip.m_area.add_p(em, i2, dirp);
1154 // Fail if out of area
1155 if(vmanip.m_area.contains(i2) == false)
1157 // Check that side is air
1158 MapNode *n2 = &vmanip.m_data[i2];
1159 if(ndef->get(*n2).walkable)
1161 // Check that under side is air
1162 vmanip.m_area.add_y(em, i2, -1);
1163 if(vmanip.m_area.contains(i2) == false)
1165 n2 = &vmanip.m_data[i2];
1166 if(ndef->get(*n2).walkable)
1168 /*// Check that under that is air (need a drop of 2)
1169 vmanip.m_area.add_y(em, i2, -1);
1170 if(vmanip.m_area.contains(i2) == false)
1172 n2 = &vmanip.m_data[i2];
1173 if(content_walkable(n2->d))
1175 // Loop further down until not air
1176 bool dropped_to_unknown = false;
1178 vmanip.m_area.add_y(em, i2, -1);
1179 n2 = &vmanip.m_data[i2];
1180 // if out of known area
1181 if(vmanip.m_area.contains(i2) == false
1182 || n2->getContent() == CONTENT_IGNORE){
1183 dropped_to_unknown = true;
1186 }while(ndef->get(*n2).walkable == false);
1187 // Loop one up so that we're in air
1188 vmanip.m_area.add_y(em, i2, 1);
1189 n2 = &vmanip.m_data[i2];
1191 bool old_is_water = (n->getContent() == c_water_source);
1192 // Move mud to new place
1193 if(!dropped_to_unknown) {
1195 // Set old place to be air (or water)
1197 *n = MapNode(c_water_source);
1199 *n = MapNode(CONTENT_AIR);
1215 /***********************
1217 ************************/
1220 Add top and bottom side of water to transforming_liquid queue
1223 for(s16 x=full_node_min.X; x<=full_node_max.X; x++)
1224 for(s16 z=full_node_min.Z; z<=full_node_max.Z; z++)
1229 bool water_found = false;
1230 // Use fast index incrementing
1231 v3s16 em = vmanip.m_area.getExtent();
1232 u32 i = vmanip.m_area.index(v3s16(p2d.X, full_node_max.Y, p2d.Y));
1233 for(s16 y=full_node_max.Y; y>=full_node_min.Y; y--)
1235 if(y == full_node_max.Y){
1237 (vmanip.m_data[i].getContent() == c_water_source ||
1238 vmanip.m_data[i].getContent() == c_lava_source);
1240 else if(water_found == false)
1242 if(vmanip.m_data[i].getContent() == c_water_source ||
1243 vmanip.m_data[i].getContent() == c_lava_source)
1245 v3s16 p = v3s16(p2d.X, y, p2d.Y);
1246 data->transforming_liquid.push_back(p);
1252 // This can be done because water_found can only
1253 // turn to true and end up here after going through
1255 if(vmanip.m_data[i+1].getContent() != c_water_source ||
1256 vmanip.m_data[i+1].getContent() != c_lava_source)
1258 v3s16 p = v3s16(p2d.X, y+1, p2d.Y);
1259 data->transforming_liquid.push_back(p);
1260 water_found = false;
1264 vmanip.m_area.add_y(em, i, -1);
1273 for(s16 x=full_node_min.X; x<=full_node_max.X; x++)
1274 for(s16 z=full_node_min.Z; z<=full_node_max.Z; z++)
1276 // Node position in 2d
1277 v2s16 p2d = v2s16(x,z);
1280 Find the lowest surface to which enough light ends up
1283 Basically just wait until not air and not leaves.
1287 v3s16 em = vmanip.m_area.getExtent();
1288 u32 i = vmanip.m_area.index(v3s16(p2d.X, node_max.Y, p2d.Y));
1290 // Go to ground level
1291 for(y=node_max.Y; y>=full_node_min.Y; y--)
1293 MapNode &n = vmanip.m_data[i];
1294 if(ndef->get(n).param_type != CPT_LIGHT
1295 || ndef->get(n).liquid_type != LIQUID_NONE)
1297 vmanip.m_area.add_y(em, i, -1);
1299 if(y >= full_node_min.Y)
1302 surface_y = full_node_min.Y;
1305 u32 i = vmanip.m_area.index(p2d.X, surface_y, p2d.Y);
1306 MapNode *n = &vmanip.m_data[i];
1307 if(n->getContent() == c_dirt){
1308 // Well yeah, this can't be overground...
1309 if(surface_y < water_level - 20)
1311 n->setContent(c_dirt_with_grass);
1318 assert(central_area_size.X == central_area_size.Z);
1320 // Divide area into parts
1322 s16 sidelen = central_area_size.X / div;
1323 double area = sidelen * sidelen;
1324 for(s16 x0=0; x0<div; x0++)
1325 for(s16 z0=0; z0<div; z0++)
1327 // Center position of part of division
1329 node_min.X + sidelen/2 + sidelen*x0,
1330 node_min.Z + sidelen/2 + sidelen*z0
1332 // Minimum edge of part of division
1334 node_min.X + sidelen*x0,
1335 node_min.Z + sidelen*z0
1337 // Maximum edge of part of division
1339 node_min.X + sidelen + sidelen*x0 - 1,
1340 node_min.Z + sidelen + sidelen*z0 - 1
1343 u32 tree_count = area * tree_amount_2d(data->seed, p2d_center);
1344 // Put trees in random places on part of division
1345 for(u32 i=0; i<tree_count; i++)
1347 s16 x = myrand_range(p2d_min.X, p2d_max.X);
1348 s16 z = myrand_range(p2d_min.Y, p2d_max.Y);
1349 s16 y = find_ground_level(vmanip, v2s16(x,z), ndef);
1350 // Don't make a tree under water level
1353 // Don't make a tree so high that it doesn't fit
1354 if(y > node_max.Y - 6)
1358 Trees grow only on mud and grass
1361 u32 i = vmanip.m_area.index(v3s16(p));
1362 MapNode *n = &vmanip.m_data[i];
1363 if(n->getContent() != c_dirt
1364 && n->getContent() != c_dirt_with_grass)
1369 make_tree(vmanip, p, false, ndef);
1379 ScopeProfiler sp(g_profiler, "EmergeThread: mapgen lighting update",
1381 //VoxelArea a(node_min, node_max);
1382 VoxelArea a(node_min-v3s16(1,0,1)*MAP_BLOCKSIZE,
1383 node_max+v3s16(1,0,1)*MAP_BLOCKSIZE);
1384 /*VoxelArea a(node_min-v3s16(1,0,1)*MAP_BLOCKSIZE/2,
1385 node_max+v3s16(1,0,1)*MAP_BLOCKSIZE/2);*/
1386 enum LightBank banks[2] = {LIGHTBANK_DAY, LIGHTBANK_NIGHT};
1387 for(int i=0; i<2; i++)
1389 enum LightBank bank = banks[i];
1391 core::map<v3s16, bool> light_sources;
1392 core::map<v3s16, u8> unlight_from;
1394 voxalgo::clearLightAndCollectSources(vmanip, a, bank, ndef,
1395 light_sources, unlight_from);
1397 bool inexistent_top_provides_sunlight = !block_is_underground;
1398 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
1399 vmanip, a, inexistent_top_provides_sunlight,
1400 light_sources, ndef);
1401 // TODO: Do stuff according to bottom_sunlight_valid
1403 vmanip.unspreadLight(bank, unlight_from, light_sources, ndef);
1405 vmanip.spreadLight(bank, light_sources, ndef);