TNT: Add on_blast to all nodes with an inventory
[oweals/minetest_game.git] / mods / default / functions.lua
index bd55d32fee873a217b92a6fd8c2abd7faa76b68c..d0164cd6ac5c227f219877875582839595ea746d 100644 (file)
@@ -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
 --
@@ -353,32 +368,61 @@ 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
 --