Default: Prevent placing sapling if grown tree intersects protection
[oweals/minetest_game.git] / mods / default / functions.lua
index c0ea1f5b53f00d0faaf1a3e93bb938ccbe0ecdc4..a98d091f0dc38535b68e51ce196bc302eb30fabe 100644 (file)
@@ -49,6 +49,18 @@ function default.node_sound_sand_defaults(table)
        return table
 end
 
+function default.node_sound_gravel_defaults(table)
+       table = table or {}
+       table.footstep = table.footstep or
+                       {name = "default_gravel_footstep", gain = 0.5}
+       table.dug = table.dug or
+                       {name = "default_gravel_footstep", gain = 1.0}
+       table.place = table.place or
+                       {name = "default_place_node", gain = 1.0}
+       default.node_sound_defaults(table)
+       return table
+end
+
 function default.node_sound_wood_defaults(table)
        table = table or {}
        table.footstep = table.footstep or
@@ -110,6 +122,21 @@ minetest.register_abm({
 })
 
 
+--
+-- optimized helper to put all items in an inventory into a drops list
+--
+function default.get_inventory_drops(pos, inventory, drops)
+       local inv = minetest.get_meta(pos):get_inventory()
+       local n = #drops
+       for i = 1, inv:get_size(inventory) do
+               local stack = inv:get_stack(inventory, i)
+               if stack:get_count() > 0 then
+                       drops[n+1] = stack:to_table()
+                       n = n + 1
+               end
+       end
+end
+
 --
 -- Papyrus and cactus growing
 --
@@ -439,12 +466,58 @@ minetest.register_abm({
 --
 
 minetest.register_abm({
-       nodenames = {"default:cobble"},
+       nodenames = {"default:cobble", "stairs:slab_cobble", "stairs:stair_cobble"},
        neighbors = {"group:water"},
        interval = 16,
        chance = 200,
        catch_up = false,
        action = function(pos, node)
-               minetest.set_node(pos, {name = "default:mossycobble"})
+               if node.name == "default:cobble" then
+                       minetest.set_node(pos, {name = "default:mossycobble"})
+               elseif node.name == "stairs:slab_cobble" then
+                       minetest.set_node(pos, {name = "stairs:slab_mossycobble", param2 = node.param2})
+               elseif node.name == "stairs:stair_cobble" then
+                       minetest.set_node(pos, {name = "stairs:stair_mossycobble", param2 = node.param2})
+               end
        end
 })
+
+
+--
+-- Checks if specified volume intersects a protected volume
+--
+
+function default.intersects_protection(minp, maxp, player_name, interval)
+       -- 'interval' is the largest allowed interval for the 3D lattice of checks
+
+       -- Compute the optimal float step 'd' for each axis so that all corners and
+       -- borders are checked. 'd' will be smaller or equal to 'interval'.
+       -- Subtracting 1e-4 ensures that the max co-ordinate will be reached by the
+       -- for loop (which might otherwise not be the case due to rounding errors).
+       local d = {}
+       for _, c in pairs({"x", "y", "z"}) do
+               if maxp[c] > minp[c] then
+                       d[c] = (maxp[c] - minp[c]) / math.ceil((maxp[c] - minp[c]) / interval) - 1e-4
+               elseif maxp[c] == minp[c] then
+                       d[c] = 1 -- Any value larger than 0 to avoid division by zero
+               else -- maxp[c] < minp[c], print error and treat as protection intersected
+                       minetest.log("error", "maxp < minp in 'default.intersects_protection()'")
+                       return true
+               end
+       end
+
+       for zf = minp.z, maxp.z, d.z do
+               local z = math.floor(zf + 0.5)
+               for yf = minp.y, maxp.y, d.y do
+                       local y = math.floor(yf + 0.5)
+                       for xf = minp.x, maxp.x, d.x do
+                               local x = math.floor(xf + 0.5)
+                               if minetest.is_protected({x = x, y = y, z = z}, player_name) then
+                                       return true
+                               end
+                       end
+               end
+       end
+
+       return false
+end