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
34 #include "mapgen_v6.h"
36 /////////////////// Mapgen V6 perlin noise default values
37 NoiseParams nparams_v6_def_terrain_base =
38 {-AVERAGE_MUD_AMOUNT, 20.0, v3f(250.0, 250.0, 250.0), 82341, 5, 0.6};
39 NoiseParams nparams_v6_def_terrain_higher =
40 {20.0, 16.0, v3f(500.0, 500.0, 500.0), 85039, 5, 0.6};
41 NoiseParams nparams_v6_def_steepness =
42 {0.85, 0.5, v3f(125.0, 125.0, 125.0), -932, 5, 0.7};
43 NoiseParams nparams_v6_def_height_select =
44 {0.5, 1.0, v3f(250.0, 250.0, 250.0), 4213, 5, 0.69};
45 NoiseParams nparams_v6_def_trees =
46 {0.0, 1.0, v3f(125.0, 125.0, 125.0), 2, 4, 0.66};
47 NoiseParams nparams_v6_def_mud =
48 {AVERAGE_MUD_AMOUNT, 2.0, v3f(200.0, 200.0, 200.0), 91013, 3, 0.55};
49 NoiseParams nparams_v6_def_beach =
50 {0.0, 1.0, v3f(250.0, 250.0, 250.0), 59420, 3, 0.50};
51 NoiseParams nparams_v6_def_biome =
52 {0.0, 1.0, v3f(250.0, 250.0, 250.0), 9130, 3, 0.50};
53 NoiseParams nparams_v6_def_cave =
54 {6.0, 6.0, v3f(250.0, 250.0, 250.0), 34329, 3, 0.50};
57 ///////////////////////////////////////////////////////////////////////////////
60 MapgenV6::MapgenV6(int mapgenid, MapgenV6Params *params) {
61 this->generating = false;
64 this->seed = (int)params->seed;
65 this->water_level = params->water_level;
66 this->flags = params->flags;
67 this->csize = v3s16(1, 1, 1) * params->chunksize * MAP_BLOCKSIZE;
69 this->freq_desert = params->freq_desert;
70 this->freq_beach = params->freq_beach;
72 this->ystride = csize.X; //////fix this
74 np_cave = params->np_cave;
76 noise_terrain_base = new Noise(params->np_terrain_base, seed, csize.X, csize.Y);
77 noise_terrain_higher = new Noise(params->np_terrain_higher, seed, csize.X, csize.Y);
78 noise_steepness = new Noise(params->np_steepness, seed, csize.X, csize.Y);
79 noise_height_select = new Noise(params->np_height_select, seed, csize.X, csize.Y);
80 noise_trees = new Noise(params->np_trees, seed, csize.X, csize.Y);
81 noise_mud = new Noise(params->np_mud, seed, csize.X, csize.Y);
82 noise_beach = new Noise(params->np_beach, seed, csize.X, csize.Y);
83 noise_biome = new Noise(params->np_biome, seed, csize.X, csize.Y);
85 map_terrain_base = noise_terrain_base->result;
86 map_terrain_higher = noise_terrain_higher->result;
87 map_steepness = noise_steepness->result;
88 map_height_select = noise_height_select->result;
89 map_trees = noise_trees->result;
90 map_mud = noise_mud->result;
91 map_beach = noise_beach->result;
92 map_biome = noise_biome->result;
96 MapgenV6::~MapgenV6() {
97 delete noise_terrain_base;
98 delete noise_terrain_higher;
99 delete noise_steepness;
100 delete noise_height_select;
109 Some helper functions for the map generator
113 // Returns Y one under area minimum if not found
114 s16 MapgenV6::find_ground_level(VoxelManipulator &vmanip, v2s16 p2d,
115 INodeDefManager *ndef)
117 v3s16 em = vmanip.m_area.getExtent();
118 s16 y_nodes_max = vmanip.m_area.MaxEdge.Y;
119 s16 y_nodes_min = vmanip.m_area.MinEdge.Y;
120 u32 i = vmanip.m_area.index(v3s16(p2d.X, y_nodes_max, p2d.Y));
122 for(y=y_nodes_max; y>=y_nodes_min; y--)
124 MapNode &n = vmanip.m_data[i];
125 if(ndef->get(n).walkable)
128 vmanip.m_area.add_y(em, i, -1);
133 return y_nodes_min - 1;
136 // Returns Y one under area minimum if not found
137 s16 MapgenV6::find_stone_level(VoxelManipulator &vmanip, v2s16 p2d,
138 INodeDefManager *ndef)
140 v3s16 em = vmanip.m_area.getExtent();
141 s16 y_nodes_max = vmanip.m_area.MaxEdge.Y;
142 s16 y_nodes_min = vmanip.m_area.MinEdge.Y;
143 u32 i = vmanip.m_area.index(v3s16(p2d.X, y_nodes_max, p2d.Y));
145 content_t c_stone = ndef->getId("mapgen_stone");
146 content_t c_desert_stone = ndef->getId("mapgen_desert_stone");
147 for(y=y_nodes_max; y>=y_nodes_min; y--)
149 MapNode &n = vmanip.m_data[i];
150 content_t c = n.getContent();
151 if(c != CONTENT_IGNORE && (
152 c == c_stone || c == c_desert_stone))
155 vmanip.m_area.add_y(em, i, -1);
160 return y_nodes_min - 1;
164 void MapgenV6::make_tree(ManualMapVoxelManipulator &vmanip, v3s16 p0,
165 bool is_apple_tree, INodeDefManager *ndef)
167 MapNode treenode(ndef->getId("mapgen_tree"));
168 MapNode leavesnode(ndef->getId("mapgen_leaves"));
169 MapNode applenode(ndef->getId("mapgen_apple"));
171 s16 trunk_h = myrand_range(4, 5);
173 for(s16 ii=0; ii<trunk_h; ii++)
175 if(vmanip.m_area.contains(p1))
176 vmanip.m_data[vmanip.m_area.index(p1)] = treenode;
180 // p1 is now the last piece of the trunk
183 VoxelArea leaves_a(v3s16(-2,-1,-2), v3s16(2,2,2));
184 //SharedPtr<u8> leaves_d(new u8[leaves_a.getVolume()]);
185 Buffer<u8> leaves_d(leaves_a.getVolume());
186 for(s32 i=0; i<leaves_a.getVolume(); i++)
189 // Force leaves at near the end of the trunk
192 for(s16 z=-d; z<=d; z++)
193 for(s16 y=-d; y<=d; y++)
194 for(s16 x=-d; x<=d; x++)
196 leaves_d[leaves_a.index(v3s16(x,y,z))] = 1;
200 // Add leaves randomly
201 for(u32 iii=0; iii<7; iii++)
206 myrand_range(leaves_a.MinEdge.X, leaves_a.MaxEdge.X-d),
207 myrand_range(leaves_a.MinEdge.Y, leaves_a.MaxEdge.Y-d),
208 myrand_range(leaves_a.MinEdge.Z, leaves_a.MaxEdge.Z-d)
211 for(s16 z=0; z<=d; z++)
212 for(s16 y=0; y<=d; y++)
213 for(s16 x=0; x<=d; x++)
215 leaves_d[leaves_a.index(p+v3s16(x,y,z))] = 1;
219 // Blit leaves to vmanip
220 for(s16 z=leaves_a.MinEdge.Z; z<=leaves_a.MaxEdge.Z; z++)
221 for(s16 y=leaves_a.MinEdge.Y; y<=leaves_a.MaxEdge.Y; y++)
222 for(s16 x=leaves_a.MinEdge.X; x<=leaves_a.MaxEdge.X; x++)
226 if(vmanip.m_area.contains(p) == false)
228 u32 vi = vmanip.m_area.index(p);
229 if(vmanip.m_data[vi].getContent() != CONTENT_AIR
230 && vmanip.m_data[vi].getContent() != CONTENT_IGNORE)
232 u32 i = leaves_a.index(x,y,z);
233 if(leaves_d[i] == 1) {
234 bool is_apple = myrand_range(0,99) < 10;
235 if(is_apple_tree && is_apple) {
236 vmanip.m_data[vi] = applenode;
238 vmanip.m_data[vi] = leavesnode;
246 Noise functions. Make sure seed is mangled differently in each one.
250 // Amount of trees per area in nodes
251 double MapgenV6::tree_amount_2d(u64 seed, v2s16 p)
253 /*double noise = noise2d_perlin(
254 0.5+(float)p.X/125, 0.5+(float)p.Y/125,
256 double noise = map_trees[(p.Y - node_min.Z) * ystride + (p.X - node_min.X)];
257 double zeroval = -0.39;
261 return 0.04 * (noise-zeroval) / (1.0-zeroval);
264 // Required by mapgen.h
265 bool MapgenV6::block_is_underground(u64 seed, v3s16 blockpos)
267 /*s16 minimum_groundlevel = (s16)get_sector_minimum_ground_level(
268 seed, v2s16(blockpos.X, blockpos.Z));*/
269 // Nah, this is just a heuristic, just return something
270 s16 minimum_groundlevel = water_level;
272 if(blockpos.Y*MAP_BLOCKSIZE + MAP_BLOCKSIZE <= minimum_groundlevel)
279 double MapgenV6::base_rock_level_2d(u64 seed, v2s16 p)
281 int index = (p.Y - node_min.Z) * ystride + (p.X - node_min.X);
283 // The base ground level
284 /*double base = (double)WATER_LEVEL - (double)AVERAGE_MUD_AMOUNT
285 + 20. * noise2d_perlin(
286 0.5+(float)p.X/250., 0.5+(float)p.Y/250.,
287 seed+82341, 5, 0.6);*/
288 double base = water_level + map_terrain_base[index];
290 // Higher ground level
291 /*double higher = (double)WATER_LEVEL + 20. + 16. * noise2d_perlin(
292 0.5+(float)p.X/500., 0.5+(float)p.Y/500.,
293 seed+85039, 5, 0.6);*/
294 double higher = water_level + map_terrain_higher[index];
296 // Limit higher to at least base
300 // Steepness factor of cliffs
301 /*double b = 0.85 + 0.5 * noise2d_perlin(
302 0.5+(float)p.X/125., 0.5+(float)p.Y/125.,
304 double b = map_steepness[index];
305 b = rangelim(b, 0.0, 1000.0);
308 b = rangelim(b, 0.5, 1000.0);
310 // Values 1.5...100 give quite horrible looking slopes
311 if(b > 1.5 && b < 100.0){
318 // Offset to more low
319 double a_off = -0.20;
322 /*double a = (double)0.5 + b * (a_off + noise2d_perlin(
323 0.5+(float)p.X/250., 0.5+(float)p.Y/250.,
324 seed+4213, 5, 0.69));*/
325 double a = 0.5 + b * (a_off + map_height_select[index]);
328 a = rangelim(a, 0.0, 1.0);
330 double h = base*(1.0-a) + higher*a;
335 double MapgenV6::baseRockLevelFromNoise(v2s16 p) {
336 double base = water_level +
337 NoisePerlin2DPosOffset(noise_terrain_base->np, p.X, 0.5, p.Y, 0.5, seed);
338 double higher = water_level +
339 NoisePerlin2DPosOffset(noise_terrain_higher->np, p.X, 0.5, p.Y, 0.5, seed);
344 double b = NoisePerlin2DPosOffset(noise_steepness->np, p.X, 0.5, p.Y, 0.5, seed);
345 b = rangelim(b, 0.0, 1000.0);
348 b = rangelim(b, 0.5, 1000.0);
350 if(b > 1.5 && b < 100.0){
357 double a_off = -0.20;
358 double a = 0.5 + b * (a_off + NoisePerlin2DNoTxfmPosOffset(
359 noise_height_select->np, p.X, 0.5, p.Y, 0.5, seed));
360 a = rangelim(a, 0.0, 1.0);
362 return base * (1.0 - a) + higher * a;
366 s16 MapgenV6::find_ground_level_from_noise(u64 seed, v2s16 p2d, s16 precision)
368 return baseRockLevelFromNoise(p2d) + AVERAGE_MUD_AMOUNT;
371 double MapgenV6::get_mud_add_amount(u64 seed, v2s16 p)
373 /*return ((float)AVERAGE_MUD_AMOUNT + 2.0 * noise2d_perlin(
374 0.5+(float)p.X/200, 0.5+(float)p.Y/200,
375 seed+91013, 3, 0.55));*/
376 int index = (p.Y - node_min.Z) * ystride + (p.X - node_min.X);
377 return map_mud[index];
380 bool MapgenV6::get_have_beach(u64 seed, v2s16 p2d)
382 // Determine whether to have sand here
383 /*double sandnoise = noise2d_perlin(
384 0.2+(float)p2d.X/250, 0.7+(float)p2d.Y/250,
385 seed+59420, 3, 0.50);*/
386 int index = (p2d.Y - node_min.Z) * ystride + (p2d.X - node_min.X);
387 double sandnoise = map_beach[index];
389 return (sandnoise > freq_beach);
392 BiomeType MapgenV6::get_biome(u64 seed, v2s16 p2d)
394 // Just do something very simple as for now
395 /*double d = noise2d_perlin(
396 0.6+(float)p2d.X/250, 0.2+(float)p2d.Y/250,
397 seed+9130, 3, 0.50);*/
398 int index = (p2d.Y - node_min.Z) * ystride + (p2d.X - node_min.X);
399 double d = map_biome[index];
402 if (flags & MGV6_BIOME_BLEND) {
403 if(d > freq_desert - 0.10 &&
404 (noise2d(p2d.X, p2d.Y, seed) + 1.0) > (freq_desert - d) * 20.0)
410 u32 MapgenV6::get_blockseed(u64 seed, v3s16 p)
412 s32 x=p.X, y=p.Y, z=p.Z;
413 return (u32)(seed%0x100000000ULL) + z*38134234 + y*42123 + x*23;
417 int MapgenV6::getGroundLevelAtPoint(v2s16 p) {
418 return baseRockLevelFromNoise(p) + AVERAGE_MUD_AMOUNT;
421 #define VMANIP_FLAG_CAVE VOXELFLAG_CHECKED1
423 void MapgenV6::makeChunk(BlockMakeData *data)
427 //dstream<<"makeBlock: no-op"<<std::endl;
431 this->generating = true;
433 assert(data->vmanip);
434 assert(data->nodedef);
435 assert(data->blockpos_requested.X >= data->blockpos_min.X &&
436 data->blockpos_requested.Y >= data->blockpos_min.Y &&
437 data->blockpos_requested.Z >= data->blockpos_min.Z);
438 assert(data->blockpos_requested.X <= data->blockpos_max.X &&
439 data->blockpos_requested.Y <= data->blockpos_max.Y &&
440 data->blockpos_requested.Z <= data->blockpos_max.Z);
442 INodeDefManager *ndef = data->nodedef;
444 // Hack: use minimum block coordinates for old code that assumes
446 v3s16 blockpos = data->blockpos_requested;
448 /*dstream<<"makeBlock(): ("<<blockpos.X<<","<<blockpos.Y<<","
449 <<blockpos.Z<<")"<<std::endl;*/
451 v3s16 blockpos_min = data->blockpos_min;
452 v3s16 blockpos_max = data->blockpos_max;
453 v3s16 blockpos_full_min = blockpos_min - v3s16(1,1,1);
454 v3s16 blockpos_full_max = blockpos_max + v3s16(1,1,1);
456 ManualMapVoxelManipulator &vmanip = *(data->vmanip);
457 // Area of central chunk
458 node_min = blockpos_min*MAP_BLOCKSIZE;
459 node_max = (blockpos_max+v3s16(1,1,1))*MAP_BLOCKSIZE-v3s16(1,1,1);
460 // Full allocated area
461 v3s16 full_node_min = (blockpos_min-1)*MAP_BLOCKSIZE;
462 v3s16 full_node_max = (blockpos_max+2)*MAP_BLOCKSIZE-v3s16(1,1,1);
464 v3s16 central_area_size = node_max - node_min + v3s16(1,1,1);
466 const s16 max_spread_amount = MAP_BLOCKSIZE;
468 int volume_blocks = (blockpos_max.X - blockpos_min.X + 1)
469 * (blockpos_max.Y - blockpos_min.Y + 1)
470 * (blockpos_max.Z - blockpos_max.Z + 1);
472 int volume_nodes = volume_blocks *
473 MAP_BLOCKSIZE*MAP_BLOCKSIZE*MAP_BLOCKSIZE;
475 // Generated surface area
476 //double gen_area_nodes = MAP_BLOCKSIZE*MAP_BLOCKSIZE * rel_volume;
478 // Horribly wrong heuristic, but better than nothing
479 bool block_is_underground = (water_level > node_max.Y);
482 Create a block-specific seed
484 u32 blockseed = get_blockseed(data->seed, full_node_min);
493 // Need to adjust for the original implementation's +.5 offset...
494 noise_terrain_base->perlinMap2D(
495 x + 0.5 * noise_terrain_base->np->spread.X,
496 z + 0.5 * noise_terrain_base->np->spread.Z);
497 noise_terrain_base->transformNoiseMap();
499 noise_terrain_higher->perlinMap2D(
500 x + 0.5 * noise_terrain_higher->np->spread.X,
501 z + 0.5 * noise_terrain_higher->np->spread.Z);
502 noise_terrain_higher->transformNoiseMap();
504 noise_steepness->perlinMap2D(
505 x + 0.5 * noise_steepness->np->spread.X,
506 z + 0.5 * noise_steepness->np->spread.Z);
507 noise_steepness->transformNoiseMap();
509 noise_height_select->perlinMap2D(
510 x + 0.5 * noise_height_select->np->spread.X,
511 z + 0.5 * noise_height_select->np->spread.Z);
513 noise_trees->perlinMap2D(
514 x + 0.5 * noise_trees->np->spread.X,
515 z + 0.5 * noise_trees->np->spread.Z);
517 noise_mud->perlinMap2D(
518 x + 0.5 * noise_mud->np->spread.X,
519 z + 0.5 * noise_mud->np->spread.Z);
520 noise_mud->transformNoiseMap();
522 noise_beach->perlinMap2D(
523 x + 0.2 * noise_beach->np->spread.X,
524 z + 0.7 * noise_beach->np->spread.Z);
526 noise_biome->perlinMap2D(
527 x + 0.6 * noise_biome->np->spread.X,
528 z + 0.2 * noise_biome->np->spread.Z);
533 Cache some ground type values for speed
536 // Creates variables c_name=id and n_name=node
537 #define CONTENT_VARIABLE(ndef, name)\
538 content_t c_##name = ndef->getId("mapgen_" #name);\
539 MapNode n_##name(c_##name);
540 // Default to something else if was CONTENT_IGNORE
541 #define CONTENT_VARIABLE_FALLBACK(name, dname)\
542 if(c_##name == CONTENT_IGNORE){\
543 c_##name = c_##dname;\
544 n_##name = n_##dname;\
547 CONTENT_VARIABLE(ndef, stone);
548 CONTENT_VARIABLE(ndef, air);
549 CONTENT_VARIABLE(ndef, water_source);
550 CONTENT_VARIABLE(ndef, dirt);
551 CONTENT_VARIABLE(ndef, sand);
552 CONTENT_VARIABLE(ndef, gravel);
553 CONTENT_VARIABLE(ndef, clay);
554 CONTENT_VARIABLE(ndef, lava_source);
555 CONTENT_VARIABLE(ndef, cobble);
556 CONTENT_VARIABLE(ndef, mossycobble);
557 CONTENT_VARIABLE(ndef, dirt_with_grass);
558 CONTENT_VARIABLE(ndef, junglegrass);
559 CONTENT_VARIABLE(ndef, stone_with_coal);
560 CONTENT_VARIABLE(ndef, stone_with_iron);
561 CONTENT_VARIABLE(ndef, mese);
562 CONTENT_VARIABLE(ndef, desert_sand);
563 CONTENT_VARIABLE_FALLBACK(desert_sand, sand);
564 CONTENT_VARIABLE(ndef, desert_stone);
565 CONTENT_VARIABLE_FALLBACK(desert_stone, stone);
567 // Maximum height of the stone surface and obstacles.
568 // This is used to guide the cave generation
569 s16 stone_surface_max_y = 0;
572 Generate general ground level to full area
576 TimeTaker timer1("Generating ground level");
578 for(s16 x=node_min.X; x<=node_max.X; x++)
579 for(s16 z=node_min.Z; z<=node_max.Z; z++)
582 v2s16 p2d = v2s16(x,z);
585 Skip of already generated
588 v3s16 p(p2d.X, node_min.Y, p2d.Y);
589 if(vmanip.m_data[vmanip.m_area.index(p)].d != CONTENT_AIR)
593 // Ground height at this point
594 float surface_y_f = 0.0;
596 // Use perlin noise for ground height
597 surface_y_f = base_rock_level_2d(data->seed, p2d);
599 /*// Experimental stuff
601 float a = highlands_level_2d(data->seed, p2d);
606 // Convert to integer
607 s16 surface_y = (s16)surface_y_f;
610 if(surface_y > stone_surface_max_y)
611 stone_surface_max_y = surface_y;
613 BiomeType bt = get_biome(data->seed, p2d);
615 Fill ground with stone
618 // Use fast index incrementing
619 v3s16 em = vmanip.m_area.getExtent();
620 u32 i = vmanip.m_area.index(v3s16(p2d.X, node_min.Y, p2d.Y));
621 for(s16 y=node_min.Y; y<=node_max.Y; y++)
623 if(vmanip.m_data[i].getContent() == CONTENT_IGNORE){
625 if(y > water_level && bt == BT_DESERT)
626 vmanip.m_data[i] = n_desert_stone;
628 vmanip.m_data[i] = n_stone;
629 } else if(y <= water_level){
630 vmanip.m_data[i] = MapNode(c_water_source);
632 vmanip.m_data[i] = MapNode(c_air);
635 vmanip.m_area.add_y(em, i, 1);
643 // Limit dirt flow area by 1 because mud is flown into neighbors.
644 assert(central_area_size.X == central_area_size.Z);
645 s16 mudflow_minpos = 0-max_spread_amount+1;
646 s16 mudflow_maxpos = central_area_size.X+max_spread_amount-2;
649 Loop this part, it will make stuff look older and newer nicely
652 /*double cave_amount = 6.0 + 6.0 * noise2d_perlin(
653 0.5+(double)node_min.X/250, 0.5+(double)node_min.Y/250,
654 data->seed+34329, 3, 0.50);*/
656 double cave_amount = NoisePerlin2D(np_cave, node_min.X, node_min.Y, data->seed);
658 const u32 age_loops = 2;
659 for(u32 i_age=0; i_age<age_loops; i_age++)
661 /******************************
662 BEGINNING OF AGING LOOP
663 ******************************/
668 //TimeTaker timer1("caves");
671 Make caves (this code is relatively horrible)
673 cave_amount = MYMAX(0.0, cave_amount);
674 u32 caves_count = cave_amount * volume_nodes / 50000;
675 u32 bruises_count = 1;
676 PseudoRandom ps(blockseed+21343);
677 PseudoRandom ps2(blockseed+1032);
678 if(ps.range(1, 6) == 1)
679 bruises_count = ps.range(0, ps.range(0, 2));
680 if(get_biome(data->seed, v2s16(node_min.X, node_min.Z)) == BT_DESERT){
684 for(u32 jj=0; jj<caves_count+bruises_count; jj++)
686 if (!(flags & MG_CAVES))
689 /*int avg_height = (int)
690 ((base_rock_level_2d(data->seed, v2s16(node_min.X, node_min.Z)) +
691 base_rock_level_2d(data->seed, v2s16(node_max.X, node_max.Z))) / 2);
692 if ((node_max.Y + node_min.Y) / 2 > avg_height)
695 bool large_cave = (jj >= caves_count);
696 s16 min_tunnel_diameter = 2;
697 s16 max_tunnel_diameter = ps.range(2,6);
698 int dswitchint = ps.range(1,14);
699 u16 tunnel_routepoints = 0;
700 int part_max_length_rs = 0;
702 part_max_length_rs = ps.range(2,4);
703 tunnel_routepoints = ps.range(5, ps.range(15,30));
704 min_tunnel_diameter = 5;
705 max_tunnel_diameter = ps.range(7, ps.range(8,24));
707 part_max_length_rs = ps.range(2,9);
708 tunnel_routepoints = ps.range(10, ps.range(15,30));
710 bool large_cave_is_flat = (ps.range(0,1) == 0);
712 v3f main_direction(0,0,0);
714 // Allowed route area size in nodes
715 v3s16 ar = central_area_size;
717 // Area starting point in nodes
721 //(this should be more than the maximum radius of the tunnel)
723 s16 more = max_spread_amount - max_tunnel_diameter/2 - insure;
724 ar += v3s16(1,0,1) * more * 2;
725 of -= v3s16(1,0,1) * more;
728 // Allow half a diameter + 7 over stone surface
729 s16 route_y_max = -of.Y + stone_surface_max_y + max_tunnel_diameter/2 + 7;
731 // Limit maximum to area
732 route_y_max = rangelim(route_y_max, 0, ar.Y-1);
737 if(node_min.Y < water_level && node_max.Y > water_level)
739 min = water_level - max_tunnel_diameter/3 - of.Y;
740 route_y_max = water_level + max_tunnel_diameter/3 - of.Y;
742 route_y_min = ps.range(min, min + max_tunnel_diameter);
743 route_y_min = rangelim(route_y_min, 0, route_y_max);
746 s16 route_start_y_min = route_y_min;
747 s16 route_start_y_max = route_y_max;
749 route_start_y_min = rangelim(route_start_y_min, 0, ar.Y-1);
750 route_start_y_max = rangelim(route_start_y_max, route_start_y_min, ar.Y-1);
752 // Randomize starting position
754 (float)(ps.next()%ar.X)+0.5,
755 (float)(ps.range(route_start_y_min, route_start_y_max))+0.5,
756 (float)(ps.next()%ar.Z)+0.5
759 v3s16 startp(orp.X, orp.Y, orp.Z);
762 MapNode airnode(CONTENT_AIR);
763 MapNode waternode(c_water_source);
764 MapNode lavanode(c_lava_source);
767 Generate some tunnel starting from orp
770 for(u16 j=0; j<tunnel_routepoints; j++)
772 if(j%dswitchint==0 && large_cave == false)
774 main_direction = v3f(
775 ((float)(ps.next()%20)-(float)10)/10,
776 ((float)(ps.next()%20)-(float)10)/30,
777 ((float)(ps.next()%20)-(float)10)/10
779 main_direction *= (float)ps.range(0, 10)/10;
783 s16 min_d = min_tunnel_diameter;
784 s16 max_d = max_tunnel_diameter;
785 s16 rs = ps.range(min_d, max_d);
787 // Every second section is rough
788 bool randomize_xz = (ps2.range(1,2) == 1);
794 rs*part_max_length_rs,
795 rs*part_max_length_rs/2,
796 rs*part_max_length_rs
802 rs*part_max_length_rs,
803 ps.range(1, rs*part_max_length_rs),
804 rs*part_max_length_rs
811 (float)(ps.next()%(maxlen.X*1))-(float)maxlen.X/2,
812 (float)(ps.next()%(maxlen.Y*1))-(float)maxlen.Y/2,
813 (float)(ps.next()%(maxlen.Z*1))-(float)maxlen.Z/2
816 // Jump downward sometimes
817 if(!large_cave && ps.range(0,12) == 0)
820 (float)(ps.next()%(maxlen.X*1))-(float)maxlen.X/2,
821 (float)(ps.next()%(maxlen.Y*2))-(float)maxlen.Y*2/2,
822 (float)(ps.next()%(maxlen.Z*1))-(float)maxlen.Z/2
828 s16 h = find_ground_level_clever(vmanip,
829 v2s16(p.X, p.Z), ndef);
830 route_y_min = h - rs/3;
831 route_y_max = h + rs;
834 vec += main_direction;
839 else if(rp.X >= ar.X)
841 if(rp.Y < route_y_min)
843 else if(rp.Y >= route_y_max)
844 rp.Y = route_y_max-1;
847 else if(rp.Z >= ar.Z)
851 for(float f=0; f<1.0; f+=1.0/vec.getLength())
853 v3f fp = orp + vec * f;
854 fp.X += 0.1*ps.range(-10,10);
855 fp.Z += 0.1*ps.range(-10,10);
856 v3s16 cp(fp.X, fp.Y, fp.Z);
861 d0 += ps.range(-1,1);
862 d1 += ps.range(-1,1);
864 for(s16 z0=d0; z0<=d1; z0++)
866 s16 si = rs/2 - MYMAX(0, abs(z0)-rs/7-1);
867 for(s16 x0=-si-ps.range(0,1); x0<=si-1+ps.range(0,1); x0++)
869 s16 maxabsxz = MYMAX(abs(x0), abs(z0));
870 s16 si2 = rs/2 - MYMAX(0, maxabsxz-rs/7-1);
871 for(s16 y0=-si2; y0<=si2; y0++)
873 /*// Make better floors in small caves
874 if(y0 <= -rs/2 && rs<=7)
876 if(large_cave_is_flat){
877 // Make large caves not so tall
878 if(rs > 7 && abs(y0) >= rs/3)
888 if(vmanip.m_area.contains(p) == false)
891 u32 i = vmanip.m_area.index(p);
895 if(full_node_min.Y < water_level &&
896 full_node_max.Y > water_level){
897 if(p.Y <= water_level)
898 vmanip.m_data[i] = waternode;
900 vmanip.m_data[i] = airnode;
901 } else if(full_node_max.Y < water_level){
902 if(p.Y < startp.Y - 2)
903 vmanip.m_data[i] = lavanode;
905 vmanip.m_data[i] = airnode;
907 vmanip.m_data[i] = airnode;
910 // Don't replace air or water or lava or ignore
911 if(vmanip.m_data[i].getContent() == CONTENT_IGNORE ||
912 vmanip.m_data[i].getContent() == CONTENT_AIR ||
913 vmanip.m_data[i].getContent() == c_water_source ||
914 vmanip.m_data[i].getContent() == c_lava_source)
917 vmanip.m_data[i] = airnode;
920 vmanip.m_flags[i] |= VMANIP_FLAG_CAVE;
938 TimeTaker timer1("add mud");
941 Add mud to the central chunk
944 for(s16 x=node_min.X; x<=node_max.X; x++)
945 for(s16 z=node_min.Z; z<=node_max.Z; z++)
947 // Node position in 2d
948 v2s16 p2d = v2s16(x,z);
950 // Randomize mud amount
951 s16 mud_add_amount = get_mud_add_amount(data->seed, p2d) / 2.0 + 0.5;
954 s16 surface_y = find_stone_level(vmanip, p2d, ndef);
955 // Handle area not found
956 if(surface_y == vmanip.m_area.MinEdge.Y - 1)
959 MapNode addnode(c_dirt);
960 BiomeType bt = get_biome(data->seed, p2d);
963 addnode = MapNode(c_desert_sand);
965 if(bt == BT_DESERT && surface_y + mud_add_amount <= water_level+1){
966 addnode = MapNode(c_sand);
967 } else if(mud_add_amount <= 0){
968 mud_add_amount = 1 - mud_add_amount;
969 addnode = MapNode(c_gravel);
970 } else if(bt == BT_NORMAL && get_have_beach(data->seed, p2d) &&
971 surface_y + mud_add_amount <= water_level+2){
972 addnode = MapNode(c_sand);
977 mud_add_amount = MYMAX(0, mud_add_amount - (surface_y - 20)/5);
982 If topmost node is grass, change it to mud.
983 It might be if it was flown to there from a neighboring
984 chunk and then converted.
987 u32 i = vmanip.m_area.index(v3s16(p2d.X, surface_y, p2d.Y));
988 MapNode *n = &vmanip.m_data[i];
989 if(n->getContent() == c_dirt_with_grass)
990 *n = MapNode(c_dirt);
998 v3s16 em = vmanip.m_area.getExtent();
999 s16 y_start = surface_y+1;
1000 u32 i = vmanip.m_area.index(v3s16(p2d.X, y_start, p2d.Y));
1001 for(s16 y=y_start; y<=node_max.Y; y++)
1003 if(mudcount >= mud_add_amount)
1006 MapNode &n = vmanip.m_data[i];
1010 vmanip.m_area.add_y(em, i, 1);
1020 Add blobs of dirt and gravel underground
1022 if(get_biome(data->seed, v2s16(node_min.X, node_min.Z)) == BT_NORMAL)
1024 PseudoRandom pr(blockseed+983);
1025 for(int i=0; i<volume_nodes/10/10/10; i++)
1027 bool only_fill_cave = (myrand_range(0,1) != 0);
1034 pr.range(node_min.X, node_max.X)-size.X/2,
1035 pr.range(node_min.Y, node_max.Y)-size.Y/2,
1036 pr.range(node_min.Z, node_max.Z)-size.Z/2
1039 if(p0.Y > -32 && pr.range(0,1) == 0)
1040 n1 = MapNode(c_dirt);
1042 n1 = MapNode(c_gravel);
1043 for(int x1=0; x1<size.X; x1++)
1044 for(int y1=0; y1<size.Y; y1++)
1045 for(int z1=0; z1<size.Z; z1++)
1047 v3s16 p = p0 + v3s16(x1,y1,z1);
1048 u32 i = vmanip.m_area.index(p);
1049 if(!vmanip.m_area.contains(i))
1051 // Cancel if not stone and not cave air
1052 if(vmanip.m_data[i].getContent() != c_stone &&
1053 !(vmanip.m_flags[i] & VMANIP_FLAG_CAVE))
1055 if(only_fill_cave && !(vmanip.m_flags[i] & VMANIP_FLAG_CAVE))
1057 vmanip.m_data[i] = n1;
1065 TimeTaker timer1("flow mud");
1068 Flow mud away from steep edges
1071 // Iterate a few times
1072 for(s16 k=0; k<3; k++)
1075 for(s16 x=mudflow_minpos; x<=mudflow_maxpos; x++)
1076 for(s16 z=mudflow_minpos; z<=mudflow_maxpos; z++)
1078 // Invert coordinates every 2nd iteration
1081 x = mudflow_maxpos - (x-mudflow_minpos);
1082 z = mudflow_maxpos - (z-mudflow_minpos);
1085 // Node position in 2d
1086 v2s16 p2d = v2s16(node_min.X, node_min.Z) + v2s16(x,z);
1088 v3s16 em = vmanip.m_area.getExtent();
1089 u32 i = vmanip.m_area.index(v3s16(p2d.X, node_max.Y, p2d.Y));
1092 while(y >= node_min.Y)
1099 for(; y>=node_min.Y; y--)
1101 n = &vmanip.m_data[i];
1102 //if(content_walkable(n->d))
1104 if(n->getContent() == c_dirt ||
1105 n->getContent() == c_dirt_with_grass ||
1106 n->getContent() == c_gravel)
1109 vmanip.m_area.add_y(em, i, -1);
1112 // Stop if out of area
1113 //if(vmanip.m_area.contains(i) == false)
1117 /*// If not mud, do nothing to it
1118 MapNode *n = &vmanip.m_data[i];
1119 if(n->d != CONTENT_MUD && n->d != CONTENT_GRASS)
1122 if(n->getContent() == c_dirt ||
1123 n->getContent() == c_dirt_with_grass)
1125 // Make it exactly mud
1126 n->setContent(c_dirt);
1129 Don't flow it if the stuff under it is not mud
1133 vmanip.m_area.add_y(em, i2, -1);
1134 // Cancel if out of area
1135 if(vmanip.m_area.contains(i2) == false)
1137 MapNode *n2 = &vmanip.m_data[i2];
1138 if(n2->getContent() != c_dirt &&
1139 n2->getContent() != c_dirt_with_grass)
1144 /*s16 recurse_count = 0;
1148 v3s16(0,0,1), // back
1149 v3s16(1,0,0), // right
1150 v3s16(0,0,-1), // front
1151 v3s16(-1,0,0), // left
1154 // Theck that upper is air or doesn't exist.
1155 // Cancel dropping if upper keeps it in place
1157 vmanip.m_area.add_y(em, i3, 1);
1158 if(vmanip.m_area.contains(i3) == true
1159 && ndef->get(vmanip.m_data[i3]).walkable)
1166 for(u32 di=0; di<4; di++)
1168 v3s16 dirp = dirs4[di];
1171 vmanip.m_area.add_p(em, i2, dirp);
1172 // Fail if out of area
1173 if(vmanip.m_area.contains(i2) == false)
1175 // Check that side is air
1176 MapNode *n2 = &vmanip.m_data[i2];
1177 if(ndef->get(*n2).walkable)
1179 // Check that under side is air
1180 vmanip.m_area.add_y(em, i2, -1);
1181 if(vmanip.m_area.contains(i2) == false)
1183 n2 = &vmanip.m_data[i2];
1184 if(ndef->get(*n2).walkable)
1186 /*// Check that under that is air (need a drop of 2)
1187 vmanip.m_area.add_y(em, i2, -1);
1188 if(vmanip.m_area.contains(i2) == false)
1190 n2 = &vmanip.m_data[i2];
1191 if(content_walkable(n2->d))
1193 // Loop further down until not air
1194 bool dropped_to_unknown = false;
1196 vmanip.m_area.add_y(em, i2, -1);
1197 n2 = &vmanip.m_data[i2];
1198 // if out of known area
1199 if(vmanip.m_area.contains(i2) == false
1200 || n2->getContent() == CONTENT_IGNORE){
1201 dropped_to_unknown = true;
1204 }while(ndef->get(*n2).walkable == false);
1205 // Loop one up so that we're in air
1206 vmanip.m_area.add_y(em, i2, 1);
1207 n2 = &vmanip.m_data[i2];
1209 bool old_is_water = (n->getContent() == c_water_source);
1210 // Move mud to new place
1211 if(!dropped_to_unknown) {
1213 // Set old place to be air (or water)
1215 *n = MapNode(c_water_source);
1217 *n = MapNode(CONTENT_AIR);
1233 /***********************
1235 ************************/
1238 Add top and bottom side of water to transforming_liquid queue
1241 for(s16 x=full_node_min.X; x<=full_node_max.X; x++)
1242 for(s16 z=full_node_min.Z; z<=full_node_max.Z; z++)
1247 bool water_found = false;
1248 // Use fast index incrementing
1249 v3s16 em = vmanip.m_area.getExtent();
1250 u32 i = vmanip.m_area.index(v3s16(p2d.X, full_node_max.Y, p2d.Y));
1251 for(s16 y=full_node_max.Y; y>=full_node_min.Y; y--)
1253 if(y == full_node_max.Y){
1255 (vmanip.m_data[i].getContent() == c_water_source ||
1256 vmanip.m_data[i].getContent() == c_lava_source);
1258 else if(water_found == false)
1260 if(vmanip.m_data[i].getContent() == c_water_source ||
1261 vmanip.m_data[i].getContent() == c_lava_source)
1263 v3s16 p = v3s16(p2d.X, y, p2d.Y);
1264 data->transforming_liquid.push_back(p);
1270 // This can be done because water_found can only
1271 // turn to true and end up here after going through
1273 if(vmanip.m_data[i+1].getContent() != c_water_source ||
1274 vmanip.m_data[i+1].getContent() != c_lava_source)
1276 v3s16 p = v3s16(p2d.X, y+1, p2d.Y);
1277 data->transforming_liquid.push_back(p);
1278 water_found = false;
1282 vmanip.m_area.add_y(em, i, -1);
1291 for(s16 x=full_node_min.X; x<=full_node_max.X; x++)
1292 for(s16 z=full_node_min.Z; z<=full_node_max.Z; z++)
1294 // Node position in 2d
1295 v2s16 p2d = v2s16(x,z);
1298 Find the lowest surface to which enough light ends up
1301 Basically just wait until not air and not leaves.
1305 v3s16 em = vmanip.m_area.getExtent();
1306 u32 i = vmanip.m_area.index(v3s16(p2d.X, node_max.Y, p2d.Y));
1308 // Go to ground level
1309 for(y=node_max.Y; y>=full_node_min.Y; y--)
1311 MapNode &n = vmanip.m_data[i];
1312 if(ndef->get(n).param_type != CPT_LIGHT
1313 || ndef->get(n).liquid_type != LIQUID_NONE)
1315 vmanip.m_area.add_y(em, i, -1);
1317 if(y >= full_node_min.Y)
1320 surface_y = full_node_min.Y;
1323 u32 i = vmanip.m_area.index(p2d.X, surface_y, p2d.Y);
1324 MapNode *n = &vmanip.m_data[i];
1325 if(n->getContent() == c_dirt){
1326 // Well yeah, this can't be overground...
1327 if(surface_y < water_level - 20)
1329 n->setContent(c_dirt_with_grass);
1336 assert(central_area_size.X == central_area_size.Z);
1337 if (flags & MG_TREES) {
1338 // Divide area into parts
1340 s16 sidelen = central_area_size.X / div;
1341 double area = sidelen * sidelen;
1342 for(s16 x0=0; x0<div; x0++)
1343 for(s16 z0=0; z0<div; z0++)
1345 // Center position of part of division
1347 node_min.X + sidelen/2 + sidelen*x0,
1348 node_min.Z + sidelen/2 + sidelen*z0
1350 // Minimum edge of part of division
1352 node_min.X + sidelen*x0,
1353 node_min.Z + sidelen*z0
1355 // Maximum edge of part of division
1357 node_min.X + sidelen + sidelen*x0 - 1,
1358 node_min.Z + sidelen + sidelen*z0 - 1
1361 u32 tree_count = area * tree_amount_2d(data->seed, p2d_center);
1362 // Put trees in random places on part of division
1363 for(u32 i=0; i<tree_count; i++)
1365 s16 x = myrand_range(p2d_min.X, p2d_max.X);
1366 s16 z = myrand_range(p2d_min.Y, p2d_max.Y);
1367 s16 y = find_ground_level(vmanip, v2s16(x,z), ndef);
1368 // Don't make a tree under water level
1371 // Don't make a tree so high that it doesn't fit
1372 if(y > node_max.Y - 6)
1376 Trees grow only on mud and grass
1379 u32 i = vmanip.m_area.index(v3s16(p));
1380 MapNode *n = &vmanip.m_data[i];
1381 if(n->getContent() != c_dirt
1382 && n->getContent() != c_dirt_with_grass)
1387 make_tree(vmanip, p, false, ndef);
1397 ScopeProfiler sp(g_profiler, "EmergeThread: mapgen lighting update",
1399 //VoxelArea a(node_min, node_max);
1400 VoxelArea a(node_min-v3s16(1,0,1)*MAP_BLOCKSIZE,
1401 node_max+v3s16(1,0,1)*MAP_BLOCKSIZE);
1402 /*VoxelArea a(node_min-v3s16(1,0,1)*MAP_BLOCKSIZE/2,
1403 node_max+v3s16(1,0,1)*MAP_BLOCKSIZE/2);*/
1404 enum LightBank banks[2] = {LIGHTBANK_DAY, LIGHTBANK_NIGHT};
1405 for(int i=0; i<2; i++)
1407 enum LightBank bank = banks[i];
1409 core::map<v3s16, bool> light_sources;
1410 core::map<v3s16, u8> unlight_from;
1412 voxalgo::clearLightAndCollectSources(vmanip, a, bank, ndef,
1413 light_sources, unlight_from);
1415 bool inexistent_top_provides_sunlight = !block_is_underground;
1416 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
1417 vmanip, a, inexistent_top_provides_sunlight,
1418 light_sources, ndef);
1419 // TODO: Do stuff according to bottom_sunlight_valid
1421 vmanip.unspreadLight(bank, unlight_from, light_sources, ndef);
1423 vmanip.spreadLight(bank, light_sources, ndef);