11-- show death cause of a creature
2+ -- @ module = true
23
34local DEATH_TYPES = reqscript (' gui/unit-info-viewer' ).DEATH_TYPES
45
56-- Gets the first corpse item at the given location
6- function getItemAtPosition (pos )
7+ local function getItemAtPosition (pos )
78 for _ , item in ipairs (df .global .world .items .other .ANY_CORPSE ) do
9+ -- could this maybe be `if same_xyz(pos, item.pos) then`?
810 if item .pos .x == pos .x and item .pos .y == pos .y and item .pos .z == pos .z then
911 print (" Automatically chose first corpse at the selected location." )
1012 return item
1113 end
1214 end
1315end
1416
15- function getRaceNameSingular (race_id )
17+ local function getRaceNameSingular (race_id )
1618 return df .creature_raw .find (race_id ).name [0 ]
1719end
1820
19- function getDeathStringFromCause (cause )
21+ local function getDeathStringFromCause (cause )
2022 if cause == - 1 then
2123 return " died"
2224 else
2325 return DEATH_TYPES [cause ]:trim ()
2426 end
2527end
2628
27- function displayDeathUnit (unit )
29+ -- Returns a cause of death given a unit
30+ local function getDeathCauseFromUnit (unit )
2831 local str = unit .name .has_name and ' ' or ' The '
2932 str = str .. dfhack .units .getReadableName (unit )
3033
3134 if not dfhack .units .isDead (unit ) then
32- print (dfhack .df2console (str ) .. " is not dead yet!" )
33- return
35+ return str .. " is not dead yet!"
3436 end
3537
3638 str = str .. (" %s" ):format (getDeathStringFromCause (unit .counters .death_cause ))
@@ -50,20 +52,20 @@ function displayDeathUnit(unit)
5052 end
5153 end
5254
53- print ( dfhack . df2console ( str ) .. ' .' )
55+ return str .. ' .'
5456end
5557
5658-- returns the item description if the item still exists; otherwise
5759-- returns the weapon name
58- function getWeaponName (item_id , subtype )
60+ local function getWeaponName (item_id , subtype )
5961 local item = df .item .find (item_id )
6062 if not item then
6163 return df .global .world .raws .itemdefs .weapons [subtype ].name
6264 end
6365 return dfhack .items .getDescription (item , 0 , false )
6466end
6567
66- function displayDeathEventHistFigUnit (histfig_unit , event )
68+ local function getDeathEventHistFigUnit (histfig_unit , event )
6769 local str = (" The %s %s %s in year %d" ):format (
6870 getRaceNameSingular (histfig_unit .race ),
6971 dfhack .translation .translateName (dfhack .units .getVisibleName (histfig_unit )),
@@ -87,11 +89,11 @@ function displayDeathEventHistFigUnit(histfig_unit, event)
8789 end
8890 end
8991
90- print ( dfhack . df2console ( str ) .. ' .' )
92+ return str .. ' .'
9193end
9294
9395-- Returns the death event for the given histfig or nil if not found
94- function getDeathEventForHistFig (histfig_id )
96+ local function getDeathEventForHistFig (histfig_id )
9597 for i = # df .global .world .history .events - 1 , 0 , - 1 do
9698 local event = df .global .world .history .events [i ]
9799 if event :getType () == df .history_event_type .HIST_FIGURE_DIED then
@@ -102,17 +104,18 @@ function getDeathEventForHistFig(histfig_id)
102104 end
103105end
104106
105- function displayDeathHistFig (histfig )
107+ -- Returns the cause of death given a histfig
108+ local function getDeathCauseFromHistFig (histfig )
106109 local histfig_unit = df .unit .find (histfig .unit_id )
107110 if not histfig_unit then
108111 qerror (" Cause of death not available" )
109112 end
110113
111114 if not dfhack .units .isDead (histfig_unit ) then
112- print (( " %s is not dead yet!" ):format (dfhack .df2console ( dfhack . units .getReadableName (histfig_unit )) ))
115+ return ( " %s is not dead yet!" ):format (dfhack .units .getReadableName (histfig_unit ))
113116 else
114117 local death_event = getDeathEventForHistFig (histfig .id )
115- displayDeathEventHistFigUnit (histfig_unit , death_event )
118+ return getDeathEventHistFigUnit (histfig_unit , death_event )
116119 end
117120end
118121
@@ -147,6 +150,19 @@ local function get_target()
147150 return selected_item .hist_figure_id , df .unit .find (selected_item .unit_id )
148151end
149152
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+
162+ if dfhack_flags .module then
163+ return
164+ end
165+
150166local hist_figure_id , selected_unit = get_target ()
151167
152168if not hist_figure_id then
@@ -155,7 +171,7 @@ elseif hist_figure_id == -1 then
155171 if not selected_unit then
156172 qerror (" Cause of death not available" )
157173 end
158- displayDeathUnit ( selected_unit )
174+ print ( dfhack . df2console ( getDeathCause ( selected_unit )) )
159175else
160- displayDeathHistFig ( df .historical_figure .find (hist_figure_id ))
176+ print ( dfhack . df2console ( getDeathCause ( df .historical_figure .find (hist_figure_id )) ))
161177end
0 commit comments