3 Copyright (C) 2010-2013 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.
20 #include "dungeongen.h"
29 #include "settings.h" // For g_settings
30 #include "main.h" // For g_profiler
32 NoiseParams nparams_dungeon_rarity =
33 {0.0, 1.0, v3f(500.0, 500.0, 500.0), 0, 2, 0.8};
34 NoiseParams nparams_dungeon_wetness =
35 {0.0, 1.0, v3f(40.0, 40.0, 40.0), 32474, 4, 1.1};
36 NoiseParams nparams_dungeon_density =
37 {0.0, 1.0, v3f(2.5, 2.5, 2.5), 0, 2, 1.4};
40 ///////////////////////////////////////////////////////////////////////////////
43 DungeonGen::DungeonGen(INodeDefManager *ndef, u64 seed, s16 waterlevel) {
46 this->water_level = waterlevel;
48 np_rarity = &nparams_dungeon_rarity;
49 np_wetness = &nparams_dungeon_wetness;
50 np_density = &nparams_dungeon_density;
52 cid_water_source = ndef->getId("mapgen_water_source");
53 cid_cobble = ndef->getId("mapgen_cobble");
54 cid_mossycobble = ndef->getId("mapgen_mossycobble");
55 cid_torch = ndef->getId("default:torch");
60 void DungeonGen::generate(ManualMapVoxelManipulator *vm, u32 bseed,
61 v3s16 nmin, v3s16 nmax) {
62 //TimeTaker t("gen dungeons");
63 int approx_groundlevel = 10 + water_level;
65 if ((nmin.Y + nmax.Y) / 2 >= approx_groundlevel ||
66 NoisePerlin3D(np_rarity, nmin.X, nmin.Y, nmin.Z, mapseed) < 0.2)
70 this->blockseed = bseed;
71 random.seed(bseed + 2);
73 cid_water_source = ndef->getId("mapgen_water_source");
74 cid_cobble = ndef->getId("mapgen_cobble");
75 cid_mossycobble = ndef->getId("mapgen_mossycobble");
76 //cid_torch = ndef->getId("default:torch");
77 cid_cobblestair = ndef->getId("stairs:stair_cobble");
79 // Dungeon generator doesn't modify places which have this set
80 vmanip->clearFlag(VMANIP_FLAG_DUNGEON_INSIDE | VMANIP_FLAG_DUNGEON_PRESERVE);
82 // Set all air and water to be untouchable to make dungeons open
83 // to caves and open air
84 for (s16 z = nmin.Z; z <= nmax.Z; z++) {
85 for (s16 y = nmin.Y; y <= nmax.Y; y++) {
86 u32 i = vmanip->m_area.index(nmin.X, y, z);
87 for (s16 x = nmin.X; x <= nmax.X; x++) {
88 content_t c = vmanip->m_data[i].getContent();
89 if (c == CONTENT_AIR || c == cid_water_source)
90 vmanip->m_flags[i] |= VMANIP_FLAG_DUNGEON_PRESERVE;
97 makeDungeon(v3s16(1,1,1) * MAP_BLOCKSIZE);
99 // Convert some cobble to mossy cobble
100 for (s16 z = nmin.Z; z <= nmax.Z; z++) {
101 for (s16 y = nmin.Y; y <= nmax.Y; y++) {
102 u32 i = vmanip->m_area.index(nmin.X, y, z);
103 for (s16 x = nmin.X; x <= nmax.X; x++) {
104 if (vmanip->m_data[i].getContent() == cid_cobble) {
105 float wetness = NoisePerlin3D(np_wetness, x, y, z, mapseed);
106 float density = NoisePerlin3D(np_density, x, y, z, blockseed);
107 if (density < wetness / 3.0)
108 vmanip->m_data[i].setContent(cid_mossycobble);
115 //printf("== gen dungeons: %dms\n", t.stop());
119 void DungeonGen::makeDungeon(v3s16 start_padding)
121 v3s16 areasize = vmanip->m_area.getExtent();
126 Find place for first room
129 for (u32 i = 0; i < 100; i++)
131 bool is_large_room = ((random.next() & 3) == 1);
132 roomsize = is_large_room ?
133 v3s16(random.range(8, 16),random.range(8, 16),random.range(8, 16)) :
134 v3s16(random.range(4, 8),random.range(4, 6),random.range(4, 8));
136 // start_padding is used to disallow starting the generation of
137 // a dungeon in a neighboring generation chunk
138 roomplace = vmanip->m_area.MinEdge + start_padding + v3s16(
139 random.range(0,areasize.X-roomsize.X-1-start_padding.X),
140 random.range(0,areasize.Y-roomsize.Y-1-start_padding.Y),
141 random.range(0,areasize.Z-roomsize.Z-1-start_padding.Z));
144 Check that we're not putting the room to an unknown place,
145 otherwise it might end up floating in the air
148 for (s16 z = 1; z < roomsize.Z - 1; z++)
149 for (s16 y = 1; y < roomsize.Y - 1; y++)
150 for (s16 x = 1; x < roomsize.X - 1; x++)
152 v3s16 p = roomplace + v3s16(x, y, z);
153 u32 vi = vmanip->m_area.index(p);
154 if (vmanip->m_flags[vi] & VMANIP_FLAG_DUNGEON_INSIDE)
159 if (vmanip->m_data[vi].getContent() == CONTENT_IGNORE)
173 Stores the center position of the last room made, so that
174 a new corridor can be started from the last room instead of
175 the new room, if chosen so.
177 v3s16 last_room_center = roomplace + v3s16(roomsize.X / 2, 1, roomsize.Z / 2);
179 u32 room_count = random.range(2, 16);
180 for (u32 i = 0; i < room_count; i++)
182 // Make a room to the determined place
183 makeRoom(roomsize, roomplace);
185 v3s16 room_center = roomplace + v3s16(roomsize.X / 2, 1, roomsize.Z / 2);
187 // Place torch at room center (for testing)
188 //vmanip->m_data[vmanip->m_area.index(room_center)] = MapNode(cid_torch);
191 if (i == room_count - 1)
194 // Determine walker start position
196 bool start_in_last_room = (random.range(0, 2) != 0);
198 v3s16 walker_start_place;
200 if(start_in_last_room)
202 walker_start_place = last_room_center;
206 walker_start_place = room_center;
207 // Store center of current room as the last one
208 last_room_center = room_center;
211 // Create walker and find a place for a door
215 m_pos = walker_start_place;
216 bool r = findPlaceForDoor(doorplace, doordir);
220 if (random.range(0,1) == 0)
222 makeDoor(doorplace, doordir);
224 // Don't actually make a door
225 doorplace -= doordir;
227 // Make a random corridor starting from the door
229 v3s16 corridor_end_dir;
230 makeCorridor(doorplace, doordir, corridor_end, corridor_end_dir);
232 // Find a place for a random sized room
233 roomsize = v3s16(random.range(4,8),random.range(4,6),random.range(4,8));
234 m_pos = corridor_end;
235 m_dir = corridor_end_dir;
236 r = findPlaceForRoomDoor(roomsize, doorplace, doordir, roomplace);
240 if (random.range(0,1) == 0)
242 makeDoor(doorplace, doordir);
244 // Don't actually make a door
245 roomplace -= doordir;
251 void DungeonGen::makeRoom(v3s16 roomsize, v3s16 roomplace)
253 MapNode n_cobble(cid_cobble);
254 MapNode n_air(CONTENT_AIR);
257 for (s16 z = 0; z < roomsize.Z; z++)
258 for (s16 y = 0; y < roomsize.Y; y++)
261 v3s16 p = roomplace + v3s16(0, y, z);
262 if (vmanip->m_area.contains(p) == false)
264 u32 vi = vmanip->m_area.index(p);
265 if (vmanip->m_flags[vi] & VMANIP_FLAG_DUNGEON_UNTOUCHABLE)
267 vmanip->m_data[vi] = n_cobble;
270 v3s16 p = roomplace + v3s16(roomsize.X - 1, y, z);
271 if (vmanip->m_area.contains(p) == false)
273 u32 vi = vmanip->m_area.index(p);
274 if (vmanip->m_flags[vi] & VMANIP_FLAG_DUNGEON_UNTOUCHABLE)
276 vmanip->m_data[vi] = n_cobble;
281 for (s16 x = 0; x < roomsize.X; x++)
282 for (s16 y = 0; y < roomsize.Y; y++)
285 v3s16 p = roomplace + v3s16(x, y, 0);
286 if (vmanip->m_area.contains(p) == false)
288 u32 vi = vmanip->m_area.index(p);
289 if (vmanip->m_flags[vi] & VMANIP_FLAG_DUNGEON_UNTOUCHABLE)
291 vmanip->m_data[vi] = n_cobble;
294 v3s16 p = roomplace + v3s16(x, y, roomsize.Z - 1);
295 if (vmanip->m_area.contains(p) == false)
297 u32 vi = vmanip->m_area.index(p);
298 if (vmanip->m_flags[vi] & VMANIP_FLAG_DUNGEON_UNTOUCHABLE)
300 vmanip->m_data[vi] = n_cobble;
304 // Make +-Y walls (floor and ceiling)
305 for (s16 z = 0; z < roomsize.Z; z++)
306 for (s16 x = 0; x < roomsize.X; x++)
309 v3s16 p = roomplace + v3s16(x, 0, z);
310 if (vmanip->m_area.contains(p) == false)
312 u32 vi = vmanip->m_area.index(p);
313 if (vmanip->m_flags[vi] & VMANIP_FLAG_DUNGEON_UNTOUCHABLE)
315 vmanip->m_data[vi] = n_cobble;
318 v3s16 p = roomplace + v3s16(x,roomsize. Y - 1, z);
319 if (vmanip->m_area.contains(p) == false)
321 u32 vi = vmanip->m_area.index(p);
322 if (vmanip->m_flags[vi] & VMANIP_FLAG_DUNGEON_UNTOUCHABLE)
324 vmanip->m_data[vi] = n_cobble;
329 for (s16 z = 1; z < roomsize.Z - 1; z++)
330 for (s16 y = 1; y < roomsize.Y - 1; y++)
331 for (s16 x = 1; x < roomsize.X - 1; x++)
333 v3s16 p = roomplace + v3s16(x, y, z);
334 if (vmanip->m_area.contains(p) == false)
336 u32 vi = vmanip->m_area.index(p);
337 vmanip->m_flags[vi] |= VMANIP_FLAG_DUNGEON_UNTOUCHABLE;
338 vmanip->m_data[vi] = n_air;
343 void DungeonGen::makeFill(v3s16 place, v3s16 size,
344 u8 avoid_flags, MapNode n, u8 or_flags)
346 for (s16 z = 0; z < size.Z; z++)
347 for (s16 y = 0; y < size.Y; y++)
348 for (s16 x = 0; x < size.X; x++)
350 v3s16 p = place + v3s16(x, y, z);
351 if (vmanip->m_area.contains(p) == false)
353 u32 vi = vmanip->m_area.index(p);
354 if (vmanip->m_flags[vi] & avoid_flags)
356 vmanip->m_flags[vi] |= or_flags;
357 vmanip->m_data[vi] = n;
362 void DungeonGen::makeHole(v3s16 place)
364 makeFill(place, v3s16(1, 2, 1), 0, MapNode(CONTENT_AIR),
365 VMANIP_FLAG_DUNGEON_INSIDE);
369 void DungeonGen::makeDoor(v3s16 doorplace, v3s16 doordir)
372 // Place torch (for testing)
373 //vmanip->m_data[vmanip->m_area.index(doorplace)] = MapNode(cid_torch);
377 void DungeonGen::makeCorridor(v3s16 doorplace,
378 v3s16 doordir, v3s16 &result_place, v3s16 &result_dir)
381 v3s16 p0 = doorplace;
384 /*if (random.next() % 2)
385 length = random.range(1, 13);
387 length = random.range(1, 6);*/
388 length = random.range(1, 13);
389 u32 partlength = random.range(1, 13);
393 if (random.next() % 2 == 0 && partlength >= 3)
394 make_stairs = random.next() % 2 ? 1 : -1;
396 for (u32 i = 0; i < length; i++) {
401 if (vmanip->m_area.contains(p) == true &&
402 vmanip->m_area.contains(p + v3s16(0, 1, 0)) == true) {
404 makeFill(p + v3s16(-1, -1, -1), v3s16(3, 5, 3),
405 VMANIP_FLAG_DUNGEON_UNTOUCHABLE, MapNode(cid_cobble), 0);
409 // TODO: fix stairs code so it works 100% (quite difficult)
411 // exclude stairs from the bottom step
412 if (((make_stairs == 1) && i != 0) ||
413 ((make_stairs == -1) && i != length - 1)) {
414 // rotate face 180 deg if making stairs backwards
415 int facedir = dir_to_facedir(dir * make_stairs);
417 u32 vi = vmanip->m_area.index(p.X - dir.X, p.Y - 1, p.Z - dir.Z);
418 if (vmanip->m_data[vi].getContent() == cid_cobble)
419 vmanip->m_data[vi] = MapNode(cid_cobblestair, 0, facedir);
421 vi = vmanip->m_area.index(p.X, p.Y, p.Z);
422 if (vmanip->m_data[vi].getContent() == cid_cobble)
423 vmanip->m_data[vi] = MapNode(cid_cobblestair, 0, facedir);
426 makeFill(p + v3s16(-1, -1, -1), v3s16(3, 4, 3),
427 VMANIP_FLAG_DUNGEON_UNTOUCHABLE, MapNode(cid_cobble), 0);
433 // Can't go here, turn away
434 dir = turn_xz(dir, random.range(0, 1));
435 make_stairs = -make_stairs;
437 partlength = random.range(1, length);
442 if (partcount >= partlength) {
445 dir = random_turn(random, dir);
447 partlength = random.range(1,length);
450 if (random.next() % 2 == 0 && partlength >= 3)
451 make_stairs = random.next() % 2 ? 1 : -1;
459 bool DungeonGen::findPlaceForDoor(v3s16 &result_place, v3s16 &result_dir)
461 for (u32 i = 0; i < 100; i++)
463 v3s16 p = m_pos + m_dir;
464 v3s16 p1 = p + v3s16(0, 1, 0);
465 if (vmanip->m_area.contains(p) == false
466 || vmanip->m_area.contains(p1) == false
472 if (vmanip->getNodeNoExNoEmerge(p).getContent() == cid_cobble
473 && vmanip->getNodeNoExNoEmerge(p1).getContent() == cid_cobble)
475 // Found wall, this is a good place!
478 // Randomize next direction
483 Determine where to move next
485 // Jump one up if the actual space is there
486 if (vmanip->getNodeNoExNoEmerge(p+v3s16(0,0,0)).getContent() == cid_cobble
487 && vmanip->getNodeNoExNoEmerge(p+v3s16(0,1,0)).getContent() == CONTENT_AIR
488 && vmanip->getNodeNoExNoEmerge(p+v3s16(0,2,0)).getContent() == CONTENT_AIR)
490 // Jump one down if the actual space is there
491 if (vmanip->getNodeNoExNoEmerge(p+v3s16(0,1,0)).getContent() == cid_cobble
492 && vmanip->getNodeNoExNoEmerge(p+v3s16(0,0,0)).getContent() == CONTENT_AIR
493 && vmanip->getNodeNoExNoEmerge(p+v3s16(0,-1,0)).getContent() == CONTENT_AIR)
495 // Check if walking is now possible
496 if (vmanip->getNodeNoExNoEmerge(p).getContent() != CONTENT_AIR
497 || vmanip->getNodeNoExNoEmerge(p+v3s16(0,1,0)).getContent() != CONTENT_AIR)
499 // Cannot continue walking here
510 bool DungeonGen::findPlaceForRoomDoor(v3s16 roomsize, v3s16 &result_doorplace,
511 v3s16 &result_doordir, v3s16 &result_roomplace)
513 for (s16 trycount = 0; trycount < 30; trycount++)
517 bool r = findPlaceForDoor(doorplace, doordir);
521 // X east, Z north, Y up
523 if (doordir == v3s16(1, 0, 0)) // X+
524 roomplace = doorplace +
525 v3s16(0, -1, random.range(-roomsize.Z + 2, -2));
526 if (doordir == v3s16(-1, 0, 0)) // X-
527 roomplace = doorplace +
528 v3s16(-roomsize.X + 1, -1, random.range(-roomsize.Z + 2, -2));
529 if (doordir == v3s16(0, 0, 1)) // Z+
530 roomplace = doorplace +
531 v3s16(random.range(-roomsize.X + 2, -2), -1, 0);
532 if (doordir == v3s16(0, 0, -1)) // Z-
533 roomplace = doorplace +
534 v3s16(random.range(-roomsize.X + 2, -2), -1, -roomsize.Z + 1);
537 if (doordir == v3s16(1, 0, 0)) // X+
538 roomplace = doorplace + v3s16(0, -1, -roomsize.Z / 2);
539 if (doordir == v3s16(-1, 0, 0)) // X-
540 roomplace = doorplace + v3s16(-roomsize.X+1,-1,-roomsize.Z / 2);
541 if (doordir == v3s16(0, 0, 1)) // Z+
542 roomplace = doorplace + v3s16(-roomsize.X / 2, -1, 0);
543 if (doordir == v3s16(0, 0, -1)) // Z-
544 roomplace = doorplace + v3s16(-roomsize.X / 2, -1, -roomsize.Z + 1);
549 for (s16 z = 1; z < roomsize.Z - 1; z++)
550 for (s16 y = 1; y < roomsize.Y - 1; y++)
551 for (s16 x = 1; x < roomsize.X - 1; x++)
553 v3s16 p = roomplace + v3s16(x, y, z);
554 if (vmanip->m_area.contains(p) == false)
559 if (vmanip->m_flags[vmanip->m_area.index(p)]
560 & VMANIP_FLAG_DUNGEON_INSIDE)
571 result_doorplace = doorplace;
572 result_doordir = doordir;
573 result_roomplace = roomplace;
580 v3s16 rand_ortho_dir(PseudoRandom &random)
582 if (random.next() % 2 == 0)
583 return random.next() % 2 ? v3s16(-1, 0, 0) : v3s16(1, 0, 0);
585 return random.next() % 2 ? v3s16(0, 0, -1) : v3s16(0, 0, 1);
589 v3s16 turn_xz(v3s16 olddir, int t)
610 v3s16 random_turn(PseudoRandom &random, v3s16 olddir)
612 int turn = random.range(0, 2);
621 dir = turn_xz(olddir, 0);
624 dir = turn_xz(olddir, 1);
629 int dir_to_facedir(v3s16 d) {
630 if (abs(d.X) > abs(d.Z))
631 return d.X < 0 ? 3 : 1;
633 return d.Z < 0 ? 2 : 0;