Skip to content

Conversation

realSquidCoder
Copy link
Contributor

Made this on my phone, might have errors so please dbl check

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>
@realSquidCoder realSquidCoder marked this pull request as ready for review April 12, 2025 15:24
@myk002 myk002 added this to 51.11-r2 Apr 12, 2025
@github-project-automation github-project-automation bot moved this to Todo in 51.11-r2 Apr 12, 2025
Copy link
Member

@myk002 myk002 left a 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

@myk002 myk002 moved this from Todo to Review In Progress in 51.11-r2 Apr 13, 2025
localize some more functions and then rename the APIs and add comments
@myk002 myk002 removed this from 51.11-r2 Apr 25, 2025
@ab9rf
Copy link
Member

ab9rf commented Jun 27, 2025

needs a changelog and documentation

@ab9rf
Copy link
Member

ab9rf commented Jun 29, 2025

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

@ab9rf ab9rf merged commit eff02eb into DFHack:master Aug 17, 2025
10 checks passed
@realSquidCoder realSquidCoder deleted the realSquidCoder-patch-1 branch August 17, 2025 19:00
Copy link
Contributor

@SilasD SilasD left a 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
Copy link
Contributor

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")
Copy link
Contributor

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
Copy link
Contributor

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.

@realSquidCoder
Copy link
Contributor Author

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants