Skip to content

Commit

Permalink
Improve digistuff:detector and digistuff:camera (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
OgelGames authored Jan 25, 2022
1 parent 813a64b commit a49e4dd
Show file tree
Hide file tree
Showing 6 changed files with 256 additions and 99 deletions.
171 changes: 115 additions & 56 deletions camera.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,47 @@

local formspec = "size[8,4]"..
"field[1.3,1;6,1;channel;Channel;${channel}]"..
"field[1.3,2;3,1.3;radius;Radius;${radius}]"..
"field[4.3,2;3,1.3;distance;Distance;${distance}]"..
"button_exit[4,3;3,1;submit;Save]"

local function get_formspec(enabled)
if enabled then
return formspec.."button[1,3;3,1;disable;Disable]"
else
return formspec.."button[1,3;3,1;enable;Enable]"
end
end

local function search_for_players(pos, send_empty)
local meta = minetest.get_meta(pos)
local distance = meta:get_int("distance")
local dir = minetest.facedir_to_dir(minetest.get_node(pos).param2)
local spot = vector.add(pos, vector.multiply(dir, -distance))
local node = minetest.get_node(spot)
while node.name == "air" and pos.y - spot.y < 10 do
spot.y = spot.y - 1
node = minetest.get_node(spot)
end
if node.name == "air" or node.name == "ignore" then
return true
end
local radius = meta:get_int("radius")
local found = {}
for _,player in pairs(minetest.get_connected_players()) do
if vector.distance(spot, player:get_pos()) <= radius then
table.insert(found, player:get_player_name())
end
end
if #found > 0 or send_empty == true then
local channel = meta:get_string("channel")
digilines.receptor_send(pos, digilines.rules.default, channel, found)
end
return true
end

minetest.register_node("digistuff:camera", {
description = "Digilines Camera",
tiles = {
"digistuff_camera_top.png",
"digistuff_camera_bottom.png",
Expand All @@ -7,84 +50,100 @@ minetest.register_node("digistuff:camera", {
"digistuff_camera_back.png",
"digistuff_camera_front.png",
},
digiline = {
receptor = {}
},
_digistuff_channelcopier_fieldname = "channel",
groups = {cracky=2},
paramtype = "light",
paramtype2 = "facedir",
drawtype = "nodebox",
node_box = {
type = "fixed",
fixed = {
{-0.1,-0.5,-0.28,0.1,-0.3,0.3}, --Camera Body
{-0.045,-0.42,-0.34,0.045,-0.36,-0.28}, -- Lens
{-0.05,-0.9,-0.05,0.05,-0.5,0.05}, --Pole
}
{-0.1,-0.5,-0.28,0.1,-0.3,0.3}, -- Camera Body
{-0.045,-0.42,-0.34,0.045,-0.36,-0.28}, -- Lens
{-0.05,-0.9,-0.05,0.05,-0.5,0.05}, -- Pole
}
},
selection_box = {
type = "fixed",
fixed = {
{-0.1,-0.5,-0.34,0.1,-0.3,0.3}, --Camera Body
}
{-0.1,-0.5,-0.34,0.1,-0.3,0.3},
}
},
description = "Digilines Camera",
sounds = default and default.node_sound_stone_defaults(),
groups = {cracky = 2},
on_construct = function(pos)
local meta = minetest.get_meta(pos)
meta:set_string("formspec","size[8,6;]field[1,1;6,2;channel;Channel;${channel}]field[1,2;6,2;radius;Radius (max 10);${radius}]field[1,3;6,2;distance;Distance (max 20);${distance}]button_exit[2.25,4;3,1;submit;Save]")
meta:set_string("formspec", get_formspec(true))
meta:set_int("radius", 1)
meta:set_int("distance", 0)
minetest.get_node_timer(pos):start(1)
end,
on_receive_fields = function(pos, formname, fields, sender)
local name = sender:get_player_name()
if minetest.is_protected(pos,name) and not minetest.check_player_privs(name,{protection_bypass=true}) then
minetest.record_protection_violation(pos,name)
on_receive_fields = function(pos, _, fields, player)
if minetest.is_protected(pos, player:get_player_name()) then
return
end
local meta = minetest.get_meta(pos)
if fields.channel then meta:set_string("channel",fields.channel) end
if fields.distance and tonumber(fields.distance) then meta:set_int("distance",math.max(math.min(20,fields.distance),0)) end
if fields.radius and tonumber(fields.radius) then meta:set_int("radius",math.max(math.min(10,fields.radius),1)) end
if fields.channel then
meta:set_string("channel", fields.channel)
end
if fields.radius then
local value = math.max(1, math.min(10, tonumber(fields.radius) or 1))
meta:set_int("radius", value)
end
if fields.distance then
local value = math.max(0, math.min(20, tonumber(fields.distance) or 0))
meta:set_int("distance", value)
end
if fields.enable then
meta:set_string("formspec", get_formspec(true))
minetest.get_node_timer(pos):start(1)
elseif fields.disable then
meta:set_string("formspec", get_formspec(false))
minetest.get_node_timer(pos):stop()
end
end,
sounds = default and default.node_sound_stone_defaults()
on_timer = search_for_players,
digiline = {
receptor = {},
effector = {
action = function(pos, node, channel, msg)
local meta = minetest.get_meta(pos)
if channel ~= meta:get_string("channel") then return end
if type(msg) == "table" then
if msg.radius then
local value = math.max(1, math.min(10, tonumber(msg.radius) or 1))
meta:set_int("radius", value)
end
if msg.distance then
local value = math.max(0, math.min(20, tonumber(msg.distance) or 0))
meta:set_int("distance", value)
end
if msg.command == "get" then
search_for_players(pos, true)
end
elseif msg == "GET" or msg == "get" then
search_for_players(pos, true)
end
end,
},
},
_digistuff_channelcopier_fieldname = "channel",
})

minetest.register_abm({
minetest.register_lbm({
label = "Digistuff camera update",
name = "digistuff:camera_update",
nodenames = {"digistuff:camera"},
interval = 1.0,
chance = 1,
action = function(pos,node)
local meta = minetest.get_meta(pos)
local channel = meta:get_string("channel")
local radius = meta:get_int("radius")
local distance = meta:get_int("distance")
local dir = vector.multiply(minetest.facedir_to_dir(node.param2),-1)
local spot = vector.add(pos,vector.multiply(dir,distance))
local i = 0
while i <= 10 and minetest.get_node(spot).name == "air" do
--Downward search for ground level
spot = vector.add(spot,vector.new(0,-1,0))
i = i + 1
end
if minetest.get_node(spot).name == "air" or minetest.get_node(spot).name == "ignore" then
--Ground not in range
return
end

local found_any = false
local players_found = {}
local objs = minetest.get_objects_inside_radius(spot,radius)
if objs then
for _,obj in ipairs(objs) do
if obj:is_player() then
table.insert(players_found,obj:get_player_name())
found_any = true
end
end
if found_any then
digilines.receptor_send({x=pos.x,y=pos.y-1,z=pos.z}, digilines.rules.default, channel, players_found)
end
end
run_at_every_load = false,
action = function(pos)
local meta = minetest.get_meta(pos)
if not meta:get("radius") then
meta:set_int("radius", 1)
end
if not meta:get("distance") then
meta:set_int("distance", 0)
end
meta:set_string("formspec", get_formspec(true))
minetest.get_node_timer(pos):start(1)
end,
})

minetest.register_craft({
Expand Down
122 changes: 85 additions & 37 deletions detector.lua
Original file line number Diff line number Diff line change
@@ -1,55 +1,103 @@

local formspec = "size[8,4]"..
"field[1.3,1;6,1;channel;Channel;${channel}]"..
"field[1.3,2;6,1.3;radius;Radius;${radius}]"..
"button_exit[4,3;3,1;submit;Save]"

local function get_formspec(enabled)
if enabled then
return formspec.."button[1,3;3,1;disable;Disable]"
else
return formspec.."button[1,3;3,1;enable;Enable]"
end
end

local function search_for_players(pos, send_empty)
local meta = minetest.get_meta(pos)
local radius = meta:get_int("radius")
local found = {}
for _,player in pairs(minetest.get_connected_players()) do
if vector.distance(pos, player:get_pos()) <= radius then
table.insert(found, player:get_player_name())
end
end
if #found > 0 or send_empty == true then
local channel = meta:get_string("channel")
digilines.receptor_send(pos, digilines.rules.default, channel, found)
end
return true
end

minetest.register_node("digistuff:detector", {
description = "Digilines Player Detector",
tiles = {
"digistuff_digidetector.png"
"digistuff_digidetector.png"
},
digiline = {
receptor = {}
},
groups = {cracky=2},
description = "Digilines Player Detector",
sounds = default and default.node_sound_stone_defaults(),
groups = {cracky = 2},
on_construct = function(pos)
local meta = minetest.get_meta(pos)
meta:set_string("formspec","size[8,4;]field[1,1;6,2;channel;Channel;${channel}]field[1,2;6,2;radius;Radius;${radius}]button_exit[2.25,3;3,1;submit;Save]")
meta:set_string("formspec", get_formspec(true))
meta:set_int("radius", 6)
minetest.get_node_timer(pos):start(1)
end,
_digistuff_channelcopier_fieldname = "channel",
on_receive_fields = function(pos, formname, fields, sender)
local name = sender:get_player_name()
if minetest.is_protected(pos,name) and not minetest.check_player_privs(name,{protection_bypass=true}) then
minetest.record_protection_violation(pos,name)
on_receive_fields = function(pos, _, fields, player)
if minetest.is_protected(pos, player:get_player_name()) then
return
end
local meta = minetest.get_meta(pos)
if fields.channel then meta:set_string("channel",fields.channel) end
if fields.msg then meta:set_string("msg",fields.msg) end
if fields.radius then meta:set_string("radius",fields.radius) end
if fields.channel then
meta:set_string("channel", fields.channel)
end
if fields.radius then
local value = math.max(1, math.min(10, tonumber(fields.radius) or 6))
meta:set_int("radius", value)
end
if fields.enable then
meta:set_string("formspec", get_formspec(true))
minetest.get_node_timer(pos):start(1)
elseif fields.disable then
meta:set_string("formspec", get_formspec(false))
minetest.get_node_timer(pos):stop()
end
end,
sounds = default and default.node_sound_stone_defaults()
on_timer = search_for_players,
digiline = {
receptor = {},
effector = {
action = function(pos, node, channel, msg)
local meta = minetest.get_meta(pos)
if channel ~= meta:get_string("channel") then return end
if type(msg) == "table" then
if msg.radius then
local value = math.max(1, math.min(10, tonumber(msg.radius) or 1))
meta:set_int("radius", value)
end
if msg.command == "get" then
search_for_players(pos, true)
end
elseif msg == "GET" or msg == "get" then
search_for_players(pos, true)
end
end,
},
},
_digistuff_channelcopier_fieldname = "channel",
})

minetest.register_abm({
minetest.register_lbm({
label = "Digistuff detector update",
name = "digistuff:detector_update",
nodenames = {"digistuff:detector"},
interval = 1.0,
chance = 1,
run_at_every_load = false,
action = function(pos)
local meta = minetest.get_meta(pos)
local channel = meta:get_string("channel")
local radius = meta:get_string("radius")
local found_any = false
local players_found = {}
if not radius or not tonumber(radius) or tonumber(radius) < 1 or tonumber(radius) > 10 then radius = 6 end
local objs = minetest.get_objects_inside_radius(pos, radius)
if objs then
for _,obj in ipairs(objs) do
if obj:is_player() then
table.insert(players_found,obj:get_player_name())
found_any = true
end
end
if found_any then
digilines.receptor_send(pos, digilines.rules.default, channel, players_found)
end
end
local meta = minetest.get_meta(pos)
if not meta:get("radius") then
meta:set_int("radius", 6)
end
meta:set_string("formspec", get_formspec(true))
minetest.get_node_timer(pos):start(1)
end,
})

minetest.register_craft({
Expand Down
28 changes: 28 additions & 0 deletions docs/camera.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Digilines Camera

The camera looks at a node in front of it, detecting players within a radius of that node, and sending a resulting list of names via digilines.

## Basic usage

Set a channel, radius and distance. The radius must be between 1 and 10, and the distance must be between 0 and 20.

The camera will search for a node "distance" nodes in front of it and up to 10 nodes down. Every second while a player is within the radius of that node, a table listing the players in range will be sent via digilines on the chosen channel.

This functionality can be toggled by clicking the "Disable" or "Enable" button.

## Commands

Similar to the `mesecons` node detector, a digiline command can be sent to retrieve the list of players on demand.

```lua
digiline_send("detector", {command = "get"})

-- A string also works
digiline_send("detector", "get")
```

The radius and distance can also be set by digilines.

```lua
digiline_send("detector", {radius = 1, distance = 0})
```
3 changes: 0 additions & 3 deletions docs/camera.txt

This file was deleted.

Loading

0 comments on commit a49e4dd

Please sign in to comment.