@@ -127,55 +127,39 @@ local function is_corpse_item(item)
127
127
return itype == df .item_type .CORPSE or itype == df .item_type .CORPSEPIECE
128
128
end
129
129
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
132
131
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 )
135
138
if not selected_item and
136
139
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
138
141
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 ])))
150
143
selected_item = getItemAtPosition (pos )
151
144
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 " )
158
151
end
159
- qerror ( " Please select a corpse " )
152
+ return selected_item . hist_figure_id , df . unit . find ( selected_item . unit_id )
160
153
end
161
154
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 ()
168
156
169
157
if not hist_figure_id then
170
158
qerror (" Cause of death not available" )
171
159
elseif hist_figure_id == - 1 then
172
160
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" )
177
162
end
178
-
179
163
displayDeathUnit (selected_unit )
180
164
else
181
165
displayDeathHistFig (df .historical_figure .find (hist_figure_id ))
0 commit comments