1
1
-- show death cause of a creature
2
+ -- @ module = true
2
3
3
4
local DEATH_TYPES = reqscript (' gui/unit-info-viewer' ).DEATH_TYPES
4
5
5
6
-- Gets the first corpse item at the given location
6
- function getItemAtPosition (pos )
7
+ local function getItemAtPosition (pos )
7
8
for _ , item in ipairs (df .global .world .items .other .ANY_CORPSE ) do
9
+ -- could this maybe be `if same_xyz(pos, item.pos) then`?
8
10
if item .pos .x == pos .x and item .pos .y == pos .y and item .pos .z == pos .z then
9
11
print (" Automatically chose first corpse at the selected location." )
10
12
return item
11
13
end
12
14
end
13
15
end
14
16
15
- function getRaceNameSingular (race_id )
17
+ local function getRaceNameSingular (race_id )
16
18
return df .creature_raw .find (race_id ).name [0 ]
17
19
end
18
20
19
- function getDeathStringFromCause (cause )
21
+ local function getDeathStringFromCause (cause )
20
22
if cause == - 1 then
21
23
return " died"
22
24
else
23
25
return DEATH_TYPES [cause ]:trim ()
24
26
end
25
27
end
26
28
27
- function displayDeathUnit (unit )
29
+ -- Returns a cause of death given a unit
30
+ local function getDeathCauseFromUnit (unit )
28
31
local str = unit .name .has_name and ' ' or ' The '
29
32
str = str .. dfhack .units .getReadableName (unit )
30
33
31
34
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!"
34
36
end
35
37
36
38
str = str .. (" %s" ):format (getDeathStringFromCause (unit .counters .death_cause ))
@@ -50,20 +52,20 @@ function displayDeathUnit(unit)
50
52
end
51
53
end
52
54
53
- print ( dfhack . df2console ( str ) .. ' .' )
55
+ return str .. ' .'
54
56
end
55
57
56
58
-- returns the item description if the item still exists; otherwise
57
59
-- returns the weapon name
58
- function getWeaponName (item_id , subtype )
60
+ local function getWeaponName (item_id , subtype )
59
61
local item = df .item .find (item_id )
60
62
if not item then
61
63
return df .global .world .raws .itemdefs .weapons [subtype ].name
62
64
end
63
65
return dfhack .items .getDescription (item , 0 , false )
64
66
end
65
67
66
- function displayDeathEventHistFigUnit (histfig_unit , event )
68
+ local function getDeathEventHistFigUnit (histfig_unit , event )
67
69
local str = (" The %s %s %s in year %d" ):format (
68
70
getRaceNameSingular (histfig_unit .race ),
69
71
dfhack .translation .translateName (dfhack .units .getVisibleName (histfig_unit )),
@@ -87,11 +89,11 @@ function displayDeathEventHistFigUnit(histfig_unit, event)
87
89
end
88
90
end
89
91
90
- print ( dfhack . df2console ( str ) .. ' .' )
92
+ return str .. ' .'
91
93
end
92
94
93
95
-- Returns the death event for the given histfig or nil if not found
94
- function getDeathEventForHistFig (histfig_id )
96
+ local function getDeathEventForHistFig (histfig_id )
95
97
for i = # df .global .world .history .events - 1 , 0 , - 1 do
96
98
local event = df .global .world .history .events [i ]
97
99
if event :getType () == df .history_event_type .HIST_FIGURE_DIED then
@@ -102,17 +104,18 @@ function getDeathEventForHistFig(histfig_id)
102
104
end
103
105
end
104
106
105
- function displayDeathHistFig (histfig )
107
+ -- Returns the cause of death given a histfig
108
+ local function getDeathCauseFromHistFig (histfig )
106
109
local histfig_unit = df .unit .find (histfig .unit_id )
107
110
if not histfig_unit then
108
111
qerror (" Cause of death not available" )
109
112
end
110
113
111
114
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 ))
113
116
else
114
117
local death_event = getDeathEventForHistFig (histfig .id )
115
- displayDeathEventHistFigUnit (histfig_unit , death_event )
118
+ return getDeathEventHistFigUnit (histfig_unit , death_event )
116
119
end
117
120
end
118
121
@@ -147,6 +150,19 @@ local function get_target()
147
150
return selected_item .hist_figure_id , df .unit .find (selected_item .unit_id )
148
151
end
149
152
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
+
150
166
local hist_figure_id , selected_unit = get_target ()
151
167
152
168
if not hist_figure_id then
@@ -155,7 +171,7 @@ elseif hist_figure_id == -1 then
155
171
if not selected_unit then
156
172
qerror (" Cause of death not available" )
157
173
end
158
- displayDeathUnit ( selected_unit )
174
+ print ( dfhack . df2console ( getDeathCause ( selected_unit )) )
159
175
else
160
- displayDeathHistFig ( df .historical_figure .find (hist_figure_id ))
176
+ print ( dfhack . df2console ( getDeathCause ( df .historical_figure .find (hist_figure_id )) ))
161
177
end
0 commit comments