forked from DFHack/dfhack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworkflow.lua
More file actions
364 lines (320 loc) · 11.1 KB
/
workflow.lua
File metadata and controls
364 lines (320 loc) · 11.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
local _ENV = mkmodule('plugins.workflow')
local utils = require 'utils'
--[[
Native functions:
* isEnabled()
* setEnabled(enable)
* listConstraints([job[,with_history] ]) -> {{...},...}
* findConstraint(token) -> {...} or nil
* setConstraint(token[, by_count, goal, gap]) -> {...}
* deleteConstraint(token) -> true/false
* getCountHistory(token) -> {{...},...} or nil
--]]
local reaction_id_cache = nil
if dfhack.is_core_context then
dfhack.onStateChange[_ENV] = function(code)
if code == SC_MAP_LOADED then
reaction_id_cache = nil
end
end
end
local function get_reaction(name)
if not reaction_id_cache then
reaction_id_cache = {}
for i,v in ipairs(df.global.world.raws.reactions.reactions) do
reaction_id_cache[v.code] = i
end
end
local id = reaction_id_cache[name] or -1
return id, df.reaction.find(id)
end
local job_outputs = {}
function job_outputs.CustomReaction(callback, job)
local rid, r = get_reaction(job.reaction_name)
if not r then
return
end
for i,prod in ipairs(r.products) do
if df.reaction_product_itemst:is_instance(prod) then
local mat_type, mat_index = prod.mat_type, prod.mat_index
local mat_mask
local get_mat_prod = prod.flags.GET_MATERIAL_PRODUCT
if get_mat_prod or prod.flags.GET_MATERIAL_SAME then
local reagent_code = prod.get_material.reagent_code
local reagent_idx, src = utils.linear_index(r.reagents, reagent_code, 'code')
if not reagent_idx then goto continue end
local item_idx, jitem = utils.linear_index(job.job_items, reagent_idx, 'reagent_index')
if jitem then
mat_type, mat_index = jitem.mat_type, jitem.mat_index
else
if not df.reaction_reagent_itemst:is_instance(src) then goto continue end
mat_type, mat_index = src.mat_type, src.mat_index
end
if get_mat_prod then
local p_code = prod.get_material.product_code
local mat = dfhack.matinfo.decode(mat_type, mat_index)
mat_type, mat_index = -1, -1
if mat then
local rp = mat.material.reaction_product
local idx = utils.linear_index(rp.id, p_code, 'value')
if idx then
mat_type, mat_index = rp.material.mat_type[idx], rp.material.mat_index[idx]
end
else
if p_code == "SOAP_MAT" then
mat_mask = { soap = true }
end
end
end
end
callback{
is_craft = prod.flags.CRAFTS,
item_type = prod.item_type, item_subtype = prod.item_subtype,
mat_type = mat_type, mat_index = mat_index, mat_mask = mat_mask
}
end
::continue::
end
end
local function guess_job_material(job)
if job.job_type == df.job_type.PrepareMeal then
return -1, -1, nil
end
local mat_type, mat_index = job.mat_type, job.mat_index
local mask_whole = job.material_category.whole
local mat_mask
local jmat = df.job_type.attrs[job.job_type].material
if jmat then
mat_type, mat_index = df.builtin_mats[jmat] or -1, -1
if mat_type < 0 and df.dfhack_material_category[jmat] then
mat_mask = { [jmat] = true }
end
end
if not mat_mask and mask_whole ~= 0 then
mat_mask = utils.parse_bitfield_int(mask_whole, df.dfhack_material_category)
if mat_mask.wood2 then
mat_mask.wood = true
mat_mask.wood2 = nil
end
end
if mat_type < 0 and #job.job_items > 0 then
local item0 = job.job_items[0]
if #job.job_items == 1 or item0.item_type == df.item_type.PLANT then
mat_type, mat_index = item0.mat_type, item0.mat_index
if item0.item_type == df.item_type.WOOD then
mat_mask = mat_mask or {}
mat_mask.wood = true
end
end
end
return mat_type, mat_index, mat_mask
end
function default_output(callback, job, mat_type, mat_index, mat_mask)
local itype = df.job_type.attrs[job.job_type].item
if itype >= 0 then
local subtype = nil
if df.item_type.attrs[itype].is_rawable then
subtype = job.item_subtype
end
callback{
item_type = itype, item_subtype = subtype,
mat_type = mat_type, mat_index = mat_index, mat_mask = mat_mask
}
end
end
function job_outputs.SmeltOre(callback, job)
local mat = dfhack.matinfo.decode(job.job_items[0])
if mat and mat.inorganic then
for i,v in ipairs(mat.inorganic.metal_ore.mat_index) do
callback{ item_type = df.item_type.BAR, mat_type = 0, mat_index = v }
end
else
callback{ item_type = df.item_type.BAR, mat_type = 0 }
end
end
function job_outputs.ExtractMetalStrands(callback, job)
local mat = dfhack.matinfo.decode(job.job_items[0])
if mat and mat.inorganic then
for i,v in ipairs(mat.inorganic.thread_metal.mat_index) do
callback{ item_type = df.item_type.THREAD, mat_type = 0, mat_index = v }
end
else
callback{ item_type = df.item_type.THREAD, mat_type = 0 }
end
end
function job_outputs.PrepareMeal(callback, job)
if job.mat_type ~= -1 then
for i,v in ipairs(df.global.world.raws.itemdefs.food) do
if v.level == job.mat_type then
callback{ item_type = df.item_type.FOOD, item_subtype = i }
end
end
else
callback{ item_type = df.item_type.FOOD }
end
end
function job_outputs.MakeCrafts(callback, job)
local mat_type, mat_index, mat_mask = guess_job_material(job)
callback{
is_craft = true,
item_type = -1,
item_subtype = -1,
mat_type = mat_type,
mat_index = mat_index,
mat_mask = mat_mask
}
end
local plant_products = {
MillPlants = 'MILL',
ProcessPlants = 'THREAD',
ProcessPlantsBarrel = 'EXTRACT_BARREL',
ProcessPlantsVial = 'EXTRACT_VIAL',
ExtractFromPlants = 'EXTRACT_STILL_VIAL',
}
for job,flag in pairs(plant_products) do
local tag = string.lower(flag)
job_outputs[job] = function(callback, job)
local mat_type, mat_index = -1, -1
local seed_type, seed_index = -1, -1
local mat = dfhack.matinfo.decode(job.job_items[0])
if mat and mat.plant and mat.plant.flags[flag] then
mat_type = mat.plant.material_defs.type[tag]
mat_index = mat.plant.material_defs.idx[tag]
seed_type = mat.plant.material_defs.type.seed
seed_index = mat.plant.material_defs.idx.seed
end
local mat_mask = { }
if flag ~= 'LEAVES' then
mat_mask.plant = true
end
default_output(callback, job, mat_type, mat_index, mat_mask)
callback{ item_type = df.item_type.SEEDS, mat_type = seed_type, mat_index = seed_index }
end
end
local function enum_job_outputs(callback, job)
local handler = job_outputs[df.job_type[job.job_type]]
if handler then
handler(callback, job)
else
default_output(callback, job, guess_job_material(job))
end
end
function doEnumJobOutputs(native_cb, job)
local function cb(info)
native_cb(
info.item_type, info.item_subtype,
info.mat_mask, info.mat_type, info.mat_index,
info.is_craft
)
end
enum_job_outputs(cb, job)
end
function listJobOutputs(job)
local res = {}
enum_job_outputs(curry(table.insert, res), job)
return res
end
function constraintToToken(cspec)
local token
if cspec.is_craft then
token = 'CRAFTS'
else
token = df.item_type[cspec.item_type] or error('invalid item type: '..cspec.item_type)
if cspec.item_subtype and cspec.item_subtype >= 0 then
local def = dfhack.items.getSubtypeDef(cspec.item_type, cspec.item_subtype)
if def then
token = token..':'..def.id
else
error('invalid subtype '..cspec.item_subtype..' of '..token)
end
end
end
local mask_part
if cspec.mat_mask then
mask_part = string.upper(table.concat(utils.list_bitfield_flags(cspec.mat_mask), ','))
end
local mat_part
if cspec.mat_type and cspec.mat_type >= 0 then
local mat = dfhack.matinfo.decode(cspec.mat_type, cspec.mat_index or -1)
if mat then
mat_part = mat:getToken()
else
error('invalid material: '..cspec.mat_type..':'..(cspec.mat_index or -1))
end
end
local qlist = {}
if cspec.is_local then
table.insert(qlist, "LOCAL")
end
if cspec.min_quality and cspec.min_quality > 0 then
local qn = df.item_quality[cspec.min_quality] or error('invalid quality: '..cspec.min_quality)
table.insert(qlist, qn)
end
local qpart
if #qlist > 0 then
qpart = table.concat(qlist, ',')
end
if mask_part or mat_part or qpart then
token = token .. '/' .. (mask_part or '')
if mat_part or qpart then
token = token .. '/' .. (mat_part or '')
if qpart then
token = token .. '/' .. (qpart or '')
end
end
end
return token
end
function listWeakenedConstraints(outputs)
local variants = {}
local known = {}
local function register(cons)
cons.token = constraintToToken(cons)
if not known[cons.token] then
known[cons.token] = true
table.insert(variants, cons)
end
end
local generic = {}
local anymat = {}
for i,cons in ipairs(outputs) do
local mask = cons.mat_mask
if (cons.mat_type or -1) >= 0 then
cons.mat_mask = nil
local info = dfhack.matinfo.decode(cons)
if info then
for i,flag in ipairs(df.dfhack_material_category) do
if flag and flag ~= 'wood2' and info:matches{[flag]=true} then
mask = mask or {}
mask[flag] = true
end
end
end
end
register(cons)
if mask then
for k,v in pairs(mask) do
table.insert(generic, {
item_type = cons.item_type,
item_subtype = cons.item_subtype,
is_craft = cons.is_craft,
mat_mask = { [k] = v }
})
end
end
table.insert(anymat, {
item_type = cons.item_type,
item_subtype = cons.item_subtype,
is_craft = cons.is_craft
})
end
for i,cons in ipairs(generic) do register(cons) end
for i,cons in ipairs(anymat) do register(cons) end
return variants
end
if dfhack.internal.IN_TEST then
test_data = {
job_outputs = job_outputs,
}
end
return _ENV