diff --git a/mods/default/functions.lua b/mods/default/functions.lua index 4c8d2f0..60c8d6e 100644 --- a/mods/default/functions.lua +++ b/mods/default/functions.lua @@ -1,5 +1,8 @@ -- mods/default/functions.lua +-- Check for a volume intersecting protection +local is_50 = minetest.has_feature("object_use_texture_alpha") or nil + -- -- Sounds -- @@ -482,25 +485,32 @@ minetest.register_abm({ -- --- Checks if specified volume intersects a protected volume +-- Checks if specified volume intersects a protected volume, return first node intersected in protected area by player -- function default.intersects_protection(minp, maxp, player_name, interval) -- 'interval' is the largest allowed interval for the 3D lattice of checks - + -- but newer engines returned first node that intersect area protected or false + if is_50 then + return minetest.is_area_protected(minp, maxp, player_name, interval) + else -- Compute the optimal float step 'd' for each axis so that all corners and -- borders are checked. 'd' will be smaller or equal to 'interval'. -- Subtracting 1e-4 ensures that the max co-ordinate will be reached by the -- for loop (which might otherwise not be the case due to rounding errors). local d = {} for _, c in pairs({"x", "y", "z"}) do + + if minp[c] > maxp[c] then + local tmp = maxp[c] + maxp[c] = minp[c] + minp[c] = tmp -- Repair positions: 'minp' > 'maxp' + 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 'default.intersects_protection()'") - return true end end @@ -510,14 +520,23 @@ function default.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 minetest.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 + +end + +if not is_50 then + -- older engines backguar compatibility + minetest.is_area_protected = default.intersects_protection end