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 General Public License as published by
7 the Free Software Foundation; either version 2 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 General Public License for more details.
15 You should have received a copy of the GNU 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.
22 #include "content_mapnode.h"
27 //#include "serverobject.h"
28 #include "content_sao.h"
29 #include "mapnode_contentfeatures.h"
35 Some helper functions for the map generator
39 static s16 find_ground_level(VoxelManipulator &vmanip, v2s16 p2d)
41 v3s16 em = vmanip.m_area.getExtent();
42 s16 y_nodes_max = vmanip.m_area.MaxEdge.Y;
43 s16 y_nodes_min = vmanip.m_area.MinEdge.Y;
44 u32 i = vmanip.m_area.index(v3s16(p2d.X, y_nodes_max, p2d.Y));
46 for(y=y_nodes_max; y>=y_nodes_min; y--)
48 MapNode &n = vmanip.m_data[i];
49 if(content_walkable(n.d))
52 vmanip.m_area.add_y(em, i, -1);
60 static s16 find_ground_level_clever(VoxelManipulator &vmanip, v2s16 p2d)
62 v3s16 em = vmanip.m_area.getExtent();
63 s16 y_nodes_max = vmanip.m_area.MaxEdge.Y;
64 s16 y_nodes_min = vmanip.m_area.MinEdge.Y;
65 u32 i = vmanip.m_area.index(v3s16(p2d.X, y_nodes_max, p2d.Y));
67 for(y=y_nodes_max; y>=y_nodes_min; y--)
69 MapNode &n = vmanip.m_data[i];
70 if(content_walkable(n.d)
71 && n.getContent() != CONTENT_TREE
72 && n.getContent() != CONTENT_LEAVES)
75 vmanip.m_area.add_y(em, i, -1);
84 void make_tree(ManualMapVoxelManipulator &vmanip, v3s16 p0, bool is_apple_tree)
86 MapNode treenode(CONTENT_TREE);
87 MapNode leavesnode(CONTENT_LEAVES);
88 MapNode applenode(CONTENT_APPLE);
90 s16 trunk_h = myrand_range(4, 5);
92 for(s16 ii=0; ii<trunk_h; ii++)
94 if(vmanip.m_area.contains(p1))
95 vmanip.m_data[vmanip.m_area.index(p1)] = treenode;
99 // p1 is now the last piece of the trunk
102 VoxelArea leaves_a(v3s16(-2,-1,-2), v3s16(2,2,2));
103 //SharedPtr<u8> leaves_d(new u8[leaves_a.getVolume()]);
104 Buffer<u8> leaves_d(leaves_a.getVolume());
105 for(s32 i=0; i<leaves_a.getVolume(); i++)
108 // Force leaves at near the end of the trunk
111 for(s16 z=-d; z<=d; z++)
112 for(s16 y=-d; y<=d; y++)
113 for(s16 x=-d; x<=d; x++)
115 leaves_d[leaves_a.index(v3s16(x,y,z))] = 1;
119 // Add leaves randomly
120 for(u32 iii=0; iii<7; iii++)
125 myrand_range(leaves_a.MinEdge.X, leaves_a.MaxEdge.X-d),
126 myrand_range(leaves_a.MinEdge.Y, leaves_a.MaxEdge.Y-d),
127 myrand_range(leaves_a.MinEdge.Z, leaves_a.MaxEdge.Z-d)
130 for(s16 z=0; z<=d; z++)
131 for(s16 y=0; y<=d; y++)
132 for(s16 x=0; x<=d; x++)
134 leaves_d[leaves_a.index(p+v3s16(x,y,z))] = 1;
138 // Blit leaves to vmanip
139 for(s16 z=leaves_a.MinEdge.Z; z<=leaves_a.MaxEdge.Z; z++)
140 for(s16 y=leaves_a.MinEdge.Y; y<=leaves_a.MaxEdge.Y; y++)
141 for(s16 x=leaves_a.MinEdge.X; x<=leaves_a.MaxEdge.X; x++)
145 if(vmanip.m_area.contains(p) == false)
147 u32 vi = vmanip.m_area.index(p);
148 if(vmanip.m_data[vi].getContent() != CONTENT_AIR
149 && vmanip.m_data[vi].getContent() != CONTENT_IGNORE)
151 u32 i = leaves_a.index(x,y,z);
152 if(leaves_d[i] == 1) {
153 bool is_apple = myrand_range(0,99) < 10;
154 if(is_apple_tree && is_apple) {
155 vmanip.m_data[vi] = applenode;
157 vmanip.m_data[vi] = leavesnode;
163 static void make_jungletree(VoxelManipulator &vmanip, v3s16 p0)
165 MapNode treenode(CONTENT_JUNGLETREE);
166 MapNode leavesnode(CONTENT_LEAVES);
168 for(s16 x=-1; x<=1; x++)
169 for(s16 z=-1; z<=1; z++)
171 if(myrand_range(0, 2) == 0)
173 v3s16 p1 = p0 + v3s16(x,0,z);
174 v3s16 p2 = p0 + v3s16(x,-1,z);
175 if(vmanip.m_area.contains(p2)
176 && vmanip.m_data[vmanip.m_area.index(p2)] == CONTENT_AIR)
177 vmanip.m_data[vmanip.m_area.index(p2)] = treenode;
178 else if(vmanip.m_area.contains(p1))
179 vmanip.m_data[vmanip.m_area.index(p1)] = treenode;
182 s16 trunk_h = myrand_range(8, 12);
184 for(s16 ii=0; ii<trunk_h; ii++)
186 if(vmanip.m_area.contains(p1))
187 vmanip.m_data[vmanip.m_area.index(p1)] = treenode;
191 // p1 is now the last piece of the trunk
194 VoxelArea leaves_a(v3s16(-3,-2,-3), v3s16(3,2,3));
195 //SharedPtr<u8> leaves_d(new u8[leaves_a.getVolume()]);
196 Buffer<u8> leaves_d(leaves_a.getVolume());
197 for(s32 i=0; i<leaves_a.getVolume(); i++)
200 // Force leaves at near the end of the trunk
203 for(s16 z=-d; z<=d; z++)
204 for(s16 y=-d; y<=d; y++)
205 for(s16 x=-d; x<=d; x++)
207 leaves_d[leaves_a.index(v3s16(x,y,z))] = 1;
211 // Add leaves randomly
212 for(u32 iii=0; iii<30; iii++)
217 myrand_range(leaves_a.MinEdge.X, leaves_a.MaxEdge.X-d),
218 myrand_range(leaves_a.MinEdge.Y, leaves_a.MaxEdge.Y-d),
219 myrand_range(leaves_a.MinEdge.Z, leaves_a.MaxEdge.Z-d)
222 for(s16 z=0; z<=d; z++)
223 for(s16 y=0; y<=d; y++)
224 for(s16 x=0; x<=d; x++)
226 leaves_d[leaves_a.index(p+v3s16(x,y,z))] = 1;
230 // Blit leaves to vmanip
231 for(s16 z=leaves_a.MinEdge.Z; z<=leaves_a.MaxEdge.Z; z++)
232 for(s16 y=leaves_a.MinEdge.Y; y<=leaves_a.MaxEdge.Y; y++)
233 for(s16 x=leaves_a.MinEdge.X; x<=leaves_a.MaxEdge.X; x++)
237 if(vmanip.m_area.contains(p) == false)
239 u32 vi = vmanip.m_area.index(p);
240 if(vmanip.m_data[vi].getContent() != CONTENT_AIR
241 && vmanip.m_data[vi].getContent() != CONTENT_IGNORE)
243 u32 i = leaves_a.index(x,y,z);
245 vmanip.m_data[vi] = leavesnode;
249 void make_papyrus(VoxelManipulator &vmanip, v3s16 p0)
251 MapNode papyrusnode(CONTENT_PAPYRUS);
253 s16 trunk_h = myrand_range(2, 3);
255 for(s16 ii=0; ii<trunk_h; ii++)
257 if(vmanip.m_area.contains(p1))
258 vmanip.m_data[vmanip.m_area.index(p1)] = papyrusnode;
263 void make_cactus(VoxelManipulator &vmanip, v3s16 p0)
265 MapNode cactusnode(CONTENT_CACTUS);
269 for(s16 ii=0; ii<trunk_h; ii++)
271 if(vmanip.m_area.contains(p1))
272 vmanip.m_data[vmanip.m_area.index(p1)] = cactusnode;
278 static void make_randomstone(VoxelManipulator &vmanip, v3s16 p0)
280 MapNode stonenode(CONTENT_STONE);
282 s16 size = myrand_range(3, 6);
284 VoxelArea stone_a(v3s16(-2,0,-2), v3s16(2,size,2));
285 Buffer<u8> stone_d(stone_a.getVolume());
286 for(s32 i=0; i<stone_a.getVolume(); i++)
289 // Force stone at bottom to make it usually touch the ground
291 for(s16 z=0; z<=0; z++)
292 for(s16 y=0; y<=0; y++)
293 for(s16 x=0; x<=0; x++)
295 stone_d[stone_a.index(v3s16(x,y,z))] = 1;
299 // Generate from perlin noise
300 for(s16 z=stone_a.MinEdge.Z; z<=stone_a.MaxEdge.Z; z++)
301 for(s16 y=stone_a.MinEdge.Y; y<=stone_a.MaxEdge.Y; y++)
302 for(s16 x=stone_a.MinEdge.X; x<=stone_a.MaxEdge.X; x++)
304 double d = noise3d_perlin((float)x/3.,(float)z/3.,(float)y/3.,
305 p0.Z*4243+p0.Y*34+p0.X, 2, 0.5);
306 if(z == stone_a.MinEdge.Z || z == stone_a.MaxEdge.Z)
308 if(/*y == stone_a.MinEdge.Y ||*/ y == stone_a.MaxEdge.Y)
310 if(x == stone_a.MinEdge.X || x == stone_a.MaxEdge.X)
314 u32 vi = stone_a.index(v3s16(x,y,z));
319 /*// Add stone randomly
320 for(u32 iii=0; iii<7; iii++)
325 myrand_range(stone_a.MinEdge.X, stone_a.MaxEdge.X-d),
326 myrand_range(stone_a.MinEdge.Y, stone_a.MaxEdge.Y-d),
327 myrand_range(stone_a.MinEdge.Z, stone_a.MaxEdge.Z-d)
330 for(s16 z=0; z<=d; z++)
331 for(s16 y=0; y<=d; y++)
332 for(s16 x=0; x<=d; x++)
334 stone_d[stone_a.index(p+v3s16(x,y,z))] = 1;
338 // Blit stone to vmanip
339 for(s16 z=stone_a.MinEdge.Z; z<=stone_a.MaxEdge.Z; z++)
340 for(s16 y=stone_a.MinEdge.Y; y<=stone_a.MaxEdge.Y; y++)
341 for(s16 x=stone_a.MinEdge.X; x<=stone_a.MaxEdge.X; x++)
345 if(vmanip.m_area.contains(p) == false)
347 u32 vi = vmanip.m_area.index(p);
348 if(vmanip.m_data[vi].getContent() != CONTENT_AIR
349 && vmanip.m_data[vi].getContent() != CONTENT_IGNORE)
351 u32 i = stone_a.index(x,y,z);
353 vmanip.m_data[vi] = stonenode;
359 static void make_largestone(VoxelManipulator &vmanip, v3s16 p0)
361 MapNode stonenode(CONTENT_STONE);
363 s16 size = myrand_range(8, 16);
365 VoxelArea stone_a(v3s16(-size/2,0,-size/2), v3s16(size/2,size,size/2));
366 Buffer<u8> stone_d(stone_a.getVolume());
367 for(s32 i=0; i<stone_a.getVolume(); i++)
370 // Force stone at bottom to make it usually touch the ground
372 for(s16 z=0; z<=0; z++)
373 for(s16 y=0; y<=0; y++)
374 for(s16 x=0; x<=0; x++)
376 stone_d[stone_a.index(v3s16(x,y,z))] = 1;
380 // Generate from perlin noise
381 for(s16 z=stone_a.MinEdge.Z; z<=stone_a.MaxEdge.Z; z++)
382 for(s16 y=stone_a.MinEdge.Y; y<=stone_a.MaxEdge.Y; y++)
383 for(s16 x=stone_a.MinEdge.X; x<=stone_a.MaxEdge.X; x++)
386 d += noise3d_perlin((float)x/10.,(float)z/10.,(float)y/10.,
387 p0.Z*5123+p0.Y*2439+p0.X, 2, 0.5);
388 double mid_z = (stone_a.MaxEdge.Z+stone_a.MinEdge.Z)/2;
389 double mid_x = (stone_a.MaxEdge.X+stone_a.MinEdge.X)/2;
390 double mid_y = (stone_a.MaxEdge.Y+stone_a.MinEdge.Y)/2;
391 double dz = (double)z-mid_z;
392 double dx = (double)x-mid_x;
393 double dy = MYMAX(0, (double)y-mid_y);
394 double r = sqrt(dz*dz+dx*dx+dy*dy);
395 d /= (2*r/size)*2 + 0.01;
398 u32 vi = stone_a.index(v3s16(x,y,z));
403 /*// Add stone randomly
404 for(u32 iii=0; iii<7; iii++)
409 myrand_range(stone_a.MinEdge.X, stone_a.MaxEdge.X-d),
410 myrand_range(stone_a.MinEdge.Y, stone_a.MaxEdge.Y-d),
411 myrand_range(stone_a.MinEdge.Z, stone_a.MaxEdge.Z-d)
414 for(s16 z=0; z<=d; z++)
415 for(s16 y=0; y<=d; y++)
416 for(s16 x=0; x<=d; x++)
418 stone_d[stone_a.index(p+v3s16(x,y,z))] = 1;
422 // Blit stone to vmanip
423 for(s16 z=stone_a.MinEdge.Z; z<=stone_a.MaxEdge.Z; z++)
424 for(s16 y=stone_a.MinEdge.Y; y<=stone_a.MaxEdge.Y; y++)
425 for(s16 x=stone_a.MinEdge.X; x<=stone_a.MaxEdge.X; x++)
429 if(vmanip.m_area.contains(p) == false)
431 u32 vi = vmanip.m_area.index(p);
432 /*if(vmanip.m_data[vi].getContent() != CONTENT_AIR
433 && vmanip.m_data[vi].getContent() != CONTENT_IGNORE)
435 u32 i = stone_a.index(x,y,z);
437 vmanip.m_data[vi] = stonenode;
443 Dungeon making routines
446 #define VMANIP_FLAG_DUNGEON_INSIDE VOXELFLAG_CHECKED1
447 #define VMANIP_FLAG_DUNGEON_PRESERVE VOXELFLAG_CHECKED2
448 #define VMANIP_FLAG_DUNGEON_UNTOUCHABLE (\
449 VMANIP_FLAG_DUNGEON_INSIDE|VMANIP_FLAG_DUNGEON_PRESERVE)
451 static void make_room1(VoxelManipulator &vmanip, v3s16 roomsize, v3s16 roomplace)
454 for(s16 z=0; z<roomsize.Z; z++)
455 for(s16 y=0; y<roomsize.Y; y++)
458 v3s16 p = roomplace + v3s16(0,y,z);
459 if(vmanip.m_area.contains(p) == false)
461 u32 vi = vmanip.m_area.index(p);
462 if(vmanip.m_flags[vi] & VMANIP_FLAG_DUNGEON_UNTOUCHABLE)
464 vmanip.m_data[vi] = MapNode(CONTENT_COBBLE);
467 v3s16 p = roomplace + v3s16(roomsize.X-1,y,z);
468 if(vmanip.m_area.contains(p) == false)
470 u32 vi = vmanip.m_area.index(p);
471 if(vmanip.m_flags[vi] & VMANIP_FLAG_DUNGEON_UNTOUCHABLE)
473 vmanip.m_data[vi] = MapNode(CONTENT_COBBLE);
478 for(s16 x=0; x<roomsize.X; x++)
479 for(s16 y=0; y<roomsize.Y; y++)
482 v3s16 p = roomplace + v3s16(x,y,0);
483 if(vmanip.m_area.contains(p) == false)
485 u32 vi = vmanip.m_area.index(p);
486 if(vmanip.m_flags[vi] & VMANIP_FLAG_DUNGEON_UNTOUCHABLE)
488 vmanip.m_data[vi] = MapNode(CONTENT_COBBLE);
491 v3s16 p = roomplace + v3s16(x,y,roomsize.Z-1);
492 if(vmanip.m_area.contains(p) == false)
494 u32 vi = vmanip.m_area.index(p);
495 if(vmanip.m_flags[vi] & VMANIP_FLAG_DUNGEON_UNTOUCHABLE)
497 vmanip.m_data[vi] = MapNode(CONTENT_COBBLE);
501 // Make +-Y walls (floor and ceiling)
502 for(s16 z=0; z<roomsize.Z; z++)
503 for(s16 x=0; x<roomsize.X; x++)
506 v3s16 p = roomplace + v3s16(x,0,z);
507 if(vmanip.m_area.contains(p) == false)
509 u32 vi = vmanip.m_area.index(p);
510 if(vmanip.m_flags[vi] & VMANIP_FLAG_DUNGEON_UNTOUCHABLE)
512 vmanip.m_data[vi] = MapNode(CONTENT_COBBLE);
515 v3s16 p = roomplace + v3s16(x,roomsize.Y-1,z);
516 if(vmanip.m_area.contains(p) == false)
518 u32 vi = vmanip.m_area.index(p);
519 if(vmanip.m_flags[vi] & VMANIP_FLAG_DUNGEON_UNTOUCHABLE)
521 vmanip.m_data[vi] = MapNode(CONTENT_COBBLE);
526 for(s16 z=1; z<roomsize.Z-1; z++)
527 for(s16 y=1; y<roomsize.Y-1; y++)
528 for(s16 x=1; x<roomsize.X-1; x++)
530 v3s16 p = roomplace + v3s16(x,y,z);
531 if(vmanip.m_area.contains(p) == false)
533 u32 vi = vmanip.m_area.index(p);
534 vmanip.m_flags[vi] |= VMANIP_FLAG_DUNGEON_UNTOUCHABLE;
535 vmanip.m_data[vi] = MapNode(CONTENT_AIR);
539 static void make_fill(VoxelManipulator &vmanip, v3s16 place, v3s16 size,
540 u8 avoid_flags, MapNode n, u8 or_flags)
542 for(s16 z=0; z<size.Z; z++)
543 for(s16 y=0; y<size.Y; y++)
544 for(s16 x=0; x<size.X; x++)
546 v3s16 p = place + v3s16(x,y,z);
547 if(vmanip.m_area.contains(p) == false)
549 u32 vi = vmanip.m_area.index(p);
550 if(vmanip.m_flags[vi] & avoid_flags)
552 vmanip.m_flags[vi] |= or_flags;
553 vmanip.m_data[vi] = n;
557 static void make_hole1(VoxelManipulator &vmanip, v3s16 place)
559 make_fill(vmanip, place, v3s16(1,2,1), 0, MapNode(CONTENT_AIR),
560 VMANIP_FLAG_DUNGEON_INSIDE);
563 static void make_door1(VoxelManipulator &vmanip, v3s16 doorplace, v3s16 doordir)
565 make_hole1(vmanip, doorplace);
566 // Place torch (for testing)
567 //vmanip.m_data[vmanip.m_area.index(doorplace)] = MapNode(CONTENT_TORCH);
570 static v3s16 rand_ortho_dir(PseudoRandom &random)
572 if(random.next()%2==0)
573 return random.next()%2 ? v3s16(-1,0,0) : v3s16(1,0,0);
575 return random.next()%2 ? v3s16(0,0,-1) : v3s16(0,0,1);
578 static v3s16 turn_xz(v3s16 olddir, int t)
598 static v3s16 random_turn(PseudoRandom &random, v3s16 olddir)
600 int turn = random.range(0,2);
609 dir = turn_xz(olddir, 0);
612 dir = turn_xz(olddir, 1);
616 static void make_corridor(VoxelManipulator &vmanip, v3s16 doorplace,
617 v3s16 doordir, v3s16 &result_place, v3s16 &result_dir,
618 PseudoRandom &random)
620 make_hole1(vmanip, doorplace);
621 v3s16 p0 = doorplace;
625 length = random.range(1,13);
627 length = random.range(1,6);
628 length = random.range(1,13);
629 u32 partlength = random.range(1,13);
632 if(random.next()%2 == 0 && partlength >= 3)
633 make_stairs = random.next()%2 ? 1 : -1;
634 for(u32 i=0; i<length; i++)
640 /*// If already empty
641 if(vmanip.getNodeNoExNoEmerge(p).getContent()
643 && vmanip.getNodeNoExNoEmerge(p+v3s16(0,1,0)).getContent()
648 if(vmanip.m_area.contains(p) == true
649 && vmanip.m_area.contains(p+v3s16(0,1,0)) == true)
653 make_fill(vmanip, p+v3s16(-1,-1,-1), v3s16(3,5,3),
654 VMANIP_FLAG_DUNGEON_UNTOUCHABLE, MapNode(CONTENT_COBBLE), 0);
655 make_fill(vmanip, p, v3s16(1,2,1), 0, MapNode(CONTENT_AIR),
656 VMANIP_FLAG_DUNGEON_INSIDE);
657 make_fill(vmanip, p-dir, v3s16(1,2,1), 0, MapNode(CONTENT_AIR),
658 VMANIP_FLAG_DUNGEON_INSIDE);
662 make_fill(vmanip, p+v3s16(-1,-1,-1), v3s16(3,4,3),
663 VMANIP_FLAG_DUNGEON_UNTOUCHABLE, MapNode(CONTENT_COBBLE), 0);
664 make_hole1(vmanip, p);
665 /*make_fill(vmanip, p, v3s16(1,2,1), 0, MapNode(CONTENT_AIR),
666 VMANIP_FLAG_DUNGEON_INSIDE);*/
673 // Can't go here, turn away
674 dir = turn_xz(dir, random.range(0,1));
675 make_stairs = -make_stairs;
677 partlength = random.range(1,length);
682 if(partcount >= partlength)
686 dir = random_turn(random, dir);
688 partlength = random.range(1,length);
691 if(random.next()%2 == 0 && partlength >= 3)
692 make_stairs = random.next()%2 ? 1 : -1;
703 RoomWalker(VoxelManipulator &vmanip_, v3s16 pos, PseudoRandom &random):
713 m_dir = rand_ortho_dir(m_random);
716 void setPos(v3s16 pos)
721 void setDir(v3s16 dir)
726 bool findPlaceForDoor(v3s16 &result_place, v3s16 &result_dir)
728 for(u32 i=0; i<100; i++)
730 v3s16 p = m_pos + m_dir;
731 v3s16 p1 = p + v3s16(0,1,0);
732 if(vmanip.m_area.contains(p) == false
733 || vmanip.m_area.contains(p1) == false
739 if(vmanip.getNodeNoExNoEmerge(p).getContent()
741 && vmanip.getNodeNoExNoEmerge(p1).getContent()
744 // Found wall, this is a good place!
747 // Randomize next direction
752 Determine where to move next
754 // Jump one up if the actual space is there
755 if(vmanip.getNodeNoExNoEmerge(p+v3s16(0,0,0)).getContent()
757 && vmanip.getNodeNoExNoEmerge(p+v3s16(0,1,0)).getContent()
759 && vmanip.getNodeNoExNoEmerge(p+v3s16(0,2,0)).getContent()
762 // Jump one down if the actual space is there
763 if(vmanip.getNodeNoExNoEmerge(p+v3s16(0,1,0)).getContent()
765 && vmanip.getNodeNoExNoEmerge(p+v3s16(0,0,0)).getContent()
767 && vmanip.getNodeNoExNoEmerge(p+v3s16(0,-1,0)).getContent()
770 // Check if walking is now possible
771 if(vmanip.getNodeNoExNoEmerge(p).getContent()
773 || vmanip.getNodeNoExNoEmerge(p+v3s16(0,1,0)).getContent()
776 // Cannot continue walking here
786 bool findPlaceForRoomDoor(v3s16 roomsize, v3s16 &result_doorplace,
787 v3s16 &result_doordir, v3s16 &result_roomplace)
789 for(s16 trycount=0; trycount<30; trycount++)
793 bool r = findPlaceForDoor(doorplace, doordir);
797 // X east, Z north, Y up
799 if(doordir == v3s16(1,0,0)) // X+
800 roomplace = doorplace +
801 v3s16(0,-1,m_random.range(-roomsize.Z+2,-2));
802 if(doordir == v3s16(-1,0,0)) // X-
803 roomplace = doorplace +
804 v3s16(-roomsize.X+1,-1,m_random.range(-roomsize.Z+2,-2));
805 if(doordir == v3s16(0,0,1)) // Z+
806 roomplace = doorplace +
807 v3s16(m_random.range(-roomsize.X+2,-2),-1,0);
808 if(doordir == v3s16(0,0,-1)) // Z-
809 roomplace = doorplace +
810 v3s16(m_random.range(-roomsize.X+2,-2),-1,-roomsize.Z+1);
813 if(doordir == v3s16(1,0,0)) // X+
814 roomplace = doorplace + v3s16(0,-1,-roomsize.Z/2);
815 if(doordir == v3s16(-1,0,0)) // X-
816 roomplace = doorplace + v3s16(-roomsize.X+1,-1,-roomsize.Z/2);
817 if(doordir == v3s16(0,0,1)) // Z+
818 roomplace = doorplace + v3s16(-roomsize.X/2,-1,0);
819 if(doordir == v3s16(0,0,-1)) // Z-
820 roomplace = doorplace + v3s16(-roomsize.X/2,-1,-roomsize.Z+1);
825 for(s16 z=1; z<roomsize.Z-1; z++)
826 for(s16 y=1; y<roomsize.Y-1; y++)
827 for(s16 x=1; x<roomsize.X-1; x++)
829 v3s16 p = roomplace + v3s16(x,y,z);
830 if(vmanip.m_area.contains(p) == false)
835 if(vmanip.m_flags[vmanip.m_area.index(p)]
836 & VMANIP_FLAG_DUNGEON_INSIDE)
847 result_doorplace = doorplace;
848 result_doordir = doordir;
849 result_roomplace = roomplace;
856 VoxelManipulator &vmanip;
859 PseudoRandom &m_random;
862 static void make_dungeon1(VoxelManipulator &vmanip, PseudoRandom &random)
864 v3s16 areasize = vmanip.m_area.getExtent();
869 Find place for first room
872 for(u32 i=0; i<100; i++)
874 roomsize = v3s16(random.range(4,8),random.range(4,6),random.range(4,8));
875 roomplace = vmanip.m_area.MinEdge + v3s16(
876 random.range(0,areasize.X-roomsize.X-1),
877 random.range(0,areasize.Y-roomsize.Y-1),
878 random.range(0,areasize.Z-roomsize.Z-1));
880 Check that we're not putting the room to an unknown place,
881 otherwise it might end up floating in the air
884 for(s16 z=1; z<roomsize.Z-1; z++)
885 for(s16 y=1; y<roomsize.Y-1; y++)
886 for(s16 x=1; x<roomsize.X-1; x++)
888 v3s16 p = roomplace + v3s16(x,y,z);
889 u32 vi = vmanip.m_area.index(p);
890 if(vmanip.m_flags[vi] & VMANIP_FLAG_DUNGEON_INSIDE)
895 if(vmanip.m_data[vi].getContent() == CONTENT_IGNORE)
909 Stores the center position of the last room made, so that
910 a new corridor can be started from the last room instead of
911 the new room, if chosen so.
913 v3s16 last_room_center = roomplace+v3s16(roomsize.X/2,1,roomsize.Z/2);
915 u32 room_count = random.range(2,7);
916 for(u32 i=0; i<room_count; i++)
918 // Make a room to the determined place
919 make_room1(vmanip, roomsize, roomplace);
921 v3s16 room_center = roomplace + v3s16(roomsize.X/2,1,roomsize.Z/2);
923 // Place torch at room center (for testing)
924 //vmanip.m_data[vmanip.m_area.index(room_center)] = MapNode(CONTENT_TORCH);
927 if(i == room_count-1)
930 // Determine walker start position
932 bool start_in_last_room = (random.range(0,2)!=0);
933 //bool start_in_last_room = true;
935 v3s16 walker_start_place;
937 if(start_in_last_room)
939 walker_start_place = last_room_center;
943 walker_start_place = room_center;
944 // Store center of current room as the last one
945 last_room_center = room_center;
948 // Create walker and find a place for a door
949 RoomWalker walker(vmanip, walker_start_place, random);
952 bool r = walker.findPlaceForDoor(doorplace, doordir);
956 if(random.range(0,1)==0)
958 make_door1(vmanip, doorplace, doordir);
960 // Don't actually make a door
961 doorplace -= doordir;
963 // Make a random corridor starting from the door
965 v3s16 corridor_end_dir;
966 make_corridor(vmanip, doorplace, doordir, corridor_end,
967 corridor_end_dir, random);
969 // Find a place for a random sized room
970 roomsize = v3s16(random.range(4,8),random.range(4,6),random.range(4,8));
971 walker.setPos(corridor_end);
972 walker.setDir(corridor_end_dir);
973 r = walker.findPlaceForRoomDoor(roomsize, doorplace, doordir, roomplace);
977 if(random.range(0,1)==0)
979 make_door1(vmanip, doorplace, doordir);
981 // Don't actually make a door
982 roomplace -= doordir;
987 static void make_nc(VoxelManipulator &vmanip, PseudoRandom &random)
991 s32 r = random.range(0, 3);
993 dir = v3s16( 1, 0, 0);
997 dir = v3s16(-1, 0, 0);
1001 dir = v3s16( 0, 0, 1);
1005 dir = v3s16( 0, 0,-1);
1008 v3s16 p = vmanip.m_area.MinEdge + v3s16(
1009 16+random.range(0,15),
1010 16+random.range(0,15),
1011 16+random.range(0,15));
1012 vmanip.m_data[vmanip.m_area.index(p)] = MapNode(CONTENT_NC, facedir_i);
1013 u32 length = random.range(3,15);
1014 for(u32 j=0; j<length; j++)
1017 vmanip.m_data[vmanip.m_area.index(p)] = MapNode(CONTENT_NC_RB);
1022 Noise functions. Make sure seed is mangled differently in each one.
1026 Scaling the output of the noise function affects the overdrive of the
1027 contour function, which affects the shape of the output considerably.
1029 #define CAVE_NOISE_SCALE 12.0
1030 //#define CAVE_NOISE_SCALE 10.0
1031 //#define CAVE_NOISE_SCALE 7.5
1032 //#define CAVE_NOISE_SCALE 5.0
1033 //#define CAVE_NOISE_SCALE 1.0
1035 //#define CAVE_NOISE_THRESHOLD (2.5/CAVE_NOISE_SCALE)
1036 #define CAVE_NOISE_THRESHOLD (1.5/CAVE_NOISE_SCALE)
1038 NoiseParams get_cave_noise1_params(u64 seed)
1040 /*return NoiseParams(NOISE_PERLIN_CONTOUR, seed+52534, 5, 0.7,
1041 200, CAVE_NOISE_SCALE);*/
1042 /*return NoiseParams(NOISE_PERLIN_CONTOUR, seed+52534, 4, 0.7,
1043 100, CAVE_NOISE_SCALE);*/
1044 /*return NoiseParams(NOISE_PERLIN_CONTOUR, seed+52534, 5, 0.6,
1045 100, CAVE_NOISE_SCALE);*/
1046 /*return NoiseParams(NOISE_PERLIN_CONTOUR, seed+52534, 5, 0.3,
1047 100, CAVE_NOISE_SCALE);*/
1048 return NoiseParams(NOISE_PERLIN_CONTOUR, seed+52534, 4, 0.5,
1049 50, CAVE_NOISE_SCALE);
1050 //return NoiseParams(NOISE_CONSTANT_ONE);
1053 NoiseParams get_cave_noise2_params(u64 seed)
1055 /*return NoiseParams(NOISE_PERLIN_CONTOUR_FLIP_YZ, seed+10325, 5, 0.7,
1056 200, CAVE_NOISE_SCALE);*/
1057 /*return NoiseParams(NOISE_PERLIN_CONTOUR_FLIP_YZ, seed+10325, 4, 0.7,
1058 100, CAVE_NOISE_SCALE);*/
1059 /*return NoiseParams(NOISE_PERLIN_CONTOUR_FLIP_YZ, seed+10325, 5, 0.3,
1060 100, CAVE_NOISE_SCALE);*/
1061 return NoiseParams(NOISE_PERLIN_CONTOUR_FLIP_YZ, seed+10325, 4, 0.5,
1062 50, CAVE_NOISE_SCALE);
1063 //return NoiseParams(NOISE_CONSTANT_ONE);
1066 NoiseParams get_ground_noise1_params(u64 seed)
1068 return NoiseParams(NOISE_PERLIN, seed+983240, 4,
1072 NoiseParams get_ground_crumbleness_params(u64 seed)
1074 return NoiseParams(NOISE_PERLIN, seed+34413, 3,
1078 NoiseParams get_ground_wetness_params(u64 seed)
1080 return NoiseParams(NOISE_PERLIN, seed+32474, 4,
1084 bool is_cave(u64 seed, v3s16 p)
1086 double d1 = noise3d_param(get_cave_noise1_params(seed), p.X,p.Y,p.Z);
1087 double d2 = noise3d_param(get_cave_noise2_params(seed), p.X,p.Y,p.Z);
1088 return d1*d2 > CAVE_NOISE_THRESHOLD;
1092 Ground density noise shall be interpreted by using this.
1094 TODO: No perlin noises here, they should be outsourced
1096 NOTE: The speed of these actually isn't terrible
1098 bool val_is_ground(double ground_noise1_val, v3s16 p, u64 seed)
1100 //return ((double)p.Y < ground_noise1_val);
1102 double f = 0.55 + noise2d_perlin(
1103 0.5+(float)p.X/250, 0.5+(float)p.Z/250,
1104 seed+920381, 3, 0.45);
1109 double h = WATER_LEVEL + 10 * noise2d_perlin(
1110 0.5+(float)p.X/250, 0.5+(float)p.Z/250,
1111 seed+84174, 4, 0.5);
1114 return ((double)p.Y - h < ground_noise1_val * f);
1118 Queries whether a position is ground or not.
1120 bool is_ground(u64 seed, v3s16 p)
1122 double val1 = noise3d_param(get_ground_noise1_params(seed), p.X,p.Y,p.Z);
1123 return val_is_ground(val1, p, seed);
1126 // Amount of trees per area in nodes
1127 double tree_amount_2d(u64 seed, v2s16 p)
1129 /*double noise = noise2d_perlin(
1130 0.5+(float)p.X/250, 0.5+(float)p.Y/250,
1132 double noise = noise2d_perlin(
1133 0.5+(float)p.X/125, 0.5+(float)p.Y/125,
1135 double zeroval = -0.39;
1139 return 0.04 * (noise-zeroval) / (1.0-zeroval);
1142 double surface_humidity_2d(u64 seed, v2s16 p)
1144 double noise = noise2d_perlin(
1145 0.5+(float)p.X/500, 0.5+(float)p.Y/500,
1146 seed+72384, 4, 0.66);
1147 noise = (noise + 1.0)/2.0;
1156 double randomstone_amount_2d(u64 seed, v2s16 p)
1158 double noise = noise2d_perlin(
1159 0.5+(float)p.X/250, 0.5+(float)p.Y/250,
1160 seed+3829434, 5, 0.66);
1161 double zeroval = 0.1;
1165 return 0.01 * (noise-zeroval) / (1.0-zeroval);
1169 double largestone_amount_2d(u64 seed, v2s16 p)
1171 double noise = noise2d_perlin(
1172 0.5+(float)p.X/250, 0.5+(float)p.Y/250,
1173 seed+14143242, 5, 0.66);
1174 double zeroval = 0.3;
1178 return 0.005 * (noise-zeroval) / (1.0-zeroval);
1182 Incrementally find ground level from 3d noise
1184 s16 find_ground_level_from_noise(u64 seed, v2s16 p2d, s16 precision)
1186 // Start a bit fuzzy to make averaging lower precision values
1188 s16 level = myrand_range(-precision/2, precision/2);
1189 s16 dec[] = {31000, 100, 20, 4, 1, 0};
1191 for(i = 1; dec[i] != 0 && precision <= dec[i]; i++)
1193 // First find non-ground by going upwards
1194 // Don't stop in caves.
1196 s16 max = level+dec[i-1]*2;
1197 v3s16 p(p2d.X, level, p2d.Y);
1198 for(; p.Y < max; p.Y += dec[i])
1200 if(!is_ground(seed, p))
1207 // Then find ground by going downwards from there.
1208 // Go in caves, too, when precision is 1.
1210 s16 min = level-dec[i-1]*2;
1211 v3s16 p(p2d.X, level, p2d.Y);
1212 for(; p.Y>min; p.Y-=dec[i])
1214 bool ground = is_ground(seed, p);
1215 /*if(dec[i] == 1 && is_cave(seed, p))
1226 // This is more like the actual ground level
1227 level += dec[i-1]/2;
1232 double get_sector_average_ground_level(u64 seed, v2s16 sectorpos, double p=4);
1234 double get_sector_average_ground_level(u64 seed, v2s16 sectorpos, double p)
1236 v2s16 node_min = sectorpos*MAP_BLOCKSIZE;
1237 v2s16 node_max = (sectorpos+v2s16(1,1))*MAP_BLOCKSIZE-v2s16(1,1);
1239 a += find_ground_level_from_noise(seed,
1240 v2s16(node_min.X, node_min.Y), p);
1241 a += find_ground_level_from_noise(seed,
1242 v2s16(node_min.X, node_max.Y), p);
1243 a += find_ground_level_from_noise(seed,
1244 v2s16(node_max.X, node_max.Y), p);
1245 a += find_ground_level_from_noise(seed,
1246 v2s16(node_max.X, node_min.Y), p);
1247 a += find_ground_level_from_noise(seed,
1248 v2s16(node_min.X+MAP_BLOCKSIZE/2, node_min.Y+MAP_BLOCKSIZE/2), p);
1253 double get_sector_maximum_ground_level(u64 seed, v2s16 sectorpos, double p=4);
1255 double get_sector_maximum_ground_level(u64 seed, v2s16 sectorpos, double p)
1257 v2s16 node_min = sectorpos*MAP_BLOCKSIZE;
1258 v2s16 node_max = (sectorpos+v2s16(1,1))*MAP_BLOCKSIZE-v2s16(1,1);
1261 a = MYMAX(a, find_ground_level_from_noise(seed,
1262 v2s16(node_min.X, node_min.Y), p));
1263 a = MYMAX(a, find_ground_level_from_noise(seed,
1264 v2s16(node_min.X, node_max.Y), p));
1265 a = MYMAX(a, find_ground_level_from_noise(seed,
1266 v2s16(node_max.X, node_max.Y), p));
1267 a = MYMAX(a, find_ground_level_from_noise(seed,
1268 v2s16(node_min.X, node_min.Y), p));
1270 a = MYMAX(a, find_ground_level_from_noise(seed,
1271 v2s16(node_min.X+MAP_BLOCKSIZE/2, node_min.Y+MAP_BLOCKSIZE/2), p));
1272 // Side middle points
1273 a = MYMAX(a, find_ground_level_from_noise(seed,
1274 v2s16(node_min.X+MAP_BLOCKSIZE/2, node_min.Y), p));
1275 a = MYMAX(a, find_ground_level_from_noise(seed,
1276 v2s16(node_min.X+MAP_BLOCKSIZE/2, node_max.Y), p));
1277 a = MYMAX(a, find_ground_level_from_noise(seed,
1278 v2s16(node_min.X, node_min.Y+MAP_BLOCKSIZE/2), p));
1279 a = MYMAX(a, find_ground_level_from_noise(seed,
1280 v2s16(node_max.X, node_min.Y+MAP_BLOCKSIZE/2), p));
1284 double get_sector_minimum_ground_level(u64 seed, v2s16 sectorpos, double p=4);
1286 double get_sector_minimum_ground_level(u64 seed, v2s16 sectorpos, double p)
1288 v2s16 node_min = sectorpos*MAP_BLOCKSIZE;
1289 v2s16 node_max = (sectorpos+v2s16(1,1))*MAP_BLOCKSIZE-v2s16(1,1);
1292 a = MYMIN(a, find_ground_level_from_noise(seed,
1293 v2s16(node_min.X, node_min.Y), p));
1294 a = MYMIN(a, find_ground_level_from_noise(seed,
1295 v2s16(node_min.X, node_max.Y), p));
1296 a = MYMIN(a, find_ground_level_from_noise(seed,
1297 v2s16(node_max.X, node_max.Y), p));
1298 a = MYMIN(a, find_ground_level_from_noise(seed,
1299 v2s16(node_min.X, node_min.Y), p));
1301 a = MYMIN(a, find_ground_level_from_noise(seed,
1302 v2s16(node_min.X+MAP_BLOCKSIZE/2, node_min.Y+MAP_BLOCKSIZE/2), p));
1303 // Side middle points
1304 a = MYMIN(a, find_ground_level_from_noise(seed,
1305 v2s16(node_min.X+MAP_BLOCKSIZE/2, node_min.Y), p));
1306 a = MYMIN(a, find_ground_level_from_noise(seed,
1307 v2s16(node_min.X+MAP_BLOCKSIZE/2, node_max.Y), p));
1308 a = MYMIN(a, find_ground_level_from_noise(seed,
1309 v2s16(node_min.X, node_min.Y+MAP_BLOCKSIZE/2), p));
1310 a = MYMIN(a, find_ground_level_from_noise(seed,
1311 v2s16(node_max.X, node_min.Y+MAP_BLOCKSIZE/2), p));
1315 bool block_is_underground(u64 seed, v3s16 blockpos)
1317 s16 minimum_groundlevel = (s16)get_sector_minimum_ground_level(
1318 seed, v2s16(blockpos.X, blockpos.Z));
1320 if(blockpos.Y*MAP_BLOCKSIZE + MAP_BLOCKSIZE <= minimum_groundlevel)
1327 #define AVERAGE_MUD_AMOUNT 4
1329 double base_rock_level_2d(u64 seed, v2s16 p)
1331 // The base ground level
1332 double base = (double)WATER_LEVEL - (double)AVERAGE_MUD_AMOUNT
1333 + 20. * noise2d_perlin(
1334 0.5+(float)p.X/500., 0.5+(float)p.Y/500.,
1335 (seed>>32)+654879876, 6, 0.6);
1337 /*// A bit hillier one
1338 double base2 = WATER_LEVEL - 4.0 + 40. * noise2d_perlin(
1339 0.5+(float)p.X/250., 0.5+(float)p.Y/250.,
1340 (seed>>27)+90340, 6, 0.69);
1344 // Higher ground level
1345 double higher = (double)WATER_LEVEL + 25. + 35. * noise2d_perlin(
1346 0.5+(float)p.X/250., 0.5+(float)p.Y/250.,
1347 seed+85039, 5, 0.69);
1348 //higher = 30; // For debugging
1350 // Limit higher to at least base
1354 // Steepness factor of cliffs
1355 double b = 1.0 + 1.0 * noise2d_perlin(
1356 0.5+(float)p.X/250., 0.5+(float)p.Y/250.,
1358 b = rangelim(b, 0.0, 1000.0);
1361 b = rangelim(b, 3.0, 1000.0);
1362 //dstream<<"b="<<b<<std::endl;
1365 // Offset to more low
1366 double a_off = -0.2;
1367 // High/low selector
1368 /*double a = 0.5 + b * (a_off + noise2d_perlin(
1369 0.5+(float)p.X/500., 0.5+(float)p.Y/500.,
1370 seed-359, 6, 0.7));*/
1371 double a = (double)0.5 + b * (a_off + noise2d_perlin(
1372 0.5+(float)p.X/250., 0.5+(float)p.Y/250.,
1373 seed-359, 5, 0.60));
1375 a = rangelim(a, 0.0, 1.0);
1377 //dstream<<"a="<<a<<std::endl;
1379 double h = base*(1.0-a) + higher*a;
1386 double get_mud_add_amount(u64 seed, v2s16 p)
1388 return ((float)AVERAGE_MUD_AMOUNT + 3.0 * noise2d_perlin(
1389 0.5+(float)p.X/200, 0.5+(float)p.Y/200,
1390 seed+91013, 3, 0.55));
1394 bool get_have_sand(u64 seed, v2s16 p2d)
1396 // Determine whether to have sand here
1397 double sandnoise = noise2d_perlin(
1398 0.5+(float)p2d.X/500, 0.5+(float)p2d.Y/500,
1399 seed+59420, 3, 0.50);
1401 return (sandnoise > -0.15);
1405 Adds random objects to block, depending on the content of the block
1407 void add_random_objects(MapBlock *block)
1410 for(s16 z0=0; z0<MAP_BLOCKSIZE; z0++)
1411 for(s16 x0=0; x0<MAP_BLOCKSIZE; x0++)
1413 bool last_node_walkable = false;
1414 for(s16 y0=0; y0<MAP_BLOCKSIZE; y0++)
1417 MapNode n = block->getNodeNoEx(p);
1418 if(n.getContent() == CONTENT_IGNORE)
1420 if(content_features(n).liquid_type != LIQUID_NONE)
1422 if(content_features(n).walkable)
1424 last_node_walkable = true;
1427 if(last_node_walkable)
1429 // If block contains light information
1430 if(content_features(n).param_type == CPT_LIGHT)
1432 if(n.getLight(LIGHTBANK_DAY) <= 3)
1434 if(myrand() % 300 == 0)
1436 v3f pos_f = intToFloat(p+block->getPosRelative(), BS);
1438 ServerActiveObject *obj = new RatSAO(NULL, 0, pos_f);
1439 std::string data = obj->getStaticData();
1440 StaticObject s_obj(obj->getType(),
1441 obj->getBasePosition(), data);
1443 block->m_static_objects.insert(0, s_obj);
1444 block->m_static_objects.insert(0, s_obj);
1445 block->m_static_objects.insert(0, s_obj);
1446 block->m_static_objects.insert(0, s_obj);
1447 block->m_static_objects.insert(0, s_obj);
1448 block->m_static_objects.insert(0, s_obj);
1451 if(myrand() % 1000 == 0)
1453 v3f pos_f = intToFloat(p+block->getPosRelative(), BS);
1455 ServerActiveObject *obj = new Oerkki1SAO(NULL,0,pos_f);
1456 std::string data = obj->getStaticData();
1457 StaticObject s_obj(obj->getType(),
1458 obj->getBasePosition(), data);
1460 block->m_static_objects.insert(0, s_obj);
1466 last_node_walkable = false;
1469 block->setChangedFlag();
1473 void make_block(BlockMakeData *data)
1477 //dstream<<"makeBlock: no-op"<<std::endl;
1481 v3s16 blockpos = data->blockpos;
1483 /*dstream<<"makeBlock(): ("<<blockpos.X<<","<<blockpos.Y<<","
1484 <<blockpos.Z<<")"<<std::endl;*/
1486 ManualMapVoxelManipulator &vmanip = *(data->vmanip);
1487 v3s16 blockpos_min = blockpos - v3s16(1,1,1);
1488 v3s16 blockpos_max = blockpos + v3s16(1,1,1);
1489 // Area of center block
1490 v3s16 node_min = blockpos*MAP_BLOCKSIZE;
1491 v3s16 node_max = (blockpos+v3s16(1,1,1))*MAP_BLOCKSIZE-v3s16(1,1,1);
1492 // Full allocated area
1493 v3s16 full_node_min = (blockpos-1)*MAP_BLOCKSIZE;
1494 v3s16 full_node_max = (blockpos+2)*MAP_BLOCKSIZE-v3s16(1,1,1);
1496 double block_area_nodes = MAP_BLOCKSIZE*MAP_BLOCKSIZE;
1498 v2s16 p2d_center(node_min.X+MAP_BLOCKSIZE/2, node_min.Z+MAP_BLOCKSIZE/2);
1501 Get average ground level from noise
1504 s16 approx_groundlevel = (s16)get_sector_average_ground_level(
1505 data->seed, v2s16(blockpos.X, blockpos.Z));
1506 //dstream<<"approx_groundlevel="<<approx_groundlevel<<std::endl;
1508 s16 approx_ground_depth = approx_groundlevel - (node_min.Y+MAP_BLOCKSIZE/2);
1510 s16 minimum_groundlevel = (s16)get_sector_minimum_ground_level(
1511 data->seed, v2s16(blockpos.X, blockpos.Z));
1512 // Minimum amount of ground above the top of the central block
1513 s16 minimum_ground_depth = minimum_groundlevel - node_max.Y;
1515 s16 maximum_groundlevel = (s16)get_sector_maximum_ground_level(
1516 data->seed, v2s16(blockpos.X, blockpos.Z), 1);
1517 // Maximum amount of ground above the bottom of the central block
1518 s16 maximum_ground_depth = maximum_groundlevel - node_min.Y;
1522 Special case for high air or water: Just fill with air and water.
1524 if(maximum_ground_depth < -20)
1526 for(s16 x=node_min.X; x<=node_max.X; x++)
1527 for(s16 z=node_min.Z; z<=node_max.Z; z++)
1532 // Use fast index incrementing
1533 v3s16 em = vmanip.m_area.getExtent();
1534 u32 i = vmanip.m_area.index(v3s16(p2d.X, node_min.Y, p2d.Y));
1535 for(s16 y=node_min.Y; y<=node_max.Y; y++)
1537 // Only modify places that have no content
1538 if(vmanip.m_data[i].getContent() == CONTENT_IGNORE)
1540 if(y <= WATER_LEVEL)
1541 vmanip.m_data[i] = MapNode(CONTENT_WATERSOURCE);
1543 vmanip.m_data[i] = MapNode(CONTENT_AIR);
1546 data->vmanip->m_area.add_y(em, i, 1);
1557 If block is deep underground, this is set to true and ground
1558 density noise is not generated, for speed optimization.
1560 bool all_is_ground_except_caves = (minimum_ground_depth > 40);
1563 Create a block-specific seed
1565 u32 blockseed = (u32)(data->seed%0x100000000ULL) + full_node_min.Z*38134234
1566 + full_node_min.Y*42123 + full_node_min.X*23;
1572 //NoiseBuffer noisebuf1;
1573 //NoiseBuffer noisebuf2;
1574 NoiseBuffer noisebuf_cave;
1575 NoiseBuffer noisebuf_ground;
1576 NoiseBuffer noisebuf_ground_crumbleness;
1577 NoiseBuffer noisebuf_ground_wetness;
1579 v3f minpos_f(node_min.X, node_min.Y, node_min.Z);
1580 v3f maxpos_f(node_max.X, node_max.Y, node_max.Z);
1582 //TimeTaker timer("noisebuf.create");
1588 noisebuf_cave.create(get_cave_noise1_params(data->seed),
1589 minpos_f.X, minpos_f.Y, minpos_f.Z,
1590 maxpos_f.X, maxpos_f.Y, maxpos_f.Z,
1592 noisebuf_cave.multiply(get_cave_noise2_params(data->seed));
1600 v3f sl = v3f(4.0, 4.0, 4.0);
1605 if(all_is_ground_except_caves == false)
1606 //noisebuf_ground.create(data->seed+983240, 6, 0.60, false,
1607 noisebuf_ground.create(get_ground_noise1_params(data->seed),
1608 minpos_f.X, minpos_f.Y, minpos_f.Z,
1609 maxpos_f.X, maxpos_f.Y, maxpos_f.Z,
1613 Ground property noise
1615 sl = v3f(2.5, 2.5, 2.5);
1616 noisebuf_ground_crumbleness.create(
1617 get_ground_crumbleness_params(data->seed),
1618 minpos_f.X, minpos_f.Y, minpos_f.Z,
1619 maxpos_f.X, maxpos_f.Y+5, maxpos_f.Z,
1621 noisebuf_ground_wetness.create(
1622 get_ground_wetness_params(data->seed),
1623 minpos_f.X, minpos_f.Y, minpos_f.Z,
1624 maxpos_f.X, maxpos_f.Y+5, maxpos_f.Z,
1629 Make base ground level
1632 for(s16 x=node_min.X; x<=node_max.X; x++)
1633 for(s16 z=node_min.Z; z<=node_max.Z; z++)
1638 // Use fast index incrementing
1639 v3s16 em = vmanip.m_area.getExtent();
1640 u32 i = vmanip.m_area.index(v3s16(p2d.X, node_min.Y, p2d.Y));
1641 for(s16 y=node_min.Y; y<=node_max.Y; y++)
1643 // Only modify places that have no content
1644 if(vmanip.m_data[i].getContent() == CONTENT_IGNORE)
1646 // First priority: make air and water.
1647 // This avoids caves inside water.
1648 if(all_is_ground_except_caves == false
1649 && val_is_ground(noisebuf_ground.get(x,y,z),
1650 v3s16(x,y,z), data->seed) == false)
1652 if(y <= WATER_LEVEL)
1653 vmanip.m_data[i] = MapNode(CONTENT_WATERSOURCE);
1655 vmanip.m_data[i] = MapNode(CONTENT_AIR);
1657 else if(noisebuf_cave.get(x,y,z) > CAVE_NOISE_THRESHOLD)
1658 vmanip.m_data[i] = MapNode(CONTENT_AIR);
1660 vmanip.m_data[i] = MapNode(CONTENT_STONE);
1663 data->vmanip->m_area.add_y(em, i, 1);
1673 PseudoRandom mineralrandom(blockseed);
1678 for(s16 i=0; i<approx_ground_depth/4; i++)
1680 if(mineralrandom.next()%50 == 0)
1682 s16 x = mineralrandom.range(node_min.X+1, node_max.X-1);
1683 s16 y = mineralrandom.range(node_min.Y+1, node_max.Y-1);
1684 s16 z = mineralrandom.range(node_min.Z+1, node_max.Z-1);
1685 for(u16 i=0; i<27; i++)
1687 v3s16 p = v3s16(x,y,z) + g_27dirs[i];
1688 u32 vi = vmanip.m_area.index(p);
1689 if(vmanip.m_data[vi].getContent() == CONTENT_STONE)
1690 if(mineralrandom.next()%8 == 0)
1691 vmanip.m_data[vi] = MapNode(CONTENT_MESE);
1700 u16 a = mineralrandom.range(0,15);
1702 u16 amount = 20 * a/1000;
1703 for(s16 i=0; i<amount; i++)
1705 s16 x = mineralrandom.range(node_min.X+1, node_max.X-1);
1706 s16 y = mineralrandom.range(node_min.Y+1, node_max.Y-1);
1707 s16 z = mineralrandom.range(node_min.Z+1, node_max.Z-1);
1709 u8 base_content = CONTENT_STONE;
1710 MapNode new_content(CONTENT_IGNORE);
1713 if(noisebuf_ground_crumbleness.get(x,y+5,z) < -0.1)
1715 new_content = MapNode(CONTENT_STONE, MINERAL_COAL);
1719 if(noisebuf_ground_wetness.get(x,y+5,z) > 0.0)
1720 new_content = MapNode(CONTENT_STONE, MINERAL_IRON);
1721 /*if(noisebuf_ground_wetness.get(x,y,z) > 0.0)
1722 vmanip.m_data[i] = MapNode(CONTENT_MUD);
1724 vmanip.m_data[i] = MapNode(CONTENT_SAND);*/
1726 /*else if(noisebuf_ground_crumbleness.get(x,y,z) > 0.1)
1730 if(new_content.getContent() != CONTENT_IGNORE)
1732 for(u16 i=0; i<27; i++)
1734 v3s16 p = v3s16(x,y,z) + g_27dirs[i];
1735 u32 vi = vmanip.m_area.index(p);
1736 if(vmanip.m_data[vi].getContent() == base_content)
1738 if(mineralrandom.next()%sparseness == 0)
1739 vmanip.m_data[vi] = new_content;
1748 //for(s16 i=0; i < MYMAX(0, 50 - abs(node_min.Y+8 - (-30))); i++)
1749 //for(s16 i=0; i<50; i++)
1750 u16 coal_amount = 30;
1751 u16 coal_rareness = 60 / coal_amount;
1752 if(coal_rareness == 0)
1754 if(mineralrandom.next()%coal_rareness == 0)
1756 u16 a = mineralrandom.next() % 16;
1757 u16 amount = coal_amount * a*a*a / 1000;
1758 for(s16 i=0; i<amount; i++)
1760 s16 x = mineralrandom.range(node_min.X+1, node_max.X-1);
1761 s16 y = mineralrandom.range(node_min.Y+1, node_max.Y-1);
1762 s16 z = mineralrandom.range(node_min.Z+1, node_max.Z-1);
1763 for(u16 i=0; i<27; i++)
1765 v3s16 p = v3s16(x,y,z) + g_27dirs[i];
1766 u32 vi = vmanip.m_area.index(p);
1767 if(vmanip.m_data[vi].getContent() == CONTENT_STONE)
1768 if(mineralrandom.next()%8 == 0)
1769 vmanip.m_data[vi] = MapNode(CONTENT_STONE, MINERAL_COAL);
1776 u16 iron_amount = 8;
1777 u16 iron_rareness = 60 / iron_amount;
1778 if(iron_rareness == 0)
1780 if(mineralrandom.next()%iron_rareness == 0)
1782 u16 a = mineralrandom.next() % 16;
1783 u16 amount = iron_amount * a*a*a / 1000;
1784 for(s16 i=0; i<amount; i++)
1786 s16 x = mineralrandom.range(node_min.X+1, node_max.X-1);
1787 s16 y = mineralrandom.range(node_min.Y+1, node_max.Y-1);
1788 s16 z = mineralrandom.range(node_min.Z+1, node_max.Z-1);
1789 for(u16 i=0; i<27; i++)
1791 v3s16 p = v3s16(x,y,z) + g_27dirs[i];
1792 u32 vi = vmanip.m_area.index(p);
1793 if(vmanip.m_data[vi].getContent() == CONTENT_STONE)
1794 if(mineralrandom.next()%8 == 0)
1795 vmanip.m_data[vi] = MapNode(CONTENT_STONE, MINERAL_IRON);
1802 Add mud and sand and others underground (in place of stone)
1805 for(s16 x=node_min.X; x<=node_max.X; x++)
1806 for(s16 z=node_min.Z; z<=node_max.Z; z++)
1811 // Use fast index incrementing
1812 v3s16 em = vmanip.m_area.getExtent();
1813 u32 i = vmanip.m_area.index(v3s16(p2d.X, node_max.Y, p2d.Y));
1814 for(s16 y=node_max.Y; y>=node_min.Y; y--)
1816 if(vmanip.m_data[i].getContent() == CONTENT_STONE)
1818 if(noisebuf_ground_crumbleness.get(x,y,z) > 1.3)
1820 if(noisebuf_ground_wetness.get(x,y,z) > 0.0)
1821 vmanip.m_data[i] = MapNode(CONTENT_MUD);
1823 vmanip.m_data[i] = MapNode(CONTENT_SAND);
1825 else if(noisebuf_ground_crumbleness.get(x,y,z) > 0.7)
1827 if(noisebuf_ground_wetness.get(x,y,z) < -0.6)
1828 vmanip.m_data[i] = MapNode(CONTENT_GRAVEL);
1830 else if(noisebuf_ground_crumbleness.get(x,y,z) <
1831 -3.0 + MYMIN(0.1 * sqrt((float)MYMAX(0, -y)), 1.5))
1833 vmanip.m_data[i] = MapNode(CONTENT_LAVASOURCE);
1834 for(s16 x1=-1; x1<=1; x1++)
1835 for(s16 y1=-1; y1<=1; y1++)
1836 for(s16 z1=-1; z1<=1; z1++)
1837 data->transforming_liquid.push_back(
1838 v3s16(p2d.X+x1, y+y1, p2d.Y+z1));
1842 data->vmanip->m_area.add_y(em, i, -1);
1851 //if(node_min.Y < approx_groundlevel)
1852 //if(myrand() % 3 == 0)
1853 //if(myrand() % 3 == 0 && node_min.Y < approx_groundlevel)
1854 //if(myrand() % 100 == 0 && node_min.Y < approx_groundlevel)
1855 //float dungeon_rarity = g_settings.getFloat("dungeon_rarity");
1856 float dungeon_rarity = 0.02;
1857 if(((noise3d(blockpos.X,blockpos.Y,blockpos.Z,data->seed)+1.0)/2.0)
1859 && node_min.Y < approx_groundlevel)
1861 // Dungeon generator doesn't modify places which have this set
1862 data->vmanip->clearFlag(VMANIP_FLAG_DUNGEON_INSIDE
1863 | VMANIP_FLAG_DUNGEON_PRESERVE);
1865 // Set all air and water to be untouchable to make dungeons open
1866 // to caves and open air
1867 for(s16 x=full_node_min.X; x<=full_node_max.X; x++)
1868 for(s16 z=full_node_min.Z; z<=full_node_max.Z; z++)
1873 // Use fast index incrementing
1874 v3s16 em = vmanip.m_area.getExtent();
1875 u32 i = vmanip.m_area.index(v3s16(p2d.X, full_node_max.Y, p2d.Y));
1876 for(s16 y=full_node_max.Y; y>=full_node_min.Y; y--)
1878 if(vmanip.m_data[i].getContent() == CONTENT_AIR)
1879 vmanip.m_flags[i] |= VMANIP_FLAG_DUNGEON_PRESERVE;
1880 else if(vmanip.m_data[i].getContent() == CONTENT_WATERSOURCE)
1881 vmanip.m_flags[i] |= VMANIP_FLAG_DUNGEON_PRESERVE;
1882 data->vmanip->m_area.add_y(em, i, -1);
1887 PseudoRandom random(blockseed+2);
1890 make_dungeon1(vmanip, random);
1892 // Convert some cobble to mossy cobble
1893 for(s16 x=full_node_min.X; x<=full_node_max.X; x++)
1894 for(s16 z=full_node_min.Z; z<=full_node_max.Z; z++)
1899 // Use fast index incrementing
1900 v3s16 em = vmanip.m_area.getExtent();
1901 u32 i = vmanip.m_area.index(v3s16(p2d.X, full_node_max.Y, p2d.Y));
1902 for(s16 y=full_node_max.Y; y>=full_node_min.Y; y--)
1904 // (noisebuf not used because it doesn't contain the
1906 double wetness = noise3d_param(
1907 get_ground_wetness_params(data->seed), x,y,z);
1908 double d = noise3d_perlin((float)x/2.5,
1909 (float)y/2.5,(float)z/2.5,
1911 if(vmanip.m_data[i].getContent() == CONTENT_COBBLE)
1915 vmanip.m_data[i].setContent(CONTENT_MOSSYCOBBLE);
1918 /*else if(vmanip.m_flags[i] & VMANIP_FLAG_DUNGEON_INSIDE)
1921 vmanip.m_data[i].setContent(CONTENT_MUD);
1923 data->vmanip->m_area.add_y(em, i, -1);
1933 PseudoRandom ncrandom(blockseed+9324342);
1934 if(ncrandom.range(0, 1000) == 0 && blockpos.Y <= -3)
1936 make_nc(vmanip, ncrandom);
1941 Add top and bottom side of water to transforming_liquid queue
1944 for(s16 x=node_min.X; x<=node_max.X; x++)
1945 for(s16 z=node_min.Z; z<=node_max.Z; z++)
1950 bool water_found = false;
1951 // Use fast index incrementing
1952 v3s16 em = vmanip.m_area.getExtent();
1953 u32 i = vmanip.m_area.index(v3s16(p2d.X, node_max.Y, p2d.Y));
1954 for(s16 y=node_max.Y; y>=node_min.Y; y--)
1956 if(water_found == false)
1958 if(vmanip.m_data[i].getContent() == CONTENT_WATERSOURCE)
1960 v3s16 p = v3s16(p2d.X, y, p2d.Y);
1961 data->transforming_liquid.push_back(p);
1967 // This can be done because water_found can only
1968 // turn to true and end up here after going through
1970 if(vmanip.m_data[i+1].getContent() != CONTENT_WATERSOURCE)
1972 v3s16 p = v3s16(p2d.X, y+1, p2d.Y);
1973 data->transforming_liquid.push_back(p);
1974 water_found = false;
1978 data->vmanip->m_area.add_y(em, i, -1);
1984 If close to ground level
1987 //if(abs(approx_ground_depth) < 30)
1988 if(minimum_ground_depth < 5 && maximum_ground_depth > -5)
1994 for(s16 x=node_min.X; x<=node_max.X; x++)
1995 for(s16 z=node_min.Z; z<=node_max.Z; z++)
2000 bool possibly_have_sand = get_have_sand(data->seed, p2d);
2001 bool have_sand = false;
2002 u32 current_depth = 0;
2003 bool air_detected = false;
2004 bool water_detected = false;
2005 bool have_clay = false;
2007 // Use fast index incrementing
2008 s16 start_y = node_max.Y+2;
2009 v3s16 em = vmanip.m_area.getExtent();
2010 u32 i = vmanip.m_area.index(v3s16(p2d.X, start_y, p2d.Y));
2011 for(s16 y=start_y; y>=node_min.Y-3; y--)
2013 if(vmanip.m_data[i].getContent() == CONTENT_WATERSOURCE)
2014 water_detected = true;
2015 if(vmanip.m_data[i].getContent() == CONTENT_AIR)
2016 air_detected = true;
2018 if((vmanip.m_data[i].getContent() == CONTENT_STONE
2019 || vmanip.m_data[i].getContent() == CONTENT_GRASS
2020 || vmanip.m_data[i].getContent() == CONTENT_MUD
2021 || vmanip.m_data[i].getContent() == CONTENT_SAND
2022 || vmanip.m_data[i].getContent() == CONTENT_GRAVEL
2023 ) && (air_detected || water_detected))
2025 if(current_depth == 0 && y <= WATER_LEVEL+2
2026 && possibly_have_sand)
2029 if(current_depth < 4)
2033 // Determine whether to have clay in the sand here
2034 double claynoise = noise2d_perlin(
2035 0.5+(float)p2d.X/500, 0.5+(float)p2d.Y/500,
2036 data->seed+4321, 6, 0.95) + 0.5;
2038 have_clay = (y <= WATER_LEVEL) && (y >= WATER_LEVEL-2) && (
2039 ((claynoise > 0) && (claynoise < 0.04) && (current_depth == 0)) ||
2040 ((claynoise > 0) && (claynoise < 0.12) && (current_depth == 1))
2043 vmanip.m_data[i] = MapNode(CONTENT_CLAY);
2045 vmanip.m_data[i] = MapNode(CONTENT_SAND);
2048 else if(current_depth==0 && !water_detected
2049 && y >= WATER_LEVEL && air_detected)
2050 vmanip.m_data[i] = MapNode(CONTENT_GRASS);
2053 vmanip.m_data[i] = MapNode(CONTENT_MUD);
2057 if(vmanip.m_data[i].getContent() == CONTENT_MUD
2058 || vmanip.m_data[i].getContent() == CONTENT_GRASS)
2059 vmanip.m_data[i] = MapNode(CONTENT_STONE);
2064 if(current_depth >= 8)
2067 else if(current_depth != 0)
2070 data->vmanip->m_area.add_y(em, i, -1);
2076 Calculate some stuff
2079 float surface_humidity = surface_humidity_2d(data->seed, p2d_center);
2080 bool is_jungle = surface_humidity > 0.75;
2082 u32 tree_count = block_area_nodes * tree_amount_2d(data->seed, p2d_center);
2089 PseudoRandom treerandom(blockseed);
2090 // Put trees in random places on part of division
2091 for(u32 i=0; i<tree_count; i++)
2093 s16 x = treerandom.range(node_min.X, node_max.X);
2094 s16 z = treerandom.range(node_min.Z, node_max.Z);
2095 //s16 y = find_ground_level(data->vmanip, v2s16(x,z));
2096 s16 y = find_ground_level_from_noise(data->seed, v2s16(x,z), 4);
2097 // Don't make a tree under water level
2100 // Make sure tree fits (only trees whose starting point is
2101 // at this block are added)
2102 if(y < node_min.Y || y > node_max.Y)
2105 Find exact ground level
2109 for(; p.Y >= y-6; p.Y--)
2111 u32 i = data->vmanip->m_area.index(p);
2112 MapNode *n = &data->vmanip->m_data[i];
2113 if(n->getContent() != CONTENT_AIR && n->getContent() != CONTENT_WATERSOURCE && n->getContent() != CONTENT_IGNORE)
2119 // If not found, handle next one
2124 u32 i = data->vmanip->m_area.index(p);
2125 MapNode *n = &data->vmanip->m_data[i];
2127 if(n->getContent() != CONTENT_MUD && n->getContent() != CONTENT_GRASS && n->getContent() != CONTENT_SAND)
2130 // Papyrus grows only on mud and in water
2131 if(n->getContent() == CONTENT_MUD && y <= WATER_LEVEL)
2134 make_papyrus(vmanip, p);
2136 // Trees grow only on mud and grass, on land
2137 else if((n->getContent() == CONTENT_MUD || n->getContent() == CONTENT_GRASS) && y > WATER_LEVEL + 2)
2140 //if(surface_humidity_2d(data->seed, v2s16(x, y)) < 0.5)
2141 if(is_jungle == false)
2144 if(myrand_range(0,4) != 0)
2145 is_apple_tree = false;
2147 is_apple_tree = noise2d_perlin(
2148 0.5+(float)p.X/100, 0.5+(float)p.Z/100,
2149 data->seed+342902, 3, 0.45) > 0.2;
2150 make_tree(vmanip, p, is_apple_tree);
2153 make_jungletree(vmanip, p);
2155 // Cactii grow only on sand, on land
2156 else if(n->getContent() == CONTENT_SAND && y > WATER_LEVEL + 2)
2159 make_cactus(vmanip, p);
2169 PseudoRandom grassrandom(blockseed);
2170 for(u32 i=0; i<surface_humidity*5*tree_count; i++)
2172 s16 x = grassrandom.range(node_min.X, node_max.X);
2173 s16 z = grassrandom.range(node_min.Z, node_max.Z);
2174 s16 y = find_ground_level_from_noise(data->seed, v2s16(x,z), 4);
2177 if(y < node_min.Y || y > node_max.Y)
2180 Find exact ground level
2184 for(; p.Y >= y-6; p.Y--)
2186 u32 i = data->vmanip->m_area.index(p);
2187 MapNode *n = &data->vmanip->m_data[i];
2188 if(content_features(*n).is_ground_content
2189 || n->getContent() == CONTENT_JUNGLETREE)
2195 // If not found, handle next one
2199 if(vmanip.m_area.contains(p) == false)
2201 if(vmanip.m_data[vmanip.m_area.index(p)].getContent() != CONTENT_AIR)
2204 if(vmanip.m_area.contains(p))
2205 vmanip.m_data[vmanip.m_area.index(p)] = CONTENT_MUD;
2207 if(vmanip.m_area.contains(p))
2208 vmanip.m_data[vmanip.m_area.index(p)] = CONTENT_JUNGLEGRASS;
2214 Add some kind of random stones
2217 u32 random_stone_count = block_area_nodes *
2218 randomstone_amount_2d(data->seed, p2d_center);
2219 // Put in random places on part of division
2220 for(u32 i=0; i<random_stone_count; i++)
2222 s16 x = myrand_range(node_min.X, node_max.X);
2223 s16 z = myrand_range(node_min.Z, node_max.Z);
2224 s16 y = find_ground_level_from_noise(data->seed, v2s16(x,z), 1);
2225 // Don't add under water level
2226 /*if(y < WATER_LEVEL)
2228 // Don't add if doesn't belong to this block
2229 if(y < node_min.Y || y > node_max.Y)
2234 u32 i = data->vmanip->m_area.index(v3s16(p));
2235 MapNode *n = &data->vmanip->m_data[i];
2236 if(n->getContent() != CONTENT_MUD && n->getContent() != CONTENT_GRASS)
2239 // Will be placed one higher
2242 make_randomstone(data->vmanip, p);
2251 u32 large_stone_count = block_area_nodes *
2252 largestone_amount_2d(data->seed, p2d_center);
2253 //u32 large_stone_count = 1;
2254 // Put in random places on part of division
2255 for(u32 i=0; i<large_stone_count; i++)
2257 s16 x = myrand_range(node_min.X, node_max.X);
2258 s16 z = myrand_range(node_min.Z, node_max.Z);
2259 s16 y = find_ground_level_from_noise(data->seed, v2s16(x,z), 1);
2260 // Don't add under water level
2261 /*if(y < WATER_LEVEL)
2263 // Don't add if doesn't belong to this block
2264 if(y < node_min.Y || y > node_max.Y)
2269 u32 i = data->vmanip->m_area.index(v3s16(p));
2270 MapNode *n = &data->vmanip->m_data[i];
2271 if(n->getContent() != CONTENT_MUD && n->getContent() != CONTENT_GRASS)
2274 // Will be placed one lower
2277 make_largestone(data->vmanip, p);
2284 BlockMakeData::BlockMakeData():
2290 BlockMakeData::~BlockMakeData()
2295 }; // namespace mapgen