Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[zone] properly handle animals in cages in non-cage buildings #3578

Merged
merged 1 commit into from
Jul 18, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 16 additions & 9 deletions plugins/lua/zone.lua
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,17 @@ local function get_cage_ref(unit)
return dfhack.units.getGeneralRef(unit, df.general_ref_type.CONTAINED_IN_ITEM)
end

local function get_built_cage(item_cage)
if not item_cage then return end
local built_cage_ref = dfhack.items.getGeneralRef(item_cage, df.general_ref_type.BUILDING_HOLDER)
if not built_cage_ref then return end
local built_cage = df.building.find(built_cage_ref.building_id)
if not built_cage then return end
if built_cage:getType() == df.building_type.Cage then
return built_cage
end
end

local function get_status(unit)
local assigned_pasture_ref = dfhack.units.getGeneralRef(unit, df.general_ref_type.BUILDING_CIVZONE_ASSIGNED)
if assigned_pasture_ref then
Expand All @@ -463,8 +474,7 @@ local function get_status(unit)
end
local cage_ref = get_cage_ref(unit)
if cage_ref then
local cage = df.item.find(cage_ref.item_id)
if dfhack.items.getGeneralRef(cage, df.general_ref_type.BUILDING_HOLDER) then
if get_built_cage(df.item.find(cage_ref.item_id)) then
return STATUS.BUILT_CAGE.value
else
return STATUS.ITEM_CAGE.value
Expand Down Expand Up @@ -573,13 +583,10 @@ local function detach_unit(unit)
unit.general_refs:erase(idx)
ref:delete()
elseif df.general_ref_contained_in_itemst:is_instance(ref) then
local cage = df.item.find(ref.item_id)
if cage then
local built_cage_ref = dfhack.items.getGeneralRef(cage, df.general_ref_type.BUILDING_HOLDER)
if built_cage_ref then
unassign_unit(df.building.find(built_cage_ref.building_id), unit)
-- unit's general ref will be removed when the unit is released from the cage
end
local built_cage = get_built_cage(df.item.find(ref.item_id))
if built_cage and built_cage:getType() == df.building_type.Cage then
unassign_unit(built_cage, unit)
-- unit's general ref will be removed when the unit is released from the cage
end
elseif df.general_ref_building_chainst:is_instance(ref) then
local chain = df.building.find(ref.building_id)
Expand Down