Skip to content

Commit af18805

Browse files
Merge branch 'realSquidCoder-patch-1' into SquidHack
2 parents 3350ad2 + eb1cd89 commit af18805

File tree

3 files changed

+38
-4
lines changed

3 files changed

+38
-4
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: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ local DEATH_TYPES = reqscript('gui/unit-info-viewer').DEATH_TYPES
66
-- Gets the first corpse item at the given location
77
local 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)
2627
end
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)
104105
end
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)
150151
end
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+
152162
if dfhack_flags.module then
153163
return
154164
end
@@ -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)))
165175
else
166-
print(dfhack.df2console(getDeathCauseFromHistFig(df.historical_figure.find(hist_figure_id))))
176+
print(dfhack.df2console(getDeathCause(df.historical_figure.find(hist_figure_id))))
167177
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)