2014-07-03 18:09:23 -06:00
|
|
|
--scripts/modtools/syndrome-trigger.lua
|
2014-07-03 06:58:43 -06:00
|
|
|
--author expwnent
|
|
|
|
--triggers scripts when units are infected with syndromes
|
2015-10-23 17:57:04 -06:00
|
|
|
--[[=begin
|
2014-07-03 06:58:43 -06:00
|
|
|
|
2015-10-23 17:57:04 -06:00
|
|
|
modtools/syndrome-trigger
|
|
|
|
=========================
|
|
|
|
Triggers dfhack commands when syndromes are applied to units.
|
|
|
|
|
|
|
|
=end]]
|
2014-07-03 06:58:43 -06:00
|
|
|
local eventful = require 'plugins.eventful'
|
|
|
|
local utils = require 'utils'
|
|
|
|
|
|
|
|
onInfection = onInfection or {}
|
|
|
|
|
2014-07-03 18:09:23 -06:00
|
|
|
eventful.enableEvent(eventful.eventType.UNLOAD,1)
|
|
|
|
eventful.onUnload.syndromeTrigger = function()
|
|
|
|
onInfection = {}
|
|
|
|
end
|
|
|
|
|
2014-07-03 06:58:43 -06:00
|
|
|
eventful.enableEvent(eventful.eventType.SYNDROME,5) --requires iterating through every unit, so not cheap, but not slow either
|
|
|
|
|
|
|
|
local function processTrigger(args)
|
|
|
|
local command = {}
|
|
|
|
for i,arg in ipairs(args.command) do
|
|
|
|
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)
|
|
|
|
local table = {}
|
|
|
|
table.unit = unit
|
|
|
|
table.unit_syndrome = unit_syndrome
|
|
|
|
table.syndrome = syndrome
|
|
|
|
for _,args in ipairs(onInfection[syn_id] or {}) do
|
|
|
|
utils.fillTable(args,table)
|
|
|
|
processTrigger(args)
|
|
|
|
utils.unfillTable(args,table)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
------------------------------
|
|
|
|
--argument processing
|
|
|
|
|
|
|
|
validArgs = validArgs or utils.invert({
|
|
|
|
'clear',
|
|
|
|
'help',
|
|
|
|
'command',
|
|
|
|
'syndrome'
|
|
|
|
})
|
|
|
|
|
|
|
|
local args = utils.processArgs({...}, validArgs)
|
|
|
|
|
|
|
|
if args.help then
|
2014-07-03 18:09:23 -06:00
|
|
|
print([[scripts/modtools/syndrome-trigger.lua
|
|
|
|
arguments
|
|
|
|
-help
|
|
|
|
print this help message
|
|
|
|
-clear
|
|
|
|
clear all triggers
|
|
|
|
-syndrome name
|
|
|
|
specify the name of a syndrome
|
|
|
|
-command [ commandStrs ]
|
|
|
|
specify the command to be executed after infection
|
|
|
|
args
|
|
|
|
\\SYNDROME_ID
|
|
|
|
\\UNIT_ID
|
|
|
|
\\LOCATION
|
2014-11-09 16:39:23 -07:00
|
|
|
\\anything -> \anything
|
2014-07-03 18:09:23 -06:00
|
|
|
anything -> anything
|
|
|
|
]])
|
2014-07-03 06:58:43 -06:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
if args.clear then
|
|
|
|
onInfection = {}
|
|
|
|
end
|
|
|
|
|
|
|
|
if not args.command then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
if not args.syndrome then
|
|
|
|
error 'Select a syndrome.'
|
|
|
|
end
|
|
|
|
|
|
|
|
local syndrome
|
|
|
|
for _,syn in ipairs(df.global.world.raws.syndromes.all) do
|
|
|
|
if syn.syn_name == args.syndrome then
|
|
|
|
if syndrome then
|
|
|
|
error ('Multiple syndromes with same name: ' .. syn.syn_name)
|
|
|
|
end
|
|
|
|
syndrome = syn.id
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if not syndrome then
|
2015-04-29 15:19:24 -06:00
|
|
|
error ('Could not find syndrome named ' .. args.syndrome)
|
2014-07-03 06:58:43 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
onInfection[syndrome] = onInfection[syndrome] or {}
|
|
|
|
table.insert(onInfection[syndrome], args)
|
|
|
|
|