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