added (optional) support for 6d facedir in dir_to_facedir and added facedir_to_dir
authorhdastwb <hdastwb@hdastwb.heliohost.org>
Wed, 17 Jul 2013 00:50:41 +0000 (20:50 -0400)
committerRealBadAngel <mk@realbadangel.pl>
Tue, 23 Jul 2013 22:31:32 +0000 (00:31 +0200)
builtin/item.lua
doc/lua_api.txt

index b8381515ac89ecc0a6ca7cc75364f21e9f1aa5da..9105d8c0cdcf6bb69e94a9078755c07edee2dd9e 100644 (file)
@@ -34,8 +34,45 @@ function minetest.get_pointed_thing_position(pointed_thing, above)
        end
 end
 
-function minetest.dir_to_facedir(dir)
-       if math.abs(dir.x) > math.abs(dir.z) then
+function minetest.dir_to_facedir(dir, is6d)
+       --account for y if requested
+       if is6d and math.abs(dir.y) > math.abs(dir.x) and math.abs(dir.y) > math.abs(dir.z) then
+
+               --from above
+               if dir.y < 0 then
+                       if math.abs(dir.x) > math.abs(dir.z) then
+                               if dir.x < 0 then
+                                       return 19
+                               else
+                                       return 13
+                               end
+                       else
+                               if dir.z < 0 then
+                                       return 10
+                               else
+                                       return 4
+                               end
+                       end
+
+               --from below
+               else
+                       if math.abs(dir.x) > math.abs(dir.z) then
+                               if dir.x < 0 then
+                                       return 15
+                               else
+                                       return 17
+                               end
+                       else
+                               if dir.z < 0 then
+                                       return 6
+                               else
+                                       return 8
+                               end
+                       end
+               end
+
+       --otherwise, place horizontally
+       elseif math.abs(dir.x) > math.abs(dir.z) then
                if dir.x < 0 then
                        return 3
                else
@@ -50,6 +87,27 @@ function minetest.dir_to_facedir(dir)
        end
 end
 
+function minetest.facedir_to_dir(facedir)
+       --a table of possible dirs
+       return ({{x=0, y=0, z=1},
+                                       {x=1, y=0, z=0},
+                                       {x=0, y=0, z=-1},
+                                       {x=-1, y=0, z=0},
+                                       {x=0, y=-1, z=0},
+                                       {x=0, y=1, z=0}})
+
+                                       --indexed into by a table of correlating facedirs
+                                       [({[0]=1, 2, 3, 4, 
+                                               5, 2, 6, 4,
+                                               6, 2, 5, 4,
+                                               1, 5, 3, 6,
+                                               1, 6, 3, 5,
+                                               1, 4, 3, 2})
+
+                                               --indexed into by the facedir in question
+                                               [facedir]]
+end
+
 function minetest.dir_to_wallmounted(dir)
        if math.abs(dir.y) > math.max(math.abs(dir.x), math.abs(dir.z)) then
                if dir.y < 0 then
index 1890682fd4e5fe8bdda04b148f6ea623bc3d6156..b0281bee7b4e2979bf9c1c938c8917817be2622b 100644 (file)
@@ -1270,8 +1270,10 @@ minetest.inventorycube(img1, img2, img3)
 ^ Returns a string for making an image of a cube (useful as an item image)
 minetest.get_pointed_thing_position(pointed_thing, above)
 ^ Get position of a pointed_thing (that you can get from somewhere)
-minetest.dir_to_facedir(dir)
-^ Convert a vector to a facedir value, used in param2 for paramtype2="facedir"
+minetest.dir_to_facedir(dir, is6d)
+^ 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
+minetest.facedir_to_dir(facedir)
+^ Convert a facedir back into a vector aimed directly out the "back" of a node
 minetest.dir_to_wallmounted(dir)
 ^ Convert a vector to a wallmounted value, used for paramtype2="wallmounted"
 minetest.get_node_drops(nodename, toolname)