Skip to content

Commit 13dd795

Browse files
kaezaparamat
authored andcommittedFeb 11, 2016
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`.
1 parent 0f03547 commit 13dd795

File tree

2 files changed

+31
-28
lines changed

2 files changed

+31
-28
lines changed
 

‎builtin/game/item.lua

+29-28
Original file line numberDiff line numberDiff line change
@@ -88,25 +88,26 @@ function core.dir_to_facedir(dir, is6d)
8888
end
8989
end
9090

91+
-- Table of possible dirs
92+
local facedir_to_dir = {
93+
{x= 0, y=0, z= 1},
94+
{x= 1, y=0, z= 0},
95+
{x= 0, y=0, z=-1},
96+
{x=-1, y=0, z= 0},
97+
{x= 0, y=-1, z= 0},
98+
{x= 0, y=1, z= 0},
99+
}
100+
-- Mapping from facedir value to index in facedir_to_dir.
101+
local facedir_to_dir_map = {
102+
[0]=1, 2, 3, 4,
103+
5, 2, 6, 4,
104+
6, 2, 5, 4,
105+
1, 5, 3, 6,
106+
1, 6, 3, 5,
107+
1, 4, 3, 2,
108+
}
91109
function core.facedir_to_dir(facedir)
92-
--a table of possible dirs
93-
return ({{x=0, y=0, z=1},
94-
{x=1, y=0, z=0},
95-
{x=0, y=0, z=-1},
96-
{x=-1, y=0, z=0},
97-
{x=0, y=-1, z=0},
98-
{x=0, y=1, z=0}})
99-
100-
--indexed into by a table of correlating facedirs
101-
[({[0]=1, 2, 3, 4,
102-
5, 2, 6, 4,
103-
6, 2, 5, 4,
104-
1, 5, 3, 6,
105-
1, 6, 3, 5,
106-
1, 4, 3, 2})
107-
108-
--indexed into by the facedir in question
109-
[facedir]]
110+
return facedir_to_dir[facedir_to_dir_map[facedir]]
110111
end
111112

112113
function core.dir_to_wallmounted(dir)
@@ -131,17 +132,17 @@ function core.dir_to_wallmounted(dir)
131132
end
132133
end
133134

135+
-- table of dirs in wallmounted order
136+
local wallmounted_to_dir = {
137+
[0] = {x = 0, y = 1, z = 0},
138+
{x = 0, y = -1, z = 0},
139+
{x = 1, y = 0, z = 0},
140+
{x = -1, y = 0, z = 0},
141+
{x = 0, y = 0, z = 1},
142+
{x = 0, y = 0, z = -1},
143+
}
134144
function core.wallmounted_to_dir(wallmounted)
135-
-- table of dirs in wallmounted order
136-
return ({[0] = {x = 0, y = 1, z = 0},
137-
{x = 0, y = -1, z = 0},
138-
{x = 1, y = 0, z = 0},
139-
{x = -1, y = 0, z = 0},
140-
{x = 0, y = 0, z = 1},
141-
{x = 0, y = 0, z = -1}})
142-
143-
--indexed into by the wallmounted in question
144-
[wallmounted]
145+
return wallmounted_to_dir[wallmounted]
145146
end
146147

147148
function core.get_node_drops(nodename, toolname)

‎doc/lua_api.txt

+2
Original file line numberDiff line numberDiff line change
@@ -2153,6 +2153,8 @@ and `minetest.auth_reload` call the authetification handler.
21532153
* Convert a facedir back into a vector aimed directly out the "back" of a node
21542154
* `minetest.dir_to_wallmounted(dir)`
21552155
* Convert a vector to a wallmounted value, used for `paramtype2="wallmounted"`
2156+
* `minetest.wallmounted_to_dir(wallmounted)`
2157+
* Convert a wallmounted value back into a vector aimed directly out the "back" of a node
21562158
* `minetest.get_node_drops(nodename, toolname)`
21572159
* Returns list of item names.
21582160
* **Note**: This will be removed or modified in a future version.

0 commit comments

Comments
 (0)
Please sign in to comment.