7564999cc91776e0b44c0d212bd62bd6780e5b52
[oweals/minetest.git] / src / mapgen.cpp
1 /*
2 Minetest-c55
3 Copyright (C) 2010-2011 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include "mapgen.h"
21 #include "voxel.h"
22 #include "noise.h"
23 #include "biome.h"
24 #include "mapblock.h"
25 #include "mapnode.h"
26 #include "map.h"
27 //#include "serverobject.h"
28 #include "content_sao.h"
29 #include "nodedef.h"
30 #include "content_mapnode.h" // For content_mapnode_get_new_name
31 #include "voxelalgorithms.h"
32 #include "profiler.h"
33 #include "settings.h" // For g_settings
34 #include "main.h" // For g_profiler
35 #include "treegen.h"
36
37
38
39 /*
40 MapgenV7Params mg_def_params_v7 = {
41         0,
42         1,
43         5,
44         MG_TREES | MG_CAVES | MG_DUNGEONS,
45         &nparams_v7_def_terrain,
46         &nparams_v7_def_bgroup,
47         &nparams_v7_def_heat,
48         &nparams_v7_def_humidity
49 };*/
50
51
52 ///////////////////////////////////////////////////////////////////////////////
53
54
55 MapgenV7::MapgenV7(BiomeDefManager *biomedef, int mapgenid, MapgenV7Params *params) {
56         this->generating  = false;
57         this->id       = mapgenid;
58         this->biomedef = biomedef;
59
60         this->seed        = params->seed;
61         this->csize       = v3s16(1, 1, 1) * params->chunksize * MAP_BLOCKSIZE; /////////////////get this from config!
62         this->water_level = params->water_level;
63
64         //g_settings->getS16("default_water_level");
65
66         /*
67         this->np_terrain  = np_terrain;
68         this->np_bgroup   = np_bgroup;
69         this->np_heat     = np_heat;
70         this->np_humidity = np_humidity;
71         */
72         noise_terrain  = new Noise(params->np_terrain,  seed, csize.X, csize.Y);
73         noise_bgroup   = new Noise(params->np_bgroup,   seed, csize.X, csize.Y);
74         noise_heat     = new Noise(params->np_heat,     seed, csize.X, csize.Y);
75         noise_humidity = new Noise(params->np_humidity, seed, csize.X, csize.Y);
76
77         this->ndef = biomedef->ndef;
78         n_air   = MapNode(ndef->getId("mapgen_air"));
79         n_water = MapNode(ndef->getId("mapgen_water_source"));
80         n_lava  = MapNode(ndef->getId("mapgen_lava_source"));
81 }
82
83
84 MapgenV7::~MapgenV7() {
85         delete noise_terrain;
86         delete noise_bgroup;
87         delete noise_heat;
88         delete noise_humidity;
89 }
90
91
92 void MapgenV7::makeChunk(BlockMakeData *data) {
93         if (data->no_op)
94                 return;
95
96         assert(data->vmanip);
97         assert(data->nodedef);
98         assert(data->blockpos_requested.X >= data->blockpos_min.X &&
99                    data->blockpos_requested.Y >= data->blockpos_min.Y &&
100                    data->blockpos_requested.Z >= data->blockpos_min.Z);
101         assert(data->blockpos_requested.X <= data->blockpos_max.X &&
102                    data->blockpos_requested.Y <= data->blockpos_max.Y &&
103                    data->blockpos_requested.Z <= data->blockpos_max.Z);
104
105         this->generating = true;
106
107         this->data    = data;
108         this->vmanip  = data->vmanip;
109         v3s16 em = vmanip->m_area.getExtent();
110         this->ystride = em.X;
111         this->zstride = em.Y * em.X;
112
113         node_min = (data->blockpos_min) * MAP_BLOCKSIZE;
114         node_max = (data->blockpos_max + v3s16(1, 1, 1)) * MAP_BLOCKSIZE - v3s16(1, 1, 1);
115         v3s16 full_node_min = (data->blockpos_min - 1) * MAP_BLOCKSIZE;
116         v3s16 full_node_max = (data->blockpos_max + 2) * MAP_BLOCKSIZE - v3s16(1,1,1);
117
118         int y1 = node_min.Y;
119         int y2 = node_max.Y;
120         int x  = node_min.X;
121         int z  = node_min.Z;
122
123         TimeTaker timer("Generating terrain");
124         map_terrain  = noise_terrain->perlinMap2D(x, z);
125         map_bgroup   = noise_bgroup->perlinMap2D(x, z);
126         map_heat     = noise_heat->perlinMap2D(x, z);
127         map_humidity = noise_humidity->perlinMap2D(x, z);
128
129         noise_bgroup->transformNoiseMap();
130         noise_heat->transformNoiseMap();
131         noise_humidity->transformNoiseMap();
132
133         int i = 0;
134         for (z = node_min.Z; z <= node_max.Z; z++) {
135                 for (x = node_min.X; x <= node_max.X; x++) {
136                         Biome *biome = biomedef->getBiome(map_bgroup[i], map_heat[i], map_humidity[i]);
137                         biome->genColumn(this, x, z, y1, y2);
138                         i++;
139                 }
140         }
141         timer.stop();
142
143         //genCave();
144         //genDungeon();
145         //add blobs of dirt and gravel underground
146         //decorateChunk();
147         updateLiquid(full_node_min, full_node_max);
148         updateLighting(node_min, node_max);
149
150         this->generating = false;
151 }
152
153
154 void MapgenV7::updateLiquid(v3s16 nmin, v3s16 nmax) {
155         bool isliquid, wasliquid;
156         u32 i;
157
158         for (s16 z = nmin.Z; z <= nmax.Z; z++) {
159                 for (s16 x = nmin.X; x <= nmax.X; x++) {
160                         v2s16 p2d(x, z);
161                         wasliquid = true;
162                         v3s16 em  = vmanip->m_area.getExtent();
163                         i = vmanip->m_area.index(v3s16(p2d.X, nmax.Y, p2d.Y));
164
165                         for (s16 y = nmax.Y; y >= nmin.Y; y--) {
166                                 isliquid = ndef->get(vmanip->m_data[i]).isLiquid();
167                                 //there was a change between liquid and nonliquid, add to queue
168                                 if (isliquid != wasliquid)
169                                         data->transforming_liquid.push_back(v3s16(p2d.X, y, p2d.Y));
170
171                                 wasliquid = isliquid;
172                                 vmanip->m_area.add_y(em, i, -1);
173                         }
174                 }
175         }
176 }
177
178
179 void MapgenV7::updateLighting(v3s16 nmin, v3s16 nmax) {
180         enum LightBank banks[2] = {LIGHTBANK_DAY, LIGHTBANK_NIGHT};
181
182         VoxelArea a(nmin - v3s16(1,0,1) * MAP_BLOCKSIZE,
183                                 nmax + v3s16(1,0,1) * MAP_BLOCKSIZE);
184         bool block_is_underground = (water_level > nmax.Y);
185         bool sunlight = !block_is_underground;
186
187         ScopeProfiler sp(g_profiler, "EmergeThread: mapgen lighting update", SPT_AVG);
188         for (int i = 0; i < 2; i++) {
189                 enum LightBank bank = banks[i];
190         core::map<v3s16, bool> light_sources;
191         core::map<v3s16, u8> unlight_from;
192
193                 voxalgo::clearLightAndCollectSources(*vmanip, a, bank, ndef,
194                                         light_sources, unlight_from);
195                 voxalgo::propagateSunlight(*vmanip, a, sunlight, light_sources, ndef);
196         //printf("light_sources: %d\t\tunlight_from: %d\n", light_sources.size(), unlight_from.size());
197                 vmanip->unspreadLight(bank, unlight_from, light_sources, ndef);
198                 vmanip->spreadLight(bank, light_sources, ndef);
199         }
200 }
201
202
203 EmergeManager::EmergeManager(IGameDef *gamedef, int mg_version) {
204         this->mg_version = mg_version;
205         this->biomedef   = new BiomeDefManager(gamedef);
206
207         this->params     = NULL;
208         setMapgenParams();
209 }
210
211
212 EmergeManager::~EmergeManager() {
213         delete biomedef;
214         delete params;
215 }
216
217
218 Mapgen *EmergeManager::getMapgen() {
219         if (!mapgen) {
220                 switch (mg_version) {
221                         case 6:
222                                 mapgen = new MapgenV6(0, params);
223                                 break;
224                         case 7:
225                                 mapgen = new MapgenV7(biomedef, 0, params);
226                                 break;
227                         default:
228                                 errorstream << "EmergeManager: Unsupported mapgen version "
229                                         << mg_version << ", falling back to V6" << std::endl;
230                                 mg_version = 6;
231                                 mapgen = new MapgenV6(0, mgv6params);
232                 }
233         }
234         return mapgen;
235 }
236
237
238 void EmergeManager::setMapgenParams() {
239         if (params)
240                 delete params;
241
242         switch (mg_version) {
243                 case 6:
244                         params = new MapgenV6Params();
245                         break;
246                 case 7:
247                         params = new MapgenV7Params();
248                         break;
249                 default: //////do something here!
250                         ;
251         }
252 }
253
254
255 void EmergeManager::addBlockToQueue() {
256
257 }
258
259
260 Biome *EmergeManager::getBiomeAtPoint(v3s16 p) {
261         float bgroup   = NoisePerlin2D(np_bgroup,   p.X, p.Y, seed);
262         float heat     = NoisePerlin2D(np_heat,     p.X, p.Y, seed);
263         float humidity = NoisePerlin2D(np_humidity, p.X, p.Y, seed);
264         return biomedef->getBiome(bgroup, heat, humidity);
265 }
266
267
268 //FIXME:  This assumes y == 0, that is, always in a non-hell/non-sky biome
269 int EmergeManager::getGroundLevelAtPoint(v2s16 p) {
270         float terrain = NoisePerlin2D(np_terrain, p.X, p.Y, seed);
271         Biome *biome = getBiomeAtPoint(v3s16(p.X, p.Y, 0));
272         return biome->getSurfaceHeight(terrain);
273 }
274
275
276 bool EmergeManager::isBlockUnderground(v3s16 blockpos) {
277         /*
278         v2s16 p = v2s16((blockpos.X * MAP_BLOCKSIZE) + MAP_BLOCKSIZE / 2,
279                                         (blockpos.Y * MAP_BLOCKSIZE) + MAP_BLOCKSIZE / 2);
280         int ground_level = getGroundLevelAtPoint(p);
281         return blockpos.Y * (MAP_BLOCKSIZE + 1) <= min(water_level, ground_level);
282         */
283
284         //yuck, but then again, should i bother being accurate?
285         //the height of the nodes in a single block is quite variable
286         return blockpos.Y * (MAP_BLOCKSIZE + 1) <= water_level;
287 }
288
289
290 u32 EmergeManager::getBlockSeed(v3s16 p) {
291         return (u32)(seed & 0xFFFFFFFF) +
292                 p.Z * 38134234 +
293                 p.Y * 42123 +
294                 p.Y * 23;
295 }
296
297
298 /////////////////////////////////// legacy static functions for farmesh
299
300
301 s16 Mapgen::find_ground_level_from_noise(u64 seed, v2s16 p2d, s16 precision) {
302         //just need to return something
303         s16 level = 5;
304         return level;
305 }
306
307
308 bool Mapgen::get_have_beach(u64 seed, v2s16 p2d) {
309         double sandnoise = noise2d_perlin(
310                         0.2+(float)p2d.X/250, 0.7+(float)p2d.Y/250,
311                         seed+59420, 3, 0.50);
312
313         return (sandnoise > 0.15);
314 }
315
316
317 double Mapgen::tree_amount_2d(u64 seed, v2s16 p) {
318         double noise = noise2d_perlin(
319                         0.5+(float)p.X/125, 0.5+(float)p.Y/125,
320                         seed+2, 4, 0.66);
321         double zeroval = -0.39;
322         if(noise < zeroval)
323                 return 0;
324         else
325                 return 0.04 * (noise-zeroval) / (1.0-zeroval);
326 }
327
328
329 #if 0 /// BIG COMMENT
330 namespace mapgen
331 {
332
333 /*
334         Some helper functions for the map generator
335 */
336
337 #if 1
338 // Returns Y one under area minimum if not found
339 static s16 find_ground_level(VoxelManipulator &vmanip, v2s16 p2d,
340                 INodeDefManager *ndef)
341 {
342         v3s16 em = vmanip.m_area.getExtent();
343         s16 y_nodes_max = vmanip.m_area.MaxEdge.Y;
344         s16 y_nodes_min = vmanip.m_area.MinEdge.Y;
345         u32 i = vmanip.m_area.index(v3s16(p2d.X, y_nodes_max, p2d.Y));
346         s16 y;
347         for(y=y_nodes_max; y>=y_nodes_min; y--)
348         {
349                 MapNode &n = vmanip.m_data[i];
350                 if(ndef->get(n).walkable)
351                         break;
352
353                 vmanip.m_area.add_y(em, i, -1);
354         }
355         if(y >= y_nodes_min)
356                 return y;
357         else
358                 return y_nodes_min - 1;
359 }
360
361 #if 0
362 // Returns Y one under area minimum if not found
363 static s16 find_ground_level_clever(VoxelManipulator &vmanip, v2s16 p2d,
364                 INodeDefManager *ndef)
365 {
366         if(!vmanip.m_area.contains(v3s16(p2d.X, vmanip.m_area.MaxEdge.Y, p2d.Y)))
367                 return vmanip.m_area.MinEdge.Y-1;
368         v3s16 em = vmanip.m_area.getExtent();
369         s16 y_nodes_max = vmanip.m_area.MaxEdge.Y;
370         s16 y_nodes_min = vmanip.m_area.MinEdge.Y;
371         u32 i = vmanip.m_area.index(v3s16(p2d.X, y_nodes_max, p2d.Y));
372         s16 y;
373         content_t c_tree = ndef->getId("mapgen_tree");
374         content_t c_leaves = ndef->getId("mapgen_leaves");
375         for(y=y_nodes_max; y>=y_nodes_min; y--)
376         {
377                 MapNode &n = vmanip.m_data[i];
378                 if(ndef->get(n).walkable
379                                 && n.getContent() != c_tree
380                                 && n.getContent() != c_leaves)
381                         break;
382
383                 vmanip.m_area.add_y(em, i, -1);
384         }
385         if(y >= y_nodes_min)
386                 return y;
387         else
388                 return y_nodes_min - 1;
389 }
390 #endif
391
392 // Returns Y one under area minimum if not found
393 static s16 find_stone_level(VoxelManipulator &vmanip, v2s16 p2d,
394                 INodeDefManager *ndef)
395 {
396         v3s16 em = vmanip.m_area.getExtent();
397         s16 y_nodes_max = vmanip.m_area.MaxEdge.Y;
398         s16 y_nodes_min = vmanip.m_area.MinEdge.Y;
399         u32 i = vmanip.m_area.index(v3s16(p2d.X, y_nodes_max, p2d.Y));
400         s16 y;
401         content_t c_stone = ndef->getId("mapgen_stone");
402         content_t c_desert_stone = ndef->getId("mapgen_desert_stone");
403         for(y=y_nodes_max; y>=y_nodes_min; y--)
404         {
405                 MapNode &n = vmanip.m_data[i];
406                 content_t c = n.getContent();
407                 if(c != CONTENT_IGNORE && (
408                                 c == c_stone || c == c_desert_stone))
409                         break;
410
411                 vmanip.m_area.add_y(em, i, -1);
412         }
413         if(y >= y_nodes_min)
414                 return y;
415         else
416                 return y_nodes_min - 1;
417 }
418 #endif
419
420
421 #if 0
422
423 static void make_papyrus(VoxelManipulator &vmanip, v3s16 p0,
424                 INodeDefManager *ndef)
425 {
426         MapNode papyrusnode(ndef->getId("mapgen_papyrus"));
427
428         s16 trunk_h = myrand_range(2, 3);
429         v3s16 p1 = p0;
430         for(s16 ii=0; ii<trunk_h; ii++)
431         {
432                 if(vmanip.m_area.contains(p1))
433                         vmanip.m_data[vmanip.m_area.index(p1)] = papyrusnode;
434                 p1.Y++;
435         }
436 }
437
438 static void make_cactus(VoxelManipulator &vmanip, v3s16 p0,
439                 INodeDefManager *ndef)
440 {
441         MapNode cactusnode(ndef->getId("mapgen_cactus"));
442
443         s16 trunk_h = 3;
444         v3s16 p1 = p0;
445         for(s16 ii=0; ii<trunk_h; ii++)
446         {
447                 if(vmanip.m_area.contains(p1))
448                         vmanip.m_data[vmanip.m_area.index(p1)] = cactusnode;
449                 p1.Y++;
450         }
451 }
452 #endif
453
454 #if 0
455 /*
456         Dungeon making routines
457 */
458
459 #define VMANIP_FLAG_DUNGEON_INSIDE VOXELFLAG_CHECKED1
460 #define VMANIP_FLAG_DUNGEON_PRESERVE VOXELFLAG_CHECKED2
461 #define VMANIP_FLAG_DUNGEON_UNTOUCHABLE (\
462                 VMANIP_FLAG_DUNGEON_INSIDE|VMANIP_FLAG_DUNGEON_PRESERVE)
463
464 static void make_room1(VoxelManipulator &vmanip, v3s16 roomsize, v3s16 roomplace,
465                 INodeDefManager *ndef)
466 {
467         // Make +-X walls
468         for(s16 z=0; z<roomsize.Z; z++)
469         for(s16 y=0; y<roomsize.Y; y++)
470         {
471                 {
472                         v3s16 p = roomplace + v3s16(0,y,z);
473                         if(vmanip.m_area.contains(p) == false)
474                                 continue;
475                         u32 vi = vmanip.m_area.index(p);
476                         if(vmanip.m_flags[vi] & VMANIP_FLAG_DUNGEON_UNTOUCHABLE)
477                                 continue;
478                         vmanip.m_data[vi] = MapNode(ndef->getId("mapgen_cobble"));
479                 }
480                 {
481                         v3s16 p = roomplace + v3s16(roomsize.X-1,y,z);
482                         if(vmanip.m_area.contains(p) == false)
483                                 continue;
484                         u32 vi = vmanip.m_area.index(p);
485                         if(vmanip.m_flags[vi] & VMANIP_FLAG_DUNGEON_UNTOUCHABLE)
486                                 continue;
487                         vmanip.m_data[vi] = MapNode(ndef->getId("mapgen_cobble"));
488                 }
489         }
490
491         // Make +-Z walls
492         for(s16 x=0; x<roomsize.X; x++)
493         for(s16 y=0; y<roomsize.Y; y++)
494         {
495                 {
496                         v3s16 p = roomplace + v3s16(x,y,0);
497                         if(vmanip.m_area.contains(p) == false)
498                                 continue;
499                         u32 vi = vmanip.m_area.index(p);
500                         if(vmanip.m_flags[vi] & VMANIP_FLAG_DUNGEON_UNTOUCHABLE)
501                                 continue;
502                         vmanip.m_data[vi] = MapNode(ndef->getId("mapgen_cobble"));
503                 }
504                 {
505                         v3s16 p = roomplace + v3s16(x,y,roomsize.Z-1);
506                         if(vmanip.m_area.contains(p) == false)
507                                 continue;
508                         u32 vi = vmanip.m_area.index(p);
509                         if(vmanip.m_flags[vi] & VMANIP_FLAG_DUNGEON_UNTOUCHABLE)
510                                 continue;
511                         vmanip.m_data[vi] = MapNode(ndef->getId("mapgen_cobble"));
512                 }
513         }
514
515         // Make +-Y walls (floor and ceiling)
516         for(s16 z=0; z<roomsize.Z; z++)
517         for(s16 x=0; x<roomsize.X; x++)
518         {
519                 {
520                         v3s16 p = roomplace + v3s16(x,0,z);
521                         if(vmanip.m_area.contains(p) == false)
522                                 continue;
523                         u32 vi = vmanip.m_area.index(p);
524                         if(vmanip.m_flags[vi] & VMANIP_FLAG_DUNGEON_UNTOUCHABLE)
525                                 continue;
526                         vmanip.m_data[vi] = MapNode(ndef->getId("mapgen_cobble"));
527                 }
528                 {
529                         v3s16 p = roomplace + v3s16(x,roomsize.Y-1,z);
530                         if(vmanip.m_area.contains(p) == false)
531                                 continue;
532                         u32 vi = vmanip.m_area.index(p);
533                         if(vmanip.m_flags[vi] & VMANIP_FLAG_DUNGEON_UNTOUCHABLE)
534                                 continue;
535                         vmanip.m_data[vi] = MapNode(ndef->getId("mapgen_cobble"));
536                 }
537         }
538
539         // Fill with air
540         for(s16 z=1; z<roomsize.Z-1; z++)
541         for(s16 y=1; y<roomsize.Y-1; y++)
542         for(s16 x=1; x<roomsize.X-1; x++)
543         {
544                 v3s16 p = roomplace + v3s16(x,y,z);
545                 if(vmanip.m_area.contains(p) == false)
546                         continue;
547                 u32 vi = vmanip.m_area.index(p);
548                 vmanip.m_flags[vi] |= VMANIP_FLAG_DUNGEON_UNTOUCHABLE;
549                 vmanip.m_data[vi] = MapNode(CONTENT_AIR);
550         }
551 }
552
553 static void make_fill(VoxelManipulator &vmanip, v3s16 place, v3s16 size,
554                 u8 avoid_flags, MapNode n, u8 or_flags)
555 {
556         for(s16 z=0; z<size.Z; z++)
557         for(s16 y=0; y<size.Y; y++)
558         for(s16 x=0; x<size.X; x++)
559         {
560                 v3s16 p = place + v3s16(x,y,z);
561                 if(vmanip.m_area.contains(p) == false)
562                         continue;
563                 u32 vi = vmanip.m_area.index(p);
564                 if(vmanip.m_flags[vi] & avoid_flags)
565                         continue;
566                 vmanip.m_flags[vi] |= or_flags;
567                 vmanip.m_data[vi] = n;
568         }
569 }
570
571 static void make_hole1(VoxelManipulator &vmanip, v3s16 place,
572                 INodeDefManager *ndef)
573 {
574         make_fill(vmanip, place, v3s16(1,2,1), 0, MapNode(CONTENT_AIR),
575                         VMANIP_FLAG_DUNGEON_INSIDE);
576 }
577
578 static void make_door1(VoxelManipulator &vmanip, v3s16 doorplace, v3s16 doordir,
579                 INodeDefManager *ndef)
580 {
581         make_hole1(vmanip, doorplace, ndef);
582         // Place torch (for testing)
583         //vmanip.m_data[vmanip.m_area.index(doorplace)] = MapNode(ndef->getId("mapgen_torch"));
584 }
585
586 static v3s16 rand_ortho_dir(PseudoRandom &random)
587 {
588         if(random.next()%2==0)
589                 return random.next()%2 ? v3s16(-1,0,0) : v3s16(1,0,0);
590         else
591                 return random.next()%2 ? v3s16(0,0,-1) : v3s16(0,0,1);
592 }
593
594 static v3s16 turn_xz(v3s16 olddir, int t)
595 {
596         v3s16 dir;
597         if(t == 0)
598         {
599                 // Turn right
600                 dir.X = olddir.Z;
601                 dir.Z = -olddir.X;
602                 dir.Y = olddir.Y;
603         }
604         else
605         {
606                 // Turn left
607                 dir.X = -olddir.Z;
608                 dir.Z = olddir.X;
609                 dir.Y = olddir.Y;
610         }
611         return dir;
612 }
613
614 static v3s16 random_turn(PseudoRandom &random, v3s16 olddir)
615 {
616         int turn = random.range(0,2);
617         v3s16 dir;
618         if(turn == 0)
619         {
620                 // Go straight
621                 dir = olddir;
622         }
623         else if(turn == 1)
624                 // Turn right
625                 dir = turn_xz(olddir, 0);
626         else
627                 // Turn left
628                 dir = turn_xz(olddir, 1);
629         return dir;
630 }
631
632 static void make_corridor(VoxelManipulator &vmanip, v3s16 doorplace,
633                 v3s16 doordir, v3s16 &result_place, v3s16 &result_dir,
634                 PseudoRandom &random, INodeDefManager *ndef)
635 {
636         make_hole1(vmanip, doorplace, ndef);
637         v3s16 p0 = doorplace;
638         v3s16 dir = doordir;
639         u32 length;
640         if(random.next()%2)
641                 length = random.range(1,13);
642         else
643                 length = random.range(1,6);
644         length = random.range(1,13);
645         u32 partlength = random.range(1,13);
646         u32 partcount = 0;
647         s16 make_stairs = 0;
648         if(random.next()%2 == 0 && partlength >= 3)
649                 make_stairs = random.next()%2 ? 1 : -1;
650         for(u32 i=0; i<length; i++)
651         {
652                 v3s16 p = p0 + dir;
653                 if(partcount != 0)
654                         p.Y += make_stairs;
655
656                 /*// If already empty
657                 if(vmanip.getNodeNoExNoEmerge(p).getContent()
658                                 == CONTENT_AIR
659                 && vmanip.getNodeNoExNoEmerge(p+v3s16(0,1,0)).getContent()
660                                 == CONTENT_AIR)
661                 {
662                 }*/
663
664                 if(vmanip.m_area.contains(p) == true
665                                 && vmanip.m_area.contains(p+v3s16(0,1,0)) == true)
666                 {
667                         if(make_stairs)
668                         {
669                                 make_fill(vmanip, p+v3s16(-1,-1,-1), v3s16(3,5,3),
670                                                 VMANIP_FLAG_DUNGEON_UNTOUCHABLE, MapNode(ndef->getId("mapgen_cobble")), 0);
671                                 make_fill(vmanip, p, v3s16(1,2,1), 0, MapNode(CONTENT_AIR),
672                                                 VMANIP_FLAG_DUNGEON_INSIDE);
673                                 make_fill(vmanip, p-dir, v3s16(1,2,1), 0, MapNode(CONTENT_AIR),
674                                                 VMANIP_FLAG_DUNGEON_INSIDE);
675                         }
676                         else
677                         {
678                                 make_fill(vmanip, p+v3s16(-1,-1,-1), v3s16(3,4,3),
679                                                 VMANIP_FLAG_DUNGEON_UNTOUCHABLE, MapNode(ndef->getId("mapgen_cobble")), 0);
680                                 make_hole1(vmanip, p, ndef);
681                                 /*make_fill(vmanip, p, v3s16(1,2,1), 0, MapNode(CONTENT_AIR),
682                                                 VMANIP_FLAG_DUNGEON_INSIDE);*/
683                         }
684
685                         p0 = p;
686                 }
687                 else
688                 {
689                         // Can't go here, turn away
690                         dir = turn_xz(dir, random.range(0,1));
691                         make_stairs = -make_stairs;
692                         partcount = 0;
693                         partlength = random.range(1,length);
694                         continue;
695                 }
696
697                 partcount++;
698                 if(partcount >= partlength)
699                 {
700                         partcount = 0;
701
702                         dir = random_turn(random, dir);
703
704                         partlength = random.range(1,length);
705
706                         make_stairs = 0;
707                         if(random.next()%2 == 0 && partlength >= 3)
708                                 make_stairs = random.next()%2 ? 1 : -1;
709                 }
710         }
711         result_place = p0;
712         result_dir = dir;
713 }
714
715 class RoomWalker
716 {
717 public:
718
719         RoomWalker(VoxelManipulator &vmanip_, v3s16 pos, PseudoRandom &random,
720                         INodeDefManager *ndef):
721                         vmanip(vmanip_),
722                         m_pos(pos),
723                         m_random(random),
724                         m_ndef(ndef)
725         {
726                 randomizeDir();
727         }
728
729         void randomizeDir()
730         {
731                 m_dir = rand_ortho_dir(m_random);
732         }
733
734         void setPos(v3s16 pos)
735         {
736                 m_pos = pos;
737         }
738
739         void setDir(v3s16 dir)
740         {
741                 m_dir = dir;
742         }
743
744         bool findPlaceForDoor(v3s16 &result_place, v3s16 &result_dir)
745         {
746                 for(u32 i=0; i<100; i++)
747                 {
748                         v3s16 p = m_pos + m_dir;
749                         v3s16 p1 = p + v3s16(0,1,0);
750                         if(vmanip.m_area.contains(p) == false
751                                         || vmanip.m_area.contains(p1) == false
752                                         || i % 4 == 0)
753                         {
754                                 randomizeDir();
755                                 continue;
756                         }
757                         if(vmanip.getNodeNoExNoEmerge(p).getContent()
758                                         == m_ndef->getId("mapgen_cobble")
759                         && vmanip.getNodeNoExNoEmerge(p1).getContent()
760                                         == m_ndef->getId("mapgen_cobble"))
761                         {
762                                 // Found wall, this is a good place!
763                                 result_place = p;
764                                 result_dir = m_dir;
765                                 // Randomize next direction
766                                 randomizeDir();
767                                 return true;
768                         }
769                         /*
770                                 Determine where to move next
771                         */
772                         // Jump one up if the actual space is there
773                         if(vmanip.getNodeNoExNoEmerge(p+v3s16(0,0,0)).getContent()
774                                         == m_ndef->getId("mapgen_cobble")
775                         && vmanip.getNodeNoExNoEmerge(p+v3s16(0,1,0)).getContent()
776                                         == CONTENT_AIR
777                         && vmanip.getNodeNoExNoEmerge(p+v3s16(0,2,0)).getContent()
778                                         == CONTENT_AIR)
779                                 p += v3s16(0,1,0);
780                         // Jump one down if the actual space is there
781                         if(vmanip.getNodeNoExNoEmerge(p+v3s16(0,1,0)).getContent()
782                                         == m_ndef->getId("mapgen_cobble")
783                         && vmanip.getNodeNoExNoEmerge(p+v3s16(0,0,0)).getContent()
784                                         == CONTENT_AIR
785                         && vmanip.getNodeNoExNoEmerge(p+v3s16(0,-1,0)).getContent()
786                                         == CONTENT_AIR)
787                                 p += v3s16(0,-1,0);
788                         // Check if walking is now possible
789                         if(vmanip.getNodeNoExNoEmerge(p).getContent()
790                                         != CONTENT_AIR
791                         || vmanip.getNodeNoExNoEmerge(p+v3s16(0,1,0)).getContent()
792                                         != CONTENT_AIR)
793                         {
794                                 // Cannot continue walking here
795                                 randomizeDir();
796                                 continue;
797                         }
798                         // Move there
799                         m_pos = p;
800                 }
801                 return false;
802         }
803
804         bool findPlaceForRoomDoor(v3s16 roomsize, v3s16 &result_doorplace,
805                         v3s16 &result_doordir, v3s16 &result_roomplace)
806         {
807                 for(s16 trycount=0; trycount<30; trycount++)
808                 {
809                         v3s16 doorplace;
810                         v3s16 doordir;
811                         bool r = findPlaceForDoor(doorplace, doordir);
812                         if(r == false)
813                                 continue;
814                         v3s16 roomplace;
815                         // X east, Z north, Y up
816 #if 1
817                         if(doordir == v3s16(1,0,0)) // X+
818                                 roomplace = doorplace +
819                                                 v3s16(0,-1,m_random.range(-roomsize.Z+2,-2));
820                         if(doordir == v3s16(-1,0,0)) // X-
821                                 roomplace = doorplace +
822                                                 v3s16(-roomsize.X+1,-1,m_random.range(-roomsize.Z+2,-2));
823                         if(doordir == v3s16(0,0,1)) // Z+
824                                 roomplace = doorplace +
825                                                 v3s16(m_random.range(-roomsize.X+2,-2),-1,0);
826                         if(doordir == v3s16(0,0,-1)) // Z-
827                                 roomplace = doorplace +
828                                                 v3s16(m_random.range(-roomsize.X+2,-2),-1,-roomsize.Z+1);
829 #endif
830 #if 0
831                         if(doordir == v3s16(1,0,0)) // X+
832                                 roomplace = doorplace + v3s16(0,-1,-roomsize.Z/2);
833                         if(doordir == v3s16(-1,0,0)) // X-
834                                 roomplace = doorplace + v3s16(-roomsize.X+1,-1,-roomsize.Z/2);
835                         if(doordir == v3s16(0,0,1)) // Z+
836                                 roomplace = doorplace + v3s16(-roomsize.X/2,-1,0);
837                         if(doordir == v3s16(0,0,-1)) // Z-
838                                 roomplace = doorplace + v3s16(-roomsize.X/2,-1,-roomsize.Z+1);
839 #endif
840
841                         // Check fit
842                         bool fits = true;
843                         for(s16 z=1; z<roomsize.Z-1; z++)
844                         for(s16 y=1; y<roomsize.Y-1; y++)
845                         for(s16 x=1; x<roomsize.X-1; x++)
846                         {
847                                 v3s16 p = roomplace + v3s16(x,y,z);
848                                 if(vmanip.m_area.contains(p) == false)
849                                 {
850                                         fits = false;
851                                         break;
852                                 }
853                                 if(vmanip.m_flags[vmanip.m_area.index(p)]
854                                                 & VMANIP_FLAG_DUNGEON_INSIDE)
855                                 {
856                                         fits = false;
857                                         break;
858                                 }
859                         }
860                         if(fits == false)
861                         {
862                                 // Find new place
863                                 continue;
864                         }
865                         result_doorplace = doorplace;
866                         result_doordir = doordir;
867                         result_roomplace = roomplace;
868                         return true;
869                 }
870                 return false;
871         }
872
873 private:
874         VoxelManipulator &vmanip;
875         v3s16 m_pos;
876         v3s16 m_dir;
877         PseudoRandom &m_random;
878         INodeDefManager *m_ndef;
879 };
880
881 static void make_dungeon1(VoxelManipulator &vmanip, PseudoRandom &random,
882                 INodeDefManager *ndef)
883 {
884         v3s16 areasize = vmanip.m_area.getExtent();
885         v3s16 roomsize;
886         v3s16 roomplace;
887
888         /*
889                 Find place for first room
890         */
891         bool fits = false;
892         for(u32 i=0; i<100; i++)
893         {
894                 roomsize = v3s16(random.range(4,8),random.range(4,6),random.range(4,8));
895                 roomplace = vmanip.m_area.MinEdge + v3s16(
896                                 random.range(0,areasize.X-roomsize.X-1),
897                                 random.range(0,areasize.Y-roomsize.Y-1),
898                                 random.range(0,areasize.Z-roomsize.Z-1));
899                 /*
900                         Check that we're not putting the room to an unknown place,
901                         otherwise it might end up floating in the air
902                 */
903                 fits = true;
904                 for(s16 z=1; z<roomsize.Z-1; z++)
905                 for(s16 y=1; y<roomsize.Y-1; y++)
906                 for(s16 x=1; x<roomsize.X-1; x++)
907                 {
908                         v3s16 p = roomplace + v3s16(x,y,z);
909                         u32 vi = vmanip.m_area.index(p);
910                         if(vmanip.m_flags[vi] & VMANIP_FLAG_DUNGEON_INSIDE)
911                         {
912                                 fits = false;
913                                 break;
914                         }
915                         if(vmanip.m_data[vi].getContent() == CONTENT_IGNORE)
916                         {
917                                 fits = false;
918                                 break;
919                         }
920                 }
921                 if(fits)
922                         break;
923         }
924         // No place found
925         if(fits == false)
926                 return;
927
928         /*
929                 Stores the center position of the last room made, so that
930                 a new corridor can be started from the last room instead of
931                 the new room, if chosen so.
932         */
933         v3s16 last_room_center = roomplace+v3s16(roomsize.X/2,1,roomsize.Z/2);
934
935         u32 room_count = random.range(2,7);
936         for(u32 i=0; i<room_count; i++)
937         {
938                 // Make a room to the determined place
939                 make_room1(vmanip, roomsize, roomplace, ndef);
940
941                 v3s16 room_center = roomplace + v3s16(roomsize.X/2,1,roomsize.Z/2);
942
943                 // Place torch at room center (for testing)
944                 //vmanip.m_data[vmanip.m_area.index(room_center)] = MapNode(ndef->getId("mapgen_torch"));
945
946                 // Quit if last room
947                 if(i == room_count-1)
948                         break;
949
950                 // Determine walker start position
951
952                 bool start_in_last_room = (random.range(0,2)!=0);
953                 //bool start_in_last_room = true;
954
955                 v3s16 walker_start_place;
956
957                 if(start_in_last_room)
958                 {
959                         walker_start_place = last_room_center;
960                 }
961                 else
962                 {
963                         walker_start_place = room_center;
964                         // Store center of current room as the last one
965                         last_room_center = room_center;
966                 }
967
968                 // Create walker and find a place for a door
969                 RoomWalker walker(vmanip, walker_start_place, random, ndef);
970                 v3s16 doorplace;
971                 v3s16 doordir;
972                 bool r = walker.findPlaceForDoor(doorplace, doordir);
973                 if(r == false)
974                         return;
975
976                 if(random.range(0,1)==0)
977                         // Make the door
978                         make_door1(vmanip, doorplace, doordir, ndef);
979                 else
980                         // Don't actually make a door
981                         doorplace -= doordir;
982
983                 // Make a random corridor starting from the door
984                 v3s16 corridor_end;
985                 v3s16 corridor_end_dir;
986                 make_corridor(vmanip, doorplace, doordir, corridor_end,
987                                 corridor_end_dir, random, ndef);
988
989                 // Find a place for a random sized room
990                 roomsize = v3s16(random.range(4,8),random.range(4,6),random.range(4,8));
991                 walker.setPos(corridor_end);
992                 walker.setDir(corridor_end_dir);
993                 r = walker.findPlaceForRoomDoor(roomsize, doorplace, doordir, roomplace);
994                 if(r == false)
995                         return;
996
997                 if(random.range(0,1)==0)
998                         // Make the door
999                         make_door1(vmanip, doorplace, doordir, ndef);
1000                 else
1001                         // Don't actually make a door
1002                         roomplace -= doordir;
1003
1004         }
1005 }
1006 #endif
1007
1008 #if 0
1009 static void make_nc(VoxelManipulator &vmanip, PseudoRandom &random,
1010                 INodeDefManager *ndef)
1011 {
1012         v3s16 dir;
1013         u8 facedir_i = 0;
1014         s32 r = random.range(0, 3);
1015         if(r == 0){
1016                 dir = v3s16( 1, 0, 0);
1017                 facedir_i = 3;
1018         }
1019         if(r == 1){
1020                 dir = v3s16(-1, 0, 0);
1021                 facedir_i = 1;
1022         }
1023         if(r == 2){
1024                 dir = v3s16( 0, 0, 1);
1025                 facedir_i = 2;
1026         }
1027         if(r == 3){
1028                 dir = v3s16( 0, 0,-1);
1029                 facedir_i = 0;
1030         }
1031         v3s16 p = vmanip.m_area.MinEdge + v3s16(
1032                         16+random.range(0,15),
1033                         16+random.range(0,15),
1034                         16+random.range(0,15));
1035         vmanip.m_data[vmanip.m_area.index(p)] = MapNode(ndef->getId("mapgen_nyancat"), facedir_i);
1036         u32 length = random.range(3,15);
1037         for(u32 j=0; j<length; j++)
1038         {
1039                 p -= dir;
1040                 vmanip.m_data[vmanip.m_area.index(p)] = MapNode(ndef->getId("mapgen_nyancat_rainbow"));
1041         }
1042 }
1043 #endif
1044
1045 /*
1046         Noise functions. Make sure seed is mangled differently in each one.
1047 */
1048
1049 #if 0
1050 /*
1051         Scaling the output of the noise function affects the overdrive of the
1052         contour function, which affects the shape of the output considerably.
1053 */
1054 #define CAVE_NOISE_SCALE 12.0
1055 //#define CAVE_NOISE_SCALE 10.0
1056 //#define CAVE_NOISE_SCALE 7.5
1057 //#define CAVE_NOISE_SCALE 5.0
1058 //#define CAVE_NOISE_SCALE 1.0
1059
1060 //#define CAVE_NOISE_THRESHOLD (2.5/CAVE_NOISE_SCALE)
1061 #define CAVE_NOISE_THRESHOLD (1.5/CAVE_NOISE_SCALE)
1062
1063 NoiseParams get_cave_noise1_params(u64 seed)
1064 {
1065         /*return NoiseParams(NOISE_PERLIN_CONTOUR, seed+52534, 5, 0.7,
1066                         200, CAVE_NOISE_SCALE);*/
1067         /*return NoiseParams(NOISE_PERLIN_CONTOUR, seed+52534, 4, 0.7,
1068                         100, CAVE_NOISE_SCALE);*/
1069         /*return NoiseParams(NOISE_PERLIN_CONTOUR, seed+52534, 5, 0.6,
1070                         100, CAVE_NOISE_SCALE);*/
1071         /*return NoiseParams(NOISE_PERLIN_CONTOUR, seed+52534, 5, 0.3,
1072                         100, CAVE_NOISE_SCALE);*/
1073         return NoiseParams(NOISE_PERLIN_CONTOUR, seed+52534, 4, 0.5,
1074                         50, CAVE_NOISE_SCALE);
1075         //return NoiseParams(NOISE_CONSTANT_ONE);
1076 }
1077
1078 NoiseParams get_cave_noise2_params(u64 seed)
1079 {
1080         /*return NoiseParams(NOISE_PERLIN_CONTOUR_FLIP_YZ, seed+10325, 5, 0.7,
1081                         200, CAVE_NOISE_SCALE);*/
1082         /*return NoiseParams(NOISE_PERLIN_CONTOUR_FLIP_YZ, seed+10325, 4, 0.7,
1083                         100, CAVE_NOISE_SCALE);*/
1084         /*return NoiseParams(NOISE_PERLIN_CONTOUR_FLIP_YZ, seed+10325, 5, 0.3,
1085                         100, CAVE_NOISE_SCALE);*/
1086         return NoiseParams(NOISE_PERLIN_CONTOUR_FLIP_YZ, seed+10325, 4, 0.5,
1087                         50, CAVE_NOISE_SCALE);
1088         //return NoiseParams(NOISE_CONSTANT_ONE);
1089 }
1090
1091 NoiseParams get_ground_noise1_params(u64 seed)
1092 {
1093         return NoiseParams(NOISE_PERLIN, seed+983240, 4,
1094                         0.55, 80.0, 40.0);
1095 }
1096
1097 NoiseParams get_ground_crumbleness_params(u64 seed)
1098 {
1099         return NoiseParams(NOISE_PERLIN, seed+34413, 3,
1100                         1.3, 20.0, 1.0);
1101 }
1102
1103 NoiseParams get_ground_wetness_params(u64 seed)
1104 {
1105         return NoiseParams(NOISE_PERLIN, seed+32474, 4,
1106                         1.1, 40.0, 1.0);
1107 }
1108
1109 bool is_cave(u64 seed, v3s16 p)
1110 {
1111         double d1 = noise3d_param(get_cave_noise1_params(seed), p.X,p.Y,p.Z);
1112         double d2 = noise3d_param(get_cave_noise2_params(seed), p.X,p.Y,p.Z);
1113         return d1*d2 > CAVE_NOISE_THRESHOLD;
1114 }
1115
1116 /*
1117         Ground density noise shall be interpreted by using this.
1118
1119         TODO: No perlin noises here, they should be outsourced
1120               and buffered
1121                   NOTE: The speed of these actually isn't terrible
1122 */
1123 bool val_is_ground(double ground_noise1_val, v3s16 p, u64 seed)
1124 {
1125         //return ((double)p.Y < ground_noise1_val);
1126
1127         double f = 0.55 + noise2d_perlin(
1128                         0.5+(float)p.X/250, 0.5+(float)p.Z/250,
1129                         seed+920381, 3, 0.45);
1130         if(f < 0.01)
1131                 f = 0.01;
1132         else if(f >= 1.0)
1133                 f *= 1.6;
1134         double h = WATER_LEVEL + 10 * noise2d_perlin(
1135                         0.5+(float)p.X/250, 0.5+(float)p.Z/250,
1136                         seed+84174, 4, 0.5);
1137         /*double f = 1;
1138         double h = 0;*/
1139         return ((double)p.Y - h < ground_noise1_val * f);
1140 }
1141
1142 /*
1143         Queries whether a position is ground or not.
1144 */
1145 bool is_ground(u64 seed, v3s16 p)
1146 {
1147         double val1 = noise3d_param(get_ground_noise1_params(seed), p.X,p.Y,p.Z);
1148         return val_is_ground(val1, p, seed);
1149 }
1150 #endif
1151
1152 // Amount of trees per area in nodes
1153 double tree_amount_2d(u64 seed, v2s16 p)
1154 {
1155         /*double noise = noise2d_perlin(
1156                         0.5+(float)p.X/250, 0.5+(float)p.Y/250,
1157                         seed+2, 5, 0.66);*/
1158         double noise = noise2d_perlin(
1159                         0.5+(float)p.X/125, 0.5+(float)p.Y/125,
1160                         seed+2, 4, 0.66);
1161         double zeroval = -0.39;
1162         if(noise < zeroval)
1163                 return 0;
1164         else
1165                 return 0.04 * (noise-zeroval) / (1.0-zeroval);
1166 }
1167
1168 #if 0
1169 double surface_humidity_2d(u64 seed, v2s16 p)
1170 {
1171         double noise = noise2d_perlin(
1172                         0.5+(float)p.X/500, 0.5+(float)p.Y/500,
1173                         seed+72384, 4, 0.66);
1174         noise = (noise + 1.0)/2.0;
1175         if(noise < 0.0)
1176                 noise = 0.0;
1177         if(noise > 1.0)
1178                 noise = 1.0;
1179         return noise;
1180 }
1181
1182 /*
1183         Incrementally find ground level from 3d noise
1184 */
1185 s16 find_ground_level_from_noise(u64 seed, v2s16 p2d, s16 precision)
1186 {
1187         // Start a bit fuzzy to make averaging lower precision values
1188         // more useful
1189         s16 level = myrand_range(-precision/2, precision/2);
1190         s16 dec[] = {31000, 100, 20, 4, 1, 0};
1191         s16 i;
1192         for(i = 1; dec[i] != 0 && precision <= dec[i]; i++)
1193         {
1194                 // First find non-ground by going upwards
1195                 // Don't stop in caves.
1196                 {
1197                         s16 max = level+dec[i-1]*2;
1198                         v3s16 p(p2d.X, level, p2d.Y);
1199                         for(; p.Y < max; p.Y += dec[i])
1200                         {
1201                                 if(!is_ground(seed, p))
1202                                 {
1203                                         level = p.Y;
1204                                         break;
1205                                 }
1206                         }
1207                 }
1208                 // Then find ground by going downwards from there.
1209                 // Go in caves, too, when precision is 1.
1210                 {
1211                         s16 min = level-dec[i-1]*2;
1212                         v3s16 p(p2d.X, level, p2d.Y);
1213                         for(; p.Y>min; p.Y-=dec[i])
1214                         {
1215                                 bool ground = is_ground(seed, p);
1216                                 /*if(dec[i] == 1 && is_cave(seed, p))
1217                                         ground = false;*/
1218                                 if(ground)
1219                                 {
1220                                         level = p.Y;
1221                                         break;
1222                                 }
1223                         }
1224                 }
1225         }
1226
1227         // This is more like the actual ground level
1228         level += dec[i-1]/2;
1229
1230         return level;
1231 }
1232
1233 double get_sector_average_ground_level(u64 seed, v2s16 sectorpos, double p=4);
1234
1235 double get_sector_average_ground_level(u64 seed, v2s16 sectorpos, double p)
1236 {
1237         v2s16 node_min = sectorpos*MAP_BLOCKSIZE;
1238         v2s16 node_max = (sectorpos+v2s16(1,1))*MAP_BLOCKSIZE-v2s16(1,1);
1239         double a = 0;
1240         a += find_ground_level_from_noise(seed,
1241                         v2s16(node_min.X, node_min.Y), p);
1242         a += find_ground_level_from_noise(seed,
1243                         v2s16(node_min.X, node_max.Y), p);
1244         a += find_ground_level_from_noise(seed,
1245                         v2s16(node_max.X, node_max.Y), p);
1246         a += find_ground_level_from_noise(seed,
1247                         v2s16(node_max.X, node_min.Y), p);
1248         a += find_ground_level_from_noise(seed,
1249                         v2s16(node_min.X+MAP_BLOCKSIZE/2, node_min.Y+MAP_BLOCKSIZE/2), p);
1250         a /= 5;
1251         return a;
1252 }
1253
1254 double get_sector_maximum_ground_level(u64 seed, v2s16 sectorpos, double p=4);
1255
1256 double get_sector_maximum_ground_level(u64 seed, v2s16 sectorpos, double p)
1257 {
1258         v2s16 node_min = sectorpos*MAP_BLOCKSIZE;
1259         v2s16 node_max = (sectorpos+v2s16(1,1))*MAP_BLOCKSIZE-v2s16(1,1);
1260         double a = -31000;
1261         // Corners
1262         a = MYMAX(a, find_ground_level_from_noise(seed,
1263                         v2s16(node_min.X, node_min.Y), p));
1264         a = MYMAX(a, find_ground_level_from_noise(seed,
1265                         v2s16(node_min.X, node_max.Y), p));
1266         a = MYMAX(a, find_ground_level_from_noise(seed,
1267                         v2s16(node_max.X, node_max.Y), p));
1268         a = MYMAX(a, find_ground_level_from_noise(seed,
1269                         v2s16(node_min.X, node_min.Y), p));
1270         // Center
1271         a = MYMAX(a, find_ground_level_from_noise(seed,
1272                         v2s16(node_min.X+MAP_BLOCKSIZE/2, node_min.Y+MAP_BLOCKSIZE/2), p));
1273         // Side middle points
1274         a = MYMAX(a, find_ground_level_from_noise(seed,
1275                         v2s16(node_min.X+MAP_BLOCKSIZE/2, node_min.Y), p));
1276         a = MYMAX(a, find_ground_level_from_noise(seed,
1277                         v2s16(node_min.X+MAP_BLOCKSIZE/2, node_max.Y), p));
1278         a = MYMAX(a, find_ground_level_from_noise(seed,
1279                         v2s16(node_min.X, node_min.Y+MAP_BLOCKSIZE/2), p));
1280         a = MYMAX(a, find_ground_level_from_noise(seed,
1281                         v2s16(node_max.X, node_min.Y+MAP_BLOCKSIZE/2), p));
1282         return a;
1283 }
1284
1285 double get_sector_minimum_ground_level(u64 seed, v2s16 sectorpos, double p=4);
1286
1287 double get_sector_minimum_ground_level(u64 seed, v2s16 sectorpos, double p)
1288 {
1289         v2s16 node_min = sectorpos*MAP_BLOCKSIZE;
1290         v2s16 node_max = (sectorpos+v2s16(1,1))*MAP_BLOCKSIZE-v2s16(1,1);
1291         double a = 31000;
1292         // Corners
1293         a = MYMIN(a, find_ground_level_from_noise(seed,
1294                         v2s16(node_min.X, node_min.Y), p));
1295         a = MYMIN(a, find_ground_level_from_noise(seed,
1296                         v2s16(node_min.X, node_max.Y), p));
1297         a = MYMIN(a, find_ground_level_from_noise(seed,
1298                         v2s16(node_max.X, node_max.Y), p));
1299         a = MYMIN(a, find_ground_level_from_noise(seed,
1300                         v2s16(node_min.X, node_min.Y), p));
1301         // Center
1302         a = MYMIN(a, find_ground_level_from_noise(seed,
1303                         v2s16(node_min.X+MAP_BLOCKSIZE/2, node_min.Y+MAP_BLOCKSIZE/2), p));
1304         // Side middle points
1305         a = MYMIN(a, find_ground_level_from_noise(seed,
1306                         v2s16(node_min.X+MAP_BLOCKSIZE/2, node_min.Y), p));
1307         a = MYMIN(a, find_ground_level_from_noise(seed,
1308                         v2s16(node_min.X+MAP_BLOCKSIZE/2, node_max.Y), p));
1309         a = MYMIN(a, find_ground_level_from_noise(seed,
1310                         v2s16(node_min.X, node_min.Y+MAP_BLOCKSIZE/2), p));
1311         a = MYMIN(a, find_ground_level_from_noise(seed,
1312                         v2s16(node_max.X, node_min.Y+MAP_BLOCKSIZE/2), p));
1313         return a;
1314 }
1315 #endif
1316
1317 // Required by mapgen.h
1318 bool block_is_underground(u64 seed, v3s16 blockpos)
1319 {
1320         /*s16 minimum_groundlevel = (s16)get_sector_minimum_ground_level(
1321                         seed, v2s16(blockpos.X, blockpos.Z));*/
1322         // Nah, this is just a heuristic, just return something
1323         s16 minimum_groundlevel = WATER_LEVEL;
1324
1325         if(blockpos.Y*MAP_BLOCKSIZE + MAP_BLOCKSIZE <= minimum_groundlevel)
1326                 return true;
1327         else
1328                 return false;
1329 }
1330
1331 #define AVERAGE_MUD_AMOUNT 4
1332
1333 double base_rock_level_2d(u64 seed, v2s16 p)
1334 {
1335         // The base ground level
1336         double base = (double)WATER_LEVEL - (double)AVERAGE_MUD_AMOUNT
1337                         + 20. * noise2d_perlin(
1338                         0.5+(float)p.X/250., 0.5+(float)p.Y/250.,
1339                         seed+82341, 5, 0.6);
1340
1341         /*// A bit hillier one
1342         double base2 = WATER_LEVEL - 4.0 + 40. * noise2d_perlin(
1343                         0.5+(float)p.X/250., 0.5+(float)p.Y/250.,
1344                         seed+93413, 6, 0.69);
1345         if(base2 > base)
1346                 base = base2;*/
1347 #if 1
1348         // Higher ground level
1349         double higher = (double)WATER_LEVEL + 20. + 16. * noise2d_perlin(
1350                         0.5+(float)p.X/500., 0.5+(float)p.Y/500.,
1351                         seed+85039, 5, 0.6);
1352         //higher = 30; // For debugging
1353
1354         // Limit higher to at least base
1355         if(higher < base)
1356                 higher = base;
1357
1358         // Steepness factor of cliffs
1359         double b = 0.85 + 0.5 * noise2d_perlin(
1360                         0.5+(float)p.X/125., 0.5+(float)p.Y/125.,
1361                         seed-932, 5, 0.7);
1362         b = rangelim(b, 0.0, 1000.0);
1363         b = b*b*b*b*b*b*b;
1364         b *= 5;
1365         b = rangelim(b, 0.5, 1000.0);
1366         // Values 1.5...100 give quite horrible looking slopes
1367         if(b > 1.5 && b < 100.0){
1368                 if(b < 10.0)
1369                         b = 1.5;
1370                 else
1371                         b = 100.0;
1372         }
1373         //dstream<<"b="<<b<<std::endl;
1374         //double b = 20;
1375         //b = 0.25;
1376
1377         // Offset to more low
1378         double a_off = -0.20;
1379         // High/low selector
1380         /*double a = 0.5 + b * (a_off + noise2d_perlin(
1381                         0.5+(float)p.X/500., 0.5+(float)p.Y/500.,
1382                         seed+4213, 6, 0.7));*/
1383         double a = (double)0.5 + b * (a_off + noise2d_perlin(
1384                         0.5+(float)p.X/250., 0.5+(float)p.Y/250.,
1385                         seed+4213, 5, 0.69));
1386         // Limit
1387         a = rangelim(a, 0.0, 1.0);
1388
1389         //dstream<<"a="<<a<<std::endl;
1390
1391         double h = base*(1.0-a) + higher*a;
1392 #else
1393         double h = base;
1394 #endif
1395         return h;
1396 }
1397
1398 s16 find_ground_level_from_noise(u64 seed, v2s16 p2d, s16 precision)
1399 {
1400         return base_rock_level_2d(seed, p2d) + AVERAGE_MUD_AMOUNT;
1401 }
1402
1403 double get_mud_add_amount(u64 seed, v2s16 p)
1404 {
1405         return ((float)AVERAGE_MUD_AMOUNT + 2.0 * noise2d_perlin(
1406                         0.5+(float)p.X/200, 0.5+(float)p.Y/200,
1407                         seed+91013, 3, 0.55));
1408 }
1409
1410 bool get_have_beach(u64 seed, v2s16 p2d)
1411 {
1412         // Determine whether to have sand here
1413         double sandnoise = noise2d_perlin(
1414                         0.2+(float)p2d.X/250, 0.7+(float)p2d.Y/250,
1415                         seed+59420, 3, 0.50);
1416
1417         return (sandnoise > 0.15);
1418 }
1419
1420 enum BiomeType
1421 {
1422         BT_NORMAL,
1423         BT_DESERT
1424 };
1425
1426 BiomeType get_biome(u64 seed, v2s16 p2d)
1427 {
1428         // Just do something very simple as for now
1429         double d = noise2d_perlin(
1430                         0.6+(float)p2d.X/250, 0.2+(float)p2d.Y/250,
1431                         seed+9130, 3, 0.50);
1432         if(d > 0.45)
1433                 return BT_DESERT;
1434         if(d > 0.35 && (noise2d( p2d.X, p2d.Y, int(seed) ) + 1.0) > ( 0.45 - d ) * 20.0  )
1435                 return BT_DESERT;
1436         return BT_NORMAL;
1437 };
1438
1439 u32 get_blockseed(u64 seed, v3s16 p)
1440 {
1441         s32 x=p.X, y=p.Y, z=p.Z;
1442         return (u32)(seed%0x100000000ULL) + z*38134234 + y*42123 + x*23;
1443 }
1444
1445 #define VMANIP_FLAG_CAVE VOXELFLAG_CHECKED1
1446
1447 void make_block(BlockMakeData *data)
1448 {
1449         if(data->no_op)
1450         {
1451                 //dstream<<"makeBlock: no-op"<<std::endl;
1452                 return;
1453         }
1454
1455         assert(data->vmanip);
1456         assert(data->nodedef);
1457         assert(data->blockpos_requested.X >= data->blockpos_min.X &&
1458                         data->blockpos_requested.Y >= data->blockpos_min.Y &&
1459                         data->blockpos_requested.Z >= data->blockpos_min.Z);
1460         assert(data->blockpos_requested.X <= data->blockpos_max.X &&
1461                         data->blockpos_requested.Y <= data->blockpos_max.Y &&
1462                         data->blockpos_requested.Z <= data->blockpos_max.Z);
1463
1464         INodeDefManager *ndef = data->nodedef;
1465
1466         // Hack: use minimum block coordinates for old code that assumes
1467         // a single block
1468         v3s16 blockpos = data->blockpos_requested;
1469
1470         /*dstream<<"makeBlock(): ("<<blockpos.X<<","<<blockpos.Y<<","
1471                         <<blockpos.Z<<")"<<std::endl;*/
1472
1473         v3s16 blockpos_min = data->blockpos_min;
1474         v3s16 blockpos_max = data->blockpos_max;
1475         v3s16 blockpos_full_min = blockpos_min - v3s16(1,1,1);
1476         v3s16 blockpos_full_max = blockpos_max + v3s16(1,1,1);
1477
1478         ManualMapVoxelManipulator &vmanip = *(data->vmanip);
1479         // Area of central chunk
1480         v3s16 node_min = blockpos_min*MAP_BLOCKSIZE;
1481         v3s16 node_max = (blockpos_max+v3s16(1,1,1))*MAP_BLOCKSIZE-v3s16(1,1,1);
1482         // Full allocated area
1483         v3s16 full_node_min = (blockpos_min-1)*MAP_BLOCKSIZE;
1484         v3s16 full_node_max = (blockpos_max+2)*MAP_BLOCKSIZE-v3s16(1,1,1);
1485
1486         v3s16 central_area_size = node_max - node_min + v3s16(1,1,1);
1487
1488         const s16 max_spread_amount = MAP_BLOCKSIZE;
1489
1490         int volume_blocks = (blockpos_max.X - blockpos_min.X + 1)
1491                         * (blockpos_max.Y - blockpos_min.Y + 1)
1492                         * (blockpos_max.Z - blockpos_max.Z + 1);
1493
1494         int volume_nodes = volume_blocks *
1495                         MAP_BLOCKSIZE*MAP_BLOCKSIZE*MAP_BLOCKSIZE;
1496
1497         // Generated surface area
1498         //double gen_area_nodes = MAP_BLOCKSIZE*MAP_BLOCKSIZE * rel_volume;
1499
1500         // Horribly wrong heuristic, but better than nothing
1501         bool block_is_underground = (WATER_LEVEL > node_max.Y);
1502
1503         /*
1504                 Create a block-specific seed
1505         */
1506         u32 blockseed = get_blockseed(data->seed, full_node_min);
1507
1508         /*
1509                 Cache some ground type values for speed
1510         */
1511
1512 // Creates variables c_name=id and n_name=node
1513 #define CONTENT_VARIABLE(ndef, name)\
1514         content_t c_##name = ndef->getId("mapgen_" #name);\
1515         MapNode n_##name(c_##name);
1516 // Default to something else if was CONTENT_IGNORE
1517 #define CONTENT_VARIABLE_FALLBACK(name, dname)\
1518         if(c_##name == CONTENT_IGNORE){\
1519                 c_##name = c_##dname;\
1520                 n_##name = n_##dname;\
1521         }
1522
1523         CONTENT_VARIABLE(ndef, stone);
1524         CONTENT_VARIABLE(ndef, air);
1525         CONTENT_VARIABLE(ndef, water_source);
1526         CONTENT_VARIABLE(ndef, dirt);
1527         CONTENT_VARIABLE(ndef, sand);
1528         CONTENT_VARIABLE(ndef, gravel);
1529         CONTENT_VARIABLE(ndef, clay);
1530         CONTENT_VARIABLE(ndef, lava_source);
1531         CONTENT_VARIABLE(ndef, cobble);
1532         CONTENT_VARIABLE(ndef, mossycobble);
1533         CONTENT_VARIABLE(ndef, dirt_with_grass);
1534         CONTENT_VARIABLE(ndef, junglegrass);
1535         CONTENT_VARIABLE(ndef, stone_with_coal);
1536         CONTENT_VARIABLE(ndef, stone_with_iron);
1537         CONTENT_VARIABLE(ndef, mese);
1538         CONTENT_VARIABLE(ndef, desert_sand);
1539         CONTENT_VARIABLE_FALLBACK(desert_sand, sand);
1540         CONTENT_VARIABLE(ndef, desert_stone);
1541         CONTENT_VARIABLE_FALLBACK(desert_stone, stone);
1542
1543         // Maximum height of the stone surface and obstacles.
1544         // This is used to guide the cave generation
1545         s16 stone_surface_max_y = 0;
1546
1547         /*
1548                 Generate general ground level to full area
1549         */
1550         {
1551 #if 1
1552         TimeTaker timer1("Generating ground level");
1553
1554         for(s16 x=node_min.X; x<=node_max.X; x++)
1555         for(s16 z=node_min.Z; z<=node_max.Z; z++)
1556         {
1557                 // Node position
1558                 v2s16 p2d = v2s16(x,z);
1559
1560                 /*
1561                         Skip of already generated
1562                 */
1563                 /*{
1564                         v3s16 p(p2d.X, node_min.Y, p2d.Y);
1565                         if(vmanip.m_data[vmanip.m_area.index(p)].d != CONTENT_AIR)
1566                                 continue;
1567                 }*/
1568
1569                 // Ground height at this point
1570                 float surface_y_f = 0.0;
1571
1572                 // Use perlin noise for ground height
1573                 surface_y_f = base_rock_level_2d(data->seed, p2d);
1574
1575                 /*// Experimental stuff
1576                 {
1577                         float a = highlands_level_2d(data->seed, p2d);
1578                         if(a > surface_y_f)
1579                                 surface_y_f = a;
1580                 }*/
1581
1582                 // Convert to integer
1583                 s16 surface_y = (s16)surface_y_f;
1584
1585                 // Log it
1586                 if(surface_y > stone_surface_max_y)
1587                         stone_surface_max_y = surface_y;
1588
1589                 BiomeType bt = get_biome(data->seed, p2d);
1590                 /*
1591                         Fill ground with stone
1592                 */
1593                 {
1594                         // Use fast index incrementing
1595                         v3s16 em = vmanip.m_area.getExtent();
1596                         u32 i = vmanip.m_area.index(v3s16(p2d.X, node_min.Y, p2d.Y));
1597                         for(s16 y=node_min.Y; y<=node_max.Y; y++)
1598                         {
1599                                 if(vmanip.m_data[i].getContent() == CONTENT_IGNORE){
1600                                         if(y <= surface_y){
1601                                                 if(y > WATER_LEVEL && bt == BT_DESERT)
1602                                                         vmanip.m_data[i] = n_desert_stone;
1603                                                 else
1604                                                         vmanip.m_data[i] = n_stone;
1605                                         } else if(y <= WATER_LEVEL){
1606                                                 vmanip.m_data[i] = MapNode(c_water_source);
1607                                         } else {
1608                                                 vmanip.m_data[i] = MapNode(c_air);
1609                                         }
1610                                 }
1611                                 vmanip.m_area.add_y(em, i, 1);
1612                         }
1613                 }
1614         }
1615 #endif
1616
1617         }//timer1
1618
1619         // Limit dirt flow area by 1 because mud is flown into neighbors.
1620         assert(central_area_size.X == central_area_size.Z);
1621         s16 mudflow_minpos = 0-max_spread_amount+1;
1622         s16 mudflow_maxpos = central_area_size.X+max_spread_amount-2;
1623
1624         /*
1625                 Loop this part, it will make stuff look older and newer nicely
1626         */
1627
1628         const u32 age_loops = 2;
1629         for(u32 i_age=0; i_age<age_loops; i_age++)
1630         { // Aging loop
1631         /******************************
1632                 BEGINNING OF AGING LOOP
1633         ******************************/
1634
1635 #if 1
1636         {
1637         // 24ms @cs=8
1638         //TimeTaker timer1("caves");
1639
1640         /*
1641                 Make caves (this code is relatively horrible)
1642         */
1643         double cave_amount = 6.0 + 6.0 * noise2d_perlin(
1644                         0.5+(double)node_min.X/250, 0.5+(double)node_min.Y/250,
1645                         data->seed+34329, 3, 0.50);
1646         cave_amount = MYMAX(0.0, cave_amount);
1647         u32 caves_count = cave_amount * volume_nodes / 50000;
1648         u32 bruises_count = 1;
1649         PseudoRandom ps(blockseed+21343);
1650         PseudoRandom ps2(blockseed+1032);
1651         if(ps.range(1, 6) == 1)
1652                 bruises_count = ps.range(0, ps.range(0, 2));
1653         if(get_biome(data->seed, v2s16(node_min.X, node_min.Y)) == BT_DESERT){
1654                 caves_count /= 3;
1655                 bruises_count /= 3;
1656         }
1657         for(u32 jj=0; jj<caves_count+bruises_count; jj++)
1658         {
1659                 bool large_cave = (jj >= caves_count);
1660                 s16 min_tunnel_diameter = 2;
1661                 s16 max_tunnel_diameter = ps.range(2,6);
1662                 int dswitchint = ps.range(1,14);
1663                 u16 tunnel_routepoints = 0;
1664                 int part_max_length_rs = 0;
1665                 if(large_cave){
1666                         part_max_length_rs = ps.range(2,4);
1667                         tunnel_routepoints = ps.range(5, ps.range(15,30));
1668                         min_tunnel_diameter = 5;
1669                         max_tunnel_diameter = ps.range(7, ps.range(8,24));
1670                 } else {
1671                         part_max_length_rs = ps.range(2,9);
1672                         tunnel_routepoints = ps.range(10, ps.range(15,30));
1673                 }
1674                 bool large_cave_is_flat = (ps.range(0,1) == 0);
1675
1676                 v3f main_direction(0,0,0);
1677
1678                 // Allowed route area size in nodes
1679                 v3s16 ar = central_area_size;
1680
1681                 // Area starting point in nodes
1682                 v3s16 of = node_min;
1683
1684                 // Allow a bit more
1685                 //(this should be more than the maximum radius of the tunnel)
1686                 //s16 insure = 5; // Didn't work with max_d = 20
1687                 s16 insure = 10;
1688                 s16 more = max_spread_amount - max_tunnel_diameter/2 - insure;
1689                 ar += v3s16(1,0,1) * more * 2;
1690                 of -= v3s16(1,0,1) * more;
1691
1692                 s16 route_y_min = 0;
1693                 // Allow half a diameter + 7 over stone surface
1694                 s16 route_y_max = -of.Y + stone_surface_max_y + max_tunnel_diameter/2 + 7;
1695
1696                 /*// If caves, don't go through surface too often
1697                 if(large_cave == false)
1698                         route_y_max -= ps.range(0, max_tunnel_diameter*2);*/
1699
1700                 // Limit maximum to area
1701                 route_y_max = rangelim(route_y_max, 0, ar.Y-1);
1702
1703                 if(large_cave)
1704                 {
1705                         /*// Minimum is at y=0
1706                         route_y_min = -of.Y - 0;*/
1707                         // Minimum is at y=max_tunnel_diameter/4
1708                         //route_y_min = -of.Y + max_tunnel_diameter/4;
1709                         //s16 min = -of.Y + max_tunnel_diameter/4;
1710                         //s16 min = -of.Y + 0;
1711                         s16 min = 0;
1712                         if(node_min.Y < WATER_LEVEL && node_max.Y > WATER_LEVEL)
1713                         {
1714                                 min = WATER_LEVEL - max_tunnel_diameter/3 - of.Y;
1715                                 route_y_max = WATER_LEVEL + max_tunnel_diameter/3 - of.Y;
1716                         }
1717                         route_y_min = ps.range(min, min + max_tunnel_diameter);
1718                         route_y_min = rangelim(route_y_min, 0, route_y_max);
1719                 }
1720
1721                 /*dstream<<"route_y_min = "<<route_y_min
1722                                 <<", route_y_max = "<<route_y_max<<std::endl;*/
1723
1724                 s16 route_start_y_min = route_y_min;
1725                 s16 route_start_y_max = route_y_max;
1726
1727                 // Start every 4th cave from surface when applicable
1728                 /*bool coming_from_surface = false;
1729                 if(node_min.Y <= 0 && node_max.Y >= 0){
1730                         coming_from_surface = (jj % 4 == 0 && large_cave == false);
1731                         if(coming_from_surface)
1732                                 route_start_y_min = -of.Y + stone_surface_max_y + 10;
1733                 }*/
1734
1735                 route_start_y_min = rangelim(route_start_y_min, 0, ar.Y-1);
1736                 route_start_y_max = rangelim(route_start_y_max, route_start_y_min, ar.Y-1);
1737
1738                 // Randomize starting position
1739                 v3f orp(
1740                         (float)(ps.next()%ar.X)+0.5,
1741                         (float)(ps.range(route_start_y_min, route_start_y_max))+0.5,
1742                         (float)(ps.next()%ar.Z)+0.5
1743                 );
1744
1745                 v3s16 startp(orp.X, orp.Y, orp.Z);
1746                 startp += of;
1747
1748                 MapNode airnode(CONTENT_AIR);
1749                 MapNode waternode(c_water_source);
1750                 MapNode lavanode(c_lava_source);
1751
1752                 /*
1753                         Generate some tunnel starting from orp
1754                 */
1755
1756                 for(u16 j=0; j<tunnel_routepoints; j++)
1757                 {
1758                         if(j%dswitchint==0 && large_cave == false)
1759                         {
1760                                 main_direction = v3f(
1761                                         ((float)(ps.next()%20)-(float)10)/10,
1762                                         ((float)(ps.next()%20)-(float)10)/30,
1763                                         ((float)(ps.next()%20)-(float)10)/10
1764                                 );
1765                                 main_direction *= (float)ps.range(0, 10)/10;
1766                         }
1767
1768                         // Randomize size
1769                         s16 min_d = min_tunnel_diameter;
1770                         s16 max_d = max_tunnel_diameter;
1771                         s16 rs = ps.range(min_d, max_d);
1772
1773                         // Every second section is rough
1774                         bool randomize_xz = (ps2.range(1,2) == 1);
1775
1776                         v3s16 maxlen;
1777                         if(large_cave)
1778                         {
1779                                 maxlen = v3s16(
1780                                         rs*part_max_length_rs,
1781                                         rs*part_max_length_rs/2,
1782                                         rs*part_max_length_rs
1783                                 );
1784                         }
1785                         else
1786                         {
1787                                 maxlen = v3s16(
1788                                         rs*part_max_length_rs,
1789                                         ps.range(1, rs*part_max_length_rs),
1790                                         rs*part_max_length_rs
1791                                 );
1792                         }
1793
1794                         v3f vec;
1795
1796                         vec = v3f(
1797                                 (float)(ps.next()%(maxlen.X*1))-(float)maxlen.X/2,
1798                                 (float)(ps.next()%(maxlen.Y*1))-(float)maxlen.Y/2,
1799                                 (float)(ps.next()%(maxlen.Z*1))-(float)maxlen.Z/2
1800                         );
1801
1802                         // Jump downward sometimes
1803                         if(!large_cave && ps.range(0,12) == 0)
1804                         {
1805                                 vec = v3f(
1806                                         (float)(ps.next()%(maxlen.X*1))-(float)maxlen.X/2,
1807                                         (float)(ps.next()%(maxlen.Y*2))-(float)maxlen.Y*2/2,
1808                                         (float)(ps.next()%(maxlen.Z*1))-(float)maxlen.Z/2
1809                                 );
1810                         }
1811
1812                         /*if(large_cave){
1813                                 v3f p = orp + vec;
1814                                 s16 h = find_ground_level_clever(vmanip,
1815                                                 v2s16(p.X, p.Z), ndef);
1816                                 route_y_min = h - rs/3;
1817                                 route_y_max = h + rs;
1818                         }*/
1819
1820                         vec += main_direction;
1821
1822                         v3f rp = orp + vec;
1823                         if(rp.X < 0)
1824                                 rp.X = 0;
1825                         else if(rp.X >= ar.X)
1826                                 rp.X = ar.X-1;
1827                         if(rp.Y < route_y_min)
1828                                 rp.Y = route_y_min;
1829                         else if(rp.Y >= route_y_max)
1830                                 rp.Y = route_y_max-1;
1831                         if(rp.Z < 0)
1832                                 rp.Z = 0;
1833                         else if(rp.Z >= ar.Z)
1834                                 rp.Z = ar.Z-1;
1835                         vec = rp - orp;
1836
1837                         for(float f=0; f<1.0; f+=1.0/vec.getLength())
1838                         {
1839                                 v3f fp = orp + vec * f;
1840                                 fp.X += 0.1*ps.range(-10,10);
1841                                 fp.Z += 0.1*ps.range(-10,10);
1842                                 v3s16 cp(fp.X, fp.Y, fp.Z);
1843
1844                                 s16 d0 = -rs/2;
1845                                 s16 d1 = d0 + rs;
1846                                 if(randomize_xz){
1847                                         d0 += ps.range(-1,1);
1848                                         d1 += ps.range(-1,1);
1849                                 }
1850                                 for(s16 z0=d0; z0<=d1; z0++)
1851                                 {
1852                                         s16 si = rs/2 - MYMAX(0, abs(z0)-rs/7-1);
1853                                         for(s16 x0=-si-ps.range(0,1); x0<=si-1+ps.range(0,1); x0++)
1854                                         {
1855                                                 s16 maxabsxz = MYMAX(abs(x0), abs(z0));
1856                                                 s16 si2 = rs/2 - MYMAX(0, maxabsxz-rs/7-1);
1857                                                 for(s16 y0=-si2; y0<=si2; y0++)
1858                                                 {
1859                                                         /*// Make better floors in small caves
1860                                                         if(y0 <= -rs/2 && rs<=7)
1861                                                                 continue;*/
1862                                                         if(large_cave_is_flat){
1863                                                                 // Make large caves not so tall
1864                                                                 if(rs > 7 && abs(y0) >= rs/3)
1865                                                                         continue;
1866                                                         }
1867
1868                                                         s16 z = cp.Z + z0;
1869                                                         s16 y = cp.Y + y0;
1870                                                         s16 x = cp.X + x0;
1871                                                         v3s16 p(x,y,z);
1872                                                         p += of;
1873
1874                                                         if(vmanip.m_area.contains(p) == false)
1875                                                                 continue;
1876
1877                                                         u32 i = vmanip.m_area.index(p);
1878
1879                                                         if(large_cave)
1880                                                         {
1881                                                                 if(full_node_min.Y < WATER_LEVEL &&
1882                                                                         full_node_max.Y > WATER_LEVEL){
1883                                                                         if(p.Y <= WATER_LEVEL)
1884                                                                                 vmanip.m_data[i] = waternode;
1885                                                                         else
1886                                                                                 vmanip.m_data[i] = airnode;
1887                                                                 } else if(full_node_max.Y < WATER_LEVEL){
1888                                                                         if(p.Y < startp.Y - 2)
1889                                                                                 vmanip.m_data[i] = lavanode;
1890                                                                         else
1891                                                                                 vmanip.m_data[i] = airnode;
1892                                                                 } else {
1893                                                                         vmanip.m_data[i] = airnode;
1894                                                                 }
1895                                                         } else {
1896                                                                 // Don't replace air or water or lava or ignore
1897                                                                 if(vmanip.m_data[i].getContent() == CONTENT_IGNORE ||
1898                                                                 vmanip.m_data[i].getContent() == CONTENT_AIR ||
1899                                                                 vmanip.m_data[i].getContent() == c_water_source ||
1900                                                                 vmanip.m_data[i].getContent() == c_lava_source)
1901                                                                         continue;
1902
1903                                                                 vmanip.m_data[i] = airnode;
1904
1905                                                                 // Set tunnel flag
1906                                                                 vmanip.m_flags[i] |= VMANIP_FLAG_CAVE;
1907                                                         }
1908                                                 }
1909                                         }
1910                                 }
1911                         }
1912
1913                         orp = rp;
1914                 }
1915
1916         }
1917
1918         }//timer1
1919 #endif
1920
1921 #if 1
1922         {
1923         // 15ms @cs=8
1924         TimeTaker timer1("add mud");
1925
1926         /*
1927                 Add mud to the central chunk
1928         */
1929
1930         for(s16 x=node_min.X; x<=node_max.X; x++)
1931         for(s16 z=node_min.Z; z<=node_max.Z; z++)
1932         {
1933                 // Node position in 2d
1934                 v2s16 p2d = v2s16(x,z);
1935
1936                 // Randomize mud amount
1937                 s16 mud_add_amount = get_mud_add_amount(data->seed, p2d) / 2.0 + 0.5;
1938
1939                 // Find ground level
1940                 s16 surface_y = find_stone_level(vmanip, p2d, ndef);
1941                 // Handle area not found
1942                 if(surface_y == vmanip.m_area.MinEdge.Y - 1)
1943                         continue;
1944
1945                 MapNode addnode(c_dirt);
1946                 BiomeType bt = get_biome(data->seed, p2d);
1947
1948                 if(bt == BT_DESERT)
1949                         addnode = MapNode(c_desert_sand);
1950
1951                 if(bt == BT_DESERT && surface_y + mud_add_amount <= WATER_LEVEL+1){
1952                         addnode = MapNode(c_sand);
1953                 } else if(mud_add_amount <= 0){
1954                         mud_add_amount = 1 - mud_add_amount;
1955                         addnode = MapNode(c_gravel);
1956                 } else if(bt == BT_NORMAL && get_have_beach(data->seed, p2d) &&
1957                                 surface_y + mud_add_amount <= WATER_LEVEL+2){
1958                         addnode = MapNode(c_sand);
1959                 }
1960
1961                 if(bt == BT_DESERT){
1962                         if(surface_y > 20){
1963                                 mud_add_amount = MYMAX(0, mud_add_amount - (surface_y - 20)/5);
1964                         }
1965                 }
1966
1967                 /*
1968                         If topmost node is grass, change it to mud.
1969                         It might be if it was flown to there from a neighboring
1970                         chunk and then converted.
1971                 */
1972                 {
1973                         u32 i = vmanip.m_area.index(v3s16(p2d.X, surface_y, p2d.Y));
1974                         MapNode *n = &vmanip.m_data[i];
1975                         if(n->getContent() == c_dirt_with_grass)
1976                                 *n = MapNode(c_dirt);
1977                 }
1978
1979                 /*
1980                         Add mud on ground
1981                 */
1982                 {
1983                         s16 mudcount = 0;
1984                         v3s16 em = vmanip.m_area.getExtent();
1985                         s16 y_start = surface_y+1;
1986                         u32 i = vmanip.m_area.index(v3s16(p2d.X, y_start, p2d.Y));
1987                         for(s16 y=y_start; y<=node_max.Y; y++)
1988                         {
1989                                 if(mudcount >= mud_add_amount)
1990                                         break;
1991
1992                                 MapNode &n = vmanip.m_data[i];
1993                                 n = addnode;
1994                                 mudcount++;
1995
1996                                 vmanip.m_area.add_y(em, i, 1);
1997                         }
1998                 }
1999
2000         }
2001
2002         }//timer1
2003 #endif
2004
2005         /*
2006                 Add blobs of dirt and gravel underground
2007         */
2008         if(get_biome(data->seed, v2s16(node_min.X, node_min.Y)) == BT_NORMAL)
2009         {
2010         PseudoRandom pr(blockseed+983);
2011         for(int i=0; i<volume_nodes/10/10/10; i++)
2012         {
2013                 bool only_fill_cave = (myrand_range(0,1) != 0);
2014                 v3s16 size(
2015                         pr.range(1, 8),
2016                         pr.range(1, 8),
2017                         pr.range(1, 8)
2018                 );
2019                 v3s16 p0(
2020                         pr.range(node_min.X, node_max.X)-size.X/2,
2021                         pr.range(node_min.Y, node_max.Y)-size.Y/2,
2022                         pr.range(node_min.Z, node_max.Z)-size.Z/2
2023                 );
2024                 MapNode n1;
2025                 if(p0.Y > -32 && pr.range(0,1) == 0)
2026                         n1 = MapNode(c_dirt);
2027                 else
2028                         n1 = MapNode(c_gravel);
2029                 for(int x1=0; x1<size.X; x1++)
2030                 for(int y1=0; y1<size.Y; y1++)
2031                 for(int z1=0; z1<size.Z; z1++)
2032                 {
2033                         v3s16 p = p0 + v3s16(x1,y1,z1);
2034                         u32 i = vmanip.m_area.index(p);
2035                         if(!vmanip.m_area.contains(i))
2036                                 continue;
2037                         // Cancel if not stone and not cave air
2038                         if(vmanip.m_data[i].getContent() != c_stone &&
2039                                         !(vmanip.m_flags[i] & VMANIP_FLAG_CAVE))
2040                                 continue;
2041                         if(only_fill_cave && !(vmanip.m_flags[i] & VMANIP_FLAG_CAVE))
2042                                 continue;
2043                         vmanip.m_data[i] = n1;
2044                 }
2045         }
2046         }
2047
2048 #if 1
2049         {
2050         // 340ms @cs=8
2051         TimeTaker timer1("flow mud");
2052
2053         /*
2054                 Flow mud away from steep edges
2055         */
2056
2057         // Iterate a few times
2058         for(s16 k=0; k<3; k++)
2059         {
2060
2061         for(s16 x=mudflow_minpos; x<=mudflow_maxpos; x++)
2062         for(s16 z=mudflow_minpos; z<=mudflow_maxpos; z++)
2063         {
2064                 // Invert coordinates every 2nd iteration
2065                 if(k%2 == 0)
2066                 {
2067                         x = mudflow_maxpos - (x-mudflow_minpos);
2068                         z = mudflow_maxpos - (z-mudflow_minpos);
2069                 }
2070
2071                 // Node position in 2d
2072                 v2s16 p2d = v2s16(node_min.X, node_min.Z) + v2s16(x,z);
2073
2074                 v3s16 em = vmanip.m_area.getExtent();
2075                 u32 i = vmanip.m_area.index(v3s16(p2d.X, node_max.Y, p2d.Y));
2076                 s16 y=node_max.Y;
2077
2078                 while(y >= node_min.Y)
2079                 {
2080
2081                 for(;; y--)
2082                 {
2083                         MapNode *n = NULL;
2084                         // Find mud
2085                         for(; y>=node_min.Y; y--)
2086                         {
2087                                 n = &vmanip.m_data[i];
2088                                 //if(content_walkable(n->d))
2089                                 //      break;
2090                                 if(n->getContent() == c_dirt ||
2091                                                 n->getContent() == c_dirt_with_grass ||
2092                                                 n->getContent() == c_gravel)
2093                                         break;
2094
2095                                 vmanip.m_area.add_y(em, i, -1);
2096                         }
2097
2098                         // Stop if out of area
2099                         //if(vmanip.m_area.contains(i) == false)
2100                         if(y < node_min.Y)
2101                                 break;
2102
2103                         /*// If not mud, do nothing to it
2104                         MapNode *n = &vmanip.m_data[i];
2105                         if(n->d != CONTENT_MUD && n->d != CONTENT_GRASS)
2106                                 continue;*/
2107
2108                         if(n->getContent() == c_dirt ||
2109                                         n->getContent() == c_dirt_with_grass)
2110                         {
2111                                 // Make it exactly mud
2112                                 n->setContent(c_dirt);
2113
2114                                 /*
2115                                         Don't flow it if the stuff under it is not mud
2116                                 */
2117                                 {
2118                                         u32 i2 = i;
2119                                         vmanip.m_area.add_y(em, i2, -1);
2120                                         // Cancel if out of area
2121                                         if(vmanip.m_area.contains(i2) == false)
2122                                                 continue;
2123                                         MapNode *n2 = &vmanip.m_data[i2];
2124                                         if(n2->getContent() != c_dirt &&
2125                                                         n2->getContent() != c_dirt_with_grass)
2126                                                 continue;
2127                                 }
2128                         }
2129
2130                         /*s16 recurse_count = 0;
2131         mudflow_recurse:*/
2132
2133                         v3s16 dirs4[4] = {
2134                                 v3s16(0,0,1), // back
2135                                 v3s16(1,0,0), // right
2136                                 v3s16(0,0,-1), // front
2137                                 v3s16(-1,0,0), // left
2138                         };
2139
2140                         // Theck that upper is air or doesn't exist.
2141                         // Cancel dropping if upper keeps it in place
2142                         u32 i3 = i;
2143                         vmanip.m_area.add_y(em, i3, 1);
2144                         if(vmanip.m_area.contains(i3) == true
2145                                         && ndef->get(vmanip.m_data[i3]).walkable)
2146                         {
2147                                 continue;
2148                         }
2149
2150                         // Drop mud on side
2151
2152                         for(u32 di=0; di<4; di++)
2153                         {
2154                                 v3s16 dirp = dirs4[di];
2155                                 u32 i2 = i;
2156                                 // Move to side
2157                                 vmanip.m_area.add_p(em, i2, dirp);
2158                                 // Fail if out of area
2159                                 if(vmanip.m_area.contains(i2) == false)
2160                                         continue;
2161                                 // Check that side is air
2162                                 MapNode *n2 = &vmanip.m_data[i2];
2163                                 if(ndef->get(*n2).walkable)
2164                                         continue;
2165                                 // Check that under side is air
2166                                 vmanip.m_area.add_y(em, i2, -1);
2167                                 if(vmanip.m_area.contains(i2) == false)
2168                                         continue;
2169                                 n2 = &vmanip.m_data[i2];
2170                                 if(ndef->get(*n2).walkable)
2171                                         continue;
2172                                 /*// Check that under that is air (need a drop of 2)
2173                                 vmanip.m_area.add_y(em, i2, -1);
2174                                 if(vmanip.m_area.contains(i2) == false)
2175                                         continue;
2176                                 n2 = &vmanip.m_data[i2];
2177                                 if(content_walkable(n2->d))
2178                                         continue;*/
2179                                 // Loop further down until not air
2180                                 bool dropped_to_unknown = false;
2181                                 do{
2182                                         vmanip.m_area.add_y(em, i2, -1);
2183                                         n2 = &vmanip.m_data[i2];
2184                                         // if out of known area
2185                                         if(vmanip.m_area.contains(i2) == false
2186                                                         || n2->getContent() == CONTENT_IGNORE){
2187                                                 dropped_to_unknown = true;
2188                                                 break;
2189                                         }
2190                                 }while(ndef->get(*n2).walkable == false);
2191                                 // Loop one up so that we're in air
2192                                 vmanip.m_area.add_y(em, i2, 1);
2193                                 n2 = &vmanip.m_data[i2];
2194
2195                                 bool old_is_water = (n->getContent() == c_water_source);
2196                                 // Move mud to new place
2197                                 if(!dropped_to_unknown) {
2198                                         *n2 = *n;
2199                                         // Set old place to be air (or water)
2200                                         if(old_is_water)
2201                                                 *n = MapNode(c_water_source);
2202                                         else
2203                                                 *n = MapNode(CONTENT_AIR);
2204                                 }
2205
2206                                 // Done
2207                                 break;
2208                         }
2209                 }
2210                 }
2211         }
2212
2213         }
2214
2215         }//timer1
2216 #endif
2217
2218         } // Aging loop
2219         /***********************
2220                 END OF AGING LOOP
2221         ************************/
2222
2223         /*
2224                 Add top and bottom side of water to transforming_liquid queue
2225         */
2226
2227         for(s16 x=full_node_min.X; x<=full_node_max.X; x++)
2228         for(s16 z=full_node_min.Z; z<=full_node_max.Z; z++)
2229         {
2230                 // Node position
2231                 v2s16 p2d(x,z);
2232                 {
2233                         bool water_found = false;
2234                         // Use fast index incrementing
2235                         v3s16 em = vmanip.m_area.getExtent();
2236                         u32 i = vmanip.m_area.index(v3s16(p2d.X, full_node_max.Y, p2d.Y));
2237                         for(s16 y=full_node_max.Y; y>=full_node_min.Y; y--)
2238                         {
2239                                 if(y == full_node_max.Y){
2240                                         water_found =
2241                                                 (vmanip.m_data[i].getContent() == c_water_source ||
2242                                                 vmanip.m_data[i].getContent() == c_lava_source);
2243                                 }
2244                                 else if(water_found == false)
2245                                 {
2246                                         if(vmanip.m_data[i].getContent() == c_water_source ||
2247                                                         vmanip.m_data[i].getContent() == c_lava_source)
2248                                         {
2249                                                 v3s16 p = v3s16(p2d.X, y, p2d.Y);
2250                                                 data->transforming_liquid.push_back(p);
2251                                                 water_found = true;
2252                                         }
2253                                 }
2254                                 else
2255                                 {
2256                                         // This can be done because water_found can only
2257                                         // turn to true and end up here after going through
2258                                         // a single block.
2259                                         if(vmanip.m_data[i+1].getContent() != c_water_source ||
2260                                                         vmanip.m_data[i+1].getContent() != c_lava_source)
2261                                         {
2262                                                 v3s16 p = v3s16(p2d.X, y+1, p2d.Y);
2263                                                 data->transforming_liquid.push_back(p);
2264                                                 water_found = false;
2265                                         }
2266                                 }
2267
2268                                 vmanip.m_area.add_y(em, i, -1);
2269                         }
2270                 }
2271         }
2272
2273         /*
2274                 Grow grass
2275         */
2276
2277         for(s16 x=full_node_min.X; x<=full_node_max.X; x++)
2278         for(s16 z=full_node_min.Z; z<=full_node_max.Z; z++)
2279         {
2280                 // Node position in 2d
2281                 v2s16 p2d = v2s16(x,z);
2282
2283                 /*
2284                         Find the lowest surface to which enough light ends up
2285                         to make grass grow.
2286
2287                         Basically just wait until not air and not leaves.
2288                 */
2289                 s16 surface_y = 0;
2290                 {
2291                         v3s16 em = vmanip.m_area.getExtent();
2292                         u32 i = vmanip.m_area.index(v3s16(p2d.X, node_max.Y, p2d.Y));
2293                         s16 y;
2294                         // Go to ground level
2295                         for(y=node_max.Y; y>=full_node_min.Y; y--)
2296                         {
2297                                 MapNode &n = vmanip.m_data[i];
2298                                 if(ndef->get(n).param_type != CPT_LIGHT
2299                                                 || ndef->get(n).liquid_type != LIQUID_NONE)
2300                                         break;
2301                                 vmanip.m_area.add_y(em, i, -1);
2302                         }
2303                         if(y >= full_node_min.Y)
2304                                 surface_y = y;
2305                         else
2306                                 surface_y = full_node_min.Y;
2307                 }
2308
2309                 u32 i = vmanip.m_area.index(p2d.X, surface_y, p2d.Y);
2310                 MapNode *n = &vmanip.m_data[i];
2311                 if(n->getContent() == c_dirt){
2312                         // Well yeah, this can't be overground...
2313                         if(surface_y < WATER_LEVEL - 20)
2314                                 continue;
2315                         n->setContent(c_dirt_with_grass);
2316                 }
2317         }
2318
2319         /*
2320                 Generate some trees
2321         */
2322         assert(central_area_size.X == central_area_size.Z);
2323         {
2324                 // Divide area into parts
2325                 s16 div = 8;
2326                 s16 sidelen = central_area_size.X / div;
2327                 double area = sidelen * sidelen;
2328                 for(s16 x0=0; x0<div; x0++)
2329                 for(s16 z0=0; z0<div; z0++)
2330                 {
2331                         // Center position of part of division
2332                         v2s16 p2d_center(
2333                                 node_min.X + sidelen/2 + sidelen*x0,
2334                                 node_min.Z + sidelen/2 + sidelen*z0
2335                         );
2336                         // Minimum edge of part of division
2337                         v2s16 p2d_min(
2338                                 node_min.X + sidelen*x0,
2339                                 node_min.Z + sidelen*z0
2340                         );
2341                         // Maximum edge of part of division
2342                         v2s16 p2d_max(
2343                                 node_min.X + sidelen + sidelen*x0 - 1,
2344                                 node_min.Z + sidelen + sidelen*z0 - 1
2345                         );
2346                         // Amount of trees
2347                         u32 tree_count = area * tree_amount_2d(data->seed, p2d_center);
2348                         // Put trees in random places on part of division
2349                         for(u32 i=0; i<tree_count; i++)
2350                         {
2351                                 s16 x = myrand_range(p2d_min.X, p2d_max.X);
2352                                 s16 z = myrand_range(p2d_min.Y, p2d_max.Y);
2353                                 s16 y = find_ground_level(vmanip, v2s16(x,z), ndef);
2354                                 // Don't make a tree under water level
2355                                 if(y < WATER_LEVEL)
2356                                         continue;
2357                                 // Don't make a tree so high that it doesn't fit
2358                                 if(y > node_max.Y - 6)
2359                                         continue;
2360                                 v3s16 p(x,y,z);
2361                                 /*
2362                                         Trees grow only on mud and grass
2363                                 */
2364                                 {
2365                                         u32 i = vmanip.m_area.index(v3s16(p));
2366                                         MapNode *n = &vmanip.m_data[i];
2367                                         if(n->getContent() != c_dirt
2368                                                         && n->getContent() != c_dirt_with_grass)
2369                                                 continue;
2370                                 }
2371                                 p.Y++;
2372                                 // Make a tree
2373                                 treegen::make_tree(vmanip, p, false, ndef);
2374                         }
2375                 }
2376         }
2377
2378 #if 0
2379         /*
2380                 Make base ground level
2381         */
2382
2383         for(s16 x=node_min.X; x<=node_max.X; x++)
2384         for(s16 z=node_min.Z; z<=node_max.Z; z++)
2385         {
2386                 // Node position
2387                 v2s16 p2d(x,z);
2388                 {
2389                         // Use fast index incrementing
2390                         v3s16 em = vmanip.m_area.getExtent();
2391                         u32 i = vmanip.m_area.index(v3s16(p2d.X, node_min.Y, p2d.Y));
2392                         for(s16 y=node_min.Y; y<=node_max.Y; y++)
2393                         {
2394                                 // Only modify places that have no content
2395                                 if(vmanip.m_data[i].getContent() == CONTENT_IGNORE)
2396                                 {
2397                                         // First priority: make air and water.
2398                                         // This avoids caves inside water.
2399                                         if(all_is_ground_except_caves == false
2400                                                         && val_is_ground(noisebuf_ground.get(x,y,z),
2401                                                         v3s16(x,y,z), data->seed) == false)
2402                                         {
2403                                                 if(y <= WATER_LEVEL)
2404                                                         vmanip.m_data[i] = n_water_source;
2405                                                 else
2406                                                         vmanip.m_data[i] = n_air;
2407                                         }
2408                                         else if(noisebuf_cave.get(x,y,z) > CAVE_NOISE_THRESHOLD)
2409                                                 vmanip.m_data[i] = n_air;
2410                                         else
2411                                                 vmanip.m_data[i] = n_stone;
2412                                 }
2413
2414                                 vmanip->m_area.add_y(em, i, 1);
2415                         }
2416                 }
2417         }
2418
2419         /*
2420                 Add mud and sand and others underground (in place of stone)
2421         */
2422
2423         for(s16 x=node_min.X; x<=node_max.X; x++)
2424         for(s16 z=node_min.Z; z<=node_max.Z; z++)
2425         {
2426                 // Node position
2427                 v2s16 p2d(x,z);
2428                 {
2429                         // Use fast index incrementing
2430                         v3s16 em = vmanip.m_area.getExtent();
2431                         u32 i = vmanip.m_area.index(v3s16(p2d.X, node_max.Y, p2d.Y));
2432                         for(s16 y=node_max.Y; y>=node_min.Y; y--)
2433                         {
2434                                 if(vmanip.m_data[i].getContent() == c_stone)
2435                                 {
2436                                         if(noisebuf_ground_crumbleness.get(x,y,z) > 1.3)
2437                                         {
2438                                                 if(noisebuf_ground_wetness.get(x,y,z) > 0.0)
2439                                                         vmanip.m_data[i] = n_dirt;
2440                                                 else
2441                                                         vmanip.m_data[i] = n_sand;
2442                                         }
2443                                         else if(noisebuf_ground_crumbleness.get(x,y,z) > 0.7)
2444                                         {
2445                                                 if(noisebuf_ground_wetness.get(x,y,z) < -0.6)
2446                                                         vmanip.m_data[i] = n_gravel;
2447                                         }
2448                                         else if(noisebuf_ground_crumbleness.get(x,y,z) <
2449                                                         -3.0 + MYMIN(0.1 * sqrt((float)MYMAX(0, -y)), 1.5))
2450                                         {
2451                                                 vmanip.m_data[i] = n_lava_source;
2452                                                 for(s16 x1=-1; x1<=1; x1++)
2453                                                 for(s16 y1=-1; y1<=1; y1++)
2454                                                 for(s16 z1=-1; z1<=1; z1++)
2455                                                         data->transforming_liquid.push_back(
2456                                                                         v3s16(p2d.X+x1, y+y1, p2d.Y+z1));
2457                                         }
2458                                 }
2459
2460                                 vmanip->m_area.add_y(em, i, -1);
2461                         }
2462                 }
2463         }
2464
2465         /*
2466                 Add dungeons
2467         */
2468
2469         //if(node_min.Y < approx_groundlevel)
2470         //if(myrand() % 3 == 0)
2471         //if(myrand() % 3 == 0 && node_min.Y < approx_groundlevel)
2472         //if(myrand() % 100 == 0 && node_min.Y < approx_groundlevel)
2473         //float dungeon_rarity = g_settings.getFloat("dungeon_rarity");
2474         float dungeon_rarity = 0.02;
2475         if(((noise3d(blockpos.X,blockpos.Y,blockpos.Z,data->seed)+1.0)/2.0)
2476                         < dungeon_rarity
2477                         && node_min.Y < approx_groundlevel)
2478         {
2479                 // Dungeon generator doesn't modify places which have this set
2480                 vmanip->clearFlag(VMANIP_FLAG_DUNGEON_INSIDE
2481                                 | VMANIP_FLAG_DUNGEON_PRESERVE);
2482
2483                 // Set all air and water to be untouchable to make dungeons open
2484                 // to caves and open air
2485                 for(s16 x=full_node_min.X; x<=full_node_max.X; x++)
2486                 for(s16 z=full_node_min.Z; z<=full_node_max.Z; z++)
2487                 {
2488                         // Node position
2489                         v2s16 p2d(x,z);
2490                         {
2491                                 // Use fast index incrementing
2492                                 v3s16 em = vmanip.m_area.getExtent();
2493                                 u32 i = vmanip.m_area.index(v3s16(p2d.X, full_node_max.Y, p2d.Y));
2494                                 for(s16 y=full_node_max.Y; y>=full_node_min.Y; y--)
2495                                 {
2496                                         if(vmanip.m_data[i].getContent() == CONTENT_AIR)
2497                                                 vmanip.m_flags[i] |= VMANIP_FLAG_DUNGEON_PRESERVE;
2498                                         else if(vmanip.m_data[i].getContent() == c_water_source)
2499                                                 vmanip.m_flags[i] |= VMANIP_FLAG_DUNGEON_PRESERVE;
2500                                         vmanip->m_area.add_y(em, i, -1);
2501                                 }
2502                         }
2503                 }
2504
2505                 PseudoRandom random(blockseed+2);
2506
2507                 // Add it
2508                 make_dungeon1(vmanip, random, ndef);
2509
2510                 // Convert some cobble to mossy cobble
2511                 for(s16 x=full_node_min.X; x<=full_node_max.X; x++)
2512                 for(s16 z=full_node_min.Z; z<=full_node_max.Z; z++)
2513                 {
2514                         // Node position
2515                         v2s16 p2d(x,z);
2516                         {
2517                                 // Use fast index incrementing
2518                                 v3s16 em = vmanip.m_area.getExtent();
2519                                 u32 i = vmanip.m_area.index(v3s16(p2d.X, full_node_max.Y, p2d.Y));
2520                                 for(s16 y=full_node_max.Y; y>=full_node_min.Y; y--)
2521                                 {
2522                                         // (noisebuf not used because it doesn't contain the
2523                                         //  full area)
2524                                         double wetness = noise3d_param(
2525                                                         get_ground_wetness_params(data->seed), x,y,z);
2526                                         double d = noise3d_perlin((float)x/2.5,
2527                                                         (float)y/2.5,(float)z/2.5,
2528                                                         blockseed, 2, 1.4);
2529                                         if(vmanip.m_data[i].getContent() == c_cobble)
2530                                         {
2531                                                 if(d < wetness/3.0)
2532                                                 {
2533                                                         vmanip.m_data[i].setContent(c_mossycobble);
2534                                                 }
2535                                         }
2536                                         /*else if(vmanip.m_flags[i] & VMANIP_FLAG_DUNGEON_INSIDE)
2537                                         {
2538                                                 if(wetness > 1.2)
2539                                                         vmanip.m_data[i].setContent(c_dirt);
2540                                         }*/
2541                                         vmanip->m_area.add_y(em, i, -1);
2542                                 }
2543                         }
2544                 }
2545         }
2546
2547         /*
2548                 Add NC
2549         */
2550         {
2551                 PseudoRandom ncrandom(blockseed+9324342);
2552                 if(ncrandom.range(0, 1000) == 0 && blockpos.Y <= -3)
2553                 {
2554                         make_nc(vmanip, ncrandom, ndef);
2555                 }
2556         }
2557
2558         /*
2559                 Add top and bottom side of water to transforming_liquid queue
2560         */
2561
2562         for(s16 x=node_min.X; x<=node_max.X; x++)
2563         for(s16 z=node_min.Z; z<=node_max.Z; z++)
2564         {
2565                 // Node position
2566                 v2s16 p2d(x,z);
2567                 {
2568                         bool water_found = false;
2569                         // Use fast index incrementing
2570                         v3s16 em = vmanip.m_area.getExtent();
2571                         u32 i = vmanip.m_area.index(v3s16(p2d.X, node_max.Y, p2d.Y));
2572                         for(s16 y=node_max.Y; y>=node_min.Y; y--)
2573                         {
2574                                 if(water_found == false)
2575                                 {
2576                                         if(vmanip.m_data[i].getContent() == c_water_source)
2577                                         {
2578                                                 v3s16 p = v3s16(p2d.X, y, p2d.Y);
2579                                                 data->transforming_liquid.push_back(p);
2580                                                 water_found = true;
2581                                         }
2582                                 }
2583                                 else
2584                                 {
2585                                         // This can be done because water_found can only
2586                                         // turn to true and end up here after going through
2587                                         // a single block.
2588                                         if(vmanip.m_data[i+1].getContent() != c_water_source)
2589                                         {
2590                                                 v3s16 p = v3s16(p2d.X, y+1, p2d.Y);
2591                                                 data->transforming_liquid.push_back(p);
2592                                                 water_found = false;
2593                                         }
2594                                 }
2595
2596                                 vmanip->m_area.add_y(em, i, -1);
2597                         }
2598                 }
2599         }
2600
2601         /*
2602                 If close to ground level
2603         */
2604
2605         //if(abs(approx_ground_depth) < 30)
2606         if(minimum_ground_depth < 5 && maximum_ground_depth > -5)
2607         {
2608                 /*
2609                         Add grass and mud
2610                 */
2611
2612                 for(s16 x=node_min.X; x<=node_max.X; x++)
2613                 for(s16 z=node_min.Z; z<=node_max.Z; z++)
2614                 {
2615                         // Node position
2616                         v2s16 p2d(x,z);
2617                         {
2618                                 bool possibly_have_sand = get_have_beach(data->seed, p2d);
2619                                 bool have_sand = false;
2620                                 u32 current_depth = 0;
2621                                 bool air_detected = false;
2622                                 bool water_detected = false;
2623                                 bool have_clay = false;
2624
2625                                 // Use fast index incrementing
2626                                 s16 start_y = node_max.Y+2;
2627                                 v3s16 em = vmanip.m_area.getExtent();
2628                                 u32 i = vmanip.m_area.index(v3s16(p2d.X, start_y, p2d.Y));
2629                                 for(s16 y=start_y; y>=node_min.Y-3; y--)
2630                                 {
2631                                         if(vmanip.m_data[i].getContent() == c_water_source)
2632                                                 water_detected = true;
2633                                         if(vmanip.m_data[i].getContent() == CONTENT_AIR)
2634                                                 air_detected = true;
2635
2636                                         if((vmanip.m_data[i].getContent() == c_stone
2637                                                         || vmanip.m_data[i].getContent() == c_dirt_with_grass
2638                                                         || vmanip.m_data[i].getContent() == c_dirt
2639                                                         || vmanip.m_data[i].getContent() == c_sand
2640                                                         || vmanip.m_data[i].getContent() == c_gravel
2641                                                         ) && (air_detected || water_detected))
2642                                         {
2643                                                 if(current_depth == 0 && y <= WATER_LEVEL+2
2644                                                                 && possibly_have_sand)
2645                                                         have_sand = true;
2646
2647                                                 if(current_depth < 4)
2648                                                 {
2649                                                         if(have_sand)
2650                                                         {
2651                                                                 vmanip.m_data[i] = MapNode(c_sand);
2652                                                         }
2653                                                         #if 1
2654                                                         else if(current_depth==0 && !water_detected
2655                                                                         && y >= WATER_LEVEL && air_detected)
2656                                                                 vmanip.m_data[i] = MapNode(c_dirt_with_grass);
2657                                                         #endif
2658                                                         else
2659                                                                 vmanip.m_data[i] = MapNode(c_dirt);
2660                                                 }
2661                                                 else
2662                                                 {
2663                                                         if(vmanip.m_data[i].getContent() == c_dirt
2664                                                                 || vmanip.m_data[i].getContent() == c_dirt_with_grass)
2665                                                                 vmanip.m_data[i] = MapNode(c_stone);
2666                                                 }
2667
2668                                                 current_depth++;
2669
2670                                                 if(current_depth >= 8)
2671                                                         break;
2672                                         }
2673                                         else if(current_depth != 0)
2674                                                 break;
2675
2676                                         vmanip->m_area.add_y(em, i, -1);
2677                                 }
2678                         }
2679                 }
2680
2681                 /*
2682                         Calculate some stuff
2683                 */
2684
2685                 float surface_humidity = surface_humidity_2d(data->seed, p2d_center);
2686                 bool is_jungle = surface_humidity > 0.75;
2687                 // Amount of trees
2688                 u32 tree_count = gen_area_nodes * tree_amount_2d(data->seed, p2d_center);
2689                 if(is_jungle)
2690                         tree_count *= 5;
2691
2692                 /*
2693                         Add trees
2694                 */
2695                 PseudoRandom treerandom(blockseed);
2696                 // Put trees in random places on part of division
2697                 for(u32 i=0; i<tree_count; i++)
2698                 {
2699                         s16 x = treerandom.range(node_min.X, node_max.X);
2700                         s16 z = treerandom.range(node_min.Z, node_max.Z);
2701                         //s16 y = find_ground_level(vmanip, v2s16(x,z));
2702                         s16 y = find_ground_level_from_noise(data->seed, v2s16(x,z), 4);
2703                         // Don't make a tree under water level
2704                         if(y < WATER_LEVEL)
2705                                 continue;
2706                         // Make sure tree fits (only trees whose starting point is
2707                         // at this block are added)
2708                         if(y < node_min.Y || y > node_max.Y)
2709                                 continue;
2710                         /*
2711                                 Find exact ground level
2712                         */
2713                         v3s16 p(x,y+6,z);
2714                         bool found = false;
2715                         for(; p.Y >= y-6; p.Y--)
2716                         {
2717                                 u32 i = vmanip->m_area.index(p);
2718                                 MapNode *n = &vmanip->m_data[i];
2719                                 if(n->getContent() != CONTENT_AIR && n->getContent() != c_water_source && n->getContent() != CONTENT_IGNORE)
2720                                 {
2721                                         found = true;
2722                                         break;
2723                                 }
2724                         }
2725                         // If not found, handle next one
2726                         if(found == false)
2727                                 continue;
2728
2729                         {
2730                                 u32 i = vmanip->m_area.index(p);
2731                                 MapNode *n = &vmanip->m_data[i];
2732
2733                                 if(n->getContent() != c_dirt && n->getContent() != c_dirt_with_grass && n->getContent() != c_sand)
2734                                                 continue;
2735
2736                                 // Papyrus grows only on mud and in water
2737                                 if(n->getContent() == c_dirt && y <= WATER_LEVEL)
2738                                 {
2739                                         p.Y++;
2740                                         make_papyrus(vmanip, p, ndef);
2741                                 }
2742                                 // Trees grow only on mud and grass, on land
2743                                 else if((n->getContent() == c_dirt || n->getContent() == c_dirt_with_grass) && y > WATER_LEVEL + 2)
2744                                 {
2745                                         p.Y++;
2746                                         //if(surface_humidity_2d(data->seed, v2s16(x, y)) < 0.5)
2747                                         if(is_jungle == false)
2748                                         {
2749                                                 bool is_apple_tree;
2750                                                 if(myrand_range(0,4) != 0)
2751                                                         is_apple_tree = false;
2752                                                 else
2753                                                         is_apple_tree = noise2d_perlin(
2754                                                                         0.5+(float)p.X/100, 0.5+(float)p.Z/100,
2755                                                                         data->seed+342902, 3, 0.45) > 0.2;
2756                                                 make_tree(vmanip, p, is_apple_tree, ndef);
2757                                         }
2758                                         else
2759                                                 make_jungletree(vmanip, p, ndef);
2760                                 }
2761                                 // Cactii grow only on sand, on land
2762                                 else if(n->getContent() == c_sand && y > WATER_LEVEL + 2)
2763                                 {
2764                                         p.Y++;
2765                                         make_cactus(vmanip, p, ndef);
2766                                 }
2767                         }
2768                 }
2769
2770                 /*
2771                         Add jungle grass
2772                 */
2773                 if(is_jungle)
2774                 {
2775                         PseudoRandom grassrandom(blockseed);
2776                         for(u32 i=0; i<surface_humidity*5*tree_count; i++)
2777                         {
2778                                 s16 x = grassrandom.range(node_min.X, node_max.X);
2779                                 s16 z = grassrandom.range(node_min.Z, node_max.Z);
2780                                 s16 y = find_ground_level_from_noise(data->seed, v2s16(x,z), 4);
2781                                 if(y < WATER_LEVEL)
2782                                         continue;
2783                                 if(y < node_min.Y || y > node_max.Y)
2784                                         continue;
2785                                 /*
2786                                         Find exact ground level
2787                                 */
2788                                 v3s16 p(x,y+6,z);
2789                                 bool found = false;
2790                                 for(; p.Y >= y-6; p.Y--)
2791                                 {
2792                                         u32 i = vmanip->m_area.index(p);
2793                                         MapNode *n = &vmanip->m_data[i];
2794                                         if(data->nodedef->get(*n).is_ground_content)
2795                                         {
2796                                                 found = true;
2797                                                 break;
2798                                         }
2799                                 }
2800                                 // If not found, handle next one
2801                                 if(found == false)
2802                                         continue;
2803                                 p.Y++;
2804                                 if(vmanip.m_area.contains(p) == false)
2805                                         continue;
2806                                 if(vmanip.m_data[vmanip.m_area.index(p)].getContent() != CONTENT_AIR)
2807                                         continue;
2808                                 /*p.Y--;
2809                                 if(vmanip.m_area.contains(p))
2810                                         vmanip.m_data[vmanip.m_area.index(p)] = c_dirt;
2811                                 p.Y++;*/
2812                                 if(vmanip.m_area.contains(p))
2813                                         vmanip.m_data[vmanip.m_area.index(p)] = c_junglegrass;
2814                         }
2815                 }
2816
2817 #if 0
2818                 /*
2819                         Add some kind of random stones
2820                 */
2821
2822                 u32 random_stone_count = gen_area_nodes *
2823                                 randomstone_amount_2d(data->seed, p2d_center);
2824                 // Put in random places on part of division
2825                 for(u32 i=0; i<random_stone_count; i++)
2826                 {
2827                         s16 x = myrand_range(node_min.X, node_max.X);
2828                         s16 z = myrand_range(node_min.Z, node_max.Z);
2829                         s16 y = find_ground_level_from_noise(data->seed, v2s16(x,z), 1);
2830                         // Don't add under water level
2831                         /*if(y < WATER_LEVEL)
2832                                 continue;*/
2833                         // Don't add if doesn't belong to this block
2834                         if(y < node_min.Y || y > node_max.Y)
2835                                 continue;
2836                         v3s16 p(x,y,z);
2837                         // Filter placement
2838                         /*{
2839                                 u32 i = vmanip->m_area.index(v3s16(p));
2840                                 MapNode *n = &vmanip->m_data[i];
2841                                 if(n->getContent() != c_dirt && n->getContent() != c_dirt_with_grass)
2842                                         continue;
2843                         }*/
2844                         // Will be placed one higher
2845                         p.Y++;
2846                         // Add it
2847                         make_randomstone(vmanip, p);
2848                 }
2849 #endif
2850
2851 #if 0
2852                 /*
2853                         Add larger stones
2854                 */
2855
2856                 u32 large_stone_count = gen_area_nodes *
2857                                 largestone_amount_2d(data->seed, p2d_center);
2858                 //u32 large_stone_count = 1;
2859                 // Put in random places on part of division
2860                 for(u32 i=0; i<large_stone_count; i++)
2861                 {
2862                         s16 x = myrand_range(node_min.X, node_max.X);
2863                         s16 z = myrand_range(node_min.Z, node_max.Z);
2864                         s16 y = find_ground_level_from_noise(data->seed, v2s16(x,z), 1);
2865                         // Don't add under water level
2866                         /*if(y < WATER_LEVEL)
2867                                 continue;*/
2868                         // Don't add if doesn't belong to this block
2869                         if(y < node_min.Y || y > node_max.Y)
2870                                 continue;
2871                         v3s16 p(x,y,z);
2872                         // Filter placement
2873                         /*{
2874                                 u32 i = vmanip->m_area.index(v3s16(p));
2875                                 MapNode *n = &vmanip->m_data[i];
2876                                 if(n->getContent() != c_dirt && n->getContent() != c_dirt_with_grass)
2877                                         continue;
2878                         }*/
2879                         // Will be placed one lower
2880                         p.Y--;
2881                         // Add it
2882                         make_largestone(vmanip, p);
2883                 }
2884 #endif
2885         }
2886
2887         /*
2888                 Add minerals
2889         */
2890
2891         {
2892                 PseudoRandom mineralrandom(blockseed);
2893
2894                 /*
2895                         Add meseblocks
2896                 */
2897                 for(s16 i=0; i<approx_ground_depth/4; i++)
2898                 {
2899                         if(mineralrandom.next()%50 == 0)
2900                         {
2901                                 s16 x = mineralrandom.range(node_min.X+1, node_max.X-1);
2902                                 s16 y = mineralrandom.range(node_min.Y+1, node_max.Y-1);
2903                                 s16 z = mineralrandom.range(node_min.Z+1, node_max.Z-1);
2904                                 for(u16 i=0; i<27; i++)
2905                                 {
2906                                         v3s16 p = v3s16(x,y,z) + g_27dirs[i];
2907                                         u32 vi = vmanip.m_area.index(p);
2908                                         if(vmanip.m_data[vi].getContent() == c_stone)
2909                                                 if(mineralrandom.next()%8 == 0)
2910                                                         vmanip.m_data[vi] = MapNode(c_mese);
2911                                 }
2912
2913                         }
2914                 }
2915                 /*
2916                         Add others
2917                 */
2918                 {
2919                         u16 a = mineralrandom.range(0,15);
2920                         a = a*a*a;
2921                         u16 amount = 20 * a/1000;
2922                         for(s16 i=0; i<amount; i++)
2923                         {
2924                                 s16 x = mineralrandom.range(node_min.X+1, node_max.X-1);
2925                                 s16 y = mineralrandom.range(node_min.Y+1, node_max.Y-1);
2926                                 s16 z = mineralrandom.range(node_min.Z+1, node_max.Z-1);
2927
2928                                 u8 base_content = c_stone;
2929                                 MapNode new_content(CONTENT_IGNORE);
2930                                 u32 sparseness = 6;
2931
2932                                 if(noisebuf_ground_crumbleness.get(x,y+5,z) < -0.1)
2933                                 {
2934                                         new_content = MapNode(c_stone_with_coal);
2935                                 }
2936                                 else
2937                                 {
2938                                         if(noisebuf_ground_wetness.get(x,y+5,z) > 0.0)
2939                                                 new_content = MapNode(c_stone_with_iron);
2940                                         /*if(noisebuf_ground_wetness.get(x,y,z) > 0.0)
2941                                                 vmanip.m_data[i] = MapNode(c_dirt);
2942                                         else
2943                                                 vmanip.m_data[i] = MapNode(c_sand);*/
2944                                 }
2945                                 /*else if(noisebuf_ground_crumbleness.get(x,y,z) > 0.1)
2946                                 {
2947                                 }*/
2948
2949                                 if(new_content.getContent() != CONTENT_IGNORE)
2950                                 {
2951                                         for(u16 i=0; i<27; i++)
2952                                         {
2953                                                 v3s16 p = v3s16(x,y,z) + g_27dirs[i];
2954                                                 u32 vi = vmanip.m_area.index(p);
2955                                                 if(vmanip.m_data[vi].getContent() == base_content)
2956                                                 {
2957                                                         if(mineralrandom.next()%sparseness == 0)
2958                                                                 vmanip.m_data[vi] = new_content;
2959                                                 }
2960                                         }
2961                                 }
2962                         }
2963                 }
2964                 /*
2965                         Add coal
2966                 */
2967                 //for(s16 i=0; i < MYMAX(0, 50 - abs(node_min.Y+8 - (-30))); i++)
2968                 //for(s16 i=0; i<50; i++)
2969                 u16 coal_amount = 30;
2970                 u16 coal_rareness = 60 / coal_amount;
2971                 if(coal_rareness == 0)
2972                         coal_rareness = 1;
2973                 if(mineralrandom.next()%coal_rareness == 0)
2974                 {
2975                         u16 a = mineralrandom.next() % 16;
2976                         u16 amount = coal_amount * a*a*a / 1000;
2977                         for(s16 i=0; i<amount; i++)
2978                         {
2979                                 s16 x = mineralrandom.range(node_min.X+1, node_max.X-1);
2980                                 s16 y = mineralrandom.range(node_min.Y+1, node_max.Y-1);
2981                                 s16 z = mineralrandom.range(node_min.Z+1, node_max.Z-1);
2982                                 for(u16 i=0; i<27; i++)
2983                                 {
2984                                         v3s16 p = v3s16(x,y,z) + g_27dirs[i];
2985                                         u32 vi = vmanip.m_area.index(p);
2986                                         if(vmanip.m_data[vi].getContent() == c_stone)
2987                                                 if(mineralrandom.next()%8 == 0)
2988                                                         vmanip.m_data[vi] = MapNode(c_stone_with_coal);
2989                                 }
2990                         }
2991                 }
2992                 /*
2993                         Add iron
2994                 */
2995                 u16 iron_amount = 8;
2996                 u16 iron_rareness = 60 / iron_amount;
2997                 if(iron_rareness == 0)
2998                         iron_rareness = 1;
2999                 if(mineralrandom.next()%iron_rareness == 0)
3000                 {
3001                         u16 a = mineralrandom.next() % 16;
3002                         u16 amount = iron_amount * a*a*a / 1000;
3003                         for(s16 i=0; i<amount; i++)
3004                         {
3005                                 s16 x = mineralrandom.range(node_min.X+1, node_max.X-1);
3006                                 s16 y = mineralrandom.range(node_min.Y+1, node_max.Y-1);
3007                                 s16 z = mineralrandom.range(node_min.Z+1, node_max.Z-1);
3008                                 for(u16 i=0; i<27; i++)
3009                                 {
3010                                         v3s16 p = v3s16(x,y,z) + g_27dirs[i];
3011                                         u32 vi = vmanip.m_area.index(p);
3012                                         if(vmanip.m_data[vi].getContent() == c_stone)
3013                                                 if(mineralrandom.next()%8 == 0)
3014                                                         vmanip.m_data[vi] = MapNode(c_stone_with_iron);
3015                                 }
3016                         }
3017                 }
3018         }
3019 #endif
3020
3021         /*
3022                 Calculate lighting
3023         */
3024         {
3025         ScopeProfiler sp(g_profiler, "EmergeThread: mapgen lighting update",
3026                         SPT_AVG);
3027         //VoxelArea a(node_min, node_max);
3028         VoxelArea a(node_min-v3s16(1,0,1)*MAP_BLOCKSIZE,
3029                         node_max+v3s16(1,0,1)*MAP_BLOCKSIZE);
3030         /*VoxelArea a(node_min-v3s16(1,0,1)*MAP_BLOCKSIZE/2,
3031                         node_max+v3s16(1,0,1)*MAP_BLOCKSIZE/2);*/
3032         enum LightBank banks[2] = {LIGHTBANK_DAY, LIGHTBANK_NIGHT};
3033         for(int i=0; i<2; i++)
3034         {
3035                 enum LightBank bank = banks[i];
3036
3037                 core::map<v3s16, bool> light_sources;
3038                 core::map<v3s16, u8> unlight_from;
3039
3040                 voxalgo::clearLightAndCollectSources(vmanip, a, bank, ndef,
3041                                 light_sources, unlight_from);
3042
3043                 bool inexistent_top_provides_sunlight = !block_is_underground;
3044                 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
3045                                 vmanip, a, inexistent_top_provides_sunlight,
3046                                 light_sources, ndef);
3047                 // TODO: Do stuff according to bottom_sunlight_valid
3048
3049                 vmanip.unspreadLight(bank, unlight_from, light_sources, ndef);
3050
3051                 vmanip.spreadLight(bank, light_sources, ndef);
3052         }
3053         }
3054 }
3055
3056 #endif ///BIG COMMENT
3057
3058 BlockMakeData::BlockMakeData():
3059         no_op(false),
3060         vmanip(NULL),
3061         seed(0),
3062         nodedef(NULL)
3063 {}
3064
3065 BlockMakeData::~BlockMakeData()
3066 {
3067         delete vmanip;
3068 }
3069
3070 //}; // namespace mapgen
3071
3072