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