Skip to content

Commit

Permalink
don't warn for units on unwalkable tiles
Browse files Browse the repository at this point in the history
if an adjacent tile is walkable. this avoids warning for, for example,
units walking under waterfalls or through variable depth water
  • Loading branch information
myk002 committed Oct 27, 2023
1 parent 5e7718c commit d842e90
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
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

0 comments on commit d842e90

Please sign in to comment.