Skip to content
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

workorder-detail-fix: Enforce order item details for buggy job types #929

Open
wants to merge 38 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
c1960e0
add workorder-detail-fix
flashy-man Jan 6, 2024
0ac190c
better way to get the job type enums
flashy-man Jan 6, 2024
152fe5b
print valid commands when no arg is passed
flashy-man Jan 6, 2024
4e42bd8
print valid cmds when no arg is passed
flashy-man Jan 6, 2024
323121d
fix bad commit
flashy-man Jan 6, 2024
08a56b0
delete old job name -> id function
flashy-man Jan 6, 2024
b4b2518
workorder-detail-fix: use local variables
flashy-man Jan 6, 2024
9635fab
fix trailing whitespace; doc underline
flashy-man Jan 6, 2024
5c643b7
move workorder-detail-fix to fix directory, add enable API
flashy-man Jan 6, 2024
69a5e1f
no arg parsing when being used as a module (for enable api)
flashy-man Jan 6, 2024
e52adbe
actually set enable state
flashy-man Jan 6, 2024
b30668f
trailing whitespace again
flashy-man Jan 6, 2024
fae0027
hopefully fix line endings (sublime text calls them 'unix' endings)
flashy-man Jan 6, 2024
45f87de
hopefully fix EOF error (just needed newline at end of file)
flashy-man Jan 6, 2024
47761b6
only print when called from command line. also fix to work better wit…
flashy-man Jan 6, 2024
e1f170e
move doc to docs/fix
flashy-man Jan 7, 2024
ea76f59
workorder-detail-fix -> fix/workorder-detail-fix
flashy-man Jan 7, 2024
727200b
add detail_fix_is_needed() to say when the fix is required
flashy-man Jan 9, 2024
39a4db4
rework: only enable handler when detail_fix_is_needed
flashy-man Jan 9, 2024
a4ce456
better status() function to account for more statuses
flashy-man Jan 9, 2024
8ad4f77
woowee doc underline
flashy-man Jan 10, 2024
a5c2cc9
rename workorder-detail-fix in changelog
flashy-man Jan 14, 2024
4e950dd
no more eventful; only repeatutil
flashy-man Jan 14, 2024
a39ec2e
merged changelog
flashy-man Jan 14, 2024
d6b0775
fix redundancy, confusing modulo math
flashy-man Jan 15, 2024
f6275f0
line endings
flashy-man Jan 15, 2024
384625d
copy flags whole instead of one-by-one
flashy-man Jan 15, 2024
3cff781
alphebetize entry in control panel registry
flashy-man Jan 18, 2024
b0146e7
update docs: new name/usage
flashy-man Jan 18, 2024
f504edc
fix problems with status()/jobs_corrected
flashy-man Jan 18, 2024
027f410
fix redundant repeatutil usage
flashy-man Jan 18, 2024
b7c0ce8
print jobs_corrected only when enabled
flashy-man Jan 18, 2024
4ca5a42
remove active job fix (unstable)
flashy-man Jan 18, 2024
f961c8f
use ipairs, utils.invert
flashy-man Jan 18, 2024
fa308ab
better way to get manager; move local declarations
flashy-man Jan 18, 2024
e584bba
automatic re-sync if lost
flashy-man Jan 18, 2024
fcc8546
documented active job limitation
flashy-man Jan 18, 2024
fea88ca
delimit block so goto can jump over local scope
flashy-man Jan 18, 2024
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 @@ -40,6 +40,7 @@ Template for new versions:
- `warn-stranded`: Update onZoom to use df's centering functionality
- `ban-cooking`: fix banning creature alcohols resulting in error
- `confirm`: properly detect clicks on the remove zone button even when the unit selection screen is also open (e.g. the vanilla assign animal to pasture panel)
- `workorder-detail-fix`: fix item types not being passed properly on some modified work order jobs

## Misc Improvements
- `gui/control-panel`: reduce frequency for `warn-stranded` check to once every 2 days
Expand Down
24 changes: 24 additions & 0 deletions docs/workorder-detail-fix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
workorder-detail-fix
=========

.. dfhack-tool::
:summary: Fixes a bug with modified work orders creating incorrect jobs.
:tags: fort bugfix workorders

Some work order jobs have a bug when their input item details have been modified.

Example 1: a Stud With Iron order, modified to stud a cabinet, instead creates a job to stud any furniture.

Example 2: a Prepare Meal order, modified to use all plant type ingredients, instead creates a job to use any ingredients.

This fix forces these jobs to properly inherit the item details from their work order.

Usage
-----

``workorder-detail-fix enable``
enables the fix
``workorder-detail-fix disable``
disables the fix
``workorder-detail-fix status``
print fix status
113 changes: 113 additions & 0 deletions workorder-detail-fix.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
local script_name = "workorder-detail-fix"
local eventful = require 'plugins.eventful'
if not handler_ref then local handler_ref = nil end

-- all jobs with the "any" (-1) type in its default job_items may be a problem
local offending_jobs = {
[df.job_type.EncrustWithGems] = true,
[df.job_type.EncrustWithGlass] = true,
[df.job_type.StudWith] = true,
[df.job_type.PrepareMeal] = true,
[df.job_type.DecorateWith] = true,
[df.job_type.SewImage] = true,
-- list may be incomplete
}

-- copy order.item fields/flags over to job's job_item
-- only the essentials: stuff that is editable via gui/job-details
local function correct_item_details(job_item, order_item)
local fields = {'item_type', 'item_subtype', 'mat_type', 'mat_index'}
for _, field in pairs(fields) do
job_item[field] = order_item[field]
end

local flags_names = {'flags1', 'flags2', 'flags3', 'flags4', 'flags5'}
for _, flags in pairs(flags_names) do
local order_flags = order_item[flags]
if type(order_flags) == "number" then
job_item[flags] = order_flags
else -- copy over the flags one by one
for o_flag, val in pairs(order_flags) do
job_item[flags][o_flag] = val
end
end
end
end

-- correct each job as it's initialized
-- this is the handler, running after the job is dispatched
local function enforce_order_details(job)
if not job.job_items then return end -- never happens (error here?)
local order_id = job.order_id -- only jobs with an ORDER ID
if (order_id == -1) or (order_id == nil) then return end

-- only jobs with the item type issue. encrusting, sewing, cooking, etc.
if not offending_jobs[job.job_type] then return end

local order = nil -- get the order ref from order id
for _, ord in ipairs(df.global.world.manager_orders) do
if ord.id == order_id then order = ord; break end
end

if not order then return end -- oops, no order
if not order.items then return end -- no order item details to enforce

-- copy the item details over when the types don't match
for idx, job_item in ipairs(job.job_items) do
local order_item = order.items[idx]
if not order_item then break end -- never happens (error here?)
if job_item.item_type ~= order_item.item_type then
-- dfhack's isSuitableItem function will allow the orders we want,
-- but disallow insane combinations like meals made of shoes
local suitable = dfhack.job.isSuitableItem(
job_item, order_item.item_type, order_item.item_subtype )
if suitable then
correct_item_details(job_item, order_item)
else --[[ error on unsuitable item?]] end
end
end
end

local function enable()
print(script_name.." ENABLED")
-- set eventful onJobInitiated handler to run every tick (frequency 0)
eventful.enableEvent(eventful.eventType.JOB_INITIATED, 0)
eventful.onJobInitiated.workorder_detail_fix = enforce_order_details
handler_ref = eventful.onJobInitiated.workorder_detail_fix
end

local function disable()
print(script_name.." DISABLED")
eventful.onJobInitiated.workorder_detail_fix = nil
handler_ref = nil
end

local function status()
local status = "DISABLED"
local handler = eventful.onJobInitiated.workorder_detail_fix
if handler ~= nil then
-- ensure the handler still matches the one copied back from eventful
if handler == handler_ref then
status = "ENABLED"
else
status = "ERROR: Handler overwritten!"
print("why is this here:", handler)
print("should be", handler_ref)
end
end
print(script_name.." status: "..status)
end

local args={...}

if not args[1] then
print(script_name.." valid cmds: enable, disable, status")
return
end

local cmd_table = { ["enable"]=enable, ["disable"]=disable, ["status"]=status }

local cmd = cmd_table[args[1]:lower()]
if cmd then cmd() else
print(script_name.." valid cmds: enable, disable, status")
end
Loading