Skip to content

Commit

Permalink
add option for scrubbing dead units from burrows
Browse files Browse the repository at this point in the history
  • Loading branch information
myk002 committed Nov 13, 2023
1 parent 6e68ebd commit d9bca7a
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 22 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/design`: show selected dimensions next to the mouse cursor when designating with vanilla tools, for example when painting a burrow or designating digging
- `quickfort`: new blueprint mode for designating burrows
- `unforbid`: now ignores worn and tattered items by default (X/XX), use -X to bypass
- `fix/dead-units`: gained ability to scrub dead units from burrow membership lists

## Fixes
- `gui/unit-syndromes`: show the syndrome names properly in the UI
Expand Down
19 changes: 18 additions & 1 deletion docs/fix/dead-units.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,26 @@ If so many units have died at your fort that your dead units list exceeds about
(like slaughtered animals and nameless goblins) from the unit list, allowing
migrants to start coming again.

It also supports scanning burrows and cleaning out dead units from burrow
assignments. The vanilla UI doesn't provide any way to remove dead units, and
the dead units artificially increase the reported count of units that are
assigned to the burrow.

Usage
-----

::

fix/dead-units
fix/dead-units [--active] [-q]
fix/dead-units --burrow [-q]

Options
-------

``--active``
Scrub dead units from the ``active`` vector so they don't show up in the
dead units list. This is the default if no option is specified.
``--burrow``
Scrub dead units from burrow membership lists.
``-q``, ``--quiet``
Surpress console output (final status update is still printed if at least one item was affected).
53 changes: 38 additions & 15 deletions fix/dead-units.lua
Original file line number Diff line number Diff line change
@@ -1,28 +1,24 @@
-- Remove uninteresting dead units from the unit list.
--[====[
local argparse = require('argparse')

fix/dead-units
==============
Removes uninteresting dead units from the unit list. Doesn't seem to give any
noticeable performance gain, but migrants normally stop if the unit list grows
to around 3000 units, and this script reduces it back.
]====]
local units = df.global.world.units.active
local count = 0
local MONTH = 1200 * 28
local YEAR = MONTH * 12

for i=#units-1,0,-1 do
local unit = units[i]
if dfhack.units.isDead(unit) and not dfhack.units.isOwnRace(unit) then
local count = 0

local function scrub_active()
for i=#units-1,0,-1 do
local unit = units[i]
if not dfhack.units.isDead(unit) or dfhack.units.isOwnRace(unit) then
goto continue
end
local remove = false
if dfhack.units.isMarkedForSlaughter(unit) then
remove = true
elseif unit.hist_figure_id == -1 then
remove = true
elseif not dfhack.units.isOwnCiv(unit) and
not (dfhack.units.isMerchant(unit) or dfhack.units.isDiplomat(unit)) then
not (dfhack.units.isMerchant(unit) or dfhack.units.isDiplomat(unit)) then
remove = true
end
if remove and unit.counters.death_id ~= -1 then
Expand All @@ -44,7 +40,34 @@ for i=#units-1,0,-1 do
count = count + 1
units:erase(i)
end
::continue::
end
end

local function scrub_burrows()
for _, burrow in ipairs(df.global.plotinfo.burrows.list) do
for _, unit_id in ipairs(burrow.units) do
local unit = df.unit.find(unit_id)
if unit and dfhack.units.isDead(unit) then
count = count + 1
dfhack.burrows.setAssignedUnit(burrow, unit, false)
end
end
end
end

print('Units removed from active: '..count)
local args = {...}
if not args[1] then args[1] = '--active' end

local quiet = false

argparse.processArgsGetopt(args, {
{nil, 'active', handler=scrub_active},
{nil, 'burrow', handler=scrub_burrows},
{nil, 'burrows', handler=scrub_burrows},
{'q', 'quiet', handler=function() quiet = true end},
})

if count > 0 or not quiet then
print('Dead units scrubbed: ' .. count)
end
15 changes: 9 additions & 6 deletions gui/control-panel.lua
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,12 @@ local REPEATS = {
['combine']={
desc='Combine partial stacks in stockpiles into full stacks.',
command={'--time', '7', '--timeUnits', 'days', '--command', '[', 'combine', 'all', '-q', ']'}},
['stuck-instruments']={
desc='Fix activity references on stuck instruments to make them usable again.',
command={'--time', '1', '--timeUnits', 'days', '--command', '[', 'fix/stuck-instruments', ']'}},
['dead-units-burrow']={
desc='Fix units still being assigned to burrows after death.',
command={'--time', '7', '--timeUnits', 'days', '--command', '[', 'fix/dead-units', '--burrow', '-q', ']'}},
['empty-wheelbarrows']={
desc='Empties wheelbarrows which have rocks stuck in them.',
command={'--time', '1', '--timeUnits', 'days', '--command', '[', 'fix/empty-wheelbarrows', '-q', ']'}},
['general-strike']={
desc='Prevent dwarves from getting stuck and refusing to work.',
command={'--time', '1', '--timeUnits', 'days', '--command', '[', 'fix/general-strike', '-q', ']'}},
Expand All @@ -129,15 +132,15 @@ local REPEATS = {
['orders-reevaluate']={
desc='Invalidates work orders once a month, allowing conditions to be rechecked.',
command={'--time', '1', '--timeUnits', 'months', '--command', '[', 'orders', 'recheck', ']'}},
['stuck-instruments']={
desc='Fix activity references on stuck instruments to make them usable again.',
command={'--time', '1', '--timeUnits', 'days', '--command', '[', 'fix/stuck-instruments', ']'}},
['warn-starving']={
desc='Show a warning dialog when units are starving or dehydrated.',
command={'--time', '10', '--timeUnits', 'days', '--command', '[', 'warn-starving', ']'}},
['warn-stranded']={
desc='Show a warning dialog when units are stranded from all others.',
command={'--time', '0.25', '--timeUnits', 'days', '--command', '[', 'warn-stranded', ']'}},
['empty-wheelbarrows']={
desc='Empties wheelbarrows which have rocks stuck in them.',
command={'--time', '1', '--timeUnits', 'days', '--command', '[', 'fix/empty-wheelbarrows', '-q', ']'}},
}
local REPEATS_LIST = {}
for k in pairs(REPEATS) do
Expand Down

0 comments on commit d9bca7a

Please sign in to comment.