forked from DFHack/scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreaction-trigger.lua
More file actions
216 lines (196 loc) · 6.39 KB
/
reaction-trigger.lua
File metadata and controls
216 lines (196 loc) · 6.39 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
-- trigger commands when custom reactions complete
-- author expwnent
-- replaces autoSyndrome
--@ module = true
local eventful = require 'plugins.eventful'
local syndromeUtil = require 'syndrome-util'
local utils = require 'utils'
reactionHooks = reactionHooks or {} --as:{_type:table,_array:{_type:table,_array:{_type:table,reactionName:__arg,syndrome:__arg,allowNonworkerTargets:__arg,allowMultipleTargets:__arg,resetPolicy:__arg,command:__arg}}}
eventful.enableEvent(eventful.eventType.UNLOAD,1)
eventful.onUnload.reactionTrigger = function()
reactionHooks = {}
end
local function findSyndrome(name)
for _,syndrome in ipairs(df.global.world.raws.mat_table.syndromes.all) do
if syndrome.syn_name == name then
return syndrome
end
end
end
function getWorkerAndBuilding(job)
local workerId = -1
local buildingId = -1
for _,generalRef in ipairs(job.general_refs) do
if generalRef:getType() == df.general_ref_type.UNIT_WORKER then
if workerId ~= -1 then
print(job)
printall(job)
error('reaction-trigger: two workers on same job: ' .. workerId .. ', ' .. generalRef.unit_id) --hint:df.general_ref_unit_workerst
else
workerId = generalRef.unit_id --hint:df.general_ref_unit_workerst
if workerId == -1 then
print(job)
printall(job)
error('reaction-trigger: invalid worker')
end
end
elseif generalRef:getType() == df.general_ref_type.BUILDING_HOLDER then
if buildingId ~= -1 then
print(job)
printall(job)
error('reaction-trigger: two buildings same job: ' .. buildingId .. ', ' .. generalRef.building_id) --hint:df.general_ref_building_holderst
else
buildingId = generalRef.building_id --hint:df.general_ref_building_holderst
if buildingId == -1 then
print(job)
printall(job)
error('reaction-trigger: invalid building')
end
end
end
end
return workerId,buildingId
end
local function processCommand(job, worker, target, building, command)
local result = {} --as:string[]
for _,arg in ipairs(command) do
if arg == '\\WORKER_ID' then
table.insert(result,''..worker.id)
elseif arg == '\\TARGET_ID' then
table.insert(result,''..target.id)
elseif arg == '\\BUILDING_ID' then
table.insert(result,''..building.id)
elseif arg == '\\LOCATION' then
table.insert(result,''..job.pos.x)
table.insert(result,''..job.pos.y)
table.insert(result,''..job.pos.z)
elseif arg == '\\REACTION_NAME' then
table.insert(result,''..job.reaction_name)
elseif string.sub(arg,1,1) == '\\' then
table.insert(result,string.sub(arg,2))
else
table.insert(result,arg)
end
end
return result
end
eventful.onJobCompleted.reactionTrigger = function(job)
if job.completion_timer > 0 then
return
end
-- if job.job_type ~= df.job_type.CustomReaction then
-- --TODO: support builtin reaction triggers if someone asks
-- return
-- end
if not job.reaction_name or job.reaction_name == '' then
return
end
-- print('reaction name: ' .. job.reaction_name)
if not job.reaction_name or not reactionHooks[job.reaction_name] then
return
end
local worker_id,building_id = getWorkerAndBuilding(job)
local worker = df.unit.find(worker_id)
local building = df.building.find(building_id)
if not worker or not building then
--this probably means that it finished before EventManager could get a copy of the job while the job was running
--TODO: consider printing a warning once
return
end
local function doAction(action)
local didSomething = false
if action.syndrome and not action.ignoreWorker then
local syndrome = findSyndrome(action.syndrome)
if syndrome then
didSomething = syndromeUtil.infectWithSyndromeIfValidTarget(worker, syndrome, syndromeUtil.ResetPolicy[action.resetPolicy]) or didSomething
end
end
if action.command and not action.ignoreWorker and (not action.syndrome or didSomething) then
local processed = processCommand(job, worker, worker, building, action.command)
dfhack.run_command(table.unpack(processed))
didSomething = true
end
if didSomething and not action.allowMultipleTargets then
return
end
if not action.allowNonworkerTargets and not action.allowMultipleTargets then
return
end
local function foreach(unit)
local didSomething = false
if unit == worker or not (action.dontSkipInactive or dfhack.units.isActive(unit)) then
return false
end
local xRange, yRange, zRange = 0, 0, 0
if action.range then
xRange,yRange,zRange = tonumber(action.range[1]), tonumber(action.range[2]), tonumber(action.range[3])
end
if unit.pos.z < building.z - zRange or unit.pos.z > building.z + zRange then
return false
elseif unit.pos.x < building.x1 - xRange or unit.pos.x > building.x2 + xRange then
return false
elseif unit.pos.y < building.y1 - yRange or unit.pos.y > building.y2 + yRange then
return false
else
if action.syndrome then
local syndrome = findSyndrome(action.syndrome)
if syndrome then
didSomething = syndromeUtil.infectWithSyndromeIfValidTarget(unit,syndrome,syndromeUtil.ResetPolicy[action.resetPolicy]) or didSomething
end
end
if action.command and ( (not action.syndrome) or didSomething ) then
local processed = processCommand(job, worker, unit, building, action.command)
dfhack.run_command(table.unpack(processed))
didSomething = true
end
if didSomething and not action.allowMultipleTargets then
return true
end
return false
end
end
for _,unit in ipairs(df.global.world.units.all) do
if foreach(unit) then
break
end
end
end
for _,action in ipairs(reactionHooks[job.reaction_name]) do
doAction(action)
end
end
local validArgs = utils.invert({
'help',
'clear',
'reactionName',
'syndrome',
'command',
'allowNonworkerTargets',
'allowMultipleTargets',
'range',
'ignoreWorker',
'dontSkipInactive', --TODO: positions for inactive units are meaningless!
'resetPolicy'
})
if moduleMode then
return
end
local args = utils.processArgs({...}, validArgs)
if args.help then
print(dfhack.script_help())
return
end
if args.clear then
reactionHooks = {}
end
if not args.reactionName then
return
end
if not reactionHooks[args.reactionName] then
reactionHooks[args.reactionName] = {}
end
if args.syndrome and not findSyndrome(args.syndrome) then
error('Could not find syndrome ' .. args.syndrome)
end
eventful.enableEvent(eventful.eventType.JOB_COMPLETED,0) --0 is necessary to catch cancelled jobs and not trigger them
table.insert(reactionHooks[args.reactionName], args)