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