attackTrigger now uses standard arguments.

develop
expwnent 2014-06-30 03:41:23 -04:00
parent 10c001b1f4
commit 1a9f9374de
1 changed files with 31 additions and 45 deletions

@ -3,11 +3,12 @@
--triggers scripts when a unit attacks another with a weapon type, a weapon of a particular material --triggers scripts when a unit attacks another with a weapon type, a weapon of a particular material
local eventful = require 'plugins.eventful' local eventful = require 'plugins.eventful'
local utils = require 'utils'
eventful.enableEvent(eventful.eventType.UNIT_ATTACK,1) eventful.enableEvent(eventful.eventType.UNIT_ATTACK,1)
itemTriggers = itemTriggers or {} itemTriggers = itemTriggers or {}
materialTriggers = materialTriggers or {} materialTriggers = materialTriggers or {}
itemMaterialTriggers = itemMaterialTriggers or {} --itemMaterialTriggers = itemMaterialTriggers or {}
local function processTrigger(command, attacker, defender) local function processTrigger(command, attacker, defender)
local command2 = {} local command2 = {}
@ -65,40 +66,24 @@ eventful.onUnitAttack.attackTrigger = function(attacker,defender,wound)
-- end -- end
end end
local args = {...} local args = utils.processArgs(...)
local i = 1 if args.clear then
local command itemTriggers = {}
local weaponType materialTriggers = {}
local material end
while i <= #args do
if command then if not args.command then
table.insert(command, args[i]) if not args.clear then
i = i+1 error 'specify a command'
else
if args[i] == '-weaponType' then
weaponType = args[i+1]
i = i+2
elseif args[i] == '-material' then
material = args[i+1]
i = i+2
elseif args[i] == '-command' then
command = {}
i = i+1
elseif args[i] == '-clear' then
itemTriggers = {}
materialTriggers = {}
i = i+1
else
error('Invalid arguments to attackTrigger.lua.')
end
end end
return
end end
if weaponType then if args.weaponType then
local temp local temp
for _,itemdef in ipairs(df.global.world.raws.itemdefs.weapons) do for _,itemdef in ipairs(df.global.world.raws.itemdefs.weapons) do
if itemdef.id == weaponType then if itemdef.id == args.weaponType then
temp = itemdef.subtype temp = itemdef.subtype
break break
end end
@ -106,36 +91,37 @@ if weaponType then
if not temp then if not temp then
error 'Could not find weapon type.' error 'Could not find weapon type.'
end end
weaponType = temp args.weaponType = temp
end end
if material then if args.material then
local i = 0 local i = 0
while true do while true do
local mat = dfhack.matinfo.decode(0,i) local mat = dfhack.matinfo.decode(0,i)
if not mat then if not mat then
error 'Could not find material.' error('Could not find material "' .. args.material .. '" after '..i..' materials.')
end end
if mat.inorganic.id == material then if mat.inorganic.id == args.material then
material = mat.index args.material = mat.index
break break
end end
i = i+1 i = i+1
end end
end end
if material then if args.material and args.weaponType then
if not materialTriggers[material] then error 'both material and weaponType defined'
materialTriggers[material] = {} end
if args.material then
if not materialTriggers[args.material] then
materialTriggers[args.material] = {}
end end
table.insert(materialTriggers[material],command) table.insert(materialTriggers[args.material],args.command)
-- table.insert(materialTriggers[material],function() elseif args.weaponType then
-- print(dfhack.run_command(command)) if not itemTriggers[args.weaponType] then
-- end) itemTriggers[args.weaponType] = {}
elseif weaponType then
if not itemTriggers[weaponType] then
itemTriggers[weaponType] = {}
end end
table.insert(itemTriggers[weaponType],command) table.insert(itemTriggers[args.weaponType],args.command)
end end