@@ -6,6 +6,7 @@ local DEATH_TYPES = reqscript('gui/unit-info-viewer').DEATH_TYPES
66-- Gets the first corpse item at the given location
77local function getItemAtPosition (pos )
88 for _ , item in ipairs (df .global .world .items .other .ANY_CORPSE ) do
9+ -- could this maybe be `if same_xyz(pos, item.pos) then`?
910 if item .pos .x == pos .x and item .pos .y == pos .y and item .pos .z == pos .z then
1011 print (" Automatically chose first corpse at the selected location." )
1112 return item
@@ -26,7 +27,7 @@ local function getDeathStringFromCause(cause)
2627end
2728
2829-- Returns a cause of death given a unit
29- function getDeathCauseFromUnit (unit )
30+ local function getDeathCauseFromUnit (unit )
3031 local str = unit .name .has_name and ' ' or ' The '
3132 str = str .. dfhack .units .getReadableName (unit )
3233
@@ -104,7 +105,7 @@ local function getDeathEventForHistFig(histfig_id)
104105end
105106
106107-- Returns the cause of death given a histfig
107- function getDeathCauseFromHistFig (histfig )
108+ local function getDeathCauseFromHistFig (histfig )
108109 local histfig_unit = df .unit .find (histfig .unit_id )
109110 if not histfig_unit then
110111 qerror (" Cause of death not available" )
@@ -149,6 +150,15 @@ local function get_target()
149150 return selected_item .hist_figure_id , df .unit .find (selected_item .unit_id )
150151end
151152
153+ -- wrapper function to take either a unit or a histfig and get the death cause
154+ function getDeathCause (target )
155+ if df .unit :is_instance (target ) then
156+ return getDeathCauseFromUnit (target )
157+ else
158+ return getDeathCauseFromHistFig (target )
159+ end
160+ end
161+
152162if dfhack_flags .module then
153163 return
154164end
@@ -161,7 +171,7 @@ elseif hist_figure_id == -1 then
161171 if not selected_unit then
162172 qerror (" Cause of death not available" )
163173 end
164- print (dfhack .df2console (getDeathCauseFromUnit (selected_unit )))
174+ print (dfhack .df2console (getDeathCause (selected_unit )))
165175else
166- print (dfhack .df2console (getDeathCauseFromHistFig (df .historical_figure .find (hist_figure_id ))))
176+ print (dfhack .df2console (getDeathCause (df .historical_figure .find (hist_figure_id ))))
167177end
0 commit comments