Skip to content

Commit a14e732

Browse files
committed
check and fix room ownership links
1 parent 5ca93b9 commit a14e732

File tree

3 files changed

+50
-16
lines changed

3 files changed

+50
-16
lines changed

changelog.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ Template for new versions:
5858
- `assign-minecarts`: reassign vehicles to routes where the vehicle has been destroyed (or has otherwise gone missing)
5959
- `fix/dry-buckets`: prompt DF to recheck requests for aid (e.g. "bring water" jobs) when a bucket is unclogged and becomes available for use
6060
- `exterminate`: show descriptive names for the listed races in addition to their IDs
61+
- `fix/ownership`: now also checks and fixes room ownership links
6162

6263
## Documentation
6364
- `gui/embark-anywhere`: add information about how the game determines world tile pathability and instructions for bridging two landmasses

docs/fix/ownership.rst

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,20 @@ fix/ownership
22
=============
33

44
.. dfhack-tool::
5-
:summary: Fixes instances of units claiming the same item or an item they don't own.
6-
:tags: fort bugfix units
5+
:summary: Fixes ownership links.
6+
:tags: fort bugfix items units
77

8-
Due to a bug a unit can believe they own an item when they actually do not.
8+
Due to a bug, a unit can believe they own an item when they actually do not.
9+
Additionally, a room can remember that it is owned by a unit, but the unit can
10+
forget that they own the room.
911

10-
When enabled in `gui/control-panel`, `fix/ownership` will run once a day to check citizens and residents and make sure they don't
11-
mistakenly own an item they shouldn't.
12+
Invalid item ownership links result in units getting stuck in a "Store owned
13+
item" job. Missing room ownership links result in rooms becoming unused by the
14+
nominal owner and unclaimable by any other unit. In particular, nobles and
15+
administrators will not recognize that their room requirements are met.
1216

13-
This should help issues of units getting stuck in a "Store owned item" job.
17+
When enabled in `gui/control-panel`, `fix/ownership` will run once a day to
18+
validate and fix ownership links for items and rooms.
1419

1520
Usage
1621
-----

fix/ownership.lua

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,57 @@
1+
local utils = require('utils')
2+
13
-- unit thinks they own the item but the item doesn't hold the proper
24
-- ref that actually makes this true
3-
local function owner_not_recognized()
5+
local function clean_item_ownership()
46
for _,unit in ipairs(dfhack.units.getCitizens()) do
57
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
1316
end
1417
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))))
1620
unit.owned_items:erase(index)
1721
::continue::
1822
end
1923
end
2024
end
2125

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+
2249
local args = {...}
2350

2451
if args[1] == "help" then
2552
print(dfhack.script_help())
2653
return
2754
end
2855

29-
owner_not_recognized()
56+
clean_item_ownership()
57+
relink_zones()

0 commit comments

Comments
 (0)