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