-
Notifications
You must be signed in to change notification settings - Fork 217
make deathcause
an api for getting the cause of death
#1434
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 7 commits
4cacf13
54f64da
c4483b5
2fb14b6
cf4af78
add5d17
d8203e4
b86f9f0
b6fd3af
e8267fc
9f9ae70
0895834
eb1cd89
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,10 @@ | ||
-- show death cause of a creature | ||
--@ module = true | ||
|
||
local DEATH_TYPES = reqscript('gui/unit-info-viewer').DEATH_TYPES | ||
|
||
-- Gets the first corpse item at the given location | ||
function getItemAtPosition(pos) | ||
local function getItemAtPosition(pos) | ||
for _, item in ipairs(df.global.world.items.other.ANY_CORPSE) do | ||
if item.pos.x == pos.x and item.pos.y == pos.y and item.pos.z == pos.z then | ||
realSquidCoder marked this conversation as resolved.
Show resolved
Hide resolved
|
||
print("Automatically chose first corpse at the selected location.") | ||
|
@@ -12,25 +13,24 @@ function getItemAtPosition(pos) | |
end | ||
end | ||
|
||
function getRaceNameSingular(race_id) | ||
local function getRaceNameSingular(race_id) | ||
return df.creature_raw.find(race_id).name[0] | ||
end | ||
|
||
function getDeathStringFromCause(cause) | ||
local function getDeathStringFromCause(cause) | ||
if cause == -1 then | ||
return "died" | ||
else | ||
return DEATH_TYPES[cause]:trim() | ||
end | ||
end | ||
|
||
function displayDeathUnit(unit) | ||
function getDeathUnit(unit) | ||
realSquidCoder marked this conversation as resolved.
Show resolved
Hide resolved
|
||
local str = unit.name.has_name and '' or 'The ' | ||
str = str .. dfhack.units.getReadableName(unit) | ||
|
||
if not dfhack.units.isDead(unit) then | ||
print(dfhack.df2console(str) .. " is not dead yet!") | ||
return | ||
return str .. " is not dead yet!" | ||
realSquidCoder marked this conversation as resolved.
Show resolved
Hide resolved
|
||
end | ||
|
||
str = str .. (" %s"):format(getDeathStringFromCause(unit.counters.death_cause)) | ||
|
@@ -50,7 +50,7 @@ function displayDeathUnit(unit) | |
end | ||
end | ||
|
||
print(dfhack.df2console(str) .. '.') | ||
return str .. '.' | ||
end | ||
|
||
-- returns the item description if the item still exists; otherwise | ||
realSquidCoder marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
@@ -63,7 +63,7 @@ function getWeaponName(item_id, subtype) | |
return dfhack.items.getDescription(item, 0, false) | ||
end | ||
|
||
function displayDeathEventHistFigUnit(histfig_unit, event) | ||
function getDeathEventHistFigUnit(histfig_unit, event) | ||
local str = ("The %s %s %s in year %d"):format( | ||
getRaceNameSingular(histfig_unit.race), | ||
dfhack.translation.translateName(dfhack.units.getVisibleName(histfig_unit)), | ||
|
@@ -87,7 +87,7 @@ function displayDeathEventHistFigUnit(histfig_unit, event) | |
end | ||
end | ||
|
||
print(dfhack.df2console(str) .. '.') | ||
return str .. '.' | ||
end | ||
|
||
-- Returns the death event for the given histfig or nil if not found | ||
|
@@ -102,17 +102,17 @@ function getDeathEventForHistFig(histfig_id) | |
end | ||
end | ||
|
||
function displayDeathHistFig(histfig) | ||
function getDeathHistFig(histfig) | ||
local histfig_unit = df.unit.find(histfig.unit_id) | ||
if not histfig_unit then | ||
qerror("Cause of death not available") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't like that an API function can deliberately crash. Consider returning this string instead of calling |
||
end | ||
|
||
if not dfhack.units.isDead(histfig_unit) then | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually I would suggest not finding the unit at all, and testing You would want to fold in the string formatting that is in
The only other use you have for a unit is to get the race, which is also in a histfig. |
||
print(("%s is not dead yet!"):format(dfhack.df2console(dfhack.units.getReadableName(histfig_unit)))) | ||
return ("%s is not dead yet!"):format(dfhack.units.getReadableName(histfig_unit)) | ||
else | ||
local death_event = getDeathEventForHistFig(histfig.id) | ||
displayDeathEventHistFigUnit(histfig_unit, death_event) | ||
return getDeathEventHistFigUnit(histfig_unit, death_event) | ||
end | ||
end | ||
|
||
|
@@ -147,6 +147,10 @@ local function get_target() | |
return selected_item.hist_figure_id, df.unit.find(selected_item.unit_id) | ||
end | ||
|
||
if dfhack_flags.module then | ||
return | ||
end | ||
|
||
local hist_figure_id, selected_unit = get_target() | ||
|
||
if not hist_figure_id then | ||
|
@@ -155,7 +159,7 @@ elseif hist_figure_id == -1 then | |
if not selected_unit then | ||
qerror("Cause of death not available") | ||
end | ||
displayDeathUnit(selected_unit) | ||
print(dfhack.df2console(getDeathUnit(selected_unit))) | ||
else | ||
displayDeathHistFig(df.historical_figure.find(hist_figure_id)) | ||
print(dfhack.df2console(getDeathHistFig(df.historical_figure.find(hist_figure_id)))) | ||
end |
Uh oh!
There was an error while loading. Please reload this page.