Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
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
  • Loading branch information
SmallJoker committed Feb 25, 2018
1 parent 4118e15 commit 66372e7
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
20 changes: 12 additions & 8 deletions builtin/game/misc.lua
Expand Up @@ -175,7 +175,7 @@ end

-- Checks if specified volume intersects a protected volume

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

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

for _, c in pairs({"x", "y", "z"}) do
if minp[c] > maxp[c] then
-- Repair positions: 'minp' > 'maxp'
local tmp = maxp[c]
maxp[c] = minp[c]
minp[c] = tmp
end

if maxp[c] > minp[c] then
d[c] = (maxp[c] - minp[c]) /
math.ceil((maxp[c] - minp[c]) / interval) - 1e-4
elseif maxp[c] == minp[c] then
else
d[c] = 1 -- Any value larger than 0 to avoid division by zero
else -- maxp[c] < minp[c], print error and treat as protection intersected
minetest.log("error", "maxp < minp in 'minetest.intersects_protection()'")
return true
end
end

Expand All @@ -205,13 +209,13 @@ function core.intersects_protection(minp, maxp, player_name, interval)
local y = math.floor(yf + 0.5)
for xf = minp.x, maxp.x, d.x do
local x = math.floor(xf + 0.5)
if core.is_protected({x = x, y = y, z = z}, player_name) then
return true
local pos = {x = x, y = y, z = z}
if core.is_protected(pos, player_name) then
return pos
end
end
end
end

return false
end

Expand Down
9 changes: 6 additions & 3 deletions doc/lua_api.txt
Expand Up @@ -3429,16 +3429,19 @@ These functions return the leftover itemstack.
* `minetest.record_protection_violation(pos, name)`
* This function calls functions registered with
`minetest.register_on_protection_violation`.
* `minetest.intersects_protection(minp, maxp, player_name, interval)
* Returns a boolean, returns true if the volume defined by `minp` and `maxp`
intersects a protected area not owned by `player_name`.
* `minetest.is_area_protected(pos1, pos2, player_name, interval)
* Returns the position of the first node that `player_name` may not modify in
the specified cuboid between `pos1` and `pos2`.
* Returns `false` if no protections were found.
* Applies `is_protected()` to a 3D lattice of points in the defined volume.
The points are spaced evenly throughout the volume and have a spacing
similar to, but no larger than, `interval`.
* All corners and edges of the defined volume are checked.
* `interval` defaults to 4.
* `interval` should be carefully chosen and maximised to avoid an excessive
number of points being checked.
* Like `minetest.is_protected`, this function may be extended or overwritten by
mods to provide a faster implementation to check the cuboid for intersections.
* `minetest.rotate_and_place(itemstack, placer, pointed_thing, infinitestacks, orient_flags)`
* Attempt to predict the desired orientation of the facedir-capable node
defined by `itemstack`, and place it accordingly (on-wall, on the floor, or
Expand Down

0 comments on commit 66372e7

Please sign in to comment.