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