Skip to content

Commit 66372e7

Browse files
authoredFeb 25, 2018
is_area_protected: Rename from intersects_protection (#7073)
* is_area_protected: Rename from intersects_protection Return first protected position Clarify docs: Mods may overwrite the function
1 parent 4118e15 commit 66372e7

File tree

2 files changed

+18
-11
lines changed

2 files changed

+18
-11
lines changed
 

‎builtin/game/misc.lua

+12-8
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ end
175175

176176
-- Checks if specified volume intersects a protected volume
177177

178-
function core.intersects_protection(minp, maxp, player_name, interval)
178+
function core.is_area_protected(minp, maxp, player_name, interval)
179179
-- 'interval' is the largest allowed interval for the 3D lattice of checks.
180180

181181
-- Compute the optimal float step 'd' for each axis so that all corners and
@@ -188,14 +188,18 @@ function core.intersects_protection(minp, maxp, player_name, interval)
188188
local d = {}
189189

190190
for _, c in pairs({"x", "y", "z"}) do
191+
if minp[c] > maxp[c] then
192+
-- Repair positions: 'minp' > 'maxp'
193+
local tmp = maxp[c]
194+
maxp[c] = minp[c]
195+
minp[c] = tmp
196+
end
197+
191198
if maxp[c] > minp[c] then
192199
d[c] = (maxp[c] - minp[c]) /
193200
math.ceil((maxp[c] - minp[c]) / interval) - 1e-4
194-
elseif maxp[c] == minp[c] then
201+
else
195202
d[c] = 1 -- Any value larger than 0 to avoid division by zero
196-
else -- maxp[c] < minp[c], print error and treat as protection intersected
197-
minetest.log("error", "maxp < minp in 'minetest.intersects_protection()'")
198-
return true
199203
end
200204
end
201205

@@ -205,13 +209,13 @@ function core.intersects_protection(minp, maxp, player_name, interval)
205209
local y = math.floor(yf + 0.5)
206210
for xf = minp.x, maxp.x, d.x do
207211
local x = math.floor(xf + 0.5)
208-
if core.is_protected({x = x, y = y, z = z}, player_name) then
209-
return true
212+
local pos = {x = x, y = y, z = z}
213+
if core.is_protected(pos, player_name) then
214+
return pos
210215
end
211216
end
212217
end
213218
end
214-
215219
return false
216220
end
217221

‎doc/lua_api.txt

+6-3
Original file line numberDiff line numberDiff line change
@@ -3429,16 +3429,19 @@ These functions return the leftover itemstack.
34293429
* `minetest.record_protection_violation(pos, name)`
34303430
* This function calls functions registered with
34313431
`minetest.register_on_protection_violation`.
3432-
* `minetest.intersects_protection(minp, maxp, player_name, interval)
3433-
* Returns a boolean, returns true if the volume defined by `minp` and `maxp`
3434-
intersects a protected area not owned by `player_name`.
3432+
* `minetest.is_area_protected(pos1, pos2, player_name, interval)
3433+
* Returns the position of the first node that `player_name` may not modify in
3434+
the specified cuboid between `pos1` and `pos2`.
3435+
* Returns `false` if no protections were found.
34353436
* Applies `is_protected()` to a 3D lattice of points in the defined volume.
34363437
The points are spaced evenly throughout the volume and have a spacing
34373438
similar to, but no larger than, `interval`.
34383439
* All corners and edges of the defined volume are checked.
34393440
* `interval` defaults to 4.
34403441
* `interval` should be carefully chosen and maximised to avoid an excessive
34413442
number of points being checked.
3443+
* Like `minetest.is_protected`, this function may be extended or overwritten by
3444+
mods to provide a faster implementation to check the cuboid for intersections.
34423445
* `minetest.rotate_and_place(itemstack, placer, pointed_thing, infinitestacks, orient_flags)`
34433446
* Attempt to predict the desired orientation of the facedir-capable node
34443447
defined by `itemstack`, and place it accordingly (on-wall, on the floor, or

0 commit comments

Comments
 (0)
Please sign in to comment.