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