2014-07-03 14:39:46 -06:00
|
|
|
--scripts/modtools/interaction-trigger.lua
|
2014-07-03 04:02:39 -06:00
|
|
|
--author expwnent
|
|
|
|
--triggers scripts when a unit does a unit interaction on another
|
|
|
|
|
|
|
|
local eventful = require 'plugins.eventful'
|
|
|
|
local utils = require 'utils'
|
|
|
|
|
2014-11-09 16:36:21 -07:00
|
|
|
attackTriggers = attackTriggers or {}
|
|
|
|
defendTriggers = defendTriggers or {}
|
|
|
|
commands = commands or {}
|
|
|
|
commandCount = commandCount or 0
|
2014-07-03 04:02:39 -06:00
|
|
|
|
2014-07-03 14:39:46 -06:00
|
|
|
eventful.enableEvent(eventful.eventType.INTERACTION,1) --cheap, so every tick is fine
|
|
|
|
eventful.enableEvent(eventful.eventType.UNLOAD,1)
|
|
|
|
|
|
|
|
eventful.onUnload.interactionTrigger = function()
|
2014-11-09 16:36:21 -07:00
|
|
|
attackTriggers = {}
|
|
|
|
defendTriggers = {}
|
|
|
|
commands = {}
|
|
|
|
commandCount = 0
|
2014-07-03 14:39:46 -06:00
|
|
|
end
|
|
|
|
|
2014-07-03 04:02:39 -06:00
|
|
|
local function processTrigger(args)
|
|
|
|
local command = {}
|
|
|
|
for _,arg in ipairs(args.command) do
|
|
|
|
if arg == '\\ATTACK_VERB' then
|
|
|
|
table.insert(command,args.attackVerb)
|
|
|
|
elseif arg == '\\DEFEND_VERB' then
|
|
|
|
table.insert(command,args.defendVerb)
|
|
|
|
elseif arg == '\\ATTACKER_ID' then
|
|
|
|
table.insert(command,args.attackerId)
|
|
|
|
elseif arg == '\\DEFENDER_ID' then
|
|
|
|
table.insert(command,args.defenderId)
|
|
|
|
elseif arg == '\\ATTACK_REPORT' then
|
|
|
|
table.insert(command,args.attackReport)
|
|
|
|
elseif arg == '\\DEFEND_REPORT' then
|
|
|
|
table.insert(command,args.defendReport)
|
|
|
|
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.onInteraction.interactionTrigger = function(attackVerb, defendVerb, attacker, defender, attackReport, defendReport)
|
|
|
|
local extras = {}
|
|
|
|
extras.attackVerb = attackVerb
|
|
|
|
extras.defendVerb = defendVerb
|
|
|
|
extras.attackReport = attackReport
|
|
|
|
extras.defendReport = defendReport
|
|
|
|
extras.attackerId = attacker
|
|
|
|
extras.defenderId = defender
|
|
|
|
local suppressAttack = false
|
|
|
|
local suppressDefend = false
|
2014-11-09 16:36:21 -07:00
|
|
|
local todo = {}
|
|
|
|
for _,trigger in ipairs(attackTriggers[attackVerb] or {}) do
|
|
|
|
todo[trigger] = true
|
2014-07-03 04:02:39 -06:00
|
|
|
end
|
2014-11-09 16:36:21 -07:00
|
|
|
for _,trigger in ipairs(defendTriggers[defendVerb] or {}) do
|
|
|
|
todo[trigger] = true
|
2014-07-03 04:02:39 -06:00
|
|
|
end
|
2014-11-09 16:36:21 -07:00
|
|
|
for k,v in pairs(todo) do
|
|
|
|
command = commands[k]
|
|
|
|
suppressAttack = suppressAttack or command.suppressAttack
|
|
|
|
suppressDefend = suppressDefend or command.suppressDefend
|
|
|
|
utils.fillTable(command,extras)
|
|
|
|
processTrigger(command)
|
|
|
|
utils.unfillTable(command,extras)
|
|
|
|
end
|
|
|
|
|
2014-07-03 04:02:39 -06:00
|
|
|
local eraseReport = function(unit,report)
|
|
|
|
for i,v in ipairs(unit.reports.log.Combat) do
|
|
|
|
if v == report then
|
|
|
|
unit.reports.log.Combat:erase(i)
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if suppressAttack or suppressDefend then
|
|
|
|
attacker = df.unit.find(tonumber(attacker))
|
|
|
|
defender = df.unit.find(tonumber(defender))
|
|
|
|
end
|
|
|
|
if suppressAttack then
|
|
|
|
eraseReport(attacker,attackReport)
|
|
|
|
eraseReport(defender,attackReport)
|
|
|
|
end
|
|
|
|
if suppressDefend then
|
|
|
|
eraseReport(attacker,defendReport)
|
|
|
|
eraseReport(defender,defendReport)
|
|
|
|
end
|
2014-11-09 16:36:21 -07:00
|
|
|
--TODO: get rid of combat report on LHS of screen
|
2014-07-03 04:02:39 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
----------------------------------------------------
|
|
|
|
--argument processing
|
|
|
|
|
|
|
|
validArgs = validArgs or utils.invert({
|
|
|
|
'clear',
|
|
|
|
'help',
|
|
|
|
'onAttackStr',
|
|
|
|
'onDefendStr',
|
|
|
|
'command',
|
|
|
|
'suppressAttack',
|
|
|
|
'suppressDefend',
|
|
|
|
})
|
|
|
|
|
|
|
|
local args = utils.processArgs({...}, validArgs)
|
|
|
|
|
2014-07-03 14:39:46 -06:00
|
|
|
if args.help then
|
|
|
|
print([[scripts/modtools/interaction-trigger.lua
|
|
|
|
arguments:
|
|
|
|
-help
|
|
|
|
print this help message
|
|
|
|
-clear
|
|
|
|
unregisters all triggers
|
|
|
|
-onAttackStr str
|
2014-11-09 16:36:21 -07:00
|
|
|
trigger the command when the attack verb is "str". both onAttackStr and onDefendStr MUST be specified
|
2014-07-03 14:39:46 -06:00
|
|
|
-onDefendStr str
|
2014-11-09 16:36:21 -07:00
|
|
|
trigger the command when the defend verb is "str". both onAttackStr and onDefendStr MUST be specified
|
2014-07-03 14:39:46 -06:00
|
|
|
-suppressAttack
|
|
|
|
delete the attack announcement from the combat logs
|
|
|
|
-suppressDefend
|
|
|
|
delete the defend announcement from the combat logs
|
2014-07-03 14:50:35 -06:00
|
|
|
-command [ commandStrs ]
|
|
|
|
specify the command to be executed
|
|
|
|
commandStrs
|
|
|
|
\\ATTACK_VERB
|
|
|
|
\\DEFEND_VERB
|
|
|
|
\\ATTACKER_ID
|
|
|
|
\\DEFENDER_ID
|
|
|
|
\\ATTACK_REPORT
|
|
|
|
\\DEFEND_REPORT
|
2014-11-09 16:36:21 -07:00
|
|
|
\\anything -> \anything
|
2014-07-03 14:50:35 -06:00
|
|
|
anything -> anything
|
2014-11-09 16:36:21 -07:00
|
|
|
You must specify both an attack string and a defend string to guarantee correct performance. Either will trigger the script when it happens, but it will not be triggered twice in a row if both happen.
|
2014-07-03 14:39:46 -06:00
|
|
|
]])
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2014-07-03 04:02:39 -06:00
|
|
|
if args.clear then
|
2014-11-09 16:36:21 -07:00
|
|
|
triggers = {}
|
|
|
|
commands = {}
|
|
|
|
commandCount = 0
|
2014-07-03 04:02:39 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
if not args.command then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2014-11-09 16:36:21 -07:00
|
|
|
if not args.onAttackStr or not args.onDefendStr then
|
|
|
|
error 'You must specify both onAttackStr and onDefendStr.'
|
2014-07-03 04:02:39 -06:00
|
|
|
end
|
|
|
|
|
2014-11-09 16:36:21 -07:00
|
|
|
commands[commandCount] = args
|
|
|
|
|
|
|
|
if not attackTriggers[args.onAttackStr] then
|
|
|
|
attackTriggers[args.onAttackStr] = {}
|
|
|
|
end
|
|
|
|
table.insert(attackTriggers[args.onAttackStr],commandCount)
|
|
|
|
|
|
|
|
if not defendTriggers[args.onDefendStr] then
|
|
|
|
defendTriggers[args.onDefendStr] = {}
|
2014-07-03 04:02:39 -06:00
|
|
|
end
|
2014-11-09 16:36:21 -07:00
|
|
|
table.insert(defendTriggers[args.onDefendStr],commandCount)
|
2014-07-03 04:02:39 -06:00
|
|
|
|
2014-11-09 16:36:21 -07:00
|
|
|
commandCount = commandCount+1
|