Add age to of fire to greatly limit fire spread

This commit is contained in:
Wuzzy 2020-05-09 17:41:51 +02:00
parent d13498e6f9
commit bfb1e3e447

@ -40,6 +40,10 @@ local fire_death_messages = {
N("@1 died in a fire."), N("@1 died in a fire."),
} }
local fire_timer = function(pos)
minetest.get_node_timer(pos):start(math.random(3, 7))
end
minetest.register_node("mcl_fire:fire", { minetest.register_node("mcl_fire:fire", {
description = S("Fire"), description = S("Fire"),
_doc_items_longdesc = fire_help, _doc_items_longdesc = fire_help,
@ -71,37 +75,73 @@ minetest.register_node("mcl_fire:fire", {
end end
end, end,
on_timer = function(pos) on_timer = function(pos)
local airs = minetest.find_nodes_in_area({x=pos.x-1, y=pos.y-1, z=pos.z-1}, {x=pos.x+1, y=pos.y+4, z=pos.z+1}, {"air"}) local node = minetest.get_node(pos)
if (#airs == 0) or ((not fire_enabled) and math.random(1,3) == 1) then -- Age is a number from 0 to 15 and is increased every timer step.
-- "old" fire is more likely to be extinguished
local age = node.param2
local flammables = minetest.find_nodes_in_area({x=pos.x-1, y=pos.y-1, z=pos.z-1}, {x=pos.x+1, y=pos.y+4, z=pos.z+1}, {"group:flammable"})
local below = minetest.get_node({x=pos.x, y=pos.z-1, z=pos.z})
local below_is_flammable = minetest.get_item_group(below.name, "flammable") > 0
-- Extinguish fire
if (not fire_enabled) and (math.random(1,3) == 1) then
minetest.remove_node(pos) minetest.remove_node(pos)
return return
end end
if (not fire_enabled) then if age == 15 and not below_is_flammable then
-- Restart timer minetest.remove_node(pos)
minetest.get_node_timer(pos):start(math.random(3, 7)) return
elseif age > 3 and #flammables == 0 and not below_is_flammable and math.random(1,4) == 1 then
minetest.remove_node(pos)
return return
end end
local burned = false local age_add = 1
if math.random(1,2) == 1 then -- If fire spread is disabled, we have to skip the "destructive" code
while #airs > 0 do if (not fire_enabled) then
local r = math.random(1, #airs) if age + age_add <= 15 then
if minetest.find_node_near(airs[r], 1, {"group:flammable"}) then node.param2 = age + age_add
minetest.set_node(airs[r], {name="mcl_fire:fire"}) minetest.set_node(pos, node)
burned = true end
break -- Restart timer
else fire_timer(pos)
table.remove(airs, r) return
end
-- Spawn fire to nearby flammable nodes
local is_next_to_flammable = minetest.find_node_near(pos, 2, {"group:flammable"}) ~= nil
if is_next_to_flammable and math.random(1,2) == 1 then
-- The fire we spawn copies the age of this fire.
-- This prevents fire from spreading infinitely far as the fire fire dies off
-- quicker the further it has spreaded.
local age_next = math.min(15, age + math.random(0, 1))
-- Select random type of fire spread
local burntype = math.random(1,2)
if burntype == 1 then
-- Spawn fire in air
local nodes = minetest.find_nodes_in_area({x=pos.x-1, y=pos.y-1, z=pos.z-1}, {x=pos.x+1, y=pos.y+4, z=pos.z+1}, {"air"})
while #nodes > 0 do
local r = math.random(1, #nodes)
if minetest.find_node_near(nodes[r], 1, {"group:flammable"}) then
minetest.set_node(nodes[r], {name="mcl_fire:fire", param2 = age_next})
break
else
table.remove(nodes, r)
end
end
else
-- Replace flammable node with fire
local nodes = minetest.find_nodes_in_area({x=pos.x-1, y=pos.y-1, z=pos.z-1}, {x=pos.x+1, y=pos.y+4, z=pos.z+1}, {"group:flammable"})
if #nodes > 0 then
local r = math.random(1, #nodes)
minetest.set_node(nodes[r], {name="mcl_fire:fire", param2 = age_next})
end end
end end
end end
if not burned then -- Regular age increase
if math.random(1,3) == 1 then if age + age_add <= 15 then
minetest.remove_node(pos) node.param2 = age + age_add
return minetest.set_node(pos, node)
end
end end
-- Restart timer -- Restart timer
minetest.get_node_timer(pos):start(math.random(3, 7)) fire_timer(pos)
end, end,
drop = "", drop = "",
sounds = {}, sounds = {},
@ -119,7 +159,7 @@ minetest.register_node("mcl_fire:fire", {
mcl_portals.light_nether_portal(pos) mcl_portals.light_nether_portal(pos)
end end
minetest.get_node_timer(pos):start(math.random(3, 7)) fire_timer(pos)
end, end,
_mcl_blast_resistance = 0, _mcl_blast_resistance = 0,
}) })
@ -168,11 +208,11 @@ minetest.register_node("mcl_fire:eternal_fire", {
end end
end end
-- Restart timer -- Restart timer
minetest.get_node_timer(pos):start(math.random(3, 7)) fire_timer(pos)
end, end,
-- Start burning timer and light Nether portal (if possible) -- Start burning timer and light Nether portal (if possible)
on_construct = function(pos) on_construct = function(pos)
minetest.get_node_timer(pos):start(math.random(3, 7)) fire_timer(pos)
if minetest.get_modpath("mcl_portals") then if minetest.get_modpath("mcl_portals") then
mcl_portals.light_nether_portal(pos) mcl_portals.light_nether_portal(pos)
@ -369,30 +409,6 @@ else -- Fire enabled
end, end,
}) })
-- Turn flammable nodes into air
minetest.register_abm({
label = "Remove flammable node below fire",
nodenames = {"group:fire"},
neighbors = {"group:flammable"},
interval = 5,
chance = 18,
catch_up = false,
action = function(pos, node, active_object_count, active_object_count_wider)
local p = {x=pos.x, y=pos.y-1, z=pos.z}
local flammable_node = minetest.get_node(p)
local def = minetest.registered_nodes[flammable_node.name]
if def.on_burn then
def.on_burn(p)
else
local g = minetest.get_item_group(flammable_node.name, "flammable")
if g ~= -1 then
minetest.set_node(p, {name="air"})
minetest.check_for_falling(p)
end
end
end,
})
end end
-- Set pointed_thing on (normal) fire. -- Set pointed_thing on (normal) fire.