forked from DFHack/scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lever.lua
203 lines (176 loc) · 4.91 KB
/
lever.lua
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
-- Inspect and pull levers
local argparse = require('argparse')
function leverPullJob(lever, priority)
local ref = df.general_ref_building_holderst:new()
ref.building_id = lever.id
local job = df.job:new()
job.job_type = df.job_type.PullLever
job.pos = {
x = lever.centerx,
y = lever.centery,
z = lever.z
}
job.flags.do_now = priority
job.general_refs:insert("#", ref)
lever.jobs:insert("#", job)
dfhack.job.linkIntoWorld(job, true)
dfhack.job.checkBuildingsNow()
print(leverDescribe(lever))
end
function leverPullInstant(lever)
for _, m in ipairs(lever.linked_mechanisms) do
local tref = dfhack.items.getGeneralRef(m, df.general_ref_type.BUILDING_HOLDER)
if tref then
tref:getBuilding():setTriggerState(lever.state)
end
end
if lever.state == 1 then
lever.state = 0
else
lever.state = 1
end
print(leverDescribe(lever))
end
function leverDescribe(lever)
local lever_name = ''
if #lever.name > 0 then
lever_name = ' ' .. lever.name
end
if lever.state == 0 then
state_text = '\\'
else
state_text = '/'
end
local t = ('lever #%d%s @[%d, %d, %d] %s'):format(lever.id, lever_name, lever.centerx, lever.centery, lever.z,
state_text)
for _, j in ipairs(lever.jobs) do
if j.job_type == df.job_type.PullLever then
local r = ''
if j.flags.do_now then
r = r .. ', now'
end
if j.flags['repeat'] then
r = r .. ', repeat'
end
if j.flags.suspend then
r = r .. ', suspended'
end
t = t .. (' (pull order%s)'):format(r)
end
end
for _, m in ipairs(lever.linked_mechanisms) do
local tref = dfhack.items.getGeneralRef(m, df.general_ref_type.BUILDING_HOLDER)
if tref then
tg = tref:getBuilding()
if pcall(function()
return tg.gate_flags
end) then
if tg.gate_flags.closed then
state = "closed"
else
state = "opened"
end
if tg.gate_flags.closing then
state = state .. (', closing (%d)'):format(tg.timer)
end
if tg.gate_flags.opening then
state = state .. (', opening (%d)'):format(tg.timer)
end
end
t = t ..
("\n linked to %s %s #%d @[%d, %d, %d]"):format(state, df.building_type[tg:getType()], tg.id,
tg.centerx, tg.centery, tg.z)
end
end
return t
end
function ListLevers()
for k, v in ipairs(df.global.world.buildings.other.TRAP) do -- hint:df.building_trapst
if v.trap_type == df.trap_type.Lever then
print(('#%d: %s'):format(k, leverDescribe(v)))
end
end
end
function getLever(opts)
local lever
if opts.pullId == nil then
lever = dfhack.gui.getSelectedBuilding()
else
lever = df.building.find(opts.pullId)
end
if df.building_trapst:is_instance(lever) and lever.trap_type == df.trap_type.Lever then
return lever
end
end
function ShowLever(opts)
local lever = getLever(opts)
if lever == nil then
print("Can't find lever with ID, or no lever selected")
return
end
dfhack.gui.revealInDwarfmodeMap(lever.centerx, lever.centery, lever.z, true)
print(leverDescribe(lever))
end
function PullLever(opts)
local lever = getLever(opts)
if lever == nil then
print("Can't find lever with ID, or no lever selected")
return
end
if opts.instant then
leverPullInstant(lever)
else
leverPullJob(lever, opts.priority)
end
end
local function parse_commandline(args)
local opts = {}
local commands = argparse.processArgsGetopt(args, {{
'h',
'help',
handler = function()
opts.help = true
end
}, {
'i',
'id',
hasArg = true,
handler = function(a)
opts.pullId = tonumber(a)
end
}, {
nil,
'priority',
handler = function()
opts.priority = true
end
}, {
nil,
'instant',
handler = function()
opts.instant = true
end
}})
if commands[1] == "list" then
opts.list = true
elseif commands[1] == "show" then
opts.show = true
elseif commands[1] == "pull" then
opts.pull = true
elseif commands[1] == "help" then
opts.help = true
end
return opts
end
local opts = parse_commandline({...})
if opts.help then
print(dfhack.script_help())
elseif opts.list then
ListLevers()
elseif opts.show then
ShowLever(opts)
elseif opts.pull then
PullLever(opts)
else
print(dfhack.script_help())
end