Skip to content

Commit a0f5b70

Browse files
hdastwbRealBadAngel
hdastwb
authored andcommittedJul 23, 2013
added (optional) support for 6d facedir in dir_to_facedir and added facedir_to_dir
1 parent 0b4b9e7 commit a0f5b70

File tree

2 files changed

+64
-4
lines changed

2 files changed

+64
-4
lines changed
 

‎builtin/item.lua

+60-2
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,45 @@ function minetest.get_pointed_thing_position(pointed_thing, above)
3434
end
3535
end
3636

37-
function minetest.dir_to_facedir(dir)
38-
if math.abs(dir.x) > math.abs(dir.z) then
37+
function minetest.dir_to_facedir(dir, is6d)
38+
--account for y if requested
39+
if is6d and math.abs(dir.y) > math.abs(dir.x) and math.abs(dir.y) > math.abs(dir.z) then
40+
41+
--from above
42+
if dir.y < 0 then
43+
if math.abs(dir.x) > math.abs(dir.z) then
44+
if dir.x < 0 then
45+
return 19
46+
else
47+
return 13
48+
end
49+
else
50+
if dir.z < 0 then
51+
return 10
52+
else
53+
return 4
54+
end
55+
end
56+
57+
--from below
58+
else
59+
if math.abs(dir.x) > math.abs(dir.z) then
60+
if dir.x < 0 then
61+
return 15
62+
else
63+
return 17
64+
end
65+
else
66+
if dir.z < 0 then
67+
return 6
68+
else
69+
return 8
70+
end
71+
end
72+
end
73+
74+
--otherwise, place horizontally
75+
elseif math.abs(dir.x) > math.abs(dir.z) then
3976
if dir.x < 0 then
4077
return 3
4178
else
@@ -50,6 +87,27 @@ function minetest.dir_to_facedir(dir)
5087
end
5188
end
5289

90+
function minetest.facedir_to_dir(facedir)
91+
--a table of possible dirs
92+
return ({{x=0, y=0, z=1},
93+
{x=1, y=0, z=0},
94+
{x=0, y=0, z=-1},
95+
{x=-1, y=0, z=0},
96+
{x=0, y=-1, z=0},
97+
{x=0, y=1, z=0}})
98+
99+
--indexed into by a table of correlating facedirs
100+
[({[0]=1, 2, 3, 4,
101+
5, 2, 6, 4,
102+
6, 2, 5, 4,
103+
1, 5, 3, 6,
104+
1, 6, 3, 5,
105+
1, 4, 3, 2})
106+
107+
--indexed into by the facedir in question
108+
[facedir]]
109+
end
110+
53111
function minetest.dir_to_wallmounted(dir)
54112
if math.abs(dir.y) > math.max(math.abs(dir.x), math.abs(dir.z)) then
55113
if dir.y < 0 then

‎doc/lua_api.txt

+4-2
Original file line numberDiff line numberDiff line change
@@ -1270,8 +1270,10 @@ minetest.inventorycube(img1, img2, img3)
12701270
^ Returns a string for making an image of a cube (useful as an item image)
12711271
minetest.get_pointed_thing_position(pointed_thing, above)
12721272
^ Get position of a pointed_thing (that you can get from somewhere)
1273-
minetest.dir_to_facedir(dir)
1274-
^ Convert a vector to a facedir value, used in param2 for paramtype2="facedir"
1273+
minetest.dir_to_facedir(dir, is6d)
1274+
^ Convert a vector to a facedir value, used in param2 for paramtype2="facedir"; passing something non-nil/false for the optional second parameter causes it to take the y component into account
1275+
minetest.facedir_to_dir(facedir)
1276+
^ Convert a facedir back into a vector aimed directly out the "back" of a node
12751277
minetest.dir_to_wallmounted(dir)
12761278
^ Convert a vector to a wallmounted value, used for paramtype2="wallmounted"
12771279
minetest.get_node_drops(nodename, toolname)

0 commit comments

Comments
 (0)
Please sign in to comment.