3 Copyright (C) 2010-2011 celeron55, Perttu Ahola <celeron55@gmail.com>
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.
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.
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.
25 //#include "serverobject.h"
26 #include "content_sao.h"
28 #include "content_mapnode.h" // For content_mapnode_get_new_name
29 #include "voxelalgorithms.h"
31 #include "main.h" // For g_profiler
37 Some helper functions for the map generator
41 // Returns Y one under area minimum if not found
42 static s16 find_ground_level(VoxelManipulator &vmanip, v2s16 p2d,
43 INodeDefManager *ndef)
45 v3s16 em = vmanip.m_area.getExtent();
46 s16 y_nodes_max = vmanip.m_area.MaxEdge.Y;
47 s16 y_nodes_min = vmanip.m_area.MinEdge.Y;
48 u32 i = vmanip.m_area.index(v3s16(p2d.X, y_nodes_max, p2d.Y));
50 for(y=y_nodes_max; y>=y_nodes_min; y--)
52 MapNode &n = vmanip.m_data[i];
53 if(ndef->get(n).walkable)
56 vmanip.m_area.add_y(em, i, -1);
61 return y_nodes_min - 1;
65 // Returns Y one under area minimum if not found
66 static s16 find_ground_level_clever(VoxelManipulator &vmanip, v2s16 p2d,
67 INodeDefManager *ndef)
69 if(!vmanip.m_area.contains(v3s16(p2d.X, vmanip.m_area.MaxEdge.Y, p2d.Y)))
70 return vmanip.m_area.MinEdge.Y-1;
71 v3s16 em = vmanip.m_area.getExtent();
72 s16 y_nodes_max = vmanip.m_area.MaxEdge.Y;
73 s16 y_nodes_min = vmanip.m_area.MinEdge.Y;
74 u32 i = vmanip.m_area.index(v3s16(p2d.X, y_nodes_max, p2d.Y));
76 content_t c_tree = ndef->getId("mapgen_tree");
77 content_t c_leaves = ndef->getId("mapgen_leaves");
78 for(y=y_nodes_max; y>=y_nodes_min; y--)
80 MapNode &n = vmanip.m_data[i];
81 if(ndef->get(n).walkable
82 && n.getContent() != c_tree
83 && n.getContent() != c_leaves)
86 vmanip.m_area.add_y(em, i, -1);
91 return y_nodes_min - 1;
95 // Returns Y one under area minimum if not found
96 static s16 find_stone_level(VoxelManipulator &vmanip, v2s16 p2d,
97 INodeDefManager *ndef)
99 v3s16 em = vmanip.m_area.getExtent();
100 s16 y_nodes_max = vmanip.m_area.MaxEdge.Y;
101 s16 y_nodes_min = vmanip.m_area.MinEdge.Y;
102 u32 i = vmanip.m_area.index(v3s16(p2d.X, y_nodes_max, p2d.Y));
104 content_t c_stone = ndef->getId("mapgen_stone");
105 content_t c_desert_stone = ndef->getId("mapgen_desert_stone");
106 for(y=y_nodes_max; y>=y_nodes_min; y--)
108 MapNode &n = vmanip.m_data[i];
109 content_t c = n.getContent();
110 if(c != CONTENT_IGNORE && (
111 c == c_stone || c == c_desert_stone))
114 vmanip.m_area.add_y(em, i, -1);
119 return y_nodes_min - 1;
123 void make_tree(ManualMapVoxelManipulator &vmanip, v3s16 p0,
124 bool is_apple_tree, INodeDefManager *ndef)
126 MapNode treenode(ndef->getId("mapgen_tree"));
127 MapNode leavesnode(ndef->getId("mapgen_leaves"));
128 MapNode applenode(ndef->getId("mapgen_apple"));
130 s16 trunk_h = myrand_range(4, 5);
132 for(s16 ii=0; ii<trunk_h; ii++)
134 if(vmanip.m_area.contains(p1))
135 vmanip.m_data[vmanip.m_area.index(p1)] = treenode;
139 // p1 is now the last piece of the trunk
142 VoxelArea leaves_a(v3s16(-2,-1,-2), v3s16(2,2,2));
143 //SharedPtr<u8> leaves_d(new u8[leaves_a.getVolume()]);
144 Buffer<u8> leaves_d(leaves_a.getVolume());
145 for(s32 i=0; i<leaves_a.getVolume(); i++)
148 // Force leaves at near the end of the trunk
151 for(s16 z=-d; z<=d; z++)
152 for(s16 y=-d; y<=d; y++)
153 for(s16 x=-d; x<=d; x++)
155 leaves_d[leaves_a.index(v3s16(x,y,z))] = 1;
159 // Add leaves randomly
160 for(u32 iii=0; iii<7; iii++)
165 myrand_range(leaves_a.MinEdge.X, leaves_a.MaxEdge.X-d),
166 myrand_range(leaves_a.MinEdge.Y, leaves_a.MaxEdge.Y-d),
167 myrand_range(leaves_a.MinEdge.Z, leaves_a.MaxEdge.Z-d)
170 for(s16 z=0; z<=d; z++)
171 for(s16 y=0; y<=d; y++)
172 for(s16 x=0; x<=d; x++)
174 leaves_d[leaves_a.index(p+v3s16(x,y,z))] = 1;
178 // Blit leaves to vmanip
179 for(s16 z=leaves_a.MinEdge.Z; z<=leaves_a.MaxEdge.Z; z++)
180 for(s16 y=leaves_a.MinEdge.Y; y<=leaves_a.MaxEdge.Y; y++)
181 for(s16 x=leaves_a.MinEdge.X; x<=leaves_a.MaxEdge.X; x++)
185 if(vmanip.m_area.contains(p) == false)
187 u32 vi = vmanip.m_area.index(p);
188 if(vmanip.m_data[vi].getContent() != CONTENT_AIR
189 && vmanip.m_data[vi].getContent() != CONTENT_IGNORE)
191 u32 i = leaves_a.index(x,y,z);
192 if(leaves_d[i] == 1) {
193 bool is_apple = myrand_range(0,99) < 10;
194 if(is_apple_tree && is_apple) {
195 vmanip.m_data[vi] = applenode;
197 vmanip.m_data[vi] = leavesnode;
204 static void make_jungletree(VoxelManipulator &vmanip, v3s16 p0,
205 INodeDefManager *ndef)
207 MapNode treenode(ndef->getId("mapgen_jungletree"));
208 MapNode leavesnode(ndef->getId("mapgen_leaves"));
210 for(s16 x=-1; x<=1; x++)
211 for(s16 z=-1; z<=1; z++)
213 if(myrand_range(0, 2) == 0)
215 v3s16 p1 = p0 + v3s16(x,0,z);
216 v3s16 p2 = p0 + v3s16(x,-1,z);
217 if(vmanip.m_area.contains(p2)
218 && vmanip.m_data[vmanip.m_area.index(p2)] == CONTENT_AIR)
219 vmanip.m_data[vmanip.m_area.index(p2)] = treenode;
220 else if(vmanip.m_area.contains(p1))
221 vmanip.m_data[vmanip.m_area.index(p1)] = treenode;
224 s16 trunk_h = myrand_range(8, 12);
226 for(s16 ii=0; ii<trunk_h; ii++)
228 if(vmanip.m_area.contains(p1))
229 vmanip.m_data[vmanip.m_area.index(p1)] = treenode;
233 // p1 is now the last piece of the trunk
236 VoxelArea leaves_a(v3s16(-3,-2,-3), v3s16(3,2,3));
237 //SharedPtr<u8> leaves_d(new u8[leaves_a.getVolume()]);
238 Buffer<u8> leaves_d(leaves_a.getVolume());
239 for(s32 i=0; i<leaves_a.getVolume(); i++)
242 // Force leaves at near the end of the trunk
245 for(s16 z=-d; z<=d; z++)
246 for(s16 y=-d; y<=d; y++)
247 for(s16 x=-d; x<=d; x++)
249 leaves_d[leaves_a.index(v3s16(x,y,z))] = 1;
253 // Add leaves randomly
254 for(u32 iii=0; iii<30; iii++)
259 myrand_range(leaves_a.MinEdge.X, leaves_a.MaxEdge.X-d),
260 myrand_range(leaves_a.MinEdge.Y, leaves_a.MaxEdge.Y-d),
261 myrand_range(leaves_a.MinEdge.Z, leaves_a.MaxEdge.Z-d)
264 for(s16 z=0; z<=d; z++)
265 for(s16 y=0; y<=d; y++)
266 for(s16 x=0; x<=d; x++)
268 leaves_d[leaves_a.index(p+v3s16(x,y,z))] = 1;
272 // Blit leaves to vmanip
273 for(s16 z=leaves_a.MinEdge.Z; z<=leaves_a.MaxEdge.Z; z++)
274 for(s16 y=leaves_a.MinEdge.Y; y<=leaves_a.MaxEdge.Y; y++)
275 for(s16 x=leaves_a.MinEdge.X; x<=leaves_a.MaxEdge.X; x++)
279 if(vmanip.m_area.contains(p) == false)
281 u32 vi = vmanip.m_area.index(p);
282 if(vmanip.m_data[vi].getContent() != CONTENT_AIR
283 && vmanip.m_data[vi].getContent() != CONTENT_IGNORE)
285 u32 i = leaves_a.index(x,y,z);
287 vmanip.m_data[vi] = leavesnode;
291 static void make_papyrus(VoxelManipulator &vmanip, v3s16 p0,
292 INodeDefManager *ndef)
294 MapNode papyrusnode(ndef->getId("mapgen_papyrus"));
296 s16 trunk_h = myrand_range(2, 3);
298 for(s16 ii=0; ii<trunk_h; ii++)
300 if(vmanip.m_area.contains(p1))
301 vmanip.m_data[vmanip.m_area.index(p1)] = papyrusnode;
306 static void make_cactus(VoxelManipulator &vmanip, v3s16 p0,
307 INodeDefManager *ndef)
309 MapNode cactusnode(ndef->getId("mapgen_cactus"));
313 for(s16 ii=0; ii<trunk_h; ii++)
315 if(vmanip.m_area.contains(p1))
316 vmanip.m_data[vmanip.m_area.index(p1)] = cactusnode;
324 Dungeon making routines
327 #define VMANIP_FLAG_DUNGEON_INSIDE VOXELFLAG_CHECKED1
328 #define VMANIP_FLAG_DUNGEON_PRESERVE VOXELFLAG_CHECKED2
329 #define VMANIP_FLAG_DUNGEON_UNTOUCHABLE (\
330 VMANIP_FLAG_DUNGEON_INSIDE|VMANIP_FLAG_DUNGEON_PRESERVE)
332 static void make_room1(VoxelManipulator &vmanip, v3s16 roomsize, v3s16 roomplace,
333 INodeDefManager *ndef)
336 for(s16 z=0; z<roomsize.Z; z++)
337 for(s16 y=0; y<roomsize.Y; y++)
340 v3s16 p = roomplace + v3s16(0,y,z);
341 if(vmanip.m_area.contains(p) == false)
343 u32 vi = vmanip.m_area.index(p);
344 if(vmanip.m_flags[vi] & VMANIP_FLAG_DUNGEON_UNTOUCHABLE)
346 vmanip.m_data[vi] = MapNode(ndef->getId("mapgen_cobble"));
349 v3s16 p = roomplace + v3s16(roomsize.X-1,y,z);
350 if(vmanip.m_area.contains(p) == false)
352 u32 vi = vmanip.m_area.index(p);
353 if(vmanip.m_flags[vi] & VMANIP_FLAG_DUNGEON_UNTOUCHABLE)
355 vmanip.m_data[vi] = MapNode(ndef->getId("mapgen_cobble"));
360 for(s16 x=0; x<roomsize.X; x++)
361 for(s16 y=0; y<roomsize.Y; y++)
364 v3s16 p = roomplace + v3s16(x,y,0);
365 if(vmanip.m_area.contains(p) == false)
367 u32 vi = vmanip.m_area.index(p);
368 if(vmanip.m_flags[vi] & VMANIP_FLAG_DUNGEON_UNTOUCHABLE)
370 vmanip.m_data[vi] = MapNode(ndef->getId("mapgen_cobble"));
373 v3s16 p = roomplace + v3s16(x,y,roomsize.Z-1);
374 if(vmanip.m_area.contains(p) == false)
376 u32 vi = vmanip.m_area.index(p);
377 if(vmanip.m_flags[vi] & VMANIP_FLAG_DUNGEON_UNTOUCHABLE)
379 vmanip.m_data[vi] = MapNode(ndef->getId("mapgen_cobble"));
383 // Make +-Y walls (floor and ceiling)
384 for(s16 z=0; z<roomsize.Z; z++)
385 for(s16 x=0; x<roomsize.X; x++)
388 v3s16 p = roomplace + v3s16(x,0,z);
389 if(vmanip.m_area.contains(p) == false)
391 u32 vi = vmanip.m_area.index(p);
392 if(vmanip.m_flags[vi] & VMANIP_FLAG_DUNGEON_UNTOUCHABLE)
394 vmanip.m_data[vi] = MapNode(ndef->getId("mapgen_cobble"));
397 v3s16 p = roomplace + v3s16(x,roomsize.Y-1,z);
398 if(vmanip.m_area.contains(p) == false)
400 u32 vi = vmanip.m_area.index(p);
401 if(vmanip.m_flags[vi] & VMANIP_FLAG_DUNGEON_UNTOUCHABLE)
403 vmanip.m_data[vi] = MapNode(ndef->getId("mapgen_cobble"));
408 for(s16 z=1; z<roomsize.Z-1; z++)
409 for(s16 y=1; y<roomsize.Y-1; y++)
410 for(s16 x=1; x<roomsize.X-1; x++)
412 v3s16 p = roomplace + v3s16(x,y,z);
413 if(vmanip.m_area.contains(p) == false)
415 u32 vi = vmanip.m_area.index(p);
416 vmanip.m_flags[vi] |= VMANIP_FLAG_DUNGEON_UNTOUCHABLE;
417 vmanip.m_data[vi] = MapNode(CONTENT_AIR);
421 static void make_fill(VoxelManipulator &vmanip, v3s16 place, v3s16 size,
422 u8 avoid_flags, MapNode n, u8 or_flags)
424 for(s16 z=0; z<size.Z; z++)
425 for(s16 y=0; y<size.Y; y++)
426 for(s16 x=0; x<size.X; x++)
428 v3s16 p = place + v3s16(x,y,z);
429 if(vmanip.m_area.contains(p) == false)
431 u32 vi = vmanip.m_area.index(p);
432 if(vmanip.m_flags[vi] & avoid_flags)
434 vmanip.m_flags[vi] |= or_flags;
435 vmanip.m_data[vi] = n;
439 static void make_hole1(VoxelManipulator &vmanip, v3s16 place,
440 INodeDefManager *ndef)
442 make_fill(vmanip, place, v3s16(1,2,1), 0, MapNode(CONTENT_AIR),
443 VMANIP_FLAG_DUNGEON_INSIDE);
446 static void make_door1(VoxelManipulator &vmanip, v3s16 doorplace, v3s16 doordir,
447 INodeDefManager *ndef)
449 make_hole1(vmanip, doorplace, ndef);
450 // Place torch (for testing)
451 //vmanip.m_data[vmanip.m_area.index(doorplace)] = MapNode(ndef->getId("mapgen_torch"));
454 static v3s16 rand_ortho_dir(PseudoRandom &random)
456 if(random.next()%2==0)
457 return random.next()%2 ? v3s16(-1,0,0) : v3s16(1,0,0);
459 return random.next()%2 ? v3s16(0,0,-1) : v3s16(0,0,1);
462 static v3s16 turn_xz(v3s16 olddir, int t)
482 static v3s16 random_turn(PseudoRandom &random, v3s16 olddir)
484 int turn = random.range(0,2);
493 dir = turn_xz(olddir, 0);
496 dir = turn_xz(olddir, 1);
500 static void make_corridor(VoxelManipulator &vmanip, v3s16 doorplace,
501 v3s16 doordir, v3s16 &result_place, v3s16 &result_dir,
502 PseudoRandom &random, INodeDefManager *ndef)
504 make_hole1(vmanip, doorplace, ndef);
505 v3s16 p0 = doorplace;
509 length = random.range(1,13);
511 length = random.range(1,6);
512 length = random.range(1,13);
513 u32 partlength = random.range(1,13);
516 if(random.next()%2 == 0 && partlength >= 3)
517 make_stairs = random.next()%2 ? 1 : -1;
518 for(u32 i=0; i<length; i++)
524 /*// If already empty
525 if(vmanip.getNodeNoExNoEmerge(p).getContent()
527 && vmanip.getNodeNoExNoEmerge(p+v3s16(0,1,0)).getContent()
532 if(vmanip.m_area.contains(p) == true
533 && vmanip.m_area.contains(p+v3s16(0,1,0)) == true)
537 make_fill(vmanip, p+v3s16(-1,-1,-1), v3s16(3,5,3),
538 VMANIP_FLAG_DUNGEON_UNTOUCHABLE, MapNode(ndef->getId("mapgen_cobble")), 0);
539 make_fill(vmanip, p, v3s16(1,2,1), 0, MapNode(CONTENT_AIR),
540 VMANIP_FLAG_DUNGEON_INSIDE);
541 make_fill(vmanip, p-dir, v3s16(1,2,1), 0, MapNode(CONTENT_AIR),
542 VMANIP_FLAG_DUNGEON_INSIDE);
546 make_fill(vmanip, p+v3s16(-1,-1,-1), v3s16(3,4,3),
547 VMANIP_FLAG_DUNGEON_UNTOUCHABLE, MapNode(ndef->getId("mapgen_cobble")), 0);
548 make_hole1(vmanip, p, ndef);
549 /*make_fill(vmanip, p, v3s16(1,2,1), 0, MapNode(CONTENT_AIR),
550 VMANIP_FLAG_DUNGEON_INSIDE);*/
557 // Can't go here, turn away
558 dir = turn_xz(dir, random.range(0,1));
559 make_stairs = -make_stairs;
561 partlength = random.range(1,length);
566 if(partcount >= partlength)
570 dir = random_turn(random, dir);
572 partlength = random.range(1,length);
575 if(random.next()%2 == 0 && partlength >= 3)
576 make_stairs = random.next()%2 ? 1 : -1;
587 RoomWalker(VoxelManipulator &vmanip_, v3s16 pos, PseudoRandom &random,
588 INodeDefManager *ndef):
599 m_dir = rand_ortho_dir(m_random);
602 void setPos(v3s16 pos)
607 void setDir(v3s16 dir)
612 bool findPlaceForDoor(v3s16 &result_place, v3s16 &result_dir)
614 for(u32 i=0; i<100; i++)
616 v3s16 p = m_pos + m_dir;
617 v3s16 p1 = p + v3s16(0,1,0);
618 if(vmanip.m_area.contains(p) == false
619 || vmanip.m_area.contains(p1) == false
625 if(vmanip.getNodeNoExNoEmerge(p).getContent()
626 == m_ndef->getId("mapgen_cobble")
627 && vmanip.getNodeNoExNoEmerge(p1).getContent()
628 == m_ndef->getId("mapgen_cobble"))
630 // Found wall, this is a good place!
633 // Randomize next direction
638 Determine where to move next
640 // Jump one up if the actual space is there
641 if(vmanip.getNodeNoExNoEmerge(p+v3s16(0,0,0)).getContent()
642 == m_ndef->getId("mapgen_cobble")
643 && vmanip.getNodeNoExNoEmerge(p+v3s16(0,1,0)).getContent()
645 && vmanip.getNodeNoExNoEmerge(p+v3s16(0,2,0)).getContent()
648 // Jump one down if the actual space is there
649 if(vmanip.getNodeNoExNoEmerge(p+v3s16(0,1,0)).getContent()
650 == m_ndef->getId("mapgen_cobble")
651 && vmanip.getNodeNoExNoEmerge(p+v3s16(0,0,0)).getContent()
653 && vmanip.getNodeNoExNoEmerge(p+v3s16(0,-1,0)).getContent()
656 // Check if walking is now possible
657 if(vmanip.getNodeNoExNoEmerge(p).getContent()
659 || vmanip.getNodeNoExNoEmerge(p+v3s16(0,1,0)).getContent()
662 // Cannot continue walking here
672 bool findPlaceForRoomDoor(v3s16 roomsize, v3s16 &result_doorplace,
673 v3s16 &result_doordir, v3s16 &result_roomplace)
675 for(s16 trycount=0; trycount<30; trycount++)
679 bool r = findPlaceForDoor(doorplace, doordir);
683 // X east, Z north, Y up
685 if(doordir == v3s16(1,0,0)) // X+
686 roomplace = doorplace +
687 v3s16(0,-1,m_random.range(-roomsize.Z+2,-2));
688 if(doordir == v3s16(-1,0,0)) // X-
689 roomplace = doorplace +
690 v3s16(-roomsize.X+1,-1,m_random.range(-roomsize.Z+2,-2));
691 if(doordir == v3s16(0,0,1)) // Z+
692 roomplace = doorplace +
693 v3s16(m_random.range(-roomsize.X+2,-2),-1,0);
694 if(doordir == v3s16(0,0,-1)) // Z-
695 roomplace = doorplace +
696 v3s16(m_random.range(-roomsize.X+2,-2),-1,-roomsize.Z+1);
699 if(doordir == v3s16(1,0,0)) // X+
700 roomplace = doorplace + v3s16(0,-1,-roomsize.Z/2);
701 if(doordir == v3s16(-1,0,0)) // X-
702 roomplace = doorplace + v3s16(-roomsize.X+1,-1,-roomsize.Z/2);
703 if(doordir == v3s16(0,0,1)) // Z+
704 roomplace = doorplace + v3s16(-roomsize.X/2,-1,0);
705 if(doordir == v3s16(0,0,-1)) // Z-
706 roomplace = doorplace + v3s16(-roomsize.X/2,-1,-roomsize.Z+1);
711 for(s16 z=1; z<roomsize.Z-1; z++)
712 for(s16 y=1; y<roomsize.Y-1; y++)
713 for(s16 x=1; x<roomsize.X-1; x++)
715 v3s16 p = roomplace + v3s16(x,y,z);
716 if(vmanip.m_area.contains(p) == false)
721 if(vmanip.m_flags[vmanip.m_area.index(p)]
722 & VMANIP_FLAG_DUNGEON_INSIDE)
733 result_doorplace = doorplace;
734 result_doordir = doordir;
735 result_roomplace = roomplace;
742 VoxelManipulator &vmanip;
745 PseudoRandom &m_random;
746 INodeDefManager *m_ndef;
749 static void make_dungeon1(VoxelManipulator &vmanip, PseudoRandom &random,
750 INodeDefManager *ndef)
752 v3s16 areasize = vmanip.m_area.getExtent();
757 Find place for first room
760 for(u32 i=0; i<100; i++)
762 roomsize = v3s16(random.range(4,8),random.range(4,6),random.range(4,8));
763 roomplace = vmanip.m_area.MinEdge + v3s16(
764 random.range(0,areasize.X-roomsize.X-1),
765 random.range(0,areasize.Y-roomsize.Y-1),
766 random.range(0,areasize.Z-roomsize.Z-1));
768 Check that we're not putting the room to an unknown place,
769 otherwise it might end up floating in the air
772 for(s16 z=1; z<roomsize.Z-1; z++)
773 for(s16 y=1; y<roomsize.Y-1; y++)
774 for(s16 x=1; x<roomsize.X-1; x++)
776 v3s16 p = roomplace + v3s16(x,y,z);
777 u32 vi = vmanip.m_area.index(p);
778 if(vmanip.m_flags[vi] & VMANIP_FLAG_DUNGEON_INSIDE)
783 if(vmanip.m_data[vi].getContent() == CONTENT_IGNORE)
797 Stores the center position of the last room made, so that
798 a new corridor can be started from the last room instead of
799 the new room, if chosen so.
801 v3s16 last_room_center = roomplace+v3s16(roomsize.X/2,1,roomsize.Z/2);
803 u32 room_count = random.range(2,7);
804 for(u32 i=0; i<room_count; i++)
806 // Make a room to the determined place
807 make_room1(vmanip, roomsize, roomplace, ndef);
809 v3s16 room_center = roomplace + v3s16(roomsize.X/2,1,roomsize.Z/2);
811 // Place torch at room center (for testing)
812 //vmanip.m_data[vmanip.m_area.index(room_center)] = MapNode(ndef->getId("mapgen_torch"));
815 if(i == room_count-1)
818 // Determine walker start position
820 bool start_in_last_room = (random.range(0,2)!=0);
821 //bool start_in_last_room = true;
823 v3s16 walker_start_place;
825 if(start_in_last_room)
827 walker_start_place = last_room_center;
831 walker_start_place = room_center;
832 // Store center of current room as the last one
833 last_room_center = room_center;
836 // Create walker and find a place for a door
837 RoomWalker walker(vmanip, walker_start_place, random, ndef);
840 bool r = walker.findPlaceForDoor(doorplace, doordir);
844 if(random.range(0,1)==0)
846 make_door1(vmanip, doorplace, doordir, ndef);
848 // Don't actually make a door
849 doorplace -= doordir;
851 // Make a random corridor starting from the door
853 v3s16 corridor_end_dir;
854 make_corridor(vmanip, doorplace, doordir, corridor_end,
855 corridor_end_dir, random, ndef);
857 // Find a place for a random sized room
858 roomsize = v3s16(random.range(4,8),random.range(4,6),random.range(4,8));
859 walker.setPos(corridor_end);
860 walker.setDir(corridor_end_dir);
861 r = walker.findPlaceForRoomDoor(roomsize, doorplace, doordir, roomplace);
865 if(random.range(0,1)==0)
867 make_door1(vmanip, doorplace, doordir, ndef);
869 // Don't actually make a door
870 roomplace -= doordir;
877 static void make_nc(VoxelManipulator &vmanip, PseudoRandom &random,
878 INodeDefManager *ndef)
882 s32 r = random.range(0, 3);
884 dir = v3s16( 1, 0, 0);
888 dir = v3s16(-1, 0, 0);
892 dir = v3s16( 0, 0, 1);
896 dir = v3s16( 0, 0,-1);
899 v3s16 p = vmanip.m_area.MinEdge + v3s16(
900 16+random.range(0,15),
901 16+random.range(0,15),
902 16+random.range(0,15));
903 vmanip.m_data[vmanip.m_area.index(p)] = MapNode(ndef->getId("mapgen_nyancat"), facedir_i);
904 u32 length = random.range(3,15);
905 for(u32 j=0; j<length; j++)
908 vmanip.m_data[vmanip.m_area.index(p)] = MapNode(ndef->getId("mapgen_nyancat_rainbow"));
914 Noise functions. Make sure seed is mangled differently in each one.
919 Scaling the output of the noise function affects the overdrive of the
920 contour function, which affects the shape of the output considerably.
922 #define CAVE_NOISE_SCALE 12.0
923 //#define CAVE_NOISE_SCALE 10.0
924 //#define CAVE_NOISE_SCALE 7.5
925 //#define CAVE_NOISE_SCALE 5.0
926 //#define CAVE_NOISE_SCALE 1.0
928 //#define CAVE_NOISE_THRESHOLD (2.5/CAVE_NOISE_SCALE)
929 #define CAVE_NOISE_THRESHOLD (1.5/CAVE_NOISE_SCALE)
931 NoiseParams get_cave_noise1_params(u64 seed)
933 /*return NoiseParams(NOISE_PERLIN_CONTOUR, seed+52534, 5, 0.7,
934 200, CAVE_NOISE_SCALE);*/
935 /*return NoiseParams(NOISE_PERLIN_CONTOUR, seed+52534, 4, 0.7,
936 100, CAVE_NOISE_SCALE);*/
937 /*return NoiseParams(NOISE_PERLIN_CONTOUR, seed+52534, 5, 0.6,
938 100, CAVE_NOISE_SCALE);*/
939 /*return NoiseParams(NOISE_PERLIN_CONTOUR, seed+52534, 5, 0.3,
940 100, CAVE_NOISE_SCALE);*/
941 return NoiseParams(NOISE_PERLIN_CONTOUR, seed+52534, 4, 0.5,
942 50, CAVE_NOISE_SCALE);
943 //return NoiseParams(NOISE_CONSTANT_ONE);
946 NoiseParams get_cave_noise2_params(u64 seed)
948 /*return NoiseParams(NOISE_PERLIN_CONTOUR_FLIP_YZ, seed+10325, 5, 0.7,
949 200, CAVE_NOISE_SCALE);*/
950 /*return NoiseParams(NOISE_PERLIN_CONTOUR_FLIP_YZ, seed+10325, 4, 0.7,
951 100, CAVE_NOISE_SCALE);*/
952 /*return NoiseParams(NOISE_PERLIN_CONTOUR_FLIP_YZ, seed+10325, 5, 0.3,
953 100, CAVE_NOISE_SCALE);*/
954 return NoiseParams(NOISE_PERLIN_CONTOUR_FLIP_YZ, seed+10325, 4, 0.5,
955 50, CAVE_NOISE_SCALE);
956 //return NoiseParams(NOISE_CONSTANT_ONE);
959 NoiseParams get_ground_noise1_params(u64 seed)
961 return NoiseParams(NOISE_PERLIN, seed+983240, 4,
965 NoiseParams get_ground_crumbleness_params(u64 seed)
967 return NoiseParams(NOISE_PERLIN, seed+34413, 3,
971 NoiseParams get_ground_wetness_params(u64 seed)
973 return NoiseParams(NOISE_PERLIN, seed+32474, 4,
977 bool is_cave(u64 seed, v3s16 p)
979 double d1 = noise3d_param(get_cave_noise1_params(seed), p.X,p.Y,p.Z);
980 double d2 = noise3d_param(get_cave_noise2_params(seed), p.X,p.Y,p.Z);
981 return d1*d2 > CAVE_NOISE_THRESHOLD;
985 Ground density noise shall be interpreted by using this.
987 TODO: No perlin noises here, they should be outsourced
989 NOTE: The speed of these actually isn't terrible
991 bool val_is_ground(double ground_noise1_val, v3s16 p, u64 seed)
993 //return ((double)p.Y < ground_noise1_val);
995 double f = 0.55 + noise2d_perlin(
996 0.5+(float)p.X/250, 0.5+(float)p.Z/250,
997 seed+920381, 3, 0.45);
1002 double h = WATER_LEVEL + 10 * noise2d_perlin(
1003 0.5+(float)p.X/250, 0.5+(float)p.Z/250,
1004 seed+84174, 4, 0.5);
1007 return ((double)p.Y - h < ground_noise1_val * f);
1011 Queries whether a position is ground or not.
1013 bool is_ground(u64 seed, v3s16 p)
1015 double val1 = noise3d_param(get_ground_noise1_params(seed), p.X,p.Y,p.Z);
1016 return val_is_ground(val1, p, seed);
1020 // Amount of trees per area in nodes
1021 double tree_amount_2d(u64 seed, v2s16 p)
1023 /*double noise = noise2d_perlin(
1024 0.5+(float)p.X/250, 0.5+(float)p.Y/250,
1026 double noise = noise2d_perlin(
1027 0.5+(float)p.X/125, 0.5+(float)p.Y/125,
1029 double zeroval = -0.39;
1033 return 0.04 * (noise-zeroval) / (1.0-zeroval);
1037 double surface_humidity_2d(u64 seed, v2s16 p)
1039 double noise = noise2d_perlin(
1040 0.5+(float)p.X/500, 0.5+(float)p.Y/500,
1041 seed+72384, 4, 0.66);
1042 noise = (noise + 1.0)/2.0;
1051 Incrementally find ground level from 3d noise
1053 s16 find_ground_level_from_noise(u64 seed, v2s16 p2d, s16 precision)
1055 // Start a bit fuzzy to make averaging lower precision values
1057 s16 level = myrand_range(-precision/2, precision/2);
1058 s16 dec[] = {31000, 100, 20, 4, 1, 0};
1060 for(i = 1; dec[i] != 0 && precision <= dec[i]; i++)
1062 // First find non-ground by going upwards
1063 // Don't stop in caves.
1065 s16 max = level+dec[i-1]*2;
1066 v3s16 p(p2d.X, level, p2d.Y);
1067 for(; p.Y < max; p.Y += dec[i])
1069 if(!is_ground(seed, p))
1076 // Then find ground by going downwards from there.
1077 // Go in caves, too, when precision is 1.
1079 s16 min = level-dec[i-1]*2;
1080 v3s16 p(p2d.X, level, p2d.Y);
1081 for(; p.Y>min; p.Y-=dec[i])
1083 bool ground = is_ground(seed, p);
1084 /*if(dec[i] == 1 && is_cave(seed, p))
1095 // This is more like the actual ground level
1096 level += dec[i-1]/2;
1101 double get_sector_average_ground_level(u64 seed, v2s16 sectorpos, double p=4);
1103 double get_sector_average_ground_level(u64 seed, v2s16 sectorpos, double p)
1105 v2s16 node_min = sectorpos*MAP_BLOCKSIZE;
1106 v2s16 node_max = (sectorpos+v2s16(1,1))*MAP_BLOCKSIZE-v2s16(1,1);
1108 a += find_ground_level_from_noise(seed,
1109 v2s16(node_min.X, node_min.Y), p);
1110 a += find_ground_level_from_noise(seed,
1111 v2s16(node_min.X, node_max.Y), p);
1112 a += find_ground_level_from_noise(seed,
1113 v2s16(node_max.X, node_max.Y), p);
1114 a += find_ground_level_from_noise(seed,
1115 v2s16(node_max.X, node_min.Y), p);
1116 a += find_ground_level_from_noise(seed,
1117 v2s16(node_min.X+MAP_BLOCKSIZE/2, node_min.Y+MAP_BLOCKSIZE/2), p);
1122 double get_sector_maximum_ground_level(u64 seed, v2s16 sectorpos, double p=4);
1124 double get_sector_maximum_ground_level(u64 seed, v2s16 sectorpos, double p)
1126 v2s16 node_min = sectorpos*MAP_BLOCKSIZE;
1127 v2s16 node_max = (sectorpos+v2s16(1,1))*MAP_BLOCKSIZE-v2s16(1,1);
1130 a = MYMAX(a, find_ground_level_from_noise(seed,
1131 v2s16(node_min.X, node_min.Y), p));
1132 a = MYMAX(a, find_ground_level_from_noise(seed,
1133 v2s16(node_min.X, node_max.Y), p));
1134 a = MYMAX(a, find_ground_level_from_noise(seed,
1135 v2s16(node_max.X, node_max.Y), p));
1136 a = MYMAX(a, find_ground_level_from_noise(seed,
1137 v2s16(node_min.X, node_min.Y), p));
1139 a = MYMAX(a, find_ground_level_from_noise(seed,
1140 v2s16(node_min.X+MAP_BLOCKSIZE/2, node_min.Y+MAP_BLOCKSIZE/2), p));
1141 // Side middle points
1142 a = MYMAX(a, find_ground_level_from_noise(seed,
1143 v2s16(node_min.X+MAP_BLOCKSIZE/2, node_min.Y), p));
1144 a = MYMAX(a, find_ground_level_from_noise(seed,
1145 v2s16(node_min.X+MAP_BLOCKSIZE/2, node_max.Y), p));
1146 a = MYMAX(a, find_ground_level_from_noise(seed,
1147 v2s16(node_min.X, node_min.Y+MAP_BLOCKSIZE/2), p));
1148 a = MYMAX(a, find_ground_level_from_noise(seed,
1149 v2s16(node_max.X, node_min.Y+MAP_BLOCKSIZE/2), p));
1153 double get_sector_minimum_ground_level(u64 seed, v2s16 sectorpos, double p=4);
1155 double get_sector_minimum_ground_level(u64 seed, v2s16 sectorpos, double p)
1157 v2s16 node_min = sectorpos*MAP_BLOCKSIZE;
1158 v2s16 node_max = (sectorpos+v2s16(1,1))*MAP_BLOCKSIZE-v2s16(1,1);
1161 a = MYMIN(a, find_ground_level_from_noise(seed,
1162 v2s16(node_min.X, node_min.Y), p));
1163 a = MYMIN(a, find_ground_level_from_noise(seed,
1164 v2s16(node_min.X, node_max.Y), p));
1165 a = MYMIN(a, find_ground_level_from_noise(seed,
1166 v2s16(node_max.X, node_max.Y), p));
1167 a = MYMIN(a, find_ground_level_from_noise(seed,
1168 v2s16(node_min.X, node_min.Y), p));
1170 a = MYMIN(a, find_ground_level_from_noise(seed,
1171 v2s16(node_min.X+MAP_BLOCKSIZE/2, node_min.Y+MAP_BLOCKSIZE/2), p));
1172 // Side middle points
1173 a = MYMIN(a, find_ground_level_from_noise(seed,
1174 v2s16(node_min.X+MAP_BLOCKSIZE/2, node_min.Y), p));
1175 a = MYMIN(a, find_ground_level_from_noise(seed,
1176 v2s16(node_min.X+MAP_BLOCKSIZE/2, node_max.Y), p));
1177 a = MYMIN(a, find_ground_level_from_noise(seed,
1178 v2s16(node_min.X, node_min.Y+MAP_BLOCKSIZE/2), p));
1179 a = MYMIN(a, find_ground_level_from_noise(seed,
1180 v2s16(node_max.X, node_min.Y+MAP_BLOCKSIZE/2), p));
1185 // Required by mapgen.h
1186 bool block_is_underground(u64 seed, v3s16 blockpos)
1188 /*s16 minimum_groundlevel = (s16)get_sector_minimum_ground_level(
1189 seed, v2s16(blockpos.X, blockpos.Z));*/
1190 // Nah, this is just a heuristic, just return something
1191 s16 minimum_groundlevel = WATER_LEVEL;
1193 if(blockpos.Y*MAP_BLOCKSIZE + MAP_BLOCKSIZE <= minimum_groundlevel)
1199 #define AVERAGE_MUD_AMOUNT 4
1201 double base_rock_level_2d(u64 seed, v2s16 p)
1203 // The base ground level
1204 double base = (double)WATER_LEVEL - (double)AVERAGE_MUD_AMOUNT
1205 + 20. * noise2d_perlin(
1206 0.5+(float)p.X/250., 0.5+(float)p.Y/250.,
1207 seed+82341, 5, 0.6);
1209 /*// A bit hillier one
1210 double base2 = WATER_LEVEL - 4.0 + 40. * noise2d_perlin(
1211 0.5+(float)p.X/250., 0.5+(float)p.Y/250.,
1212 seed+93413, 6, 0.69);
1216 // Higher ground level
1217 double higher = (double)WATER_LEVEL + 20. + 16. * noise2d_perlin(
1218 0.5+(float)p.X/500., 0.5+(float)p.Y/500.,
1219 seed+85039, 5, 0.6);
1220 //higher = 30; // For debugging
1222 // Limit higher to at least base
1226 // Steepness factor of cliffs
1227 double b = 0.85 + 0.5 * noise2d_perlin(
1228 0.5+(float)p.X/125., 0.5+(float)p.Y/125.,
1230 b = rangelim(b, 0.0, 1000.0);
1233 b = rangelim(b, 0.5, 1000.0);
1234 // Values 1.5...100 give quite horrible looking slopes
1235 if(b > 1.5 && b < 100.0){
1241 //dstream<<"b="<<b<<std::endl;
1245 // Offset to more low
1246 double a_off = -0.20;
1247 // High/low selector
1248 /*double a = 0.5 + b * (a_off + noise2d_perlin(
1249 0.5+(float)p.X/500., 0.5+(float)p.Y/500.,
1250 seed+4213, 6, 0.7));*/
1251 double a = (double)0.5 + b * (a_off + noise2d_perlin(
1252 0.5+(float)p.X/250., 0.5+(float)p.Y/250.,
1253 seed+4213, 5, 0.69));
1255 a = rangelim(a, 0.0, 1.0);
1257 //dstream<<"a="<<a<<std::endl;
1259 double h = base*(1.0-a) + higher*a;
1266 s16 find_ground_level_from_noise(u64 seed, v2s16 p2d, s16 precision)
1268 return base_rock_level_2d(seed, p2d) + AVERAGE_MUD_AMOUNT;
1271 double get_mud_add_amount(u64 seed, v2s16 p)
1273 return ((float)AVERAGE_MUD_AMOUNT + 2.0 * noise2d_perlin(
1274 0.5+(float)p.X/200, 0.5+(float)p.Y/200,
1275 seed+91013, 3, 0.55));
1278 bool get_have_beach(u64 seed, v2s16 p2d)
1280 // Determine whether to have sand here
1281 double sandnoise = noise2d_perlin(
1282 0.2+(float)p2d.X/250, 0.7+(float)p2d.Y/250,
1283 seed+59420, 3, 0.50);
1285 return (sandnoise > 0.15);
1294 BiomeType get_biome(u64 seed, v2s16 p2d)
1296 // Just do something very simple as for now
1297 double d = noise2d_perlin(
1298 0.6+(float)p2d.X/250, 0.2+(float)p2d.Y/250,
1299 seed+9130, 3, 0.50);
1302 if(d > 0.35 && (noise2d( p2d.X, p2d.Y, int(seed) ) + 1.0) > ( 0.45 - d ) * 20.0 )
1307 u32 get_blockseed(u64 seed, v3s16 p)
1309 s32 x=p.X, y=p.Y, z=p.Z;
1310 return (u32)(seed%0x100000000ULL) + z*38134234 + y*42123 + x*23;
1313 #define VMANIP_FLAG_CAVE VOXELFLAG_CHECKED1
1315 void make_block(BlockMakeData *data)
1319 //dstream<<"makeBlock: no-op"<<std::endl;
1323 assert(data->vmanip);
1324 assert(data->nodedef);
1325 assert(data->blockpos_requested.X >= data->blockpos_min.X &&
1326 data->blockpos_requested.Y >= data->blockpos_min.Y &&
1327 data->blockpos_requested.Z >= data->blockpos_min.Z);
1328 assert(data->blockpos_requested.X <= data->blockpos_max.X &&
1329 data->blockpos_requested.Y <= data->blockpos_max.Y &&
1330 data->blockpos_requested.Z <= data->blockpos_max.Z);
1332 INodeDefManager *ndef = data->nodedef;
1334 // Hack: use minimum block coordinates for old code that assumes
1336 v3s16 blockpos = data->blockpos_requested;
1338 /*dstream<<"makeBlock(): ("<<blockpos.X<<","<<blockpos.Y<<","
1339 <<blockpos.Z<<")"<<std::endl;*/
1341 v3s16 blockpos_min = data->blockpos_min;
1342 v3s16 blockpos_max = data->blockpos_max;
1343 v3s16 blockpos_full_min = blockpos_min - v3s16(1,1,1);
1344 v3s16 blockpos_full_max = blockpos_max + v3s16(1,1,1);
1346 ManualMapVoxelManipulator &vmanip = *(data->vmanip);
1347 // Area of central chunk
1348 v3s16 node_min = blockpos_min*MAP_BLOCKSIZE;
1349 v3s16 node_max = (blockpos_max+v3s16(1,1,1))*MAP_BLOCKSIZE-v3s16(1,1,1);
1350 // Full allocated area
1351 v3s16 full_node_min = (blockpos_min-1)*MAP_BLOCKSIZE;
1352 v3s16 full_node_max = (blockpos_max+2)*MAP_BLOCKSIZE-v3s16(1,1,1);
1354 v3s16 central_area_size = node_max - node_min + v3s16(1,1,1);
1356 const s16 max_spread_amount = MAP_BLOCKSIZE;
1358 int volume_blocks = (blockpos_max.X - blockpos_min.X + 1)
1359 * (blockpos_max.Y - blockpos_min.Y + 1)
1360 * (blockpos_max.Z - blockpos_max.Z + 1);
1362 int volume_nodes = volume_blocks *
1363 MAP_BLOCKSIZE*MAP_BLOCKSIZE*MAP_BLOCKSIZE;
1365 // Generated surface area
1366 //double gen_area_nodes = MAP_BLOCKSIZE*MAP_BLOCKSIZE * rel_volume;
1368 // Horribly wrong heuristic, but better than nothing
1369 bool block_is_underground = (WATER_LEVEL > node_max.Y);
1372 Create a block-specific seed
1374 u32 blockseed = get_blockseed(data->seed, full_node_min);
1377 Cache some ground type values for speed
1380 // Creates variables c_name=id and n_name=node
1381 #define CONTENT_VARIABLE(ndef, name)\
1382 content_t c_##name = ndef->getId("mapgen_" #name);\
1383 MapNode n_##name(c_##name);
1384 // Default to something else if was CONTENT_IGNORE
1385 #define CONTENT_VARIABLE_FALLBACK(name, dname)\
1386 if(c_##name == CONTENT_IGNORE){\
1387 c_##name = c_##dname;\
1388 n_##name = n_##dname;\
1391 CONTENT_VARIABLE(ndef, stone);
1392 CONTENT_VARIABLE(ndef, air);
1393 CONTENT_VARIABLE(ndef, water_source);
1394 CONTENT_VARIABLE(ndef, dirt);
1395 CONTENT_VARIABLE(ndef, sand);
1396 CONTENT_VARIABLE(ndef, gravel);
1397 CONTENT_VARIABLE(ndef, clay);
1398 CONTENT_VARIABLE(ndef, lava_source);
1399 CONTENT_VARIABLE(ndef, cobble);
1400 CONTENT_VARIABLE(ndef, mossycobble);
1401 CONTENT_VARIABLE(ndef, dirt_with_grass);
1402 CONTENT_VARIABLE(ndef, junglegrass);
1403 CONTENT_VARIABLE(ndef, stone_with_coal);
1404 CONTENT_VARIABLE(ndef, stone_with_iron);
1405 CONTENT_VARIABLE(ndef, mese);
1406 CONTENT_VARIABLE(ndef, desert_sand);
1407 CONTENT_VARIABLE_FALLBACK(desert_sand, sand);
1408 CONTENT_VARIABLE(ndef, desert_stone);
1409 CONTENT_VARIABLE_FALLBACK(desert_stone, stone);
1411 // Maximum height of the stone surface and obstacles.
1412 // This is used to guide the cave generation
1413 s16 stone_surface_max_y = 0;
1416 Generate general ground level to full area
1420 TimeTaker timer1("Generating ground level");
1422 for(s16 x=node_min.X; x<=node_max.X; x++)
1423 for(s16 z=node_min.Z; z<=node_max.Z; z++)
1426 v2s16 p2d = v2s16(x,z);
1429 Skip of already generated
1432 v3s16 p(p2d.X, node_min.Y, p2d.Y);
1433 if(vmanip.m_data[vmanip.m_area.index(p)].d != CONTENT_AIR)
1437 // Ground height at this point
1438 float surface_y_f = 0.0;
1440 // Use perlin noise for ground height
1441 surface_y_f = base_rock_level_2d(data->seed, p2d);
1443 /*// Experimental stuff
1445 float a = highlands_level_2d(data->seed, p2d);
1450 // Convert to integer
1451 s16 surface_y = (s16)surface_y_f;
1454 if(surface_y > stone_surface_max_y)
1455 stone_surface_max_y = surface_y;
1457 BiomeType bt = get_biome(data->seed, p2d);
1459 Fill ground with stone
1462 // Use fast index incrementing
1463 v3s16 em = vmanip.m_area.getExtent();
1464 u32 i = vmanip.m_area.index(v3s16(p2d.X, node_min.Y, p2d.Y));
1465 for(s16 y=node_min.Y; y<=node_max.Y; y++)
1467 if(vmanip.m_data[i].getContent() == CONTENT_IGNORE){
1469 if(y > WATER_LEVEL && bt == BT_DESERT)
1470 vmanip.m_data[i] = n_desert_stone;
1472 vmanip.m_data[i] = n_stone;
1473 } else if(y <= WATER_LEVEL){
1474 vmanip.m_data[i] = MapNode(c_water_source);
1476 vmanip.m_data[i] = MapNode(c_air);
1479 vmanip.m_area.add_y(em, i, 1);
1487 // Limit dirt flow area by 1 because mud is flown into neighbors.
1488 assert(central_area_size.X == central_area_size.Z);
1489 s16 mudflow_minpos = 0-max_spread_amount+1;
1490 s16 mudflow_maxpos = central_area_size.X+max_spread_amount-2;
1493 Loop this part, it will make stuff look older and newer nicely
1496 const u32 age_loops = 2;
1497 for(u32 i_age=0; i_age<age_loops; i_age++)
1499 /******************************
1500 BEGINNING OF AGING LOOP
1501 ******************************/
1506 //TimeTaker timer1("caves");
1509 Make caves (this code is relatively horrible)
1511 double cave_amount = 6.0 + 6.0 * noise2d_perlin(
1512 0.5+(double)node_min.X/250, 0.5+(double)node_min.Y/250,
1513 data->seed+34329, 3, 0.50);
1514 cave_amount = MYMAX(0.0, cave_amount);
1515 u32 caves_count = cave_amount * volume_nodes / 50000;
1516 u32 bruises_count = 1;
1517 PseudoRandom ps(blockseed+21343);
1518 PseudoRandom ps2(blockseed+1032);
1519 if(ps.range(1, 6) == 1)
1520 bruises_count = ps.range(0, ps.range(0, 2));
1521 if(get_biome(data->seed, v2s16(node_min.X, node_min.Y)) == BT_DESERT){
1525 for(u32 jj=0; jj<caves_count+bruises_count; jj++)
1527 bool large_cave = (jj >= caves_count);
1528 s16 min_tunnel_diameter = 2;
1529 s16 max_tunnel_diameter = ps.range(2,6);
1530 int dswitchint = ps.range(1,14);
1531 u16 tunnel_routepoints = 0;
1532 int part_max_length_rs = 0;
1534 part_max_length_rs = ps.range(2,4);
1535 tunnel_routepoints = ps.range(5, ps.range(15,30));
1536 min_tunnel_diameter = 5;
1537 max_tunnel_diameter = ps.range(7, ps.range(8,24));
1539 part_max_length_rs = ps.range(2,9);
1540 tunnel_routepoints = ps.range(10, ps.range(15,30));
1542 bool large_cave_is_flat = (ps.range(0,1) == 0);
1544 v3f main_direction(0,0,0);
1546 // Allowed route area size in nodes
1547 v3s16 ar = central_area_size;
1549 // Area starting point in nodes
1550 v3s16 of = node_min;
1553 //(this should be more than the maximum radius of the tunnel)
1554 //s16 insure = 5; // Didn't work with max_d = 20
1556 s16 more = max_spread_amount - max_tunnel_diameter/2 - insure;
1557 ar += v3s16(1,0,1) * more * 2;
1558 of -= v3s16(1,0,1) * more;
1560 s16 route_y_min = 0;
1561 // Allow half a diameter + 7 over stone surface
1562 s16 route_y_max = -of.Y + stone_surface_max_y + max_tunnel_diameter/2 + 7;
1564 /*// If caves, don't go through surface too often
1565 if(large_cave == false)
1566 route_y_max -= ps.range(0, max_tunnel_diameter*2);*/
1568 // Limit maximum to area
1569 route_y_max = rangelim(route_y_max, 0, ar.Y-1);
1573 /*// Minimum is at y=0
1574 route_y_min = -of.Y - 0;*/
1575 // Minimum is at y=max_tunnel_diameter/4
1576 //route_y_min = -of.Y + max_tunnel_diameter/4;
1577 //s16 min = -of.Y + max_tunnel_diameter/4;
1578 //s16 min = -of.Y + 0;
1580 if(node_min.Y < WATER_LEVEL && node_max.Y > WATER_LEVEL)
1582 min = WATER_LEVEL - max_tunnel_diameter/3 - of.Y;
1583 route_y_max = WATER_LEVEL + max_tunnel_diameter/3 - of.Y;
1585 route_y_min = ps.range(min, min + max_tunnel_diameter);
1586 route_y_min = rangelim(route_y_min, 0, route_y_max);
1589 /*dstream<<"route_y_min = "<<route_y_min
1590 <<", route_y_max = "<<route_y_max<<std::endl;*/
1592 s16 route_start_y_min = route_y_min;
1593 s16 route_start_y_max = route_y_max;
1595 // Start every 4th cave from surface when applicable
1596 /*bool coming_from_surface = false;
1597 if(node_min.Y <= 0 && node_max.Y >= 0){
1598 coming_from_surface = (jj % 4 == 0 && large_cave == false);
1599 if(coming_from_surface)
1600 route_start_y_min = -of.Y + stone_surface_max_y + 10;
1603 route_start_y_min = rangelim(route_start_y_min, 0, ar.Y-1);
1604 route_start_y_max = rangelim(route_start_y_max, route_start_y_min, ar.Y-1);
1606 // Randomize starting position
1608 (float)(ps.next()%ar.X)+0.5,
1609 (float)(ps.range(route_start_y_min, route_start_y_max))+0.5,
1610 (float)(ps.next()%ar.Z)+0.5
1613 v3s16 startp(orp.X, orp.Y, orp.Z);
1616 MapNode airnode(CONTENT_AIR);
1617 MapNode waternode(c_water_source);
1618 MapNode lavanode(c_lava_source);
1621 Generate some tunnel starting from orp
1624 for(u16 j=0; j<tunnel_routepoints; j++)
1626 if(j%dswitchint==0 && large_cave == false)
1628 main_direction = v3f(
1629 ((float)(ps.next()%20)-(float)10)/10,
1630 ((float)(ps.next()%20)-(float)10)/30,
1631 ((float)(ps.next()%20)-(float)10)/10
1633 main_direction *= (float)ps.range(0, 10)/10;
1637 s16 min_d = min_tunnel_diameter;
1638 s16 max_d = max_tunnel_diameter;
1639 s16 rs = ps.range(min_d, max_d);
1641 // Every second section is rough
1642 bool randomize_xz = (ps2.range(1,2) == 1);
1648 rs*part_max_length_rs,
1649 rs*part_max_length_rs/2,
1650 rs*part_max_length_rs
1656 rs*part_max_length_rs,
1657 ps.range(1, rs*part_max_length_rs),
1658 rs*part_max_length_rs
1665 (float)(ps.next()%(maxlen.X*1))-(float)maxlen.X/2,
1666 (float)(ps.next()%(maxlen.Y*1))-(float)maxlen.Y/2,
1667 (float)(ps.next()%(maxlen.Z*1))-(float)maxlen.Z/2
1670 // Jump downward sometimes
1671 if(!large_cave && ps.range(0,12) == 0)
1674 (float)(ps.next()%(maxlen.X*1))-(float)maxlen.X/2,
1675 (float)(ps.next()%(maxlen.Y*2))-(float)maxlen.Y*2/2,
1676 (float)(ps.next()%(maxlen.Z*1))-(float)maxlen.Z/2
1682 s16 h = find_ground_level_clever(vmanip,
1683 v2s16(p.X, p.Z), ndef);
1684 route_y_min = h - rs/3;
1685 route_y_max = h + rs;
1688 vec += main_direction;
1693 else if(rp.X >= ar.X)
1695 if(rp.Y < route_y_min)
1697 else if(rp.Y >= route_y_max)
1698 rp.Y = route_y_max-1;
1701 else if(rp.Z >= ar.Z)
1705 for(float f=0; f<1.0; f+=1.0/vec.getLength())
1707 v3f fp = orp + vec * f;
1708 fp.X += 0.1*ps.range(-10,10);
1709 fp.Z += 0.1*ps.range(-10,10);
1710 v3s16 cp(fp.X, fp.Y, fp.Z);
1715 d0 += ps.range(-1,1);
1716 d1 += ps.range(-1,1);
1718 for(s16 z0=d0; z0<=d1; z0++)
1720 s16 si = rs/2 - MYMAX(0, abs(z0)-rs/7-1);
1721 for(s16 x0=-si-ps.range(0,1); x0<=si-1+ps.range(0,1); x0++)
1723 s16 maxabsxz = MYMAX(abs(x0), abs(z0));
1724 s16 si2 = rs/2 - MYMAX(0, maxabsxz-rs/7-1);
1725 for(s16 y0=-si2; y0<=si2; y0++)
1727 /*// Make better floors in small caves
1728 if(y0 <= -rs/2 && rs<=7)
1730 if(large_cave_is_flat){
1731 // Make large caves not so tall
1732 if(rs > 7 && abs(y0) >= rs/3)
1742 if(vmanip.m_area.contains(p) == false)
1745 u32 i = vmanip.m_area.index(p);
1749 if(full_node_min.Y < WATER_LEVEL &&
1750 full_node_max.Y > WATER_LEVEL){
1751 if(p.Y <= WATER_LEVEL)
1752 vmanip.m_data[i] = waternode;
1754 vmanip.m_data[i] = airnode;
1755 } else if(full_node_max.Y < WATER_LEVEL){
1756 if(p.Y < startp.Y - 2)
1757 vmanip.m_data[i] = lavanode;
1759 vmanip.m_data[i] = airnode;
1761 vmanip.m_data[i] = airnode;
1764 // Don't replace air or water or lava or ignore
1765 if(vmanip.m_data[i].getContent() == CONTENT_IGNORE ||
1766 vmanip.m_data[i].getContent() == CONTENT_AIR ||
1767 vmanip.m_data[i].getContent() == c_water_source ||
1768 vmanip.m_data[i].getContent() == c_lava_source)
1771 vmanip.m_data[i] = airnode;
1774 vmanip.m_flags[i] |= VMANIP_FLAG_CAVE;
1792 TimeTaker timer1("add mud");
1795 Add mud to the central chunk
1798 for(s16 x=node_min.X; x<=node_max.X; x++)
1799 for(s16 z=node_min.Z; z<=node_max.Z; z++)
1801 // Node position in 2d
1802 v2s16 p2d = v2s16(x,z);
1804 // Randomize mud amount
1805 s16 mud_add_amount = get_mud_add_amount(data->seed, p2d) / 2.0 + 0.5;
1807 // Find ground level
1808 s16 surface_y = find_stone_level(vmanip, p2d, ndef);
1809 // Handle area not found
1810 if(surface_y == vmanip.m_area.MinEdge.Y - 1)
1813 MapNode addnode(c_dirt);
1814 BiomeType bt = get_biome(data->seed, p2d);
1817 addnode = MapNode(c_desert_sand);
1819 if(bt == BT_DESERT && surface_y + mud_add_amount <= WATER_LEVEL+1){
1820 addnode = MapNode(c_sand);
1821 } else if(mud_add_amount <= 0){
1822 mud_add_amount = 1 - mud_add_amount;
1823 addnode = MapNode(c_gravel);
1824 } else if(bt == BT_NORMAL && get_have_beach(data->seed, p2d) &&
1825 surface_y + mud_add_amount <= WATER_LEVEL+2){
1826 addnode = MapNode(c_sand);
1829 if(bt == BT_DESERT){
1831 mud_add_amount = MYMAX(0, mud_add_amount - (surface_y - 20)/5);
1836 If topmost node is grass, change it to mud.
1837 It might be if it was flown to there from a neighboring
1838 chunk and then converted.
1841 u32 i = vmanip.m_area.index(v3s16(p2d.X, surface_y, p2d.Y));
1842 MapNode *n = &vmanip.m_data[i];
1843 if(n->getContent() == c_dirt_with_grass)
1844 *n = MapNode(c_dirt);
1852 v3s16 em = vmanip.m_area.getExtent();
1853 s16 y_start = surface_y+1;
1854 u32 i = vmanip.m_area.index(v3s16(p2d.X, y_start, p2d.Y));
1855 for(s16 y=y_start; y<=node_max.Y; y++)
1857 if(mudcount >= mud_add_amount)
1860 MapNode &n = vmanip.m_data[i];
1864 vmanip.m_area.add_y(em, i, 1);
1874 Add blobs of dirt and gravel underground
1876 if(get_biome(data->seed, v2s16(node_min.X, node_min.Y)) == BT_NORMAL)
1878 PseudoRandom pr(blockseed+983);
1879 for(int i=0; i<volume_nodes/10/10/10; i++)
1881 bool only_fill_cave = (myrand_range(0,1) != 0);
1888 pr.range(node_min.X, node_max.X)-size.X/2,
1889 pr.range(node_min.Y, node_max.Y)-size.Y/2,
1890 pr.range(node_min.Z, node_max.Z)-size.Z/2
1893 if(p0.Y > -32 && pr.range(0,1) == 0)
1894 n1 = MapNode(c_dirt);
1896 n1 = MapNode(c_gravel);
1897 for(int x1=0; x1<size.X; x1++)
1898 for(int y1=0; y1<size.Y; y1++)
1899 for(int z1=0; z1<size.Z; z1++)
1901 v3s16 p = p0 + v3s16(x1,y1,z1);
1902 u32 i = vmanip.m_area.index(p);
1903 if(!vmanip.m_area.contains(i))
1905 // Cancel if not stone and not cave air
1906 if(vmanip.m_data[i].getContent() != c_stone &&
1907 !(vmanip.m_flags[i] & VMANIP_FLAG_CAVE))
1909 if(only_fill_cave && !(vmanip.m_flags[i] & VMANIP_FLAG_CAVE))
1911 vmanip.m_data[i] = n1;
1919 TimeTaker timer1("flow mud");
1922 Flow mud away from steep edges
1925 // Iterate a few times
1926 for(s16 k=0; k<3; k++)
1929 for(s16 x=mudflow_minpos; x<=mudflow_maxpos; x++)
1930 for(s16 z=mudflow_minpos; z<=mudflow_maxpos; z++)
1932 // Invert coordinates every 2nd iteration
1935 x = mudflow_maxpos - (x-mudflow_minpos);
1936 z = mudflow_maxpos - (z-mudflow_minpos);
1939 // Node position in 2d
1940 v2s16 p2d = v2s16(node_min.X, node_min.Z) + v2s16(x,z);
1942 v3s16 em = vmanip.m_area.getExtent();
1943 u32 i = vmanip.m_area.index(v3s16(p2d.X, node_max.Y, p2d.Y));
1946 while(y >= node_min.Y)
1953 for(; y>=node_min.Y; y--)
1955 n = &vmanip.m_data[i];
1956 //if(content_walkable(n->d))
1958 if(n->getContent() == c_dirt ||
1959 n->getContent() == c_dirt_with_grass ||
1960 n->getContent() == c_gravel)
1963 vmanip.m_area.add_y(em, i, -1);
1966 // Stop if out of area
1967 //if(vmanip.m_area.contains(i) == false)
1971 /*// If not mud, do nothing to it
1972 MapNode *n = &vmanip.m_data[i];
1973 if(n->d != CONTENT_MUD && n->d != CONTENT_GRASS)
1976 if(n->getContent() == c_dirt ||
1977 n->getContent() == c_dirt_with_grass)
1979 // Make it exactly mud
1980 n->setContent(c_dirt);
1983 Don't flow it if the stuff under it is not mud
1987 vmanip.m_area.add_y(em, i2, -1);
1988 // Cancel if out of area
1989 if(vmanip.m_area.contains(i2) == false)
1991 MapNode *n2 = &vmanip.m_data[i2];
1992 if(n2->getContent() != c_dirt &&
1993 n2->getContent() != c_dirt_with_grass)
1998 /*s16 recurse_count = 0;
2002 v3s16(0,0,1), // back
2003 v3s16(1,0,0), // right
2004 v3s16(0,0,-1), // front
2005 v3s16(-1,0,0), // left
2008 // Theck that upper is air or doesn't exist.
2009 // Cancel dropping if upper keeps it in place
2011 vmanip.m_area.add_y(em, i3, 1);
2012 if(vmanip.m_area.contains(i3) == true
2013 && ndef->get(vmanip.m_data[i3]).walkable)
2020 for(u32 di=0; di<4; di++)
2022 v3s16 dirp = dirs4[di];
2025 vmanip.m_area.add_p(em, i2, dirp);
2026 // Fail if out of area
2027 if(vmanip.m_area.contains(i2) == false)
2029 // Check that side is air
2030 MapNode *n2 = &vmanip.m_data[i2];
2031 if(ndef->get(*n2).walkable)
2033 // Check that under side is air
2034 vmanip.m_area.add_y(em, i2, -1);
2035 if(vmanip.m_area.contains(i2) == false)
2037 n2 = &vmanip.m_data[i2];
2038 if(ndef->get(*n2).walkable)
2040 /*// Check that under that is air (need a drop of 2)
2041 vmanip.m_area.add_y(em, i2, -1);
2042 if(vmanip.m_area.contains(i2) == false)
2044 n2 = &vmanip.m_data[i2];
2045 if(content_walkable(n2->d))
2047 // Loop further down until not air
2048 bool dropped_to_unknown = false;
2050 vmanip.m_area.add_y(em, i2, -1);
2051 n2 = &vmanip.m_data[i2];
2052 // if out of known area
2053 if(vmanip.m_area.contains(i2) == false
2054 || n2->getContent() == CONTENT_IGNORE){
2055 dropped_to_unknown = true;
2058 }while(ndef->get(*n2).walkable == false);
2059 // Loop one up so that we're in air
2060 vmanip.m_area.add_y(em, i2, 1);
2061 n2 = &vmanip.m_data[i2];
2063 bool old_is_water = (n->getContent() == c_water_source);
2064 // Move mud to new place
2065 if(!dropped_to_unknown) {
2067 // Set old place to be air (or water)
2069 *n = MapNode(c_water_source);
2071 *n = MapNode(CONTENT_AIR);
2087 /***********************
2089 ************************/
2092 Add top and bottom side of water to transforming_liquid queue
2095 for(s16 x=full_node_min.X; x<=full_node_max.X; x++)
2096 for(s16 z=full_node_min.Z; z<=full_node_max.Z; z++)
2101 bool water_found = false;
2102 // Use fast index incrementing
2103 v3s16 em = vmanip.m_area.getExtent();
2104 u32 i = vmanip.m_area.index(v3s16(p2d.X, full_node_max.Y, p2d.Y));
2105 for(s16 y=full_node_max.Y; y>=full_node_min.Y; y--)
2107 if(y == full_node_max.Y){
2109 (vmanip.m_data[i].getContent() == c_water_source ||
2110 vmanip.m_data[i].getContent() == c_lava_source);
2112 else if(water_found == false)
2114 if(vmanip.m_data[i].getContent() == c_water_source ||
2115 vmanip.m_data[i].getContent() == c_lava_source)
2117 v3s16 p = v3s16(p2d.X, y, p2d.Y);
2118 data->transforming_liquid.push_back(p);
2124 // This can be done because water_found can only
2125 // turn to true and end up here after going through
2127 if(vmanip.m_data[i+1].getContent() != c_water_source ||
2128 vmanip.m_data[i+1].getContent() != c_lava_source)
2130 v3s16 p = v3s16(p2d.X, y+1, p2d.Y);
2131 data->transforming_liquid.push_back(p);
2132 water_found = false;
2136 vmanip.m_area.add_y(em, i, -1);
2145 for(s16 x=full_node_min.X; x<=full_node_max.X; x++)
2146 for(s16 z=full_node_min.Z; z<=full_node_max.Z; z++)
2148 // Node position in 2d
2149 v2s16 p2d = v2s16(x,z);
2152 Find the lowest surface to which enough light ends up
2155 Basically just wait until not air and not leaves.
2159 v3s16 em = vmanip.m_area.getExtent();
2160 u32 i = vmanip.m_area.index(v3s16(p2d.X, node_max.Y, p2d.Y));
2162 // Go to ground level
2163 for(y=node_max.Y; y>=full_node_min.Y; y--)
2165 MapNode &n = vmanip.m_data[i];
2166 if(ndef->get(n).param_type != CPT_LIGHT
2167 || ndef->get(n).liquid_type != LIQUID_NONE)
2169 vmanip.m_area.add_y(em, i, -1);
2171 if(y >= full_node_min.Y)
2174 surface_y = full_node_min.Y;
2177 u32 i = vmanip.m_area.index(p2d.X, surface_y, p2d.Y);
2178 MapNode *n = &vmanip.m_data[i];
2179 if(n->getContent() == c_dirt){
2180 // Well yeah, this can't be overground...
2181 if(surface_y < WATER_LEVEL - 20)
2183 n->setContent(c_dirt_with_grass);
2190 assert(central_area_size.X == central_area_size.Z);
2192 // Divide area into parts
2194 s16 sidelen = central_area_size.X / div;
2195 double area = sidelen * sidelen;
2196 for(s16 x0=0; x0<div; x0++)
2197 for(s16 z0=0; z0<div; z0++)
2199 // Center position of part of division
2201 node_min.X + sidelen/2 + sidelen*x0,
2202 node_min.Z + sidelen/2 + sidelen*z0
2204 // Minimum edge of part of division
2206 node_min.X + sidelen*x0,
2207 node_min.Z + sidelen*z0
2209 // Maximum edge of part of division
2211 node_min.X + sidelen + sidelen*x0 - 1,
2212 node_min.Z + sidelen + sidelen*z0 - 1
2215 u32 tree_count = area * tree_amount_2d(data->seed, p2d_center);
2216 // Put trees in random places on part of division
2217 for(u32 i=0; i<tree_count; i++)
2219 s16 x = myrand_range(p2d_min.X, p2d_max.X);
2220 s16 z = myrand_range(p2d_min.Y, p2d_max.Y);
2221 s16 y = find_ground_level(vmanip, v2s16(x,z), ndef);
2222 // Don't make a tree under water level
2225 // Don't make a tree so high that it doesn't fit
2226 if(y > node_max.Y - 6)
2230 Trees grow only on mud and grass
2233 u32 i = vmanip.m_area.index(v3s16(p));
2234 MapNode *n = &vmanip.m_data[i];
2235 if(n->getContent() != c_dirt
2236 && n->getContent() != c_dirt_with_grass)
2241 make_tree(vmanip, p, false, ndef);
2248 Make base ground level
2251 for(s16 x=node_min.X; x<=node_max.X; x++)
2252 for(s16 z=node_min.Z; z<=node_max.Z; z++)
2257 // Use fast index incrementing
2258 v3s16 em = vmanip.m_area.getExtent();
2259 u32 i = vmanip.m_area.index(v3s16(p2d.X, node_min.Y, p2d.Y));
2260 for(s16 y=node_min.Y; y<=node_max.Y; y++)
2262 // Only modify places that have no content
2263 if(vmanip.m_data[i].getContent() == CONTENT_IGNORE)
2265 // First priority: make air and water.
2266 // This avoids caves inside water.
2267 if(all_is_ground_except_caves == false
2268 && val_is_ground(noisebuf_ground.get(x,y,z),
2269 v3s16(x,y,z), data->seed) == false)
2271 if(y <= WATER_LEVEL)
2272 vmanip.m_data[i] = n_water_source;
2274 vmanip.m_data[i] = n_air;
2276 else if(noisebuf_cave.get(x,y,z) > CAVE_NOISE_THRESHOLD)
2277 vmanip.m_data[i] = n_air;
2279 vmanip.m_data[i] = n_stone;
2282 vmanip->m_area.add_y(em, i, 1);
2288 Add mud and sand and others underground (in place of stone)
2291 for(s16 x=node_min.X; x<=node_max.X; x++)
2292 for(s16 z=node_min.Z; z<=node_max.Z; z++)
2297 // Use fast index incrementing
2298 v3s16 em = vmanip.m_area.getExtent();
2299 u32 i = vmanip.m_area.index(v3s16(p2d.X, node_max.Y, p2d.Y));
2300 for(s16 y=node_max.Y; y>=node_min.Y; y--)
2302 if(vmanip.m_data[i].getContent() == c_stone)
2304 if(noisebuf_ground_crumbleness.get(x,y,z) > 1.3)
2306 if(noisebuf_ground_wetness.get(x,y,z) > 0.0)
2307 vmanip.m_data[i] = n_dirt;
2309 vmanip.m_data[i] = n_sand;
2311 else if(noisebuf_ground_crumbleness.get(x,y,z) > 0.7)
2313 if(noisebuf_ground_wetness.get(x,y,z) < -0.6)
2314 vmanip.m_data[i] = n_gravel;
2316 else if(noisebuf_ground_crumbleness.get(x,y,z) <
2317 -3.0 + MYMIN(0.1 * sqrt((float)MYMAX(0, -y)), 1.5))
2319 vmanip.m_data[i] = n_lava_source;
2320 for(s16 x1=-1; x1<=1; x1++)
2321 for(s16 y1=-1; y1<=1; y1++)
2322 for(s16 z1=-1; z1<=1; z1++)
2323 data->transforming_liquid.push_back(
2324 v3s16(p2d.X+x1, y+y1, p2d.Y+z1));
2328 vmanip->m_area.add_y(em, i, -1);
2337 //if(node_min.Y < approx_groundlevel)
2338 //if(myrand() % 3 == 0)
2339 //if(myrand() % 3 == 0 && node_min.Y < approx_groundlevel)
2340 //if(myrand() % 100 == 0 && node_min.Y < approx_groundlevel)
2341 //float dungeon_rarity = g_settings.getFloat("dungeon_rarity");
2342 float dungeon_rarity = 0.02;
2343 if(((noise3d(blockpos.X,blockpos.Y,blockpos.Z,data->seed)+1.0)/2.0)
2345 && node_min.Y < approx_groundlevel)
2347 // Dungeon generator doesn't modify places which have this set
2348 vmanip->clearFlag(VMANIP_FLAG_DUNGEON_INSIDE
2349 | VMANIP_FLAG_DUNGEON_PRESERVE);
2351 // Set all air and water to be untouchable to make dungeons open
2352 // to caves and open air
2353 for(s16 x=full_node_min.X; x<=full_node_max.X; x++)
2354 for(s16 z=full_node_min.Z; z<=full_node_max.Z; z++)
2359 // Use fast index incrementing
2360 v3s16 em = vmanip.m_area.getExtent();
2361 u32 i = vmanip.m_area.index(v3s16(p2d.X, full_node_max.Y, p2d.Y));
2362 for(s16 y=full_node_max.Y; y>=full_node_min.Y; y--)
2364 if(vmanip.m_data[i].getContent() == CONTENT_AIR)
2365 vmanip.m_flags[i] |= VMANIP_FLAG_DUNGEON_PRESERVE;
2366 else if(vmanip.m_data[i].getContent() == c_water_source)
2367 vmanip.m_flags[i] |= VMANIP_FLAG_DUNGEON_PRESERVE;
2368 vmanip->m_area.add_y(em, i, -1);
2373 PseudoRandom random(blockseed+2);
2376 make_dungeon1(vmanip, random, ndef);
2378 // Convert some cobble to mossy cobble
2379 for(s16 x=full_node_min.X; x<=full_node_max.X; x++)
2380 for(s16 z=full_node_min.Z; z<=full_node_max.Z; z++)
2385 // Use fast index incrementing
2386 v3s16 em = vmanip.m_area.getExtent();
2387 u32 i = vmanip.m_area.index(v3s16(p2d.X, full_node_max.Y, p2d.Y));
2388 for(s16 y=full_node_max.Y; y>=full_node_min.Y; y--)
2390 // (noisebuf not used because it doesn't contain the
2392 double wetness = noise3d_param(
2393 get_ground_wetness_params(data->seed), x,y,z);
2394 double d = noise3d_perlin((float)x/2.5,
2395 (float)y/2.5,(float)z/2.5,
2397 if(vmanip.m_data[i].getContent() == c_cobble)
2401 vmanip.m_data[i].setContent(c_mossycobble);
2404 /*else if(vmanip.m_flags[i] & VMANIP_FLAG_DUNGEON_INSIDE)
2407 vmanip.m_data[i].setContent(c_dirt);
2409 vmanip->m_area.add_y(em, i, -1);
2419 PseudoRandom ncrandom(blockseed+9324342);
2420 if(ncrandom.range(0, 1000) == 0 && blockpos.Y <= -3)
2422 make_nc(vmanip, ncrandom, ndef);
2427 Add top and bottom side of water to transforming_liquid queue
2430 for(s16 x=node_min.X; x<=node_max.X; x++)
2431 for(s16 z=node_min.Z; z<=node_max.Z; z++)
2436 bool water_found = false;
2437 // Use fast index incrementing
2438 v3s16 em = vmanip.m_area.getExtent();
2439 u32 i = vmanip.m_area.index(v3s16(p2d.X, node_max.Y, p2d.Y));
2440 for(s16 y=node_max.Y; y>=node_min.Y; y--)
2442 if(water_found == false)
2444 if(vmanip.m_data[i].getContent() == c_water_source)
2446 v3s16 p = v3s16(p2d.X, y, p2d.Y);
2447 data->transforming_liquid.push_back(p);
2453 // This can be done because water_found can only
2454 // turn to true and end up here after going through
2456 if(vmanip.m_data[i+1].getContent() != c_water_source)
2458 v3s16 p = v3s16(p2d.X, y+1, p2d.Y);
2459 data->transforming_liquid.push_back(p);
2460 water_found = false;
2464 vmanip->m_area.add_y(em, i, -1);
2470 If close to ground level
2473 //if(abs(approx_ground_depth) < 30)
2474 if(minimum_ground_depth < 5 && maximum_ground_depth > -5)
2480 for(s16 x=node_min.X; x<=node_max.X; x++)
2481 for(s16 z=node_min.Z; z<=node_max.Z; z++)
2486 bool possibly_have_sand = get_have_beach(data->seed, p2d);
2487 bool have_sand = false;
2488 u32 current_depth = 0;
2489 bool air_detected = false;
2490 bool water_detected = false;
2491 bool have_clay = false;
2493 // Use fast index incrementing
2494 s16 start_y = node_max.Y+2;
2495 v3s16 em = vmanip.m_area.getExtent();
2496 u32 i = vmanip.m_area.index(v3s16(p2d.X, start_y, p2d.Y));
2497 for(s16 y=start_y; y>=node_min.Y-3; y--)
2499 if(vmanip.m_data[i].getContent() == c_water_source)
2500 water_detected = true;
2501 if(vmanip.m_data[i].getContent() == CONTENT_AIR)
2502 air_detected = true;
2504 if((vmanip.m_data[i].getContent() == c_stone
2505 || vmanip.m_data[i].getContent() == c_dirt_with_grass
2506 || vmanip.m_data[i].getContent() == c_dirt
2507 || vmanip.m_data[i].getContent() == c_sand
2508 || vmanip.m_data[i].getContent() == c_gravel
2509 ) && (air_detected || water_detected))
2511 if(current_depth == 0 && y <= WATER_LEVEL+2
2512 && possibly_have_sand)
2515 if(current_depth < 4)
2519 vmanip.m_data[i] = MapNode(c_sand);
2522 else if(current_depth==0 && !water_detected
2523 && y >= WATER_LEVEL && air_detected)
2524 vmanip.m_data[i] = MapNode(c_dirt_with_grass);
2527 vmanip.m_data[i] = MapNode(c_dirt);
2531 if(vmanip.m_data[i].getContent() == c_dirt
2532 || vmanip.m_data[i].getContent() == c_dirt_with_grass)
2533 vmanip.m_data[i] = MapNode(c_stone);
2538 if(current_depth >= 8)
2541 else if(current_depth != 0)
2544 vmanip->m_area.add_y(em, i, -1);
2550 Calculate some stuff
2553 float surface_humidity = surface_humidity_2d(data->seed, p2d_center);
2554 bool is_jungle = surface_humidity > 0.75;
2556 u32 tree_count = gen_area_nodes * tree_amount_2d(data->seed, p2d_center);
2563 PseudoRandom treerandom(blockseed);
2564 // Put trees in random places on part of division
2565 for(u32 i=0; i<tree_count; i++)
2567 s16 x = treerandom.range(node_min.X, node_max.X);
2568 s16 z = treerandom.range(node_min.Z, node_max.Z);
2569 //s16 y = find_ground_level(vmanip, v2s16(x,z));
2570 s16 y = find_ground_level_from_noise(data->seed, v2s16(x,z), 4);
2571 // Don't make a tree under water level
2574 // Make sure tree fits (only trees whose starting point is
2575 // at this block are added)
2576 if(y < node_min.Y || y > node_max.Y)
2579 Find exact ground level
2583 for(; p.Y >= y-6; p.Y--)
2585 u32 i = vmanip->m_area.index(p);
2586 MapNode *n = &vmanip->m_data[i];
2587 if(n->getContent() != CONTENT_AIR && n->getContent() != c_water_source && n->getContent() != CONTENT_IGNORE)
2593 // If not found, handle next one
2598 u32 i = vmanip->m_area.index(p);
2599 MapNode *n = &vmanip->m_data[i];
2601 if(n->getContent() != c_dirt && n->getContent() != c_dirt_with_grass && n->getContent() != c_sand)
2604 // Papyrus grows only on mud and in water
2605 if(n->getContent() == c_dirt && y <= WATER_LEVEL)
2608 make_papyrus(vmanip, p, ndef);
2610 // Trees grow only on mud and grass, on land
2611 else if((n->getContent() == c_dirt || n->getContent() == c_dirt_with_grass) && y > WATER_LEVEL + 2)
2614 //if(surface_humidity_2d(data->seed, v2s16(x, y)) < 0.5)
2615 if(is_jungle == false)
2618 if(myrand_range(0,4) != 0)
2619 is_apple_tree = false;
2621 is_apple_tree = noise2d_perlin(
2622 0.5+(float)p.X/100, 0.5+(float)p.Z/100,
2623 data->seed+342902, 3, 0.45) > 0.2;
2624 make_tree(vmanip, p, is_apple_tree, ndef);
2627 make_jungletree(vmanip, p, ndef);
2629 // Cactii grow only on sand, on land
2630 else if(n->getContent() == c_sand && y > WATER_LEVEL + 2)
2633 make_cactus(vmanip, p, ndef);
2643 PseudoRandom grassrandom(blockseed);
2644 for(u32 i=0; i<surface_humidity*5*tree_count; i++)
2646 s16 x = grassrandom.range(node_min.X, node_max.X);
2647 s16 z = grassrandom.range(node_min.Z, node_max.Z);
2648 s16 y = find_ground_level_from_noise(data->seed, v2s16(x,z), 4);
2651 if(y < node_min.Y || y > node_max.Y)
2654 Find exact ground level
2658 for(; p.Y >= y-6; p.Y--)
2660 u32 i = vmanip->m_area.index(p);
2661 MapNode *n = &vmanip->m_data[i];
2662 if(data->nodedef->get(*n).is_ground_content)
2668 // If not found, handle next one
2672 if(vmanip.m_area.contains(p) == false)
2674 if(vmanip.m_data[vmanip.m_area.index(p)].getContent() != CONTENT_AIR)
2677 if(vmanip.m_area.contains(p))
2678 vmanip.m_data[vmanip.m_area.index(p)] = c_dirt;
2680 if(vmanip.m_area.contains(p))
2681 vmanip.m_data[vmanip.m_area.index(p)] = c_junglegrass;
2687 Add some kind of random stones
2690 u32 random_stone_count = gen_area_nodes *
2691 randomstone_amount_2d(data->seed, p2d_center);
2692 // Put in random places on part of division
2693 for(u32 i=0; i<random_stone_count; i++)
2695 s16 x = myrand_range(node_min.X, node_max.X);
2696 s16 z = myrand_range(node_min.Z, node_max.Z);
2697 s16 y = find_ground_level_from_noise(data->seed, v2s16(x,z), 1);
2698 // Don't add under water level
2699 /*if(y < WATER_LEVEL)
2701 // Don't add if doesn't belong to this block
2702 if(y < node_min.Y || y > node_max.Y)
2707 u32 i = vmanip->m_area.index(v3s16(p));
2708 MapNode *n = &vmanip->m_data[i];
2709 if(n->getContent() != c_dirt && n->getContent() != c_dirt_with_grass)
2712 // Will be placed one higher
2715 make_randomstone(vmanip, p);
2724 u32 large_stone_count = gen_area_nodes *
2725 largestone_amount_2d(data->seed, p2d_center);
2726 //u32 large_stone_count = 1;
2727 // Put in random places on part of division
2728 for(u32 i=0; i<large_stone_count; i++)
2730 s16 x = myrand_range(node_min.X, node_max.X);
2731 s16 z = myrand_range(node_min.Z, node_max.Z);
2732 s16 y = find_ground_level_from_noise(data->seed, v2s16(x,z), 1);
2733 // Don't add under water level
2734 /*if(y < WATER_LEVEL)
2736 // Don't add if doesn't belong to this block
2737 if(y < node_min.Y || y > node_max.Y)
2742 u32 i = vmanip->m_area.index(v3s16(p));
2743 MapNode *n = &vmanip->m_data[i];
2744 if(n->getContent() != c_dirt && n->getContent() != c_dirt_with_grass)
2747 // Will be placed one lower
2750 make_largestone(vmanip, p);
2760 PseudoRandom mineralrandom(blockseed);
2765 for(s16 i=0; i<approx_ground_depth/4; i++)
2767 if(mineralrandom.next()%50 == 0)
2769 s16 x = mineralrandom.range(node_min.X+1, node_max.X-1);
2770 s16 y = mineralrandom.range(node_min.Y+1, node_max.Y-1);
2771 s16 z = mineralrandom.range(node_min.Z+1, node_max.Z-1);
2772 for(u16 i=0; i<27; i++)
2774 v3s16 p = v3s16(x,y,z) + g_27dirs[i];
2775 u32 vi = vmanip.m_area.index(p);
2776 if(vmanip.m_data[vi].getContent() == c_stone)
2777 if(mineralrandom.next()%8 == 0)
2778 vmanip.m_data[vi] = MapNode(c_mese);
2787 u16 a = mineralrandom.range(0,15);
2789 u16 amount = 20 * a/1000;
2790 for(s16 i=0; i<amount; i++)
2792 s16 x = mineralrandom.range(node_min.X+1, node_max.X-1);
2793 s16 y = mineralrandom.range(node_min.Y+1, node_max.Y-1);
2794 s16 z = mineralrandom.range(node_min.Z+1, node_max.Z-1);
2796 u8 base_content = c_stone;
2797 MapNode new_content(CONTENT_IGNORE);
2800 if(noisebuf_ground_crumbleness.get(x,y+5,z) < -0.1)
2802 new_content = MapNode(c_stone_with_coal);
2806 if(noisebuf_ground_wetness.get(x,y+5,z) > 0.0)
2807 new_content = MapNode(c_stone_with_iron);
2808 /*if(noisebuf_ground_wetness.get(x,y,z) > 0.0)
2809 vmanip.m_data[i] = MapNode(c_dirt);
2811 vmanip.m_data[i] = MapNode(c_sand);*/
2813 /*else if(noisebuf_ground_crumbleness.get(x,y,z) > 0.1)
2817 if(new_content.getContent() != CONTENT_IGNORE)
2819 for(u16 i=0; i<27; i++)
2821 v3s16 p = v3s16(x,y,z) + g_27dirs[i];
2822 u32 vi = vmanip.m_area.index(p);
2823 if(vmanip.m_data[vi].getContent() == base_content)
2825 if(mineralrandom.next()%sparseness == 0)
2826 vmanip.m_data[vi] = new_content;
2835 //for(s16 i=0; i < MYMAX(0, 50 - abs(node_min.Y+8 - (-30))); i++)
2836 //for(s16 i=0; i<50; i++)
2837 u16 coal_amount = 30;
2838 u16 coal_rareness = 60 / coal_amount;
2839 if(coal_rareness == 0)
2841 if(mineralrandom.next()%coal_rareness == 0)
2843 u16 a = mineralrandom.next() % 16;
2844 u16 amount = coal_amount * a*a*a / 1000;
2845 for(s16 i=0; i<amount; i++)
2847 s16 x = mineralrandom.range(node_min.X+1, node_max.X-1);
2848 s16 y = mineralrandom.range(node_min.Y+1, node_max.Y-1);
2849 s16 z = mineralrandom.range(node_min.Z+1, node_max.Z-1);
2850 for(u16 i=0; i<27; i++)
2852 v3s16 p = v3s16(x,y,z) + g_27dirs[i];
2853 u32 vi = vmanip.m_area.index(p);
2854 if(vmanip.m_data[vi].getContent() == c_stone)
2855 if(mineralrandom.next()%8 == 0)
2856 vmanip.m_data[vi] = MapNode(c_stone_with_coal);
2863 u16 iron_amount = 8;
2864 u16 iron_rareness = 60 / iron_amount;
2865 if(iron_rareness == 0)
2867 if(mineralrandom.next()%iron_rareness == 0)
2869 u16 a = mineralrandom.next() % 16;
2870 u16 amount = iron_amount * a*a*a / 1000;
2871 for(s16 i=0; i<amount; i++)
2873 s16 x = mineralrandom.range(node_min.X+1, node_max.X-1);
2874 s16 y = mineralrandom.range(node_min.Y+1, node_max.Y-1);
2875 s16 z = mineralrandom.range(node_min.Z+1, node_max.Z-1);
2876 for(u16 i=0; i<27; i++)
2878 v3s16 p = v3s16(x,y,z) + g_27dirs[i];
2879 u32 vi = vmanip.m_area.index(p);
2880 if(vmanip.m_data[vi].getContent() == c_stone)
2881 if(mineralrandom.next()%8 == 0)
2882 vmanip.m_data[vi] = MapNode(c_stone_with_iron);
2893 ScopeProfiler sp(g_profiler, "EmergeThread: mapgen lighting update",
2895 //VoxelArea a(node_min, node_max);
2896 VoxelArea a(node_min-v3s16(1,0,1)*MAP_BLOCKSIZE,
2897 node_max+v3s16(1,0,1)*MAP_BLOCKSIZE);
2898 /*VoxelArea a(node_min-v3s16(1,0,1)*MAP_BLOCKSIZE/2,
2899 node_max+v3s16(1,0,1)*MAP_BLOCKSIZE/2);*/
2900 enum LightBank banks[2] = {LIGHTBANK_DAY, LIGHTBANK_NIGHT};
2901 for(int i=0; i<2; i++)
2903 enum LightBank bank = banks[i];
2905 core::map<v3s16, bool> light_sources;
2906 core::map<v3s16, u8> unlight_from;
2908 voxalgo::clearLightAndCollectSources(vmanip, a, bank, ndef,
2909 light_sources, unlight_from);
2911 bool inexistent_top_provides_sunlight = !block_is_underground;
2912 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
2913 vmanip, a, inexistent_top_provides_sunlight,
2914 light_sources, ndef);
2915 // TODO: Do stuff according to bottom_sunlight_valid
2917 vmanip.unspreadLight(bank, unlight_from, light_sources, ndef);
2919 vmanip.spreadLight(bank, light_sources, ndef);
2924 BlockMakeData::BlockMakeData():
2931 BlockMakeData::~BlockMakeData()
2936 }; // namespace mapgen