mapgen: drop mapgen id from child mapgens.
[oweals/minetest.git] / src / mapgen / dungeongen.cpp
1 /*
2 Minetest
3 Copyright (C) 2010-2018 celeron55, Perttu Ahola <celeron55@gmail.com>
4 Copyright (C) 2015-2018 paramat
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20
21 #include "dungeongen.h"
22 #include <cmath>
23 #include "mapgen.h"
24 #include "voxel.h"
25 #include "noise.h"
26 #include "mapblock.h"
27 #include "mapnode.h"
28 #include "map.h"
29 #include "nodedef.h"
30 #include "settings.h"
31
32 //#define DGEN_USE_TORCHES
33
34 NoiseParams nparams_dungeon_density(0.9, 0.5, v3f(500.0, 500.0, 500.0), 0, 2, 0.8, 2.0);
35 NoiseParams nparams_dungeon_alt_wall(-0.4, 1.0, v3f(40.0, 40.0, 40.0), 32474, 6, 1.1, 2.0);
36
37
38 ///////////////////////////////////////////////////////////////////////////////
39
40
41 DungeonGen::DungeonGen(const NodeDefManager *ndef,
42         GenerateNotifier *gennotify, DungeonParams *dparams)
43 {
44         assert(ndef);
45
46         this->ndef      = ndef;
47         this->gennotify = gennotify;
48
49 #ifdef DGEN_USE_TORCHES
50         c_torch  = ndef->getId("default:torch");
51 #endif
52
53         if (dparams) {
54                 dp = *dparams;
55         } else {
56                 // Default dungeon parameters
57                 dp.seed = 0;
58
59                 dp.c_wall     = ndef->getId("mapgen_cobble");
60                 dp.c_alt_wall = ndef->getId("mapgen_mossycobble");
61                 dp.c_stair    = ndef->getId("mapgen_stair_cobble");
62
63                 dp.diagonal_dirs       = false;
64                 dp.only_in_ground      = true;
65                 dp.holesize            = v3s16(1, 2, 1);
66                 dp.corridor_len_min    = 1;
67                 dp.corridor_len_max    = 13;
68                 dp.room_size_min       = v3s16(4, 4, 4);
69                 dp.room_size_max       = v3s16(8, 6, 8);
70                 dp.room_size_large_min = v3s16(8, 8, 8);
71                 dp.room_size_large_max = v3s16(16, 16, 16);
72                 dp.rooms_min           = 2;
73                 dp.rooms_max           = 16;
74                 dp.notifytype          = GENNOTIFY_DUNGEON;
75
76                 dp.np_density  = nparams_dungeon_density;
77                 dp.np_alt_wall = nparams_dungeon_alt_wall;
78         }
79 }
80
81
82 void DungeonGen::generate(MMVManip *vm, u32 bseed, v3s16 nmin, v3s16 nmax)
83 {
84         assert(vm);
85
86         //TimeTaker t("gen dungeons");
87
88         float nval_density = NoisePerlin3D(&dp.np_density, nmin.X, nmin.Y, nmin.Z, dp.seed);
89         if (nval_density < 1.0f)
90                 return;
91
92         static const bool preserve_ignore = !g_settings->getBool("projecting_dungeons");
93
94         this->vm = vm;
95         this->blockseed = bseed;
96         random.seed(bseed + 2);
97
98         // Dungeon generator doesn't modify places which have this set
99         vm->clearFlag(VMANIP_FLAG_DUNGEON_INSIDE | VMANIP_FLAG_DUNGEON_PRESERVE);
100
101         if (dp.only_in_ground) {
102                 // Set all air and liquid drawtypes to be untouchable to make dungeons
103                 // open to air and liquids.
104                 // Optionally set ignore to be untouchable to prevent projecting dungeons.
105                 // Like randomwalk caves, preserve nodes that have 'is_ground_content = false',
106                 // to avoid dungeons that generate out beyond the edge of a mapchunk destroying
107                 // nodes added by mods in 'register_on_generated()'.
108                 for (s16 z = nmin.Z; z <= nmax.Z; z++) {
109                         for (s16 y = nmin.Y; y <= nmax.Y; y++) {
110                                 u32 i = vm->m_area.index(nmin.X, y, z);
111                                 for (s16 x = nmin.X; x <= nmax.X; x++) {
112                                         content_t c = vm->m_data[i].getContent();
113                                         NodeDrawType dtype = ndef->get(c).drawtype;
114                                         if (dtype == NDT_AIRLIKE || dtype == NDT_LIQUID ||
115                                                         (preserve_ignore && c == CONTENT_IGNORE) ||
116                                                         !ndef->get(c).is_ground_content)
117                                                 vm->m_flags[i] |= VMANIP_FLAG_DUNGEON_PRESERVE;
118                                         i++;
119                                 }
120                         }
121                 }
122         }
123
124         // Add them
125         for (u32 i = 0; i < std::floor(nval_density); i++)
126                 makeDungeon(v3s16(1, 1, 1) * MAP_BLOCKSIZE);
127
128         // Optionally convert some structure to alternative structure
129         if (dp.c_alt_wall == CONTENT_IGNORE)
130                 return;
131
132         for (s16 z = nmin.Z; z <= nmax.Z; z++)
133         for (s16 y = nmin.Y; y <= nmax.Y; y++) {
134                 u32 i = vm->m_area.index(nmin.X, y, z);
135                 for (s16 x = nmin.X; x <= nmax.X; x++) {
136                         if (vm->m_data[i].getContent() == dp.c_wall) {
137                                 if (NoisePerlin3D(&dp.np_alt_wall, x, y, z, blockseed) > 0.0f)
138                                         vm->m_data[i].setContent(dp.c_alt_wall);
139                         }
140                         i++;
141                 }
142         }
143
144         //printf("== gen dungeons: %dms\n", t.stop());
145 }
146
147
148 void DungeonGen::makeDungeon(v3s16 start_padding)
149 {
150         const v3s16 &areasize = vm->m_area.getExtent();
151         v3s16 roomsize;
152         v3s16 roomplace;
153
154         /*
155                 Find place for first room.
156                 There is a 1 in 4 chance of the first room being 'large',
157                 all other rooms are not 'large'.
158         */
159         bool fits = false;
160         for (u32 i = 0; i < 100 && !fits; i++) {
161                 bool is_large_room = ((random.next() & 3) == 1);
162                 if (is_large_room) {
163                         roomsize.Z = random.range(
164                                 dp.room_size_large_min.Z, dp.room_size_large_max.Z);
165                         roomsize.Y = random.range(
166                                 dp.room_size_large_min.Y, dp.room_size_large_max.Y);
167                         roomsize.X = random.range(
168                                 dp.room_size_large_min.X, dp.room_size_large_max.X);
169                 } else {
170                         roomsize.Z = random.range(dp.room_size_min.Z, dp.room_size_max.Z);
171                         roomsize.Y = random.range(dp.room_size_min.Y, dp.room_size_max.Y);
172                         roomsize.X = random.range(dp.room_size_min.X, dp.room_size_max.X);
173                 }
174
175                 // start_padding is used to disallow starting the generation of
176                 // a dungeon in a neighboring generation chunk
177                 roomplace = vm->m_area.MinEdge + start_padding;
178                 roomplace.Z += random.range(0, areasize.Z - roomsize.Z - start_padding.Z);
179                 roomplace.Y += random.range(0, areasize.Y - roomsize.Y - start_padding.Y);
180                 roomplace.X += random.range(0, areasize.X - roomsize.X - start_padding.X);
181
182                 /*
183                         Check that we're not putting the room to an unknown place,
184                         otherwise it might end up floating in the air
185                 */
186                 fits = true;
187                 for (s16 z = 0; z < roomsize.Z; z++)
188                 for (s16 y = 0; y < roomsize.Y; y++)
189                 for (s16 x = 0; x < roomsize.X; x++) {
190                         v3s16 p = roomplace + v3s16(x, y, z);
191                         u32 vi = vm->m_area.index(p);
192                         if ((vm->m_flags[vi] & VMANIP_FLAG_DUNGEON_UNTOUCHABLE) ||
193                                         vm->m_data[vi].getContent() == CONTENT_IGNORE) {
194                                 fits = false;
195                                 break;
196                         }
197                 }
198         }
199         // No place found
200         if (!fits)
201                 return;
202
203         /*
204                 Stores the center position of the last room made, so that
205                 a new corridor can be started from the last room instead of
206                 the new room, if chosen so.
207         */
208         v3s16 last_room_center = roomplace + v3s16(roomsize.X / 2, 1, roomsize.Z / 2);
209
210         u32 room_count = random.range(dp.rooms_min, dp.rooms_max);
211         for (u32 i = 0; i < room_count; i++) {
212                 // Make a room to the determined place
213                 makeRoom(roomsize, roomplace);
214
215                 v3s16 room_center = roomplace + v3s16(roomsize.X / 2, 1, roomsize.Z / 2);
216                 if (gennotify)
217                         gennotify->addEvent(dp.notifytype, room_center);
218
219 #ifdef DGEN_USE_TORCHES
220                 // Place torch at room center (for testing)
221                 vm->m_data[vm->m_area.index(room_center)] = MapNode(c_torch);
222 #endif
223
224                 // Quit if last room
225                 if (i == room_count - 1)
226                         break;
227
228                 // Determine walker start position
229
230                 bool start_in_last_room = (random.range(0, 2) != 0);
231
232                 v3s16 walker_start_place;
233
234                 if (start_in_last_room) {
235                         walker_start_place = last_room_center;
236                 } else {
237                         walker_start_place = room_center;
238                         // Store center of current room as the last one
239                         last_room_center = room_center;
240                 }
241
242                 // Create walker and find a place for a door
243                 v3s16 doorplace;
244                 v3s16 doordir;
245
246                 m_pos = walker_start_place;
247                 if (!findPlaceForDoor(doorplace, doordir))
248                         return;
249
250                 if (random.range(0, 1) == 0)
251                         // Make the door
252                         makeDoor(doorplace, doordir);
253                 else
254                         // Don't actually make a door
255                         doorplace -= doordir;
256
257                 // Make a random corridor starting from the door
258                 v3s16 corridor_end;
259                 v3s16 corridor_end_dir;
260                 makeCorridor(doorplace, doordir, corridor_end, corridor_end_dir);
261
262                 // Find a place for a random sized room
263                 roomsize.Z = random.range(dp.room_size_min.Z, dp.room_size_max.Z);
264                 roomsize.Y = random.range(dp.room_size_min.Y, dp.room_size_max.Y);
265                 roomsize.X = random.range(dp.room_size_min.X, dp.room_size_max.X);
266
267                 m_pos = corridor_end;
268                 m_dir = corridor_end_dir;
269                 if (!findPlaceForRoomDoor(roomsize, doorplace, doordir, roomplace))
270                         return;
271
272                 if (random.range(0, 1) == 0)
273                         // Make the door
274                         makeDoor(doorplace, doordir);
275                 else
276                         // Don't actually make a door
277                         roomplace -= doordir;
278
279         }
280 }
281
282
283 void DungeonGen::makeRoom(v3s16 roomsize, v3s16 roomplace)
284 {
285         MapNode n_wall(dp.c_wall);
286         MapNode n_air(CONTENT_AIR);
287
288         // Make +-X walls
289         for (s16 z = 0; z < roomsize.Z; z++)
290         for (s16 y = 0; y < roomsize.Y; y++) {
291                 {
292                         v3s16 p = roomplace + v3s16(0, y, z);
293                         if (!vm->m_area.contains(p))
294                                 continue;
295                         u32 vi = vm->m_area.index(p);
296                         if (vm->m_flags[vi] & VMANIP_FLAG_DUNGEON_UNTOUCHABLE)
297                                 continue;
298                         vm->m_data[vi] = n_wall;
299                 }
300                 {
301                         v3s16 p = roomplace + v3s16(roomsize.X - 1, y, z);
302                         if (!vm->m_area.contains(p))
303                                 continue;
304                         u32 vi = vm->m_area.index(p);
305                         if (vm->m_flags[vi] & VMANIP_FLAG_DUNGEON_UNTOUCHABLE)
306                                 continue;
307                         vm->m_data[vi] = n_wall;
308                 }
309         }
310
311         // Make +-Z walls
312         for (s16 x = 0; x < roomsize.X; x++)
313         for (s16 y = 0; y < roomsize.Y; y++) {
314                 {
315                         v3s16 p = roomplace + v3s16(x, y, 0);
316                         if (!vm->m_area.contains(p))
317                                 continue;
318                         u32 vi = vm->m_area.index(p);
319                         if (vm->m_flags[vi] & VMANIP_FLAG_DUNGEON_UNTOUCHABLE)
320                                 continue;
321                         vm->m_data[vi] = n_wall;
322                 }
323                 {
324                         v3s16 p = roomplace + v3s16(x, y, roomsize.Z - 1);
325                         if (!vm->m_area.contains(p))
326                                 continue;
327                         u32 vi = vm->m_area.index(p);
328                         if (vm->m_flags[vi] & VMANIP_FLAG_DUNGEON_UNTOUCHABLE)
329                                 continue;
330                         vm->m_data[vi] = n_wall;
331                 }
332         }
333
334         // Make +-Y walls (floor and ceiling)
335         for (s16 z = 0; z < roomsize.Z; z++)
336         for (s16 x = 0; x < roomsize.X; x++) {
337                 {
338                         v3s16 p = roomplace + v3s16(x, 0, z);
339                         if (!vm->m_area.contains(p))
340                                 continue;
341                         u32 vi = vm->m_area.index(p);
342                         if (vm->m_flags[vi] & VMANIP_FLAG_DUNGEON_UNTOUCHABLE)
343                                 continue;
344                         vm->m_data[vi] = n_wall;
345                 }
346                 {
347                         v3s16 p = roomplace + v3s16(x,roomsize. Y - 1, z);
348                         if (!vm->m_area.contains(p))
349                                 continue;
350                         u32 vi = vm->m_area.index(p);
351                         if (vm->m_flags[vi] & VMANIP_FLAG_DUNGEON_UNTOUCHABLE)
352                                 continue;
353                         vm->m_data[vi] = n_wall;
354                 }
355         }
356
357         // Fill with air
358         for (s16 z = 1; z < roomsize.Z - 1; z++)
359         for (s16 y = 1; y < roomsize.Y - 1; y++)
360         for (s16 x = 1; x < roomsize.X - 1; x++) {
361                 v3s16 p = roomplace + v3s16(x, y, z);
362                 if (!vm->m_area.contains(p))
363                         continue;
364                 u32 vi = vm->m_area.index(p);
365                 vm->m_flags[vi] |= VMANIP_FLAG_DUNGEON_UNTOUCHABLE;
366                 vm->m_data[vi] = n_air;
367         }
368 }
369
370
371 void DungeonGen::makeFill(v3s16 place, v3s16 size,
372         u8 avoid_flags, MapNode n, u8 or_flags)
373 {
374         for (s16 z = 0; z < size.Z; z++)
375         for (s16 y = 0; y < size.Y; y++)
376         for (s16 x = 0; x < size.X; x++) {
377                 v3s16 p = place + v3s16(x, y, z);
378                 if (!vm->m_area.contains(p))
379                         continue;
380                 u32 vi = vm->m_area.index(p);
381                 if (vm->m_flags[vi] & avoid_flags)
382                         continue;
383                 vm->m_flags[vi] |= or_flags;
384                 vm->m_data[vi] = n;
385         }
386 }
387
388
389 void DungeonGen::makeHole(v3s16 place)
390 {
391         makeFill(place, dp.holesize, 0, MapNode(CONTENT_AIR),
392                 VMANIP_FLAG_DUNGEON_INSIDE);
393 }
394
395
396 void DungeonGen::makeDoor(v3s16 doorplace, v3s16 doordir)
397 {
398         makeHole(doorplace);
399
400 #ifdef DGEN_USE_TORCHES
401         // Place torch (for testing)
402         vm->m_data[vm->m_area.index(doorplace)] = MapNode(c_torch);
403 #endif
404 }
405
406
407 void DungeonGen::makeCorridor(v3s16 doorplace, v3s16 doordir,
408         v3s16 &result_place, v3s16 &result_dir)
409 {
410         makeHole(doorplace);
411         v3s16 p0 = doorplace;
412         v3s16 dir = doordir;
413         u32 length = random.range(dp.corridor_len_min, dp.corridor_len_max);
414         u32 partlength = random.range(dp.corridor_len_min, dp.corridor_len_max);
415         u32 partcount = 0;
416         s16 make_stairs = 0;
417
418         if (random.next() % 2 == 0 && partlength >= 3)
419                 make_stairs = random.next() % 2 ? 1 : -1;
420
421         for (u32 i = 0; i < length; i++) {
422                 v3s16 p = p0 + dir;
423                 if (partcount != 0)
424                         p.Y += make_stairs;
425
426                 // Check segment of minimum size corridor is in voxelmanip
427                 if (vm->m_area.contains(p) && vm->m_area.contains(p + v3s16(0, 1, 0))) {
428                         if (make_stairs) {
429                                 makeFill(p + v3s16(-1, -1, -1),
430                                         dp.holesize + v3s16(2, 3, 2),
431                                         VMANIP_FLAG_DUNGEON_UNTOUCHABLE,
432                                         MapNode(dp.c_wall),
433                                         0);
434                                 makeFill(p, dp.holesize, VMANIP_FLAG_DUNGEON_UNTOUCHABLE,
435                                         MapNode(CONTENT_AIR), VMANIP_FLAG_DUNGEON_INSIDE);
436                                 makeFill(p - dir, dp.holesize, VMANIP_FLAG_DUNGEON_UNTOUCHABLE,
437                                         MapNode(CONTENT_AIR), VMANIP_FLAG_DUNGEON_INSIDE);
438
439                                 // TODO: fix stairs code so it works 100%
440                                 // (quite difficult)
441
442                                 // exclude stairs from the bottom step
443                                 // exclude stairs from diagonal steps
444                                 if (((dir.X ^ dir.Z) & 1) &&
445                                                 (((make_stairs ==  1) && i != 0) ||
446                                                 ((make_stairs == -1) && i != length - 1))) {
447                                         // rotate face 180 deg if
448                                         // making stairs backwards
449                                         int facedir = dir_to_facedir(dir * make_stairs);
450                                         v3s16 ps = p;
451                                         u16 stair_width = (dir.Z != 0) ? dp.holesize.X : dp.holesize.Z;
452                                         // Stair width direction vector
453                                         v3s16 swv = (dir.Z != 0) ? v3s16(1, 0, 0) : v3s16(0, 0, 1);
454
455                                         for (u16 st = 0; st < stair_width; st++) {
456                                                 if (make_stairs == -1) {
457                                                         u32 vi = vm->m_area.index(ps.X - dir.X, ps.Y - 1, ps.Z - dir.Z);
458                                                         if (vm->m_area.contains(ps + v3s16(-dir.X, -1, -dir.Z)) &&
459                                                                         vm->m_data[vi].getContent() == dp.c_wall) {
460                                                                 vm->m_flags[vi] |= VMANIP_FLAG_DUNGEON_UNTOUCHABLE;
461                                                                 vm->m_data[vi] = MapNode(dp.c_stair, 0, facedir);
462                                                         }
463                                                 } else if (make_stairs == 1) {
464                                                         u32 vi = vm->m_area.index(ps.X, ps.Y - 1, ps.Z);
465                                                         if (vm->m_area.contains(ps + v3s16(0, -1, 0)) &&
466                                                                         vm->m_data[vi].getContent() == dp.c_wall) {
467                                                                 vm->m_flags[vi] |= VMANIP_FLAG_DUNGEON_UNTOUCHABLE;
468                                                                 vm->m_data[vi] = MapNode(dp.c_stair, 0, facedir);
469                                                         }
470                                                 }
471                                                 ps += swv;
472                                         }
473                                 }
474                         } else {
475                                 makeFill(p + v3s16(-1, -1, -1),
476                                         dp.holesize + v3s16(2, 2, 2),
477                                         VMANIP_FLAG_DUNGEON_UNTOUCHABLE,
478                                         MapNode(dp.c_wall),
479                                         0);
480                                 makeHole(p);
481                         }
482
483                         p0 = p;
484                 } else {
485                         // Can't go here, turn away
486                         dir = turn_xz(dir, random.range(0, 1));
487                         make_stairs = -make_stairs;
488                         partcount = 0;
489                         partlength = random.range(1, length);
490                         continue;
491                 }
492
493                 partcount++;
494                 if (partcount >= partlength) {
495                         partcount = 0;
496
497                         dir = random_turn(random, dir);
498
499                         partlength = random.range(1, length);
500
501                         make_stairs = 0;
502                         if (random.next() % 2 == 0 && partlength >= 3)
503                                 make_stairs = random.next() % 2 ? 1 : -1;
504                 }
505         }
506         result_place = p0;
507         result_dir = dir;
508 }
509
510
511 bool DungeonGen::findPlaceForDoor(v3s16 &result_place, v3s16 &result_dir)
512 {
513         for (u32 i = 0; i < 100; i++) {
514                 v3s16 p = m_pos + m_dir;
515                 v3s16 p1 = p + v3s16(0, 1, 0);
516                 if (!vm->m_area.contains(p) || !vm->m_area.contains(p1) || i % 4 == 0) {
517                         randomizeDir();
518                         continue;
519                 }
520                 if (vm->getNodeNoExNoEmerge(p).getContent() == dp.c_wall &&
521                                 vm->getNodeNoExNoEmerge(p1).getContent() == dp.c_wall) {
522                         // Found wall, this is a good place!
523                         result_place = p;
524                         result_dir = m_dir;
525                         // Randomize next direction
526                         randomizeDir();
527                         return true;
528                 }
529                 /*
530                         Determine where to move next
531                 */
532                 // Jump one up if the actual space is there
533                 if (vm->getNodeNoExNoEmerge(p +
534                                 v3s16(0, 0, 0)).getContent() == dp.c_wall &&
535                                 vm->getNodeNoExNoEmerge(p +
536                                 v3s16(0, 1, 0)).getContent() == CONTENT_AIR &&
537                                 vm->getNodeNoExNoEmerge(p +
538                                 v3s16(0, 2, 0)).getContent() == CONTENT_AIR)
539                         p += v3s16(0,1,0);
540                 // Jump one down if the actual space is there
541                 if (vm->getNodeNoExNoEmerge(p +
542                                 v3s16(0, 1, 0)).getContent() == dp.c_wall &&
543                                 vm->getNodeNoExNoEmerge(p +
544                                 v3s16(0, 0, 0)).getContent() == CONTENT_AIR &&
545                                 vm->getNodeNoExNoEmerge(p +
546                                 v3s16(0, -1, 0)).getContent() == CONTENT_AIR)
547                         p += v3s16(0, -1, 0);
548                 // Check if walking is now possible
549                 if (vm->getNodeNoExNoEmerge(p).getContent() != CONTENT_AIR ||
550                                 vm->getNodeNoExNoEmerge(p +
551                                 v3s16(0, 1, 0)).getContent() != CONTENT_AIR) {
552                         // Cannot continue walking here
553                         randomizeDir();
554                         continue;
555                 }
556                 // Move there
557                 m_pos = p;
558         }
559         return false;
560 }
561
562
563 bool DungeonGen::findPlaceForRoomDoor(v3s16 roomsize, v3s16 &result_doorplace,
564         v3s16 &result_doordir, v3s16 &result_roomplace)
565 {
566         for (s16 trycount = 0; trycount < 30; trycount++) {
567                 v3s16 doorplace;
568                 v3s16 doordir;
569                 bool r = findPlaceForDoor(doorplace, doordir);
570                 if (!r)
571                         continue;
572                 v3s16 roomplace;
573                 // X east, Z north, Y up
574                 if (doordir == v3s16(1, 0, 0)) // X+
575                         roomplace = doorplace +
576                                 v3s16(0, -1, random.range(-roomsize.Z + 2, -2));
577                 if (doordir == v3s16(-1, 0, 0)) // X-
578                         roomplace = doorplace +
579                                 v3s16(-roomsize.X + 1, -1, random.range(-roomsize.Z + 2, -2));
580                 if (doordir == v3s16(0, 0, 1)) // Z+
581                         roomplace = doorplace +
582                                 v3s16(random.range(-roomsize.X + 2, -2), -1, 0);
583                 if (doordir == v3s16(0, 0, -1)) // Z-
584                         roomplace = doorplace +
585                                 v3s16(random.range(-roomsize.X + 2, -2), -1, -roomsize.Z + 1);
586
587                 // Check fit
588                 bool fits = true;
589                 for (s16 z = 1; z < roomsize.Z - 1; z++)
590                 for (s16 y = 1; y < roomsize.Y - 1; y++)
591                 for (s16 x = 1; x < roomsize.X - 1; x++) {
592                         v3s16 p = roomplace + v3s16(x, y, z);
593                         if (!vm->m_area.contains(p)) {
594                                 fits = false;
595                                 break;
596                         }
597                         if (vm->m_flags[vm->m_area.index(p)] & VMANIP_FLAG_DUNGEON_INSIDE) {
598                                 fits = false;
599                                 break;
600                         }
601                 }
602                 if (!fits) {
603                         // Find new place
604                         continue;
605                 }
606                 result_doorplace = doorplace;
607                 result_doordir   = doordir;
608                 result_roomplace = roomplace;
609                 return true;
610         }
611         return false;
612 }
613
614
615 v3s16 rand_ortho_dir(PseudoRandom &random, bool diagonal_dirs)
616 {
617         // Make diagonal directions somewhat rare
618         if (diagonal_dirs && (random.next() % 4 == 0)) {
619                 v3s16 dir;
620                 int trycount = 0;
621
622                 do {
623                         trycount++;
624
625                         dir.Z = random.next() % 3 - 1;
626                         dir.Y = 0;
627                         dir.X = random.next() % 3 - 1;
628                 } while ((dir.X == 0 || dir.Z == 0) && trycount < 10);
629
630                 return dir;
631         }
632
633         if (random.next() % 2 == 0)
634                 return random.next() % 2 ? v3s16(-1, 0, 0) : v3s16(1, 0, 0);
635
636         return random.next() % 2 ? v3s16(0, 0, -1) : v3s16(0, 0, 1);
637 }
638
639
640 v3s16 turn_xz(v3s16 olddir, int t)
641 {
642         v3s16 dir;
643         if (t == 0) {
644                 // Turn right
645                 dir.X = olddir.Z;
646                 dir.Z = -olddir.X;
647                 dir.Y = olddir.Y;
648         } else {
649                 // Turn left
650                 dir.X = -olddir.Z;
651                 dir.Z = olddir.X;
652                 dir.Y = olddir.Y;
653         }
654         return dir;
655 }
656
657
658 v3s16 random_turn(PseudoRandom &random, v3s16 olddir)
659 {
660         int turn = random.range(0, 2);
661         v3s16 dir;
662         if (turn == 0)
663                 // Go straight
664                 dir = olddir;
665         else if (turn == 1)
666                 // Turn right
667                 dir = turn_xz(olddir, 0);
668         else
669                 // Turn left
670                 dir = turn_xz(olddir, 1);
671         return dir;
672 }
673
674
675 int dir_to_facedir(v3s16 d)
676 {
677         if (abs(d.X) > abs(d.Z))
678                 return d.X < 0 ? 3 : 1;
679
680         return d.Z < 0 ? 2 : 0;
681 }