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 //#define DGEN_USE_TORCHES
34 NoiseParams nparams_dungeon_rarity =
35 {0.0, 1.0, v3f(500.0, 500.0, 500.0), 0, 2, 0.8};
36 NoiseParams nparams_dungeon_wetness =
37 {0.0, 1.0, v3f(40.0, 40.0, 40.0), 32474, 4, 1.1};
38 NoiseParams nparams_dungeon_density =
39 {0.0, 1.0, v3f(2.5, 2.5, 2.5), 0, 2, 1.4};
42 ///////////////////////////////////////////////////////////////////////////////
45 DungeonGen::DungeonGen(INodeDefManager *ndef, u64 seed, s16 waterlevel,
46 DungeonParams *dparams) {
49 this->water_level = waterlevel;
51 #ifdef DGEN_USE_TORCHES
52 c_torch = ndef->getId("default:torch");
56 memcpy(&dp, dparams, sizeof(dp));
58 dp.c_water = ndef->getId("mapgen_water_source");
59 dp.c_cobble = ndef->getId("mapgen_cobble");
60 dp.c_moss = ndef->getId("mapgen_mossycobble");
61 dp.c_stair = ndef->getId("mapgen_stair_cobble");
63 dp.diagonal_dirs = false;
65 dp.holesize = v3s16(1, 2, 1);
66 dp.roomsize = v3s16(0,0,0);
68 dp.np_rarity = nparams_dungeon_rarity;
69 dp.np_wetness = nparams_dungeon_wetness;
70 dp.np_density = nparams_dungeon_density;
75 void DungeonGen::generate(ManualMapVoxelManipulator *vm, u32 bseed,
76 v3s16 nmin, v3s16 nmax) {
77 //TimeTaker t("gen dungeons");
78 int approx_groundlevel = 10 + water_level;
80 if ((nmin.Y + nmax.Y) / 2 >= approx_groundlevel ||
81 NoisePerlin3D(&dp.np_rarity, nmin.X, nmin.Y, nmin.Z, mapseed) < 0.2)
85 this->blockseed = bseed;
86 random.seed(bseed + 2);
88 // Dungeon generator doesn't modify places which have this set
89 vmanip->clearFlag(VMANIP_FLAG_DUNGEON_INSIDE | VMANIP_FLAG_DUNGEON_PRESERVE);
91 // Set all air and water to be untouchable to make dungeons open
92 // to caves and open air
93 for (s16 z = nmin.Z; z <= nmax.Z; z++) {
94 for (s16 y = nmin.Y; y <= nmax.Y; y++) {
95 u32 i = vmanip->m_area.index(nmin.X, y, z);
96 for (s16 x = nmin.X; x <= nmax.X; x++) {
97 content_t c = vmanip->m_data[i].getContent();
98 if (c == CONTENT_AIR || c == dp.c_water)
99 vmanip->m_flags[i] |= VMANIP_FLAG_DUNGEON_PRESERVE;
106 makeDungeon(v3s16(1,1,1) * MAP_BLOCKSIZE);
108 // Convert some cobble to mossy cobble
109 if (dp.mossratio != 0.0) {
110 for (s16 z = nmin.Z; z <= nmax.Z; z++)
111 for (s16 y = nmin.Y; y <= nmax.Y; y++) {
112 u32 i = vmanip->m_area.index(nmin.X, y, z);
113 for (s16 x = nmin.X; x <= nmax.X; x++) {
114 if (vmanip->m_data[i].getContent() == dp.c_cobble) {
115 float wetness = NoisePerlin3D(&dp.np_wetness, x, y, z, mapseed);
116 float density = NoisePerlin3D(&dp.np_density, x, y, z, blockseed);
117 if (density < wetness / dp.mossratio)
118 vmanip->m_data[i].setContent(dp.c_moss);
125 //printf("== gen dungeons: %dms\n", t.stop());
129 void DungeonGen::makeDungeon(v3s16 start_padding)
131 v3s16 areasize = vmanip->m_area.getExtent();
136 Find place for first room
139 for (u32 i = 0; i < 100; i++)
141 bool is_large_room = ((random.next() & 3) == 1);
142 roomsize = is_large_room ?
143 v3s16(random.range(8, 16),random.range(8, 16),random.range(8, 16)) :
144 v3s16(random.range(4, 8),random.range(4, 6),random.range(4, 8));
145 roomsize += dp.roomsize;
147 // start_padding is used to disallow starting the generation of
148 // a dungeon in a neighboring generation chunk
149 roomplace = vmanip->m_area.MinEdge + start_padding + v3s16(
150 random.range(0,areasize.X-roomsize.X-1-start_padding.X),
151 random.range(0,areasize.Y-roomsize.Y-1-start_padding.Y),
152 random.range(0,areasize.Z-roomsize.Z-1-start_padding.Z));
155 Check that we're not putting the room to an unknown place,
156 otherwise it might end up floating in the air
159 for (s16 z = 1; z < roomsize.Z - 1; z++)
160 for (s16 y = 1; y < roomsize.Y - 1; y++)
161 for (s16 x = 1; x < roomsize.X - 1; x++)
163 v3s16 p = roomplace + v3s16(x, y, z);
164 u32 vi = vmanip->m_area.index(p);
165 if (vmanip->m_flags[vi] & VMANIP_FLAG_DUNGEON_INSIDE)
170 if (vmanip->m_data[vi].getContent() == CONTENT_IGNORE)
184 Stores the center position of the last room made, so that
185 a new corridor can be started from the last room instead of
186 the new room, if chosen so.
188 v3s16 last_room_center = roomplace + v3s16(roomsize.X / 2, 1, roomsize.Z / 2);
190 u32 room_count = random.range(2, 16);
191 for (u32 i = 0; i < room_count; i++)
193 // Make a room to the determined place
194 makeRoom(roomsize, roomplace);
196 v3s16 room_center = roomplace + v3s16(roomsize.X / 2, 1, roomsize.Z / 2);
198 #ifdef DGEN_USE_TORCHES
199 // Place torch at room center (for testing)
200 vmanip->m_data[vmanip->m_area.index(room_center)] = MapNode(c_torch);
204 if (i == room_count - 1)
207 // Determine walker start position
209 bool start_in_last_room = (random.range(0, 2) != 0);
211 v3s16 walker_start_place;
213 if(start_in_last_room)
215 walker_start_place = last_room_center;
219 walker_start_place = room_center;
220 // Store center of current room as the last one
221 last_room_center = room_center;
224 // Create walker and find a place for a door
228 m_pos = walker_start_place;
229 bool r = findPlaceForDoor(doorplace, doordir);
233 if (random.range(0,1) == 0)
235 makeDoor(doorplace, doordir);
237 // Don't actually make a door
238 doorplace -= doordir;
240 // Make a random corridor starting from the door
242 v3s16 corridor_end_dir;
243 makeCorridor(doorplace, doordir, corridor_end, corridor_end_dir);
245 // Find a place for a random sized room
246 roomsize = v3s16(random.range(4,8),random.range(4,6),random.range(4,8));
247 roomsize += dp.roomsize;
249 m_pos = corridor_end;
250 m_dir = corridor_end_dir;
251 r = findPlaceForRoomDoor(roomsize, doorplace, doordir, roomplace);
255 if (random.range(0,1) == 0)
257 makeDoor(doorplace, doordir);
259 // Don't actually make a door
260 roomplace -= doordir;
266 void DungeonGen::makeRoom(v3s16 roomsize, v3s16 roomplace)
268 MapNode n_cobble(dp.c_cobble);
269 MapNode n_air(CONTENT_AIR);
272 for (s16 z = 0; z < roomsize.Z; z++)
273 for (s16 y = 0; y < roomsize.Y; y++)
276 v3s16 p = roomplace + v3s16(0, y, z);
277 if (vmanip->m_area.contains(p) == false)
279 u32 vi = vmanip->m_area.index(p);
280 if (vmanip->m_flags[vi] & VMANIP_FLAG_DUNGEON_UNTOUCHABLE)
282 vmanip->m_data[vi] = n_cobble;
285 v3s16 p = roomplace + v3s16(roomsize.X - 1, y, z);
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;
296 for (s16 x = 0; x < roomsize.X; x++)
297 for (s16 y = 0; y < roomsize.Y; y++)
300 v3s16 p = roomplace + v3s16(x, y, 0);
301 if (vmanip->m_area.contains(p) == false)
303 u32 vi = vmanip->m_area.index(p);
304 if (vmanip->m_flags[vi] & VMANIP_FLAG_DUNGEON_UNTOUCHABLE)
306 vmanip->m_data[vi] = n_cobble;
309 v3s16 p = roomplace + v3s16(x, y, roomsize.Z - 1);
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;
319 // Make +-Y walls (floor and ceiling)
320 for (s16 z = 0; z < roomsize.Z; z++)
321 for (s16 x = 0; x < roomsize.X; x++)
324 v3s16 p = roomplace + v3s16(x, 0, z);
325 if (vmanip->m_area.contains(p) == false)
327 u32 vi = vmanip->m_area.index(p);
328 if (vmanip->m_flags[vi] & VMANIP_FLAG_DUNGEON_UNTOUCHABLE)
330 vmanip->m_data[vi] = n_cobble;
333 v3s16 p = roomplace + v3s16(x,roomsize. Y - 1, z);
334 if (vmanip->m_area.contains(p) == false)
336 u32 vi = vmanip->m_area.index(p);
337 if (vmanip->m_flags[vi] & VMANIP_FLAG_DUNGEON_UNTOUCHABLE)
339 vmanip->m_data[vi] = n_cobble;
344 for (s16 z = 1; z < roomsize.Z - 1; z++)
345 for (s16 y = 1; y < roomsize.Y - 1; y++)
346 for (s16 x = 1; x < roomsize.X - 1; x++)
348 v3s16 p = roomplace + v3s16(x, y, z);
349 if (vmanip->m_area.contains(p) == false)
351 u32 vi = vmanip->m_area.index(p);
352 vmanip->m_flags[vi] |= VMANIP_FLAG_DUNGEON_UNTOUCHABLE;
353 vmanip->m_data[vi] = n_air;
358 void DungeonGen::makeFill(v3s16 place, v3s16 size,
359 u8 avoid_flags, MapNode n, u8 or_flags)
361 for (s16 z = 0; z < size.Z; z++)
362 for (s16 y = 0; y < size.Y; y++)
363 for (s16 x = 0; x < size.X; x++)
365 v3s16 p = place + v3s16(x, y, z);
366 if (vmanip->m_area.contains(p) == false)
368 u32 vi = vmanip->m_area.index(p);
369 if (vmanip->m_flags[vi] & avoid_flags)
371 vmanip->m_flags[vi] |= or_flags;
372 vmanip->m_data[vi] = n;
377 void DungeonGen::makeHole(v3s16 place)
379 makeFill(place, dp.holesize, 0,
380 MapNode(CONTENT_AIR), VMANIP_FLAG_DUNGEON_INSIDE);
384 void DungeonGen::makeDoor(v3s16 doorplace, v3s16 doordir)
388 #ifdef DGEN_USE_TORCHES
389 // Place torch (for testing)
390 vmanip->m_data[vmanip->m_area.index(doorplace)] = MapNode(c_torch);
395 void DungeonGen::makeCorridor(v3s16 doorplace,
396 v3s16 doordir, v3s16 &result_place, v3s16 &result_dir)
399 v3s16 p0 = doorplace;
402 /*if (random.next() % 2)
403 length = random.range(1, 13);
405 length = random.range(1, 6);*/
406 length = random.range(1, 13);
407 u32 partlength = random.range(1, 13);
411 if (random.next() % 2 == 0 && partlength >= 3)
412 make_stairs = random.next() % 2 ? 1 : -1;
414 for (u32 i = 0; i < length; i++) {
419 if (vmanip->m_area.contains(p) == true &&
420 vmanip->m_area.contains(p + v3s16(0, 1, 0)) == true) {
422 makeFill(p + v3s16(-1, -1, -1), dp.holesize + v3s16(2, 3, 2),
423 VMANIP_FLAG_DUNGEON_UNTOUCHABLE, MapNode(dp.c_cobble), 0);
427 // TODO: fix stairs code so it works 100% (quite difficult)
429 // exclude stairs from the bottom step
430 // exclude stairs from diagonal steps
431 if (((dir.X ^ dir.Z) & 1) &&
432 (((make_stairs == 1) && i != 0) ||
433 ((make_stairs == -1) && i != length - 1))) {
434 // rotate face 180 deg if making stairs backwards
435 int facedir = dir_to_facedir(dir * make_stairs);
437 u32 vi = vmanip->m_area.index(p.X - dir.X, p.Y - 1, p.Z - dir.Z);
438 if (vmanip->m_data[vi].getContent() == dp.c_cobble)
439 vmanip->m_data[vi] = MapNode(dp.c_stair, 0, facedir);
441 vi = vmanip->m_area.index(p.X, p.Y, p.Z);
442 if (vmanip->m_data[vi].getContent() == dp.c_cobble)
443 vmanip->m_data[vi] = MapNode(dp.c_stair, 0, facedir);
446 makeFill(p + v3s16(-1, -1, -1), dp.holesize + v3s16(2, 2, 2),
447 VMANIP_FLAG_DUNGEON_UNTOUCHABLE, MapNode(dp.c_cobble), 0);
453 // Can't go here, turn away
454 dir = turn_xz(dir, random.range(0, 1));
455 make_stairs = -make_stairs;
457 partlength = random.range(1, length);
462 if (partcount >= partlength) {
465 dir = random_turn(random, dir);
467 partlength = random.range(1,length);
470 if (random.next() % 2 == 0 && partlength >= 3)
471 make_stairs = random.next() % 2 ? 1 : -1;
479 bool DungeonGen::findPlaceForDoor(v3s16 &result_place, v3s16 &result_dir)
481 for (u32 i = 0; i < 100; i++)
483 v3s16 p = m_pos + m_dir;
484 v3s16 p1 = p + v3s16(0, 1, 0);
485 if (vmanip->m_area.contains(p) == false
486 || vmanip->m_area.contains(p1) == false
492 if (vmanip->getNodeNoExNoEmerge(p).getContent() == dp.c_cobble
493 && vmanip->getNodeNoExNoEmerge(p1).getContent() == dp.c_cobble)
495 // Found wall, this is a good place!
498 // Randomize next direction
503 Determine where to move next
505 // Jump one up if the actual space is there
506 if (vmanip->getNodeNoExNoEmerge(p+v3s16(0,0,0)).getContent() == dp.c_cobble
507 && vmanip->getNodeNoExNoEmerge(p+v3s16(0,1,0)).getContent() == CONTENT_AIR
508 && vmanip->getNodeNoExNoEmerge(p+v3s16(0,2,0)).getContent() == CONTENT_AIR)
510 // Jump one down if the actual space is there
511 if (vmanip->getNodeNoExNoEmerge(p+v3s16(0,1,0)).getContent() == dp.c_cobble
512 && vmanip->getNodeNoExNoEmerge(p+v3s16(0,0,0)).getContent() == CONTENT_AIR
513 && vmanip->getNodeNoExNoEmerge(p+v3s16(0,-1,0)).getContent() == CONTENT_AIR)
515 // Check if walking is now possible
516 if (vmanip->getNodeNoExNoEmerge(p).getContent() != CONTENT_AIR
517 || vmanip->getNodeNoExNoEmerge(p+v3s16(0,1,0)).getContent() != CONTENT_AIR)
519 // Cannot continue walking here
530 bool DungeonGen::findPlaceForRoomDoor(v3s16 roomsize, v3s16 &result_doorplace,
531 v3s16 &result_doordir, v3s16 &result_roomplace)
533 for (s16 trycount = 0; trycount < 30; trycount++)
537 bool r = findPlaceForDoor(doorplace, doordir);
541 // X east, Z north, Y up
543 if (doordir == v3s16(1, 0, 0)) // X+
544 roomplace = doorplace +
545 v3s16(0, -1, random.range(-roomsize.Z + 2, -2));
546 if (doordir == v3s16(-1, 0, 0)) // X-
547 roomplace = doorplace +
548 v3s16(-roomsize.X + 1, -1, random.range(-roomsize.Z + 2, -2));
549 if (doordir == v3s16(0, 0, 1)) // Z+
550 roomplace = doorplace +
551 v3s16(random.range(-roomsize.X + 2, -2), -1, 0);
552 if (doordir == v3s16(0, 0, -1)) // Z-
553 roomplace = doorplace +
554 v3s16(random.range(-roomsize.X + 2, -2), -1, -roomsize.Z + 1);
557 if (doordir == v3s16(1, 0, 0)) // X+
558 roomplace = doorplace + v3s16(0, -1, -roomsize.Z / 2);
559 if (doordir == v3s16(-1, 0, 0)) // X-
560 roomplace = doorplace + v3s16(-roomsize.X+1,-1,-roomsize.Z / 2);
561 if (doordir == v3s16(0, 0, 1)) // Z+
562 roomplace = doorplace + v3s16(-roomsize.X / 2, -1, 0);
563 if (doordir == v3s16(0, 0, -1)) // Z-
564 roomplace = doorplace + v3s16(-roomsize.X / 2, -1, -roomsize.Z + 1);
569 for (s16 z = 1; z < roomsize.Z - 1; z++)
570 for (s16 y = 1; y < roomsize.Y - 1; y++)
571 for (s16 x = 1; x < roomsize.X - 1; x++)
573 v3s16 p = roomplace + v3s16(x, y, z);
574 if (vmanip->m_area.contains(p) == false)
579 if (vmanip->m_flags[vmanip->m_area.index(p)]
580 & VMANIP_FLAG_DUNGEON_INSIDE)
591 result_doorplace = doorplace;
592 result_doordir = doordir;
593 result_roomplace = roomplace;
600 v3s16 rand_ortho_dir(PseudoRandom &random, bool diagonal_dirs)
602 // Make diagonal directions somewhat rare
603 if (diagonal_dirs && (random.next() % 4 == 0)) {
609 dir = v3s16(random.next() % 3 - 1, 0, random.next() % 3 - 1);
610 } while ((dir.X == 0 && dir.Z == 0) && trycount < 10);
614 if (random.next() % 2 == 0)
615 return random.next() % 2 ? v3s16(-1, 0, 0) : v3s16(1, 0, 0);
617 return random.next() % 2 ? v3s16(0, 0, -1) : v3s16(0, 0, 1);
622 v3s16 turn_xz(v3s16 olddir, int t)
643 v3s16 random_turn(PseudoRandom &random, v3s16 olddir)
645 int turn = random.range(0, 2);
654 dir = turn_xz(olddir, 0);
657 dir = turn_xz(olddir, 1);
662 int dir_to_facedir(v3s16 d) {
663 if (abs(d.X) > abs(d.Z))
664 return d.X < 0 ? 3 : 1;
666 return d.Z < 0 ? 2 : 0;