-
Notifications
You must be signed in to change notification settings - Fork 0
/
magic_nodes.lua
146 lines (110 loc) · 2.97 KB
/
magic_nodes.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
--[[
Due to engine limitations with non-trivial technical hurdles, minetest
collision boxes can only occupy a 3x3x3 node space at most. To work
around this limitation, magic invisible undiggable nodes are added to
fill in the shape of a mesh. The nodes are tracked in meta and removed
on destruction.
]]
bitumen.magic = {}
-- used to create a large collision box because minetest doesn't allow ones bigger than 3x3x3
minetest.register_node("bitumen:collision_node", {
paramtype = "light",
drawtype = "airlike",
-- drawtype = "node",
-- tiles = {"default_leaves.png"},
walkable = true,
sunlight_propagates = true,
-- groups = {choppy = 1},
})
local function add(a, b)
return {
x = a.x + b[1],
y = a.y + b[2], -- wtf?
z = a.z + b[3]
}
end
bitumen.magic.set_nodes = function(pos, nodename, def)
local set = {}
for _,delta in ipairs(def) do
local p = add(pos, delta)
local n = minetest.get_node(p)
local g = minetest.registered_nodes[n.name].groups
if g and not g.bitumen_magic_proof then
-- print("magic node at ".. minetest.pos_to_string(p))
minetest.set_node(p, {name= nodename})
-- minetest.set_node(p, {name= "default:glass"})
-- save the parent node
local meta = minetest.get_meta(p)
meta:set_string("magic_parent", minetest.serialize(pos))
table.insert(set, p)
end
end
-- save positions for all the magic nodes
local meta = minetest.get_meta(pos)
local oldset = meta:get_string("magic_children") or ""
if oldset == "" then
oldset = {}
else
oldset = minetest.deserialize(oldset)
end
for _,p in ipairs(set) do
table.insert(oldset, p)
end
meta:set_string("magic_children", minetest.serialize(oldset))
end
bitumen.magic.set_collision_nodes = function(pos, def)
bitumen.magic.set_nodes(pos, "bitumen:collision_node", def)
end
bitumen.magic.gensphere = function(center, radius)
local out = {}
for x = -radius, radius do
for y = -radius, radius do
for z = -radius, radius do
if math.sqrt(x * x + y * y + z * z) <= radius then
table.insert(out, {center[1]+x, center[2]+y, center[3]+z})
end
end
end
end
return out
end
-- center is the base
bitumen.magic.gencylinder = function(center, radius, height)
local out = {}
for x = -radius, radius do
for z = -radius, radius do
if math.sqrt(x * x + z * z) <= radius then
for y = 0, height do
table.insert(out, {center[1]+x, center[2]+y, center[3]+z})
end
end
end
end
return out
end
bitumen.magic.gencube = function(low, high)
local out = {}
for x = low[1], high[1] do
for y = low[2], high[2] do
for z = low[3], high[3] do
table.insert(out, {x, y, z})
end
end
end
return out
end
bitumen.magic.on_destruct = function(pos)
local meta = minetest.get_meta(pos)
local magic = meta:get_string("magic_children")
if magic == nil then
return
end
magic = minetest.deserialize(magic)
if magic == nil then
return
end
-- clean up all the magic
for _,p in ipairs(magic) do
minetest.set_node(p, {name = "air"})
end
end