Merge branch 'playerdetector_update' of https://github.com/HybridDog/minetest-mod-mesecons into HybridDog-playerdetector_update

This commit is contained in:
Jeija 2016-02-14 12:58:02 +01:00
commit 6b54f025c1
1 changed files with 102 additions and 108 deletions

View File

@ -4,16 +4,15 @@ local GET_COMMAND = "GET"
-- Detects players in a certain radius -- Detects players in a certain radius
-- The radius can be specified in mesecons/settings.lua -- The radius can be specified in mesecons/settings.lua
local object_detector_make_formspec = function (pos) local function object_detector_make_formspec(pos)
local meta = minetest.get_meta(pos) minetest.get_meta(pos):set_string("formspec", "size[9,2.5]" ..
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 object_detector_on_receive_fields = function(pos, formname, fields) 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) local meta = minetest.get_meta(pos)
meta:set_string("scanname", fields.scanname) meta:set_string("scanname", fields.scanname)
@ -22,25 +21,35 @@ local object_detector_on_receive_fields = function(pos, formname, fields)
end end
-- returns true if player was found, false if not -- returns true if player was found, false if not
local object_detector_scan = function (pos) local function object_detector_scan(pos)
local objs = minetest.get_objects_inside_radius(pos, mesecon.setting("detector_radius", 6)) local objs = minetest.get_objects_inside_radius(pos, mesecon.setting("detector_radius", 6))
for k, obj in pairs(objs) do
local isname = obj:get_player_name() -- "" is returned if it is not a player; "" ~= nil! -- abort if no scan results were found
local scanname = minetest.get_meta(pos):get_string("scanname") if next(objs) == nil then return false end
if (isname == scanname and isname ~= "") or (isname ~= "" and scanname == "") then -- player with scanname found or not scanname specified
return true local scanname = minetest.get_meta(pos):get_string("scanname")
local every_player = scanname == ""
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
end end
return false return false
end end
-- set player name when receiving a digiline signal on a specific channel -- set player name when receiving a digiline signal on a specific channel
local object_detector_digiline = { local object_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 active_channel = meta:get_string("digiline_channel") if channel == meta:get_string("digiline_channel") then
if channel == active_channel then
meta:set_string("scanname", msg) meta:set_string("scanname", msg)
object_detector_make_formspec(pos) object_detector_make_formspec(pos)
end end
@ -89,43 +98,44 @@ minetest.register_craft({
} }
}) })
minetest.register_abm( minetest.register_abm({
{nodenames = {"mesecons_detector:object_detector_off"}, nodenames = {"mesecons_detector:object_detector_off"},
interval = 1.0, interval = 1,
chance = 1, chance = 1,
action = function(pos) action = function(pos, node)
if object_detector_scan(pos) then if not object_detector_scan(pos) then return end
minetest.swap_node(pos, {name = "mesecons_detector:object_detector_on"})
mesecon.receptor_on(pos, mesecon.rules.pplate) node.name = "mesecons_detector:object_detector_on"
end minetest.swap_node(pos, node)
mesecon.receptor_on(pos, mesecon.rules.pplate)
end, end,
}) })
minetest.register_abm( minetest.register_abm({
{nodenames = {"mesecons_detector:object_detector_on"}, nodenames = {"mesecons_detector:object_detector_on"},
interval = 1.0, interval = 1,
chance = 1, chance = 1,
action = function(pos) action = function(pos, node)
if not object_detector_scan(pos) then if object_detector_scan(pos) then return end
minetest.swap_node(pos, {name = "mesecons_detector:object_detector_off"})
mesecon.receptor_off(pos, mesecon.rules.pplate) node.name = "mesecons_detector:object_detector_off"
end minetest.swap_node(pos, node)
mesecon.receptor_off(pos, mesecon.rules.pplate)
end, end,
}) })
-- Node detector -- Node detector
-- Detects the node in front of it -- Detects the node in front of it
local node_detector_make_formspec = function (pos) local function node_detector_make_formspec(pos)
local meta = minetest.get_meta(pos) minetest.get_meta(pos):set_string("formspec", "size[9,2.5]" ..
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;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 node_detector_on_receive_fields = function(pos, formname, fields) 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) local meta = minetest.get_meta(pos)
meta:set_string("scanname", fields.scanname) meta:set_string("scanname", fields.scanname)
@ -133,41 +143,59 @@ local node_detector_on_receive_fields = function(pos, formname, fields)
node_detector_make_formspec(pos) node_detector_make_formspec(pos)
end end
-- returns true if player was found, false if not -- returns true if node was found, false if not
local node_detector_scan = function (pos) local function node_detector_scan(pos)
if not pos then return end
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 scandir = minetest.facedir_to_dir(node.param2)
if not scandir then return end local frontname = minetest.get_node(
local frontpos = vector.subtract(pos, scandir) vector.subtract(pos, minetest.facedir_to_dir(node.param2))
local frontnode = minetest.get_node(frontpos) ).name
local meta = minetest.get_meta(pos) local scanname = minetest.get_meta(pos):get_string("scanname")
return (frontnode.name == meta:get_string("scanname")) or
(frontnode.name ~= "air" and frontnode.name ~= "ignore" and meta:get_string("scanname") == "") return (frontname == scanname) or
(frontname ~= "air" and frontname ~= "ignore" and scanname == "")
end end
-- set player name when receiving a digiline signal on a specific channel -- set player name when receiving a digiline signal on a specific channel
local node_detector_digiline = { 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 active_channel = meta:get_string("digiline_channel") if channel ~= meta:get_string("digiline_channel") then return end
if channel == active_channel then
if msg == GET_COMMAND then if msg == GET_COMMAND then
local frontpos = vector.subtract(pos, minetest.facedir_to_dir(node.param2)) local nodename = minetest.get_node(
local name = minetest.get_node(frontpos).name vector.subtract(pos, minetest.facedir_to_dir(node.param2))
digiline:receptor_send(pos, digiline.rules.default, channel, name) ).name
else
meta:set_string("scanname", msg) digiline:receptor_send(pos, digiline.rules.default, channel, nodename)
node_detector_make_formspec(pos) else
end meta:set_string("scanname", msg)
node_detector_make_formspec(pos)
end end
end, end,
}, },
receptor = {} receptor = {}
} }
local function after_place_node_detector(pos, placer)
local placer_pos = placer:getpos()
if not placer_pos then
return
end
--correct for the player's height
if placer:is_player() then
placer_pos.y = placer_pos.y + 1.625
end
--correct for 6d facedir
local node = minetest.get_node(pos)
node.param2 = minetest.dir_to_facedir(vector.subtract(pos, placer_pos), true)
minetest.set_node(pos, node)
end
minetest.register_node("mesecons_detector:node_detector_off", { minetest.register_node("mesecons_detector:node_detector_off", {
tiles = {"default_steel_block.png", "default_steel_block.png", "default_steel_block.png", "default_steel_block.png", "default_steel_block.png", "jeija_node_detector_off.png"}, tiles = {"default_steel_block.png", "default_steel_block.png", "default_steel_block.png", "default_steel_block.png", "default_steel_block.png", "jeija_node_detector_off.png"},
paramtype = "light", paramtype = "light",
@ -180,25 +208,7 @@ 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 = function (pos, placer) after_place_node = after_place_node_detector,
local placer_pos = placer:getpos()
--correct for the player's height
if placer:is_player() then placer_pos.y = placer_pos.y + 1.5 end
--correct for 6d facedir
if placer_pos then
local dir = {
x = pos.x - placer_pos.x,
y = pos.y - placer_pos.y,
z = pos.z - placer_pos.z
}
local node = minetest.get_node(pos)
node.param2 = minetest.dir_to_facedir(dir, true)
minetest.set_node(pos, node)
minetest.log("action", "real (6d) facedir: " .. node.param2)
end
end,
sounds = default.node_sound_stone_defaults(), sounds = default.node_sound_stone_defaults(),
digiline = node_detector_digiline digiline = node_detector_digiline
}) })
@ -215,25 +225,7 @@ 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 = function (pos, placer) after_place_node = after_place_node_detector,
local placer_pos = placer:getpos()
--correct for the player's height
if placer:is_player() then placer_pos.y = placer_pos.y + 1.5 end
--correct for 6d facedir
if placer_pos then
local dir = {
x = pos.x - placer_pos.x,
y = pos.y - placer_pos.y,
z = pos.z - placer_pos.z
}
local node = minetest.get_node(pos)
node.param2 = minetest.dir_to_facedir(dir, true)
minetest.set_node(pos, node)
minetest.log("action", "real (6d) facedir: " .. node.param2)
end
end,
sounds = default.node_sound_stone_defaults(), sounds = default.node_sound_stone_defaults(),
digiline = node_detector_digiline digiline = node_detector_digiline
}) })
@ -247,26 +239,28 @@ minetest.register_craft({
} }
}) })
minetest.register_abm( minetest.register_abm({
{nodenames = {"mesecons_detector:node_detector_off"}, nodenames = {"mesecons_detector:node_detector_off"},
interval = 1.0, interval = 1,
chance = 1, chance = 1,
action = function(pos, node) action = function(pos, node)
if node_detector_scan(pos) then if not node_detector_scan(pos) then return end
minetest.swap_node(pos, {name = "mesecons_detector:node_detector_on", param2 = node.param2})
mesecon.receptor_on(pos) node.name = "mesecons_detector:node_detector_on"
end minetest.swap_node(pos, node)
mesecon.receptor_on(pos)
end, end,
}) })
minetest.register_abm( minetest.register_abm({
{nodenames = {"mesecons_detector:node_detector_on"}, nodenames = {"mesecons_detector:node_detector_on"},
interval = 1.0, interval = 1,
chance = 1, chance = 1,
action = function(pos, node) action = function(pos, node)
if not node_detector_scan(pos) then if node_detector_scan(pos) then return end
minetest.swap_node(pos, {name = "mesecons_detector:node_detector_off", param2 = node.param2})
mesecon.receptor_off(pos) node.name = "mesecons_detector:node_detector_off"
end minetest.swap_node(pos, node)
mesecon.receptor_off(pos)
end, end,
}) })