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"
31 //#define DGEN_USE_TORCHES
33 NoiseParams nparams_dungeon_rarity(0.0, 1.0, v3f(500.0, 500.0, 500.0), 0, 2, 0.8, 2.0);
34 NoiseParams nparams_dungeon_wetness(0.0, 1.0, v3f(40.0, 40.0, 40.0), 32474, 4, 1.1, 2.0);
35 NoiseParams nparams_dungeon_density(0.0, 1.0, v3f(2.5, 2.5, 2.5), 0, 2, 1.4, 2.0);
38 ///////////////////////////////////////////////////////////////////////////////
41 DungeonGen::DungeonGen(Mapgen *mapgen, DungeonParams *dparams)
44 this->vm = mapgen->vm;
46 #ifdef DGEN_USE_TORCHES
47 c_torch = ndef->getId("default:torch");
51 memcpy(&dp, dparams, sizeof(dp));
53 dp.c_water = mg->ndef->getId("mapgen_water_source");
54 dp.c_cobble = mg->ndef->getId("mapgen_cobble");
55 dp.c_moss = mg->ndef->getId("mapgen_mossycobble");
56 dp.c_stair = mg->ndef->getId("mapgen_stair_cobble");
58 dp.diagonal_dirs = false;
60 dp.holesize = v3s16(1, 2, 1);
61 dp.roomsize = v3s16(0, 0, 0);
62 dp.notifytype = GENNOTIFY_DUNGEON;
64 dp.np_rarity = nparams_dungeon_rarity;
65 dp.np_wetness = nparams_dungeon_wetness;
66 dp.np_density = nparams_dungeon_density;
71 void DungeonGen::generate(u32 bseed, v3s16 nmin, v3s16 nmax)
73 //TimeTaker t("gen dungeons");
74 if (NoisePerlin3D(&dp.np_rarity, nmin.X, nmin.Y, nmin.Z, mg->seed) < 0.2)
77 this->blockseed = bseed;
78 random.seed(bseed + 2);
80 // Dungeon generator doesn't modify places which have this set
81 vm->clearFlag(VMANIP_FLAG_DUNGEON_INSIDE | VMANIP_FLAG_DUNGEON_PRESERVE);
83 // Set all air and water to be untouchable
84 // to make dungeons open to caves and open air
85 for (s16 z = nmin.Z; z <= nmax.Z; z++) {
86 for (s16 y = nmin.Y; y <= nmax.Y; y++) {
87 u32 i = vm->m_area.index(nmin.X, y, z);
88 for (s16 x = nmin.X; x <= nmax.X; x++) {
89 content_t c = vm->m_data[i].getContent();
90 if (c == CONTENT_AIR || c == dp.c_water)
91 vm->m_flags[i] |= VMANIP_FLAG_DUNGEON_PRESERVE;
98 makeDungeon(v3s16(1, 1, 1) * MAP_BLOCKSIZE);
100 // Convert some cobble to mossy cobble
101 if (dp.mossratio != 0.0) {
102 for (s16 z = nmin.Z; z <= nmax.Z; z++)
103 for (s16 y = nmin.Y; y <= nmax.Y; y++) {
104 u32 i = vm->m_area.index(nmin.X, y, z);
105 for (s16 x = nmin.X; x <= nmax.X; x++) {
106 if (vm->m_data[i].getContent() == dp.c_cobble) {
107 float wetness = NoisePerlin3D(&dp.np_wetness, x, y, z, mg->seed);
108 float density = NoisePerlin3D(&dp.np_density, x, y, z, blockseed);
109 if (density < wetness / dp.mossratio)
110 vm->m_data[i].setContent(dp.c_moss);
117 //printf("== gen dungeons: %dms\n", t.stop());
121 void DungeonGen::makeDungeon(v3s16 start_padding)
123 v3s16 areasize = vm->m_area.getExtent();
128 Find place for first room
131 for (u32 i = 0; i < 100 && !fits; i++) {
132 bool is_large_room = ((random.next() & 3) == 1);
133 roomsize = is_large_room ?
134 v3s16(random.range(8, 16), random.range(8, 16), random.range(8, 16)) :
135 v3s16(random.range(4, 8), random.range(4, 6), random.range(4, 8));
136 roomsize += dp.roomsize;
138 // start_padding is used to disallow starting the generation of
139 // a dungeon in a neighboring generation chunk
140 roomplace = vm->m_area.MinEdge + start_padding + v3s16(
141 random.range(0, areasize.X - roomsize.X - start_padding.X),
142 random.range(0, areasize.Y - roomsize.Y - start_padding.Y),
143 random.range(0, areasize.Z - roomsize.Z - start_padding.Z));
146 Check that we're not putting the room to an unknown place,
147 otherwise it might end up floating in the air
150 for (s16 z = 0; z < roomsize.Z; z++)
151 for (s16 y = 0; y < roomsize.Y; y++)
152 for (s16 x = 0; x < roomsize.X; x++) {
153 v3s16 p = roomplace + v3s16(x, y, z);
154 u32 vi = vm->m_area.index(p);
155 if ((vm->m_flags[vi] & VMANIP_FLAG_DUNGEON_UNTOUCHABLE) ||
156 vm->m_data[vi].getContent() == CONTENT_IGNORE) {
167 Stores the center position of the last room made, so that
168 a new corridor can be started from the last room instead of
169 the new room, if chosen so.
171 v3s16 last_room_center = roomplace + v3s16(roomsize.X / 2, 1, roomsize.Z / 2);
173 u32 room_count = random.range(2, 16);
174 for (u32 i = 0; i < room_count; i++) {
175 // Make a room to the determined place
176 makeRoom(roomsize, roomplace);
178 v3s16 room_center = roomplace + v3s16(roomsize.X / 2, 1, roomsize.Z / 2);
179 mg->gennotify.addEvent(dp.notifytype, room_center);
181 #ifdef DGEN_USE_TORCHES
182 // Place torch at room center (for testing)
183 vm->m_data[vm->m_area.index(room_center)] = MapNode(c_torch);
187 if (i == room_count - 1)
190 // Determine walker start position
192 bool start_in_last_room = (random.range(0, 2) != 0);
194 v3s16 walker_start_place;
196 if (start_in_last_room) {
197 walker_start_place = last_room_center;
199 walker_start_place = room_center;
200 // Store center of current room as the last one
201 last_room_center = room_center;
204 // Create walker and find a place for a door
208 m_pos = walker_start_place;
209 if (!findPlaceForDoor(doorplace, doordir))
212 if (random.range(0, 1) == 0)
214 makeDoor(doorplace, doordir);
216 // Don't actually make a door
217 doorplace -= doordir;
219 // Make a random corridor starting from the door
221 v3s16 corridor_end_dir;
222 makeCorridor(doorplace, doordir, corridor_end, corridor_end_dir);
224 // Find a place for a random sized room
225 roomsize = v3s16(random.range(4, 8), random.range(4, 6), random.range(4, 8));
226 roomsize += dp.roomsize;
228 m_pos = corridor_end;
229 m_dir = corridor_end_dir;
230 if (!findPlaceForRoomDoor(roomsize, doorplace, doordir, roomplace))
233 if (random.range(0, 1) == 0)
235 makeDoor(doorplace, doordir);
237 // Don't actually make a door
238 roomplace -= doordir;
244 void DungeonGen::makeRoom(v3s16 roomsize, v3s16 roomplace)
246 MapNode n_cobble(dp.c_cobble);
247 MapNode n_air(CONTENT_AIR);
250 for (s16 z = 0; z < roomsize.Z; z++)
251 for (s16 y = 0; y < roomsize.Y; y++) {
253 v3s16 p = roomplace + v3s16(0, y, z);
254 if (!vm->m_area.contains(p))
256 u32 vi = vm->m_area.index(p);
257 if (vm->m_flags[vi] & VMANIP_FLAG_DUNGEON_UNTOUCHABLE)
259 vm->m_data[vi] = n_cobble;
262 v3s16 p = roomplace + v3s16(roomsize.X - 1, y, z);
263 if (!vm->m_area.contains(p))
265 u32 vi = vm->m_area.index(p);
266 if (vm->m_flags[vi] & VMANIP_FLAG_DUNGEON_UNTOUCHABLE)
268 vm->m_data[vi] = n_cobble;
273 for (s16 x = 0; x < roomsize.X; x++)
274 for (s16 y = 0; y < roomsize.Y; y++) {
276 v3s16 p = roomplace + v3s16(x, y, 0);
277 if (!vm->m_area.contains(p))
279 u32 vi = vm->m_area.index(p);
280 if (vm->m_flags[vi] & VMANIP_FLAG_DUNGEON_UNTOUCHABLE)
282 vm->m_data[vi] = n_cobble;
285 v3s16 p = roomplace + v3s16(x, y, roomsize.Z - 1);
286 if (!vm->m_area.contains(p))
288 u32 vi = vm->m_area.index(p);
289 if (vm->m_flags[vi] & VMANIP_FLAG_DUNGEON_UNTOUCHABLE)
291 vm->m_data[vi] = n_cobble;
295 // Make +-Y walls (floor and ceiling)
296 for (s16 z = 0; z < roomsize.Z; z++)
297 for (s16 x = 0; x < roomsize.X; x++) {
299 v3s16 p = roomplace + v3s16(x, 0, z);
300 if (!vm->m_area.contains(p))
302 u32 vi = vm->m_area.index(p);
303 if (vm->m_flags[vi] & VMANIP_FLAG_DUNGEON_UNTOUCHABLE)
305 vm->m_data[vi] = n_cobble;
308 v3s16 p = roomplace + v3s16(x,roomsize. Y - 1, z);
309 if (!vm->m_area.contains(p))
311 u32 vi = vm->m_area.index(p);
312 if (vm->m_flags[vi] & VMANIP_FLAG_DUNGEON_UNTOUCHABLE)
314 vm->m_data[vi] = n_cobble;
319 for (s16 z = 1; z < roomsize.Z - 1; z++)
320 for (s16 y = 1; y < roomsize.Y - 1; y++)
321 for (s16 x = 1; x < roomsize.X - 1; x++) {
322 v3s16 p = roomplace + v3s16(x, y, z);
323 if (!vm->m_area.contains(p))
325 u32 vi = vm->m_area.index(p);
326 vm->m_flags[vi] |= VMANIP_FLAG_DUNGEON_UNTOUCHABLE;
327 vm->m_data[vi] = n_air;
332 void DungeonGen::makeFill(v3s16 place, v3s16 size,
333 u8 avoid_flags, MapNode n, u8 or_flags)
335 for (s16 z = 0; z < size.Z; z++)
336 for (s16 y = 0; y < size.Y; y++)
337 for (s16 x = 0; x < size.X; x++) {
338 v3s16 p = place + v3s16(x, y, z);
339 if (!vm->m_area.contains(p))
341 u32 vi = vm->m_area.index(p);
342 if (vm->m_flags[vi] & avoid_flags)
344 vm->m_flags[vi] |= or_flags;
350 void DungeonGen::makeHole(v3s16 place)
352 makeFill(place, dp.holesize, 0, MapNode(CONTENT_AIR),
353 VMANIP_FLAG_DUNGEON_INSIDE);
357 void DungeonGen::makeDoor(v3s16 doorplace, v3s16 doordir)
361 #ifdef DGEN_USE_TORCHES
362 // Place torch (for testing)
363 vm->m_data[vm->m_area.index(doorplace)] = MapNode(c_torch);
368 void DungeonGen::makeCorridor(v3s16 doorplace, v3s16 doordir,
369 v3s16 &result_place, v3s16 &result_dir)
372 v3s16 p0 = doorplace;
375 /*if (random.next() % 2)
376 length = random.range(1, 13);
378 length = random.range(1, 6);*/
379 length = random.range(1, 13);
380 u32 partlength = random.range(1, 13);
384 if (random.next() % 2 == 0 && partlength >= 3)
385 make_stairs = random.next() % 2 ? 1 : -1;
387 for (u32 i = 0; i < length; i++) {
392 if (vm->m_area.contains(p) && vm->m_area.contains(p + v3s16(0, 1, 0))) {
394 makeFill(p + v3s16(-1, -1, -1),
395 dp.holesize + v3s16(2, 3, 2),
396 VMANIP_FLAG_DUNGEON_UNTOUCHABLE,
397 MapNode(dp.c_cobble),
402 // TODO: fix stairs code so it works 100%
405 // exclude stairs from the bottom step
406 // exclude stairs from diagonal steps
407 if (((dir.X ^ dir.Z) & 1) &&
408 (((make_stairs == 1) && i != 0) ||
409 ((make_stairs == -1) && i != length - 1))) {
410 // rotate face 180 deg if
411 // making stairs backwards
412 int facedir = dir_to_facedir(dir * make_stairs);
414 u32 vi = vm->m_area.index(p.X - dir.X, p.Y - 1, p.Z - dir.Z);
415 if (vm->m_data[vi].getContent() == dp.c_cobble)
416 vm->m_data[vi] = MapNode(dp.c_stair, 0, facedir);
418 vi = vm->m_area.index(p.X, p.Y, p.Z);
419 if (vm->m_data[vi].getContent() == dp.c_cobble)
420 vm->m_data[vi] = MapNode(dp.c_stair, 0, facedir);
423 makeFill(p + v3s16(-1, -1, -1),
424 dp.holesize + v3s16(2, 2, 2),
425 VMANIP_FLAG_DUNGEON_UNTOUCHABLE,
426 MapNode(dp.c_cobble),
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++) {
462 v3s16 p = m_pos + m_dir;
463 v3s16 p1 = p + v3s16(0, 1, 0);
464 if (!vm->m_area.contains(p) || !vm->m_area.contains(p1) || i % 4 == 0) {
468 if (vm->getNodeNoExNoEmerge(p).getContent() == dp.c_cobble &&
469 vm->getNodeNoExNoEmerge(p1).getContent() == dp.c_cobble) {
470 // Found wall, this is a good place!
473 // Randomize next direction
478 Determine where to move next
480 // Jump one up if the actual space is there
481 if (vm->getNodeNoExNoEmerge(p +
482 v3s16(0, 0, 0)).getContent() == dp.c_cobble &&
483 vm->getNodeNoExNoEmerge(p +
484 v3s16(0, 1, 0)).getContent() == CONTENT_AIR &&
485 vm->getNodeNoExNoEmerge(p +
486 v3s16(0, 2, 0)).getContent() == CONTENT_AIR)
488 // Jump one down if the actual space is there
489 if (vm->getNodeNoExNoEmerge(p +
490 v3s16(0, 1, 0)).getContent() == dp.c_cobble &&
491 vm->getNodeNoExNoEmerge(p +
492 v3s16(0, 0, 0)).getContent() == CONTENT_AIR &&
493 vm->getNodeNoExNoEmerge(p +
494 v3s16(0, -1, 0)).getContent() == CONTENT_AIR)
495 p += v3s16(0, -1, 0);
496 // Check if walking is now possible
497 if (vm->getNodeNoExNoEmerge(p).getContent() != CONTENT_AIR ||
498 vm->getNodeNoExNoEmerge(p +
499 v3s16(0, 1, 0)).getContent() != CONTENT_AIR) {
500 // Cannot continue walking here
511 bool DungeonGen::findPlaceForRoomDoor(v3s16 roomsize, v3s16 &result_doorplace,
512 v3s16 &result_doordir, v3s16 &result_roomplace)
514 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++) {
552 v3s16 p = roomplace + v3s16(x, y, z);
553 if (!vm->m_area.contains(p)) {
557 if (vm->m_flags[vm->m_area.index(p)] & VMANIP_FLAG_DUNGEON_INSIDE) {
566 result_doorplace = doorplace;
567 result_doordir = doordir;
568 result_roomplace = roomplace;
575 v3s16 rand_ortho_dir(PseudoRandom &random, bool diagonal_dirs)
577 // Make diagonal directions somewhat rare
578 if (diagonal_dirs && (random.next() % 4 == 0)) {
584 dir = v3s16(random.next() % 3 - 1, 0, random.next() % 3 - 1);
585 } while ((dir.X == 0 && dir.Z == 0) && trycount < 10);
589 if (random.next() % 2 == 0)
590 return random.next() % 2 ? v3s16(-1, 0, 0) : v3s16(1, 0, 0);
592 return random.next() % 2 ? v3s16(0, 0, -1) : v3s16(0, 0, 1);
597 v3s16 turn_xz(v3s16 olddir, int t)
615 v3s16 random_turn(PseudoRandom &random, v3s16 olddir)
617 int turn = random.range(0, 2);
624 dir = turn_xz(olddir, 0);
627 dir = turn_xz(olddir, 1);
632 int dir_to_facedir(v3s16 d)
634 if (abs(d.X) > abs(d.Z))
635 return d.X < 0 ? 3 : 1;
637 return d.Z < 0 ? 2 : 0;