-
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
make deathcause
an api for getting the cause of death
#1434
Conversation
make it more api-like
Remove the console formatting Signed-off-by: Squid Coder <92821989+realSquidCoder@users.noreply.github.com>
Format to keep old usage working Signed-off-by: Squid Coder <92821989+realSquidCoder@users.noreply.github.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could you remove the leftover parens from the return
statements?
Also, needs a module declaration and standard side effects guard. See: https://docs.dfhack.org/en/latest/docs/dev/Lua%20API.html#importing-scripts
final fixes
localize some more functions and then rename the APIs and add comments
needs a changelog and documentation |
i have tested this PR locally and it doesn't cause any malfunction. i haven't confirmed that the added functionality works as expected, but at least it shouldn't introduce a regression. CI testing isn't working correctly for some reason i can't explain i will approve this once the API it introduces is documented |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I saw some minor things, nothing that really justifies delaying a merge.
One performance-related, one deliberately crashing if an event record wasn't found at all, one that requires a histfig's unit to be in world.units.all as opposed to being flushed to disk.
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a very big loop. >750000 entries in my current fort's world, year 515.
Since a histfig has a .died_year
field, you might be able to early-out with not-found ( nil
, probably?) when you reach an event that occurred in the previous year.
You might even be able to get really clever and use a binary search to narrow down to the proper year's range.
This assumes that world.history.events
is sorted by event date, which I believe but have not verified.
Speaking of that loop, this code implicitly returns nil
when no matching event is found. The caller doesn't check for that.
@@ -102,17 +104,18 @@ function getDeathEventForHistFig(histfig_id) | |||
end | |||
end | |||
|
|||
function displayDeathHistFig(histfig) | |||
-- Returns the cause of death given a histfig | |||
local function getDeathCauseFromHistFig(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 comment
The 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 qerror()
.
@@ -102,17 +104,18 @@ function getDeathEventForHistFig(histfig_id) | |||
end | |||
end | |||
|
|||
function displayDeathHistFig(histfig) | |||
-- Returns the cause of death given a histfig | |||
local 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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually I would suggest not finding the unit at all, and testing histfig.died_year == -1
instead of not dfhack.units.isDead()
.
You would want to fold in the string formatting that is in getDeathEventHistFigUnit()
.
dfhack.units.getReadableName()
accepts histfigs as well as units.
The only other use you have for a unit is to get the race, which is also in a histfig.
@SilasD while i think ur comments should be looked over, they cover parts of code that was pre-existing. As such, there should probably be a new PR that covers the issues of the pre-existing deathcause scripts. |
Made this on my phone, might have errors so please dbl check