Skip to content

Commit bf29271

Browse files
committed
clean up deathcause target selection logic
1 parent 6cd1f6e commit bf29271

File tree

1 file changed

+18
-34
lines changed

1 file changed

+18
-34
lines changed

deathcause.lua

Lines changed: 18 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -127,55 +127,39 @@ local function is_corpse_item(item)
127127
return itype == df.item_type.CORPSE or itype == df.item_type.CORPSEPIECE
128128
end
129129

130-
local selected_item = dfhack.gui.getSelectedItem(true)
131-
local selected_unit = dfhack.gui.getSelectedUnit(true)
130+
local view_sheets = df.global.game.main_interface.view_sheets
132131

133-
if not selected_unit and not selected_item then
134-
local pos
132+
local function get_target()
133+
local selected_unit = dfhack.gui.getSelectedUnit(true)
134+
if selected_unit then
135+
return selected_unit.hist_figure_id, selected_unit
136+
end
137+
local selected_item = dfhack.gui.getSelectedItem(true)
135138
if not selected_item and
136139
dfhack.gui.matchFocusString('dwarfmode/ViewSheets/ITEM_LIST', dfhack.gui.getDFViewscreen(true)) and
137-
#df.global.game.main_interface.view_sheets.viewing_itid > 0
140+
#view_sheets.viewing_itid > 0
138141
then
139-
selected_item = df.item.find(df.global.game.main_interface.view_sheets.viewing_itid[0])
140-
elseif selected_item then
141-
pos = xyz2pos(dfhack.items.getPosition(selected_item))
142-
end
143-
if not selected_item then
144-
pos = guidm.getCursorPos()
145-
end
146-
if pos then
147-
-- if there isn't a selected unit and we don't have a selected item or the selected item is not a corpse
148-
-- let's try to look for corpses at the same location because it's probably what the user wants
149-
-- we will just grab the first one as it's the best we can do
142+
local pos = xyz2pos(dfhack.items.getPosition(df.item.find(view_sheets.viewing_itid[0])))
150143
selected_item = getItemAtPosition(pos)
151144
end
152-
end
153-
154-
if not selected_unit and not is_corpse_item(selected_item) then
155-
if df.item_remainsst:is_instance(selected_item) then
156-
print(("The %s died."):format(getRaceNameSingular(selected_item.race)))
157-
return
145+
if not is_corpse_item(selected_item) then
146+
if df.item_remainsst:is_instance(selected_item) then
147+
print(("The %s died."):format(getRaceNameSingular(selected_item.race)))
148+
return
149+
end
150+
qerror("Please select a unit, a corpse, or a body part")
158151
end
159-
qerror("Please select a corpse")
152+
return selected_item.hist_figure_id, df.unit.find(selected_item.unit_id)
160153
end
161154

162-
local hist_figure_id
163-
if selected_item then
164-
hist_figure_id = selected_item.hist_figure_id
165-
elseif selected_unit then
166-
hist_figure_id = selected_unit.hist_figure_id
167-
end
155+
local hist_figure_id, selected_unit = get_target()
168156

169157
if not hist_figure_id then
170158
qerror("Cause of death not available")
171159
elseif hist_figure_id == -1 then
172160
if not selected_unit then
173-
selected_unit = df.unit.find(selected_item.unit_id)
174-
if not selected_unit then
175-
qerror("Cause of death not available")
176-
end
161+
qerror("Cause of death not available")
177162
end
178-
179163
displayDeathUnit(selected_unit)
180164
else
181165
displayDeathHistFig(df.historical_figure.find(hist_figure_id))

0 commit comments

Comments
 (0)