|
| 1 | +local utils = require('utils') |
| 2 | + |
1 | 3 | -- unit thinks they own the item but the item doesn't hold the proper |
2 | 4 | -- ref that actually makes this true |
3 | | -local function owner_not_recognized() |
| 5 | +local function clean_item_ownership() |
4 | 6 | for _,unit in ipairs(dfhack.units.getCitizens()) do |
5 | 7 | for index = #unit.owned_items-1, 0, -1 do |
6 | | - local item = df.item.find(unit.owned_items[index]) |
7 | | - if not item then goto continue end |
8 | | - |
9 | | - for _, ref in ipairs(item.general_refs) do |
10 | | - if df.general_ref_unit_itemownerst:is_instance(ref) then |
11 | | - -- make sure the ref belongs to unit |
12 | | - if ref.unit_id == unit.id then goto continue end |
| 8 | + local item_id = unit.owned_items[index] |
| 9 | + local item = df.item.find(item_id) |
| 10 | + if item then |
| 11 | + for _, ref in ipairs(item.general_refs) do |
| 12 | + if df.general_ref_unit_itemownerst:is_instance(ref) then |
| 13 | + -- make sure the ref belongs to unit |
| 14 | + if ref.unit_id == unit.id then goto continue end |
| 15 | + end |
13 | 16 | end |
14 | 17 | end |
15 | | - print('Erasing ' .. dfhack.TranslateName(unit.name) .. ' invalid claim on item #' .. item.id) |
| 18 | + print(('fix/ownership: Erasing invalid claim on item #%d for %s'):format( |
| 19 | + item_id, dfhack.df2console(dfhack.units.getReadableName(unit)))) |
16 | 20 | unit.owned_items:erase(index) |
17 | 21 | ::continue:: |
18 | 22 | end |
19 | 23 | end |
20 | 24 | end |
21 | 25 |
|
| 26 | +local other = df.global.world.buildings.other |
| 27 | +local zone_vecs = { |
| 28 | + other.ZONE_BEDROOM, |
| 29 | + other.ZONE_OFFICE, |
| 30 | + other.ZONE_DINING_HALL, |
| 31 | + other.ZONE_TOMB, |
| 32 | +} |
| 33 | +local function relink_zones() |
| 34 | + for _,zones in ipairs(zone_vecs) do |
| 35 | + for _,zone in ipairs(zones) do |
| 36 | + local unit = zone.assigned_unit |
| 37 | + if not unit then goto continue end |
| 38 | + if not utils.linear_index(unit.owned_buildings, zone.id, 'id') then |
| 39 | + print(('fix/ownership: Restoring %s ownership link for %s'):format( |
| 40 | + df.civzone_type[zone:getSubtype()], dfhack.df2console(dfhack.units.getReadableName(unit)))) |
| 41 | + dfhack.buildings.setOwner(zone, nil) |
| 42 | + dfhack.buildings.setOwner(zone, unit) |
| 43 | + end |
| 44 | + ::continue:: |
| 45 | + end |
| 46 | + end |
| 47 | +end |
| 48 | + |
22 | 49 | local args = {...} |
23 | 50 |
|
24 | 51 | if args[1] == "help" then |
25 | 52 | print(dfhack.script_help()) |
26 | 53 | return |
27 | 54 | end |
28 | 55 |
|
29 | | -owner_not_recognized() |
| 56 | +clean_item_ownership() |
| 57 | +relink_zones() |
0 commit comments