mesecons/mesecons_detector/init.lua

318 lines
10 KiB
Lua
Raw Normal View History

local GET_COMMAND = "GET"
-- Object detector
-- Detects players in a certain radius
-- The radius can be specified in mesecons/settings.lua
2017-03-15 16:47:31 +01:00
local function can_dig(pos, player)
local meta = minetest.get_meta(pos)
local owner = meta:get_string("owner")
return owner == "" or owner == player:get_player_name()
end
local function object_detector_make_formspec(pos)
2017-03-13 21:13:57 +01:00
local meta = minetest.get_meta(pos)
2017-03-14 17:34:42 +01:00
meta:set_string("formspec", "size[9,2.5]" ..
"field[0.3, 0;9,2;scanname;Name of player to scan for (empty for any):;${scanname}]"..
"field[0.3,1.5;4,2;digiline_channel;Digiline Channel (optional):;${digiline_channel}]"..
"button_exit[7,0.75;2,3;;Save]")
if not meta:get_string("owner") then meta:set_string("owner", "") end
2017-03-13 21:13:57 +01:00
end
local function after_place(pos, placer)
if placer then
minetest.get_meta(pos):set_string("owner", placer:get_player_name())
end
end
2017-03-13 21:13:57 +01:00
local function object_detector_on_receive_fields(pos, formname, fields, sender)
if not fields.scanname or not fields.digiline_channel then return end
2014-04-30 14:44:47 +02:00
local meta = minetest.get_meta(pos)
2017-03-13 21:13:57 +01:00
local owner = meta:get_string("owner")
if owner ~= "" and sender:get_player_name() ~= owner then
minetest.chat_send_player(sender:get_player_name(), "This detector is owned by "..owner.."!")
return
2017-03-14 17:34:42 +01:00
end
meta:set_string("scanname", fields.scanname)
meta:set_string("digiline_channel", fields.digiline_channel)
object_detector_make_formspec(pos)
end
-- returns true if player was found, false if not
local function object_detector_scan(pos)
local objs = minetest.get_objects_inside_radius(pos, mesecon.setting("detector_radius", 6))
-- abort if no scan results were found
if next(objs) == nil then return false end
local scanname = minetest.get_meta(pos):get_string("scanname")
2017-03-13 21:13:57 +01:00
local sep = "," -- Begin code to split scanname into array
2017-03-14 17:34:42 +01:00
local t={}
2017-03-13 21:13:57 +01:00
for str in string.gmatch(scanname, "([^"..sep.."]+)") do
2017-03-14 17:34:42 +01:00
t[str:gsub("%s+", "")] = true
2017-03-13 21:13:57 +01:00
end -- end split code
local every_player = scanname == ""
for _, obj in pairs(objs) do
-- "" is returned if it is not a player; "" ~= nil; so only handle objects with foundname ~= ""
local foundname = obj:get_player_name()
if foundname ~= "" then
2017-03-14 17:34:42 +01:00
for _ in pairs(t) do
if t[foundname] then return true end
end
end
2017-03-14 17:34:42 +01:00
if scanname == "" then return true end -- Return true if no player name was specified
end
return false
end
-- set player name when receiving a digiline signal on a specific channel
local object_detector_digiline = {
effector = {
action = function(pos, node, channel, msg)
local meta = minetest.get_meta(pos)
if channel == meta:get_string("digiline_channel") then
meta:set_string("scanname", msg)
object_detector_make_formspec(pos)
end
end,
}
}
minetest.register_node("mesecons_detector:object_detector_off", {
tiles = {"default_steel_block.png", "default_steel_block.png", "jeija_object_detector_off.png", "jeija_object_detector_off.png", "jeija_object_detector_off.png", "jeija_object_detector_off.png"},
paramtype = "light",
walkable = true,
groups = {cracky=3},
description="Player Detector",
mesecons = {receptor = {
state = mesecon.state.off,
rules = mesecon.rules.pplate
}},
2017-03-13 21:13:57 +01:00
on_construct = object_detector_make_formspec,
2017-03-15 16:47:31 +01:00
can_dig = can_dig,
2017-03-13 21:13:57 +01:00
after_place_node = after_place,
on_receive_fields = object_detector_on_receive_fields,
sounds = default.node_sound_stone_defaults(),
digiline = object_detector_digiline
})
minetest.register_node("mesecons_detector:object_detector_on", {
tiles = {"default_steel_block.png", "default_steel_block.png", "jeija_object_detector_on.png", "jeija_object_detector_on.png", "jeija_object_detector_on.png", "jeija_object_detector_on.png"},
paramtype = "light",
walkable = true,
groups = {cracky=3,not_in_creative_inventory=1},
drop = 'mesecons_detector:object_detector_off',
mesecons = {receptor = {
state = mesecon.state.on,
rules = mesecon.rules.pplate
}},
on_construct = object_detector_make_formspec,
2017-03-15 16:47:31 +01:00
can_dig = can_dig,
2017-03-13 21:13:57 +01:00
after_place_node = after_place,
on_receive_fields = object_detector_on_receive_fields,
sounds = default.node_sound_stone_defaults(),
digiline = object_detector_digiline
})
minetest.register_craft({
output = 'mesecons_detector:object_detector_off',
recipe = {
{"default:steel_ingot", "default:steel_ingot", "default:steel_ingot"},
{"default:steel_ingot", "mesecons_luacontroller:luacontroller0000", "default:steel_ingot"},
{"default:steel_ingot", "group:mesecon_conductor_craftable", "default:steel_ingot"},
}
})
minetest.register_abm({
nodenames = {"mesecons_detector:object_detector_off"},
interval = 1,
chance = 1,
action = function(pos, node)
if not object_detector_scan(pos) then return end
node.name = "mesecons_detector:object_detector_on"
minetest.swap_node(pos, node)
mesecon.receptor_on(pos, mesecon.rules.pplate)
end,
})
minetest.register_abm({
nodenames = {"mesecons_detector:object_detector_on"},
interval = 1,
chance = 1,
action = function(pos, node)
if object_detector_scan(pos) then return end
node.name = "mesecons_detector:object_detector_off"
minetest.swap_node(pos, node)
mesecon.receptor_off(pos, mesecon.rules.pplate)
end,
})
-- Node detector
-- Detects the node in front of it
local function node_detector_make_formspec(pos)
2017-03-13 21:13:57 +01:00
local meta = minetest.get_meta(pos)
meta:set_string("formspec", "size[9,2.5]" ..
"field[0.3, 0;9,2;scanname;Name of node to scan for (empty for any):;${scanname}]"..
2017-03-13 21:13:57 +01:00
"field[0.3,1.5;2,2;distance;Distance (0-10):;${distance}]"..
"field[2.5,1.5;4,2;digiline_channel;Digiline Channel (optional):;${digiline_channel}]"..
"button_exit[7,0.75;2,3;;Save]")
2017-03-13 21:13:57 +01:00
if not meta:get_string("owner") then meta:set_string("owner", "") end
end
2017-03-13 21:13:57 +01:00
local function node_detector_on_receive_fields(pos, fieldname, fields, sender)
if not fields.scanname or not fields.digiline_channel or not fields.distance then return end
local meta = minetest.get_meta(pos)
2017-03-13 21:13:57 +01:00
local owner = meta:get_string("owner")
if owner ~= "" and sender:get_player_name() ~= owner then
minetest.chat_send_player(sender:get_player_name(), "This detector is owned by "..owner.."!")
return
end
meta:set_string("scanname", fields.scanname)
2017-03-13 21:13:57 +01:00
meta:set_string("distance", fields.distance)
meta:set_string("digiline_channel", fields.digiline_channel)
node_detector_make_formspec(pos)
end
-- returns true if node was found, false if not
local function node_detector_scan(pos)
local node = minetest.get_node_or_nil(pos)
if not node then return end
2017-03-15 16:39:05 +01:00
local meta = minetest.get_meta(pos)
local distance = meta:get_int("distance")
if distance < 0 then distance = 0 end
if distance > 10 then distance = 10 end
local frontname = minetest.get_node(
2017-03-14 17:34:42 +01:00
vector.subtract(pos, vector.multiply(minetest.facedir_to_dir(node.param2), distance+1))
).name
2017-03-15 16:39:05 +01:00
local scanname = meta:get_string("scanname")
return (frontname == scanname) or
(frontname ~= "air" and frontname ~= "ignore" and scanname == "")
end
-- set player name when receiving a digiline signal on a specific channel
local node_detector_digiline = {
effector = {
action = function(pos, node, channel, msg)
local meta = minetest.get_meta(pos)
if channel ~= meta:get_string("digiline_channel") then return end
if msg == GET_COMMAND then
local nodename = minetest.get_node(
vector.subtract(pos, minetest.facedir_to_dir(node.param2))
).name
digiline:receptor_send(pos, digiline.rules.default, channel, nodename)
else
meta:set_string("scanname", msg)
node_detector_make_formspec(pos)
end
end,
},
receptor = {}
}
local function after_place_node_detector(pos, placer)
local placer_pos = placer:getpos()
if not placer_pos then
return
end
--correct for the player's height
if placer:is_player() then
placer_pos.y = placer_pos.y + 1.625
end
--correct for 6d facedir
local node = minetest.get_node(pos)
node.param2 = minetest.dir_to_facedir(vector.subtract(pos, placer_pos), true)
minetest.set_node(pos, node)
2017-03-13 21:13:57 +01:00
if placer then
minetest.get_meta(pos):set_string("owner", placer:get_player_name())
end
end
minetest.register_node("mesecons_detector:node_detector_off", {
tiles = {"default_steel_block.png", "default_steel_block.png", "default_steel_block.png", "default_steel_block.png", "default_steel_block.png", "jeija_node_detector_off.png"},
paramtype = "light",
paramtype2 = "facedir",
walkable = true,
groups = {cracky=3},
description="Node Detector",
mesecons = {receptor = {
state = mesecon.state.off
}},
on_construct = node_detector_make_formspec,
2017-03-15 16:47:31 +01:00
can_dig = can_dig,
on_receive_fields = node_detector_on_receive_fields,
after_place_node = after_place_node_detector,
sounds = default.node_sound_stone_defaults(),
digiline = node_detector_digiline
})
minetest.register_node("mesecons_detector:node_detector_on", {
tiles = {"default_steel_block.png", "default_steel_block.png", "default_steel_block.png", "default_steel_block.png", "default_steel_block.png", "jeija_node_detector_on.png"},
paramtype = "light",
paramtype2 = "facedir",
walkable = true,
groups = {cracky=3,not_in_creative_inventory=1},
drop = 'mesecons_detector:node_detector_off',
mesecons = {receptor = {
state = mesecon.state.on
}},
on_construct = node_detector_make_formspec,
2017-03-15 16:47:31 +01:00
can_dig = can_dig,
on_receive_fields = node_detector_on_receive_fields,
after_place_node = after_place_node_detector,
sounds = default.node_sound_stone_defaults(),
digiline = node_detector_digiline
})
minetest.register_craft({
output = 'mesecons_detector:node_detector_off',
recipe = {
{"default:steel_ingot", "group:mesecon_conductor_craftable", "default:steel_ingot"},
{"default:steel_ingot", "mesecons_luacontroller:luacontroller0000", "default:steel_ingot"},
{"default:steel_ingot", "default:steel_ingot", "default:steel_ingot"},
}
})
minetest.register_abm({
nodenames = {"mesecons_detector:node_detector_off"},
interval = 1,
chance = 1,
action = function(pos, node)
if not node_detector_scan(pos) then return end
node.name = "mesecons_detector:node_detector_on"
minetest.swap_node(pos, node)
mesecon.receptor_on(pos)
end,
})
minetest.register_abm({
nodenames = {"mesecons_detector:node_detector_on"},
interval = 1,
chance = 1,
action = function(pos, node)
if node_detector_scan(pos) then return end
node.name = "mesecons_detector:node_detector_off"
minetest.swap_node(pos, node)
mesecon.receptor_off(pos)
end,
})