2015-09-03 18:54:10 +02:00
|
|
|
-- Adjustable Player Detector
|
|
|
|
-- Detects players in a certain radius
|
|
|
|
-- The radius can be changes by right-click (by default 6)
|
|
|
|
|
2015-09-04 16:58:17 +02:00
|
|
|
local function make_formspec(meta)
|
2015-09-03 18:54:10 +02:00
|
|
|
meta:set_string("formspec", "size[9,5]" ..
|
2016-12-09 15:09:15 +01:00
|
|
|
"field[0.3, 0;9,2;scanname;Coma-separated list of the names of players to scan for (empty for any):;${scanname}]"..
|
2015-09-03 18:54:10 +02:00
|
|
|
"field[0.3,1.5;4,2;digiline_channel;Digiline Channel (optional):;${digiline_channel}]"..
|
2017-04-19 20:56:30 +02:00
|
|
|
"field[0.3,3;4,2;radius;Detection radius:;${radius}]"..
|
2015-09-03 18:54:10 +02:00
|
|
|
"button_exit[3.5,3.5;2,3;;Save]")
|
|
|
|
end
|
|
|
|
|
2015-09-04 16:58:17 +02:00
|
|
|
local function object_detector_make_formspec(pos)
|
|
|
|
make_formspec(minetest.get_meta(pos))
|
|
|
|
end
|
|
|
|
|
2016-04-27 18:42:20 +02:00
|
|
|
local function object_detector_on_receive_fields(pos, _, fields, player)
|
2015-09-04 16:58:17 +02:00
|
|
|
if not fields.scanname
|
2016-04-27 18:42:20 +02:00
|
|
|
or not fields.digiline_channel
|
|
|
|
or minetest.is_protected(pos, player:get_player_name()) then
|
2015-09-04 16:58:17 +02:00
|
|
|
return
|
|
|
|
end
|
2015-09-03 18:54:10 +02:00
|
|
|
|
|
|
|
local meta = minetest.get_meta(pos)
|
|
|
|
meta:set_string("scanname", fields.scanname)
|
|
|
|
meta:set_string("digiline_channel", fields.digiline_channel)
|
2015-09-04 16:58:17 +02:00
|
|
|
local r = tonumber(fields.radius)
|
|
|
|
if r then
|
|
|
|
meta:set_int("radius", r)
|
|
|
|
end
|
2015-09-03 18:54:10 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
-- returns true if player was found, false if not
|
|
|
|
local object_detector_scan = function (pos)
|
|
|
|
local meta = minetest.get_meta(pos)
|
2015-09-04 16:58:17 +02:00
|
|
|
local scanname = meta:get_string("scanname")
|
|
|
|
local scan_all = scanname == ""
|
2016-12-09 15:09:15 +01:00
|
|
|
local scan_names = scanname:split(',')
|
2015-09-03 18:54:10 +02:00
|
|
|
local radius = meta:get_int("radius")
|
2015-09-04 16:58:17 +02:00
|
|
|
if radius == 0 then
|
|
|
|
radius = 6
|
|
|
|
end
|
|
|
|
for _,obj in pairs(minetest.get_objects_inside_radius(pos, radius)) do
|
2015-09-03 18:54:10 +02:00
|
|
|
local isname = obj:get_player_name() -- "" is returned if it is not a player; "" ~= nil!
|
2016-12-09 15:09:15 +01:00
|
|
|
if isname ~= "" then
|
|
|
|
if scan_all then
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
for _, name in ipairs(scan_names) do
|
|
|
|
if isname == name then
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
end
|
2015-09-03 18:54:10 +02:00
|
|
|
end
|
|
|
|
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)
|
|
|
|
local active_channel = meta:get_string("digiline_channel")
|
2015-09-04 16:58:17 +02:00
|
|
|
if channel ~= active_channel then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
meta:set_string("scanname", msg)
|
2015-09-04 22:08:42 +02:00
|
|
|
if meta:get_string("formspec") ~= "" then
|
2015-09-04 16:58:17 +02:00
|
|
|
make_formspec(meta)
|
2015-09-03 18:54:10 +02:00
|
|
|
end
|
|
|
|
end,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
minetest.register_node("moremesecons_adjustable_player_detector:player_detector_off", {
|
2015-09-04 22:08:42 +02:00
|
|
|
tiles = {"default_steel_block.png", "default_steel_block.png", "moremesecons_player_detector_off.png"},
|
2015-09-03 18:54:10 +02:00
|
|
|
paramtype = "light",
|
|
|
|
walkable = true,
|
|
|
|
groups = {cracky=3},
|
|
|
|
description="Adjustable Player 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_adjustable_player_detector:player_detector_on", {
|
2015-09-04 22:08:42 +02:00
|
|
|
tiles = {"default_steel_block.png", "default_steel_block.png", "moremesecons_player_detector_on.png"},
|
2015-09-03 18:54:10 +02:00
|
|
|
paramtype = "light",
|
|
|
|
walkable = true,
|
|
|
|
groups = {cracky=3,not_in_creative_inventory=1},
|
2016-04-02 11:23:39 +02:00
|
|
|
drop = 'moremesecons_adjustable_player_detector:player_detector_off',
|
2015-09-03 18:54:10 +02:00
|
|
|
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({
|
2015-09-03 18:57:19 +02:00
|
|
|
output = 'moremesecons_adjustable_player_detector:player_detector_off',
|
2015-09-03 18:54:10 +02:00
|
|
|
recipe = {
|
|
|
|
{"mesecons_detector:object_detector_off"},
|
|
|
|
{"default:mese_crystal_fragment"}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2015-09-04 22:08:42 +02:00
|
|
|
minetest.register_abm({
|
|
|
|
nodenames = {"moremesecons_adjustable_player_detector:player_detector_off"},
|
2015-09-03 18:54:10 +02:00
|
|
|
interval = 1.0,
|
|
|
|
chance = 1,
|
|
|
|
action = function(pos)
|
|
|
|
if object_detector_scan(pos) then
|
|
|
|
minetest.swap_node(pos, {name = "moremesecons_adjustable_player_detector:player_detector_on"})
|
|
|
|
mesecon.receptor_on(pos, mesecon.rules.pplate)
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
})
|
|
|
|
|
2015-09-04 22:08:42 +02:00
|
|
|
minetest.register_abm({
|
|
|
|
nodenames = {"moremesecons_adjustable_player_detector:player_detector_on"},
|
2015-09-03 18:54:10 +02:00
|
|
|
interval = 1.0,
|
|
|
|
chance = 1,
|
|
|
|
action = function(pos)
|
|
|
|
if not object_detector_scan(pos) then
|
|
|
|
minetest.swap_node(pos, {name = "moremesecons_adjustable_player_detector:player_detector_off"})
|
|
|
|
mesecon.receptor_off(pos, mesecon.rules.pplate)
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
})
|