hardtorch/features/node.lua

251 lines
7.1 KiB
Lua
Raw Permalink Normal View History

2017-08-31 19:21:00 -07:00
--[[
2019-12-02 09:12:11 -08:00
Mod HardTorch for Minetest
2022-04-23 19:19:03 -07:00
Copyright (C) 2017-2022 BrunoMine (https://github.com/BrunoMine)
2017-08-31 19:21:00 -07:00
2019-12-02 09:12:11 -08:00
You should have received a copy of the GNU General Public License
2019-12-04 08:49:43 -08:00
along with this program. If not, see <https://www.gnu.org/licenses/>.
2017-08-31 19:21:00 -07:00
Nodes
]]
2019-12-02 09:12:11 -08:00
-- Registered nodes
2017-08-31 19:21:00 -07:00
-- Nodes registrados
hardtorch.registered_nodes = {}
-- Turn off torch node
local turn_off_torch_node = function(pos, def)
local node = minetest.get_node(pos)
if node.name == def.nodes.node then
node.name=def.nodes_off.node
elseif node.name == def.nodes.node_ceiling then
node.name=def.nodes_off.node_ceiling
elseif node.name == def.nodes.node_wall then
node.name=def.nodes_off.node_wall
end
minetest.set_node(pos, node)
end
2019-12-02 09:12:11 -08:00
-- Register torch node
2017-08-31 19:21:00 -07:00
-- Registrar Node de tocha
hardtorch.register_node = function(torchname, def)
for nt,nn in pairs(def.nodes) do
hardtorch.registered_nodes[nn] = torchname
2017-09-03 09:48:15 -07:00
if def.nodes.fire_source ~= false then
2019-12-02 09:12:11 -08:00
hardtorch.fire_sources[nn] = true
2017-09-03 09:48:15 -07:00
end
2017-08-31 19:21:00 -07:00
end
2019-12-02 09:12:11 -08:00
-- Take torch and fuel with wear
-- Pega tocha e combustivel com desgaste
2017-08-31 19:21:00 -07:00
local on_dig = function(pos, node, digger)
if not hardtorch.registered_nodes[node.name] then return end
2022-04-23 19:19:03 -07:00
if not digger then return end -- Evita erro com jogador nulo
2017-08-31 19:21:00 -07:00
local meta = minetest.get_meta(pos)
local inv = digger:get_inventory()
2019-12-02 09:12:11 -08:00
-- Calculate wear
2017-09-02 03:09:50 -07:00
local wear = hardtorch.get_node_wear(pos)
local itemstack = {name=torchname, count=1}
2019-12-02 09:12:11 -08:00
-- If torch is the fuel
-- Se a tocha é o próprio cosbustivel
local torch_is_fuel = false
2017-09-02 03:09:50 -07:00
if torchname.."_on" == meta:get_string("hardtorch_fuel") then
itemstack.wear = wear
2019-12-02 09:12:11 -08:00
torch_is_fuel = true
2017-09-02 03:09:50 -07:00
end
2019-12-02 09:12:11 -08:00
-- Keep torch lit if possible
-- Deixa tocha acesa se possivel
if not hardtorch.in_loop[digger:get_player_name()] then
2017-09-02 03:09:50 -07:00
itemstack.name = torchname.."_on"
end
2019-12-02 09:12:11 -08:00
-- Checks if torch fits in inventory
2017-09-02 03:09:50 -07:00
-- Verifica se tocha cabe no inventario
2017-08-31 19:21:00 -07:00
if inv:room_for_item("main", itemstack) then
2019-12-02 09:12:11 -08:00
2017-08-31 19:21:00 -07:00
inv:add_item("main", itemstack)
2019-12-02 09:12:11 -08:00
-- Lights with loop if added previously lit
2017-09-02 03:09:50 -07:00
-- Acende com loop caso adicinou acesa anteriormente
2019-12-02 09:12:11 -08:00
if not hardtorch.in_loop[digger:get_player_name()] then
2017-08-31 19:21:00 -07:00
local list, i, itemstack = hardtorch.find_and_get_item(digger, torchname.."_on")
2017-09-02 03:09:50 -07:00
itemstack:set_name(torchname)
2019-12-02 09:12:11 -08:00
itemstack = hardtorch.turnon_torch(itemstack, digger)
2017-08-31 19:21:00 -07:00
inv:set_stack(list, i, itemstack)
end
else
2019-12-02 09:12:11 -08:00
-- Drop torch
2017-08-31 19:21:00 -07:00
minetest.add_item(pos, itemstack)
end
2017-09-02 03:09:50 -07:00
2019-12-02 09:12:11 -08:00
-- Checks if fuel fits in inventory
2017-09-02 03:09:50 -07:00
-- Verifica se combustivel cabe no inventario
2019-12-02 09:12:11 -08:00
if torch_is_fuel == false then
2017-09-02 03:09:50 -07:00
local fuelstack = {name=meta:get_string("hardtorch_fuel"), count=1, wear=wear}
if inv:room_for_item("main", fuelstack) then
-- Coloca no inventario
inv:add_item("main", fuelstack)
else
2019-12-02 09:12:11 -08:00
-- Drop fuel
2017-09-02 03:09:50 -07:00
minetest.add_item(pos, fuelstack)
end
end
2019-12-02 09:12:11 -08:00
2017-08-31 19:21:00 -07:00
minetest.remove_node(pos)
end
2017-09-02 03:09:50 -07:00
2019-12-02 09:12:11 -08:00
-- Replaces craftitem/node in a tool (to be lit)
-- Troca craftitem/nó em ferramenta (para ser acesa)
local on_use = function(itemstack, player, pointed_thing)
if not hardtorch.registered_nodes[itemstack:get_name()] then return end
2019-12-02 09:12:11 -08:00
local leftover = itemstack:get_count() - 1
2017-08-31 19:21:00 -07:00
local inv = player:get_inventory()
2017-09-02 03:09:50 -07:00
2019-12-02 09:12:11 -08:00
-- Localize the item on inventory
2017-08-31 19:21:00 -07:00
-- Localiza o item no iventario
local list, i = player:get_wield_list(), player:get_wield_index()
local itemstack2 = inv:get_stack(list, i)
if itemstack:to_string() ~= itemstack2:to_string() then
return
end
2019-12-02 09:12:11 -08:00
-- Replace item
2017-09-02 03:09:50 -07:00
itemstack:replace({name=torchname, count=1, wear=0, metadata=""})
2017-08-31 19:21:00 -07:00
inv:set_stack(list, i, itemstack)
2019-12-02 09:12:11 -08:00
-- If has left over, try put on inventory or drop (with audible and textual warning)
2017-08-31 19:21:00 -07:00
-- Caso tenha sobra tenta colocar no inventario, ou joga no chão (com aviso sonoro e textual)
2019-12-02 09:12:11 -08:00
if leftover > 0 then
-- Put in inventory
if inv:room_for_item("main", def.nodes.node.." "..leftover) then
inv:add_item("main", def.nodes.node.." "..leftover)
-- Drop
2017-08-31 19:21:00 -07:00
else
2019-12-02 09:12:11 -08:00
-- Set torchs in inveotory to drop
-- Coloca a tocha no inventario para dropar
inv:set_stack(list, i, def.nodes.node.." "..leftover)
2017-08-31 19:21:00 -07:00
minetest.item_drop(inv:get_stack(list, i), player, player:getpos())
2019-12-02 09:12:11 -08:00
-- Restore to the torch tool
-- Restaura para a ferramenta tocha
2017-08-31 19:21:00 -07:00
inv:set_stack(list, i, itemstack)
end
end
2019-12-02 09:12:11 -08:00
-- Restore torch name
-- Restaura nome da tocha
itemstack:set_name(torchname)
2017-08-31 19:21:00 -07:00
2017-09-02 03:09:50 -07:00
return itemstack
2017-08-31 19:21:00 -07:00
end
2019-12-02 09:12:11 -08:00
-- Update the torch after place it
-- Atualiza a tocha apos coloca-la
2017-08-31 19:21:00 -07:00
local after_place_node = function(pos, placer, itemstack, pointed_thing)
2017-09-04 14:50:10 -07:00
if not hardtorch.registered_nodes[minetest.get_node(pos).name] then return end
2017-08-31 19:21:00 -07:00
2019-12-02 09:12:11 -08:00
-- Starts wear time count
-- Inicia contagem de tempo de desgaste
2017-09-02 03:09:50 -07:00
local timer = minetest.get_node_timer(pos)
if timer:is_started() ~= true then
2019-12-02 09:12:11 -08:00
-- Set initial wear if necessary
2017-09-02 03:09:50 -07:00
-- Define desgaste inicial caso necessario
local meta = minetest.get_meta(pos)
if meta:get_string("hardtorch_fuel") == "" then
meta:set_string("hardtorch_fuel", def.fuel[1])
meta:set_int("hardtorch_wear", 0)
end
2019-12-02 09:12:11 -08:00
-- Starts counting to end fire according to set wear
2017-09-02 03:09:50 -07:00
-- Inicia contagem para acabar fogo de acordo com desgaste definido
timer:start(hardtorch.get_node_timeout(pos))
end
2017-08-31 19:21:00 -07:00
end
2019-12-02 09:12:11 -08:00
-- Remove torch when fire end
2017-08-31 19:21:00 -07:00
-- Remove tocha quando fogo acabar
local on_timer = function(pos, elapsed)
if not hardtorch.registered_nodes[minetest.get_node(pos).name] then return end
2017-09-02 07:20:23 -07:00
if def.nodes_off then
turn_off_torch_node(pos, def)
2017-09-02 07:20:23 -07:00
else
minetest.remove_node(pos)
end
2017-08-31 19:21:00 -07:00
end
2017-09-02 03:09:50 -07:00
2017-08-31 19:21:00 -07:00
local node_torch_def = {
drop="",
on_dig=on_dig,
on_use=on_use,
after_place_node=after_place_node,
on_timer=on_timer,
}
2019-12-02 09:12:11 -08:00
-- Prevent normal placement in special cases
2017-09-02 03:09:50 -07:00
-- Impedir colocação normal em casos especiais
if hardtorch.torch_lighter then
node_torch_def.on_place = function(itemstack, placer, pointed_thing)
return itemstack
end
end
2017-08-31 19:21:00 -07:00
2019-12-02 09:12:11 -08:00
-- Upgrades torches with new calling features
-- Atualiza tochas com novas funções de chamadas
2017-08-31 19:21:00 -07:00
minetest.override_item(def.nodes.node, node_torch_def)
2019-12-04 06:42:28 -08:00
if def.nodes.node_ceiling then minetest.override_item(def.nodes.node_ceiling, node_torch_def) end
if def.nodes.node_wall then minetest.override_item(def.nodes.node_wall, node_torch_def) end
2017-08-31 19:21:00 -07:00
2019-12-02 09:12:11 -08:00
-- Turn off torches in contact with water
2017-08-31 19:21:00 -07:00
-- Apagar tochas em contato com agua
minetest.register_abm({
2019-12-02 09:12:11 -08:00
label = "Turn off wet torch",
2017-08-31 19:21:00 -07:00
nodenames = {def.nodes.node, def.nodes.node_ceiling, def.nodes.node_wall},
neighbors = {"group:water"},
interval = 1,
chance = 3,
catch_up = false,
action = function(pos, node)
if def.works_in_water == true then return end
if hardtorch.check_node_sides(pos, {"group:water"}) == false then return end
hardtorch.turnoff_by_water_sound(pos, torchname)
local force_drop = false
-- Try turn off
if def.nodes_off then
turn_off_torch_node(pos, def)
else
force_drop = true
end
-- Drop
if force_drop == true or def.drop_on_water ~= false then
2017-08-31 19:21:00 -07:00
minetest.remove_node(pos)
if type(def.drop_on_water) == "string" then
minetest.add_item(pos, {name=def.drop_on_water, wear=wear})
else
minetest.add_item(pos, {name=torchname, wear=wear})
end
2017-08-31 19:21:00 -07:00
end
end,
})
end