-
Notifications
You must be signed in to change notification settings - Fork 4
/
protection.lua
118 lines (103 loc) · 4.31 KB
/
protection.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
--[[
Copyright (C) 2021 Jude Melton-Houghton
This file is part of area_containers. It implements the protection layer.
area_containers is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
area_containers is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with area_containers. If not, see <https://www.gnu.org/licenses/>.
]]
--[[
OVERVIEW
The protection layer extends one block above and below the inside Y-level
block. It excludes the insides (but walls are protected.)
]]
local use = ...
local PROTECTION_TYPE = use("settings", {"PROTECTION"})
local floor_blocksize = use("misc", {"floor_blocksize"})
local INSIDE_Y_LEVEL, get_params_from_inside = use("relation", {
"INSIDE_Y_LEVEL", "get_params_from_inside",
})
if PROTECTION_TYPE == "none" then return end
-- Protection settings. PADDING indicates that blocks around inside blocks are
-- also protected.
local LAYER = PROTECTION_TYPE == "layer"
local AROUND = PROTECTION_TYPE == "around"
local PADDING = LAYER or AROUND
-- The minimum and maximum layers at which the protection applies.
local MIN_APPLICABLE_Y = PADDING and INSIDE_Y_LEVEL - 16 or INSIDE_Y_LEVEL
local MAX_APPLICABLE_Y = PADDING and INSIDE_Y_LEVEL + 16 + 15 or
INSIDE_Y_LEVEL + 15
-- Determines whether the block is one of the 26 around an inside block and
-- should thus be protected with protection type "around".
-- Modifies block_min_pos.
local function pos_around(block_min_pos)
if block_min_pos.y > INSIDE_Y_LEVEL then
block_min_pos.y = block_min_pos.y - 16
if get_params_from_inside(block_min_pos) then return true end
elseif block_min_pos.y < INSIDE_Y_LEVEL then
block_min_pos.y = block_min_pos.y + 16
if get_params_from_inside(block_min_pos) then return true end
end
block_min_pos.x = block_min_pos.x + 16
if get_params_from_inside(block_min_pos) then return true end
block_min_pos.z = block_min_pos.z + 16
if get_params_from_inside(block_min_pos) then return true end
block_min_pos.x = block_min_pos.x - 16
if get_params_from_inside(block_min_pos) then return true end
block_min_pos.x = block_min_pos.x - 16
if get_params_from_inside(block_min_pos) then return true end
block_min_pos.z = block_min_pos.z - 16
if get_params_from_inside(block_min_pos) then return true end
block_min_pos.z = block_min_pos.z - 16
if get_params_from_inside(block_min_pos) then return true end
block_min_pos.x = block_min_pos.x + 16
if get_params_from_inside(block_min_pos) then return true end
block_min_pos.x = block_min_pos.x + 16
if get_params_from_inside(block_min_pos) then return true end
return false
end
-- If not AROUND, the function is never used.
if not AROUND then pos_around = nil end
-- Checks whether the position is protected only according to area_containers.
-- See the overview for this file.
local function is_area_containers_protected(pos)
-- Check that the position is within the protected level:
local y = pos.y
if y >= MIN_APPLICABLE_Y and y <= MAX_APPLICABLE_Y then
-- The minimum position of the block containing pos:
local block_min_pos = vector.apply(pos, floor_blocksize)
if get_params_from_inside(block_min_pos) then
-- The position is in an inside block.
-- Protect the walls:
local block_offset = vector.subtract(pos, block_min_pos)
if block_offset.x == 0 or block_offset.x == 15 or
block_offset.y == 0 or block_offset.y == 15 or
block_offset.z == 0 or block_offset.z == 15 then
return true
end
elseif PADDING then
if LAYER then
-- Non-inside blocks in the layer are protected.
return true
else
return pos_around(block_min_pos)
end
end
end
return false
end
local old_is_protected = minetest.is_protected
function minetest.is_protected(pos, name)
-- Apply our mod's protection unless the player can bypass it:
if is_area_containers_protected(pos) and
not minetest.check_player_privs(name, "protection_bypass") then
return true
end
return old_is_protected(pos, name)
end