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