Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ Template for new versions:
## New Features

## Fixes
- `caravan`: fix UI freeze in the ``Move goods to/from depot`` dialog when marking or unmarking large item groups; item lists are now built incrementally so the UI stays responsive, and the item scan itself is faster

## Misc Improvements

Expand Down
33 changes: 19 additions & 14 deletions internal/caravan/common.lua
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,16 @@ function obfuscate_value(value, threshold)
return ('%s?'):format(estimate(threshold*30, 999, 1000))
end

-- takes into account trade agreements
function get_perceived_value(item, caravan_state)
local EMPTY_CONTAINED = {}

Copy link
Copy Markdown
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 having this constant. it made me wonder, is something being maintained in this between yields? is this something that needs to be cleared out when the calling script exits?

I think you should zap it and just use a {} empty-table constructor in the two places it's used.

Edit: wait, hold the phone, it's completely unneccesary. discussion below. zap it.

Suggested change
local EMPTY_CONTAINED = {}


-- takes into account trade agreements. contained is an optional precomputed
-- contents list so callers that scan many items can look it up once per item.
function get_perceived_value(item, caravan_state, contained)
local value = dfhack.items.getValue(item, caravan_state)
for _,contained_item in ipairs(dfhack.items.getContainedItems(item)) do
if not contained then
contained = item.flags.container and dfhack.items.getContainedItems(item) or EMPTY_CONTAINED

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it turns out that getContainedItems does the right thing if the item being probed isn't a container; it returns an empty list instead of throwing or otherwise misfunctioning. I think it's just as fast to call it as to test a flag bit and choose between two options.
The only reason I see to continue using the constant is to have a pre-constructed empty table to iterate over instead of constructing and abandoning one on each call. And I don't think that's worth it, especially with the new code to dodge the whole issue with the passed-in contents list.

Suggested change
contained = item.flags.container and dfhack.items.getContainedItems(item) or EMPTY_CONTAINED
contained = dfhack.items.getContainedItems(item)

end
for _,contained_item in ipairs(contained) do
value = value + dfhack.items.getValue(contained_item, caravan_state)
for _,contained_contained_item in ipairs(dfhack.items.getContainedItems(contained_item)) do
value = value + dfhack.items.getValue(contained_contained_item, caravan_state)
Expand Down Expand Up @@ -611,21 +617,20 @@ function get_info_widgets(self, export_agreements, strict_ethical_bins_default,
end

local function match_risky(item, risky_items)
for item_type, subtypes in pairs(risky_items) do
for subtype in pairs(subtypes) do
if item_type == item:getType() and (subtype == -1 or subtype == item:getSubtype()) then
return true
end
end
end
return false
local subtypes = risky_items[item:getType()]
if not subtypes then return false end
return subtypes[-1] ~= nil or subtypes[item:getSubtype()] ~= nil
Comment on lines +620 to +622

@SilasD SilasD Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I puzzled over this for a bit before I understood that the old code really was walking a hash table instead of going directly to the element it's trying to check. odd. good catch.

I'm not sure you need the explicit tests for ~= nil; these values should be true or at least truthy, I would think. but ISTR Lua does 'is this nil' in one VM instruction, and 'is this truthy' in one VM instruction, so it's a wash.

end

-- returns is_banned, is_risky
function scan_banned(item, risky_items)
-- returns is_banned, is_risky. contained is an optional precomputed contents
-- list so callers that scan many items can look it up once per item.
function scan_banned(item, risky_items, contained)
if not dfhack.items.checkMandates(item) then return true, true end
if match_risky(item, risky_items) then return false, true end
for _,contained_item in ipairs(dfhack.items.getContainedItems(item)) do
if not contained then
contained = item.flags.container and dfhack.items.getContainedItems(item) or EMPTY_CONTAINED

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
contained = item.flags.container and dfhack.items.getContainedItems(item) or EMPTY_CONTAINED
contained = dfhack.items.getContainedItems(item)

end
for _,contained_item in ipairs(contained) do
if not dfhack.items.checkMandates(contained_item) then return true, true end
if match_risky(contained_item, risky_items) then return false, true end
end
Expand Down
Loading
Loading