Skip to content

Commit eff02eb

Browse files
authored
Merge pull request #1434 from SquidCoderIndustries/realSquidCoder-patch-1
make `deathcause` an api for getting the cause of death
2 parents a02935d + eb1cd89 commit eff02eb

File tree

3 files changed

+56
-16
lines changed

3 files changed

+56
-16
lines changed

changelog.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ Template for new versions:
3131
- `gui/autotraining`: configuration tool for autotraining
3232

3333
## New Features
34+
- `deathcause`: added functionality to this script to fetch cause of death programatically
3435

3536
## Fixes
3637

deathcause.lua

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,38 @@
11
-- show death cause of a creature
2+
--@ module = true
23

34
local 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
1315
end
1416

15-
function getRaceNameSingular(race_id)
17+
local function getRaceNameSingular(race_id)
1618
return df.creature_raw.find(race_id).name[0]
1719
end
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
2527
end
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 .. '.'
5456
end
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)
6466
end
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 .. '.'
9193
end
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
103105
end
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
117120
end
118121

@@ -147,6 +150,19 @@ local function get_target()
147150
return selected_item.hist_figure_id, df.unit.find(selected_item.unit_id)
148151
end
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+
150166
local hist_figure_id, selected_unit = get_target()
151167

152168
if 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)))
159175
else
160-
displayDeathHistFig(df.historical_figure.find(hist_figure_id))
176+
print(dfhack.df2console(getDeathCause(df.historical_figure.find(hist_figure_id))))
161177
end

docs/deathcause.rst

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,26 @@ Usage
1414
::
1515

1616
deathcause
17+
18+
API
19+
---
20+
21+
The ``deathcause`` script can be called programmatically by other scripts, either via the
22+
commandline interface with ``dfhack.run_script()`` or via the API functions
23+
defined in :source-scripts:`deathcause.lua`, available from the return value of
24+
``reqscript('deathcause')``:
25+
26+
* ``getDeathCause(unit or historical_figure)``
27+
28+
Returns a string with the unit or historical figure's cause of death. Note that using a historical
29+
figure will sometimes provide more information than using a unit.
30+
31+
32+
API usage example::
33+
34+
local dc = reqscript('deathcause')
35+
36+
-- Note: this is an arguably bad example because this is the same as running deathcause
37+
-- from the launcher, but this would theoretically still work.
38+
local deathReason = dc.getDeathCauseFromUnit(dfhack.gui.getSelectedUnit())
39+
print(deathReason)

0 commit comments

Comments
 (0)