areas/interact.lua

87 lines
2.3 KiB
Lua
Raw Normal View History

2021-04-24 03:55:20 -07:00
local S = areas.S
2020-03-15 18:08:31 -07:00
2021-05-19 01:42:16 -07:00
local enable_damage = minetest.settings:get_bool("enable_damage")
2013-12-17 17:31:11 -08:00
local old_is_protected = minetest.is_protected
2013-12-15 14:47:56 -08:00
function minetest.is_protected(pos, name)
if not areas:canInteract(pos, name) then
return true
end
2013-12-15 14:47:56 -08:00
return old_is_protected(pos, name)
end
2013-12-15 14:47:56 -08:00
minetest.register_on_protection_violation(function(pos, name)
if not areas:canInteract(pos, name) then
local owners = areas:getNodeOwners(pos)
minetest.chat_send_player(name,
2020-03-08 14:15:00 -07:00
S("@1 is protected by @2.",
2013-12-15 14:47:56 -08:00
minetest.pos_to_string(pos),
table.concat(owners, ", ")))
2020-03-15 18:07:49 -07:00
-- Little damage player
local player = minetest.get_player_by_name(name)
if player and player:is_player() then
2021-05-19 01:42:16 -07:00
if enable_damage then
local hp = player:get_hp()
if hp and hp > 2 then
player:set_hp(hp - 1)
end
end
local player_pos = player:get_pos()
if pos.y < player_pos.y then
player_pos.y = player_pos.y + 1
player:set_pos(player_pos)
2020-03-15 18:07:49 -07:00
end
end
2013-09-02 16:16:14 -07:00
end
2013-12-15 14:47:56 -08:00
end)
2021-05-19 01:42:16 -07:00
local function can_pvp_at(pos)
for id in pairs(areas:getAreasAtPos(pos)) do
-- This uses areas:canPvP instead of area.canPvP in case areas:canPvP
-- is overridden
if areas:canPvP(id) then
return true
end
end
return false
end
2021-06-27 08:59:23 -07:00
minetest.register_on_punchplayer(function(player, hitter, time_from_last_punch)
2021-05-19 01:42:16 -07:00
if not enable_damage then
2021-06-27 08:59:23 -07:00
return true
2021-05-19 01:42:16 -07:00
end
-- If it's a mob, deal damage as usual
if not hitter or not hitter:is_player() then
return false
end
2021-11-22 09:22:32 -08:00
local player_name = hitter:get_player_name()
2021-05-19 01:42:16 -07:00
-- It is possible to use cheats
if time_from_last_punch < 0.25 then
2021-11-22 09:22:32 -08:00
minetest.chat_send_player(player_name, S("Wow, wow, take it easy!"))
2021-05-19 01:42:16 -07:00
return true
end
-- Allow PvP if both players are in a PvP area
if can_pvp_at(hitter:get_pos()) and can_pvp_at(player:get_pos()) then
return false
2021-05-19 01:42:16 -07:00
end
-- Otherwise, it doesn't do damage
2021-11-22 09:22:32 -08:00
minetest.chat_send_player(player_name, S("PvP is not allowed in this area!"))
2021-05-19 01:42:16 -07:00
return true
end)
local old_calculate_knockback = minetest.calculate_knockback
function minetest.calculate_knockback(player, hitter, time_from_last_punch, ...)
if player:is_player() and hitter and hitter:is_player() and
(time_from_last_punch < 0.25 or not can_pvp_at(player:get_pos()) or
not can_pvp_at(hitter:get_pos())) then
return 0
end
return old_calculate_knockback(player, hitter, time_from_last_punch, ...)
end