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