Make grass buildable_to
[oweals/minetest_game.git] / mods / default / leafdecay.lua
index a01fe6168baab259aa706eeb939c6dcd158869e3..330bb33b8e17e4dd3a20eeb8099de94b307c35f6 100644 (file)
 
 default.leafdecay_trunk_cache = {}
 default.leafdecay_enable_cache = true
+-- Spread the load of finding trunks
+default.leafdecay_trunk_find_allow_accumulator = 0
+
+minetest.register_globalstep(function(dtime)
+       local finds_per_second = 5000
+       default.leafdecay_trunk_find_allow_accumulator =
+                       math.floor(dtime * finds_per_second)
+end)
 
 minetest.register_abm({
        nodenames = {"group:leafdecay"},
@@ -39,7 +47,7 @@ minetest.register_abm({
                                local n = minetest.env:get_node(trunkp)
                                local reg = minetest.registered_nodes[n.name]
                                -- Assume ignore is a trunk, to make the thing work at the border of the active area
-                               if n.name == "ignore" or (reg.groups.tree and reg.groups.tree ~= 0) then
+                               if n.name == "ignore" or (reg and reg.groups.tree and reg.groups.tree ~= 0) then
                                        --print("cached trunk still exists")
                                        return
                                end
@@ -48,27 +56,20 @@ minetest.register_abm({
                                table.remove(default.leafdecay_trunk_cache, p0_hash)
                        end
                end
-               for dx = -d, d do if do_preserve then break end
-               for dy = -d, d do if do_preserve then break end
-               for dz = -d, d do if do_preserve then break end
-                       local p = {
-                               x = p0.x + dx,
-                               y = p0.y + dy,
-                               z = p0.z + dz,
-                       }
-                       local n = minetest.env:get_node(p)
-                       local reg = minetest.registered_nodes[n.name]
-                       -- Assume ignore is a trunk, to make the thing work at the border of the active area
-                       if n.name == "ignore" or (reg.groups.tree and reg.groups.tree ~= 0) then
-                               do_preserve = true
-                               if default.leafdecay_enable_cache then
-                                       --print("caching trunk")
-                                       -- Cache the trunk
-                                       default.leafdecay_trunk_cache[p0_hash] = p
-                               end
-                       end
-               end
+               if default.leafdecay_trunk_find_allow_accumulator <= 0 then
+                       return
                end
+               default.leafdecay_trunk_find_allow_accumulator =
+                               default.leafdecay_trunk_find_allow_accumulator - 1
+               -- Assume ignore is a trunk, to make the thing work at the border of the active area
+               local p1 = minetest.env:find_node_near(p0, d, {"ignore", "group:tree"})
+               if p1 then
+                       do_preserve = true
+                       if default.leafdecay_enable_cache then
+                               --print("caching trunk")
+                               -- Cache the trunk
+                               default.leafdecay_trunk_cache[p0_hash] = p1
+                       end
                end
                if not do_preserve then
                        -- Drop stuff other than the node itself
@@ -85,6 +86,7 @@ minetest.register_abm({
                        end
                        -- Remove node
                        minetest.env:remove_node(p0)
+                       nodeupdate(p0)
                end
        end
 })