Make TNT respect on_blast, implement on_blast for some nodes
authorWuzzy <almikes@aol.com>
Sat, 7 Mar 2015 07:32:00 +0000 (08:32 +0100)
committerNovatux <nathanael.courant@laposte.net>
Tue, 12 May 2015 14:32:52 +0000 (16:32 +0200)
Implemented nodes:
- Steel Door: Ignores explosion
- Locked Chest: Ignores explosion
- Fire: Ignores explosion
- TNT: Starts burning
- Burning TNT: Explodes immediately
- Gunpowder: Starts burning
- Burning Gunpowder: Ignores explosion

mods/default/nodes.lua
mods/doors/init.lua
mods/fire/init.lua
mods/tnt/init.lua

index d875531473849957cef2966f88c259fbfccbd6a9..ac7414b1212350b191bdae133fcb3b980869b597 100644 (file)
@@ -1341,6 +1341,7 @@ minetest.register_node("default:chest_locked", {
                        )
                end
        end,
+       on_blast = function() end,
 })
 
 
index 7c2cf1c7a2515e3eff212170560924db5cab03bd..74c7eec67990660f68e22f154c213c25ae8c161a 100644 (file)
@@ -108,6 +108,33 @@ function doors.register_door(name, def)
                end
        end
 
+       local function check_and_blast(pos, name)
+               local node = minetest.get_node(pos)
+               if node.name == name then
+                       minetest.remove_node(pos)
+               end
+       end
+
+       local function make_on_blast(base_name, door_type, other_door_type)
+               if def.only_placer_can_open then
+                       return function() end
+               else
+                       if door_type == "_b_1" or door_type == "_b_2" then
+                               return function(pos, intensity)
+                                       check_and_blast(pos, name..door_type)
+                                       pos.y = pos.y + 1
+                                       check_and_blast(pos, name..other_door_type)
+                               end
+                       elseif door_type == "_t_1" or door_type == "_t_2" then
+                               return function(pos, intensity)
+                                       check_and_blast(pos, name..door_type)
+                                       pos.y = pos.y - 1
+                                       check_and_blast(pos, name..other_door_type)
+                               end
+                       end
+               end
+       end
+
        local function on_rightclick(pos, dir, check_name, replace, replace_dir, params)
                pos.y = pos.y+dir
                if not minetest.get_node(pos).name == check_name then
@@ -173,7 +200,8 @@ function doors.register_door(name, def)
                
                can_dig = check_player_priv,
                sounds = def.sounds,
-               sunlight_propagates = def.sunlight
+               sunlight_propagates = def.sunlight,
+               on_blast = make_on_blast(name, "_b_1", "_t_1")
        })
 
        minetest.register_node(name.."_t_1", {
@@ -205,7 +233,8 @@ function doors.register_door(name, def)
                
                can_dig = check_player_priv,
                sounds = def.sounds,
-               sunlight_propagates = def.sunlight,
+               sunlight_propagates = def.sunlight,
+               on_blast = make_on_blast(name, "_t_1", "_b_1")
        })
 
        minetest.register_node(name.."_b_2", {
@@ -237,7 +266,8 @@ function doors.register_door(name, def)
                
                can_dig = check_player_priv,
                sounds = def.sounds,
-               sunlight_propagates = def.sunlight
+               sunlight_propagates = def.sunlight,
+               on_blast = make_on_blast(name, "_b_2", "_t_2")
        })
 
        minetest.register_node(name.."_t_2", {
@@ -269,7 +299,8 @@ function doors.register_door(name, def)
                
                can_dig = check_player_priv,
                sounds = def.sounds,
-               sunlight_propagates = def.sunlight
+               sunlight_propagates = def.sunlight,
+               on_blast = make_on_blast(name, "_t_2", "_b_2")
        })
 
 end
index 309c3988d591cee609818f1de32d598c4b8db100..20b1dd215a51653a731cf2788909a406dcf37bbd 100644 (file)
@@ -24,6 +24,9 @@ minetest.register_node("fire:basic_flame", {
        on_destruct = function(pos)
                minetest.after(0, fire.on_flame_remove_at, pos)
        end,
+
+       -- unaffected by explosions
+       on_blast = function() end,
 })
 
 fire.D = 6
index fda4ec399dc5d15524ce6684a3b53890675f165f..dfe0a2301b5155da6ed38a122426e1f6fbf30c8a 100644 (file)
@@ -23,6 +23,7 @@ minetest.after(0, function()
                        name = name,
                        drops = def.drops,
                        flammable = def.groups.flammable,
+                       on_blast = def.on_blast,
                }
        end
 end)
@@ -82,7 +83,13 @@ local function destroy(drops, pos, cid)
        if def and def.flammable then
                minetest.set_node(pos, fire_node)
        else
-               minetest.remove_node(pos)
+               local on_blast = def.on_blast
+               if on_blast ~= nil then
+                       on_blast(pos, 1)
+                       return
+               else
+                       minetest.remove_node(pos)
+               end
                if def then
                        local node_drops = minetest.get_node_drops(def.name, "")
                        for _, item in ipairs(node_drops) do
@@ -172,12 +179,6 @@ local function explode(pos, radius)
        local p = {}
 
        local c_air = minetest.get_content_id("air")
-       local c_tnt = minetest.get_content_id("tnt:tnt")
-       local c_tnt_burning = minetest.get_content_id("tnt:tnt_burning")
-       local c_gunpowder = minetest.get_content_id("tnt:gunpowder")
-       local c_gunpowder_burning = minetest.get_content_id("tnt:gunpowder_burning")
-       local c_boom = minetest.get_content_id("tnt:boom")
-       local c_fire = minetest.get_content_id("fire:basic_flame")
 
        for z = -radius, radius do
        for y = -radius, radius do
@@ -189,13 +190,7 @@ local function explode(pos, radius)
                        p.x = pos.x + x
                        p.y = pos.y + y
                        p.z = pos.z + z
-                       if cid == c_tnt or cid == c_gunpowder then
-                               burn(p)
-                       elseif cid ~= c_tnt_burning and
-                                       cid ~= c_gunpowder_burning and
-                                       cid ~= c_air and
-                                       cid ~= c_fire and
-                                       cid ~= c_boom then
+                       if cid ~= c_air then
                                destroy(drops, p, cid)
                        end
                end
@@ -231,6 +226,9 @@ minetest.register_node("tnt:tnt", {
                        minetest.get_node_timer(pos):start(4)
                end
        end,
+       on_blast = function(pos, intensity)
+               burn(pos)
+       end,
        mesecons = {effector = {action_on = boom}},
 })
 
@@ -250,6 +248,8 @@ minetest.register_node("tnt:tnt_burning", {
        drop = "",
        sounds = default.node_sound_wood_defaults(),
        on_timer = boom,
+       -- unaffected by explosions
+       on_blast = function() end,
 })
 
 minetest.register_node("tnt:boom", {
@@ -262,6 +262,8 @@ minetest.register_node("tnt:boom", {
        on_timer = function(pos, elapsed)
                minetest.remove_node(pos)
        end,
+       -- unaffected by explosions
+       on_blast = function() end,
 })
 
 minetest.register_node("tnt:gunpowder", {
@@ -285,6 +287,9 @@ minetest.register_node("tnt:gunpowder", {
                        burn(pos)
                end
        end,
+       on_blast = function(pos, intensity)
+               burn(pos)
+       end,
 })
 
 minetest.register_node("tnt:gunpowder_burning", {
@@ -324,7 +329,9 @@ minetest.register_node("tnt:gunpowder_burning", {
                end
                end
                minetest.remove_node(pos)
-       end
+       end,
+       -- unaffected by explosions
+       on_blast = function() end,
 })
 
 minetest.register_abm({