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
13 -- Spread the load of finding trunks
14 default.leafdecay_trunk_find_allow_accumulator = 0
16 minetest.register_globalstep(function(dtime)
17 local finds_per_second = 5000
18 default.leafdecay_trunk_find_allow_accumulator =
19 math.floor(dtime * finds_per_second)
22 minetest.register_abm({
23 nodenames = {"group:leafdecay"},
24 neighbors = {"air", "group:liquid"},
25 -- A low interval and a high inverse chance spreads the load
29 action = function(p0, node, _, _)
30 --print("leafdecay ABM at "..p0.x..", "..p0.y..", "..p0.z..")")
31 local do_preserve = false
32 local d = minetest.registered_nodes[node.name].groups.leafdecay
33 if not d or d == 0 then
34 --print("not groups.leafdecay")
37 local n0 = minetest.env:get_node(p0)
38 if n0.param2 ~= 0 then
39 --print("param2 ~= 0")
43 if default.leafdecay_enable_cache then
44 p0_hash = minetest.hash_node_position(p0)
45 local trunkp = default.leafdecay_trunk_cache[p0_hash]
47 local n = minetest.env:get_node(trunkp)
48 local reg = minetest.registered_nodes[n.name]
49 -- Assume ignore is a trunk, to make the thing work at the border of the active area
50 if n.name == "ignore" or (reg.groups.tree and reg.groups.tree ~= 0) then
51 --print("cached trunk still exists")
54 --print("cached trunk is invalid")
56 table.remove(default.leafdecay_trunk_cache, p0_hash)
59 if default.leafdecay_trunk_find_allow_accumulator <= 0 then
62 default.leafdecay_trunk_find_allow_accumulator =
63 default.leafdecay_trunk_find_allow_accumulator - 1
64 for dx = -d, d do if do_preserve then break end
65 for dy = -d, d do if do_preserve then break end
66 for dz = -d, d do if do_preserve then break end
72 local n = minetest.env:get_node(p)
73 local reg = minetest.registered_nodes[n.name]
74 -- Assume ignore is a trunk, to make the thing work at the border of the active area
75 if n.name == "ignore" or (reg.groups.tree and reg.groups.tree ~= 0) then
77 if default.leafdecay_enable_cache then
78 --print("caching trunk")
80 default.leafdecay_trunk_cache[p0_hash] = p
86 if not do_preserve then
87 -- Drop stuff other than the node itself
88 itemstacks = minetest.get_node_drops(n0.name)
89 for _, itemname in ipairs(itemstacks) do
90 if itemname ~= n0.name then
92 x = p0.x - 0.5 + math.random(),
93 y = p0.y - 0.5 + math.random(),
94 z = p0.z - 0.5 + math.random(),
96 minetest.env:add_item(p_drop, itemname)
100 minetest.env:remove_node(p0)