TNT: Add on_blast to all nodes with an inventory
[oweals/minetest_game.git] / mods / default / functions.lua
index 29506c40b8d5ad6aae4c87d278ccdc33e1e78cb2..d0164cd6ac5c227f219877875582839595ea746d 100644 (file)
@@ -40,9 +40,9 @@ end
 function default.node_sound_sand_defaults(table)
        table = table or {}
        table.footstep = table.footstep or
-                       {name = "default_sand_footstep", gain = 0.2}
+                       {name = "default_sand_footstep", gain = 0.12}
        table.dug = table.dug or
-                       {name = "default_sand_footstep", gain = 0.4}
+                       {name = "default_sand_footstep", gain = 0.24}
        table.place = table.place or
                        {name = "default_place_node", gain = 1.0}
        default.node_sound_defaults(table)
@@ -110,6 +110,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
 --
@@ -213,16 +228,27 @@ function default.register_fence(name, def)
        -- Allow almost everything to be overridden
        local default_fields = {
                paramtype = "light",
-               drawtype = "fencelike",
+               drawtype = "nodebox",
+               node_box = {
+                       type = "connected",
+                       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}},
+               },
+               connects_to = {"group:fence", "group:wood", "group:tree"},
                inventory_image = fence_texture,
                wield_image = fence_texture,
-               tiles = { def.texture },
+               tiles = {def.texture},
                sunlight_propagates = true,
                is_ground_content = false,
-               selection_box = {
-                       type = "fixed",
-                       fixed = {-1/7, -1/2, -1/7, 1/7, 1/2, 1/7},
-               },
                groups = {},
        }
        for k, v in pairs(default_fields) do
@@ -342,38 +368,71 @@ minetest.register_abm({
 
 
 --
--- Grass growing on well-lit dirt
+-- Convert dirt to something that fits the environment
 --
 
 minetest.register_abm({
        nodenames = {"default:dirt"},
-       neighbors = {"air"},
+       neighbors = {
+               "default:dirt_with_grass",
+               "default:dirt_with_dry_grass",
+               "default:dirt_with_snow",
+               "group:grass",
+               "group:dry_grass",
+               "default:snow",
+       },
        interval = 6,
        chance = 67,
        catch_up = false,
        action = function(pos, node)
+               -- Most likely case, half the time it's too dark for this.
                local above = {x = pos.x, y = pos.y + 1, z = pos.z}
-               local name = minetest.get_node(above).name
-               local nodedef = minetest.registered_nodes[name]
-               if nodedef and (nodedef.sunlight_propagates or nodedef.paramtype == "light") and
-                               nodedef.liquidtype == "none" and
-                               (minetest.get_node_light(above) or 0) >= 13 then
-                       if name == "default:snow" or name == "default:snowblock" then
-                               minetest.set_node(pos, {name = "default:dirt_with_snow"})
-                       else
-                               minetest.set_node(pos, {name = "default:dirt_with_grass"})
+               if (minetest.get_node_light(above) or 0) < 13 then
+                       return
+               end
+
+               -- Look for likely neighbors.
+               local p2 = minetest.find_node_near(pos, 1, {"default:dirt_with_grass",
+                               "default:dirt_with_dry_grass", "default:dirt_with_snow"})
+               if p2 then
+                       -- But the node needs to be under air in this case.
+                       local n2 = minetest.get_node(above)
+                       if n2 and n2.name == "air" then
+                               local n3 = minetest.get_node(p2)
+                               minetest.set_node(pos, {name = n3.name})
+                               return
                        end
                end
+
+               -- Anything on top?
+               local n2 = minetest.get_node(above)
+               if not n2 then
+                       return
+               end
+
+               local name = n2.name
+               -- 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
 })
 
-
 --
 -- Grass and dry grass removed in darkness
 --
 
 minetest.register_abm({
-       nodenames = {"default:dirt_with_grass", "default:dirt_with_dry_grass"},
+       nodenames = {
+               "default:dirt_with_grass",
+               "default:dirt_with_dry_grass",
+               "default:dirt_with_snow",
+       },
        interval = 8,
        chance = 50,
        catch_up = false,