Skip to content

Commit

Permalink
Handle large protected depths
Browse files Browse the repository at this point in the history
Portals will now fail to ignite instead of defaulting to a fallback depth of (starty - 256) when no unprotected target can be found.
The search depth is also increased from (starty - 256) to (starty - 646)

This is to properly handle large protected areas that are sometimes set up at spawn - see issue #26
  • Loading branch information
Treer committed Aug 2, 2020
1 parent e326a94 commit 02d062b
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 9 deletions.
2 changes: 1 addition & 1 deletion init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ function nether.debug(message, ...)
end
end

local composed_message = string.format(message, unpack(args))
local composed_message = "nether: " .. string.format(message, unpack(args))

if math.floor(DEBUG_FLAGS / 1) % 2 == 1 then print(composed_message) end
if math.floor(DEBUG_FLAGS / 2) % 2 == 1 then minetest.chat_send_all(composed_message) end
Expand Down
29 changes: 23 additions & 6 deletions portal_api.lua
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ if minetest.get_mod_storage == nil then
error(nether.modname .. " does not support Minetest versions earlier than 0.4.16", 0)
end

local S = nether.get_translator
nether.portal_destination_not_found_message =
S("Mysterious forces prevented you from opening that portal. Please try another location")


--[[
Positions
Expand Down Expand Up @@ -663,7 +668,6 @@ nether.PortalShape_Platform = {
-- Portal implementation functions --
-- =============================== --

local S = nether.get_translator
local debugf = nether.debug
local ignition_item_name
local mod_storage = minetest.get_mod_storage()
Expand Down Expand Up @@ -1367,8 +1371,13 @@ local function ignite_portal(ignition_pos, player_name, ignition_node_name)
destination_orientation = orientation
end

if destination_anchorPos == nil then
if destination_anchorPos == nil or destination_anchorPos.y == nil then
-- destination_anchorPos.y was also checked for nil in case portal_definition.find_surface_anchorPos()
-- had used nether.find_surface_target_y() and that had returned nil.
debugf("No portal destination available here!")
if (player_name or "") ~= "" then
minetest.chat_send_player(player_name, nether.portal_destination_not_found_message)
end
return false
else
local destination_wormholePos = portal_definition.shape.get_wormholePos_from_anchorPos(destination_anchorPos, destination_orientation)
Expand Down Expand Up @@ -2217,7 +2226,7 @@ function nether.volume_is_natural_and_unprotected(minp, maxp, player_name)
end

if minetest.is_area_protected(minp, maxp, player_name or "") then
debugf("Volume is protected %s-%s", minp, maxp)
debugf("Volume is protected against player '%s', %s-%s", player_name, minp, maxp)
return false;
end

Expand Down Expand Up @@ -2316,7 +2325,12 @@ function nether.find_surface_target_y(target_x, target_z, portal_name, player_na
local minp = {x = minp_schem.x, y = 0, z = minp_schem.z}
local maxp = {x = maxp_schem.x, y = 0, z = maxp_schem.z}

for y = start_y, start_y - 256, -16 do
-- Starting searchstep at -16 and making it larger by 2 after each step gives a 20-step search range down to -646:
-- 0, -16, -34, -54, -76, -100, -126, -154, -184, -216, -250, -286, -324, -364, -406, -450, -496, -544, -594, -646
local searchstep = -16;

local y = start_y
while y > start_y - 650 do
-- Check volume for non-natural nodes
minp.y = minp_schem.y + y
maxp.y = maxp_schem.y + y
Expand All @@ -2331,9 +2345,11 @@ function nether.find_surface_target_y(target_x, target_z, portal_name, player_na
return y
end
end
y = y + searchstep
searchstep = searchstep - 2
end

return start_y - 256 -- Fallback
return nil -- Portal ignition failure. Possibly due to a large protected area.
end


Expand Down Expand Up @@ -2365,7 +2381,8 @@ function nether.find_nearest_working_portal(portal_name, anchorPos, distance_lim
if portalFound then
return portal_info.anchorPos, portal_info.orientation
else
debugf("Portal wasn't found, removing portal from mod_storage at %s orientation %s", portal_info.anchorPos, portal_info.orientation)
debugf("Portal wasn't found, removing portal from mod_storage at %s orientation %s",
portal_info.anchorPos, portal_info.orientation)
-- The portal at that location must have been destroyed
remove_portal_location_info(portal_name, portal_info.anchorPos)
end
Expand Down
8 changes: 6 additions & 2 deletions portal_api.txt
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ Helper functions
surface to be reused.
* player_name is optional, providing it prevents the exclusion of surface
target areas which are protected by the player.
* May return nil in extreme circumstances, such as the surface being
protected down to a great depth.

* `nether.find_nearest_working_portal(portal_name, anchorPos, distance_limit, y_factor)`: returns
(anchorPos, orientation), or nil if no portal was found within the
Expand Down Expand Up @@ -222,7 +224,8 @@ Used by `nether.register_portal`.
-- If the location of an existing portal is returned then include the
-- orientation, otherwise the existing portal could be overwritten by
-- a new one with the orientation of the surface portal.
-- Return nil to prevent the portal from igniting.
-- Return nil, or a position with a nil y component, to prevent the
-- portal from igniting.
-- player_name may be "", e.g. if the portal was ignited by a mesecon,
-- and is provided for use with volume_is_natural_and_unprotected() etc.

Expand All @@ -239,7 +242,8 @@ Used by `nether.register_portal`.
-- If the location of an existing portal is returned then include the
-- orientation, otherwise the existing portal could be overwritten by
-- a new one with the orientation of the realm portal.
-- Return nil to prevent the portal from igniting.
-- Return nil, or a position with a nil y component, to prevent the
-- portal from igniting.
-- player_name may be "", e.g. if the portal was ignited by a mesecon,
-- and is provided for use with volume_is_natural_and_unprotected() etc.

Expand Down

0 comments on commit 02d062b

Please sign in to comment.