forked from DFHack/dfhack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprojectile-trigger.lua
More file actions
114 lines (95 loc) · 2.56 KB
/
Copy pathprojectile-trigger.lua
File metadata and controls
114 lines (95 loc) · 2.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
--scripts/modtools/projectile-trigger.lua
--author expwnent
--based on Putnam's projectileExpansion
--TODO: trigger based on contaminants
--[[
BEGIN_DOCS
.. _scripts/modtools/projectile-trigger:
modtools/projectile-trigger
===========================
This triggers dfhack commands when projectiles hit their targets.
END_DOCS
]]
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)