Fix leaf decay at borders of the active block area
[oweals/minetest_game.git] / mods / default / leafdecay.lua
1 -- minetest/default/leafdecay.lua
2
3 -- To enable leaf decay for a node, add it to the "leafdecay" group.
4 --
5 -- The rating of the group determines how far from a node in the group "tree"
6 -- the node can be without decaying.
7 --
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.
10
11 default.leafdecay_trunk_cache = {}
12 default.leafdecay_enable_cache = true
13
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
18         interval = 2,
19         chance = 5,
20
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")
27                         return
28                 end
29                 local n0 = minetest.env:get_node(p0)
30                 if n0.param2 ~= 0 then
31                         --print("param2 ~= 0")
32                         return
33                 end
34                 local p0_hash = nil
35                 if default.leafdecay_enable_cache then
36                         p0_hash = minetest.hash_node_position(p0)
37                         local trunkp = default.leafdecay_trunk_cache[p0_hash]
38                         if trunkp then
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")
44                                         return
45                                 end
46                                 --print("cached trunk is invalid")
47                                 -- Cache is invalid
48                                 table.remove(default.leafdecay_trunk_cache, p0_hash)
49                         end
50                 end
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
54                         local p = {
55                                 x = p0.x + dx,
56                                 y = p0.y + dy,
57                                 z = p0.z + dz,
58                         }
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
63                                 do_preserve = true
64                                 if default.leafdecay_enable_cache then
65                                         --print("caching trunk")
66                                         -- Cache the trunk
67                                         default.leafdecay_trunk_cache[p0_hash] = p
68                                 end
69                         end
70                 end
71                 end
72                 end
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
78                                         local p_drop = {
79                                                 x = p0.x - 0.5 + math.random(),
80                                                 y = p0.y - 0.5 + math.random(),
81                                                 z = p0.z - 0.5 + math.random(),
82                                         }
83                                         minetest.env:add_item(p_drop, itemname)
84                                 end
85                         end
86                         -- Remove node
87                         minetest.env:remove_node(p0)
88                 end
89         end
90 })
91