-
Notifications
You must be signed in to change notification settings - Fork 231
Expand file tree
/
Copy pathsyndrome-trigger.lua
More file actions
164 lines (142 loc) · 4.59 KB
/
Copy pathsyndrome-trigger.lua
File metadata and controls
164 lines (142 loc) · 4.59 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
-- triggers scripts when a syndrome is applied
--author expwnent
local usage = [====[
modtools/syndrome-trigger
=========================
Triggers dfhack commands when syndromes are applied to units.
Arguments::
-clear
clear any previously registered syndrome triggers
-syndrome SYN_NAME
specify a syndrome by its SYN_NAME
enclose the name in quotation marks if it includes spaces
example:
-syndrome "gila monster bite"
-synclass SYN_CLASS
any syndrome with the specified SYN_CLASS will act as a trigger
enclose in quotation marks if it includes spaces
example:
-synclass VAMPCURSE
-command [ commandStrs ]
specify the command to be executed after infection
remember to include a space after/before the square brackets!
the following may be added to appropriate commands where relevant:
\\UNIT_ID
inserts the ID of the infected unit
\\LOCATION
inserts the x, y, z coordinates of the infected unit
\\SYNDROME_ID
inserts the ID of the syndrome
note that:
\\anything -> \anything
anything -> anything
examples:
-command [ full-heal -unit \\UNIT_ID ]
heals units when they acquire the specified syndrome
-command [ modtools/spawn-flow -flowType Dragonfire -location [ \\LOCATION ] ]
spawns dragonfire at the location of infected units
]====]
local eventful = require 'plugins.eventful'
local utils = require 'utils'
onInfection = onInfection or {} --as:{_type:table,_array:{_type:table,_array:{_type:table,command:__arg}}}
eventful.enableEvent(eventful.eventType.UNLOAD,1)
eventful.onUnload.syndromeTrigger = function()
onInfection = {}
end
eventful.enableEvent(eventful.eventType.SYNDROME,5) --requires iterating through every unit, so not cheap, but not slow either
local function processTrigger(args)
local command = {} --as:string[]
for i,arg in ipairs(args.command) do --as:string
if arg == '\\SYNDROME_ID' then
table.insert(command, '' .. args.syndrome.id)
elseif arg == '\\UNIT_ID' then
table.insert(command, '' .. args.unit.id)
elseif arg == '\\LOCATION' then
table.insert(command, '' .. args.unit.pos.x)
table.insert(command, '' .. args.unit.pos.y)
table.insert(command, '' .. args.unit.pos.z)
elseif string.sub(arg,1,1) == '\\' then
table.insert(command, string.sub(arg,2))
else
table.insert(command, arg)
end
end
dfhack.run_command(table.unpack(command))
end
eventful.onSyndrome.syndromeTrigger = function(unitId, syndromeIndex)
local unit = df.unit.find(unitId)
local unit_syndrome = unit.syndromes.active[syndromeIndex]
local syn_id = unit_syndrome['type']
if not onInfection[syn_id] then
return
end
local syndrome = df.syndrome.find(syn_id)
for _,args in ipairs(onInfection[syn_id] or {}) do
processTrigger({
unit = unit,
unit_syndrome = unit_syndrome,
syndrome = syndrome,
command = args.command
})
end
end
------------------------------
--argument processing
local validArgs = utils.invert({
'clear',
'help',
'command',
'syndrome',
'synclass',
'unit'
})
local args = utils.processArgs({...}, validArgs)
if args.help then
print(usage)
return
end
if args.clear then
onInfection = {}
if not args.command and not args.syndrome then
return
end
end
if not args.syndrome and not args.synclass then
qerror('Syndrome not specified! Enter "modtools/syndrome-trigger -help" for more information.')
end
if not args.command then
qerror('Command not specified! Enter "modtools/syndrome-trigger -help" for more information.')
end
function processSyndrome(syndrome)
onInfection[syndrome] = onInfection[syndrome] or {}
table.insert(onInfection[syndrome], args)
end
local synFound = false
for _,syn in ipairs(df.global.world.raws.mat_table.syndromes.all) do
local matchedSyn = false
if args.syndrome then
if syn.syn_name == args.syndrome then
if syndrome then
qerror ('Multiple syndromes with the same name: ' .. syn.syn_name)
end
synFound = true
matchedSyn = true
processSyndrome(syn.id)
end
end
if not matchedSyn and args.synclass then -- no need to check for a SYN_CLASS match if the syndrome has already been registered
for _,synclass in ipairs(syn.syn_class) do
if synclass.value == args.synclass then
synFound = true
processSyndrome(syn.id)
end
end
end
end
if not synFound then
if args.syndrome then
qerror ('Invalid syndrome name: '..args.syndrome..'')
elseif args.synclass then
qerror ('Invalid syndrome class: '..args.synclass..'')
end
end