Skip to content

Commit 41c2b2a

Browse files
sofarparamat
authored andcommittedMar 18, 2016
Allow both sides of the bed to be digged up.
Tested with nodebreaker, fire. If called from lua, minetest.remove_node() calls on_destruct() callbacks before the map is actually updated. This means that we can't look at the map data to determine if we're done cleaning up adjacent nodes, and we have to stop recursing some other way. There's no data we can pass around through functions that would survive scope to a secondary on_destruct() callback, so we have to maintain local state somewhere in the mod namespace. In this case, we keep a bitflag. The bitflag is set to "true" by default. On the first half removal, the flag is flipped and afterwards we remove the other half node. When the on_destruct for the other half is running, it's value is false and we flip it back to true without removing the other half node. This thus prevents recursing. To facilitate easier finding of the bed partner, we tell our on_destruct whether we're a top or bottom half node through a passed flag. Now that the top is diggable, we just need to assure that it drops a bottom bed part.
1 parent cb57f70 commit 41c2b2a

File tree

1 file changed

+29
-8
lines changed

1 file changed

+29
-8
lines changed
 

Diff for: ‎mods/beds/api.lua

+29-8
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,27 @@
1+
2+
local reverse = true
3+
4+
local function destruct_bed(pos, n)
5+
local node = minetest.get_node(pos)
6+
local other
7+
8+
if n == 2 then
9+
local dir = minetest.facedir_to_dir(node.param2)
10+
other = vector.subtract(pos, dir)
11+
elseif n == 1 then
12+
local dir = minetest.facedir_to_dir(node.param2)
13+
other = vector.add(pos, dir)
14+
end
15+
16+
if reverse then
17+
reverse = not reverse
18+
minetest.remove_node(other)
19+
nodeupdate(other)
20+
else
21+
reverse = not reverse
22+
end
23+
end
24+
125
function beds.register_bed(name, def)
226
minetest.register_node(name .. "_bottom", {
327
description = def.description,
@@ -39,14 +63,7 @@ function beds.register_bed(name, def)
3963
end,
4064

4165
on_destruct = function(pos)
42-
local n = minetest.get_node_or_nil(pos)
43-
if not n then return end
44-
local dir = minetest.facedir_to_dir(n.param2)
45-
local p = vector.add(pos, dir)
46-
local n2 = minetest.get_node(p)
47-
if minetest.get_item_group(n2.name, "bed") == 2 and n.param2 == n2.param2 then
48-
minetest.remove_node(p)
49-
end
66+
destruct_bed(pos, 1)
5067
end,
5168

5269
on_rightclick = function(pos, node, clicker)
@@ -95,10 +112,14 @@ function beds.register_bed(name, def)
95112
pointable = false,
96113
groups = {snappy = 1, choppy = 2, oddly_breakable_by_hand = 2, flammable = 3, bed = 2},
97114
sounds = default.node_sound_wood_defaults(),
115+
drop = name .. "_bottom",
98116
node_box = {
99117
type = "fixed",
100118
fixed = def.nodebox.top,
101119
},
120+
on_destruct = function(pos)
121+
destruct_bed(pos, 2)
122+
end,
102123
})
103124

104125
minetest.register_alias(name, name .. "_bottom")

0 commit comments

Comments
 (0)
Please sign in to comment.