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