|
| 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) |
0 commit comments