-
Notifications
You must be signed in to change notification settings - Fork 207
workorder-detail-fix: Enforce order item details for buggy job types #929
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
Open
flashy-man
wants to merge
38
commits into
DFHack:master
Choose a base branch
from
flashy-man:workorder-detail-fix
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
38 commits
Select commit
Hold shift + click to select a range
c1960e0
add workorder-detail-fix
flashy-man 0ac190c
better way to get the job type enums
flashy-man 152fe5b
print valid commands when no arg is passed
flashy-man 4e42bd8
print valid cmds when no arg is passed
flashy-man 323121d
fix bad commit
flashy-man 08a56b0
delete old job name -> id function
flashy-man b4b2518
workorder-detail-fix: use local variables
flashy-man 9635fab
fix trailing whitespace; doc underline
flashy-man 5c643b7
move workorder-detail-fix to fix directory, add enable API
flashy-man 69a5e1f
no arg parsing when being used as a module (for enable api)
flashy-man e52adbe
actually set enable state
flashy-man b30668f
trailing whitespace again
flashy-man fae0027
hopefully fix line endings (sublime text calls them 'unix' endings)
flashy-man 45f87de
hopefully fix EOF error (just needed newline at end of file)
flashy-man 47761b6
only print when called from command line. also fix to work better wit…
flashy-man e1f170e
move doc to docs/fix
flashy-man ea76f59
workorder-detail-fix -> fix/workorder-detail-fix
flashy-man 727200b
add detail_fix_is_needed() to say when the fix is required
flashy-man 39a4db4
rework: only enable handler when detail_fix_is_needed
flashy-man a4ce456
better status() function to account for more statuses
flashy-man 8ad4f77
woowee doc underline
flashy-man a5c2cc9
rename workorder-detail-fix in changelog
flashy-man 4e950dd
no more eventful; only repeatutil
flashy-man a39ec2e
merged changelog
flashy-man d6b0775
fix redundancy, confusing modulo math
flashy-man f6275f0
line endings
flashy-man 384625d
copy flags whole instead of one-by-one
flashy-man 3cff781
alphebetize entry in control panel registry
flashy-man b0146e7
update docs: new name/usage
flashy-man f504edc
fix problems with status()/jobs_corrected
flashy-man 027f410
fix redundant repeatutil usage
flashy-man b7c0ce8
print jobs_corrected only when enabled
flashy-man 4ca5a42
remove active job fix (unstable)
flashy-man f961c8f
use ipairs, utils.invert
flashy-man fa308ab
better way to get manager; move local declarations
flashy-man e584bba
automatic re-sync if lost
flashy-man fcc8546
documented active job limitation
flashy-man fea88ca
delimit block so goto can jump over local scope
flashy-man File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
local script_name = "workorder-detail-fix" | ||
local eventful = require 'plugins.eventful' | ||
if not handler_ref then local handler_ref = nil end | ||
|
||
local function get_job_id(match) | ||
for val, name in ipairs(df.job_type) do | ||
if name == match then return val end | ||
end | ||
end | ||
|
||
-- all jobs with the "any" (-1) type in its default job_items may be a problem | ||
local offending_jobs = { | ||
[get_job_id("EncrustWithGems")] = true, | ||
[get_job_id("EncrustWithGlass")] = true, | ||
[get_job_id("StudWith")] = true, | ||
[get_job_id("PrepareMeal")] = true, | ||
[get_job_id("DecorateWith")] = true, | ||
[get_job_id("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 | ||
|
||
args={...} | ||
|
||
cmd_table = { ["enable"]=enable, ["disable"]=disable, ["status"]=status } | ||
|
||
cmd = cmd_table[args[1]:lower()] | ||
if cmd then cmd() else | ||
print(script_name.." valid cmds: enable, disable, status") | ||
end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.