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

[warn-stranded] don't warn for units on unwalkable tiles #876

Merged
merged 1 commit into from
Oct 27, 2023
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ Template for new versions:
- `gui/unit-syndromes`: show the syndrome names properly in the UI

## Misc Improvements
- `warn-stranded`: don't warn for units that are temporarily on unwalkable tiles (e.g. as they pass under a waterfall)

## Removed

Expand Down
27 changes: 22 additions & 5 deletions warn-stranded.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
--@module = true

local gui = require 'gui'
local utils = require 'utils'
local widgets = require 'gui.widgets'
local argparse = require 'argparse'
local args = {...}
Expand Down Expand Up @@ -266,7 +265,7 @@ function WarningWindow:onClear()
end

function WarningWindow:onZoom()
local index, choice = self.subviews.list:getSelected()
local _, choice = self.subviews.list:getSelected()
local unit = choice.data['unit']

local target = xyz2pos(dfhack.units.getPosition(unit))
Expand Down Expand Up @@ -299,6 +298,11 @@ local function compareGroups(group_one, group_two)
return #group_one['units'] < #group_two['units']
end

local function getWalkGroup(pos)
local block = dfhack.maps.getTileBlock(pos)
return block and block.walkable[pos.x % 16][pos.y % 16]
end

local function getStrandedUnits()
local groupCount = 0
local grouped = {}
Expand All @@ -310,9 +314,22 @@ local function getStrandedUnits()

-- Pathability group calculation is from gui/pathable
for _, unit in ipairs(citizens) do
local target = xyz2pos(dfhack.units.getPosition(unit))
local block = dfhack.maps.getTileBlock(target)
local walkGroup = block and block.walkable[target.x % 16][target.y % 16] or 0
local unitPos = xyz2pos(dfhack.units.getPosition(unit))
local walkGroup = getWalkGroup(unitPos) or 0

-- if on an unpathable tile, use the walkGroup of an adjacent tile. this prevents
-- warnings for units that are walking under falling water, which sometimes makes
-- a tile unwalkable while the unit is standing on it
if walkGroup == 0 then
walkGroup = getWalkGroup(xyz2pos(unitPos.x-1, unitPos.y-1, unitPos.z))
or getWalkGroup(xyz2pos(unitPos.x, unitPos.y-1, unitPos.z))
or getWalkGroup(xyz2pos(unitPos.x+1, unitPos.y-1, unitPos.z))
or getWalkGroup(xyz2pos(unitPos.x-1, unitPos.y, unitPos.z))
or getWalkGroup(xyz2pos(unitPos.x+1, unitPos.y, unitPos.z))
or getWalkGroup(xyz2pos(unitPos.x-1, unitPos.y+1, unitPos.z))
or getWalkGroup(xyz2pos(unitPos.x, unitPos.y+1, unitPos.z))
or getWalkGroup(xyz2pos(unitPos.x+1, unitPos.y+1, unitPos.z))
end

if unitIgnored(unit) then
table.insert(ensure_key(ignoredGroup, walkGroup), unit)
Expand Down