Improve object and node detectors

* Support detection of multiple players in object detector
* Add distance parameter to node detector
* Fix area protection
This commit is contained in:
Josh Mars 2017-03-13 16:13:57 -04:00 committed by Jeija
parent 967bde284a
commit 2554164674
3 changed files with 38 additions and 13 deletions

View File

@ -4,3 +4,4 @@ It can also receive digiline signals. You can either send "GET" and it will
respond with the detected nodename or you can send any other string and it will respond with the detected nodename or you can send any other string and it will
set this string as the node to scan for. set this string as the node to scan for.
Nodenames must include the mod they reside in, so for instance default:dirt, not just dirt. Nodenames must include the mod they reside in, so for instance default:dirt, not just dirt.
The distance parameter specifies how many blocks are between the node detector and the node to detect.

View File

@ -1,3 +1,4 @@
The object detector is a receptor. It changes its state when a player approaches. The object detector is a receptor. It changes its state when a player approaches.
Right-click it to set a name to scan for. Right-click it to set a name to scan for.
You can also search for comma-separated lists of players where the detector gets activated if any of the names in the list are found.
It can also receive digiline signals which are the name to scan for on the specified channel in the right-click menu. It can also receive digiline signals which are the name to scan for on the specified channel in the right-click menu.

View File

@ -5,15 +5,18 @@ local GET_COMMAND = "GET"
-- The radius can be specified in mesecons/settings.lua -- The radius can be specified in mesecons/settings.lua
local function object_detector_make_formspec(pos) local function object_detector_make_formspec(pos)
minetest.get_meta(pos):set_string("formspec", "size[9,2.5]" .. local meta = minetest.get_meta(pos)
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, 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}]".. "field[0.3,1.5;4,2;digiline_channel;Digiline Channel (optional):;${digiline_channel}]"..
"button_exit[7,0.75;2,3;;Save]") "button_exit[7,0.75;2,3;;Save]")
end end
local function object_detector_on_receive_fields(pos, _, fields) local function object_detector_on_receive_fields(pos, formname, fields, sender)
if not fields.scanname or not fields.digiline_channel then return end if not fields.scanname or not fields.digiline_channel then return end
if minetest.is_protected(pos, sender:get_player_name()) then return end
local meta = minetest.get_meta(pos) local meta = minetest.get_meta(pos)
meta:set_string("scanname", fields.scanname) meta:set_string("scanname", fields.scanname)
meta:set_string("digiline_channel", fields.digiline_channel) meta:set_string("digiline_channel", fields.digiline_channel)
@ -28,14 +31,17 @@ local function object_detector_scan(pos)
if next(objs) == nil then return false end if next(objs) == nil then return false end
local scanname = minetest.get_meta(pos):get_string("scanname") local scanname = minetest.get_meta(pos):get_string("scanname")
local scan_for = {}
for _, str in pairs(string.split(scanname:gsub(" ", ""), ",")) do
scan_for[str] = true
end
local every_player = scanname == "" local every_player = scanname == ""
for _, obj in pairs(objs) do for _, obj in pairs(objs) do
-- "" is returned if it is not a player; "" ~= nil; so only handle objects with foundname ~= "" -- "" is returned if it is not a player; "" ~= nil; so only handle objects with foundname ~= ""
local foundname = obj:get_player_name() local foundname = obj:get_player_name()
if foundname ~= "" then if foundname ~= "" then
-- return true if scanning for any player or if specific playername was detected if every_player or scan_for[foundname] then
if scanname == "" or foundname == scanname then
return true return true
end end
end end
@ -128,17 +134,23 @@ minetest.register_abm({
-- Detects the node in front of it -- Detects the node in front of it
local function node_detector_make_formspec(pos) local function node_detector_make_formspec(pos)
minetest.get_meta(pos):set_string("formspec", "size[9,2.5]" .. local meta = minetest.get_meta(pos)
if meta:get_string("distance") == "" then meta:set_string("distance", "0") end
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}]".. "field[0.3, 0;9,2;scanname;Name of node to scan for (empty for any):;${scanname}]"..
"field[0.3,1.5;4,2;digiline_channel;Digiline Channel (optional):;${digiline_channel}]".. "field[0.3,1.5;2.5,2;distance;Distance (0-"..mesecon.setting("node_detector_distance_max", 10).."):;${distance}]"..
"field[3,1.5;4,2;digiline_channel;Digiline Channel (optional):;${digiline_channel}]"..
"button_exit[7,0.75;2,3;;Save]") "button_exit[7,0.75;2,3;;Save]")
end end
local function node_detector_on_receive_fields(pos, _, fields) local function node_detector_on_receive_fields(pos, fieldname, fields, sender)
if not fields.scanname or not fields.digiline_channel then return end if not fields.scanname or not fields.digiline_channel then return end
if minetest.is_protected(pos, sender:get_player_name()) then return end
local meta = minetest.get_meta(pos) local meta = minetest.get_meta(pos)
meta:set_string("scanname", fields.scanname) meta:set_string("scanname", fields.scanname)
meta:set_string("distance", fields.distance or "0")
meta:set_string("digiline_channel", fields.digiline_channel) meta:set_string("digiline_channel", fields.digiline_channel)
node_detector_make_formspec(pos) node_detector_make_formspec(pos)
end end
@ -148,10 +160,17 @@ local function node_detector_scan(pos)
local node = minetest.get_node_or_nil(pos) local node = minetest.get_node_or_nil(pos)
if not node then return end if not node then return end
local meta = minetest.get_meta(pos)
local distance = meta:get_int("distance")
local distance_max = mesecon.setting("node_detector_distance_max", 10)
if distance < 0 then distance = 0 end
if distance > distance_max then distance = distance_max end
local frontname = minetest.get_node( local frontname = minetest.get_node(
vector.subtract(pos, minetest.facedir_to_dir(node.param2)) vector.subtract(pos, vector.multiply(minetest.facedir_to_dir(node.param2), distance + 1))
).name ).name
local scanname = minetest.get_meta(pos):get_string("scanname") local scanname = meta:get_string("scanname")
return (frontname == scanname) or return (frontname == scanname) or
(frontname ~= "air" and frontname ~= "ignore" and scanname == "") (frontname ~= "air" and frontname ~= "ignore" and scanname == "")
@ -162,11 +181,17 @@ local node_detector_digiline = {
effector = { effector = {
action = function(pos, node, channel, msg) action = function(pos, node, channel, msg)
local meta = minetest.get_meta(pos) local meta = minetest.get_meta(pos)
local distance = meta:get_int("distance")
local distance_max = mesecon.setting("node_detector_distance_max", 10)
if distance < 0 then distance = 0 end
if distance > distance_max then distance = distance_max end
if channel ~= meta:get_string("digiline_channel") then return end if channel ~= meta:get_string("digiline_channel") then return end
if msg == GET_COMMAND then if msg == GET_COMMAND then
local nodename = minetest.get_node( local nodename = minetest.get_node(
vector.subtract(pos, minetest.facedir_to_dir(node.param2)) vector.subtract(pos, vector.multiply(minetest.facedir_to_dir(node.param2), distance + 1))
).name ).name
digiline:receptor_send(pos, digiline.rules.default, channel, nodename) digiline:receptor_send(pos, digiline.rules.default, channel, nodename)
@ -208,7 +233,6 @@ minetest.register_node("mesecons_detector:node_detector_off", {
}}, }},
on_construct = node_detector_make_formspec, on_construct = node_detector_make_formspec,
on_receive_fields = node_detector_on_receive_fields, on_receive_fields = node_detector_on_receive_fields,
after_place_node = after_place_node_detector,
sounds = default.node_sound_stone_defaults(), sounds = default.node_sound_stone_defaults(),
digiline = node_detector_digiline digiline = node_detector_digiline
}) })
@ -225,7 +249,6 @@ minetest.register_node("mesecons_detector:node_detector_on", {
}}, }},
on_construct = node_detector_make_formspec, on_construct = node_detector_make_formspec,
on_receive_fields = node_detector_on_receive_fields, on_receive_fields = node_detector_on_receive_fields,
after_place_node = after_place_node_detector,
sounds = default.node_sound_stone_defaults(), sounds = default.node_sound_stone_defaults(),
digiline = node_detector_digiline digiline = node_detector_digiline
}) })