1 -- minetest/default/leafdecay.lua
3 -- To enable leaf decay for a node, add it to the "leafdecay" group.
5 -- The rating of the group determines how far from a node in the group "tree"
6 -- the node can be without decaying.
8 -- If param2 of the node is ~= 0, the node will always be preserved. Thus, if
9 -- the player places a node of that kind, you will want to set param2=1 or so.
11 default.leafdecay_trunk_cache = {}
12 default.leafdecay_enable_cache = true
14 minetest.register_abm({
15 nodenames = {"group:leafdecay"},
16 neighbors = {"air", "group:liquid"},
17 -- A low interval and a high inverse chance spreads the load
21 action = function(p0, node, _, _)
22 --print("leafdecay ABM at "..p0.x..", "..p0.y..", "..p0.z..")")
23 local do_preserve = false
24 local d = minetest.registered_nodes[node.name].groups.leafdecay
25 if not d or d == 0 then
26 --print("not groups.leafdecay")
29 local n0 = minetest.env:get_node(p0)
30 if n0.param2 ~= 0 then
31 --print("param2 ~= 0")
35 if default.leafdecay_enable_cache then
36 p0_hash = minetest.hash_node_position(p0)
37 local trunkp = default.leafdecay_trunk_cache[p0_hash]
39 local n = minetest.env:get_node(trunkp)
40 local reg = minetest.registered_nodes[n.name]
41 if reg.groups.tree and reg.groups.tree ~= 0 then
42 --print("cached trunk still exists")
45 --print("cached trunk is invalid")
47 table.remove(default.leafdecay_trunk_cache, p0_hash)
50 for dx = -d, d do if do_preserve then break end
51 for dy = -d, d do if do_preserve then break end
52 for dz = -d, d do if do_preserve then break end
58 local n = minetest.env:get_node(p)
59 local reg = minetest.registered_nodes[n.name]
60 if reg.groups.tree and reg.groups.tree ~= 0 then
62 if default.leafdecay_enable_cache then
63 --print("caching trunk")
65 default.leafdecay_trunk_cache[p0_hash] = p
71 if not do_preserve then
72 -- Drop stuff other than the node itself
73 itemstacks = minetest.get_node_drops(n0.name)
74 for _, itemname in ipairs(itemstacks) do
75 if itemname ~= n0.name then
77 x = p0.x - 0.5 + math.random(),
78 y = p0.y - 0.5 + math.random(),
79 z = p0.z - 0.5 + math.random(),
81 minetest.env:add_item(p_drop, itemname)
85 minetest.env:remove_node(p0)