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
- `workorder`: refuse item-making orders that lack a required ``item_subtype`` instead of creating orders whose jobs crash DF when they are named

## Misc Improvements

Expand Down
15 changes: 15 additions & 0 deletions workorder.lua
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,21 @@ function create_orders(orders, quiet)
.. df.item_type[order.item_type] .. ":" .. it["item_subtype"] )
end

-- An item that has subtypes (shoes, armor, weapons, tools, ammo, toys, ...) needs one:
-- an order left at item_subtype -1 validates, but DF crashes when it names the resulting job.
-- Meals are the exception: they keep item_subtype -1 and store the ingredient count in mat_type.
if order.item_subtype == -1 then
local produced = order.item_type ~= df.item_type.NONE and order.item_type
or df.job_type.attrs[order.job_type].item
if produced and produced ~= df.item_type.NONE and produced ~= df.item_type.FOOD then
local def = df['itemdef_' .. string.lower(df.item_type[produced]) .. 'st']
if def and #def.get_vector() > 0 then
qerror(("Manager order %s needs an item_subtype (e.g. %s)"):format(
df.job_type[order.job_type], def.get_vector()[0].id))
end
end
Comment on lines +237 to +243

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 fully understand that Claude copied working code, but the code it copied isn't really correct.

It should use our API calls

dfhack.items.getSubtypeCount(item_type)
Returns the number of raw-defined subtypes of the given item type, or -1 if not applicable.

dfhack.items.getSubtypeDef(item_type, subtype)
Returns the raw definition for the given item type and subtype, or nil if invalid.

for example (minimal testing):

Suggested change
if produced and produced ~= df.item_type.NONE and produced ~= df.item_type.FOOD then
local def = df['itemdef_' .. string.lower(df.item_type[produced]) .. 'st']
if def and #def.get_vector() > 0 then
qerror(("Manager order %s needs an item_subtype (e.g. %s)"):format(
df.job_type[order.job_type], def.get_vector()[0].id))
end
end
if produced and produced ~= df.item_type.NONE and produced ~= df.item_type.FOOD
and dfhack.items.getSubtypeCount(produced) > -1
then
local example_def = dfhack.items.getSubtypeDef(produced, 0)
if example_def then
qerror(("Manager order %s needs an item_subtype (e.g. %s)"):format(
df.job_type[order.job_type], example_def.id))
end
end

end

if it["meal_ingredients"] then
order.mat_type = tonumber(it["meal_ingredients"])
or qerror ( "Invalid meal ingredients for managed order: " .. it["meal_ingredients"]
Expand Down