improve mcl_fire

cache functions used in ABM/globalsteps
remove depends.txt
This commit is contained in:
AFCMS 2021-03-15 01:10:33 +01:00
parent c8a7ca729e
commit fe6b7cb89b
5 changed files with 71 additions and 50 deletions

View File

@ -1,5 +0,0 @@
mcl_core
mcl_worlds
mcl_sounds
mcl_particles
mcl_portals?

View File

@ -1,5 +1,8 @@
local S = minetest.get_translator("mcl_fire") local S = minetest.get_translator("mcl_fire")
local get_node = minetest.get_node
local add_entity = minetest.add_entity
-- Fire Charge -- Fire Charge
minetest.register_craftitem("mcl_fire:fire_charge", { minetest.register_craftitem("mcl_fire:fire_charge", {
description = S("Fire Charge"), description = S("Fire Charge"),
@ -11,7 +14,7 @@ minetest.register_craftitem("mcl_fire:fire_charge", {
stack_max = 64, stack_max = 64,
on_place = function(itemstack, user, pointed_thing) on_place = function(itemstack, user, pointed_thing)
-- Use pointed node's on_rightclick function first, if present -- Use pointed node's on_rightclick function first, if present
local node = minetest.get_node(pointed_thing.under) local node = get_node(pointed_thing.under)
if user and not user:get_player_control().sneak then if user and not user:get_player_control().sneak then
if minetest.registered_nodes[node.name] and minetest.registered_nodes[node.name].on_rightclick then if minetest.registered_nodes[node.name] and minetest.registered_nodes[node.name].on_rightclick then
return minetest.registered_nodes[node.name].on_rightclick(pointed_thing.under, node, user, itemstack) or itemstack return minetest.registered_nodes[node.name].on_rightclick(pointed_thing.under, node, user, itemstack) or itemstack
@ -45,7 +48,7 @@ minetest.register_craftitem("mcl_fire:fire_charge", {
_on_dispense = function(stack, pos, droppos, dropnode, dropdir) _on_dispense = function(stack, pos, droppos, dropnode, dropdir)
-- Throw fire charge -- Throw fire charge
local shootpos = vector.add(pos, vector.multiply(dropdir, 0.51)) local shootpos = vector.add(pos, vector.multiply(dropdir, 0.51))
local fireball = minetest.add_entity(shootpos, "mobs_mc:blaze_fireball") local fireball = add_entity(shootpos, "mobs_mc:blaze_fireball")
local ent = fireball:get_luaentity() local ent = fireball:get_luaentity()
ent._shot_from_dispenser = true ent._shot_from_dispenser = true
local v = ent.velocity or 1 local v = ent.velocity or 1

View File

@ -1,4 +1,6 @@
local S = minetest.get_translator("mcl_fire") local S = minetest.get_translator("mcl_fire")
local get_node = minetest.get_node
local add_node = minetest.add_node
-- Flint and Steel -- Flint and Steel
minetest.register_tool("mcl_fire:flint_and_steel", { minetest.register_tool("mcl_fire:flint_and_steel", {
@ -12,7 +14,7 @@ minetest.register_tool("mcl_fire:flint_and_steel", {
groups = { tool = 1, }, groups = { tool = 1, },
on_place = function(itemstack, user, pointed_thing) on_place = function(itemstack, user, pointed_thing)
-- Use pointed node's on_rightclick function first, if present -- Use pointed node's on_rightclick function first, if present
local node = minetest.get_node(pointed_thing.under) local node = get_node(pointed_thing.under)
if user and not user:get_player_control().sneak then if user and not user:get_player_control().sneak then
if minetest.registered_nodes[node.name] and minetest.registered_nodes[node.name].on_rightclick then if minetest.registered_nodes[node.name] and minetest.registered_nodes[node.name].on_rightclick then
return minetest.registered_nodes[node.name].on_rightclick(pointed_thing.under, node, user, itemstack) or itemstack return minetest.registered_nodes[node.name].on_rightclick(pointed_thing.under, node, user, itemstack) or itemstack
@ -33,7 +35,7 @@ minetest.register_tool("mcl_fire:flint_and_steel", {
) )
local used = false local used = false
if pointed_thing.type == "node" then if pointed_thing.type == "node" then
local nodedef = minetest.registered_nodes[minetest.get_node(pointed_thing.under).name] local nodedef = minetest.registered_nodes[get_node(pointed_thing.under).name]
if nodedef and nodedef._on_ignite then if nodedef and nodedef._on_ignite then
local overwrite = nodedef._on_ignite(user, pointed_thing) local overwrite = nodedef._on_ignite(user, pointed_thing)
if not overwrite then if not overwrite then
@ -56,7 +58,7 @@ minetest.register_tool("mcl_fire:flint_and_steel", {
_on_dispense = function(stack, pos, droppos, dropnode, dropdir) _on_dispense = function(stack, pos, droppos, dropnode, dropdir)
-- Ignite air -- Ignite air
if dropnode.name == "air" then if dropnode.name == "air" then
minetest.add_node(droppos, {name="mcl_fire:fire"}) add_node(droppos, {name="mcl_fire:fire"})
if not minetest.is_creative_enabled("") then if not minetest.is_creative_enabled("") then
stack:add_wear(65535/65) -- 65 uses stack:add_wear(65535/65) -- 65 uses
end end

View File

@ -1,10 +1,29 @@
-- Global namespace for functions -- Global namespace for functions
mcl_fire = {} mcl_fire = {}
local modpath = minetest.get_modpath(minetest.get_current_modname())
local S = minetest.get_translator("mcl_fire") local S = minetest.get_translator("mcl_fire")
local N = function(s) return s end local N = function(s) return s end
local has_mcl_portals = minetest.get_modpath("mcl_portals")
local set_node = minetest.set_node
local get_node = minetest.get_node
local add_node = minetest.add_node
local remove_node = minetest.remove_node
local swap_node = minetest.swap_node
local get_node_or_nil = minetest.get_node_or_nil
local find_nodes_in_area = minetest.find_nodes_in_area
local find_node_near = minetest.find_node_near
local get_item_group = minetest.get_item_group
local get_connected_players = minetest.get_connected_players
local vector = vector
local math = math
-- inverse pyramid pattern above lava source, floor 1 of 2: -- inverse pyramid pattern above lava source, floor 1 of 2:
local lava_fire= local lava_fire=
{ {
@ -90,7 +109,7 @@ local fire_timer = function(pos)
end end
local spawn_fire = function(pos, age) local spawn_fire = function(pos, age)
minetest.set_node(pos, {name="mcl_fire:fire", param2 = age}) set_node(pos, {name="mcl_fire:fire", param2 = age})
minetest.check_single_for_falling({x=pos.x, y=pos.y+1, z=pos.z}) minetest.check_single_for_falling({x=pos.x, y=pos.y+1, z=pos.z})
end end
@ -120,28 +139,28 @@ minetest.register_node("mcl_fire:fire", {
groups = {fire = 1, dig_immediate = 3, not_in_creative_inventory = 1, dig_by_piston=1, destroys_items=1, set_on_fire=8}, groups = {fire = 1, dig_immediate = 3, not_in_creative_inventory = 1, dig_by_piston=1, destroys_items=1, set_on_fire=8},
floodable = true, floodable = true,
on_flood = function(pos, oldnode, newnode) on_flood = function(pos, oldnode, newnode)
if minetest.get_item_group(newnode.name, "water") ~= 0 then if get_item_group(newnode.name, "water") ~= 0 then
minetest.sound_play("fire_extinguish_flame", {pos = pos, gain = 0.25, max_hear_distance = 16}, true) minetest.sound_play("fire_extinguish_flame", {pos = pos, gain = 0.25, max_hear_distance = 16}, true)
end end
end, end,
on_timer = function(pos) on_timer = function(pos)
local node = minetest.get_node(pos) local node = get_node(pos)
-- Age is a number from 0 to 15 and is increased every timer step. -- Age is a number from 0 to 15 and is increased every timer step.
-- "old" fire is more likely to be extinguished -- "old" fire is more likely to be extinguished
local age = node.param2 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 flammables = 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 = get_node({x=pos.x, y=pos.z-1, z=pos.z})
local below_is_flammable = minetest.get_item_group(below.name, "flammable") > 0 local below_is_flammable = get_item_group(below.name, "flammable") > 0
-- Extinguish fire -- Extinguish fire
if (not fire_enabled) and (math.random(1,3) == 1) then if (not fire_enabled) and (math.random(1,3) == 1) then
minetest.remove_node(pos) remove_node(pos)
return return
end end
if age == 15 and not below_is_flammable then if age == 15 and not below_is_flammable then
minetest.remove_node(pos) remove_node(pos)
return return
elseif age > 3 and #flammables == 0 and not below_is_flammable and math.random(1,4) == 1 then elseif age > 3 and #flammables == 0 and not below_is_flammable and math.random(1,4) == 1 then
minetest.remove_node(pos) remove_node(pos)
return return
end end
local age_add = 1 local age_add = 1
@ -149,14 +168,14 @@ minetest.register_node("mcl_fire:fire", {
if (not fire_enabled) then if (not fire_enabled) then
if age + age_add <= 15 then if age + age_add <= 15 then
node.param2 = age + age_add node.param2 = age + age_add
minetest.set_node(pos, node) set_node(pos, node)
end end
-- Restart timer -- Restart timer
fire_timer(pos) fire_timer(pos)
return return
end end
-- Spawn fire to nearby flammable nodes -- Spawn fire to nearby flammable nodes
local is_next_to_flammable = minetest.find_node_near(pos, 2, {"group:flammable"}) ~= nil local is_next_to_flammable = find_node_near(pos, 2, {"group:flammable"}) ~= nil
if is_next_to_flammable and math.random(1,2) == 1 then if is_next_to_flammable and math.random(1,2) == 1 then
-- The fire we spawn copies the age of this fire. -- The fire we spawn copies the age of this fire.
-- This prevents fire from spreading infinitely far as the fire fire dies off -- This prevents fire from spreading infinitely far as the fire fire dies off
@ -166,10 +185,10 @@ minetest.register_node("mcl_fire:fire", {
local burntype = math.random(1,2) local burntype = math.random(1,2)
if burntype == 1 then if burntype == 1 then
-- Spawn fire in air -- 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"}) local nodes = 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 while #nodes > 0 do
local r = math.random(1, #nodes) local r = math.random(1, #nodes)
if minetest.find_node_near(nodes[r], 1, {"group:flammable"}) then if find_node_near(nodes[r], 1, {"group:flammable"}) then
spawn_fire(nodes[r], age_next) spawn_fire(nodes[r], age_next)
break break
else else
@ -178,12 +197,12 @@ minetest.register_node("mcl_fire:fire", {
end end
else else
-- Burn flammable block -- Burn flammable block
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"}) local nodes = 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 if #nodes > 0 then
local r = math.random(1, #nodes) local r = math.random(1, #nodes)
local nn = minetest.get_node(nodes[r]).name local nn = get_node(nodes[r]).name
local ndef = minetest.registered_nodes[nn] local ndef = minetest.registered_nodes[nn]
local fgroup = minetest.get_item_group(nn, "flammable") local fgroup = get_item_group(nn, "flammable")
if ndef and ndef._on_burn then if ndef and ndef._on_burn then
ndef._on_burn(nodes[r]) ndef._on_burn(nodes[r])
elseif fgroup ~= -1 then elseif fgroup ~= -1 then
@ -195,7 +214,7 @@ minetest.register_node("mcl_fire:fire", {
-- Regular age increase -- Regular age increase
if age + age_add <= 15 then if age + age_add <= 15 then
node.param2 = age + age_add node.param2 = age + age_add
minetest.set_node(pos, node) set_node(pos, node)
end end
-- Restart timer -- Restart timer
fire_timer(pos) fire_timer(pos)
@ -205,14 +224,14 @@ minetest.register_node("mcl_fire:fire", {
-- Turn into eternal fire on special blocks, light Nether portal (if possible), start burning timer -- Turn into eternal fire on special blocks, light Nether portal (if possible), start burning timer
on_construct = function(pos) on_construct = function(pos)
local bpos = {x=pos.x, y=pos.y-1, z=pos.z} local bpos = {x=pos.x, y=pos.y-1, z=pos.z}
local under = minetest.get_node(bpos).name local under = get_node(bpos).name
local dim = mcl_worlds.pos_to_dimension(bpos) local dim = mcl_worlds.pos_to_dimension(bpos)
if under == "mcl_nether:magma" or under == "mcl_nether:netherrack" or (under == "mcl_core:bedrock" and dim == "end") then if under == "mcl_nether:magma" or under == "mcl_nether:netherrack" or (under == "mcl_core:bedrock" and dim == "end") then
minetest.swap_node(pos, {name = "mcl_fire:eternal_fire"}) swap_node(pos, {name = "mcl_fire:eternal_fire"})
end end
if minetest.get_modpath("mcl_portals") then if has_mcl_portals then
mcl_portals.light_nether_portal(pos) mcl_portals.light_nether_portal(pos)
end end
@ -251,17 +270,17 @@ minetest.register_node("mcl_fire:eternal_fire", {
groups = {fire = 1, dig_immediate = 3, not_in_creative_inventory = 1, dig_by_piston = 1, destroys_items = 1, set_on_fire=8}, groups = {fire = 1, dig_immediate = 3, not_in_creative_inventory = 1, dig_by_piston = 1, destroys_items = 1, set_on_fire=8},
floodable = true, floodable = true,
on_flood = function(pos, oldnode, newnode) on_flood = function(pos, oldnode, newnode)
if minetest.get_item_group(newnode.name, "water") ~= 0 then if get_item_group(newnode.name, "water") ~= 0 then
minetest.sound_play("fire_extinguish_flame", {pos = pos, gain = 0.25, max_hear_distance = 16}, true) minetest.sound_play("fire_extinguish_flame", {pos = pos, gain = 0.25, max_hear_distance = 16}, true)
end end
end, end,
on_timer = function(pos) on_timer = function(pos)
if fire_enabled then if fire_enabled then
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 airs = 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 #airs > 0 do while #airs > 0 do
local r = math.random(1, #airs) local r = math.random(1, #airs)
if minetest.find_node_near(airs[r], 1, {"group:flammable"}) then if find_node_near(airs[r], 1, {"group:flammable"}) then
local node = minetest.get_node(airs[r]) local node = get_node(airs[r])
local age = node.param2 local age = node.param2
local age_next = math.min(15, age + math.random(0, 1)) local age_next = math.min(15, age + math.random(0, 1))
spawn_fire(airs[r], age_next) spawn_fire(airs[r], age_next)
@ -278,7 +297,7 @@ minetest.register_node("mcl_fire:eternal_fire", {
on_construct = function(pos) on_construct = function(pos)
fire_timer(pos) fire_timer(pos)
if minetest.get_modpath("mcl_portals") then if has_mcl_portals then --Calling directly minetest.get_modpath consumes 4x more compute time
mcl_portals.light_nether_portal(pos) mcl_portals.light_nether_portal(pos)
end end
spawn_smoke(pos) spawn_smoke(pos)
@ -313,7 +332,7 @@ if flame_sound then
local ppos = player:get_pos() local ppos = player:get_pos()
local areamin = vector.subtract(ppos, radius) local areamin = vector.subtract(ppos, radius)
local areamax = vector.add(ppos, radius) local areamax = vector.add(ppos, radius)
local fpos, num = minetest.find_nodes_in_area( local fpos, num = find_nodes_in_area(
areamin, areamin,
areamax, areamax,
{"mcl_fire:fire", "mcl_fire:eternal_fire"} {"mcl_fire:fire", "mcl_fire:eternal_fire"}
@ -384,7 +403,7 @@ if flame_sound then
end end
timer = 0 timer = 0
local players = minetest.get_connected_players() local players = get_connected_players()
for n = 1, #players do for n = 1, #players do
mcl_fire.update_player_sound(players[n]) mcl_fire.update_player_sound(players[n])
end end
@ -416,7 +435,7 @@ minetest.register_abm({
chance = 1, chance = 1,
catch_up = false, catch_up = false,
action = function(pos, node, active_object_count, active_object_count_wider) action = function(pos, node, active_object_count, active_object_count_wider)
minetest.remove_node(pos) remove_node(pos)
minetest.sound_play("fire_extinguish_flame", minetest.sound_play("fire_extinguish_flame",
{pos = pos, max_hear_distance = 16, gain = 0.15}, true) {pos = pos, max_hear_distance = 16, gain = 0.15}, true)
end, end,
@ -429,8 +448,8 @@ local function has_flammable(pos)
local npos, node local npos, node
for n, v in ipairs(alldirs) do for n, v in ipairs(alldirs) do
npos = vector.add(pos, v) npos = vector.add(pos, v)
node = minetest.get_node_or_nil(npos) node = get_node_or_nil(npos)
if node and node.name and minetest.get_item_group(node.name, "flammable") ~= 0 then if node and node.name and get_item_group(node.name, "flammable") ~= 0 then
return npos return npos
end end
end end
@ -447,7 +466,7 @@ if not fire_enabled then
interval = 10, interval = 10,
chance = 10, chance = 10,
catch_up = false, catch_up = false,
action = minetest.remove_node, action = remove_node,
}) })
else -- Fire enabled else -- Fire enabled
@ -465,12 +484,12 @@ else -- Fire enabled
i = math.random(1,9) i = math.random(1,9)
dir = lava_fire[i] dir = lava_fire[i]
target = {x=pos.x+dir.x, y=pos.y+dir.y, z=pos.z+dir.z} target = {x=pos.x+dir.x, y=pos.y+dir.y, z=pos.z+dir.z}
node = minetest.get_node(target) node = get_node(target)
if not node or node.name ~= "air" then if not node or node.name ~= "air" then
i = ((i + math.random(0,7)) % 9) + 1 i = ((i + math.random(0,7)) % 9) + 1
dir = lava_fire[i] dir = lava_fire[i]
target = {x=pos.x+dir.x, y=pos.y+dir.y, z=pos.z+dir.z} target = {x=pos.x+dir.x, y=pos.y+dir.y, z=pos.z+dir.z}
node = minetest.get_node(target) node = get_node(target)
if not node or node.name ~= "air" then if not node or node.name ~= "air" then
return return
end end
@ -480,7 +499,7 @@ else -- Fire enabled
local dir2, target2, node2 local dir2, target2, node2
dir2 = lava_fire[i2] dir2 = lava_fire[i2]
target2 = {x=target.x+dir2.x, y=target.y+dir2.y, z=target.z+dir2.z} target2 = {x=target.x+dir2.x, y=target.y+dir2.y, z=target.z+dir2.z}
node2 = minetest.get_node(target2) node2 = get_node(target2)
if node2 and node2.name == "air" then if node2 and node2.name == "air" then
f = has_flammable(target2) f = has_flammable(target2)
if f then if f then
@ -521,9 +540,9 @@ mcl_fire.set_fire = function(pointed_thing, player, allow_on_fire)
else else
pname = player:get_player_name() pname = player:get_player_name()
end end
local n = minetest.get_node(pointed_thing.above) local n = get_node(pointed_thing.above)
local nu = minetest.get_node(pointed_thing.under) local nu = get_node(pointed_thing.under)
if allow_on_fire == false and minetest.get_item_group(nu.name, "fire") ~= 0 then if allow_on_fire == false and get_item_group(nu.name, "fire") ~= 0 then
return return
end end
if minetest.is_protected(pointed_thing.above, pname) then if minetest.is_protected(pointed_thing.above, pname) then
@ -531,7 +550,7 @@ mcl_fire.set_fire = function(pointed_thing, player, allow_on_fire)
return return
end end
if n.name == "air" then if n.name == "air" then
minetest.add_node(pointed_thing.above, {name="mcl_fire:fire"}) add_node(pointed_thing.above, {name="mcl_fire:fire"})
end end
end end
@ -549,5 +568,5 @@ minetest.register_alias("mcl_fire:basic_flame", "mcl_fire:fire")
minetest.register_alias("fire:basic_flame", "mcl_fire:fire") minetest.register_alias("fire:basic_flame", "mcl_fire:fire")
minetest.register_alias("fire:permanent_flame", "mcl_fire:eternal_fire") minetest.register_alias("fire:permanent_flame", "mcl_fire:eternal_fire")
dofile(minetest.get_modpath(minetest.get_current_modname()).."/flint_and_steel.lua") dofile(modpath.."/flint_and_steel.lua")
dofile(minetest.get_modpath(minetest.get_current_modname()).."/fire_charge.lua") dofile(modpath.."/fire_charge.lua")

View File

@ -1 +1,3 @@
name = mcl_fire name = mcl_fire
depends = mcl_core, mcl_worlds, mcl_sounds, mcl_particles
optional_depends = mcl_portals