summaryrefslogtreecommitdiff
path: root/mesecons_detector
diff options
context:
space:
mode:
Diffstat (limited to 'mesecons_detector')
-rw-r--r--mesecons_detector/init.lua91
1 files changed, 43 insertions, 48 deletions
diff --git a/mesecons_detector/init.lua b/mesecons_detector/init.lua
index edef641..65e5c6e 100644
--- a/mesecons_detector/init.lua
+++ b/mesecons_detector/init.lua
@@ -12,10 +12,7 @@ local function object_detector_make_formspec(pos)
end
local function object_detector_on_receive_fields(pos, _, fields)
- if not fields.scanname
- or not fields.digiline_channel then
- return
- end
+ if not fields.scanname or not fields.digiline_channel then return end
local meta = minetest.get_meta(pos)
meta:set_string("scanname", fields.scanname)
@@ -26,19 +23,24 @@ 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))
- if not next(objs) then
- return false
- end
+
+ -- abort if no scan results were found
+ if next(objs) == nil then return false end
local scanname = minetest.get_meta(pos):get_string("scanname")
local every_player = scanname == ""
- for _,obj in pairs(objs) do
- local isname = obj:get_player_name() -- "" is returned if it is not a player; "" ~= nil!
- if isname ~= ""
- and (every_player or isname == scanname) then -- no scanname specified or player with scanname found
- return true
+ 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
+ -- return true if scanning for any player or if specific playername was detected
+ if scanname == "" or foundname == scanname then
+ return true
+ end
end
end
+
return false
end
@@ -101,9 +103,8 @@ minetest.register_abm({
interval = 1,
chance = 1,
action = function(pos, node)
- if not object_detector_scan(pos) then
- return
- end
+ 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)
@@ -115,9 +116,8 @@ minetest.register_abm({
interval = 1,
chance = 1,
action = function(pos, node)
- if object_detector_scan(pos) then
- return
- end
+ 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)
@@ -135,10 +135,7 @@ local function node_detector_make_formspec(pos)
end
local function node_detector_on_receive_fields(pos, _, fields)
- if not fields.scanname
- or not fields.digiline_channel then
- return
- end
+ if not fields.scanname or not fields.digiline_channel then return end
local meta = minetest.get_meta(pos)
meta:set_string("scanname", fields.scanname)
@@ -146,18 +143,18 @@ local function node_detector_on_receive_fields(pos, _, fields)
node_detector_make_formspec(pos)
end
--- returns true if player was found, false if not
+-- 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
+ if not node then return end
+
local frontname = minetest.get_node(
vector.subtract(pos, minetest.facedir_to_dir(node.param2))
).name
- local meta = minetest.get_meta(pos)
- return (frontname == meta:get_string("scanname")) or
- (frontname ~= "air" and frontname ~= "ignore" and meta:get_string("scanname") == "")
+ local scanname = minetest.get_meta(pos):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
@@ -165,15 +162,14 @@ 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 channel ~= meta:get_string("digiline_channel") then return end
+
if msg == GET_COMMAND then
- digiline:receptor_send(pos, digiline.rules.default, channel,
- minetest.get_node(
- vector.subtract(pos, minetest.facedir_to_dir(node.param2))
- ).name
- )
+ 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)
@@ -198,7 +194,6 @@ local function after_place_node_detector(pos, placer)
local node = minetest.get_node(pos)
node.param2 = minetest.dir_to_facedir(vector.subtract(pos, placer_pos), true)
minetest.set_node(pos, node)
- --minetest.log("action", "real (6d) facedir: " .. node.param2)
end
minetest.register_node("mesecons_detector:node_detector_off", {
@@ -249,11 +244,11 @@ minetest.register_abm({
interval = 1,
chance = 1,
action = function(pos, node)
- if node_detector_scan(pos) then
- node.name = "mesecons_detector:node_detector_on"
- minetest.swap_node(pos, node)
- mesecon.receptor_on(pos)
- end
+ 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,
})
@@ -262,10 +257,10 @@ minetest.register_abm({
interval = 1,
chance = 1,
action = function(pos, node)
- if not node_detector_scan(pos) then
- node.name = "mesecons_detector:node_detector_off"
- minetest.swap_node(pos, node)
- mesecon.receptor_off(pos)
- end
+ 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,
})