Upload files to "lwcomponents"

This commit is contained in:
Mavori 2025-06-27 17:54:41 +02:00
commit abe8504053
5 changed files with 2849 additions and 0 deletions

657
lwcomponents/siren.lua Normal file
View file

@ -0,0 +1,657 @@
local utils = ...
local S = utils.S
if utils.digilines_supported or utils.mesecon_supported then
local sound_interval = 5.0
local siren_sounds =
{
"lwsiren-buzz",
"lwsiren-horn",
"lwsiren-raid",
"lwsiren-siren",
}
local function start_sound (pos)
local meta = minetest.get_meta (pos)
if meta then
local handle = meta:get_int ("sound_handle")
if handle ~= 0 then
minetest.sound_stop (handle)
meta:set_int ("sound_handle", 0)
end
local sound = siren_sounds[meta:get_int ("sound")]
if sound then
handle = minetest.sound_play (
sound,
{
pos = pos,
max_hear_distance = meta:get_int ("distance"),
gain = meta:get_int ("gain") / 100
})
meta:set_int ("sound_handle", handle)
end
end
end
local function stop_sound (pos)
local meta = minetest.get_meta (pos)
if meta then
local handle = meta:get_int ("sound_handle")
if handle ~= 0 then
minetest.sound_stop (handle)
meta:set_int ("sound_handle", 0)
end
end
end
local function get_form_spec (is_off, distance, gain, sound)
return
"formspec_version[3]\n"..
"size[11.75,6.0;true]\n"..
"field[1.0,1.0;4.0,0.8;channel;Channel;${channel}]\n"..
"button[5.5,1.0;2.0,0.8;setchannel;Set]\n"..
"button[8.25,1.0;2.5,0.8;"..((is_off and "start;Start") or "stop;Stop").."]\n"..
"label[1.0,2.5;Distance]\n"..
"scrollbaroptions[min=0;max=100;smallstep=10;largestep=10;thumbsize=10]\n"..
"scrollbar[1.0,2.9;6.0,0.5;horizontal;distance;"..tostring (distance).."]\n"..
"label[1.0,4.2;Volume]\n"..
"scrollbaroptions[min=0;max=100;smallstep=10;largestep=10;thumbsize=10]\n"..
"scrollbar[1.0,4.5;6.0,0.5;horizontal;gain;"..tostring (gain).."]\n"..
"textlist[7.75,2.25;3.0,2.75;sound;Buzzer,Horn,Raid,Siren;"..tostring (sound)..";false]"
end
local function update_form_spec (pos)
local node = minetest.get_node (pos)
local meta = minetest.get_meta (pos)
if node and meta then
local is_off = node.name == "lwcomponents:siren" or
node.name == "lwcomponents:siren_locked"
meta:set_string ("formspec",
get_form_spec (is_off,
meta:get_int ("distance"),
meta:get_int ("gain"),
meta:get_int ("sound")))
end
end
local function start_siren (pos)
local node = minetest.get_node (pos)
if node then
if node.name == "lwcomponents:siren" then
node.name = "lwcomponents:siren_on"
stop_sound (pos)
minetest.swap_node (pos, node)
update_form_spec (pos)
elseif node.name == "lwcomponents:siren_locked" then
node.name = "lwcomponents:siren_locked_on"
stop_sound (pos)
minetest.swap_node (pos, node)
update_form_spec (pos)
end
end
end
local function stop_siren (pos)
local node = minetest.get_node (pos)
if node then
if node.name == "lwcomponents:siren_on" or
node.name == "lwcomponents:siren_alarm" then
node.name = "lwcomponents:siren"
minetest.get_node_timer (pos):stop ()
stop_sound (pos)
minetest.swap_node (pos, node)
update_form_spec (pos)
elseif node.name == "lwcomponents:siren_locked_on" or
node.name == "lwcomponents:siren_locked_alarm" then
node.name = "lwcomponents:siren_locked"
minetest.get_node_timer (pos):stop ()
stop_sound (pos)
minetest.swap_node (pos, node)
update_form_spec (pos)
end
end
end
local function start_alarm (pos)
local node = minetest.get_node (pos)
if node then
if node.name == "lwcomponents:siren_on" then
node.name = "lwcomponents:siren_alarm"
minetest.get_node_timer (pos):start (sound_interval)
start_sound (pos)
minetest.swap_node (pos, node)
elseif node.name == "lwcomponents:siren_locked_on" then
node.name = "lwcomponents:siren_locked_alarm"
minetest.get_node_timer (pos):start (sound_interval)
start_sound (pos)
minetest.swap_node (pos, node)
end
end
end
local function stop_alarm (pos)
local node = minetest.get_node (pos)
if node then
if node.name == "lwcomponents:siren_alarm" then
node.name = "lwcomponents:siren_on"
minetest.get_node_timer (pos):stop ()
stop_sound (pos)
minetest.swap_node (pos, node)
elseif node.name == "lwcomponents:siren_locked_alarm" then
node.name = "lwcomponents:siren_locked_on"
minetest.get_node_timer (pos):stop ()
stop_sound (pos)
minetest.swap_node (pos, node)
end
end
end
local function on_destruct (pos)
minetest.get_node_timer (pos):stop ()
stop_sound (pos)
end
local function after_place_node (pos, placer, itemstack, pointed_thing)
local meta = minetest.get_meta (pos)
local is_off = itemstack and (itemstack:get_name () == "lwcomponents:siren" or
itemstack:get_name () == "lwcomponents:siren_locked")
meta:set_string ("formspec", get_form_spec (is_off, 10, 50, 1))
meta:set_int ("sound", 1)
meta:set_int ("distance", 10)
meta:set_int ("gain", 50)
meta:set_int ("sound_handle", 0)
-- If return true no item is taken from itemstack
return false
end
local function after_place_node_locked (pos, placer, itemstack, pointed_thing)
after_place_node (pos, placer, itemstack, pointed_thing)
if placer and placer:is_player () then
local meta = minetest.get_meta (pos)
meta:set_string ("owner", placer:get_player_name ())
meta:set_string ("infotext", "Siren (owned by "..placer:get_player_name ()..")")
end
-- If return true no item is taken from itemstack
return false
end
local function on_receive_fields (pos, formname, fields, sender)
if not utils.can_interact_with_node (pos, sender) then
return
end
if fields.setchannel then
local meta = minetest.get_meta (pos)
if meta then
meta:set_string ("channel", fields.channel)
end
end
if fields.start then
start_siren (pos)
end
if fields.stop then
stop_siren (pos)
end
if fields.sound then
local event = minetest.explode_textlist_event (fields.sound)
if event.type == "CHG" then
local meta = minetest.get_meta (pos)
if meta then
meta:set_int ("sound", event.index)
end
end
end
if fields.gain then
local event = minetest.explode_scrollbar_event (fields.gain)
if event.type == "CHG" then
local meta = minetest.get_meta (pos)
if meta then
meta:set_int ("gain", event.value)
end
end
end
if fields.distance then
local event = minetest.explode_scrollbar_event (fields.distance)
if event.type == "CHG" then
local meta = minetest.get_meta (pos)
if meta then
meta:set_int ("distance", event.value)
end
end
end
end
local function can_dig (pos, player)
if not utils.can_interact_with_node (pos, player) then
return false
end
return true
end
local function on_blast (pos, intensity)
local meta = minetest.get_meta (pos)
if meta then
if intensity >= 1.0 then
on_destruct (pos)
minetest.remove_node (pos)
else -- intensity < 1.0
local node = minetest.get_node_or_nil (pos)
if node then
local items = minetest.get_node_drops (node, nil)
if items and #items > 0 then
local stack = ItemStack (items[1])
if stack then
utils.item_drop (stack, nil, pos)
on_destruct (pos)
minetest.remove_node (pos)
end
end
end
end
end
end
local function on_timer (pos, elapsed)
start_sound (pos)
return true
end
local function on_rightclick (pos, node, clicker, itemstack, pointed_thing)
if not utils.can_interact_with_node (pos, clicker) then
if clicker and clicker:is_player () then
local owner = "<unknown>"
local meta = minetest.get_meta (pos)
if meta then
owner = meta:get_string ("owner")
end
local spec =
"formspec_version[3]"..
"size[8.0,4.0,false]"..
"label[1.0,1.0;Owned by "..minetest.formspec_escape (owner).."]"..
"button_exit[3.0,2.0;2.0,1.0;close;Close]"
minetest.show_formspec (clicker:get_player_name (),
"lwcomponents:component_privately_owned",
spec)
end
else
update_form_spec (pos)
end
return itemstack
end
local function digilines_support ()
if utils.digilines_supported then
return
{
wire =
{
rules = utils.digilines_default_rules,
},
effector =
{
action = function (pos, node, channel, msg)
local meta = minetest.get_meta(pos)
if meta then
local this_channel = meta:get_string ("channel")
if this_channel ~= "" and this_channel == channel and
type (msg) == "string" then
local m = { }
for w in string.gmatch(msg, "[^%s]+") do
m[#m + 1] = w
end
if m[1] == "start" then
start_siren (pos)
elseif m[1] == "stop" then
stop_siren (pos)
elseif m[1] == "siren" then
if m[2] == "on" then
start_alarm (pos)
elseif m[2] == "off" then
stop_alarm (pos)
end
elseif m[1] == "distance" then
local distance = math.min (math.max (tonumber (m[2] or 1) or 1, 1), 100)
meta:set_int ("distance", distance)
elseif m[1] == "volume" then
local volume = math.min (math.max (tonumber (m[2] or 1) or 1, 1), 100)
meta:set_int ("gain", volume)
elseif m[1] == "sound" then
if m[2] == "buzzer" then
meta:set_int ("sound", 1)
update_form_spec (pos)
elseif m[2] == "horn" then
meta:set_int ("sound", 2)
update_form_spec (pos)
elseif m[2] == "raid" then
meta:set_int ("sound", 3)
update_form_spec (pos)
elseif m[2] == "siren" then
meta:set_int ("sound", 4)
update_form_spec (pos)
end
end
end
end
end,
}
}
end
return nil
end
local function mesecon_support ()
if utils.mesecon_supported then
return
{
effector =
{
rules = utils.mesecon_default_rules,
action_on = function (pos, node)
-- do something to turn the effector on
start_alarm (pos)
end,
action_off = function (pos, node)
-- do something to turn the effector off
stop_alarm (pos)
end,
}
}
end
return nil
end
minetest.register_node("lwcomponents:siren", {
description = S("Siren"),
tiles = { "lwsiren_base.png", "lwsiren_base.png", "lwsiren.png",
"lwsiren.png", "lwsiren.png", "lwsiren.png"},
is_ground_content = false,
groups = { cracky = 3, wires_connect = 1 },
--sounds = default.node_sound_stone_defaults (),
paramtype = "none",
param1 = 0,
floodable = false,
drop = "lwcomponents:siren",
_digistuff_channelcopier_fieldname = "channel",
mesecons = mesecon_support (),
digiline = digilines_support (),
on_destruct = on_destruct,
on_receive_fields = on_receive_fields,
can_dig = can_dig,
after_place_node = after_place_node,
on_blast = on_blast,
on_timer = on_timer,
on_rightclick = on_rightclick
})
minetest.register_node("lwcomponents:siren_locked", {
description = S("Siren (locked)"),
tiles = { "lwsiren_base.png", "lwsiren_base.png", "lwsiren.png",
"lwsiren.png", "lwsiren.png", "lwsiren.png"},
is_ground_content = false,
groups = { cracky = 3, wires_connect = 1 },
--sounds = default.node_sound_stone_defaults (),
paramtype = "none",
param1 = 0,
floodable = false,
drop = "lwcomponents:siren_locked",
_digistuff_channelcopier_fieldname = "channel",
mesecons = mesecon_support (),
digiline = digilines_support (),
on_destruct = on_destruct,
on_receive_fields = on_receive_fields,
can_dig = can_dig,
after_place_node = after_place_node_locked,
on_blast = on_blast,
on_timer = on_timer,
on_rightclick = on_rightclick
})
minetest.register_node("lwcomponents:siren_on", {
description = S("Siren"),
tiles = { "lwsiren_base.png", "lwsiren_base.png", "lwsiren_on.png",
"lwsiren_on.png", "lwsiren_on.png", "lwsiren_on.png"},
is_ground_content = false,
groups = { cracky = 3, not_in_creative_inventory = 1, wires_connect = 1 },
--sounds = default.node_sound_stone_defaults (),
paramtype = "none",
param1 = 0,
floodable = false,
drop = "lwcomponents:siren",
_digistuff_channelcopier_fieldname = "channel",
mesecons = mesecon_support (),
digiline = digilines_support (),
on_destruct = on_destruct,
on_receive_fields = on_receive_fields,
can_dig = can_dig,
after_place_node = after_place_node,
on_blast = on_blast,
on_timer = on_timer,
on_rightclick = on_rightclick
})
minetest.register_node("lwcomponents:siren_locked_on", {
description = S("Siren (locked)"),
tiles = { "lwsiren_base.png", "lwsiren_base.png", "lwsiren_on.png",
"lwsiren_on.png", "lwsiren_on.png", "lwsiren_on.png"},
is_ground_content = false,
groups = { cracky = 3, not_in_creative_inventory = 1, wires_connect = 1 },
--sounds = default.node_sound_stone_defaults (),
paramtype = "none",
param1 = 0,
floodable = false,
drop = "lwcomponents:siren_locked",
_digistuff_channelcopier_fieldname = "channel",
mesecons = mesecon_support (),
digiline = digilines_support (),
on_destruct = on_destruct,
on_receive_fields = on_receive_fields,
can_dig = can_dig,
after_place_node = after_place_node_locked,
on_blast = on_blast,
on_timer = on_timer,
on_rightclick = on_rightclick
})
minetest.register_node("lwcomponents:siren_alarm", {
description = S("Siren"),
tiles = { "lwsiren_base.png", "lwsiren_base.png", "lwsiren_alarm.png",
"lwsiren_alarm.png", "lwsiren_alarm.png", "lwsiren_alarm.png"},
is_ground_content = false,
groups = { cracky = 3, not_in_creative_inventory = 1, wires_connect = 1 },
--sounds = default.node_sound_stone_defaults (),
paramtype = "none",
param1 = 0,
light_source = 3,
floodable = false,
drop = "lwcomponents:siren",
_digistuff_channelcopier_fieldname = "channel",
mesecons = mesecon_support (),
digiline = digilines_support (),
on_destruct = on_destruct,
on_receive_fields = on_receive_fields,
can_dig = can_dig,
after_place_node = after_place_node,
on_blast = on_blast,
on_timer = on_timer,
on_rightclick = on_rightclick
})
minetest.register_node("lwcomponents:siren_locked_alarm", {
description = S("Siren (locked)"),
tiles = { "lwsiren_base.png", "lwsiren_base.png", "lwsiren_alarm.png",
"lwsiren_alarm.png", "lwsiren_alarm.png", "lwsiren_alarm.png"},
is_ground_content = false,
groups = { cracky = 3, not_in_creative_inventory = 1, wires_connect = 1 },
--sounds = default.node_sound_stone_defaults (),
paramtype = "none",
param1 = 0,
light_source = 3,
floodable = false,
drop = "lwcomponents:siren_locked",
_digistuff_channelcopier_fieldname = "channel",
mesecons = mesecon_support (),
digiline = digilines_support (),
on_destruct = on_destruct,
on_receive_fields = on_receive_fields,
can_dig = can_dig,
after_place_node = after_place_node_locked,
on_blast = on_blast,
on_timer = on_timer,
on_rightclick = on_rightclick
})
end -- utils.digilines_supported or utils.mesecon_supported
--

View file

@ -0,0 +1,146 @@
local utils = ...
local S = utils.S
if utils.unifieddyes_supported and utils.mesecon_supported then
mesecon.register_node (":lwcomponents:solid_conductor",
{
description = S("Solid Color Conductor"),
tiles = { "lwsolid_conductor.png" },
is_ground_content = false,
sounds = ( default and default.node_sound_wood_defaults() ),
paramtype2 = "color",
palette = "unifieddyes_palette_extended.png",
on_rotate = false,
drop = "lwcomponents:solid_conductor_off",
digiline = { wire = { rules = utils.digilines_default_rules } },
on_construct = unifieddyes.on_construct,
on_dig = unifieddyes.on_dig,
},
{
tiles = { "lwsolid_conductor.png" },
mesecons =
{
conductor =
{
rules = utils.mesecon_default_rules,
state = utils.mesecon_state_off,
onstate = "lwcomponents:solid_conductor_on",
}
},
groups = {
dig_immediate = 2,
ud_param2_colorable = 1,
wires_connect = 1
},
},
{
tiles = { "lwsolid_conductor.png" },
mesecons =
{
conductor =
{
rules = utils.mesecon_default_rules,
state = utils.mesecon_state_on,
offstate = "lwcomponents:solid_conductor_off",
}
},
groups = {
dig_immediate = 2,
ud_param2_colorable = 1,
not_in_creative_inventory = 1,
wires_connect = 1
},
}
)
unifieddyes.register_color_craft ({
output = "lwcomponents:solid_conductor_off 3",
palette = "extended",
type = "shapeless",
neutral_node = "lwcomponents:solid_conductor_off",
recipe = {
"NEUTRAL_NODE",
"NEUTRAL_NODE",
"NEUTRAL_NODE",
"MAIN_DYE"
}
})
mesecon.register_node (":lwcomponents:solid_horizontal_conductor",
{
description = S("Solid Color Horizontal Conductor"),
tiles = { "lwsolid_conductor.png" },
is_ground_content = false,
sounds = ( default and default.node_sound_wood_defaults() ),
paramtype2 = "color",
palette = "unifieddyes_palette_extended.png",
on_rotate = false,
drop = "lwcomponents:solid_horizontal_conductor_off",
digiline = { wire = { rules = utils.digilines_flat_rules } },
on_construct = unifieddyes.on_construct,
on_dig = unifieddyes.on_dig,
},
{
tiles = { "lwsolid_conductor.png" },
mesecons =
{
conductor =
{
rules = utils.mesecon_flat_rules,
state = utils.mesecon_state_off,
onstate = "lwcomponents:solid_horizontal_conductor_on",
}
},
groups = {
dig_immediate = 2,
ud_param2_colorable = 1,
wires_connect = 1
},
},
{
tiles = { "lwsolid_conductor.png" },
mesecons =
{
conductor =
{
rules = utils.mesecon_flat_rules,
state = utils.mesecon_state_on,
offstate = "lwcomponents:solid_horizontal_conductor_off",
}
},
groups = {
dig_immediate = 2,
ud_param2_colorable = 1,
not_in_creative_inventory = 1,
wires_connect = 1
},
}
)
unifieddyes.register_color_craft ({
output = "lwcomponents:solid_horizontal_conductor_off 3",
palette = "extended",
type = "shapeless",
neutral_node = "lwcomponents:solid_horizontal_conductor_off",
recipe = {
"NEUTRAL_NODE",
"NEUTRAL_NODE",
"NEUTRAL_NODE",
"MAIN_DYE"
}
})
end -- utils.unifieddyes_supported and utils.mesecon_supported then

1495
lwcomponents/storage.lua Normal file

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,80 @@
local utils = ...
local S = utils.S
if utils.mesecon_supported then
local through_wire_get_rules = function (node)
local rules = { {x = -1, y = 0, z = 0},
{x = 2, y = 0, z = 0},
{x = 3, y = 0, z = 0} }
if node.param2 == 2 then
rules = mesecon.rotate_rules_left(rules)
elseif node.param2 == 3 then
rules = mesecon.rotate_rules_right(mesecon.rotate_rules_right(rules))
elseif node.param2 == 0 then
rules = mesecon.rotate_rules_right(rules)
end
return rules
end
mesecon.register_node ("lwcomponents:through_wire", {
description = S("Mesecons Through Wire"),
drawtype = "nodebox",
paramtype = "light",
paramtype2 = "facedir",
is_ground_content = false,
sunlight_propagates = true,
walkable = false,
on_rotate = false,
selection_box = {
type = "fixed",
fixed = { -3/16, -8/16, -8/16, 3/16, 3/16, 8/16 }
},
node_box = {
type = "fixed",
fixed = {
{ -3/16, -3/16, 13/32 , 3/16, 3/16 , 8/16 }, -- the smaller bump
{ -1/32, -1/32, 1/2 , 1/32, 1/32 , 3/2 }, -- the wire through the block
{ -2/32, -1/2 , 0.5002-3/32 , 2/32, 0 , 0.5 }, -- the vertical wire bit
{ -2/32, -1/2 , -16/32+0.001 , 2/32, -14/32, 7/16+0.002 } -- the horizontal wire
}
},
drop = "lwcomponents:through_wire_off",
--sounds = default.node_sound_defaults(),
}, {
tiles = { "mesecons_wire_off.png" },
groups = { dig_immediate = 3 },
mesecons = {
conductor = {
state = mesecon.state.off,
rules = through_wire_get_rules,
onstate = "lwcomponents:through_wire_on"
}
}
}, {
tiles = { "mesecons_wire_on.png" },
groups = { dig_immediate = 3, not_in_creative_inventory = 1 },
mesecons = {
conductor = {
state = mesecon.state.on,
rules = through_wire_get_rules,
offstate = "lwcomponents:through_wire_off"
}
}
})
end -- utils.mesecon_supported
--

471
lwcomponents/utils.lua Normal file
View file

@ -0,0 +1,471 @@
local utils = ...
if minetest.get_translator and minetest.get_translator ("lwcomponents") then
utils.S = minetest.get_translator ("lwcomponents")
elseif minetest.global_exists ("intllib") then
if intllib.make_gettext_pair then
utils.S = intllib.make_gettext_pair ()
else
utils.S = intllib.Getter ()
end
else
utils.S = function (s) return s end
end
-- check for mesecon
if minetest.global_exists ("mesecon") then
utils.mesecon_supported = true
utils.mesecon_state_on = mesecon.state.on
utils.mesecon_state_off = mesecon.state.off
utils.mesecon_receptor_on = mesecon.receptor_on
utils.mesecon_receptor_off = mesecon.receptor_off
utils.mesecon_default_rules = mesecon.rules.default
utils.mesecon_flat_rules = mesecon.rules.flat
else
utils.mesecon_supported = false
utils.mesecon_state_on = "on"
utils.mesecon_state_off = "off"
utils.mesecon_default_rules = { }
utils.mesecon_flat_rules = { }
-- dummies
utils.mesecon_receptor_on = function (pos, rules)
end
utils.mesecon_receptor_off = function (pos, rules)
end
end
-- check for digilines
if minetest.global_exists ("digilines") then
utils.digilines_supported = true
utils.digilines_default_rules = digiline.rules.default
utils.digilines_flat_rules = {
{ x = 1, y = 0, z = 0 },
{ x = -1, y = 0, z = 0 },
{ x = 0, y = 0, z = 1 },
{ x = 0, y = 0, z = -1 },
}
utils.digilines_receptor_send = digilines.receptor_send
else
utils.digilines_supported = false
utils.digilines_default_rules = { }
utils.digilines_flat_rules = { }
-- dummy
utils.digilines_receptor_send = function (pos, rules, channel, msg)
end
end
-- check for unifieddyes
if minetest.global_exists ("unifieddyes") then
utils.unifieddyes_supported = true
else
utils.unifieddyes_supported = false
end
-- check for hopper
if minetest.global_exists ("hopper") then
utils.hopper_supported = true
utils.hopper_add_container = function (list)
hopper:add_container (list)
end
else
utils.hopper_supported = false
utils.hopper_add_container = function (list)
end
end
-- check for digistuff
if minetest.global_exists ("digistuff") then
utils.digistuff_supported = true
else
utils.digistuff_supported = false
end
-- check for pipeworks
if minetest.global_exists ("pipeworks") then
utils.pipeworks_supported = true
utils.pipeworks_after_place = pipeworks.after_place
utils.pipeworks_after_dig = pipeworks.after_dig
else
utils.pipeworks_supported = false
utils.pipeworks_after_place = function (pos)
end
utils.pipeworks_after_dig = function (pos)
end
end
function utils.on_destroy (itemstack)
local stack = ItemStack (itemstack)
if stack and stack:get_count () > 0 then
local def = utils.find_item_def (stack:get_name ())
if def and def.on_destroy then
def.on_destroy (stack)
end
end
end
function utils.item_pickup (entity, cleanup)
local stack = nil
if entity and entity.name and entity.name == "__builtin:item" and
entity.itemstring and entity.itemstring ~= "" then
stack = ItemStack (entity.itemstring)
if cleanup ~= false then
entity.itemstring = ""
entity.object:remove ()
end
end
return stack
end
function utils.item_drop (itemstack, dropper, pos)
if itemstack then
local def = utils.find_item_def (itemstack:get_name ())
if def and def.on_drop then
return def.on_drop (itemstack, dropper, pos)
end
end
return minetest.item_drop (itemstack, dropper, pos)
end
function utils.can_interact_with_node (pos, player)
if not player or not player:is_player () then
return false
end
if minetest.check_player_privs (player, "protection_bypass") then
return true
end
local meta = minetest.get_meta (pos)
if meta then
local owner = meta:get_string ("owner")
local name = player:get_player_name ()
if not owner or owner == "" or owner == name then
return true
end
end
return false
end
function utils.get_far_node (pos)
local node = minetest.get_node (pos)
if node.name == "ignore" then
minetest.get_voxel_manip ():read_from_map (pos, pos)
node = minetest.get_node (pos)
if node.name == "ignore" then
return nil
end
end
return node
end
function utils.find_item_def (name)
local def = minetest.registered_items[name]
if not def then
def = minetest.registered_craftitems[name]
end
if not def then
def = minetest.registered_nodes[name]
end
if not def then
def = minetest.registered_tools[name]
end
return def
end
function utils.table_equal (t1, t2)
for k, v in pairs (t1) do
if type (t2[k]) ~= type (v) then
return false
end
if type (v) == "table" then
if not utils.table_equal (v, t2[k]) then
return false
end
elseif v ~= t2[k] then
return false
end
end
for k, v in pairs (t2) do
if type (t1[k]) ~= type (v) then
return false
end
if type (v) == "table" then
if not utils.table_equal (v, t1[k]) then
return false
end
elseif v ~= t1[k] then
return false
end
end
return true
end
function utils.is_same_item (item1, item2)
local copy1 = ItemStack (item1)
local copy2 = ItemStack (item2)
if copy1 and copy2 then
copy1:set_count (1)
copy2:set_count (1)
return utils.table_equal (copy1:to_table (), copy2:to_table ())
end
return false
end
function utils.unescape_description (description)
description = description:gsub (string.char (27, 70), ""):
gsub (string.char (27, 69), ""):
gsub ("\n", " ")
local first = description:find (string.char (27, 40, 84, 64), 1, true)
while first do
local last = description:find (")", first + 4, true)
if not last then
last = first + 3
end
description = description:sub (1, first - 1)..description:sub (last + 1)
first = description:find (string.char (27, 40, 84, 64), 1, true)
end
return description
end
function utils.is_drop (obj)
if obj then
local entity = obj.get_luaentity and obj:get_luaentity ()
return (entity and entity.name and entity.name == "__builtin:item")
end
return false
end
function utils.destroy_node (pos)
local node = utils.get_far_node (pos)
if node then
local items = minetest.get_node_drops (node, nil)
if items then
for i = 1, #items do
local stack = ItemStack (items[i])
if stack and not stack:is_empty () then
local name = stack:get_name ()
local def = utils.find_item_def (name)
if def then
if def.preserve_metadata then
def.preserve_metadata (pos, node, minetest.get_meta (pos), { stack })
end
utils.on_destroy (stack)
end
end
end
end
minetest.remove_node (pos)
end
end
utils.registered_spawners = { }
-- each entry [spawner_itemname] = spawner_func
function utils.register_spawner (itemname, spawn_func)
if type (itemname) == "string" and type (spawn_func) == "function" then
if not utils.registered_spawners[itemname] then
utils.registered_spawners[itemname] = spawn_func
return true
end
end
return false
end
function utils.spawn_registered (itemname, spawn_pos, itemstack, owner, spawner_pos, spawner_dir, force)
local func = utils.registered_spawners[itemname]
if func then
local result, obj, cancel = pcall (func, spawn_pos, itemstack, owner, spawner_pos, spawner_dir, force)
if result and (obj == nil or type (obj) == "userdata" or type (obj) == "table") then
return obj, cancel
end
minetest.log ("error", "lwcomponents.register_spawner spawner function for "..itemname.." failed "..
((type (obj) == "string" and obj) or ""))
return nil, true
end
return nil, false
end
function utils.can_place (pos)
local node = minetest.get_node_or_nil (pos)
if node and node.name ~= "air" then
local def = minetest.registered_nodes[node.name]
if not def or not def.buildable_to then
return false
end
end
return true
end
function utils.is_protected (pos, player)
local name = (player and player:get_player_name ()) or ""
return minetest.is_protected (pos, name)
end
function utils.get_on_rightclick (pos, player)
local node = minetest.get_node_or_nil (pos)
if node then
local def = minetest.registered_nodes[node.name]
if def and def.on_rightclick and
not (player and player:is_player () and
player:get_player_control ().sneak) then
return def.on_rightclick
end
end
return nil
end
function utils.is_creative (player)
if minetest.settings:get_bool ("creative_mode") then
return true
end
if player and player:is_player () then
return minetest.is_creative_enabled (player:get_player_name ()) or
minetest.check_player_privs (player, "creative")
end
return false
end
local crafting_mods = dofile (minetest.get_modpath ("lwcomponents").."/crafting_mods.lua")
function utils.get_crafting_mods (item)
return crafting_mods[item]
end
function utils.hex_decode (hex)
return (hex:gsub ("%x%x", function (digits)
return string.char (tonumber (digits, 16))
end))
end
function utils.hex_encode (str)
return (str:gsub (".", function (char)
return string.format ("%2x", char:byte ())
end))
end
--