]> git.lizzy.rs Git - minetest.git/blob - src/mapgen/dungeongen.cpp
porting.cpp: better minetest support on BSD
[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                 memcpy(&dp, dparams, sizeof(dp));
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. Optionally set ignore to be untouchable to
104                 // prevent projecting dungeons.
105                 for (s16 z = nmin.Z; z <= nmax.Z; z++) {
106                         for (s16 y = nmin.Y; y <= nmax.Y; y++) {
107                                 u32 i = vm->m_area.index(nmin.X, y, z);
108                                 for (s16 x = nmin.X; x <= nmax.X; x++) {
109                                         content_t c = vm->m_data[i].getContent();
110                                         NodeDrawType dtype = ndef->get(c).drawtype;
111                                         if (dtype == NDT_AIRLIKE || dtype == NDT_LIQUID ||
112                                                         (preserve_ignore && c == CONTENT_IGNORE))
113                                                 vm->m_flags[i] |= VMANIP_FLAG_DUNGEON_PRESERVE;
114                                         i++;
115                                 }
116                         }
117                 }
118         }
119
120         // Add them
121         for (u32 i = 0; i < std::floor(nval_density); i++)
122                 makeDungeon(v3s16(1, 1, 1) * MAP_BLOCKSIZE);
123
124         // Optionally convert some structure to alternative structure
125         if (dp.c_alt_wall == CONTENT_IGNORE)
126                 return;
127
128         for (s16 z = nmin.Z; z <= nmax.Z; z++)
129         for (s16 y = nmin.Y; y <= nmax.Y; y++) {
130                 u32 i = vm->m_area.index(nmin.X, y, z);
131                 for (s16 x = nmin.X; x <= nmax.X; x++) {
132                         if (vm->m_data[i].getContent() == dp.c_wall) {
133                                 if (NoisePerlin3D(&dp.np_alt_wall, x, y, z, blockseed) > 0.0f)
134                                         vm->m_data[i].setContent(dp.c_alt_wall);
135                         }
136                         i++;
137                 }
138         }
139
140         //printf("== gen dungeons: %dms\n", t.stop());
141 }
142
143
144 void DungeonGen::makeDungeon(v3s16 start_padding)
145 {
146         const v3s16 &areasize = vm->m_area.getExtent();
147         v3s16 roomsize;
148         v3s16 roomplace;
149
150         /*
151                 Find place for first room.
152                 There is a 1 in 4 chance of the first room being 'large',
153                 all other rooms are not 'large'.
154         */
155         bool fits = false;
156         for (u32 i = 0; i < 100 && !fits; i++) {
157                 bool is_large_room = ((random.next() & 3) == 1);
158                 if (is_large_room) {
159                         roomsize.Z = random.range(
160                                 dp.room_size_large_min.Z, dp.room_size_large_max.Z);
161                         roomsize.Y = random.range(
162                                 dp.room_size_large_min.Y, dp.room_size_large_max.Y);
163                         roomsize.X = random.range(
164                                 dp.room_size_large_min.X, dp.room_size_large_max.X);
165                 } else {
166                         roomsize.Z = random.range(dp.room_size_min.Z, dp.room_size_max.Z);
167                         roomsize.Y = random.range(dp.room_size_min.Y, dp.room_size_max.Y);
168                         roomsize.X = random.range(dp.room_size_min.X, dp.room_size_max.X);
169                 }
170
171                 // start_padding is used to disallow starting the generation of
172                 // a dungeon in a neighboring generation chunk
173                 roomplace = vm->m_area.MinEdge + start_padding;
174                 roomplace.Z += random.range(0, areasize.Z - roomsize.Z - start_padding.Z);
175                 roomplace.Y += random.range(0, areasize.Y - roomsize.Y - start_padding.Y);
176                 roomplace.X += random.range(0, areasize.X - roomsize.X - start_padding.X);
177
178                 /*
179                         Check that we're not putting the room to an unknown place,
180                         otherwise it might end up floating in the air
181                 */
182                 fits = true;
183                 for (s16 z = 0; z < roomsize.Z; z++)
184                 for (s16 y = 0; y < roomsize.Y; y++)
185                 for (s16 x = 0; x < roomsize.X; x++) {
186                         v3s16 p = roomplace + v3s16(x, y, z);
187                         u32 vi = vm->m_area.index(p);
188                         if ((vm->m_flags[vi] & VMANIP_FLAG_DUNGEON_UNTOUCHABLE) ||
189                                         vm->m_data[vi].getContent() == CONTENT_IGNORE) {
190                                 fits = false;
191                                 break;
192                         }
193                 }
194         }
195         // No place found
196         if (!fits)
197                 return;
198
199         /*
200                 Stores the center position of the last room made, so that
201                 a new corridor can be started from the last room instead of
202                 the new room, if chosen so.
203         */
204         v3s16 last_room_center = roomplace + v3s16(roomsize.X / 2, 1, roomsize.Z / 2);
205
206         u32 room_count = random.range(dp.rooms_min, dp.rooms_max);
207         for (u32 i = 0; i < room_count; i++) {
208                 // Make a room to the determined place
209                 makeRoom(roomsize, roomplace);
210
211                 v3s16 room_center = roomplace + v3s16(roomsize.X / 2, 1, roomsize.Z / 2);
212                 if (gennotify)
213                         gennotify->addEvent(dp.notifytype, room_center);
214
215 #ifdef DGEN_USE_TORCHES
216                 // Place torch at room center (for testing)
217                 vm->m_data[vm->m_area.index(room_center)] = MapNode(c_torch);
218 #endif
219
220                 // Quit if last room
221                 if (i == room_count - 1)
222                         break;
223
224                 // Determine walker start position
225
226                 bool start_in_last_room = (random.range(0, 2) != 0);
227
228                 v3s16 walker_start_place;
229
230                 if (start_in_last_room) {
231                         walker_start_place = last_room_center;
232                 } else {
233                         walker_start_place = room_center;
234                         // Store center of current room as the last one
235                         last_room_center = room_center;
236                 }
237
238                 // Create walker and find a place for a door
239                 v3s16 doorplace;
240                 v3s16 doordir;
241
242                 m_pos = walker_start_place;
243                 if (!findPlaceForDoor(doorplace, doordir))
244                         return;
245
246                 if (random.range(0, 1) == 0)
247                         // Make the door
248                         makeDoor(doorplace, doordir);
249                 else
250                         // Don't actually make a door
251                         doorplace -= doordir;
252
253                 // Make a random corridor starting from the door
254                 v3s16 corridor_end;
255                 v3s16 corridor_end_dir;
256                 makeCorridor(doorplace, doordir, corridor_end, corridor_end_dir);
257
258                 // Find a place for a random sized room
259                 roomsize.Z = random.range(dp.room_size_min.Z, dp.room_size_max.Z);
260                 roomsize.Y = random.range(dp.room_size_min.Y, dp.room_size_max.Y);
261                 roomsize.X = random.range(dp.room_size_min.X, dp.room_size_max.X);
262
263                 m_pos = corridor_end;
264                 m_dir = corridor_end_dir;
265                 if (!findPlaceForRoomDoor(roomsize, doorplace, doordir, roomplace))
266                         return;
267
268                 if (random.range(0, 1) == 0)
269                         // Make the door
270                         makeDoor(doorplace, doordir);
271                 else
272                         // Don't actually make a door
273                         roomplace -= doordir;
274
275         }
276 }
277
278
279 void DungeonGen::makeRoom(v3s16 roomsize, v3s16 roomplace)
280 {
281         MapNode n_wall(dp.c_wall);
282         MapNode n_air(CONTENT_AIR);
283
284         // Make +-X walls
285         for (s16 z = 0; z < roomsize.Z; z++)
286         for (s16 y = 0; y < roomsize.Y; y++) {
287                 {
288                         v3s16 p = roomplace + v3s16(0, y, z);
289                         if (!vm->m_area.contains(p))
290                                 continue;
291                         u32 vi = vm->m_area.index(p);
292                         if (vm->m_flags[vi] & VMANIP_FLAG_DUNGEON_UNTOUCHABLE)
293                                 continue;
294                         vm->m_data[vi] = n_wall;
295                 }
296                 {
297                         v3s16 p = roomplace + v3s16(roomsize.X - 1, y, z);
298                         if (!vm->m_area.contains(p))
299                                 continue;
300                         u32 vi = vm->m_area.index(p);
301                         if (vm->m_flags[vi] & VMANIP_FLAG_DUNGEON_UNTOUCHABLE)
302                                 continue;
303                         vm->m_data[vi] = n_wall;
304                 }
305         }
306
307         // Make +-Z walls
308         for (s16 x = 0; x < roomsize.X; x++)
309         for (s16 y = 0; y < roomsize.Y; y++) {
310                 {
311                         v3s16 p = roomplace + v3s16(x, y, 0);
312                         if (!vm->m_area.contains(p))
313                                 continue;
314                         u32 vi = vm->m_area.index(p);
315                         if (vm->m_flags[vi] & VMANIP_FLAG_DUNGEON_UNTOUCHABLE)
316                                 continue;
317                         vm->m_data[vi] = n_wall;
318                 }
319                 {
320                         v3s16 p = roomplace + v3s16(x, y, roomsize.Z - 1);
321                         if (!vm->m_area.contains(p))
322                                 continue;
323                         u32 vi = vm->m_area.index(p);
324                         if (vm->m_flags[vi] & VMANIP_FLAG_DUNGEON_UNTOUCHABLE)
325                                 continue;
326                         vm->m_data[vi] = n_wall;
327                 }
328         }
329
330         // Make +-Y walls (floor and ceiling)
331         for (s16 z = 0; z < roomsize.Z; z++)
332         for (s16 x = 0; x < roomsize.X; x++) {
333                 {
334                         v3s16 p = roomplace + v3s16(x, 0, z);
335                         if (!vm->m_area.contains(p))
336                                 continue;
337                         u32 vi = vm->m_area.index(p);
338                         if (vm->m_flags[vi] & VMANIP_FLAG_DUNGEON_UNTOUCHABLE)
339                                 continue;
340                         vm->m_data[vi] = n_wall;
341                 }
342                 {
343                         v3s16 p = roomplace + v3s16(x,roomsize. Y - 1, z);
344                         if (!vm->m_area.contains(p))
345                                 continue;
346                         u32 vi = vm->m_area.index(p);
347                         if (vm->m_flags[vi] & VMANIP_FLAG_DUNGEON_UNTOUCHABLE)
348                                 continue;
349                         vm->m_data[vi] = n_wall;
350                 }
351         }
352
353         // Fill with air
354         for (s16 z = 1; z < roomsize.Z - 1; z++)
355         for (s16 y = 1; y < roomsize.Y - 1; y++)
356         for (s16 x = 1; x < roomsize.X - 1; x++) {
357                 v3s16 p = roomplace + v3s16(x, y, z);
358                 if (!vm->m_area.contains(p))
359                         continue;
360                 u32 vi = vm->m_area.index(p);
361                 vm->m_flags[vi] |= VMANIP_FLAG_DUNGEON_UNTOUCHABLE;
362                 vm->m_data[vi] = n_air;
363         }
364 }
365
366
367 void DungeonGen::makeFill(v3s16 place, v3s16 size,
368         u8 avoid_flags, MapNode n, u8 or_flags)
369 {
370         for (s16 z = 0; z < size.Z; z++)
371         for (s16 y = 0; y < size.Y; y++)
372         for (s16 x = 0; x < size.X; x++) {
373                 v3s16 p = place + v3s16(x, y, z);
374                 if (!vm->m_area.contains(p))
375                         continue;
376                 u32 vi = vm->m_area.index(p);
377                 if (vm->m_flags[vi] & avoid_flags)
378                         continue;
379                 vm->m_flags[vi] |= or_flags;
380                 vm->m_data[vi] = n;
381         }
382 }
383
384
385 void DungeonGen::makeHole(v3s16 place)
386 {
387         makeFill(place, dp.holesize, 0, MapNode(CONTENT_AIR),
388                 VMANIP_FLAG_DUNGEON_INSIDE);
389 }
390
391
392 void DungeonGen::makeDoor(v3s16 doorplace, v3s16 doordir)
393 {
394         makeHole(doorplace);
395
396 #ifdef DGEN_USE_TORCHES
397         // Place torch (for testing)
398         vm->m_data[vm->m_area.index(doorplace)] = MapNode(c_torch);
399 #endif
400 }
401
402
403 void DungeonGen::makeCorridor(v3s16 doorplace, v3s16 doordir,
404         v3s16 &result_place, v3s16 &result_dir)
405 {
406         makeHole(doorplace);
407         v3s16 p0 = doorplace;
408         v3s16 dir = doordir;
409         u32 length = random.range(dp.corridor_len_min, dp.corridor_len_max);
410         u32 partlength = random.range(dp.corridor_len_min, dp.corridor_len_max);
411         u32 partcount = 0;
412         s16 make_stairs = 0;
413
414         if (random.next() % 2 == 0 && partlength >= 3)
415                 make_stairs = random.next() % 2 ? 1 : -1;
416
417         for (u32 i = 0; i < length; i++) {
418                 v3s16 p = p0 + dir;
419                 if (partcount != 0)
420                         p.Y += make_stairs;
421
422                 // Check segment of minimum size corridor is in voxelmanip
423                 if (vm->m_area.contains(p) && vm->m_area.contains(p + v3s16(0, 1, 0))) {
424                         if (make_stairs) {
425                                 makeFill(p + v3s16(-1, -1, -1),
426                                         dp.holesize + v3s16(2, 3, 2),
427                                         VMANIP_FLAG_DUNGEON_UNTOUCHABLE,
428                                         MapNode(dp.c_wall),
429                                         0);
430                                 makeFill(p, dp.holesize, VMANIP_FLAG_DUNGEON_UNTOUCHABLE,
431                                         MapNode(CONTENT_AIR), VMANIP_FLAG_DUNGEON_INSIDE);
432                                 makeFill(p - dir, dp.holesize, VMANIP_FLAG_DUNGEON_UNTOUCHABLE,
433                                         MapNode(CONTENT_AIR), VMANIP_FLAG_DUNGEON_INSIDE);
434
435                                 // TODO: fix stairs code so it works 100%
436                                 // (quite difficult)
437
438                                 // exclude stairs from the bottom step
439                                 // exclude stairs from diagonal steps
440                                 if (((dir.X ^ dir.Z) & 1) &&
441                                                 (((make_stairs ==  1) && i != 0) ||
442                                                 ((make_stairs == -1) && i != length - 1))) {
443                                         // rotate face 180 deg if
444                                         // making stairs backwards
445                                         int facedir = dir_to_facedir(dir * make_stairs);
446                                         v3s16 ps = p;
447                                         u16 stair_width = (dir.Z != 0) ? dp.holesize.X : dp.holesize.Z;
448                                         // Stair width direction vector
449                                         v3s16 swv = (dir.Z != 0) ? v3s16(1, 0, 0) : v3s16(0, 0, 1);
450
451                                         for (u16 st = 0; st < stair_width; st++) {
452                                                 if (make_stairs == -1) {
453                                                         u32 vi = vm->m_area.index(ps.X - dir.X, ps.Y - 1, ps.Z - dir.Z);
454                                                         if (vm->m_area.contains(ps + v3s16(-dir.X, -1, -dir.Z)) &&
455                                                                         vm->m_data[vi].getContent() == dp.c_wall) {
456                                                                 vm->m_flags[vi] |= VMANIP_FLAG_DUNGEON_UNTOUCHABLE;
457                                                                 vm->m_data[vi] = MapNode(dp.c_stair, 0, facedir);
458                                                         }
459                                                 } else if (make_stairs == 1) {
460                                                         u32 vi = vm->m_area.index(ps.X, ps.Y - 1, ps.Z);
461                                                         if (vm->m_area.contains(ps + v3s16(0, -1, 0)) &&
462                                                                         vm->m_data[vi].getContent() == dp.c_wall) {
463                                                                 vm->m_flags[vi] |= VMANIP_FLAG_DUNGEON_UNTOUCHABLE;
464                                                                 vm->m_data[vi] = MapNode(dp.c_stair, 0, facedir);
465                                                         }
466                                                 }
467                                                 ps += swv;
468                                         }
469                                 }
470                         } else {
471                                 makeFill(p + v3s16(-1, -1, -1),
472                                         dp.holesize + v3s16(2, 2, 2),
473                                         VMANIP_FLAG_DUNGEON_UNTOUCHABLE,
474                                         MapNode(dp.c_wall),
475                                         0);
476                                 makeHole(p);
477                         }
478
479                         p0 = p;
480                 } else {
481                         // Can't go here, turn away
482                         dir = turn_xz(dir, random.range(0, 1));
483                         make_stairs = -make_stairs;
484                         partcount = 0;
485                         partlength = random.range(1, length);
486                         continue;
487                 }
488
489                 partcount++;
490                 if (partcount >= partlength) {
491                         partcount = 0;
492
493                         dir = random_turn(random, dir);
494
495                         partlength = random.range(1, length);
496
497                         make_stairs = 0;
498                         if (random.next() % 2 == 0 && partlength >= 3)
499                                 make_stairs = random.next() % 2 ? 1 : -1;
500                 }
501         }
502         result_place = p0;
503         result_dir = dir;
504 }
505
506
507 bool DungeonGen::findPlaceForDoor(v3s16 &result_place, v3s16 &result_dir)
508 {
509         for (u32 i = 0; i < 100; i++) {
510                 v3s16 p = m_pos + m_dir;
511                 v3s16 p1 = p + v3s16(0, 1, 0);
512                 if (!vm->m_area.contains(p) || !vm->m_area.contains(p1) || i % 4 == 0) {
513                         randomizeDir();
514                         continue;
515                 }
516                 if (vm->getNodeNoExNoEmerge(p).getContent() == dp.c_wall &&
517                                 vm->getNodeNoExNoEmerge(p1).getContent() == dp.c_wall) {
518                         // Found wall, this is a good place!
519                         result_place = p;
520                         result_dir = m_dir;
521                         // Randomize next direction
522                         randomizeDir();
523                         return true;
524                 }
525                 /*
526                         Determine where to move next
527                 */
528                 // Jump one up if the actual space is there
529                 if (vm->getNodeNoExNoEmerge(p +
530                                 v3s16(0, 0, 0)).getContent() == dp.c_wall &&
531                                 vm->getNodeNoExNoEmerge(p +
532                                 v3s16(0, 1, 0)).getContent() == CONTENT_AIR &&
533                                 vm->getNodeNoExNoEmerge(p +
534                                 v3s16(0, 2, 0)).getContent() == CONTENT_AIR)
535                         p += v3s16(0,1,0);
536                 // Jump one down if the actual space is there
537                 if (vm->getNodeNoExNoEmerge(p +
538                                 v3s16(0, 1, 0)).getContent() == dp.c_wall &&
539                                 vm->getNodeNoExNoEmerge(p +
540                                 v3s16(0, 0, 0)).getContent() == CONTENT_AIR &&
541                                 vm->getNodeNoExNoEmerge(p +
542                                 v3s16(0, -1, 0)).getContent() == CONTENT_AIR)
543                         p += v3s16(0, -1, 0);
544                 // Check if walking is now possible
545                 if (vm->getNodeNoExNoEmerge(p).getContent() != CONTENT_AIR ||
546                                 vm->getNodeNoExNoEmerge(p +
547                                 v3s16(0, 1, 0)).getContent() != CONTENT_AIR) {
548                         // Cannot continue walking here
549                         randomizeDir();
550                         continue;
551                 }
552                 // Move there
553                 m_pos = p;
554         }
555         return false;
556 }
557
558
559 bool DungeonGen::findPlaceForRoomDoor(v3s16 roomsize, v3s16 &result_doorplace,
560         v3s16 &result_doordir, v3s16 &result_roomplace)
561 {
562         for (s16 trycount = 0; trycount < 30; trycount++) {
563                 v3s16 doorplace;
564                 v3s16 doordir;
565                 bool r = findPlaceForDoor(doorplace, doordir);
566                 if (!r)
567                         continue;
568                 v3s16 roomplace;
569                 // X east, Z north, Y up
570                 if (doordir == v3s16(1, 0, 0)) // X+
571                         roomplace = doorplace +
572                                 v3s16(0, -1, random.range(-roomsize.Z + 2, -2));
573                 if (doordir == v3s16(-1, 0, 0)) // X-
574                         roomplace = doorplace +
575                                 v3s16(-roomsize.X + 1, -1, random.range(-roomsize.Z + 2, -2));
576                 if (doordir == v3s16(0, 0, 1)) // Z+
577                         roomplace = doorplace +
578                                 v3s16(random.range(-roomsize.X + 2, -2), -1, 0);
579                 if (doordir == v3s16(0, 0, -1)) // Z-
580                         roomplace = doorplace +
581                                 v3s16(random.range(-roomsize.X + 2, -2), -1, -roomsize.Z + 1);
582
583                 // Check fit
584                 bool fits = true;
585                 for (s16 z = 1; z < roomsize.Z - 1; z++)
586                 for (s16 y = 1; y < roomsize.Y - 1; y++)
587                 for (s16 x = 1; x < roomsize.X - 1; x++) {
588                         v3s16 p = roomplace + v3s16(x, y, z);
589                         if (!vm->m_area.contains(p)) {
590                                 fits = false;
591                                 break;
592                         }
593                         if (vm->m_flags[vm->m_area.index(p)] & VMANIP_FLAG_DUNGEON_INSIDE) {
594                                 fits = false;
595                                 break;
596                         }
597                 }
598                 if (!fits) {
599                         // Find new place
600                         continue;
601                 }
602                 result_doorplace = doorplace;
603                 result_doordir   = doordir;
604                 result_roomplace = roomplace;
605                 return true;
606         }
607         return false;
608 }
609
610
611 v3s16 rand_ortho_dir(PseudoRandom &random, bool diagonal_dirs)
612 {
613         // Make diagonal directions somewhat rare
614         if (diagonal_dirs && (random.next() % 4 == 0)) {
615                 v3s16 dir;
616                 int trycount = 0;
617
618                 do {
619                         trycount++;
620
621                         dir.Z = random.next() % 3 - 1;
622                         dir.Y = 0;
623                         dir.X = random.next() % 3 - 1;
624                 } while ((dir.X == 0 || dir.Z == 0) && trycount < 10);
625
626                 return dir;
627         }
628
629         if (random.next() % 2 == 0)
630                 return random.next() % 2 ? v3s16(-1, 0, 0) : v3s16(1, 0, 0);
631
632         return random.next() % 2 ? v3s16(0, 0, -1) : v3s16(0, 0, 1);
633 }
634
635
636 v3s16 turn_xz(v3s16 olddir, int t)
637 {
638         v3s16 dir;
639         if (t == 0) {
640                 // Turn right
641                 dir.X = olddir.Z;
642                 dir.Z = -olddir.X;
643                 dir.Y = olddir.Y;
644         } else {
645                 // Turn left
646                 dir.X = -olddir.Z;
647                 dir.Z = olddir.X;
648                 dir.Y = olddir.Y;
649         }
650         return dir;
651 }
652
653
654 v3s16 random_turn(PseudoRandom &random, v3s16 olddir)
655 {
656         int turn = random.range(0, 2);
657         v3s16 dir;
658         if (turn == 0)
659                 // Go straight
660                 dir = olddir;
661         else if (turn == 1)
662                 // Turn right
663                 dir = turn_xz(olddir, 0);
664         else
665                 // Turn left
666                 dir = turn_xz(olddir, 1);
667         return dir;
668 }
669
670
671 int dir_to_facedir(v3s16 d)
672 {
673         if (abs(d.X) > abs(d.Z))
674                 return d.X < 0 ? 3 : 1;
675
676         return d.Z < 0 ? 2 : 0;
677 }