-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapi.lua
More file actions
208 lines (172 loc) · 5.68 KB
/
api.lua
File metadata and controls
208 lines (172 loc) · 5.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
--- API Methods
--
-- @topic api
--- Get new rotation for that is opened or closed.
--
-- @local
-- @tparam int old_rot Previous node rotation.
-- @tparam string paramtype2 Node paramtype2.
-- @tparam[opt] bool open Whether door is being opened or closed (default: true).
-- @tparam[opt] bool inward Whether door opens inward or outward (default: true).
-- @tparam[opt] bool invert If `true`, door swings in opposite direction.
-- @treturn int New rotation.
local get_door_rotation = function(old_rot, paramtype2, open, inward, invert)
-- only facedir is supported
if not string.find(paramtype2, "facedir") then return end
local color_info = core.strip_param2_color(old_rot, paramtype2)
if color_info then
old_rot = old_rot - color_info
end
open = open ~= false
inward = inward ~= false
invert = invert == true
local new_rot = old_rot
if (not invert and ((open and inward) or (not open and not inward))) or
(invert and ((not open and inward) or (open and not inward))) then
new_rot = new_rot-1
if new_rot < 0 then
new_rot = 3
end
else
new_rot = new_rot+1
if new_rot > 3 then
new_rot = 0
end
end
if color_info then
new_rot = new_rot + color_info
end
return new_rot
end
local get_paramtype2 = function(node)
local node_def = core.registered_nodes[node.name]
if node_def then
return node_def.paramtype2
end
end
--- Helper method for inward opening door-like nodes.
--
-- @function simple_models:door_inward_open
-- @tparam vector pos Position of node.
-- @tparam string new_node Technical name of node replacement.
-- @tparam bool invert If `true`, door swings in opposite direction (right instead of left).
simple_models.door_inward_open = function(self, pos, new_node, invert)
local node = core.get_node_or_nil(pos)
if not node then return end
local new_rot = get_door_rotation(node.param2, get_paramtype2(node), true, true, invert)
if not new_rot then return end
core.swap_node(pos, {
name = new_node,
param1 = node.param1,
param2 = new_rot,
})
end
--- Helper method for inward closing door-like nodes.
--
-- @function simple_models:door_inward_close
-- @tparam vector pos Position of node.
-- @tparam string new_node Technical name of node replacement.
-- @tparam bool invert If `true`, door swings in opposite direction (right instead of left).
simple_models.door_inward_close = function(self, pos, new_node, invert)
local node = core.get_node_or_nil(pos)
if not node then return end
local new_rot = get_door_rotation(node.param2, get_paramtype2(node), false, true, invert)
if not new_rot then return end
core.swap_node(pos, {
name = new_node,
param1 = node.param1,
param2 = new_rot,
})
end
local get_pos_front = function(pos, param2, paramtype2)
local new_pos = table.copy(pos)
local rot = param2
local color_info = core.strip_param2_color(param2, paramtype2)
if color_info then
rot = rot - color_info
end
if rot == 0 then
new_pos.z = new_pos.z-1
elseif rot == 2 then
new_pos.z = new_pos.z+1
elseif rot == 1 then
new_pos.x = new_pos.x-1
elseif rot == 3 then
new_pos.x = new_pos.x+1
end
return new_pos
end
local get_pos_behind = function(pos, param2, paramtype2, invert)
local new_pos = table.copy(pos)
local rot = param2
local color_info = core.strip_param2_color(param2, paramtype2)
if color_info then
rot = rot - color_info
end
local addto = 1
if invert then
addto = -addto
end
if rot == 0 then
new_pos.x = new_pos.x - addto
elseif rot == 2 then
new_pos.x = new_pos.x + addto
elseif rot == 1 then
new_pos.z = new_pos.z + addto
elseif rot == 3 then
new_pos.z = new_pos.z - addto
end
return new_pos
end
--- Helper method for outward opening door-like nodes.
--
-- @function simple_models:door_outward_open
-- @tparam vector pos Position of node.
-- @tparam string new_node Technical name of node replacement.
-- @tparam bool invert If `true`, door swings in opposite direction (right instead of left).
simple_models.door_outward_open = function(self, pos, new_node, invert)
local node = core.get_node_or_nil(pos)
if not node then return end
local paramtype2 = get_paramtype2(node)
local new_pos = get_pos_front(pos, node.param2, paramtype2)
local blocker = core.get_node(new_pos)
-- something is blocking door or new_pos is same as old
if blocker and blocker.name ~= "air" then return end
local new_rot = get_door_rotation(node.param2, paramtype2, true, false, invert)
if not new_rot then return end
local old_meta_table = core.get_meta(pos):to_table()
core.remove_node(pos)
core.set_node(new_pos, {
name = new_node,
param1 = node.param1,
param2 = new_rot,
})
-- transfer meta to new pos
core.get_meta(new_pos):from_table(old_meta_table)
end
--- Helper method for outward closing door-like nodes.
--
-- @function simple_models:door_outward_close
-- @tparam vector pos Position of node.
-- @tparam string new_node Technical name of node replacement.
-- @tparam bool invert If `true`, door swings in opposite direction (right instead of left).
simple_models.door_outward_close = function(self, pos, new_node, invert)
local node = core.get_node_or_nil(pos)
if not node then return end
local paramtype2 = get_paramtype2(node)
local new_pos = get_pos_behind(pos, node.param2, paramtype2, invert)
local blocker = core.get_node(new_pos)
-- something is blocking door or new_pos is same as old
if blocker and blocker.name ~= "air" then return end
local new_rot = get_door_rotation(node.param2, paramtype2, false, false, invert)
if not new_rot then return end
local old_meta_table = core.get_meta(pos):to_table()
core.remove_node(pos)
core.set_node(new_pos, {
name = new_node,
param1 = node.param1,
param2 = new_rot,
})
-- transfer meta to new pos
core.get_meta(new_pos):from_table(old_meta_table)
end