From f42f6a4bd9077b2710315cc157776e7b3ee7e328 Mon Sep 17 00:00:00 2001 From: paly2 Date: Wed, 9 Sep 2015 12:17:16 +0200 Subject: [PATCH] Add the "entity detector" node --- moremesecons_entity_detector/depends.txt | 1 + moremesecons_entity_detector/init.lua | 132 ++++++++++++++++++ .../moremesecons_entity_detector_off.png | Bin 0 -> 632 bytes .../moremesecons_entity_detector_on.png | Bin 0 -> 633 bytes 4 files changed, 133 insertions(+) create mode 100644 moremesecons_entity_detector/depends.txt create mode 100644 moremesecons_entity_detector/init.lua create mode 100644 moremesecons_entity_detector/textures/moremesecons_entity_detector_off.png create mode 100644 moremesecons_entity_detector/textures/moremesecons_entity_detector_on.png diff --git a/moremesecons_entity_detector/depends.txt b/moremesecons_entity_detector/depends.txt new file mode 100644 index 0000000..acaa924 --- /dev/null +++ b/moremesecons_entity_detector/depends.txt @@ -0,0 +1 @@ +mesecons diff --git a/moremesecons_entity_detector/init.lua b/moremesecons_entity_detector/init.lua new file mode 100644 index 0000000..92aade3 --- /dev/null +++ b/moremesecons_entity_detector/init.lua @@ -0,0 +1,132 @@ +-- Entity detector +-- Detects entitys in a certain radius +-- The radius can be changes by right-click (by default 6) + +local function make_formspec(meta) + meta:set_string("formspec", "size[9,5]" .. + "field[0.3, 0;9,2;scanname;Name (itemstring) of entity to scan for (empty for any):;${scanname}]".. + "field[0.3,1.5;4,2;digiline_channel;Digiline Channel (optional):;${digiline_channel}]".. + "field[0.3,3;2,2;radius;Detection radius:;${radius}]".. + "button_exit[3.5,3.5;2,3;;Save]") +end + +local function object_detector_make_formspec(pos) + make_formspec(minetest.get_meta(pos)) +end + +local function object_detector_on_receive_fields(pos, formname, fields) + if not fields.scanname + or not fields.digiline_channel then + return + end + + local meta = minetest.get_meta(pos) + meta:set_string("scanname", fields.scanname) + meta:set_string("digiline_channel", fields.digiline_channel) + local r = tonumber(fields.radius) + if r then + meta:set_int("radius", r) + end +end + +-- returns true if entity was found, false if not +local object_detector_scan = function (pos) + local meta = minetest.get_meta(pos) + local scanname = meta:get_string("scanname") + local scan_all = scanname == "" + local radius = meta:get_int("radius") + if radius == 0 then + radius = 6 + end + for _,obj in pairs(minetest.get_objects_inside_radius(pos, radius)) do + if not obj:is_player() then + local isname = (obj:get_luaentity()).name + if isname + and (scan_all or isname == scanname) then -- entity with scanname found or not scanname specified + return true + end + end + end + return false +end + +-- set entity 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) + local active_channel = meta:get_string("digiline_channel") + if channel ~= active_channel then + return + end + meta:set_string("scanname", msg) + if meta:get_string("formspec") ~= "" then + make_formspec(meta) + end + end, + } +} + +minetest.register_node("moremesecons_entity_detector:entity_detector_off", { + tiles = {"default_steel_block.png", "default_steel_block.png", "moremesecons_entity_detector_off.png"}, + paramtype = "light", + walkable = true, + groups = {cracky=3}, + description="Entity Detector", + mesecons = {receptor = { + state = mesecon.state.off, + rules = mesecon.rules.pplate + }}, + on_construct = object_detector_make_formspec, + on_receive_fields = object_detector_on_receive_fields, + sounds = default.node_sound_stone_defaults(), + digiline = object_detector_digiline +}) + +minetest.register_node("moremesecons_entity_detector:entity_detector_on", { + tiles = {"default_steel_block.png", "default_steel_block.png", "moremesecons_entity_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, + on_receive_fields = object_detector_on_receive_fields, + sounds = default.node_sound_stone_defaults(), + digiline = object_detector_digiline +}) + +minetest.register_craft({ + output = 'moremesecons_entity_detector:entity_detector_off', + recipe = { + {"mesecons_detector:object_detector_off"}, + {"default:mese_crystal_fragment"} + } +}) + +minetest.register_abm({ + nodenames = {"moremesecons_entity_detector:entity_detector_off"}, + interval = 1.0, + chance = 1, + action = function(pos) + if object_detector_scan(pos) then + minetest.swap_node(pos, {name = "moremesecons_entity_detector:entity_detector_on"}) + mesecon.receptor_on(pos, mesecon.rules.pplate) + end + end, +}) + +minetest.register_abm({ + nodenames = {"moremesecons_entity_detector:entity_detector_on"}, + interval = 1.0, + chance = 1, + action = function(pos) + if not object_detector_scan(pos) then + minetest.swap_node(pos, {name = "moremesecons_entity_detector:entity_detector_off"}) + mesecon.receptor_off(pos, mesecon.rules.pplate) + end + end, +}) diff --git a/moremesecons_entity_detector/textures/moremesecons_entity_detector_off.png b/moremesecons_entity_detector/textures/moremesecons_entity_detector_off.png new file mode 100644 index 0000000000000000000000000000000000000000..3dc58bfc6071454e74e434c0d32f93a97444da15 GIT binary patch literal 632 zcmV-;0*C#HP)N_Y7kukb%|w1d+Iq1&JE|EE|3WGN`b^9bwrUCp@ClJ^i}( zR=H?38soRDQ>RXugM$MA);V{2 zdaA0bYLJwjh%`;3s?0vi3;+PCN<{bf_q<-ORW+(A5sV*Y?>!MQi>eSY5izrZnx^69 za;d70S%zL!MFc=m*gVgzwFER_3jpA*ZMNGjmu1P!62{itfz|2G>ii|T6Y3fQu z7K;V%?(V8881?tI3qevGJ)@Zv61rDMM_ugOerTG;ZZ;bcd3ktE;? zfOl`-uU0E#jNRYgABX)$5*@uh#6O&zoP0e$=c=ky^-m$B7XRVl;lTnJhT-o7y}7=2 z&V>*}goxf89|KrjUe3$Xrj)$*pFiE?xg#PX%2MjO9ta@}Y$)>+03R+cBt`%{o6SJT zbEm3l=tM+Bh@h(8XHXs_eZ0B?z|2Ka*bo8}hc-ifObUr9rZ_$*mt|>;AxYx`R>>G+jG5-e�J) zDDzpCW$X3Y&StZ_ySwo|ecwChy!SE2x~_M2cAlP|ilUfICeP2$i^XDZZ|?`8HbzHU S69*Fj0000N_Y7?&kb%|w1Q9lp_(%wUmJPoG0ToubBP@GiCOo3kGyS^v zR=H?38soRDQ>RXuy}dmE);V`_ za-yoLYLJwjh%`;3s?0vi3;+PCN<{bf_q<##RW+(A5sV*Y?>!MQi>eSY5izrZnx^5! zVx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