Fix nearly all warnings
[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 "cavegen.h"
37 #include "treegen.h"
38 #include "mapgen_v6.h"
39
40 /////////////////// Mapgen V6 perlin noise default values
41 NoiseParams nparams_v6_def_terrain_base =
42         {-AVERAGE_MUD_AMOUNT, 20.0, v3f(250.0, 250.0, 250.0), 82341, 5, 0.6};
43 NoiseParams nparams_v6_def_terrain_higher =
44         {20.0, 16.0, v3f(500.0, 500.0, 500.0), 85039, 5, 0.6};
45 NoiseParams nparams_v6_def_steepness =
46         {0.85, 0.5, v3f(125.0, 125.0, 125.0), -932, 5, 0.7};
47 NoiseParams nparams_v6_def_height_select =
48         {0.5, 1.0, v3f(250.0, 250.0, 250.0), 4213, 5, 0.69};
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 NoiseParams nparams_v6_def_humidity =
58         {0.5, 0.5, v3f(500.0, 500.0, 500.0), 72384, 4, 0.66};
59 NoiseParams nparams_v6_def_trees =
60         {0.0, 1.0, v3f(125.0, 125.0, 125.0), 2, 4, 0.66};
61 NoiseParams nparams_v6_def_apple_trees =
62         {0.0, 1.0, v3f(100.0, 100.0, 100.0), 342902, 3, 0.45};
63
64
65 ///////////////////////////////////////////////////////////////////////////////
66
67
68 MapgenV6::MapgenV6(int mapgenid, MapgenV6Params *params, EmergeManager *emerge) {
69         this->generating  = false;
70         this->id       = mapgenid;
71         this->emerge   = emerge;
72
73         this->seed     = (int)params->seed;
74         this->water_level = params->water_level;
75         this->flags   = params->flags;
76         this->csize   = v3s16(1, 1, 1) * params->chunksize * MAP_BLOCKSIZE;
77
78         this->freq_desert = params->freq_desert;
79         this->freq_beach  = params->freq_beach;
80
81         this->ystride = csize.X; //////fix this
82
83         np_cave        = &params->np_cave;
84         np_humidity    = &params->np_humidity;
85         np_trees       = &params->np_trees;
86         np_apple_trees = &params->np_apple_trees;
87
88         noise_terrain_base   = new Noise(&params->np_terrain_base,   seed, csize.X, csize.Y);
89         noise_terrain_higher = new Noise(&params->np_terrain_higher, seed, csize.X, csize.Y);
90         noise_steepness      = new Noise(&params->np_steepness,      seed, csize.X, csize.Y);
91         noise_height_select  = new Noise(&params->np_height_select,  seed, csize.X, csize.Y);
92         noise_mud            = new Noise(&params->np_mud,            seed, csize.X, csize.Y);
93         noise_beach          = new Noise(&params->np_beach,          seed, csize.X, csize.Y);
94         noise_biome          = new Noise(&params->np_biome,          seed, csize.X, csize.Y);
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_mud;
104         delete noise_beach;
105         delete noise_biome;
106 }
107
108
109 //////////////////////// Some helper functions for the map generator
110
111 // Returns Y one under area minimum if not found
112 s16 MapgenV6::find_ground_level(v2s16 p2d) {
113         v3s16 em = vm->m_area.getExtent();
114         s16 y_nodes_max = vm->m_area.MaxEdge.Y;
115         s16 y_nodes_min = vm->m_area.MinEdge.Y;
116         u32 i = vm->m_area.index(p2d.X, y_nodes_max, p2d.Y);
117         s16 y;
118         
119         for (y = y_nodes_max; y >= y_nodes_min; y--) {
120                 MapNode &n = vm->m_data[i];
121                 if(ndef->get(n).walkable)
122                         break;
123
124                 vm->m_area.add_y(em, i, -1);
125         }
126         return (y >= y_nodes_min) ? y : y_nodes_min - 1;
127 }
128
129 // Returns Y one under area minimum if not found
130 s16 MapgenV6::find_stone_level(v2s16 p2d) {
131         v3s16 em = vm->m_area.getExtent();
132         s16 y_nodes_max = vm->m_area.MaxEdge.Y;
133         s16 y_nodes_min = vm->m_area.MinEdge.Y;
134         u32 i = vm->m_area.index(p2d.X, y_nodes_max, p2d.Y);
135         s16 y;
136
137         for (y = y_nodes_max; y >= y_nodes_min; y--) {
138                 MapNode &n = vm->m_data[i];
139                 content_t c = n.getContent();
140                 if (c != CONTENT_IGNORE && (
141                         c == c_stone || c == c_desert_stone))
142                         break;
143
144                 vm->m_area.add_y(em, i, -1);
145         }
146         return (y >= y_nodes_min) ? y : y_nodes_min - 1;
147 }
148
149
150 // Required by mapgen.h
151 bool MapgenV6::block_is_underground(u64 seed, v3s16 blockpos)
152 {
153         /*s16 minimum_groundlevel = (s16)get_sector_minimum_ground_level(
154                         seed, v2s16(blockpos.X, blockpos.Z));*/
155         // Nah, this is just a heuristic, just return something
156         s16 minimum_groundlevel = water_level;
157
158         if(blockpos.Y*MAP_BLOCKSIZE + MAP_BLOCKSIZE <= minimum_groundlevel)
159                 return true;
160         else
161                 return false;
162 }
163
164
165 //////////////////////// Base terrain height functions
166
167 float MapgenV6::baseTerrainLevel(float terrain_base, float terrain_higher,
168                                                                         float steepness, float height_select) { 
169         float base   = water_level + terrain_base;
170         float higher = water_level + terrain_higher;
171
172         // Limit higher ground level to at least base
173         if(higher < base)
174                 higher = base;
175
176         // Steepness factor of cliffs
177         float b = steepness;
178         b = rangelim(b, 0.0, 1000.0);
179         b = 5 * b * b * b * b * b * b * b;
180         b = rangelim(b, 0.5, 1000.0);
181
182         // Values 1.5...100 give quite horrible looking slopes
183         if (b > 1.5 && b < 100.0)
184                 b = (b < 10.0) ? 1.5 : 100.0;
185
186         float a_off = -0.20; // Offset to more low
187         float a = 0.5 + b * (a_off + height_select);
188         a = rangelim(a, 0.0, 1.0); // Limit
189         
190         return base * (1.0 - a) + higher * a;
191 }
192
193
194 float MapgenV6::baseTerrainLevelFromNoise(v2s16 p) {
195         if (flags & MG_FLAT)
196                 return water_level;
197                 
198         float terrain_base   = NoisePerlin2DPosOffset(noise_terrain_base->np,
199                                                         p.X, 0.5, p.Y, 0.5, seed);
200         float terrain_higher = NoisePerlin2DPosOffset(noise_terrain_higher->np,
201                                                         p.X, 0.5, p.Y, 0.5, seed);
202         float steepness      = NoisePerlin2DPosOffset(noise_steepness->np,
203                                                         p.X, 0.5, p.Y, 0.5, seed);
204         float height_select  = NoisePerlin2DNoTxfmPosOffset(noise_height_select->np,
205                                                         p.X, 0.5, p.Y, 0.5, seed);
206
207         return baseTerrainLevel(terrain_base, terrain_higher,
208                                                         steepness,    height_select);
209 }
210
211
212 float MapgenV6::baseTerrainLevelFromMap(v2s16 p) {
213         int index = (p.Y - node_min.Z) * ystride + (p.X - node_min.X);
214         return baseTerrainLevelFromMap(index);
215 }
216
217
218 float MapgenV6::baseTerrainLevelFromMap(int index) {
219         if (flags & MG_FLAT)
220                 return water_level;
221         
222         float terrain_base   = noise_terrain_base->result[index];
223         float terrain_higher = noise_terrain_higher->result[index];
224         float steepness      = noise_steepness->result[index];
225         float height_select  = noise_height_select->result[index];
226         
227         return baseTerrainLevel(terrain_base, terrain_higher,
228                                                         steepness,    height_select);
229 }
230
231
232 s16 MapgenV6::find_ground_level_from_noise(u64 seed, v2s16 p2d, s16 precision) {
233         return baseTerrainLevelFromNoise(p2d) + AVERAGE_MUD_AMOUNT;
234 }
235
236
237 int MapgenV6::getGroundLevelAtPoint(v2s16 p) {
238         return baseTerrainLevelFromNoise(p) + AVERAGE_MUD_AMOUNT;
239 }
240
241
242 //////////////////////// Noise functions
243
244 float MapgenV6::getMudAmount(v2s16 p) {
245         int index = (p.Y - node_min.Z) * ystride + (p.X - node_min.X);
246         return getMudAmount(index);
247 }
248
249
250 bool MapgenV6::getHaveBeach(v2s16 p) {
251         int index = (p.Y - node_min.Z) * ystride + (p.X - node_min.X);
252         return getHaveBeach(index);
253 }
254
255
256 BiomeType MapgenV6::getBiome(v2s16 p) {
257         int index = (p.Y - node_min.Z) * ystride + (p.X - node_min.X);
258         return getBiome(index, p);
259 }
260
261
262 float MapgenV6::getHumidity(v2s16 p)
263 {
264         /*double noise = noise2d_perlin(
265                 0.5+(float)p.X/500, 0.5+(float)p.Y/500,
266                 seed+72384, 4, 0.66);
267         noise = (noise + 1.0)/2.0;*/
268
269         float noise = NoisePerlin2D(np_humidity, p.X, p.Y, seed);
270
271         if (noise < 0.0)
272                 noise = 0.0;
273         if (noise > 1.0)
274                 noise = 1.0;
275         return noise;
276 }
277
278
279 float MapgenV6::getTreeAmount(v2s16 p)
280 {
281         /*double noise = noise2d_perlin(
282                         0.5+(float)p.X/125, 0.5+(float)p.Y/125,
283                         seed+2, 4, 0.66);*/
284         
285         float noise = NoisePerlin2D(np_trees, p.X, p.Y, seed);
286         float zeroval = -0.39;
287         if (noise < zeroval)
288                 return 0;
289         else
290                 return 0.04 * (noise-zeroval) / (1.0-zeroval);
291 }
292
293
294 bool MapgenV6::getHaveAppleTree(v2s16 p)
295 {
296         /*is_apple_tree = noise2d_perlin(
297                 0.5+(float)p.X/100, 0.5+(float)p.Z/100,
298                 data->seed+342902, 3, 0.45) > 0.2;*/
299         
300         float noise = NoisePerlin2D(np_apple_trees, p.X, p.Y, seed);
301         
302         return noise > 0.2;
303 }
304
305
306 float MapgenV6::getMudAmount(int index)
307 {
308         if (flags & MG_FLAT)
309                 return AVERAGE_MUD_AMOUNT;
310                 
311         /*return ((float)AVERAGE_MUD_AMOUNT + 2.0 * noise2d_perlin(
312                         0.5+(float)p.X/200, 0.5+(float)p.Y/200,
313                         seed+91013, 3, 0.55));*/
314         
315         return noise_mud->result[index];
316 }
317
318
319 bool MapgenV6::getHaveBeach(int index)
320 {
321         // Determine whether to have sand here
322         /*double sandnoise = noise2d_perlin(
323                         0.2+(float)p2d.X/250, 0.7+(float)p2d.Y/250,
324                         seed+59420, 3, 0.50);*/
325         
326         float sandnoise = noise_beach->result[index];
327         return (sandnoise > freq_beach);
328 }
329
330
331 BiomeType MapgenV6::getBiome(int index, v2s16 p)
332 {
333         // Just do something very simple as for now
334         /*double d = noise2d_perlin(
335                         0.6+(float)p2d.X/250, 0.2+(float)p2d.Y/250,
336                         seed+9130, 3, 0.50);*/
337         
338         float d = noise_biome->result[index];
339         if (d > freq_desert)
340                 return BT_DESERT;
341                 
342         if ((flags & MGV6_BIOME_BLEND) &&
343                 (d > freq_desert - 0.10) &&
344                 ((noise2d(p.X, p.Y, seed) + 1.0) > (freq_desert - d) * 20.0))
345                 return BT_DESERT;
346         
347         return BT_NORMAL;
348 }
349
350
351 u32 MapgenV6::get_blockseed(u64 seed, v3s16 p)
352 {
353         s32 x=p.X, y=p.Y, z=p.Z;
354         return (u32)(seed%0x100000000ULL) + z*38134234 + y*42123 + x*23;
355 }
356
357
358 //////////////////////// Map generator
359
360 void MapgenV6::makeChunk(BlockMakeData *data) {
361         assert(data->vmanip);
362         assert(data->nodedef);
363         assert(data->blockpos_requested.X >= data->blockpos_min.X &&
364                    data->blockpos_requested.Y >= data->blockpos_min.Y &&
365                    data->blockpos_requested.Z >= data->blockpos_min.Z);
366         assert(data->blockpos_requested.X <= data->blockpos_max.X &&
367                    data->blockpos_requested.Y <= data->blockpos_max.Y &&
368                    data->blockpos_requested.Z <= data->blockpos_max.Z);
369                         
370         this->generating = true;
371         this->vm   = data->vmanip;      
372         this->ndef = data->nodedef;
373         
374         // Hack: use minimum block coords for old code that assumes a single block
375         v3s16 blockpos = data->blockpos_requested;
376         v3s16 blockpos_min = data->blockpos_min;
377         v3s16 blockpos_max = data->blockpos_max;
378
379         // Area of central chunk
380         node_min = blockpos_min*MAP_BLOCKSIZE;
381         node_max = (blockpos_max+v3s16(1,1,1))*MAP_BLOCKSIZE-v3s16(1,1,1);
382
383         // Full allocated area
384         full_node_min = (blockpos_min-1)*MAP_BLOCKSIZE;
385         full_node_max = (blockpos_max+2)*MAP_BLOCKSIZE-v3s16(1,1,1);
386
387         central_area_size = node_max - node_min + v3s16(1,1,1);
388         assert(central_area_size.X == central_area_size.Z);
389
390         int volume_blocks = (blockpos_max.X - blockpos_min.X + 1)
391                                           * (blockpos_max.Y - blockpos_min.Y + 1)
392                                           * (blockpos_max.Z - blockpos_max.Z + 1);
393
394         volume_nodes = volume_blocks *
395                 MAP_BLOCKSIZE * MAP_BLOCKSIZE * MAP_BLOCKSIZE;
396
397         // Create a block-specific seed
398         blockseed = get_blockseed(data->seed, full_node_min);
399
400         // Make some noise
401         calculateNoise();
402
403         c_stone           = ndef->getId("mapgen_stone");
404         c_dirt            = ndef->getId("mapgen_dirt");
405         c_dirt_with_grass = ndef->getId("mapgen_dirt_with_grass");
406         c_sand            = ndef->getId("mapgen_sand");
407         c_water_source    = ndef->getId("mapgen_water_source");
408         c_lava_source     = ndef->getId("mapgen_lava_source");
409         c_gravel          = ndef->getId("mapgen_gravel");
410         c_cobble          = ndef->getId("mapgen_cobble");
411         c_desert_sand     = ndef->getId("mapgen_desert_sand");
412         c_desert_stone    = ndef->getId("mapgen_desert_stone");
413         if (c_desert_sand == CONTENT_IGNORE)
414                 c_desert_sand = c_sand;
415         if (c_desert_stone == CONTENT_IGNORE)
416                 c_desert_stone = c_stone;
417
418         // Maximum height of the stone surface and obstacles.
419         // This is used to guide the cave generation
420         s16 stone_surface_max_y;
421
422         // Generate general ground level to full area
423         stone_surface_max_y = generateGround();
424
425         generateExperimental();
426
427         const s16 max_spread_amount = MAP_BLOCKSIZE;
428         // Limit dirt flow area by 1 because mud is flown into neighbors.
429         s16 mudflow_minpos = -max_spread_amount + 1;
430         s16 mudflow_maxpos = central_area_size.X + max_spread_amount - 2;
431
432         // Loop this part, it will make stuff look older and newer nicely
433         const u32 age_loops = 2;
434         for (u32 i_age = 0; i_age < age_loops; i_age++) { // Aging loop
435                 // Make caves (this code is relatively horrible)
436                 if (flags & MG_CAVES)
437                         generateCaves(stone_surface_max_y);
438
439                 // Add mud to the central chunk
440                 addMud();
441
442                 // Add blobs of dirt and gravel underground
443                 addDirtGravelBlobs();
444
445                 // Flow mud away from steep edges
446                 flowMud(mudflow_minpos, mudflow_maxpos);
447
448         }
449         
450         // Add dungeons
451         if (flags & MG_DUNGEONS) {
452                 DungeonGen dgen(ndef, data->seed, water_level);
453                 dgen.generate(vm, blockseed, full_node_min, full_node_max);
454         }
455         
456         // Add top and bottom side of water to transforming_liquid queue
457         updateLiquid(&data->transforming_liquid, full_node_min, full_node_max);
458
459         // Grow grass
460         growGrass();
461
462         // Generate some trees, and add grass, if a jungle
463         if (flags & MG_TREES)
464                 placeTreesAndJungleGrass();
465
466         // Generate the registered ores
467         for (unsigned int i = 0; i != emerge->ores.size(); i++) {
468                 Ore *ore = emerge->ores[i];
469                 ore->placeOre(this, blockseed + i, node_min, node_max);
470         }
471
472         // Calculate lighting
473         calcLighting(node_min - v3s16(1, 1, 1) * MAP_BLOCKSIZE,
474                                  node_max + v3s16(1, 0, 1) * MAP_BLOCKSIZE);
475         
476         this->generating = false;
477 }
478
479
480 void MapgenV6::calculateNoise() {
481         int x = node_min.X;
482         int z = node_min.Z;
483
484         // Need to adjust for the original implementation's +.5 offset...
485         if (!(flags & MG_FLAT)) {
486                 noise_terrain_base->perlinMap2D(
487                         x + 0.5 * noise_terrain_base->np->spread.X,
488                         z + 0.5 * noise_terrain_base->np->spread.Z);
489                 noise_terrain_base->transformNoiseMap();
490
491                 noise_terrain_higher->perlinMap2D(
492                         x + 0.5 * noise_terrain_higher->np->spread.X,
493                         z + 0.5 * noise_terrain_higher->np->spread.Z);
494                 noise_terrain_higher->transformNoiseMap();
495
496                 noise_steepness->perlinMap2D(
497                         x + 0.5 * noise_steepness->np->spread.X,
498                         z + 0.5 * noise_steepness->np->spread.Z);
499                 noise_steepness->transformNoiseMap();
500
501                 noise_height_select->perlinMap2D(
502                         x + 0.5 * noise_height_select->np->spread.X,
503                         z + 0.5 * noise_height_select->np->spread.Z);
504
505                 noise_mud->perlinMap2D(
506                         x + 0.5 * noise_mud->np->spread.X,
507                         z + 0.5 * noise_mud->np->spread.Z);
508                 noise_mud->transformNoiseMap();
509         }
510
511         noise_beach->perlinMap2D(
512                 x + 0.2 * noise_beach->np->spread.X,
513                 z + 0.7 * noise_beach->np->spread.Z);
514
515         noise_biome->perlinMap2D(
516                 x + 0.6 * noise_biome->np->spread.X,
517                 z + 0.2 * noise_biome->np->spread.Z);
518 }
519
520
521 int MapgenV6::generateGround() {
522         //TimeTaker timer1("Generating ground level");
523         MapNode n_air(CONTENT_AIR), n_water_source(c_water_source);
524         MapNode n_stone(c_stone), n_desert_stone(c_desert_stone);
525         int stone_surface_max_y = -MAP_GENERATION_LIMIT;
526         u32 index = 0;
527         
528         for (s16 z = node_min.Z; z <= node_max.Z; z++)
529         for (s16 x = node_min.X; x <= node_max.X; x++, index++) {
530                 // Surface height
531                 s16 surface_y = (s16)baseTerrainLevelFromMap(index);
532                 
533                 // Log it
534                 if (surface_y > stone_surface_max_y)
535                         stone_surface_max_y = surface_y;
536
537                 BiomeType bt = getBiome(index, v2s16(x, z));
538                 
539                 // Fill ground with stone
540                 v3s16 em = vm->m_area.getExtent();
541                 u32 i = vm->m_area.index(x, node_min.Y, z);
542                 for (s16 y = node_min.Y; y <= node_max.Y; y++) {
543                         if (vm->m_data[i].getContent() == CONTENT_IGNORE) {
544                                 if (y <= surface_y) {
545                                         vm->m_data[i] = (y > water_level && bt == BT_DESERT) ? 
546                                                 n_desert_stone : n_stone;
547                                 } else if (y <= water_level) {
548                                         vm->m_data[i] = n_water_source;
549                                 } else {
550                                         vm->m_data[i] = n_air;
551                                 }
552                         }
553                         vm->m_area.add_y(em, i, 1);
554                 }
555         }
556         
557         return stone_surface_max_y;
558 }
559
560
561 void MapgenV6::addMud() {
562         // 15ms @cs=8
563         //TimeTaker timer1("add mud");
564         MapNode n_dirt(c_dirt), n_gravel(c_gravel);
565         MapNode n_sand(c_sand), n_desert_sand(c_desert_sand);
566         MapNode addnode;
567
568         u32 index = 0;
569         for (s16 z = node_min.Z; z <= node_max.Z; z++)
570         for (s16 x = node_min.X; x <= node_max.X; x++, index++) {
571                 // Randomize mud amount
572                 s16 mud_add_amount = getMudAmount(index) / 2.0 + 0.5;
573
574                 // Find ground level
575                 s16 surface_y = find_stone_level(v2s16(x, z)); /////////////////optimize this!
576                 
577                 // Handle area not found
578                 if (surface_y == vm->m_area.MinEdge.Y - 1)
579                         continue;
580                 
581                 BiomeType bt = getBiome(index, v2s16(x, z));
582                 addnode = (bt == BT_DESERT) ? n_desert_sand : n_dirt;
583
584                 if (bt == BT_DESERT && surface_y + mud_add_amount <= water_level + 1) {
585                         addnode = n_sand;
586                 } else if (mud_add_amount <= 0) {
587                         mud_add_amount = 1 - mud_add_amount;
588                         addnode = n_gravel;
589                 } else if (bt == BT_NORMAL && getHaveBeach(index) &&
590                                 surface_y + mud_add_amount <= water_level + 2) {
591                         addnode = n_sand;
592                 }
593
594                 if (bt == BT_DESERT && surface_y > 20)
595                         mud_add_amount = MYMAX(0, mud_add_amount - (surface_y - 20) / 5);
596
597                 // If topmost node is grass, change it to mud.  It might be if it was
598                 // flown to there from a neighboring chunk and then converted.
599                 u32 i = vm->m_area.index(x, surface_y, z);
600                 if (vm->m_data[i].getContent() == c_dirt_with_grass)
601                         vm->m_data[i] = n_dirt;
602
603                 // Add mud on ground
604                 s16 mudcount = 0;
605                 v3s16 em = vm->m_area.getExtent();
606                 s16 y_start = surface_y + 1;
607                 i = vm->m_area.index(x, y_start, z);
608                 for (s16 y = y_start; y <= node_max.Y; y++) {
609                         if (mudcount >= mud_add_amount)
610                                 break;
611
612                         vm->m_data[i] = addnode;
613                         mudcount++;
614
615                         vm->m_area.add_y(em, i, 1);
616                 }
617         }
618 }
619
620
621 void MapgenV6::flowMud(s16 &mudflow_minpos, s16 &mudflow_maxpos) {
622         // 340ms @cs=8
623         TimeTaker timer1("flow mud");
624
625         // Iterate a few times
626         for(s16 k = 0; k < 3; k++) {
627                 for (s16 z = mudflow_minpos; z <= mudflow_maxpos; z++)
628                 for (s16 x = mudflow_minpos; x <= mudflow_maxpos; x++) {
629                         // Invert coordinates every 2nd iteration
630                         if (k % 2 == 0) {
631                                 x = mudflow_maxpos - (x - mudflow_minpos);
632                                 z = mudflow_maxpos - (z - mudflow_minpos);
633                         }
634
635                         // Node position in 2d
636                         v2s16 p2d = v2s16(node_min.X, node_min.Z) + v2s16(x, z);
637
638                         v3s16 em = vm->m_area.getExtent();
639                         u32 i = vm->m_area.index(p2d.X, node_max.Y, p2d.Y);
640                         s16 y = node_max.Y;
641
642                         while(y >= node_min.Y)
643                         {
644
645                         for(;; y--)
646                         {
647                                 MapNode *n = NULL;
648                                 // Find mud
649                                 for(; y >= node_min.Y; y--) {
650                                         n = &vm->m_data[i];
651                                         if (n->getContent() == c_dirt ||
652                                                 n->getContent() == c_dirt_with_grass ||
653                                                 n->getContent() == c_gravel)
654                                                 break;
655
656                                         vm->m_area.add_y(em, i, -1);
657                                 }
658
659                                 // Stop if out of area
660                                 //if(vmanip.m_area.contains(i) == false)
661                                 if (y < node_min.Y)
662                                         break;
663
664                                 if (n->getContent() == c_dirt ||
665                                         n->getContent() == c_dirt_with_grass)
666                                 {
667                                         // Make it exactly mud
668                                         n->setContent(c_dirt);
669
670                                         // Don't flow it if the stuff under it is not mud
671                                         {
672                                                 u32 i2 = i;
673                                                 vm->m_area.add_y(em, i2, -1);
674                                                 // Cancel if out of area
675                                                 if(vm->m_area.contains(i2) == false)
676                                                         continue;
677                                                 MapNode *n2 = &vm->m_data[i2];
678                                                 if (n2->getContent() != c_dirt &&
679                                                         n2->getContent() != c_dirt_with_grass)
680                                                         continue;
681                                         }
682                                 }
683
684                                 v3s16 dirs4[4] = {
685                                         v3s16(0,0,1), // back
686                                         v3s16(1,0,0), // right
687                                         v3s16(0,0,-1), // front
688                                         v3s16(-1,0,0), // left
689                                 };
690
691                                 // Check that upper is air or doesn't exist.
692                                 // Cancel dropping if upper keeps it in place
693                                 u32 i3 = i;
694                                 vm->m_area.add_y(em, i3, 1);
695                                 if (vm->m_area.contains(i3) == true &&
696                                         ndef->get(vm->m_data[i3]).walkable)
697                                         continue;
698
699                                 // Drop mud on side
700                                 for(u32 di=0; di<4; di++) {
701                                         v3s16 dirp = dirs4[di];
702                                         u32 i2 = i;
703                                         // Move to side
704                                         vm->m_area.add_p(em, i2, dirp);
705                                         // Fail if out of area
706                                         if (vm->m_area.contains(i2) == false)
707                                                 continue;
708                                         // Check that side is air
709                                         MapNode *n2 = &vm->m_data[i2];
710                                         if (ndef->get(*n2).walkable)
711                                                 continue;
712                                         // Check that under side is air
713                                         vm->m_area.add_y(em, i2, -1);
714                                         if (vm->m_area.contains(i2) == false)
715                                                 continue;
716                                         n2 = &vm->m_data[i2];
717                                         if (ndef->get(*n2).walkable)
718                                                 continue;
719                                         // Loop further down until not air
720                                         bool dropped_to_unknown = false;
721                                         do {
722                                                 vm->m_area.add_y(em, i2, -1);
723                                                 n2 = &vm->m_data[i2];
724                                                 // if out of known area
725                                                 if(vm->m_area.contains(i2) == false ||
726                                                         n2->getContent() == CONTENT_IGNORE) {
727                                                         dropped_to_unknown = true;
728                                                         break;
729                                                 }
730                                         } while (ndef->get(*n2).walkable == false);
731                                         // Loop one up so that we're in air
732                                         vm->m_area.add_y(em, i2, 1);
733                                         n2 = &vm->m_data[i2];
734
735                                         bool old_is_water = (n->getContent() == c_water_source);
736                                         // Move mud to new place
737                                         if (!dropped_to_unknown) {
738                                                 *n2 = *n;
739                                                 // Set old place to be air (or water)
740                                                 if(old_is_water)
741                                                         *n = MapNode(c_water_source);
742                                                 else
743                                                         *n = MapNode(CONTENT_AIR);
744                                         }
745
746                                         // Done
747                                         break;
748                                 }
749                         }
750                         }
751                 }
752         }
753 }
754
755
756 void MapgenV6::addDirtGravelBlobs() {
757         if (getBiome(v2s16(node_min.X, node_min.Z)) != BT_NORMAL)
758                 return;
759         
760         PseudoRandom pr(blockseed + 983);
761         for (int i = 0; i < volume_nodes/10/10/10; i++) {
762                 bool only_fill_cave = (myrand_range(0,1) != 0);
763                 v3s16 size(
764                         pr.range(1, 8),
765                         pr.range(1, 8),
766                         pr.range(1, 8)
767                 );
768                 v3s16 p0(
769                         pr.range(node_min.X, node_max.X) - size.X / 2,
770                         pr.range(node_min.Y, node_max.Y) - size.Y / 2,
771                         pr.range(node_min.Z, node_max.Z) - size.Z / 2
772                 );
773                 
774                 MapNode n1((p0.Y > -32 && !pr.range(0, 1)) ? c_dirt : c_gravel);
775                 for (int z1 = 0; z1 < size.Z; z1++)
776                 for (int y1 = 0; y1 < size.Y; y1++)
777                 for (int x1 = 0; x1 < size.X; x1++) {
778                         v3s16 p = p0 + v3s16(x1, y1, z1);
779                         u32 i = vm->m_area.index(p);
780                         if (!vm->m_area.contains(i))
781                                 continue;
782                         // Cancel if not stone and not cave air
783                         if (vm->m_data[i].getContent() != c_stone &&
784                                 !(vm->m_flags[i] & VMANIP_FLAG_CAVE))
785                                 continue;
786                         if (only_fill_cave && !(vm->m_flags[i] & VMANIP_FLAG_CAVE))
787                                 continue;
788                         vm->m_data[i] = n1;
789                 }
790         }
791 }
792
793
794 void MapgenV6::placeTreesAndJungleGrass() {
795         //TimeTaker t("placeTrees");
796         if (node_max.Y < water_level)
797                 return;
798         
799         PseudoRandom grassrandom(blockseed + 53);
800         content_t c_junglegrass = ndef->getId("mapgen_junglegrass");
801         // if we don't have junglegrass, don't place cignore... that's bad
802         if (c_junglegrass == CONTENT_IGNORE)
803                 c_junglegrass = CONTENT_AIR;
804         MapNode n_junglegrass(c_junglegrass);
805         v3s16 em = vm->m_area.getExtent();
806         
807         // Divide area into parts
808         s16 div = 8;
809         s16 sidelen = central_area_size.X / div;
810         double area = sidelen * sidelen;
811         
812         // N.B.  We must add jungle grass first, since tree leaves will
813         // obstruct the ground, giving us a false ground level
814         for (s16 z0 = 0; z0 < div; z0++)
815         for (s16 x0 = 0; x0 < div; x0++) {
816                 // Center position of part of division
817                 v2s16 p2d_center(
818                         node_min.X + sidelen / 2 + sidelen * x0,
819                         node_min.Z + sidelen / 2 + sidelen * z0
820                 );
821                 // Minimum edge of part of division
822                 v2s16 p2d_min(
823                         node_min.X + sidelen * x0,
824                         node_min.Z + sidelen * z0
825                 );
826                 // Maximum edge of part of division
827                 v2s16 p2d_max(
828                         node_min.X + sidelen + sidelen * x0 - 1,
829                         node_min.Z + sidelen + sidelen * z0 - 1
830                 );
831                 
832                 // Amount of trees, jungle area
833                 u32 tree_count = area * getTreeAmount(p2d_center);
834                 
835                 float humidity;
836                 bool is_jungle = false;
837                 if (flags & MGV6_JUNGLES) {
838                         humidity = getHumidity(p2d_center);
839                         if (humidity > 0.75) {
840                                 is_jungle = true;
841                                 tree_count *= 4;
842                         }
843                 }
844
845                 // Add jungle grass
846                 if (is_jungle) {                        
847                         u32 grass_count = 5 * humidity * tree_count;
848                         for (u32 i = 0; i < grass_count; i++) {
849                                 s16 x = grassrandom.range(p2d_min.X, p2d_max.X);
850                                 s16 z = grassrandom.range(p2d_min.Y, p2d_max.Y);
851                                 
852                                 s16 y = find_ground_level(v2s16(x, z)); ////////////////optimize this!
853                                 if (y < water_level || y < node_min.Y || y > node_max.Y)
854                                         continue;
855                                 
856                                 u32 vi = vm->m_area.index(x, y, z);
857                                 // place on dirt_with_grass, since we know it is exposed to sunlight
858                                 if (vm->m_data[vi].getContent() == c_dirt_with_grass) {
859                                         vm->m_area.add_y(em, vi, 1);
860                                         vm->m_data[vi] = n_junglegrass;
861                                 }
862                         }
863                 }
864                 
865                 // Put trees in random places on part of division
866                 for (u32 i = 0; i < tree_count; i++) {
867                         s16 x = myrand_range(p2d_min.X, p2d_max.X);
868                         s16 z = myrand_range(p2d_min.Y, p2d_max.Y);
869                         s16 y = find_ground_level(v2s16(x, z)); ////////////////////optimize this!
870                         // Don't make a tree under water level
871                         // Don't make a tree so high that it doesn't fit
872                         if(y < water_level || y > node_max.Y - 6)
873                                 continue;
874                         
875                         v3s16 p(x,y,z);
876                         // Trees grow only on mud and grass
877                         {
878                                 u32 i = vm->m_area.index(p);
879                                 MapNode *n = &vm->m_data[i];
880                                 if (n->getContent() != c_dirt &&
881                                         n->getContent() != c_dirt_with_grass)
882                                         continue;
883                         }
884                         p.Y++;
885                         
886                         // Make a tree
887                         if (is_jungle) {
888                                 treegen::make_jungletree(*vm, p, ndef, myrand());
889                         } else {
890                                 bool is_apple_tree = (myrand_range(0, 3) == 0) &&
891                                                                                 getHaveAppleTree(v2s16(x, z));
892                                 treegen::make_tree(*vm, p, is_apple_tree, ndef, myrand());
893                         }
894                 }
895         }
896         //printf("placeTreesAndJungleGrass: %dms\n", t.stop());
897 }
898
899
900 void MapgenV6::growGrass() {
901         for (s16 z = full_node_min.Z; z <= full_node_max.Z; z++)
902         for (s16 x = full_node_min.X; x <= full_node_max.X; x++) {
903                 // Find the lowest surface to which enough light ends up to make
904                 // grass grow.  Basically just wait until not air and not leaves.
905                 s16 surface_y = 0;
906                 {
907                         v3s16 em = vm->m_area.getExtent();
908                         u32 i = vm->m_area.index(x, node_max.Y, z);
909                         s16 y;
910                         // Go to ground level
911                         for (y = node_max.Y; y >= full_node_min.Y; y--) {
912                                 MapNode &n = vm->m_data[i];
913                                 if (ndef->get(n).param_type != CPT_LIGHT ||
914                                         ndef->get(n).liquid_type != LIQUID_NONE)
915                                         break;
916                                 vm->m_area.add_y(em, i, -1);
917                         }
918                         surface_y = (y >= full_node_min.Y) ? y : full_node_min.Y;
919                 }
920
921                 u32 i = vm->m_area.index(x, surface_y, z);
922                 MapNode *n = &vm->m_data[i];
923                 if (n->getContent() == c_dirt && surface_y >= water_level - 20)
924                         n->setContent(c_dirt_with_grass);
925         }
926 }
927
928
929 void MapgenV6::generateCaves(int max_stone_y) {
930         float cave_amount = NoisePerlin2D(np_cave, node_min.X, node_min.Y, seed);
931         int volume_nodes = (node_max.X - node_min.X + 1) *
932                                            (node_max.Y - node_min.Y + 1) * MAP_BLOCKSIZE;
933         cave_amount = MYMAX(0.0, cave_amount);
934         u32 caves_count = cave_amount * volume_nodes / 50000;
935         u32 bruises_count = 1;
936         PseudoRandom ps(blockseed + 21343);
937         PseudoRandom ps2(blockseed + 1032);
938         
939         if (ps.range(1, 6) == 1)
940                 bruises_count = ps.range(0, ps.range(0, 2));
941         
942         if (getBiome(v2s16(node_min.X, node_min.Z)) == BT_DESERT) {
943                 caves_count   /= 3;
944                 bruises_count /= 3;
945         }
946         
947         for (u32 i = 0; i < caves_count + bruises_count; i++) {
948                 bool large_cave = (i >= caves_count);
949                 CaveV6 cave(this, &ps, &ps2, large_cave);
950
951                 cave.makeCave(node_min, node_max, max_stone_y);
952         }
953 }