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 -- Assume ignore is a trunk, to make the thing work at the border of the active area
42 if n.name == "ignore" or (reg.groups.tree and reg.groups.tree ~= 0) then
43 --print("cached trunk still exists")
46 --print("cached trunk is invalid")
48 table.remove(default.leafdecay_trunk_cache, p0_hash)
51 for dx = -d, d do if do_preserve then break end
52 for dy = -d, d do if do_preserve then break end
53 for dz = -d, d do if do_preserve then break end
59 local n = minetest.env:get_node(p)
60 local reg = minetest.registered_nodes[n.name]
61 -- Assume ignore is a trunk, to make the thing work at the border of the active area
62 if n.name == "ignore" or (reg.groups.tree and reg.groups.tree ~= 0) then
64 if default.leafdecay_enable_cache then
65 --print("caching trunk")
67 default.leafdecay_trunk_cache[p0_hash] = p
73 if not do_preserve then
74 -- Drop stuff other than the node itself
75 itemstacks = minetest.get_node_drops(n0.name)
76 for _, itemname in ipairs(itemstacks) do
77 if itemname ~= n0.name then
79 x = p0.x - 0.5 + math.random(),
80 y = p0.y - 0.5 + math.random(),
81 z = p0.z - 0.5 + math.random(),
83 minetest.env:add_item(p_drop, itemname)
87 minetest.env:remove_node(p0)