From 4be41eb1ed2ff366bfe7bc190fc6a60098bf5b31 Mon Sep 17 00:00:00 2001 From: expwnent Date: Thu, 3 Jul 2014 18:28:05 -0400 Subject: [PATCH] projectile-trigger.lua --- NEWS | 2 + scripts/modtools/item-trigger.lua | 2 +- scripts/modtools/projectile-trigger.lua | 101 ++++++++++++++++++++++++ 3 files changed, 104 insertions(+), 1 deletion(-) create mode 100644 scripts/modtools/projectile-trigger.lua diff --git a/NEWS b/NEWS index 86cfc9da0..deb0ce76e 100644 --- a/NEWS +++ b/NEWS @@ -69,6 +69,8 @@ DFHack future makes raw moddable gods possible projectileExpansion.lua adds extra functionality to projectiles + projectile-trigger.lua + standardized version of projectileExpansion reaction-trigger.lua trigger commands when custom reactions complete replaces autoSyndrome diff --git a/scripts/modtools/item-trigger.lua b/scripts/modtools/item-trigger.lua index 61d1cb3bf..b5b6ab700 100644 --- a/scripts/modtools/item-trigger.lua +++ b/scripts/modtools/item-trigger.lua @@ -48,7 +48,7 @@ function processTrigger(command) elseif arg == '\\UNIT_ID' then command2[i] = command.unit.id elseif string.sub(arg,1,1) == '\\' then - command2[i] = string.sub(command,2) + command2[i] = string.sub(arg,2) else command2[i] = arg end diff --git a/scripts/modtools/projectile-trigger.lua b/scripts/modtools/projectile-trigger.lua new file mode 100644 index 000000000..5315a9fd2 --- /dev/null +++ b/scripts/modtools/projectile-trigger.lua @@ -0,0 +1,101 @@ +--scripts/modtools/projectile-trigger.lua +--author expwnent +--based on Putnam's projectileExpansion + +local eventful = require 'plugins.eventful' +local utils = require 'utils' + +materialTriggers = materialTriggers or {} + +eventful.enableEvent(eventful.eventType.UNLOAD,1) +eventful.onUnload.projectileTrigger = function() + materialTriggers = {} +end + +function processTrigger(args) + local command2 = {} + for _,arg in ipairs(args.command) do + if arg == '\\LOCATION' then + table.insert(command2,args.pos.x) + table.insert(command2,args.pos.y) + table.insert(command2,args.pos.z) + elseif arg == '\\PROJECTILE_ID' then + table.insert(command2,args.projectile.id) + elseif arg == '\\FIRER_ID' then + table.insert(command2,args.projectile.firer.id) + elseif string.sub(arg,1,1) == '\\' then + table.insert(command2,string.sub(arg,2)) + else + table.insert(command2,arg) + end + end + dfhack.run_command(table.unpack(command2)) +end + +eventful.onProjItemCheckImpact.expansion = function(projectile) + local matStr = dfhack.matinfo.decode(projectile.item):getToken() + local table = {} + table.pos = projectile.cur_pos + table.projectile = projectile + table.item = projectile.item + for _,args in ipairs(materialTriggers[matStr] or {}) do + utils.fillTable(args,table) + processTrigger(args) + utils.unfillTable(args,table) + end +end + +validArgs = validArgs or utils.invert({ + 'help', + 'clear', + 'command', + 'material', +}) + +local args = utils.processArgs({...}, validArgs) + +if args.help then + print([[scripts/modtools/projectile-trigger.lua +arguments + -help + print this help message + -clear + unregister all triggers + -material + specify a material for projectiles that will trigger the command + examples: + INORGANIC:IRON + CREATURE_MAT:DWARF:BRAIN + PLANT_MAT:MUSHROOM_HELMET_PLUMP:DRINK + -command [ commandList ] + \\LOCATION + \\PROJECTILE_ID + \\FIRER_ID + \\anything -> anything + anything -> anything +]]) + return +end + +if args.clear then + materialTriggers = {} +end + +if not args.command then + return +end + +if not args.material then + error 'specify a material' +end + +if not dfhack.matinfo.find(args.material) then + error ('invalid material: ' .. args.material) +end + +if not materialTriggers[args.material] then + materialTriggers[args.material] = {} +end +table.insert(materialTriggers[args.material], args) + +