Always disable leafdecay for player-placed leaves, even when 'sneaking'
[oweals/minetest_game.git] / mods / default / functions.lua
index 3bda4bf036f487d09bf73b845ad50442aa0c2638..91f8bb3912b6693ba52b9148cf5b2a6327aaaef1 100644 (file)
@@ -1,5 +1,3 @@
--- mods/default/functions.lua
-
 --
 -- Sounds
 --
@@ -77,8 +75,6 @@ function default.node_sound_leaves_defaults(table)
                        {name = "default_grass_footstep", gain = 0.45}
        table.dug = table.dug or
                        {name = "default_grass_footstep", gain = 0.7}
-       table.dig = table.dig or
-                       {name = "default_dig_crumbly", gain = 0.4}
        table.place = table.place or
                        {name = "default_place_node", gain = 1.0}
        default.node_sound_defaults(table)
@@ -119,6 +115,21 @@ function default.node_sound_water_defaults(table)
        return table
 end
 
+function default.node_sound_snow_defaults(table)
+       table = table or {}
+       table.footstep = table.footstep or
+                       {name = "default_snow_footstep", gain = 0.2}
+       table.dig = table.dig or
+                       {name = "default_snow_footstep", gain = 0.3}
+       table.dug = table.dug or
+                       {name = "default_snow_footstep", gain = 0.3}
+       table.place = table.place or
+                       {name = "default_place_node", gain = 1.0}
+       default.node_sound_defaults(table)
+       return table
+end
+
+
 --
 -- Lavacooling
 --
@@ -133,21 +144,23 @@ default.cool_lava = function(pos, node)
                {pos = pos, max_hear_distance = 16, gain = 0.25})
 end
 
-minetest.register_abm({
-       label = "Lava cooling",
-       nodenames = {"default:lava_source", "default:lava_flowing"},
-       neighbors = {"group:cools_lava", "group:water"},
-       interval = 1,
-       chance = 1,
-       catch_up = false,
-       action = function(...)
-               default.cool_lava(...)
-       end,
-})
+if minetest.settings:get_bool("enable_lavacooling") ~= false then
+       minetest.register_abm({
+               label = "Lava cooling",
+               nodenames = {"default:lava_source", "default:lava_flowing"},
+               neighbors = {"group:cools_lava", "group:water"},
+               interval = 2,
+               chance = 2,
+               catch_up = false,
+               action = function(...)
+                       default.cool_lava(...)
+               end,
+       })
+end
 
 
 --
--- optimized helper to put all items in an inventory into a drops list
+-- Optimized helper to put all items in an inventory into a drops list
 --
 
 function default.get_inventory_drops(pos, inventory, drops)
@@ -162,11 +175,12 @@ function default.get_inventory_drops(pos, inventory, drops)
        end
 end
 
+
 --
 -- Papyrus and cactus growing
 --
 
--- wrapping the functions in abm action is necessary to make overriding them possible
+-- Wrapping the functions in ABM action is necessary to make overriding them possible
 
 function default.grow_cactus(pos, node)
        if node.param2 >= 4 then
@@ -186,6 +200,9 @@ function default.grow_cactus(pos, node)
        if height == 4 or node.name ~= "air" then
                return
        end
+       if minetest.get_node_light(pos) < 13 then
+               return
+       end
        minetest.set_node(pos, {name = "default:cactus"})
        return true
 end
@@ -209,6 +226,9 @@ function default.grow_papyrus(pos, node)
        if height == 4 or node.name ~= "air" then
                return
        end
+       if minetest.get_node_light(pos) < 13 then
+               return
+       end
        minetest.set_node(pos, {name = "default:papyrus"})
        return true
 end
@@ -237,7 +257,7 @@ minetest.register_abm({
 
 
 --
--- dig upwards
+-- Dig upwards
 --
 
 function default.dig_up(pos, node, digger)
@@ -253,6 +273,7 @@ end
 --
 -- Fence registration helper
 --
+local fence_collision_extra = minetest.settings:get_bool("enable_fence_tall") and 3/8 or 0
 
 function default.register_fence(name, def)
        minetest.register_craft({
@@ -271,19 +292,29 @@ function default.register_fence(name, def)
                drawtype = "nodebox",
                node_box = {
                        type = "connected",
-                       fixed = {{-1/8, -1/2, -1/8, 1/8, 1/2, 1/8}},
+                       fixed = {-1/8, -1/2, -1/8, 1/8, 1/2, 1/8},
                        -- connect_top =
                        -- connect_bottom =
-                       connect_front = {{-1/16,3/16,-1/2,1/16,5/16,-1/8},
-                               {-1/16,-5/16,-1/2,1/16,-3/16,-1/8}},
-                       connect_left = {{-1/2,3/16,-1/16,-1/8,5/16,1/16},
-                               {-1/2,-5/16,-1/16,-1/8,-3/16,1/16}},
-                       connect_back = {{-1/16,3/16,1/8,1/16,5/16,1/2},
-                               {-1/16,-5/16,1/8,1/16,-3/16,1/2}},
-                       connect_right = {{1/8,3/16,-1/16,1/2,5/16,1/16},
-                               {1/8,-5/16,-1/16,1/2,-3/16,1/16}},
+                       connect_front = {{-1/16,  3/16, -1/2,   1/16,  5/16, -1/8 },
+                                        {-1/16, -5/16, -1/2,   1/16, -3/16, -1/8 }},
+                       connect_left =  {{-1/2,   3/16, -1/16, -1/8,   5/16,  1/16},
+                                        {-1/2,  -5/16, -1/16, -1/8,  -3/16,  1/16}},
+                       connect_back =  {{-1/16,  3/16,  1/8,   1/16,  5/16,  1/2 },
+                                        {-1/16, -5/16,  1/8,   1/16, -3/16,  1/2 }},
+                       connect_right = {{ 1/8,   3/16, -1/16,  1/2,   5/16,  1/16},
+                                        { 1/8,  -5/16, -1/16,  1/2,  -3/16,  1/16}}
                },
-               connects_to = {"group:fence", "group:wood", "group:tree"},
+               collision_box = {
+                       type = "connected",
+                       fixed = {-1/8, -1/2, -1/8, 1/8, 1/2 + fence_collision_extra, 1/8},
+                       -- connect_top =
+                       -- connect_bottom =
+                       connect_front = {-1/8, -1/2, -1/2,  1/8, 1/2 + fence_collision_extra, -1/8},
+                       connect_left =  {-1/2, -1/2, -1/8, -1/8, 1/2 + fence_collision_extra,  1/8},
+                       connect_back =  {-1/8, -1/2,  1/8,  1/8, 1/2 + fence_collision_extra,  1/2},
+                       connect_right = { 1/8, -1/2, -1/8,  1/2, 1/2 + fence_collision_extra,  1/8}
+               },
+               connects_to = {"group:fence", "group:wood", "group:tree", "group:wall"},
                inventory_image = fence_texture,
                wield_image = fence_texture,
                tiles = {def.texture},
@@ -292,7 +323,76 @@ function default.register_fence(name, def)
                groups = {},
        }
        for k, v in pairs(default_fields) do
-               if not def[k] then
+               if def[k] == nil then
+                       def[k] = v
+               end
+       end
+
+       -- Always add to the fence group, even if no group provided
+       def.groups.fence = 1
+
+       def.texture = nil
+       def.material = nil
+
+       minetest.register_node(name, def)
+end
+
+
+--
+-- Fence rail registration helper
+--
+
+function default.register_fence_rail(name, def)
+       minetest.register_craft({
+               output = name .. " 16",
+               recipe = {
+                       { def.material, def.material },
+                       { "", ""},
+                       { def.material, def.material },
+               }
+       })
+
+       local fence_rail_texture = "default_fence_rail_overlay.png^" .. def.texture ..
+                       "^default_fence_rail_overlay.png^[makealpha:255,126,126"
+       -- Allow almost everything to be overridden
+       local default_fields = {
+               paramtype = "light",
+               drawtype = "nodebox",
+               node_box = {
+                       type = "connected",
+                       fixed = {{-1/16,  3/16, -1/16, 1/16,  5/16, 1/16},
+                                {-1/16, -3/16, -1/16, 1/16, -5/16, 1/16}},
+                       -- connect_top =
+                       -- connect_bottom =
+                       connect_front = {{-1/16,  3/16, -1/2,   1/16,  5/16, -1/16},
+                                        {-1/16, -5/16, -1/2,   1/16, -3/16, -1/16}},
+                       connect_left =  {{-1/2,   3/16, -1/16, -1/16,  5/16,  1/16},
+                                        {-1/2,  -5/16, -1/16, -1/16, -3/16,  1/16}},
+                       connect_back =  {{-1/16,  3/16,  1/16,  1/16,  5/16,  1/2 },
+                                        {-1/16, -5/16,  1/16,  1/16, -3/16,  1/2 }},
+                       connect_right = {{ 1/16,  3/16, -1/16,  1/2,   5/16,  1/16},
+                                        { 1/16, -5/16, -1/16,  1/2,  -3/16,  1/16}}
+               },
+               collision_box = {
+                       type = "connected",
+                       fixed = {-1/8, -1/2, -1/8, 1/8, 1/2 + fence_collision_extra, 1/8},
+                       -- connect_top =
+                       -- connect_bottom =
+                       connect_front = {-1/8, -1/2, -1/2,  1/8, 1/2 + fence_collision_extra, -1/8},
+                       connect_left =  {-1/2, -1/2, -1/8, -1/8, 1/2 + fence_collision_extra,  1/8},
+                       connect_back =  {-1/8, -1/2,  1/8,  1/8, 1/2 + fence_collision_extra,  1/2},
+                       connect_right = { 1/8, -1/2, -1/8,  1/2, 1/2 + fence_collision_extra,  1/8}
+               },
+               connects_to = {"group:fence", "group:wall"},
+               inventory_image = fence_rail_texture,
+               wield_image = fence_rail_texture,
+               tiles = {def.texture},
+               sunlight_propagates = true,
+               is_ground_content = false,
+               groups = {},
+       }
+       for k, v in pairs(default_fields) do
+               if def[k] == nil then
                        def[k] = v
                end
        end
@@ -314,53 +414,72 @@ end
 -- Prevent decay of placed leaves
 
 default.after_place_leaves = function(pos, placer, itemstack, pointed_thing)
-       if placer and not placer:get_player_control().sneak then
+       if placer and placer:is_player() then
                local node = minetest.get_node(pos)
                node.param2 = 1
                minetest.set_node(pos, node)
        end
 end
 
--- Leafdecay ABM
-
-minetest.register_abm({
-       label = "Leaf decay",
-       nodenames = {"group:leafdecay"},
-       neighbors = {"air"},
-       interval = 2,
-       chance = 10,
-       catch_up = false,
-
-       action = function(pos, node, _, _)
-               -- Check if leaf is placed
-               if node.param2 ~= 0 then
-                       return
+-- Leafdecay
+local function leafdecay_after_destruct(pos, oldnode, def)
+       for _, v in pairs(minetest.find_nodes_in_area(vector.subtract(pos, def.radius),
+                       vector.add(pos, def.radius), def.leaves)) do
+               local node = minetest.get_node(v)
+               local timer = minetest.get_node_timer(v)
+               if node.param2 ~= 1 and not timer:is_started() then
+                       timer:start(math.random(20, 120) / 10)
                end
+       end
+end
 
-               local rad = minetest.registered_nodes[node.name].groups.leafdecay
-               -- Assume ignore is a trunk, to make this
-               -- work at the border of a loaded area
-               if minetest.find_node_near(pos, rad, {"ignore", "group:tree"}) then
-                       return
-               end
-               -- Drop stuff
-               local itemstacks = minetest.get_node_drops(node.name)
-               for _, itemname in ipairs(itemstacks) do
-                       if itemname ~= node.name or
-                                       minetest.get_item_group(node.name, "leafdecay_drop") ~= 0 then
-                               local p_drop = {
-                                       x = pos.x - 0.5 + math.random(),
-                                       y = pos.y - 0.5 + math.random(),
-                                       z = pos.z - 0.5 + math.random(),
-                               }
-                               minetest.add_item(p_drop, itemname)
+local function leafdecay_on_timer(pos, def)
+       if minetest.find_node_near(pos, def.radius, def.trunks) then
+               return false
+       end
+
+       local node = minetest.get_node(pos)
+       local drops = minetest.get_node_drops(node.name)
+       for _, item in ipairs(drops) do
+               local is_leaf
+               for _, v in pairs(def.leaves) do
+                       if v == item then
+                               is_leaf = true
                        end
                end
-               -- Remove node
-               minetest.remove_node(pos)
-               minetest.check_for_falling(pos)
+               if minetest.get_item_group(item, "leafdecay_drop") ~= 0 or
+                               not is_leaf then
+                       minetest.add_item({
+                               x = pos.x - 0.5 + math.random(),
+                               y = pos.y - 0.5 + math.random(),
+                               z = pos.z - 0.5 + math.random(),
+                       }, item)
+               end
        end
-})
+
+       minetest.remove_node(pos)
+       minetest.check_for_falling(pos)
+end
+
+function default.register_leafdecay(def)
+       assert(def.leaves)
+       assert(def.trunks)
+       assert(def.radius)
+       for _, v in pairs(def.trunks) do
+               minetest.override_item(v, {
+                       after_destruct = function(pos, oldnode)
+                               leafdecay_after_destruct(pos, oldnode, def)
+                       end,
+               })
+       end
+       for _, v in pairs(def.leaves) do
+               minetest.override_item(v, {
+                       on_timer = function(pos)
+                               leafdecay_on_timer(pos, def)
+                       end,
+               })
+       end
+end
 
 
 --
@@ -373,7 +492,6 @@ minetest.register_abm({
        neighbors = {
                "air",
                "group:grass",
-               "group:dry_grass",
                "default:snow",
        },
        interval = 6,
@@ -400,11 +518,8 @@ minetest.register_abm({
                -- Snow check is cheapest, so comes first
                if name == "default:snow" then
                        minetest.set_node(pos, {name = "default:dirt_with_snow"})
-               -- Most likely case first
                elseif minetest.get_item_group(name, "grass") ~= 0 then
                        minetest.set_node(pos, {name = "default:dirt_with_grass"})
-               elseif minetest.get_item_group(name, "dry_grass") ~= 0 then
-                       minetest.set_node(pos, {name = "default:dirt_with_dry_grass"})
                end
        end
 })
@@ -416,7 +531,7 @@ minetest.register_abm({
 
 minetest.register_abm({
        label = "Grass covered",
-       nodenames = {"group:spreading_dirt_type"},
+       nodenames = {"group:spreading_dirt_type", "default:dry_dirt_with_dry_grass"},
        interval = 8,
        chance = 50,
        catch_up = false,
@@ -427,7 +542,11 @@ minetest.register_abm({
                if name ~= "ignore" and nodedef and not ((nodedef.sunlight_propagates or
                                nodedef.paramtype == "light") and
                                nodedef.liquidtype == "none") then
-                       minetest.set_node(pos, {name = "default:dirt"})
+                       if node.name == "default:dry_dirt_with_dry_grass" then
+                               minetest.set_node(pos, {name = "default:dry_dirt"})
+                       else
+                               minetest.set_node(pos, {name = "default:dirt"})
+                       end
                end
        end
 })
@@ -437,78 +556,70 @@ minetest.register_abm({
 -- Moss growth on cobble near water
 --
 
+local moss_correspondences = {
+       ["default:cobble"] = "default:mossycobble",
+       ["stairs:slab_cobble"] = "stairs:slab_mossycobble",
+       ["stairs:stair_cobble"] = "stairs:stair_mossycobble",
+       ["stairs:stair_inner_cobble"] = "stairs:stair_inner_mossycobble",
+       ["stairs:stair_outer_cobble"] = "stairs:stair_outer_mossycobble",
+       ["walls:cobble"] = "walls:mossycobble",
+}
 minetest.register_abm({
        label = "Moss growth",
-       nodenames = {"default:cobble", "stairs:slab_cobble", "stairs:stair_cobble", "walls:cobble"},
+       nodenames = {"default:cobble", "stairs:slab_cobble", "stairs:stair_cobble",
+               "stairs:stair_inner_cobble", "stairs:stair_outer_cobble",
+               "walls:cobble"},
        neighbors = {"group:water"},
        interval = 16,
        chance = 200,
        catch_up = false,
        action = function(pos, node)
-               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})
-               elseif node.name == "walls:cobble" then
-                       minetest.set_node(pos, {name = "walls:mossycobble", param2 = node.param2})
+               node.name = moss_correspondences[node.name]
+               if node.name then
+                       minetest.set_node(pos, node)
                end
        end
 })
 
 
 --
--- Checks if specified volume intersects a protected volume
+-- NOTICE: This method is not an official part of the API yet.
+-- This method may change in future.
 --
 
-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()'")
+function default.can_interact_with_node(player, pos)
+       if player and player:is_player() then
+               if minetest.check_player_privs(player, "protection_bypass") then
                        return true
                end
+       else
+               return false
+       end
+
+       local meta = minetest.get_meta(pos)
+       local owner = meta:get_string("owner")
+
+       if not owner or owner == "" or owner == player:get_player_name() then
+               return true
        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
+       -- Is player wielding the right key?
+       local item = player:get_wielded_item()
+       if item:get_name() == "default:key" then
+               local key_meta = item:get_meta()
+
+               if key_meta:get_string("secret") == "" then
+                       local key_oldmeta = item:get_metadata()
+                       if key_oldmeta == "" or not minetest.parse_json(key_oldmeta) then
+                               return false
                        end
+
+                       key_meta:set_string("secret", minetest.parse_json(key_oldmeta).secret)
+                       item:set_metadata("")
                end
+
+               return meta:get_string("key_lock_secret") == key_meta:get_string("secret")
        end
 
        return false
 end
-
-
---
--- Coral death near air
---
-
-minetest.register_abm({
-       nodenames = {"default:coral_brown", "default:coral_orange"},
-       neighbors = {"air"},
-       interval = 17,
-       chance = 5,
-       catch_up = false,
-       action = function(pos, node)
-               minetest.set_node(pos, {name = "default:coral_skeleton"})
-       end,
-})