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
local eventful = require 'plugins.eventful'
local utils = require 'utils'
eventful.enableEvent(eventful.eventType.UNIT_ATTACK,1)
itemTriggers = itemTriggers or {}
materialTriggers = materialTriggers or {}
itemMaterialTriggers = itemMaterialTriggers or {}
--itemMaterialTriggers = itemMaterialTriggers or {}
local function processTrigger(command, attacker, defender)
local command2 = {}
@ -65,40 +66,24 @@ eventful.onUnitAttack.attackTrigger = function(attacker,defender,wound)
-- end
end
local args = {...}
local args = utils.processArgs(...)
local i = 1
local command
local weaponType
local material
while i <= #args do
if command then
table.insert(command, args[i])
i = i+1
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
if args.clear then
itemTriggers = {}
materialTriggers = {}
end
if not args.command then
if not args.clear then
error 'specify a command'
end
return
end
if weaponType then
if args.weaponType then
local temp
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
break
end
@ -106,36 +91,37 @@ if weaponType then
if not temp then
error 'Could not find weapon type.'
end
weaponType = temp
args.weaponType = temp
end
if material then
if args.material then
local i = 0
while true do
local mat = dfhack.matinfo.decode(0,i)
if not mat then
error 'Could not find material.'
error('Could not find material "' .. args.material .. '" after '..i..' materials.')
end
if mat.inorganic.id == material then
material = mat.index
if mat.inorganic.id == args.material then
args.material = mat.index
break
end
i = i+1
end
end
if material then
if not materialTriggers[material] then
materialTriggers[material] = {}
if args.material and args.weaponType then
error 'both material and weaponType defined'
end
if args.material then
if not materialTriggers[args.material] then
materialTriggers[args.material] = {}
end
table.insert(materialTriggers[material],command)
-- table.insert(materialTriggers[material],function()
-- print(dfhack.run_command(command))
-- end)
elseif weaponType then
if not itemTriggers[weaponType] then
itemTriggers[weaponType] = {}
table.insert(materialTriggers[args.material],args.command)
elseif args.weaponType then
if not itemTriggers[args.weaponType] then
itemTriggers[args.weaponType] = {}
end
table.insert(itemTriggers[weaponType],command)
table.insert(itemTriggers[args.weaponType],args.command)
end