diff options
author | Diego Martinez <kaeza@users.sf.net> | 2016-02-09 21:46:22 -0300 |
---|---|---|
committer | paramat <mat.gregory@virginmedia.com> | 2016-02-11 04:23:17 +0000 |
commit | 13dd7959dbb62ca9327062181d7c4f04b469c38c (patch) | |
tree | d3db1e8ed2ecc0795aa09019dd06201a342ec8ba /builtin/game | |
parent | 0f03547b033690932707b2dde1ce3eeb09ddcf7e (diff) | |
download | minetest-13dd7959dbb62ca9327062181d7c4f04b469c38c.tar.gz minetest-13dd7959dbb62ca9327062181d7c4f04b469c38c.tar.bz2 minetest-13dd7959dbb62ca9327062181d7c4f04b469c38c.zip |
Initialize facedir and wallmounted tables only once.
This makes the functions a bit faster since they don't
have to recreate the tables every invocation, and makes
the code more readable.
Also, document `wallmounted_to_dir`.
The function was implemented but not documented in `lua_api.txt`.
Diffstat (limited to 'builtin/game')
-rw-r--r-- | builtin/game/item.lua | 57 |
1 files changed, 29 insertions, 28 deletions
diff --git a/builtin/game/item.lua b/builtin/game/item.lua index c168bf096..c42aff5b0 100644 --- a/builtin/game/item.lua +++ b/builtin/game/item.lua @@ -88,25 +88,26 @@ function core.dir_to_facedir(dir, is6d) end end +-- Table of possible dirs +local facedir_to_dir = { + {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}, +} +-- Mapping from facedir value to index in facedir_to_dir. +local facedir_to_dir_map = { + [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, +} function core.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]] + return facedir_to_dir[facedir_to_dir_map[facedir]] end function core.dir_to_wallmounted(dir) @@ -131,17 +132,17 @@ function core.dir_to_wallmounted(dir) end end +-- table of dirs in wallmounted order +local wallmounted_to_dir = { + [0] = {x = 0, y = 1, z = 0}, + {x = 0, y = -1, z = 0}, + {x = 1, y = 0, z = 0}, + {x = -1, y = 0, z = 0}, + {x = 0, y = 0, z = 1}, + {x = 0, y = 0, z = -1}, +} function core.wallmounted_to_dir(wallmounted) - -- table of dirs in wallmounted order - return ({[0] = {x = 0, y = 1, z = 0}, - {x = 0, y = -1, z = 0}, - {x = 1, y = 0, z = 0}, - {x = -1, y = 0, z = 0}, - {x = 0, y = 0, z = 1}, - {x = 0, y = 0, z = -1}}) - - --indexed into by the wallmounted in question - [wallmounted] + return wallmounted_to_dir[wallmounted] end function core.get_node_drops(nodename, toolname) |