Mineclonia/mods/ITEMS/mcl_fire/init.lua

238 lines
6.4 KiB
Lua
Raw Normal View History

local init = os.clock()
2017-02-01 15:26:43 +00:00
minetest.register_node("mcl_fire:basic_flame", {
2015-06-29 17:55:56 +00:00
description = "Fire",
2017-01-05 03:50:26 +00:00
drawtype = "firelike",
2015-06-29 17:55:56 +00:00
tiles = {{
name="fire_basic_flame_animated.png",
animation={type="vertical_frames", aspect_w=32, aspect_h=32, length=1},
}},
inventory_image = "fire_basic_flame.png",
2017-02-01 21:12:08 +00:00
-- Real light level: 15 (but Minetest caps at 14)
2015-06-29 17:55:56 +00:00
light_source = 14,
groups = {igniter=2,dig_immediate=3,dig_by_water=1,not_in_creative_inventory=1},
2015-06-29 17:55:56 +00:00
drop = '',
walkable = false,
buildable_to = true,
damage_per_second = 4,
after_place_node = function(pos, placer)
2017-02-01 15:43:05 +00:00
mcl_fire.on_flame_add_at(pos)
2015-06-29 17:55:56 +00:00
end,
after_dig_node = function(pos, oldnode, oldmetadata, digger)
2017-02-01 15:43:05 +00:00
mcl_fire.on_flame_remove_at(pos)
2015-06-29 17:55:56 +00:00
end,
})
2017-02-01 15:43:05 +00:00
mcl_fire = {}
mcl_fire.D = 6
2015-06-29 17:55:56 +00:00
-- key: position hash of low corner of area
-- value: {handle=sound handle, name=sound name}
2017-02-01 15:43:05 +00:00
mcl_fire.sounds = {}
2015-06-29 17:55:56 +00:00
2017-02-01 15:43:05 +00:00
function mcl_fire.get_area_p0p1(pos)
2015-06-29 17:55:56 +00:00
local p0 = {
2017-02-01 15:43:05 +00:00
x=math.floor(pos.x/mcl_fire.D)*mcl_fire.D,
y=math.floor(pos.y/mcl_fire.D)*mcl_fire.D,
z=math.floor(pos.z/mcl_fire.D)*mcl_fire.D,
2015-06-29 17:55:56 +00:00
}
local p1 = {
2017-02-01 15:43:05 +00:00
x=p0.x+mcl_fire.D-1,
y=p0.y+mcl_fire.D-1,
z=p0.z+mcl_fire.D-1
2015-06-29 17:55:56 +00:00
}
return p0, p1
end
2017-02-01 15:43:05 +00:00
function mcl_fire.update_sounds_around(pos)
local p0, p1 = mcl_fire.get_area_p0p1(pos)
2015-06-29 17:55:56 +00:00
local cp = {x=(p0.x+p1.x)/2, y=(p0.y+p1.y)/2, z=(p0.z+p1.z)/2}
2017-02-01 15:26:43 +00:00
local flames_p = minetest.find_nodes_in_area(p0, p1, {"mcl_fire:basic_flame"})
2015-06-29 17:55:56 +00:00
--print("number of flames at "..minetest.pos_to_string(p0).."/"
-- ..minetest.pos_to_string(p1)..": "..#flames_p)
local should_have_sound = (#flames_p > 0)
local wanted_sound = nil
if #flames_p >= 9 then
wanted_sound = {name="fire_large", gain=1.5}
elseif #flames_p > 0 then
wanted_sound = {name="fire_small", gain=1.5}
end
local p0_hash = minetest.hash_node_position(p0)
2017-02-01 15:43:05 +00:00
local sound = mcl_fire.sounds[p0_hash]
2015-06-29 17:55:56 +00:00
if not sound then
if should_have_sound then
2017-02-01 15:43:05 +00:00
mcl_fire.sounds[p0_hash] = {
2015-06-29 17:55:56 +00:00
handle = minetest.sound_play(wanted_sound, {pos=cp, loop=true}),
name = wanted_sound.name,
}
end
else
if not wanted_sound then
minetest.sound_stop(sound.handle)
2017-02-01 15:43:05 +00:00
mcl_fire.sounds[p0_hash] = nil
2015-06-29 17:55:56 +00:00
elseif sound.name ~= wanted_sound.name then
minetest.sound_stop(sound.handle)
2017-02-01 15:43:05 +00:00
mcl_fire.sounds[p0_hash] = {
2015-06-29 17:55:56 +00:00
handle = minetest.sound_play(wanted_sound, {pos=cp, loop=true}),
name = wanted_sound.name,
}
end
end
end
2017-02-01 15:43:05 +00:00
function mcl_fire.on_flame_add_at(pos)
2015-06-29 17:55:56 +00:00
--print("flame added at "..minetest.pos_to_string(pos))
2017-02-01 15:43:05 +00:00
mcl_fire.update_sounds_around(pos)
2015-06-29 17:55:56 +00:00
end
2017-02-01 15:43:05 +00:00
function mcl_fire.on_flame_remove_at(pos)
2015-06-29 17:55:56 +00:00
--print("flame removed at "..minetest.pos_to_string(pos))
2017-02-01 15:43:05 +00:00
mcl_fire.update_sounds_around(pos)
2015-06-29 17:55:56 +00:00
end
2017-02-01 15:43:05 +00:00
function mcl_fire.find_pos_for_flame_around(pos)
2015-06-29 17:55:56 +00:00
return minetest.find_node_near(pos, 1, {"air"})
end
2017-02-01 15:43:05 +00:00
function mcl_fire.flame_should_extinguish(pos)
2015-06-29 17:55:56 +00:00
if minetest.setting_getbool("disable_fire") then return true end
--return minetest.find_node_near(pos, 1, {"group:puts_out_fire"})
local p0 = {x=pos.x-2, y=pos.y, z=pos.z-2}
local p1 = {x=pos.x+2, y=pos.y, z=pos.z+2}
local ps = minetest.find_nodes_in_area(p0, p1, {"group:puts_out_fire"})
return (#ps ~= 0)
end
-- Ignite neighboring nodes
minetest.register_abm({
nodenames = {"group:flammable"},
neighbors = {"group:igniter"},
interval = 1,
chance = 2,
action = function(p0, node, _, _)
-- If there is water or stuff like that around flame, don't ignite
2017-02-01 15:43:05 +00:00
if mcl_fire.flame_should_extinguish(p0) then
2015-06-29 17:55:56 +00:00
return
end
2017-02-01 15:43:05 +00:00
local p = mcl_fire.find_pos_for_flame_around(p0)
2015-06-29 17:55:56 +00:00
if p then
2017-02-01 15:26:43 +00:00
minetest.set_node(p, {name="mcl_fire:basic_flame"})
2017-02-01 15:43:05 +00:00
mcl_fire.on_flame_add_at(p)
2015-06-29 17:55:56 +00:00
end
end,
})
-- Rarely ignite things from far
minetest.register_abm({
nodenames = {"group:igniter"},
neighbors = {"air"},
interval = 2,
chance = 10,
action = function(p0, node, _, _)
local reg = minetest.registered_nodes[node.name]
if not reg or not reg.groups.igniter or reg.groups.igniter < 2 then
return
end
local d = reg.groups.igniter
local p = minetest.find_node_near(p0, d, {"group:flammable"})
if p then
-- If there is water or stuff like that around flame, don't ignite
2017-02-01 15:43:05 +00:00
if mcl_fire.flame_should_extinguish(p) then
2015-06-29 17:55:56 +00:00
return
end
2017-02-01 15:43:05 +00:00
local p2 = mcl_fire.find_pos_for_flame_around(p)
2015-06-29 17:55:56 +00:00
if p2 then
2017-02-01 15:26:43 +00:00
minetest.set_node(p2, {name="mcl_fire:basic_flame"})
2017-02-01 15:43:05 +00:00
mcl_fire.on_flame_add_at(p2)
2015-06-29 17:55:56 +00:00
end
end
end,
})
-- Remove flammable nodes and flame
minetest.register_abm({
2017-02-01 15:26:43 +00:00
nodenames = {"mcl_fire:basic_flame"},
2015-06-29 17:55:56 +00:00
interval = 1,
chance = 2,
action = function(p0, node, _, _)
-- If there is water or stuff like that around flame, remove flame
2017-02-01 15:43:05 +00:00
if mcl_fire.flame_should_extinguish(p0) then
2015-06-29 17:55:56 +00:00
minetest.remove_node(p0)
2017-02-01 15:43:05 +00:00
mcl_fire.on_flame_remove_at(p0)
2015-06-29 17:55:56 +00:00
return
end
-- Make the following things rarer
if math.random(1,3) == 1 then
return
end
-- If there are no flammable nodes around flame, remove flame
if not minetest.find_node_near(p0, 1, {"group:flammable"}) then
minetest.remove_node(p0)
2017-02-01 15:43:05 +00:00
mcl_fire.on_flame_remove_at(p0)
2015-06-29 17:55:56 +00:00
return
end
if math.random(1,3) == 1 then
-- remove a flammable node around flame
local p = minetest.find_node_near(p0, 1, {"group:flammable"})
if p then
-- If there is water or stuff like that around flame, don't remove
2017-02-01 15:43:05 +00:00
if mcl_fire.flame_should_extinguish(p0) then
2015-06-29 17:55:56 +00:00
return
end
minetest.remove_node(p)
core.check_for_falling(p)
2015-06-29 17:55:56 +00:00
end
else
-- remove flame
minetest.remove_node(p0)
2017-02-01 15:43:05 +00:00
mcl_fire.on_flame_remove_at(p0)
2015-06-29 17:55:56 +00:00
end
end,
})
2017-02-01 15:43:05 +00:00
--
-- Flint and Steel
--
function mcl_fire.set_fire(pointed_thing)
local n = minetest.get_node(pointed_thing.above)
if n.name ~= "" and n.name == "air" and not minetest.is_protected(pointed_thing.above, "fire") then
minetest.add_node(pointed_thing.above, {name="mcl_fire:basic_flame"})
end
end
--
-- Fire Particles
--
function mcl_fire.add_fire(pos)
local null = {x=0, y=0, z=0}
pos.y = pos.y+0.19
2017-02-10 15:03:38 +00:00
minetest.add_particle({
pos = pos,
velocity = null,
acceleration = null,
expirationtime = 1.1,
size = 1.5,
collisiondetection = true,
texture = "default_fire_particle"..tostring(math.random(1,2)) ..".png"
})
2017-02-01 15:43:05 +00:00
pos.y = pos.y +0.01
2017-02-10 15:03:38 +00:00
minetest.add_particle({
pos = pos,
velocity = null,
acceleration = null,
expirationtime = 0.8,
size = 1.5,
collisiondetection = true,
texture = "default_fire_particle"..tostring(math.random(1,2)) ..".png"
})
2017-02-01 15:43:05 +00:00
end
2017-02-01 15:31:27 +00:00
dofile(minetest.get_modpath(minetest.get_current_modname()).."/flint_and_steel.lua")
2017-02-01 15:39:51 +00:00
dofile(minetest.get_modpath(minetest.get_current_modname()).."/fire_charge.lua")
2017-02-01 15:31:27 +00:00
local time_to_load= os.clock() - init
print(string.format("[MOD] "..minetest.get_current_modname().." loaded in %.4f s", time_to_load))