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