forked from DFHack/scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtroubleshoot-item.lua
89 lines (79 loc) · 2.89 KB
/
troubleshoot-item.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
-- troubleshoot-item.lua
--@ module = true
--[====[
troubleshoot-item
=================
Print various properties of the selected item. Sometimes useful for
troubleshooting issues such as why dwarves won't pick up a certain item.
]====]
function find_specific_ref(object, type)
for i, ref in pairs(object.specific_refs) do
if ref.type == type then
return ref
end
end
end
function coord_to_str(coord)
local out = {}
for k, v in pairs(coord) do
-- handle 2D and 3D coordinates
if k == 'x' then out[1] = v end
if k == 'y' then out[2] = v end
if k == 'z' then out[3] = v end
end
return '(' .. table.concat(out, ', ') .. ')'
end
function troubleshoot_item(item, out)
local outstr = nil --as:string
if out == nil then
outstr = ''
out = function(s) outstr = outstr .. s .. '\n' end
end
local function warn(s) out('WARNING: ' .. s) end
assert(df.item:is_instance(item), 'not an item')
if item.id < 0 then warn('Invalid ID: ' .. item.id) end
if not df.item.find(item.id) then warn('Could not locate item in item lists') end
if item.flags.forbid then out('Forbidden') end
if item.flags.melt then out('Melt-designated') end
if item.flags.dump then out('Dump-designated') end
if item.flags.in_chest then out('In chest') end
if item.flags.on_fire then out('On fire') end
if item.flags.rotten then out('Rotten') end
if item.flags.trader then out('Trade good') end
if item.flags.owned then out('Owned') end
if item.flags.foreign then out('Foreign') end
if item.flags.encased then out('Encased in ice') end
if item.flags.garbage_collect then warn('Marked for garbage collection') end
if item.flags.construction then out('In construction') end
if item.flags.in_building then out('In building') end
if item.flags.in_job then
out('In job')
local ref = find_specific_ref(item, df.specific_ref_type.JOB)
if ref then
out('Job type: ' .. df.job_type[ref.data.JOB.job_type])
out('Job position: ' .. coord_to_str(ref.data.JOB.pos))
local found_job_item = false
for i, job_item_ref in pairs(ref.data.JOB.items) do
if item == job_item_ref.item then found_job_item = true end
end
if not found_job_item then warn('Item not attached to job') end
else
warn('No job specific_ref found')
end
end
for i, ref in pairs(item.specific_refs) do
if ref.type ~= df.specific_ref_type.JOB then
out('Unhandled specific_ref: ' .. df.specific_ref_type[ref.type])
end
end
if outstr then return outstr end
end
function main(args)
local item = dfhack.gui.getSelectedItem(true)
if item then
troubleshoot_item(item, print)
else
qerror('No item found')
end
end
if not moduleMode then main({...}) end