-
Notifications
You must be signed in to change notification settings - Fork 209
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
Open
realSquidCoder
wants to merge
11
commits into
DFHack:master
Choose a base branch
from
SquidCoderIndustries:realSquidCoder-patch-1
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
4cacf13
Update deathcause.lua
realSquidCoder 54f64da
Merge branch 'DFHack:master' into realSquidCoder-patch-1
realSquidCoder c4483b5
Update deathcause.lua
realSquidCoder 2fb14b6
Update deathcause.lua
realSquidCoder cf4af78
Update deathcause.lua
realSquidCoder add5d17
Update deathcause.lua
realSquidCoder d8203e4
Merge branch 'master' into realSquidCoder-patch-1
realSquidCoder b86f9f0
Update deathcause.lua
realSquidCoder b6fd3af
Merge remote-tracking branch 'upstream/master' into realSquidCoder-pa…
realSquidCoder e8267fc
Update changelog.txt
realSquidCoder 9f9ae70
add api docs
realSquidCoder File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
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. Not important, but this check can be |
||
print("Automatically chose first corpse at the selected location.") | ||
|
@@ -12,25 +13,25 @@ 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) | ||
-- Returns a cause of death given a unit | ||
function getDeathCauseFromUnit(unit) | ||
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,20 +51,20 @@ 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
|
||
-- returns the weapon name | ||
function getWeaponName(item_id, subtype) | ||
local function getWeaponName(item_id, subtype) | ||
local item = df.item.find(item_id) | ||
if not item then | ||
return df.global.world.raws.itemdefs.weapons[subtype].name | ||
end | ||
return dfhack.items.getDescription(item, 0, false) | ||
end | ||
|
||
function displayDeathEventHistFigUnit(histfig_unit, event) | ||
local 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,11 +88,11 @@ 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 | ||
function getDeathEventForHistFig(histfig_id) | ||
local function getDeathEventForHistFig(histfig_id) | ||
for i = #df.global.world.history.events - 1, 0, -1 do | ||
local event = df.global.world.history.events[i] | ||
if event:getType() == df.history_event_type.HIST_FIGURE_DIED then | ||
|
@@ -102,17 +103,18 @@ function getDeathEventForHistFig(histfig_id) | |
end | ||
end | ||
|
||
function displayDeathHistFig(histfig) | ||
-- Returns the cause of death given a histfig | ||
function getDeathCauseFromHistFig(histfig) | ||
local histfig_unit = df.unit.find(histfig.unit_id) | ||
if not histfig_unit then | ||
qerror("Cause of death not available") | ||
end | ||
|
||
if not dfhack.units.isDead(histfig_unit) then | ||
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 +149,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 +161,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(getDeathCauseFromUnit(selected_unit))) | ||
else | ||
displayDeathHistFig(df.historical_figure.find(hist_figure_id)) | ||
print(dfhack.df2console(getDeathCauseFromHistFig(df.historical_figure.find(hist_figure_id)))) | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.