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