tweak & tidy code

This commit is contained in:
tenplus1 2024-12-05 13:53:25 +00:00
parent e49346d65d
commit 22c81f4818
2 changed files with 60 additions and 74 deletions

23
3d.lua
View File

@ -1,8 +1,10 @@
-- translation support -- translation support
local S = minetest.get_translator("real_torch") local S = minetest.get_translator("real_torch")
-- flood helper function -- flood helper function
local function on_flood(pos, oldnode, newnode) local function on_flood(pos, oldnode, newnode)
-- drop as unlit torch -- drop as unlit torch
@ -26,8 +28,8 @@ local function on_flood(pos, oldnode, newnode)
return false return false
end end
-- unlit floor torch -- unlit floor torch
minetest.register_node("real_torch:torch", { minetest.register_node("real_torch:torch", {
description = S("Unlit Torch"), description = S("Unlit Torch"),
drawtype = "mesh", drawtype = "mesh",
@ -52,8 +54,7 @@ minetest.register_node("real_torch:torch", {
groups = {choppy = 2, dig_immediate = 3, flammable = 1, attached_node = 1}, groups = {choppy = 2, dig_immediate = 3, flammable = 1, attached_node = 1},
drop = "real_torch:torch", drop = "real_torch:torch",
selection_box = { selection_box = {
type = "wallmounted", type = "wallmounted", wall_bottom = {-1/8, -1/2, -1/8, 1/8, 2/16, 1/8}
wall_bottom = {-1/8, -1/2, -1/8, 1/8, 2/16, 1/8}
}, },
sounds = default.node_sound_wood_defaults(), sounds = default.node_sound_wood_defaults(),
@ -68,7 +69,7 @@ minetest.register_node("real_torch:torch", {
and placer:get_player_control().sneak) then and placer:get_player_control().sneak) then
return def.on_rightclick(under, node, placer, itemstack, return def.on_rightclick(under, node, placer, itemstack,
pointed_thing) or itemstack pointed_thing) or itemstack
end end
local above = pointed_thing.above local above = pointed_thing.above
@ -99,8 +100,8 @@ minetest.register_node("real_torch:torch", {
floodable = true, on_rotate = false, on_flood = on_flood floodable = true, on_rotate = false, on_flood = on_flood
}) })
-- unlit wall torch -- unlit wall torch
minetest.register_node("real_torch:torch_wall", { minetest.register_node("real_torch:torch_wall", {
drawtype = "mesh", drawtype = "mesh",
mesh = "torch_wall.obj", mesh = "torch_wall.obj",
@ -124,8 +125,7 @@ minetest.register_node("real_torch:torch_wall", {
}, },
drop = "real_torch:torch", drop = "real_torch:torch",
selection_box = { selection_box = {
type = "wallmounted", type = "wallmounted", wall_side = {-1/2, -1/2, -1/8, -1/8, 1/8, 1/8}
wall_side = {-1/2, -1/2, -1/8, -1/8, 1/8, 1/8}
}, },
sounds = default.node_sound_wood_defaults(), sounds = default.node_sound_wood_defaults(),
@ -139,8 +139,8 @@ minetest.register_node("real_torch:torch_wall", {
floodable = true, on_rotate = false, on_flood = on_flood floodable = true, on_rotate = false, on_flood = on_flood
}) })
-- unlit ceiling torch -- unlit ceiling torch
minetest.register_node("real_torch:torch_ceiling", { minetest.register_node("real_torch:torch_ceiling", {
drawtype = "mesh", drawtype = "mesh",
mesh = "torch_ceiling.obj", mesh = "torch_ceiling.obj",
@ -164,8 +164,7 @@ minetest.register_node("real_torch:torch_ceiling", {
}, },
drop = "real_torch:torch", drop = "real_torch:torch",
selection_box = { selection_box = {
type = "wallmounted", type = "wallmounted", wall_top = {-1/8, -1/16, -5/16, 1/8, 1/2, 1/8}
wall_top = {-1/8, -1/16, -5/16, 1/8, 1/2, 1/8}
}, },
sounds = default.node_sound_wood_defaults(), sounds = default.node_sound_wood_defaults(),
@ -179,11 +178,11 @@ minetest.register_node("real_torch:torch_ceiling", {
floodable = true, on_rotate = false, on_flood = on_flood floodable = true, on_rotate = false, on_flood = on_flood
}) })
-- burnout helper function -- burnout helper function
local function torch_override(name) local function torch_override(name)
-- override default torch to burn out after 8-10 minutes -- override default torch to burn out after 8-10 minutes and drop in water
minetest.override_item("default:" .. name, { minetest.override_item("default:" .. name, {
on_timer = function(pos, elapsed) on_timer = function(pos, elapsed)

111
init.lua
View File

@ -3,18 +3,19 @@
real_torch = {} real_torch = {}
-- check for timer settings or use defaults -- translation and torch duration settings
local S = minetest.get_translator("real_torch")
real_torch.min_duration = tonumber(minetest.settings:get("torch_min_duration")) or 1200 real_torch.min_duration = tonumber(minetest.settings:get("torch_min_duration")) or 1200
real_torch.max_duration = tonumber(minetest.settings:get("torch_max_duration")) or 1800 real_torch.max_duration = tonumber(minetest.settings:get("torch_max_duration")) or 1800
-- translation support -- add unlit torches & override current
local S = minetest.get_translator("real_torch")
-- add unlit torches
dofile(minetest.get_modpath("real_torch") .. "/3d.lua") dofile(minetest.get_modpath("real_torch") .. "/3d.lua")
-- start timer on any already placed torches -- start timer on any already placed torches
minetest.register_lbm({ minetest.register_lbm({
name = "real_torch:convert_torch_to_node_timer", name = "real_torch:convert_torch_to_node_timer",
nodenames = {"default:torch", "default:torch_wall", "default:torch_ceiling"}, nodenames = {"default:torch", "default:torch_wall", "default:torch_ceiling"},
@ -29,15 +30,15 @@ minetest.register_lbm({
end end
}) })
-- creative check -- creative check
local creative_mode_cache = minetest.settings:get_bool("creative_mode") local creative_mode_cache = minetest.settings:get_bool("creative_mode")
local function is_creative(name) local function is_creative(name)
return creative_mode_cache or minetest.check_player_privs(name, {creative = true}) return creative_mode_cache or minetest.check_player_privs(name, {creative = true})
end end
-- coal powder -- coal powder
minetest.register_craftitem("real_torch:coal_powder", { minetest.register_craftitem("real_torch:coal_powder", {
description = S("Coal Powder"), description = S("Coal Powder"),
inventory_image = "real_torch_coal_powder.png", inventory_image = "real_torch_coal_powder.png",
@ -51,22 +52,16 @@ minetest.register_craftitem("real_torch:coal_powder", {
local pos = pointed_thing.under local pos = pointed_thing.under
local nod = minetest.get_node(pos) local nod = minetest.get_node(pos)
local rep = false
if nod.name == "real_torch:torch" then if nod.name == "real_torch:torch" then
nod.name = "default:torch" nod.name = "default:torch"
rep = true
elseif nod.name == "real_torch:torch_wall" then elseif nod.name == "real_torch:torch_wall" then
nod.name = "default:torch_wall" nod.name = "default:torch_wall"
rep = true
elseif nod.name == "real_torch:torch_ceiling" then elseif nod.name == "real_torch:torch_ceiling" then
nod.name = "default:torch_ceiling" nod.name = "default:torch_ceiling"
rep = true else nod.name = "" end
end
if rep then if nod.name ~= "" then
minetest.set_node(pos, {name = nod.name, param2 = nod.param2}) minetest.set_node(pos, {name = nod.name, param2 = nod.param2})
@ -114,8 +109,8 @@ minetest.register_craft({
} }
}) })
-- explosion effect
-- particle effects
local function add_effects(pos, radius) local function add_effects(pos, radius)
minetest.add_particle({ minetest.add_particle({
@ -155,67 +150,59 @@ end
-- will also re-light torches and cause player damage when used on lit torch. -- will also re-light torches and cause player damage when used on lit torch.
if minetest.get_modpath("tnt") then if minetest.get_modpath("tnt") then
minetest.override_item("tnt:gunpowder", { minetest.override_item("tnt:gunpowder", {
on_use = function(itemstack, user, pointed_thing) on_use = function(itemstack, user, pointed_thing)
if not pointed_thing or pointed_thing.type ~= "node" then if not pointed_thing or pointed_thing.type ~= "node" then
return return
end end
local pos = pointed_thing.under local pos = pointed_thing.under
local nod = minetest.get_node(pos) local nod = minetest.get_node(pos)
local rep = false -- torch already lit, boom!
if nod.name == "default:torch"
or nod.name == "default:torch_wall"
or nod.name == "default:torch_ceiling" then
if nod.name == "real_torch:torch" then -- small delay to fix dupe bug
nod.name = "default:torch" minetest.after(0.1, function(user)
rep = true
elseif nod.name == "real_torch:torch_wall" then if user and user:get_pos() then
nod.name = "default:torch_wall" user:set_hp(user:get_hp() - 2)
rep = true end
end, user)
elseif nod.name == "real_torch:torch_ceiling" then add_effects(pos, 1)
nod.name = "default:torch_ceiling"
rep = true
end
if rep then if not is_creative(user:get_player_name()) then
itemstack:take_item()
end
end
minetest.set_node(pos, {name = nod.name, param2 = nod.param2}) if nod.name == "real_torch:torch" then
nod.name = "default:torch"
elseif nod.name == "real_torch:torch_wall" then
nod.name = "default:torch_wall"
elseif nod.name == "real_torch:torch_ceiling" then
nod.name = "default:torch_ceiling"
else nod.name = "" end
add_effects(pos, 1) if nod.name ~= "" then
if not is_creative(user:get_player_name()) then minetest.set_node(pos, {name = nod.name, param2 = nod.param2})
itemstack:take_item()
add_effects(pos, 1)
if not is_creative(user:get_player_name()) then
itemstack:take_item()
end
end end
return itemstack return itemstack
end end
})
if nod.name == "default:torch"
or nod.name == "default:torch_wall"
or nod.name == "default:torch_ceiling" then
-- small delay to fix dupe bug
minetest.after(0.1, function(user)
if user and user:get_pos() then
user:set_hp(user:get_hp() - 2)
end
end, user)
add_effects(pos, 1)
if not is_creative(user:get_player_name()) then
itemstack:take_item()
end
end
return itemstack
end
})
end end