forked from DFHack/scripts
-
Notifications
You must be signed in to change notification settings - Fork 1
/
do-job-now.lua
215 lines (186 loc) · 5.82 KB
/
do-job-now.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
204
205
206
207
208
209
210
211
212
213
214
215
-- makes a job involving current selection high priority
local utils = require('utils')
local function print_help()
print(dfhack.script_help())
end
local function getUnitName(unit)
local language_name = dfhack.units.getVisibleName(unit)
if language_name.has_name then
return dfhack.df2console(dfhack.TranslateName( language_name ))
end
-- animals
return dfhack.units.getProfessionName(unit)
end
local function doJobNow(job)
local job_str = dfhack.job.getName(job)
if not job.flags.do_now then
job.flags.do_now = true
print("Made the job " .. job_str .. " top priority")
else
print("The job " .. job_str .. " is already top priority")
end
local building = dfhack.job.getHolder(job)
if building then
print("... at " .. utils.getBuildingName(building))
end
local unit = dfhack.job.getWorker(job)
if unit then
print("... by " .. getUnitName(unit) )
end
end
local function doItemJobNow(item)
if not item.flags.in_job then
qerror(dfhack.items.getDescription(item, 0) .. " must be in a job! (look for 'TSK')")
end
local sref = dfhack.items.getSpecificRef(item, df.specific_ref_type.JOB)
if sref then
doJobNow(sref.data.job)
return
end
print("Couldn't find any job for " .. dfhack.items.getDescription(item, 0))
end
local function doBuildingJobNow(building)
--print('This will attempt to make a job of a building a top priority')
if #building.jobs > 0
and (
building.jobs[0].job_type == df.job_type.ConstructBuilding
or building.jobs[0].job_type == df.job_type.DestroyBuilding
)
then
doJobNow(building.jobs[0])
return
end
print("Couldn't find either construct or destroy building job for " .. utils.getBuildingName(building))
end
local function doUnitJobNow(unit)
if dfhack.units.isCitizen(unit) then
--print('This will attempt to make a job of ' .. getUnitName(unit) .. ' a top priority')
local t_job = unit.job
if t_job then
local job = t_job.current_job
if job then
doJobNow(job)
return
end
end
print("Couldn't find any job for " .. getUnitName(unit) )
else
--print('This will attempt to make a job with ' .. getUnitName(unit) .. ' a top priority')
local needle = unit.id
for _link, job in utils.listpairs(df.global.world.jobs.list) do
if #job.general_refs > 0 then
for _, gref in ipairs(job.general_refs) do
--if gref:getType() == df.general_ref_type.UNIT then -- can't do this: there are many different types that can be units
local u = gref:getUnit()
if u and u.id == needle then
doJobNow(job)
return
end
--end
end
end
end
print("Couldn't find any job involving " .. getUnitName(unit) )
end
end
local function doPlantJobNow(plant)
--print('This will attempt to make a job with a plant a top priority')
for _link, job in utils.listpairs(df.global.world.jobs.list) do
if plant.pos.x == job.pos.x
and plant.pos.y == job.pos.y
and plant.pos.z == job.pos.z
then
doJobNow(job)
return
end
end
print("Couldn't find any job involving this plant.")
end
local function doWorkOrderJobsNow(order)
local needle = order.id
local cnt = 0
for _link, job in utils.listpairs(df.global.world.jobs.list) do
if job.order_id == needle then
doJobNow(job)
cnt = cnt + 1
end
end
if cnt > 0 then
print("Found " .. cnt .. " jobs for this work order.")
else
print("Couldn't find any job for this work order.")
end
end
local function getSelectedWorkOrder()
local scr = dfhack.gui.getCurViewscreen()
local orders
local idx
if df.viewscreen_jobmanagementst:is_instance(scr) then
orders = df.global.world.manager_orders.all
idx = scr.sel_idx
elseif df.viewscreen_workshop_profilest:is_instance(scr)
and scr.tab == df.viewscreen_workshop_profilest.T_tab.Orders
then
orders = scr.orders
idx = scr.order_idx
end
if orders then
if idx < #orders then
return orders[idx]
else
qerror("Invalid work order selected")
end
end
return nil
end
local function doSelectedEntityJobNow()
-- do we have a job selected?
local job = dfhack.gui.getSelectedJob(true)
if job then
doJobNow(job)
return
end
-- do we have an item selected?
local item = dfhack.gui.getSelectedItem(true)
if item then
doItemJobNow(item)
return
end
-- do we have a building selected?
local building = dfhack.gui.getSelectedBuilding(true)
if building then
doBuildingJobNow(building)
return
end
-- do we have a unit selected?
local unit = dfhack.gui.getSelectedUnit(true)
if unit then
doUnitJobNow(unit)
return
end
-- do we have a plant selected?
local plant = dfhack.gui.getSelectedPlant(true)
if plant then
doPlantJobNow(plant)
return
end
-- do we have a work order selected?
local order = getSelectedWorkOrder()
if order then
doWorkOrderJobsNow(order)
return
end
qerror("Select something job-related in game.")
end
local default_action = print_help
local actions = {
-- help
["-?"] = print_help,
["?"] = print_help,
["--help"] = print_help,
["help"] = print_help,
-- action
["default"] = doSelectedEntityJobNow,
}
-- Lua is beautiful.
(actions[ (...) or "default" ] or default_action)(...)