Misc. cleanup and minor fixes
[oweals/minetest.git] / src / mapgen_v6.cpp
1 /*
2 Minetest
3 Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
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.
9
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.
14
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.
18 */
19
20 #include "mapgen.h"
21 #include "voxel.h"
22 #include "noise.h"
23 #include "mapblock.h"
24 #include "mapnode.h"
25 #include "map.h"
26 //#include "serverobject.h"
27 #include "content_sao.h"
28 #include "nodedef.h"
29 #include "content_mapnode.h" // For content_mapnode_get_new_name
30 #include "voxelalgorithms.h"
31 #include "profiler.h"
32 #include "settings.h" // For g_settings
33 #include "main.h" // For g_profiler
34 #include "emerge.h"
35 #include "mapgen_v6.h"
36
37 /////////////////// Mapgen V6 perlin noise default values
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), 85039, 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};
56
57
58 ///////////////////////////////////////////////////////////////////////////////
59
60
61 MapgenV6::MapgenV6(int mapgenid, MapgenV6Params *params) {
62         this->generating  = false;
63         this->id       = mapgenid;
64
65         this->seed     = (int)params->seed;
66         this->water_level = params->water_level;
67         this->flags   = params->flags;
68         this->csize   = v3s16(1, 1, 1) * params->chunksize * MAP_BLOCKSIZE;
69
70         this->freq_desert = params->freq_desert;
71         this->freq_beach  = params->freq_beach;
72
73         this->ystride = csize.X; //////fix this
74
75         np_cave = params->np_cave;
76
77         noise_terrain_base   = new Noise(params->np_terrain_base,   seed, csize.X, csize.Y);
78         noise_terrain_higher = new Noise(params->np_terrain_higher, seed, csize.X, csize.Y);
79         noise_steepness      = new Noise(params->np_steepness,      seed, csize.X, csize.Y);
80         noise_height_select  = new Noise(params->np_height_select,  seed, csize.X, csize.Y);
81         noise_trees          = new Noise(params->np_trees,          seed, csize.X, csize.Y);
82         noise_mud            = new Noise(params->np_mud,            seed, csize.X, csize.Y);
83         noise_beach          = new Noise(params->np_beach,          seed, csize.X, csize.Y);
84         noise_biome          = new Noise(params->np_biome,          seed, csize.X, csize.Y);
85
86         map_terrain_base   = noise_terrain_base->result;
87         map_terrain_higher = noise_terrain_higher->result;
88         map_steepness      = noise_steepness->result;
89         map_height_select  = noise_height_select->result;
90         map_trees          = noise_trees->result;
91         map_mud            = noise_mud->result;
92         map_beach          = noise_beach->result;
93         map_biome          = noise_biome->result;
94 }
95
96
97 MapgenV6::~MapgenV6() {
98         delete noise_terrain_base;
99         delete noise_terrain_higher;
100         delete noise_steepness;
101         delete noise_height_select;
102         delete noise_trees;
103         delete noise_mud;
104         delete noise_beach;
105         delete noise_biome;
106 }
107
108
109 /*
110         Some helper functions for the map generator
111 */
112
113 #if 1
114 // Returns Y one under area minimum if not found
115 s16 MapgenV6::find_ground_level(VoxelManipulator &vmanip, v2s16 p2d,
116                 INodeDefManager *ndef)
117 {
118         v3s16 em = vmanip.m_area.getExtent();
119         s16 y_nodes_max = vmanip.m_area.MaxEdge.Y;
120         s16 y_nodes_min = vmanip.m_area.MinEdge.Y;
121         u32 i = vmanip.m_area.index(v3s16(p2d.X, y_nodes_max, p2d.Y));
122         s16 y;
123         for(y=y_nodes_max; y>=y_nodes_min; y--)
124         {
125                 MapNode &n = vmanip.m_data[i];
126                 if(ndef->get(n).walkable)
127                         break;
128
129                 vmanip.m_area.add_y(em, i, -1);
130         }
131         if(y >= y_nodes_min)
132                 return y;
133         else
134                 return y_nodes_min - 1;
135 }
136
137 // Returns Y one under area minimum if not found
138 s16 MapgenV6::find_stone_level(VoxelManipulator &vmanip, v2s16 p2d,
139                 INodeDefManager *ndef)
140 {
141         v3s16 em = vmanip.m_area.getExtent();
142         s16 y_nodes_max = vmanip.m_area.MaxEdge.Y;
143         s16 y_nodes_min = vmanip.m_area.MinEdge.Y;
144         u32 i = vmanip.m_area.index(v3s16(p2d.X, y_nodes_max, p2d.Y));
145         s16 y;
146         content_t c_stone = ndef->getId("mapgen_stone");
147         content_t c_desert_stone = ndef->getId("mapgen_desert_stone");
148         for(y=y_nodes_max; y>=y_nodes_min; y--)
149         {
150                 MapNode &n = vmanip.m_data[i];
151                 content_t c = n.getContent();
152                 if(c != CONTENT_IGNORE && (
153                                 c == c_stone || c == c_desert_stone))
154                         break;
155
156                 vmanip.m_area.add_y(em, i, -1);
157         }
158         if(y >= y_nodes_min)
159                 return y;
160         else
161                 return y_nodes_min - 1;
162 }
163 #endif
164
165 void MapgenV6::make_tree(ManualMapVoxelManipulator &vmanip, v3s16 p0,
166                 bool is_apple_tree, INodeDefManager *ndef)
167 {
168         MapNode treenode(ndef->getId("mapgen_tree"));
169         MapNode leavesnode(ndef->getId("mapgen_leaves"));
170         MapNode applenode(ndef->getId("mapgen_apple"));
171
172         s16 trunk_h = myrand_range(4, 5);
173         v3s16 p1 = p0;
174         for(s16 ii=0; ii<trunk_h; ii++)
175         {
176                 if(vmanip.m_area.contains(p1))
177                         vmanip.m_data[vmanip.m_area.index(p1)] = treenode;
178                 p1.Y++;
179         }
180
181         // p1 is now the last piece of the trunk
182         p1.Y -= 1;
183
184         VoxelArea leaves_a(v3s16(-2,-1,-2), v3s16(2,2,2));
185         //SharedPtr<u8> leaves_d(new u8[leaves_a.getVolume()]);
186         Buffer<u8> leaves_d(leaves_a.getVolume());
187         for(s32 i=0; i<leaves_a.getVolume(); i++)
188                 leaves_d[i] = 0;
189
190         // Force leaves at near the end of the trunk
191         {
192                 s16 d = 1;
193                 for(s16 z=-d; z<=d; z++)
194                 for(s16 y=-d; y<=d; y++)
195                 for(s16 x=-d; x<=d; x++)
196                 {
197                         leaves_d[leaves_a.index(v3s16(x,y,z))] = 1;
198                 }
199         }
200
201         // Add leaves randomly
202         for(u32 iii=0; iii<7; iii++)
203         {
204                 s16 d = 1;
205
206                 v3s16 p(
207                         myrand_range(leaves_a.MinEdge.X, leaves_a.MaxEdge.X-d),
208                         myrand_range(leaves_a.MinEdge.Y, leaves_a.MaxEdge.Y-d),
209                         myrand_range(leaves_a.MinEdge.Z, leaves_a.MaxEdge.Z-d)
210                 );
211
212                 for(s16 z=0; z<=d; z++)
213                 for(s16 y=0; y<=d; y++)
214                 for(s16 x=0; x<=d; x++)
215                 {
216                         leaves_d[leaves_a.index(p+v3s16(x,y,z))] = 1;
217                 }
218         }
219
220         // Blit leaves to vmanip
221         for(s16 z=leaves_a.MinEdge.Z; z<=leaves_a.MaxEdge.Z; z++)
222         for(s16 y=leaves_a.MinEdge.Y; y<=leaves_a.MaxEdge.Y; y++)
223         for(s16 x=leaves_a.MinEdge.X; x<=leaves_a.MaxEdge.X; x++)
224         {
225                 v3s16 p(x,y,z);
226                 p += p1;
227                 if(vmanip.m_area.contains(p) == false)
228                         continue;
229                 u32 vi = vmanip.m_area.index(p);
230                 if(vmanip.m_data[vi].getContent() != CONTENT_AIR
231                                 && vmanip.m_data[vi].getContent() != CONTENT_IGNORE)
232                         continue;
233                 u32 i = leaves_a.index(x,y,z);
234                 if(leaves_d[i] == 1) {
235                         bool is_apple = myrand_range(0,99) < 10;
236                         if(is_apple_tree && is_apple) {
237                                 vmanip.m_data[vi] = applenode;
238                         } else {
239                                 vmanip.m_data[vi] = leavesnode;
240                         }
241                 }
242         }
243 }
244
245
246 /*
247         Noise functions. Make sure seed is mangled differently in each one.
248 */
249
250
251 // Amount of trees per area in nodes
252 double MapgenV6::tree_amount_2d(u64 seed, v2s16 p)
253 {
254         /*double noise = noise2d_perlin(
255                         0.5+(float)p.X/125, 0.5+(float)p.Y/125,
256                         seed+2, 4, 0.66);*/
257         double noise = map_trees[(p.Y - node_min.Z) * ystride + (p.X - node_min.X)];
258         double zeroval = -0.39;
259         if(noise < zeroval)
260                 return 0;
261         else
262                 return 0.04 * (noise-zeroval) / (1.0-zeroval);
263 }
264
265 // Required by mapgen.h
266 bool MapgenV6::block_is_underground(u64 seed, v3s16 blockpos)
267 {
268         /*s16 minimum_groundlevel = (s16)get_sector_minimum_ground_level(
269                         seed, v2s16(blockpos.X, blockpos.Z));*/
270         // Nah, this is just a heuristic, just return something
271         s16 minimum_groundlevel = water_level;
272
273         if(blockpos.Y*MAP_BLOCKSIZE + MAP_BLOCKSIZE <= minimum_groundlevel)
274                 return true;
275         else
276                 return false;
277 }
278
279
280 double MapgenV6::base_rock_level_2d(u64 seed, v2s16 p)
281 {
282         if (flags & MG_FLAT)
283                 return water_level;
284         
285         int index = (p.Y - node_min.Z) * ystride + (p.X - node_min.X);
286
287         // The base ground level
288         /*double base = (double)WATER_LEVEL - (double)AVERAGE_MUD_AMOUNT
289                         + 20. * noise2d_perlin(
290                         0.5+(float)p.X/250., 0.5+(float)p.Y/250.,
291                         seed+82341, 5, 0.6);*/
292         double base = water_level + map_terrain_base[index];
293
294         // Higher ground level
295         /*double higher = (double)WATER_LEVEL + 20. + 16. * noise2d_perlin(
296                         0.5+(float)p.X/500., 0.5+(float)p.Y/500.,
297                         seed+85039, 5, 0.6);*/
298         double higher = water_level + map_terrain_higher[index];
299
300         // Limit higher to at least base
301         if(higher < base)
302                 higher = base;
303
304         // Steepness factor of cliffs
305         /*double b = 0.85 + 0.5 * noise2d_perlin(
306                         0.5+(float)p.X/125., 0.5+(float)p.Y/125.,
307                         seed-932, 5, 0.7);*/
308         double b = map_steepness[index];
309         b = rangelim(b, 0.0, 1000.0);
310         b = pow(b, 7);
311         b *= 5;
312         b = rangelim(b, 0.5, 1000.0);
313
314         // Values 1.5...100 give quite horrible looking slopes
315         if(b > 1.5 && b < 100.0){
316                 if(b < 10.0)
317                         b = 1.5;
318                 else
319                         b = 100.0;
320         }
321
322         // Offset to more low
323         double a_off = -0.20;
324
325         // High/low selector
326         /*double a = (double)0.5 + b * (a_off + noise2d_perlin(
327                         0.5+(float)p.X/250., 0.5+(float)p.Y/250.,
328                         seed+4213, 5, 0.69));*/
329         double a = 0.5 + b * (a_off + map_height_select[index]);
330
331         // Limit
332         a = rangelim(a, 0.0, 1.0);
333
334         double h = base*(1.0-a) + higher*a;
335
336         return h;
337 }
338
339 double MapgenV6::baseRockLevelFromNoise(v2s16 p) {
340         if (flags & MG_FLAT)
341                 return water_level;
342         
343         double base = water_level + 
344                 NoisePerlin2DPosOffset(noise_terrain_base->np, p.X, 0.5, p.Y, 0.5, seed);
345         double higher = water_level +
346                 NoisePerlin2DPosOffset(noise_terrain_higher->np, p.X, 0.5, p.Y, 0.5, seed);
347
348         if (higher < base)
349                 higher = base;
350
351         double b = NoisePerlin2DPosOffset(noise_steepness->np, p.X, 0.5, p.Y, 0.5, seed);
352         b = rangelim(b, 0.0, 1000.0);
353         b = b*b*b*b*b*b*b;
354         b *= 5;
355         b = rangelim(b, 0.5, 1000.0);
356
357         if(b > 1.5 && b < 100.0){
358                 if(b < 10.0)
359                         b = 1.5;
360                 else
361                         b = 100.0;
362         }
363         
364         double a_off = -0.20;
365         double a = 0.5 + b * (a_off + NoisePerlin2DNoTxfmPosOffset(
366                         noise_height_select->np, p.X, 0.5, p.Y, 0.5, seed));
367         a = rangelim(a, 0.0, 1.0);
368
369         return base * (1.0 - a) + higher * a;
370 }
371
372
373 s16 MapgenV6::find_ground_level_from_noise(u64 seed, v2s16 p2d, s16 precision)
374 {
375         return baseRockLevelFromNoise(p2d) + AVERAGE_MUD_AMOUNT;
376 }
377
378 double MapgenV6::get_mud_add_amount(u64 seed, v2s16 p)
379 {
380         if (flags & MG_FLAT)
381                 return AVERAGE_MUD_AMOUNT;
382                 
383         /*return ((float)AVERAGE_MUD_AMOUNT + 2.0 * noise2d_perlin(
384                         0.5+(float)p.X/200, 0.5+(float)p.Y/200,
385                         seed+91013, 3, 0.55));*/
386         int index = (p.Y - node_min.Z) * ystride + (p.X - node_min.X);
387         return map_mud[index];
388 }
389
390 bool MapgenV6::get_have_beach(u64 seed, v2s16 p2d)
391 {
392         // Determine whether to have sand here
393         /*double sandnoise = noise2d_perlin(
394                         0.2+(float)p2d.X/250, 0.7+(float)p2d.Y/250,
395                         seed+59420, 3, 0.50);*/
396         int index = (p2d.Y - node_min.Z) * ystride + (p2d.X - node_min.X);
397         double sandnoise = map_beach[index];
398
399         return (sandnoise > freq_beach);
400 }
401
402 BiomeType MapgenV6::get_biome(u64 seed, v2s16 p2d)
403 {
404         // Just do something very simple as for now
405         /*double d = noise2d_perlin(
406                         0.6+(float)p2d.X/250, 0.2+(float)p2d.Y/250,
407                         seed+9130, 3, 0.50);*/
408         int index = (p2d.Y - node_min.Z) * ystride + (p2d.X - node_min.X);
409         double d = map_biome[index];
410         if(d > freq_desert)
411                 return BT_DESERT;
412         if (flags & MGV6_BIOME_BLEND) {
413                 if(d > freq_desert - 0.10 &&
414                          (noise2d(p2d.X, p2d.Y, seed) + 1.0) > (freq_desert - d) * 20.0)
415                         return BT_DESERT;
416         }
417         return BT_NORMAL;
418 };
419
420 u32 MapgenV6::get_blockseed(u64 seed, v3s16 p)
421 {
422         s32 x=p.X, y=p.Y, z=p.Z;
423         return (u32)(seed%0x100000000ULL) + z*38134234 + y*42123 + x*23;
424 }
425
426
427 int MapgenV6::getGroundLevelAtPoint(v2s16 p) {
428         return baseRockLevelFromNoise(p) + AVERAGE_MUD_AMOUNT;
429 }
430
431 #define VMANIP_FLAG_CAVE VOXELFLAG_CHECKED1
432
433 void MapgenV6::makeChunk(BlockMakeData *data)
434 {
435         this->generating = true;
436
437         assert(data->vmanip);
438         assert(data->nodedef);
439         assert(data->blockpos_requested.X >= data->blockpos_min.X &&
440                         data->blockpos_requested.Y >= data->blockpos_min.Y &&
441                         data->blockpos_requested.Z >= data->blockpos_min.Z);
442         assert(data->blockpos_requested.X <= data->blockpos_max.X &&
443                         data->blockpos_requested.Y <= data->blockpos_max.Y &&
444                         data->blockpos_requested.Z <= data->blockpos_max.Z);
445
446         INodeDefManager *ndef = data->nodedef;
447
448         // Hack: use minimum block coordinates for old code that assumes
449         // a single block
450         v3s16 blockpos = data->blockpos_requested;
451
452         /*dstream<<"makeBlock(): ("<<blockpos.X<<","<<blockpos.Y<<","
453                         <<blockpos.Z<<")"<<std::endl;*/
454
455         v3s16 blockpos_min = data->blockpos_min;
456         v3s16 blockpos_max = data->blockpos_max;
457         v3s16 blockpos_full_min = blockpos_min - v3s16(1,1,1);
458         v3s16 blockpos_full_max = blockpos_max + v3s16(1,1,1);
459
460         ManualMapVoxelManipulator &vmanip = *(data->vmanip);
461         // Area of central chunk
462         node_min = blockpos_min*MAP_BLOCKSIZE;
463         node_max = (blockpos_max+v3s16(1,1,1))*MAP_BLOCKSIZE-v3s16(1,1,1);
464         // Full allocated area
465         v3s16 full_node_min = (blockpos_min-1)*MAP_BLOCKSIZE;
466         v3s16 full_node_max = (blockpos_max+2)*MAP_BLOCKSIZE-v3s16(1,1,1);
467
468         v3s16 central_area_size = node_max - node_min + v3s16(1,1,1);
469
470         const s16 max_spread_amount = MAP_BLOCKSIZE;
471
472         int volume_blocks = (blockpos_max.X - blockpos_min.X + 1)
473                         * (blockpos_max.Y - blockpos_min.Y + 1)
474                         * (blockpos_max.Z - blockpos_max.Z + 1);
475
476         int volume_nodes = volume_blocks *
477                         MAP_BLOCKSIZE*MAP_BLOCKSIZE*MAP_BLOCKSIZE;
478
479         // Generated surface area
480         //double gen_area_nodes = MAP_BLOCKSIZE*MAP_BLOCKSIZE * rel_volume;
481
482         // Horribly wrong heuristic, but better than nothing
483         bool block_is_underground = (water_level > node_max.Y);
484
485         /*
486                 Create a block-specific seed
487         */
488         u32 blockseed = get_blockseed(data->seed, full_node_min);
489
490         /*
491                 Make some noise
492         */
493         {
494                 int x = node_min.X;
495                 int z = node_min.Z;
496
497                 // Need to adjust for the original implementation's +.5 offset...
498                 if (!(flags & MG_FLAT)) {
499                         noise_terrain_base->perlinMap2D(
500                                 x + 0.5 * noise_terrain_base->np->spread.X,
501                                 z + 0.5 * noise_terrain_base->np->spread.Z);
502                         noise_terrain_base->transformNoiseMap();
503
504                         noise_terrain_higher->perlinMap2D(
505                                 x + 0.5 * noise_terrain_higher->np->spread.X,
506                                 z + 0.5 * noise_terrain_higher->np->spread.Z);
507                         noise_terrain_higher->transformNoiseMap();
508
509                         noise_steepness->perlinMap2D(
510                                 x + 0.5 * noise_steepness->np->spread.X,
511                                 z + 0.5 * noise_steepness->np->spread.Z);
512                         noise_steepness->transformNoiseMap();
513
514                         noise_height_select->perlinMap2D(
515                                 x + 0.5 * noise_height_select->np->spread.X,
516                                 z + 0.5 * noise_height_select->np->spread.Z);
517                 }
518                 
519                 noise_trees->perlinMap2D(
520                         x + 0.5 * noise_trees->np->spread.X,
521                         z + 0.5 * noise_trees->np->spread.Z);
522                         
523                 if (!(flags & MG_FLAT)) {
524                         noise_mud->perlinMap2D(
525                                 x + 0.5 * noise_mud->np->spread.X,
526                                 z + 0.5 * noise_mud->np->spread.Z);
527                         noise_mud->transformNoiseMap();
528                 }
529                 noise_beach->perlinMap2D(
530                         x + 0.2 * noise_beach->np->spread.X,
531                         z + 0.7 * noise_beach->np->spread.Z);
532
533                 noise_biome->perlinMap2D(
534                         x + 0.6 * noise_biome->np->spread.X,
535                         z + 0.2 * noise_biome->np->spread.Z);
536         }
537
538
539         /*
540                 Cache some ground type values for speed
541         */
542
543 // Creates variables c_name=id and n_name=node
544 #define CONTENT_VARIABLE(ndef, name)\
545         content_t c_##name = ndef->getId("mapgen_" #name);\
546         MapNode n_##name(c_##name);
547 // Default to something else if was CONTENT_IGNORE
548 #define CONTENT_VARIABLE_FALLBACK(name, dname)\
549         if(c_##name == CONTENT_IGNORE){\
550                 c_##name = c_##dname;\
551                 n_##name = n_##dname;\
552         }
553
554         CONTENT_VARIABLE(ndef, stone);
555         CONTENT_VARIABLE(ndef, air);
556         CONTENT_VARIABLE(ndef, water_source);
557         CONTENT_VARIABLE(ndef, dirt);
558         CONTENT_VARIABLE(ndef, sand);
559         CONTENT_VARIABLE(ndef, gravel);
560         CONTENT_VARIABLE(ndef, clay);
561         CONTENT_VARIABLE(ndef, lava_source);
562         CONTENT_VARIABLE(ndef, cobble);
563         CONTENT_VARIABLE(ndef, mossycobble);
564         CONTENT_VARIABLE(ndef, dirt_with_grass);
565         CONTENT_VARIABLE(ndef, junglegrass);
566         CONTENT_VARIABLE(ndef, stone_with_coal);
567         CONTENT_VARIABLE(ndef, stone_with_iron);
568         CONTENT_VARIABLE(ndef, mese);
569         CONTENT_VARIABLE(ndef, desert_sand);
570         CONTENT_VARIABLE_FALLBACK(desert_sand, sand);
571         CONTENT_VARIABLE(ndef, desert_stone);
572         CONTENT_VARIABLE_FALLBACK(desert_stone, stone);
573
574         // Maximum height of the stone surface and obstacles.
575         // This is used to guide the cave generation
576         s16 stone_surface_max_y = 0;
577
578         /*
579                 Generate general ground level to full area
580         */
581         {
582 #if 1
583         TimeTaker timer1("Generating ground level");
584
585         for(s16 x=node_min.X; x<=node_max.X; x++)
586         for(s16 z=node_min.Z; z<=node_max.Z; z++)
587         {
588                 // Node position
589                 v2s16 p2d = v2s16(x,z);
590
591                 /*
592                         Skip of already generated
593                 */
594                 /*{
595                         v3s16 p(p2d.X, node_min.Y, p2d.Y);
596                         if(vmanip.m_data[vmanip.m_area.index(p)].d != CONTENT_AIR)
597                                 continue;
598                 }*/
599
600                 // Ground height at this point
601                 float surface_y_f = 0.0;
602
603                 // Use perlin noise for ground height
604                 surface_y_f = base_rock_level_2d(data->seed, p2d);
605
606                 /*// Experimental stuff
607                 {
608                         float a = highlands_level_2d(data->seed, p2d);
609                         if(a > surface_y_f)
610                                 surface_y_f = a;
611                 }*/
612
613                 // Convert to integer
614                 s16 surface_y = (s16)surface_y_f;
615
616                 // Log it
617                 if(surface_y > stone_surface_max_y)
618                         stone_surface_max_y = surface_y;
619
620                 BiomeType bt = get_biome(data->seed, p2d);
621                 /*
622                         Fill ground with stone
623                 */
624                 {
625                         // Use fast index incrementing
626                         v3s16 em = vmanip.m_area.getExtent();
627                         u32 i = vmanip.m_area.index(v3s16(p2d.X, node_min.Y, p2d.Y));
628                         for(s16 y=node_min.Y; y<=node_max.Y; y++)
629                         {
630                                 if(vmanip.m_data[i].getContent() == CONTENT_IGNORE){
631                                         if(y <= surface_y){
632                                                 if(y > water_level && bt == BT_DESERT)
633                                                         vmanip.m_data[i] = n_desert_stone;
634                                                 else
635                                                         vmanip.m_data[i] = n_stone;
636                                         } else if(y <= water_level){
637                                                 vmanip.m_data[i] = MapNode(c_water_source);
638                                         } else {
639                                                 vmanip.m_data[i] = MapNode(c_air);
640                                         }
641                                 }
642                                 vmanip.m_area.add_y(em, i, 1);
643                         }
644                 }
645         }
646 #endif
647
648         }//timer1
649
650         // Limit dirt flow area by 1 because mud is flown into neighbors.
651         assert(central_area_size.X == central_area_size.Z);
652         s16 mudflow_minpos = 0-max_spread_amount+1;
653         s16 mudflow_maxpos = central_area_size.X+max_spread_amount-2;
654
655         /*
656                 Loop this part, it will make stuff look older and newer nicely
657         */
658
659         /*double cave_amount = 6.0 + 6.0 * noise2d_perlin(
660                         0.5+(double)node_min.X/250, 0.5+(double)node_min.Y/250,
661                         data->seed+34329, 3, 0.50);*/
662
663         double cave_amount = NoisePerlin2D(np_cave, node_min.X, node_min.Y, data->seed);
664
665         const u32 age_loops = 2;
666         for(u32 i_age=0; i_age<age_loops; i_age++)
667         { // Aging loop
668         /******************************
669                 BEGINNING OF AGING LOOP
670         ******************************/
671
672 #if 1
673         {
674         // 24ms @cs=8
675         //TimeTaker timer1("caves");
676
677         /*
678                 Make caves (this code is relatively horrible)
679         */
680         cave_amount = MYMAX(0.0, cave_amount);
681         u32 caves_count = cave_amount * volume_nodes / 50000;
682         u32 bruises_count = 1;
683         PseudoRandom ps(blockseed+21343);
684         PseudoRandom ps2(blockseed+1032);
685         if(ps.range(1, 6) == 1)
686                 bruises_count = ps.range(0, ps.range(0, 2));
687         if(get_biome(data->seed, v2s16(node_min.X, node_min.Z)) == BT_DESERT){
688                 caves_count /= 3;
689                 bruises_count /= 3;
690         }
691         for(u32 jj=0; jj<caves_count+bruises_count; jj++)
692         {
693                 if (!(flags & MG_CAVES))
694                         continue;
695
696                 /*int avg_height = (int)
697                           ((base_rock_level_2d(data->seed, v2s16(node_min.X, node_min.Z)) +
698                                 base_rock_level_2d(data->seed, v2s16(node_max.X, node_max.Z))) / 2);
699                 if ((node_max.Y + node_min.Y) / 2 > avg_height)
700                         break;*/
701
702                 bool large_cave = (jj >= caves_count);
703                 s16 min_tunnel_diameter = 2;
704                 s16 max_tunnel_diameter = ps.range(2,6);
705                 int dswitchint = ps.range(1,14);
706                 u16 tunnel_routepoints = 0;
707                 int part_max_length_rs = 0;
708                 if(large_cave){
709                         part_max_length_rs = ps.range(2,4);
710                         tunnel_routepoints = ps.range(5, ps.range(15,30));
711                         min_tunnel_diameter = 5;
712                         max_tunnel_diameter = ps.range(7, ps.range(8,24));
713                 } else {
714                         part_max_length_rs = ps.range(2,9);
715                         tunnel_routepoints = ps.range(10, ps.range(15,30));
716                 }
717                 bool large_cave_is_flat = (ps.range(0,1) == 0);
718
719                 v3f main_direction(0,0,0);
720
721                 // Allowed route area size in nodes
722                 v3s16 ar = central_area_size;
723
724                 // Area starting point in nodes
725                 v3s16 of = node_min;
726
727                 // Allow a bit more
728                 //(this should be more than the maximum radius of the tunnel)
729                 s16 insure = 10;
730                 s16 more = max_spread_amount - max_tunnel_diameter/2 - insure;
731                 ar += v3s16(1,0,1) * more * 2;
732                 of -= v3s16(1,0,1) * more;
733
734                 s16 route_y_min = 0;
735                 // Allow half a diameter + 7 over stone surface
736                 s16 route_y_max = -of.Y + stone_surface_max_y + max_tunnel_diameter/2 + 7;
737
738                 // Limit maximum to area
739                 route_y_max = rangelim(route_y_max, 0, ar.Y-1);
740
741                 if(large_cave)
742                 {
743                         s16 min = 0;
744                         if(node_min.Y < water_level && node_max.Y > water_level)
745                         {
746                                 min = water_level - max_tunnel_diameter/3 - of.Y;
747                                 route_y_max = water_level + max_tunnel_diameter/3 - of.Y;
748                         }
749                         route_y_min = ps.range(min, min + max_tunnel_diameter);
750                         route_y_min = rangelim(route_y_min, 0, route_y_max);
751                 }
752
753                 s16 route_start_y_min = route_y_min;
754                 s16 route_start_y_max = route_y_max;
755
756                 route_start_y_min = rangelim(route_start_y_min, 0, ar.Y-1);
757                 route_start_y_max = rangelim(route_start_y_max, route_start_y_min, ar.Y-1);
758
759                 // Randomize starting position
760                 v3f orp(
761                         (float)(ps.next()%ar.X)+0.5,
762                         (float)(ps.range(route_start_y_min, route_start_y_max))+0.5,
763                         (float)(ps.next()%ar.Z)+0.5
764                 );
765
766                 v3s16 startp(orp.X, orp.Y, orp.Z);
767                 startp += of;
768
769                 MapNode airnode(CONTENT_AIR);
770                 MapNode waternode(c_water_source);
771                 MapNode lavanode(c_lava_source);
772
773                 /*
774                         Generate some tunnel starting from orp
775                 */
776
777                 for(u16 j=0; j<tunnel_routepoints; j++)
778                 {
779                         if(j%dswitchint==0 && large_cave == false)
780                         {
781                                 main_direction = v3f(
782                                         ((float)(ps.next()%20)-(float)10)/10,
783                                         ((float)(ps.next()%20)-(float)10)/30,
784                                         ((float)(ps.next()%20)-(float)10)/10
785                                 );
786                                 main_direction *= (float)ps.range(0, 10)/10;
787                         }
788
789                         // Randomize size
790                         s16 min_d = min_tunnel_diameter;
791                         s16 max_d = max_tunnel_diameter;
792                         s16 rs = ps.range(min_d, max_d);
793
794                         // Every second section is rough
795                         bool randomize_xz = (ps2.range(1,2) == 1);
796
797                         v3s16 maxlen;
798                         if(large_cave)
799                         {
800                                 maxlen = v3s16(
801                                         rs*part_max_length_rs,
802                                         rs*part_max_length_rs/2,
803                                         rs*part_max_length_rs
804                                 );
805                         }
806                         else
807                         {
808                                 maxlen = v3s16(
809                                         rs*part_max_length_rs,
810                                         ps.range(1, rs*part_max_length_rs),
811                                         rs*part_max_length_rs
812                                 );
813                         }
814
815                         v3f vec;
816
817                         vec = v3f(
818                                 (float)(ps.next()%(maxlen.X*1))-(float)maxlen.X/2,
819                                 (float)(ps.next()%(maxlen.Y*1))-(float)maxlen.Y/2,
820                                 (float)(ps.next()%(maxlen.Z*1))-(float)maxlen.Z/2
821                         );
822
823                         // Jump downward sometimes
824                         if(!large_cave && ps.range(0,12) == 0)
825                         {
826                                 vec = v3f(
827                                         (float)(ps.next()%(maxlen.X*1))-(float)maxlen.X/2,
828                                         (float)(ps.next()%(maxlen.Y*2))-(float)maxlen.Y*2/2,
829                                         (float)(ps.next()%(maxlen.Z*1))-(float)maxlen.Z/2
830                                 );
831                         }
832
833                         /*if(large_cave){
834                                 v3f p = orp + vec;
835                                 s16 h = find_ground_level_clever(vmanip,
836                                                 v2s16(p.X, p.Z), ndef);
837                                 route_y_min = h - rs/3;
838                                 route_y_max = h + rs;
839                         }*/
840
841                         vec += main_direction;
842
843                         v3f rp = orp + vec;
844                         if(rp.X < 0)
845                                 rp.X = 0;
846                         else if(rp.X >= ar.X)
847                                 rp.X = ar.X-1;
848                         if(rp.Y < route_y_min)
849                                 rp.Y = route_y_min;
850                         else if(rp.Y >= route_y_max)
851                                 rp.Y = route_y_max-1;
852                         if(rp.Z < 0)
853                                 rp.Z = 0;
854                         else if(rp.Z >= ar.Z)
855                                 rp.Z = ar.Z-1;
856                         vec = rp - orp;
857
858                         for(float f=0; f<1.0; f+=1.0/vec.getLength())
859                         {
860                                 v3f fp = orp + vec * f;
861                                 fp.X += 0.1*ps.range(-10,10);
862                                 fp.Z += 0.1*ps.range(-10,10);
863                                 v3s16 cp(fp.X, fp.Y, fp.Z);
864
865                                 s16 d0 = -rs/2;
866                                 s16 d1 = d0 + rs;
867                                 if(randomize_xz){
868                                         d0 += ps.range(-1,1);
869                                         d1 += ps.range(-1,1);
870                                 }
871                                 for(s16 z0=d0; z0<=d1; z0++)
872                                 {
873                                         s16 si = rs/2 - MYMAX(0, abs(z0)-rs/7-1);
874                                         for(s16 x0=-si-ps.range(0,1); x0<=si-1+ps.range(0,1); x0++)
875                                         {
876                                                 s16 maxabsxz = MYMAX(abs(x0), abs(z0));
877                                                 s16 si2 = rs/2 - MYMAX(0, maxabsxz-rs/7-1);
878                                                 for(s16 y0=-si2; y0<=si2; y0++)
879                                                 {
880                                                         /*// Make better floors in small caves
881                                                         if(y0 <= -rs/2 && rs<=7)
882                                                                 continue;*/
883                                                         if(large_cave_is_flat){
884                                                                 // Make large caves not so tall
885                                                                 if(rs > 7 && abs(y0) >= rs/3)
886                                                                         continue;
887                                                         }
888
889                                                         s16 z = cp.Z + z0;
890                                                         s16 y = cp.Y + y0;
891                                                         s16 x = cp.X + x0;
892                                                         v3s16 p(x,y,z);
893                                                         p += of;
894
895                                                         if(vmanip.m_area.contains(p) == false)
896                                                                 continue;
897
898                                                         u32 i = vmanip.m_area.index(p);
899
900                                                         if(large_cave)
901                                                         {
902                                                                 if(full_node_min.Y < water_level &&
903                                                                         full_node_max.Y > water_level){
904                                                                         if(p.Y <= water_level)
905                                                                                 vmanip.m_data[i] = waternode;
906                                                                         else
907                                                                                 vmanip.m_data[i] = airnode;
908                                                                 } else if(full_node_max.Y < water_level){
909                                                                         if(p.Y < startp.Y - 2)
910                                                                                 vmanip.m_data[i] = lavanode;
911                                                                         else
912                                                                                 vmanip.m_data[i] = airnode;
913                                                                 } else {
914                                                                         vmanip.m_data[i] = airnode;
915                                                                 }
916                                                         } else {
917                                                                 // Don't replace air or water or lava or ignore
918                                                                 if(vmanip.m_data[i].getContent() == CONTENT_IGNORE ||
919                                                                 vmanip.m_data[i].getContent() == CONTENT_AIR ||
920                                                                 vmanip.m_data[i].getContent() == c_water_source ||
921                                                                 vmanip.m_data[i].getContent() == c_lava_source)
922                                                                         continue;
923
924                                                                 vmanip.m_data[i] = airnode;
925
926                                                                 // Set tunnel flag
927                                                                 vmanip.m_flags[i] |= VMANIP_FLAG_CAVE;
928                                                         }
929                                                 }
930                                         }
931                                 }
932                         }
933
934                         orp = rp;
935                 }
936
937         }
938
939         }//timer1
940 #endif
941
942 #if 1
943         {
944         // 15ms @cs=8
945         TimeTaker timer1("add mud");
946
947         /*
948                 Add mud to the central chunk
949         */
950
951         for(s16 x=node_min.X; x<=node_max.X; x++)
952         for(s16 z=node_min.Z; z<=node_max.Z; z++)
953         {
954                 // Node position in 2d
955                 v2s16 p2d = v2s16(x,z);
956
957                 // Randomize mud amount
958                 s16 mud_add_amount = get_mud_add_amount(data->seed, p2d) / 2.0 + 0.5;
959
960                 // Find ground level
961                 s16 surface_y = find_stone_level(vmanip, p2d, ndef);
962                 // Handle area not found
963                 if(surface_y == vmanip.m_area.MinEdge.Y - 1)
964                         continue;
965
966                 MapNode addnode(c_dirt);
967                 BiomeType bt = get_biome(data->seed, p2d);
968
969                 if(bt == BT_DESERT)
970                         addnode = MapNode(c_desert_sand);
971
972                 if(bt == BT_DESERT && surface_y + mud_add_amount <= water_level+1){
973                         addnode = MapNode(c_sand);
974                 } else if(mud_add_amount <= 0){
975                         mud_add_amount = 1 - mud_add_amount;
976                         addnode = MapNode(c_gravel);
977                 } else if(bt == BT_NORMAL && get_have_beach(data->seed, p2d) &&
978                                 surface_y + mud_add_amount <= water_level+2){
979                         addnode = MapNode(c_sand);
980                 }
981
982                 if(bt == BT_DESERT){
983                         if(surface_y > 20){
984                                 mud_add_amount = MYMAX(0, mud_add_amount - (surface_y - 20)/5);
985                         }
986                 }
987
988                 /*
989                         If topmost node is grass, change it to mud.
990                         It might be if it was flown to there from a neighboring
991                         chunk and then converted.
992                 */
993                 {
994                         u32 i = vmanip.m_area.index(v3s16(p2d.X, surface_y, p2d.Y));
995                         MapNode *n = &vmanip.m_data[i];
996                         if(n->getContent() == c_dirt_with_grass)
997                                 *n = MapNode(c_dirt);
998                 }
999
1000                 /*
1001                         Add mud on ground
1002                 */
1003                 {
1004                         s16 mudcount = 0;
1005                         v3s16 em = vmanip.m_area.getExtent();
1006                         s16 y_start = surface_y+1;
1007                         u32 i = vmanip.m_area.index(v3s16(p2d.X, y_start, p2d.Y));
1008                         for(s16 y=y_start; y<=node_max.Y; y++)
1009                         {
1010                                 if(mudcount >= mud_add_amount)
1011                                         break;
1012
1013                                 MapNode &n = vmanip.m_data[i];
1014                                 n = addnode;
1015                                 mudcount++;
1016
1017                                 vmanip.m_area.add_y(em, i, 1);
1018                         }
1019                 }
1020
1021         }
1022
1023         }//timer1
1024 #endif
1025
1026         /*
1027                 Add blobs of dirt and gravel underground
1028         */
1029         if(get_biome(data->seed, v2s16(node_min.X, node_min.Z)) == BT_NORMAL)
1030         {
1031         PseudoRandom pr(blockseed+983);
1032         for(int i=0; i<volume_nodes/10/10/10; i++)
1033         {
1034                 bool only_fill_cave = (myrand_range(0,1) != 0);
1035                 v3s16 size(
1036                         pr.range(1, 8),
1037                         pr.range(1, 8),
1038                         pr.range(1, 8)
1039                 );
1040                 v3s16 p0(
1041                         pr.range(node_min.X, node_max.X)-size.X/2,
1042                         pr.range(node_min.Y, node_max.Y)-size.Y/2,
1043                         pr.range(node_min.Z, node_max.Z)-size.Z/2
1044                 );
1045                 MapNode n1;
1046                 if(p0.Y > -32 && pr.range(0,1) == 0)
1047                         n1 = MapNode(c_dirt);
1048                 else
1049                         n1 = MapNode(c_gravel);
1050                 for(int x1=0; x1<size.X; x1++)
1051                 for(int y1=0; y1<size.Y; y1++)
1052                 for(int z1=0; z1<size.Z; z1++)
1053                 {
1054                         v3s16 p = p0 + v3s16(x1,y1,z1);
1055                         u32 i = vmanip.m_area.index(p);
1056                         if(!vmanip.m_area.contains(i))
1057                                 continue;
1058                         // Cancel if not stone and not cave air
1059                         if(vmanip.m_data[i].getContent() != c_stone &&
1060                                         !(vmanip.m_flags[i] & VMANIP_FLAG_CAVE))
1061                                 continue;
1062                         if(only_fill_cave && !(vmanip.m_flags[i] & VMANIP_FLAG_CAVE))
1063                                 continue;
1064                         vmanip.m_data[i] = n1;
1065                 }
1066         }
1067         }
1068
1069 #if 1
1070         {
1071         // 340ms @cs=8
1072         TimeTaker timer1("flow mud");
1073
1074         /*
1075                 Flow mud away from steep edges
1076         */
1077
1078         // Iterate a few times
1079         for(s16 k=0; k<3; k++)
1080         {
1081
1082         for(s16 x=mudflow_minpos; x<=mudflow_maxpos; x++)
1083         for(s16 z=mudflow_minpos; z<=mudflow_maxpos; z++)
1084         {
1085                 // Invert coordinates every 2nd iteration
1086                 if(k%2 == 0)
1087                 {
1088                         x = mudflow_maxpos - (x-mudflow_minpos);
1089                         z = mudflow_maxpos - (z-mudflow_minpos);
1090                 }
1091
1092                 // Node position in 2d
1093                 v2s16 p2d = v2s16(node_min.X, node_min.Z) + v2s16(x,z);
1094
1095                 v3s16 em = vmanip.m_area.getExtent();
1096                 u32 i = vmanip.m_area.index(v3s16(p2d.X, node_max.Y, p2d.Y));
1097                 s16 y=node_max.Y;
1098
1099                 while(y >= node_min.Y)
1100                 {
1101
1102                 for(;; y--)
1103                 {
1104                         MapNode *n = NULL;
1105                         // Find mud
1106                         for(; y>=node_min.Y; y--)
1107                         {
1108                                 n = &vmanip.m_data[i];
1109                                 //if(content_walkable(n->d))
1110                                 //      break;
1111                                 if(n->getContent() == c_dirt ||
1112                                                 n->getContent() == c_dirt_with_grass ||
1113                                                 n->getContent() == c_gravel)
1114                                         break;
1115
1116                                 vmanip.m_area.add_y(em, i, -1);
1117                         }
1118
1119                         // Stop if out of area
1120                         //if(vmanip.m_area.contains(i) == false)
1121                         if(y < node_min.Y)
1122                                 break;
1123
1124                         /*// If not mud, do nothing to it
1125                         MapNode *n = &vmanip.m_data[i];
1126                         if(n->d != CONTENT_MUD && n->d != CONTENT_GRASS)
1127                                 continue;*/
1128
1129                         if(n->getContent() == c_dirt ||
1130                                         n->getContent() == c_dirt_with_grass)
1131                         {
1132                                 // Make it exactly mud
1133                                 n->setContent(c_dirt);
1134
1135                                 /*
1136                                         Don't flow it if the stuff under it is not mud
1137                                 */
1138                                 {
1139                                         u32 i2 = i;
1140                                         vmanip.m_area.add_y(em, i2, -1);
1141                                         // Cancel if out of area
1142                                         if(vmanip.m_area.contains(i2) == false)
1143                                                 continue;
1144                                         MapNode *n2 = &vmanip.m_data[i2];
1145                                         if(n2->getContent() != c_dirt &&
1146                                                         n2->getContent() != c_dirt_with_grass)
1147                                                 continue;
1148                                 }
1149                         }
1150
1151                         /*s16 recurse_count = 0;
1152         mudflow_recurse:*/
1153
1154                         v3s16 dirs4[4] = {
1155                                 v3s16(0,0,1), // back
1156                                 v3s16(1,0,0), // right
1157                                 v3s16(0,0,-1), // front
1158                                 v3s16(-1,0,0), // left
1159                         };
1160
1161                         // Theck that upper is air or doesn't exist.
1162                         // Cancel dropping if upper keeps it in place
1163                         u32 i3 = i;
1164                         vmanip.m_area.add_y(em, i3, 1);
1165                         if(vmanip.m_area.contains(i3) == true
1166                                         && ndef->get(vmanip.m_data[i3]).walkable)
1167                         {
1168                                 continue;
1169                         }
1170
1171                         // Drop mud on side
1172
1173                         for(u32 di=0; di<4; di++)
1174                         {
1175                                 v3s16 dirp = dirs4[di];
1176                                 u32 i2 = i;
1177                                 // Move to side
1178                                 vmanip.m_area.add_p(em, i2, dirp);
1179                                 // Fail if out of area
1180                                 if(vmanip.m_area.contains(i2) == false)
1181                                         continue;
1182                                 // Check that side is air
1183                                 MapNode *n2 = &vmanip.m_data[i2];
1184                                 if(ndef->get(*n2).walkable)
1185                                         continue;
1186                                 // Check that under side is air
1187                                 vmanip.m_area.add_y(em, i2, -1);
1188                                 if(vmanip.m_area.contains(i2) == false)
1189                                         continue;
1190                                 n2 = &vmanip.m_data[i2];
1191                                 if(ndef->get(*n2).walkable)
1192                                         continue;
1193                                 /*// Check that under that is air (need a drop of 2)
1194                                 vmanip.m_area.add_y(em, i2, -1);
1195                                 if(vmanip.m_area.contains(i2) == false)
1196                                         continue;
1197                                 n2 = &vmanip.m_data[i2];
1198                                 if(content_walkable(n2->d))
1199                                         continue;*/
1200                                 // Loop further down until not air
1201                                 bool dropped_to_unknown = false;
1202                                 do{
1203                                         vmanip.m_area.add_y(em, i2, -1);
1204                                         n2 = &vmanip.m_data[i2];
1205                                         // if out of known area
1206                                         if(vmanip.m_area.contains(i2) == false
1207                                                         || n2->getContent() == CONTENT_IGNORE){
1208                                                 dropped_to_unknown = true;
1209                                                 break;
1210                                         }
1211                                 }while(ndef->get(*n2).walkable == false);
1212                                 // Loop one up so that we're in air
1213                                 vmanip.m_area.add_y(em, i2, 1);
1214                                 n2 = &vmanip.m_data[i2];
1215
1216                                 bool old_is_water = (n->getContent() == c_water_source);
1217                                 // Move mud to new place
1218                                 if(!dropped_to_unknown) {
1219                                         *n2 = *n;
1220                                         // Set old place to be air (or water)
1221                                         if(old_is_water)
1222                                                 *n = MapNode(c_water_source);
1223                                         else
1224                                                 *n = MapNode(CONTENT_AIR);
1225                                 }
1226
1227                                 // Done
1228                                 break;
1229                         }
1230                 }
1231                 }
1232         }
1233
1234         }
1235
1236         }//timer1
1237 #endif
1238
1239         } // Aging loop
1240         /***********************
1241                 END OF AGING LOOP
1242         ************************/
1243
1244         /*
1245                 Add top and bottom side of water to transforming_liquid queue
1246         */
1247
1248         for(s16 x=full_node_min.X; x<=full_node_max.X; x++)
1249         for(s16 z=full_node_min.Z; z<=full_node_max.Z; z++)
1250         {
1251                 // Node position
1252                 v2s16 p2d(x,z);
1253                 {
1254                         bool water_found = false;
1255                         // Use fast index incrementing
1256                         v3s16 em = vmanip.m_area.getExtent();
1257                         u32 i = vmanip.m_area.index(v3s16(p2d.X, full_node_max.Y, p2d.Y));
1258                         for(s16 y=full_node_max.Y; y>=full_node_min.Y; y--)
1259                         {
1260                                 if(y == full_node_max.Y){
1261                                         water_found =
1262                                                 (vmanip.m_data[i].getContent() == c_water_source ||
1263                                                 vmanip.m_data[i].getContent() == c_lava_source);
1264                                 }
1265                                 else if(water_found == false)
1266                                 {
1267                                         if(vmanip.m_data[i].getContent() == c_water_source ||
1268                                                         vmanip.m_data[i].getContent() == c_lava_source)
1269                                         {
1270                                                 v3s16 p = v3s16(p2d.X, y, p2d.Y);
1271                                                 data->transforming_liquid.push_back(p);
1272                                                 water_found = true;
1273                                         }
1274                                 }
1275                                 else
1276                                 {
1277                                         // This can be done because water_found can only
1278                                         // turn to true and end up here after going through
1279                                         // a single block.
1280                                         if(vmanip.m_data[i+1].getContent() != c_water_source ||
1281                                                         vmanip.m_data[i+1].getContent() != c_lava_source)
1282                                         {
1283                                                 v3s16 p = v3s16(p2d.X, y+1, p2d.Y);
1284                                                 data->transforming_liquid.push_back(p);
1285                                                 water_found = false;
1286                                         }
1287                                 }
1288
1289                                 vmanip.m_area.add_y(em, i, -1);
1290                         }
1291                 }
1292         }
1293
1294         /*
1295                 Grow grass
1296         */
1297
1298         for(s16 x=full_node_min.X; x<=full_node_max.X; x++)
1299         for(s16 z=full_node_min.Z; z<=full_node_max.Z; z++)
1300         {
1301                 // Node position in 2d
1302                 v2s16 p2d = v2s16(x,z);
1303
1304                 /*
1305                         Find the lowest surface to which enough light ends up
1306                         to make grass grow.
1307
1308                         Basically just wait until not air and not leaves.
1309                 */
1310                 s16 surface_y = 0;
1311                 {
1312                         v3s16 em = vmanip.m_area.getExtent();
1313                         u32 i = vmanip.m_area.index(v3s16(p2d.X, node_max.Y, p2d.Y));
1314                         s16 y;
1315                         // Go to ground level
1316                         for(y=node_max.Y; y>=full_node_min.Y; y--)
1317                         {
1318                                 MapNode &n = vmanip.m_data[i];
1319                                 if(ndef->get(n).param_type != CPT_LIGHT
1320                                                 || ndef->get(n).liquid_type != LIQUID_NONE)
1321                                         break;
1322                                 vmanip.m_area.add_y(em, i, -1);
1323                         }
1324                         if(y >= full_node_min.Y)
1325                                 surface_y = y;
1326                         else
1327                                 surface_y = full_node_min.Y;
1328                 }
1329
1330                 u32 i = vmanip.m_area.index(p2d.X, surface_y, p2d.Y);
1331                 MapNode *n = &vmanip.m_data[i];
1332                 if(n->getContent() == c_dirt){
1333                         // Well yeah, this can't be overground...
1334                         if(surface_y < water_level - 20)
1335                                 continue;
1336                         n->setContent(c_dirt_with_grass);
1337                 }
1338         }
1339
1340         /*
1341                 Generate some trees
1342         */
1343         assert(central_area_size.X == central_area_size.Z);
1344         if (flags & MG_TREES) {
1345                 // Divide area into parts
1346                 s16 div = 8;
1347                 s16 sidelen = central_area_size.X / div;
1348                 double area = sidelen * sidelen;
1349                 for(s16 x0=0; x0<div; x0++)
1350                 for(s16 z0=0; z0<div; z0++)
1351                 {
1352                         // Center position of part of division
1353                         v2s16 p2d_center(
1354                                 node_min.X + sidelen/2 + sidelen*x0,
1355                                 node_min.Z + sidelen/2 + sidelen*z0
1356                         );
1357                         // Minimum edge of part of division
1358                         v2s16 p2d_min(
1359                                 node_min.X + sidelen*x0,
1360                                 node_min.Z + sidelen*z0
1361                         );
1362                         // Maximum edge of part of division
1363                         v2s16 p2d_max(
1364                                 node_min.X + sidelen + sidelen*x0 - 1,
1365                                 node_min.Z + sidelen + sidelen*z0 - 1
1366                         );
1367                         // Amount of trees
1368                         u32 tree_count = area * tree_amount_2d(data->seed, p2d_center);
1369                         // Put trees in random places on part of division
1370                         for(u32 i=0; i<tree_count; i++)
1371                         {
1372                                 s16 x = myrand_range(p2d_min.X, p2d_max.X);
1373                                 s16 z = myrand_range(p2d_min.Y, p2d_max.Y);
1374                                 s16 y = find_ground_level(vmanip, v2s16(x,z), ndef);
1375                                 // Don't make a tree under water level
1376                                 if(y < water_level)
1377                                         continue;
1378                                 // Don't make a tree so high that it doesn't fit
1379                                 if(y > node_max.Y - 6)
1380                                         continue;
1381                                 v3s16 p(x,y,z);
1382                                 /*
1383                                         Trees grow only on mud and grass
1384                                 */
1385                                 {
1386                                         u32 i = vmanip.m_area.index(v3s16(p));
1387                                         MapNode *n = &vmanip.m_data[i];
1388                                         if(n->getContent() != c_dirt
1389                                                         && n->getContent() != c_dirt_with_grass)
1390                                                 continue;
1391                                 }
1392                                 p.Y++;
1393                                 // Make a tree
1394                                 make_tree(vmanip, p, false, ndef);
1395                         }
1396                 }
1397         }
1398
1399
1400         /*
1401                 Calculate lighting
1402         */
1403         {
1404         ScopeProfiler sp(g_profiler, "EmergeThread: mapgen lighting update",
1405                         SPT_AVG);
1406         //VoxelArea a(node_min, node_max);
1407         VoxelArea a(node_min-v3s16(1,0,1)*MAP_BLOCKSIZE,
1408                         node_max+v3s16(1,0,1)*MAP_BLOCKSIZE);
1409         /*VoxelArea a(node_min-v3s16(1,0,1)*MAP_BLOCKSIZE/2,
1410                         node_max+v3s16(1,0,1)*MAP_BLOCKSIZE/2);*/
1411         enum LightBank banks[2] = {LIGHTBANK_DAY, LIGHTBANK_NIGHT};
1412         for(int i=0; i<2; i++)
1413         {
1414                 enum LightBank bank = banks[i];
1415
1416                 core::map<v3s16, bool> light_sources;
1417                 core::map<v3s16, u8> unlight_from;
1418
1419                 voxalgo::clearLightAndCollectSources(vmanip, a, bank, ndef,
1420                                 light_sources, unlight_from);
1421
1422                 bool inexistent_top_provides_sunlight = !block_is_underground;
1423                 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
1424                                 vmanip, a, inexistent_top_provides_sunlight,
1425                                 light_sources, ndef);
1426                 // TODO: Do stuff according to bottom_sunlight_valid
1427
1428                 vmanip.unspreadLight(bank, unlight_from, light_sources, ndef);
1429
1430                 vmanip.spreadLight(bank, light_sources, ndef);
1431         }
1432         }
1433         this->generating = false;
1434 }