Optional audible alarm on protection violation

master
Aaron Suen 2020-06-10 06:46:54 -04:00
parent 6aca7f5091
commit f64d4ebba1
5 changed files with 49 additions and 11 deletions

38
alarm.lua Normal file
View File

@ -0,0 +1,38 @@
-- LUALOCALS < ---------------------------------------------------------
local minetest, nodecore, tonumber
= minetest, nodecore, tonumber
-- LUALOCALS > ---------------------------------------------------------
local modname = minetest.get_current_modname()
local exmachina = nodecore[modname]
local squelch = {}
minetest.register_on_protection_violation(function(pos)
local gain = tonumber(minetest.settings:get(modname .. "_alarm")) or 1
if gain <= 0 then return end
local owner = exmachina.owner_search(pos)
if not owner then return end
local cpos = exmachina.location_get(owner)
if not cpos then return end
local hash = minetest.hash_node_position(cpos)
local t = squelch[hash]
local now = minetest.get_us_time()
if t and t > now - 1000000 then return end
squelch[hash] = now
local minpos, maxpos = exmachina.getbounds(cpos)
for x = minpos.x, maxpos.x, maxpos.x - minpos.x do
for y = minpos.y, maxpos.y, maxpos.y - minpos.y do
for z = minpos.z, maxpos.z, maxpos.z - minpos.z do
minetest.sound_play(modname .. "_denied", {
gain = gain,
pos = {x = x, y = y, z = z}
})
end
end
end
end)

10
api.lua
View File

@ -12,7 +12,6 @@ local function getsetting(n, v)
return tonumber(minetest.settings:get(modname .. "_" .. n)) or v
end
local exmachina = {
store = modstore,
path = minetest.get_worldpath() .. "/" .. modname,
extent = math_floor(getsetting("extent", 3)),
height = math_floor(getsetting("height", 3)),
@ -72,6 +71,15 @@ function exmachina.getbounds(pos, margin)
}
end
function exmachina.owner_search(pos)
for k, v in pairs(modstore:to_table().fields) do
local min, max = exmachina.getbounds(minetest.string_to_pos(v), 0.5)
if pos.x >= min.x and pos.x <= max.x
and pos.y >= min.y and pos.y <= max.y
and pos.z >= min.z and pos.z <= max.z then return k end
end
end
function exmachina.areapuffs(pos, qty, time)
local minpos, maxpos = exmachina.getbounds(pos, 0.5)
return minetest.add_particlespawner({

View File

@ -10,3 +10,4 @@ include("abm")
include("crafting")
include("admin")
include("protect")
include("alarm")

View File

@ -25,19 +25,10 @@ local function match(nodename)
end
end
local function owner(pos)
for k, v in pairs(exmachina.store:to_table().fields) do
local min, max = exmachina.getbounds(minetest.string_to_pos(v), 0.5)
if pos.x >= min.x and pos.x <= max.x
and pos.y >= min.y and pos.y <= max.y
and pos.z >= min.z and pos.z <= max.z then return k end
end
end
local function check(pos, thingname, pname)
if minetest.check_player_privs(pname, "protection_bypass") then return end
if match(thingname) then
local owned = owner(pos)
local owned = exmachina.owner_search(pos)
if owned and owned ~= pname then return true end
end
end

Binary file not shown.