forked from HybridDog/replacer
-
Notifications
You must be signed in to change notification settings - Fork 3
/
replacer_patterns.lua
192 lines (170 loc) · 4.23 KB
/
replacer_patterns.lua
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
replacer.patterns = {}
local rp = replacer.patterns
local poshash = minetest.hash_node_position
-- cache results of minetest.get_node
replacer.patterns.known_nodes = {}
function replacer.patterns.get_node(pos)
local i = poshash(pos)
local node = rp.known_nodes[i]
if nil ~= node then
return node
end
node = minetest.get_node(pos)
rp.known_nodes[i] = node
return node
end
-- tests if there's a node at pos which should be replaced
function replacer.patterns.replaceable(pos, name, pname)
return (rp.get_node(pos).name == name) and (not minetest.is_protected(pos, pname))
end
replacer.patterns.translucent_nodes = {}
function replacer.patterns.node_translucent(name)
local is_translucent = rp.translucent_nodes[name]
if nil ~= is_translucent then
return is_translucent
end
local data = minetest.registered_nodes[name]
if data and ((not data.drawtype) or ("normal" == data.drawtype)) then
rp.translucent_nodes[name] = false
return false
end
rp.translucent_nodes[name] = true
return true
end
function replacer.patterns.field_position(pos, data)
return rp.replaceable(pos, data.name, data.pname)
and rp.node_translucent(
rp.get_node(vector.add(data.above, pos)).name) ~= data.right_clicked
end
replacer.patterns.offsets_touch = {
{ x =-1, y = 0, z = 0 },
{ x = 1, y = 0, z = 0 },
{ x = 0, y =-1, z = 0 },
{ x = 0, y = 1, z = 0 },
{ x = 0, y = 0, z =-1 },
{ x = 0, y = 0, z = 1 },
}
-- 3x3x3 hollow cube
replacer.patterns.offsets_hollowcube = {}
local p
for x = -1, 1 do
for y = -1, 1 do
for z = -1, 1 do
if (0 ~= x) or (0 ~= y) or (0 ~= z) then
p = { x = x, y = y, z = z }
rp.offsets_hollowcube[#rp.offsets_hollowcube + 1] = p
end
end
end
end
-- To get the crust, first nodes near it need to be collected
function replacer.patterns.crust_above_position(pos, data)
-- test if the node at pos is a translucent node and not part of the crust
local nd = rp.get_node(pos).name
if (nd == data.name) or (not rp.node_translucent(nd)) then
return false
end
-- test if a node of the crust is near pos
local p2
for i = 1, 26 do
p2 = rp.offsets_hollowcube[i]
if rp.replaceable(vector.add(pos, p2), data.name, data.pname) then
return true
end
end
return false
end
-- used to get nodes the crust belongs to
function replacer.patterns.crust_under_position(pos, data)
if not rp.replaceable(pos, data.name, data.pname) then
return false
end
local p2
for i = 1, 26 do
p2 = rp.offsets_hollowcube[i]
if data.aboves[poshash(vector.add(pos, p2))] then
return true
end
end
return false
end
-- extract the crust from the nodes the crust belongs to
function replacer.patterns.reduce_crust_ps(data)
local newps = {}
local n = 0
local p, p2
for i = 1, data.num do
p = data.ps[i]
for i = 1, 6 do
p2 = rp.offsets_touch[i]
if data.aboves[poshash(vector.add(p, p2))] then
n = n + 1
newps[n] = p
break
end
end
end
data.ps = newps
data.num = n
end
-- gets the air nodes touching the crust
function replacer.patterns.reduce_crust_above_ps(data)
local newps = {}
local n = 0
local p, p2
for i = 1, data.num do
p = data.ps[i]
if rp.replaceable(p, "air", data.pname) then
for i = 1, 6 do
p2 = rp.offsets_touch[i]
if rp.replaceable(vector.add(p, p2), data.name, data.pname) then
n = n + 1
newps[n] = p
break
end
end
end
end
data.ps = newps
data.num = n
end
function replacer.patterns.mantle_position(pos, data)
if not rp.replaceable(pos, data.name, data.pname) then
return false
end
for i = 1, 6 do
if rp.get_node(vector.add(pos, rp.offsets_touch[i])).name ~= data.name then
return true
end
end
return false
end
-- finds out positions using depth first search
function replacer.patterns.get_ps(pos, fdata, adps, max)
adps = adps or rp.offsets_touch
local tab = {}
local num = 0
local todo = { pos }
local ti = 1
local tab_avoid = {}
local p, i
while 0 ~= ti do
p = todo[ti]
ti = ti - 1
for _, p2 in pairs(adps) do
p2 = vector.add(p, p2)
i = poshash(p2)
if (not tab_avoid[i]) and fdata.func(p2, fdata) then
num = num + 1
tab[num] = p2
ti = ti + 1
todo[ti] = p2
tab_avoid[i] = true
if max and (num >= max) then
return tab, num, tab_avoid
end
end -- if
end -- for
end -- while
return tab, num, tab_avoid
end