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