From a0920104fca2ec2cda1e9f89a6f2fa24ba55a5b1 Mon Sep 17 00:00:00 2001 From: Jeija Date: Fri, 22 Feb 2013 19:20:23 +0100 Subject: [PATCH] Object Detector, cleanup and different behaviour: * Name to scan for can be specified by right-clicking it * It can receive digiline signals that change the name to scan for * A sign above it for the name doesn't work anymore (this features wasn't used often anyway) --- mesecons_detector/init.lua | 91 ++++++++++++++++++++++++-------------- 1 file changed, 57 insertions(+), 34 deletions(-) diff --git a/mesecons_detector/init.lua b/mesecons_detector/init.lua index 2394e96..5072426 100644 --- a/mesecons_detector/init.lua +++ b/mesecons_detector/init.lua @@ -1,7 +1,48 @@ -- Object detector --- Detects all entities in a certain radius +-- Detects players in a certain radius -- The radius can be specified in mesecons/settings.lua +local object_detector_make_formspec = function (pos) + local meta = minetest.env: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,1.5;4,2;digiline_channel;Digiline Channel (optional):;${digiline_channel}]") +end + +local object_detector_on_receive_fields = function (pos, formname, fields) + local meta = minetest.env:get_meta(pos) + 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 object_detector_scan = function (pos) + local objs = minetest.env:get_objects_inside_radius(pos, OBJECT_DETECTOR_RADIUS) + for k, obj in pairs(objs) do + local isname = obj:get_player_name() -- "" is returned if it is not a player; "" ~= nil! + local scanname = minetest.env:get_meta(pos):get_string("scanname") + if (isname == scanname and isname ~= "") or (isname ~= "" and scanname == "") then -- player with scanname found or not scanname specified + return true + end + end + return false +end + +-- set player name when receiving a digiline signal on a specific channel +object_detector_digiline = { + effector = { + action = function (pos, node, channel, msg) + local meta = minetest.env:get_meta(pos) + local active_channel = meta:get_string("digiline_channel") + if channel == active_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", @@ -10,7 +51,10 @@ minetest.register_node("mesecons_detector:object_detector_off", { description="Player Detector", mesecons = {receptor = { state = mesecon.state.off - }} + }}, + on_construct = object_detector_make_formspec, + on_receive_fields = object_detector_on_receive_fields, + digiline = object_detector_digiline }) minetest.register_node("mesecons_detector:object_detector_on", { @@ -21,7 +65,10 @@ minetest.register_node("mesecons_detector:object_detector_on", { drop = 'mesecons_detector:object_detector_off', mesecons = {receptor = { state = mesecon.state.on - }} + }}, + on_construct = object_detector_make_formspec, + on_receive_fields = object_detector_on_receive_fields, + digiline = object_detector_digiline }) minetest.register_craft({ @@ -37,19 +84,10 @@ minetest.register_abm( {nodenames = {"mesecons_detector:object_detector_off"}, interval = 1.0, chance = 1, - action = function(pos, node, active_object_count, active_object_count_wider) - local objs = minetest.env:get_objects_inside_radius(pos, OBJECT_DETECTOR_RADIUS) - for k, obj in pairs(objs) do - if obj:get_entity_name()~="mesecons_pistons:piston_pusher_sticky" and obj:get_entity_name()~="mesecons_pistons:piston_pusher_normal" and obj:get_player_name()~=nil then - if minetest.env:get_node({x=pos.x, y=pos.y-1, z=pos.z}).name=="default:sign_wall" then - if obj:get_player_name()~=minetest.env:get_meta({x=pos.x, y=pos.y-1, z=pos.z}):get_string("text") then - return - end - end - local objpos=obj:getpos() - minetest.env:add_node(pos, {name="mesecons_detector:object_detector_on"}) - mesecon:receptor_on(pos) - end + action = function(pos) + if object_detector_scan(pos) then + mesecon:swap_node(pos, "mesecons_detector:object_detector_on") + mesecon:receptor_on(pos) end end, }) @@ -58,24 +96,9 @@ minetest.register_abm( {nodenames = {"mesecons_detector:object_detector_on"}, interval = 1.0, chance = 1, - action = function(pos, node, active_object_count, active_object_count_wider) - local objs = minetest.env:get_objects_inside_radius(pos, OBJECT_DETECTOR_RADIUS) - local objectfound=0 - for k, obj in pairs(objs) do - if obj:get_entity_name()~="mesecons_pistons:piston_pusher_sticky" and obj:get_entity_name()~="mesecons_pistons:piston_pusher_normal" and obj~=nil - and obj:get_player_name()~=nil then - if minetest.env:get_node({x=pos.x, y=pos.y-1, z=pos.z}).name=="default:sign_wall" then - if minetest.env:get_meta({x=pos.x, y=pos.y-1, z=pos.z}):get_string("text")== obj:get_player_name() then - objectfound=objectfound+1 - end - else --- Detected object is not piston pusher - will be changed if every entity has a type (like entity_type=mob) - objectfound=objectfound + 1 - end - end - end - if objectfound==0 then - minetest.env:add_node(pos, {name="mesecons_detector:object_detector_off"}) + action = function(pos) + if not object_detector_scan(pos) then + mesecon:swap_node(pos, "mesecons_detector:object_detector_off") mesecon:receptor_off(pos) end end,