Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: infinite loop in Zone:randomPosition when no valid tile exist #3178

Merged
merged 1 commit into from
Dec 14, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions data/libs/systems/zones.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,24 @@ function Zone:randomPosition()
logger.error("Zone:randomPosition() - Zone {} has no positions", self:getName())
return nil
end
local destination = positions[math.random(1, #positions)]
local tile = destination:getTile()
while not tile or not tile:isWalkable(false, false, false, false, true) do
destination = positions[math.random(1, #positions)]
tile = destination:getTile()

local validPositions = {}
for _, position in ipairs(positions) do
local tile = position:getTile()
if tile and tile:isWalkable(false, false, false, false, true) then
table.insert(validPositions, position)
else
logger.debug("Zone:randomPosition() - Position {} is invalid (Tile: {}, Walkable: {})", position, tile or "nil", tile and tile:isWalkable(false, false, false, false, true) or "false")
end
end

if #validPositions == 0 then
logger.error("Zone:randomPosition() - No valid positions in Zone {}", self:getName())
return nil
end

local destination = validPositions[math.random(1, #validPositions)]
logger.debug("Zone:randomPosition() - Selected valid position: {}", destination)
return destination
end

Expand Down
Loading