2012-10-23 11:42:03 -06:00
|
|
|
-- A GUI front-end for the workflow plugin.
|
|
|
|
|
|
|
|
local utils = require 'utils'
|
|
|
|
local gui = require 'gui'
|
|
|
|
local guidm = require 'gui.dwarfmode'
|
|
|
|
local guimat = require 'gui.materials'
|
|
|
|
local widgets = require 'gui.widgets'
|
|
|
|
local dlg = require 'gui.dialogs'
|
|
|
|
|
|
|
|
local workflow = require 'plugins.workflow'
|
|
|
|
|
2012-10-26 01:52:41 -06:00
|
|
|
function check_enabled(cb)
|
2012-10-23 11:42:03 -06:00
|
|
|
if workflow.isEnabled() then
|
2012-10-26 01:52:41 -06:00
|
|
|
return cb()
|
2012-10-23 11:42:03 -06:00
|
|
|
else
|
|
|
|
dlg.showYesNoPrompt(
|
|
|
|
'Enable Plugin',
|
2012-10-26 01:52:41 -06:00
|
|
|
{ 'The workflow plugin is not enabled currently.', NEWLINE, NEWLINE,
|
2012-10-23 11:42:03 -06:00
|
|
|
'Press ', { key = 'MENU_CONFIRM' }, ' to enable it.' },
|
|
|
|
COLOR_YELLOW,
|
2012-10-26 01:52:41 -06:00
|
|
|
function()
|
2012-10-23 11:42:03 -06:00
|
|
|
workflow.setEnabled(true)
|
2012-10-26 01:52:41 -06:00
|
|
|
return cb()
|
|
|
|
end
|
2012-10-23 11:42:03 -06:00
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-10-25 02:15:18 -06:00
|
|
|
function check_repeat(job, cb)
|
|
|
|
if job.flags['repeat'] then
|
2012-10-26 01:52:41 -06:00
|
|
|
return cb()
|
2012-10-25 02:15:18 -06:00
|
|
|
else
|
|
|
|
dlg.showYesNoPrompt(
|
|
|
|
'Not Repeat Job',
|
|
|
|
{ 'Workflow only tracks repeating jobs.', NEWLINE, NEWLINE,
|
|
|
|
'Press ', { key = 'MENU_CONFIRM' }, ' to make this one repeat.' },
|
|
|
|
COLOR_YELLOW,
|
|
|
|
function()
|
|
|
|
job.flags['repeat'] = true
|
2012-10-26 01:52:41 -06:00
|
|
|
return cb()
|
2012-10-25 02:15:18 -06:00
|
|
|
end
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-10-23 11:42:03 -06:00
|
|
|
JobConstraints = defclass(JobConstraints, guidm.MenuOverlay)
|
|
|
|
|
|
|
|
JobConstraints.focus_path = 'workflow-job'
|
|
|
|
|
|
|
|
JobConstraints.ATTRS {
|
|
|
|
job = DEFAULT_NIL,
|
|
|
|
frame_inset = 1,
|
|
|
|
frame_background = COLOR_BLACK,
|
|
|
|
}
|
|
|
|
|
2012-10-24 09:25:06 -06:00
|
|
|
local null_cons = { goal_value = 0, goal_gap = 0, goal_by_count = false }
|
|
|
|
|
2012-10-23 11:42:03 -06:00
|
|
|
function JobConstraints:init(args)
|
|
|
|
self.building = dfhack.job.getHolder(self.job)
|
|
|
|
|
|
|
|
self:addviews{
|
|
|
|
widgets.Label{
|
|
|
|
frame = { l = 0, t = 0 },
|
|
|
|
text = {
|
|
|
|
'Workflow Constraints'
|
|
|
|
}
|
|
|
|
},
|
|
|
|
widgets.List{
|
|
|
|
view_id = 'list',
|
2012-10-24 09:25:06 -06:00
|
|
|
frame = { t = 2, b = 6 },
|
2012-10-23 11:42:03 -06:00
|
|
|
row_height = 4,
|
|
|
|
scroll_keys = widgets.SECONDSCROLL,
|
|
|
|
},
|
2012-10-24 09:25:06 -06:00
|
|
|
widgets.Label{
|
|
|
|
frame = { l = 0, b = 3 },
|
|
|
|
enabled = self:callback('isAnySelected'),
|
|
|
|
text = {
|
|
|
|
{ key = 'BUILDING_TRIGGER_ENABLE_CREATURE',
|
|
|
|
text = function()
|
|
|
|
local cons = self:getCurConstraint() or null_cons
|
|
|
|
if cons.goal_by_count then
|
|
|
|
return ': Count stacks '
|
|
|
|
else
|
|
|
|
return ': Count items '
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
on_activate = self:callback('onChangeUnit') },
|
|
|
|
{ key = 'BUILDING_TRIGGER_ENABLE_MAGMA', text = ': Modify',
|
|
|
|
on_activate = self:callback('onEditRange') },
|
|
|
|
NEWLINE, ' ',
|
|
|
|
{ key = 'BUILDING_TRIGGER_MIN_SIZE_DOWN',
|
|
|
|
on_activate = self:callback('onIncRange', 'goal_gap', 5) },
|
|
|
|
{ key = 'BUILDING_TRIGGER_MIN_SIZE_UP',
|
|
|
|
on_activate = self:callback('onIncRange', 'goal_gap', -1) },
|
|
|
|
{ text = function()
|
|
|
|
local cons = self:getCurConstraint() or null_cons
|
|
|
|
return string.format(': Min %-4d ', cons.goal_value - cons.goal_gap)
|
|
|
|
end },
|
|
|
|
{ key = 'BUILDING_TRIGGER_MAX_SIZE_DOWN',
|
|
|
|
on_activate = self:callback('onIncRange', 'goal_value', -1) },
|
|
|
|
{ key = 'BUILDING_TRIGGER_MAX_SIZE_UP',
|
|
|
|
on_activate = self:callback('onIncRange', 'goal_value', 5) },
|
|
|
|
{ text = function()
|
|
|
|
local cons = self:getCurConstraint() or null_cons
|
|
|
|
return string.format(': Max %-4d', cons.goal_value)
|
|
|
|
end },
|
|
|
|
}
|
|
|
|
},
|
2012-10-23 11:42:03 -06:00
|
|
|
widgets.Label{
|
|
|
|
frame = { l = 0, b = 0 },
|
|
|
|
text = {
|
2012-10-24 09:25:06 -06:00
|
|
|
{ key = 'CUSTOM_N', text = ': New limit, ',
|
|
|
|
on_activate = self:callback('onNewConstraint') },
|
|
|
|
{ key = 'CUSTOM_X', text = ': Delete',
|
|
|
|
enabled = self:callback('isAnySelected'),
|
|
|
|
on_activate = self:callback('onDeleteConstraint') },
|
|
|
|
NEWLINE, NEWLINE,
|
2012-10-23 11:42:03 -06:00
|
|
|
{ key = 'LEAVESCREEN', text = ': Back',
|
|
|
|
on_activate = self:callback('dismiss') }
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
self:initListChoices(args.clist)
|
|
|
|
end
|
|
|
|
|
|
|
|
function JobConstraints:onGetSelectedBuilding()
|
|
|
|
return self.building
|
|
|
|
end
|
|
|
|
|
|
|
|
function JobConstraints:onGetSelectedJob()
|
|
|
|
return self.job
|
|
|
|
end
|
|
|
|
|
|
|
|
function describe_item_type(iobj)
|
|
|
|
local itemline = 'any item'
|
2012-10-24 11:49:30 -06:00
|
|
|
if iobj.is_craft then
|
|
|
|
itemline = 'any craft'
|
|
|
|
elseif iobj.item_type >= 0 then
|
2012-10-23 11:42:03 -06:00
|
|
|
itemline = df.item_type.attrs[iobj.item_type].caption or iobj.item_type
|
2012-10-24 11:49:30 -06:00
|
|
|
local subtype = iobj.item_subtype or -1
|
|
|
|
local def = dfhack.items.getSubtypeDef(iobj.item_type, subtype)
|
|
|
|
local count = dfhack.items.getSubtypeCount(iobj.item_type, subtype)
|
2012-10-23 11:42:03 -06:00
|
|
|
if def then
|
|
|
|
itemline = def.name
|
|
|
|
elseif count >= 0 then
|
|
|
|
itemline = 'any '..itemline
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return itemline
|
|
|
|
end
|
|
|
|
|
|
|
|
function is_caste_mat(iobj)
|
2012-10-24 11:49:30 -06:00
|
|
|
return dfhack.items.isCasteMaterial(iobj.item_type or -1)
|
2012-10-23 11:42:03 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
function describe_material(iobj)
|
|
|
|
local matline = 'any material'
|
|
|
|
if is_caste_mat(iobj) then
|
2012-10-24 11:49:30 -06:00
|
|
|
matline = 'no material'
|
|
|
|
elseif (iobj.mat_type or -1) >= 0 then
|
2012-10-23 11:42:03 -06:00
|
|
|
local info = dfhack.matinfo.decode(iobj.mat_type, iobj.mat_index)
|
|
|
|
if info then
|
|
|
|
matline = info:toString()
|
|
|
|
else
|
|
|
|
matline = iobj.mat_type..':'..iobj.mat_index
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return matline
|
|
|
|
end
|
|
|
|
|
2012-10-25 02:15:18 -06:00
|
|
|
function JobConstraints:initListChoices(clist, sel_token)
|
2012-10-23 11:42:03 -06:00
|
|
|
clist = clist or workflow.listConstraints(self.job)
|
|
|
|
|
|
|
|
local choices = {}
|
|
|
|
|
|
|
|
for i,cons in ipairs(clist) do
|
|
|
|
local goal = (cons.goal_value-cons.goal_gap)..'-'..cons.goal_value
|
|
|
|
local curval
|
|
|
|
if cons.goal_by_count then
|
|
|
|
goal = goal .. ' stacks'
|
|
|
|
curval = cons.cur_count
|
|
|
|
else
|
|
|
|
goal = goal .. ' items'
|
|
|
|
curval = cons.cur_amount
|
|
|
|
end
|
|
|
|
local order_pen = COLOR_GREY
|
|
|
|
if cons.request == 'resume' then
|
|
|
|
order_pen = COLOR_GREEN
|
|
|
|
elseif cons.request == 'suspend' then
|
2012-10-26 01:52:41 -06:00
|
|
|
order_pen = COLOR_BLUE
|
2012-10-23 11:42:03 -06:00
|
|
|
end
|
2012-10-24 11:49:30 -06:00
|
|
|
local itemstr = describe_item_type(cons)
|
2012-10-23 11:42:03 -06:00
|
|
|
if cons.min_quality > 0 then
|
|
|
|
itemstr = itemstr .. ' ('..df.item_quality[cons.min_quality]..')'
|
|
|
|
end
|
|
|
|
local matstr = describe_material(cons)
|
|
|
|
local matflagstr = ''
|
2012-10-25 02:15:18 -06:00
|
|
|
local matflags = utils.list_bitfield_flags(cons.mat_mask)
|
2012-10-23 11:42:03 -06:00
|
|
|
if #matflags > 0 then
|
2012-10-24 09:25:06 -06:00
|
|
|
matflags[1] = 'any '..matflags[1]
|
|
|
|
if matstr == 'any material' then
|
|
|
|
matstr = table.concat(matflags, ', ')
|
|
|
|
matflags = {}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if #matflags > 0 then
|
|
|
|
matflagstr = table.concat(matflags, ', ')
|
2012-10-23 11:42:03 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
table.insert(choices, {
|
|
|
|
text = {
|
|
|
|
goal, ' ', { text = '(now '..curval..')', pen = order_pen }, NEWLINE,
|
|
|
|
' ', itemstr, NEWLINE, ' ', matstr, NEWLINE, ' ', matflagstr
|
|
|
|
},
|
2012-10-25 02:15:18 -06:00
|
|
|
token = cons.token,
|
2012-10-23 11:42:03 -06:00
|
|
|
obj = cons
|
|
|
|
})
|
|
|
|
end
|
|
|
|
|
2012-10-25 02:15:18 -06:00
|
|
|
local selidx = nil
|
|
|
|
if sel_token then
|
|
|
|
selidx = utils.linear_index(choices, sel_token, 'token')
|
|
|
|
end
|
|
|
|
|
|
|
|
self.subviews.list:setChoices(choices, selidx)
|
2012-10-23 11:42:03 -06:00
|
|
|
end
|
|
|
|
|
2012-10-24 09:25:06 -06:00
|
|
|
function JobConstraints:isAnySelected()
|
|
|
|
return self.subviews.list:getSelected() ~= nil
|
|
|
|
end
|
|
|
|
|
|
|
|
function JobConstraints:getCurConstraint()
|
|
|
|
local i,v = self.subviews.list:getSelected()
|
|
|
|
if v then return v.obj end
|
|
|
|
end
|
|
|
|
|
|
|
|
function JobConstraints:saveConstraint(cons)
|
2012-10-25 02:15:18 -06:00
|
|
|
local out = workflow.setConstraint(cons.token, cons.goal_by_count, cons.goal_value, cons.goal_gap)
|
|
|
|
self:initListChoices(nil, out.token)
|
2012-10-24 09:25:06 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
function JobConstraints:onChangeUnit()
|
|
|
|
local cons = self:getCurConstraint()
|
|
|
|
cons.goal_by_count = not cons.goal_by_count
|
|
|
|
self:saveConstraint(cons)
|
|
|
|
end
|
|
|
|
|
|
|
|
function JobConstraints:onEditRange()
|
|
|
|
local cons = self:getCurConstraint()
|
|
|
|
dlg.showInputPrompt(
|
|
|
|
'Input Range',
|
|
|
|
'Enter the new constraint range:',
|
|
|
|
COLOR_WHITE,
|
|
|
|
(cons.goal_value-cons.goal_gap)..'-'..cons.goal_value,
|
|
|
|
function(text)
|
|
|
|
local maxv = string.match(text, '^%s*(%d+)%s*$')
|
|
|
|
if maxv then
|
|
|
|
cons.goal_value = maxv
|
|
|
|
return self:saveConstraint(cons)
|
|
|
|
end
|
|
|
|
local minv,maxv = string.match(text, '^%s*(%d+)-(%d+)%s*$')
|
|
|
|
if minv and maxv and minv ~= maxv then
|
|
|
|
cons.goal_value = math.max(minv,maxv)
|
|
|
|
cons.goal_gap = math.abs(maxv-minv)
|
|
|
|
return self:saveConstraint(cons)
|
|
|
|
end
|
|
|
|
dlg.showMessage('Invalid Range', 'This range is invalid: '..text, COLOR_LIGHTRED)
|
|
|
|
end
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
function JobConstraints:onIncRange(field, delta)
|
|
|
|
local cons = self:getCurConstraint()
|
|
|
|
if not cons.goal_by_count then
|
|
|
|
delta = delta * 5
|
|
|
|
end
|
|
|
|
cons[field] = math.max(1, cons[field] + delta)
|
|
|
|
self:saveConstraint(cons)
|
|
|
|
end
|
|
|
|
|
|
|
|
function JobConstraints:onNewConstraint()
|
2012-10-24 11:49:30 -06:00
|
|
|
local outputs = workflow.listJobOutputs(self.job)
|
|
|
|
if #outputs == 0 then
|
|
|
|
dlg.showMessage('Unsupported', 'Workflow cannot guess the outputs of this job.', COLOR_LIGHTRED)
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2012-10-25 02:15:18 -06:00
|
|
|
local variants = workflow.listWeakenedConstraints(outputs)
|
2012-10-24 11:49:30 -06:00
|
|
|
|
|
|
|
local choices = {}
|
|
|
|
for i,cons in ipairs(variants) do
|
|
|
|
local itemstr = describe_item_type(cons)
|
|
|
|
local matstr = describe_material(cons)
|
2012-10-25 02:15:18 -06:00
|
|
|
local matflags = utils.list_bitfield_flags(cons.mat_mask)
|
2012-10-24 11:49:30 -06:00
|
|
|
if #matflags > 0 then
|
|
|
|
local fstr = table.concat(matflags, '/')
|
|
|
|
if matstr == 'any material' then
|
|
|
|
matstr = 'any '..fstr
|
|
|
|
else
|
|
|
|
matstr = 'any '..fstr..' '..matstr
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
table.insert(choices, { text = itemstr..' of '..matstr, obj = cons })
|
|
|
|
end
|
|
|
|
|
|
|
|
dlg.showListPrompt(
|
2012-11-10 06:06:54 -07:00
|
|
|
'New limit',
|
2012-10-25 02:15:18 -06:00
|
|
|
'Select one of the possible outputs:',
|
2012-10-24 11:49:30 -06:00
|
|
|
COLOR_WHITE,
|
|
|
|
choices,
|
|
|
|
function(idx,item)
|
|
|
|
self:saveConstraint(item.obj)
|
|
|
|
end
|
|
|
|
)
|
2012-10-24 09:25:06 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
function JobConstraints:onDeleteConstraint()
|
|
|
|
local cons = self:getCurConstraint()
|
|
|
|
dlg.showYesNoPrompt(
|
|
|
|
'Delete Constraint',
|
|
|
|
'Really delete the current constraint?',
|
|
|
|
COLOR_YELLOW,
|
|
|
|
function()
|
|
|
|
workflow.deleteConstraint(cons.token)
|
|
|
|
self:initListChoices()
|
|
|
|
end
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2012-10-23 11:42:03 -06:00
|
|
|
function JobConstraints:onInput(keys)
|
|
|
|
if self:propagateMoveKeys(keys) then
|
|
|
|
if df.global.world.selected_building ~= self.building then
|
|
|
|
self:dismiss()
|
|
|
|
end
|
|
|
|
else
|
|
|
|
JobConstraints.super.onInput(self, keys)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if not string.match(dfhack.gui.getCurFocus(), '^dwarfmode/QueryBuilding/Some/Workshop/Job') then
|
|
|
|
qerror("This script requires a workshop job selected in the 'q' mode")
|
|
|
|
end
|
|
|
|
|
2012-10-26 01:52:41 -06:00
|
|
|
local job = dfhack.gui.getSelectedJob()
|
|
|
|
|
2012-10-23 11:42:03 -06:00
|
|
|
check_enabled(function()
|
2012-10-26 01:52:41 -06:00
|
|
|
check_repeat(job, function()
|
2012-10-25 02:15:18 -06:00
|
|
|
local clist = workflow.listConstraints(job)
|
|
|
|
if not clist then
|
|
|
|
dlg.showMessage('Not Supported', 'This type of job is not supported by workflow.', COLOR_LIGHTRED)
|
|
|
|
return
|
|
|
|
end
|
|
|
|
JobConstraints{ job = job, clist = clist }:show()
|
|
|
|
end)
|
2012-10-23 11:42:03 -06:00
|
|
|
end)
|
|
|
|
|