Skip to content

Commit 57110a0

Browse files
authored
Merge pull request #1614 from Alistair-Afton/stuck-written-materials
fix/stuck-written-materials: new tool to free written materials stuck in jobs
2 parents e8d84eb + 5b8d35e commit 57110a0

5 files changed

Lines changed: 385 additions & 0 deletions

File tree

‎changelog.txt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ Template for new versions:
2727
# Future
2828

2929
## New Tools
30+
- `fix/stuck-written-materials`: free written materials that are stuck in a non-existent job
3031

3132
- `devel/infinite-sky-probe`: tool for testing whether infinite sky is working correctly
3233
- `gui/export-world-map`: New GUI tool to configure world map exports from the embark selection screen.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
fix/stuck-written-materials
2+
===========================
3+
4+
.. dfhack-tool::
5+
:summary: Allow bugged written materials to be interacted with again.
6+
:tags: fort bugfix items
7+
8+
Fixes books, quires, and scrolls that are stuck permanently in a job that no
9+
longer exists. This can happen, for example, when a visitor that was reading
10+
or carrying a written work joins the fortress, or when squads return from
11+
missions with written materials.
12+
13+
This works around the same family of issues as `fix/stuck-instruments`
14+
(:bug:`9485`), and should be run if you notice any written materials that
15+
cannot be hauled or interacted with.
16+
17+
18+
Usage
19+
-----
20+
21+
``fix/stuck-written-materials``
22+
Fixes item data for all stuck written materials on the map.
23+
``fix/stuck-written-materials -n``, ``fix/stuck-written-materials --dry-run``
24+
List how many written materials would be fixed without performing the
25+
action.

‎fix/stuck-written-materials.lua‎

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
-- Fixes written materials that are stuck in a non-existent job
2+
--@module = true
3+
4+
local argparse = require('argparse')
5+
local utils = require('utils')
6+
7+
local quire_subtype, scroll_subtype
8+
9+
function is_written_material(item)
10+
if df.item_bookst:is_instance(item) then
11+
return true
12+
end
13+
if not df.item_toolst:is_instance(item) then
14+
return false
15+
end
16+
local subtype = item:getSubtype()
17+
return subtype == quire_subtype or subtype == scroll_subtype
18+
end
19+
20+
local function get_ids_of_items_in_jobs()
21+
local ids = {}
22+
for _, job in utils.listpairs(df.global.world.jobs.list) do
23+
for _, item_ref in ipairs(job.items) do
24+
if item_ref.item then
25+
ids[item_ref.item.id] = true
26+
end
27+
end
28+
end
29+
return ids
30+
end
31+
32+
function is_in_live_job(item, ids)
33+
-- contents of a container are flagged in_job when the container itself
34+
-- is attached to a job
35+
while item do
36+
if ids[item.id] then return true end
37+
item = dfhack.items.getContainer(item)
38+
end
39+
return false
40+
end
41+
42+
local function remove_dead_refs(item)
43+
for i = #item.specific_refs - 1, 0, -1 do
44+
local ref = item.specific_refs[i]
45+
-- the jobs these refs point at may have already been deleted, so the
46+
-- ref itself must not be dereferenced; just remove it
47+
if ref.type == df.specific_ref_type.JOB then
48+
ref:delete()
49+
item.specific_refs:erase(i)
50+
end
51+
end
52+
end
53+
54+
function fixWrittenMaterials(opts)
55+
quire_subtype = dfhack.items.findSubtype('TOOL:ITEM_TOOL_QUIRE')
56+
scroll_subtype = dfhack.items.findSubtype('TOOL:ITEM_TOOL_SCROLL')
57+
58+
local fixed = 0
59+
local in_job_ids = get_ids_of_items_in_jobs()
60+
61+
for _, vec in ipairs{df.global.world.items.other.BOOK,
62+
df.global.world.items.other.TOOL} do
63+
for _, item in ipairs(vec) do
64+
if not is_written_material(item) then goto continue end
65+
66+
local stuck = false
67+
68+
-- written materials can keep references to activity events that no
69+
-- longer exist, e.g. when a visitor that was reading or carrying
70+
-- them joins the fort (bug 9485)
71+
for i = #item.general_refs - 1, 0, -1 do
72+
local ref = item.general_refs[i]
73+
if ref:getType() == df.general_ref_type.ACTIVITY_EVENT and
74+
not df.activity_entry.find(ref.activity_id) then
75+
if not opts.dry_run then
76+
ref:delete()
77+
item.general_refs:erase(i)
78+
end
79+
stuck = true
80+
end
81+
end
82+
83+
-- they can also be left with the in_job flag set while no actual
84+
-- job references them
85+
if item.flags.in_job and not is_in_live_job(item, in_job_ids) then
86+
if not opts.dry_run then
87+
remove_dead_refs(item)
88+
item.flags.in_job = false
89+
end
90+
stuck = true
91+
end
92+
93+
if stuck then
94+
print(dfhack.df2console(('Found stuck written material: %s'):format(
95+
dfhack.items.getDescription(item, 0, true))))
96+
fixed = fixed + 1
97+
end
98+
99+
::continue::
100+
end
101+
end
102+
103+
if fixed > 0 or opts.dry_run then
104+
print(("%s %d stuck written material(s)."):format(
105+
opts.dry_run and "Found" or "Fixed",
106+
fixed
107+
))
108+
end
109+
end
110+
111+
if dfhack_flags.module then
112+
return
113+
end
114+
115+
local opts = {}
116+
117+
local positionals = argparse.processArgsGetopt({...}, {
118+
{ 'h', 'help', handler = function() opts.help = true end },
119+
{ 'n', 'dry-run', handler = function() opts.dry_run = true end },
120+
})
121+
122+
if positionals[1] == 'help' or opts.help then
123+
print(dfhack.script_help())
124+
return
125+
end
126+
127+
fixWrittenMaterials(opts)

‎internal/control-panel/registry.lua‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ COMMANDS_BY_IDX = {
103103
params={'--time', '1', '--timeUnits', 'days', '--command', '[', 'fix/stuck-squad', ']'}},
104104
{command='fix/stuck-worship', group='bugfix', mode='repeat', default=true,
105105
params={'--time', '1', '--timeUnits', 'days', '--command', '[', 'fix/stuck-worship', '-q', ']'}},
106+
{command='fix/stuck-written-materials', group='bugfix', mode='repeat', default=true,
107+
params={'--time', '1', '--timeUnits', 'days', '--command', '[', 'fix/stuck-written-materials', ']'}},
106108
{command='fix/noexert-exhaustion', group='bugfix', mode='repeat', default=true,
107109
params={'--time', '439', '--timeUnits', 'ticks', '--command', '[', 'fix/noexert-exhaustion', ']'}},
108110
{command='fix/wildlife', group='bugfix', mode='repeat',

0 commit comments

Comments
 (0)