-
Notifications
You must be signed in to change notification settings - Fork 231
Expand file tree
/
Copy pathreaction-product-trigger.lua
More file actions
126 lines (109 loc) · 3.55 KB
/
Copy pathreaction-product-trigger.lua
File metadata and controls
126 lines (109 loc) · 3.55 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
-- trigger commands before/after reactions produce items
-- author expwnent
--@ module = true
local usage = [====[
modtools/reaction-product-trigger
=================================
This triggers dfhack commands when reaction products are produced, once per
product. Usage::
-clear
unregister all reaction hooks
-reactionName name
specify the name of the reaction
-command [ commandStrs ]
specify the command to be run on the target(s)
special args
\\WORKER_ID
\\REACTION
\\BUILDING_ID
\\LOCATION
\\INPUT_ITEMS
\\OUTPUT_ITEMS
\\anything -> \anything
anything -> anything
]====]
local eventful = require 'plugins.eventful'
local utils = require 'utils'
--TODO: onUnload
productHooks = productHooks or {} --as:{_type:table,_array:{_type:table,_array:{_type:table,clear:__arg,reactionName:__arg,command:__arg}}}
eventful.enableEvent(eventful.eventType.UNLOAD,1)
eventful.onUnload.reactionProductTrigger = function()
productHooks = {}
end
--productHooks.before = productHooks.before or {}
--productHooks.after = productHooks.after or {}
local function processArgs(args, reaction, reaction_product, unit, input_items, input_reagents, output_items, buildingId)
local result = {} --as:string[]
for _,arg in ipairs(args) do
if arg == '\\WORKER_ID' then
table.insert(result,tostring(unit.id))
elseif arg == '\\REACTION' then
table.insert(result,reaction.code)
-- elseif arg == '\\REACTION_PRODUCT' then
-- table.insert(result,reaction_product)
elseif arg == '\\INPUT_ITEMS' then
--table.insert(result,'[')
for _,item in ipairs(input_items) do
table.insert(result,tostring(item.id))
end
--table.insert(result,']')
elseif arg == '\\OUTPUT_ITEMS' then
--table.insert(result,'[')
for _,item in ipairs(output_items) do
table.insert(result,tostring(item.id))
end
--table.insert(result,']')
elseif arg == '\\BUILDING_ID' then
if not buildingId then
error('BUILDING_ID is not supported for adventure mode reactions')
end
table.insert(result,tostring(buildingId))
elseif string.sub(arg,1,1) == '\\' then
table.insert(result,string.sub(arg,2))
else
table.insert(result,arg)
end
end
return result
end
local function afterProduce(reaction,reaction_product,unit,input_items,input_reagents,output_items)
--adv mode reactions have no associated building.
local _,buildingId
if unit.job.current_job then
_,buildingId = dfhack.script_environment('modtools/reaction-trigger').getWorkerAndBuilding(unit.job.current_job)
end
for _,hook in ipairs(productHooks[reaction.code] or {}) do
local command = hook.command
local processed = processArgs(command, reaction, reaction_product, unit, input_items, input_reagents, output_items, buildingId)
dfhack.run_command(table.unpack(processed))
end
end
eventful.onReactionComplete.reactionProductTrigger = function(reaction,reaction_product,unit,input_items,input_reagents,output_items)
afterProduce(reaction,reaction_product,unit,input_items,input_reagents,output_items)
end
local validArgs = utils.invert({
'help',
'clear',
'reactionName',
'command',
})
if moduleMode then
return
end
local args = utils.processArgs({...}, validArgs)
if args.help then
print(usage)
return
end
if args.clear then
productHooks = {}
return
end
if not args.reactionName then
error('No reactionName.')
end
if not args.command then
error('No command.')
end
productHooks[args.reactionName] = productHooks[args.reactionName] or {}
table.insert(productHooks[args.reactionName], args)