-
Notifications
You must be signed in to change notification settings - Fork 227
Expand file tree
/
Copy pathmultihaul.lua
More file actions
329 lines (292 loc) · 10.3 KB
/
multihaul.lua
File metadata and controls
329 lines (292 loc) · 10.3 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
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
-- Allow haulers to pick up multiple nearby items when using wheelbarrows
--@module = true
--@enable = true
local eventful = require('plugins.eventful')
local utils = require('utils')
local itemtools = reqscript('item')
local GLOBAL_KEY = 'multihaul'
local finish_jobs_without_wheelbarrow
local function get_default_state()
return {
enabled=false,
debug_enabled=false,
radius=10,
wheelbarrow_search_radius_k=5,
max_items=10,
mode='sametype',
autowheelbarrows=true
}
end
state = state or get_default_state()
function isEnabled()
return state.enabled
end
local function persist_state()
dfhack.persistent.saveSiteData(GLOBAL_KEY, state)
end
local function load_state()
state = get_default_state()
utils.assign(state, dfhack.persistent.getSiteData(GLOBAL_KEY, state))
end
local function for_each_item_in_radius(x, y, z, radius, fn)
local xmin = math.max(0, x - radius)
local xmax = math.min(df.global.world.map.x_count - 1, x + radius)
local ymin = math.max(0, y - radius)
local ymax = math.min(df.global.world.map.y_count - 1, y + radius)
local bxmin, bxmax = math.floor(xmin/16), math.floor(xmax/16)
local bymin, bymax = math.floor(ymin/16), math.floor(ymax/16)
for by = bymin, bymax do
for bx = bxmin, bxmax do
local block = dfhack.maps.getTileBlock(bx*16, by*16, z)
if block then
for _, id in ipairs(block.items) do
local item = df.item.find(id)
if item and fn(item) then return end
end
end
end
end
end
local function get_job_stockpile(job)
local ref = dfhack.job.getGeneralRef(job, df.general_ref_type.BUILDING_HOLDER)
return ref and df.building.find(ref.building_id) or nil
end
local function items_identical(a, b)
return a:getType() == b:getType() and a:getSubtype() == b:getSubtype() and
a.mat_type == b.mat_type and a.mat_index == b.mat_index
end
local function items_sametype(a, b)
return a:getType() == b:getType()
end
local function items_samesubtype(a, b)
return a:getType() == b:getType() and a:getSubtype() == b:getSubtype()
end
local match_fns = {
any = function() return true end,
identical = items_identical,
sametype = items_sametype,
samesubtype = items_samesubtype,
}
local function items_match(a, b)
local fn = match_fns[state.mode] or match_fns.sametype
return fn(a, b)
end
local function emptyContainedItems(wheelbarrow)
local items = dfhack.items.getContainedItems(wheelbarrow)
if #items == 0 then return end
if state.debug_enabled then
dfhack.gui.showAnnouncement('multihaul: emptying wheelbarrow', COLOR_CYAN)
end
for _, item in ipairs(items) do
if item.flags.in_job then
local job_ref = dfhack.items.getSpecificRef(item, df.specific_ref_type.JOB)
if job_ref then
dfhack.job.removeJob(job_ref.data.job)
end
end
dfhack.items.moveToGround(item, wheelbarrow.pos)
end
finish_jobs_without_wheelbarrow()
end
local function add_nearby_items(job)
if #job.items == 0 then return end
local target = job.items[0].item
if not target then return end
local stockpile = get_job_stockpile(job)
if not stockpile then return end
local x,y,z = dfhack.items.getPosition(target)
if not x then return end
local cond = {}
itemtools.condition_stockpiled(cond)
local is_stockpiled = cond[1]
local function matches(it)
return items_match(it, target)
end
local count = 0
for_each_item_in_radius(x, y, z, state.radius, function(it)
if it ~= target and not it.flags.in_job and it.flags.on_ground and not it.flags.forbid and
not it:isWheelbarrow() and not dfhack.items.isRouteVehicle(it) and
not is_stockpiled(it) and matches(it) then
dfhack.job.attachJobItem(job, it, df.job_role_type.Hauled, -1, -1)
count = count + 1
if state.debug_enabled then
dfhack.gui.showAnnouncement(
('multihaul: added %s to hauling job of %s'):format(
dfhack.items.getDescription(it, 0), dfhack.items.getDescription(target, 0)),
COLOR_CYAN)
end
if count >= state.max_items then return true end
end
end)
end
local function find_attached_wheelbarrow(job)
for _, jitem in ipairs(job.items) do
local item = jitem.item
if item and item:isWheelbarrow() then
if jitem.role ~= df.job_role_type.PushHaulVehicle then
return 'badrole'
end
local ref = dfhack.items.getSpecificRef(item, df.specific_ref_type.JOB)
if ref and ref.data.job == job then
return item
end
end
end
return nil
end
local function find_free_wheelbarrow(stockpile)
if not df.building_stockpilest:is_instance(stockpile) then return nil end
local sx, sy, sz = stockpile.centerx, stockpile.centery, stockpile.z
local found
for_each_item_in_radius(sx, sy, sz, state.radius*state.wheelbarrow_search_radius_k or 10*state.wheelbarrow_search_radius_k, function(it)
if it:isWheelbarrow() and not it.flags.in_job then
found = it
return true
end
end)
return found
end
local function attach_free_wheelbarrow(job)
local stockpile = get_job_stockpile(job)
if not stockpile then return nil end
local wheelbarrow = find_free_wheelbarrow(stockpile)
if not wheelbarrow then return nil end
if dfhack.job.attachJobItem(job, wheelbarrow,
df.job_role_type.PushHaulVehicle, -1, -1) then
if state.debug_enabled then
dfhack.gui.showAnnouncement('multihaul: adding wheelbarrow to a job', COLOR_CYAN)
end
return wheelbarrow
end
end
function finish_jobs_without_wheelbarrow()
local count = 0
for _, job in utils.listpairs(df.global.world.jobs.list) do
if job.job_type == df.job_type.StoreItemInStockpile and #job.items > 1 and not find_attached_wheelbarrow(job) then
for _, jobitem in ipairs(job.items) do
local item = jobitem.item
if item and item.flags.in_job then
local ref = dfhack.items.getSpecificRef(item, df.specific_ref_type.JOB)
if ref and ref.data.job == job then
dfhack.job.removeJob(job)
end
end
end
job.items:resize(0)
job.completion_timer = 0
count = count + 1
end
end
if count > 0 then
dfhack.gui.showAnnouncement('multihaul: clearing stuck hauling job', COLOR_CYAN)
end
end
local function on_new_job(job)
if job.job_type ~= df.job_type.StoreItemInStockpile then return end
local wheelbarrow = find_attached_wheelbarrow(job)
if wheelbarrow == 'badrole' then return
end
if not wheelbarrow and state.autowheelbarrows then
wheelbarrow = attach_free_wheelbarrow(job)
end
if not wheelbarrow then return end
add_nearby_items(job)
emptyContainedItems(wheelbarrow)
end
local function enable(val)
state.enabled = val
if state.enabled then
eventful.onJobInitiated[GLOBAL_KEY] = on_new_job
else
eventful.onJobInitiated[GLOBAL_KEY] = nil
end
persist_state()
end
if dfhack.internal.IN_TEST then
unit_test_hooks = {on_new_job=on_new_job, enable=enable,
load_state=load_state}
end
-- state change handler
dfhack.onStateChange[GLOBAL_KEY] = function(sc)
if sc == SC_MAP_UNLOADED then
state.enabled = false
eventful.onJobInitiated[GLOBAL_KEY] = nil
return
end
if sc == SC_MAP_LOADED then
load_state()
end
end
if dfhack_flags.module then
return
end
local args = {...}
if dfhack_flags.enable then
if dfhack_flags.enable_state then
enable(true)
else
enable(false)
end
return
end
local function parse_options(start_idx)
local i = start_idx
while i <= #args do
local a = args[i]
if a == '--debug' then
local m = args[i + 1]
if m == 'off' or m == 'disable' then
state.debug_enabled = false
i = i + 1
else
state.debug_enabled = true
end
elseif a == '--autowheelbarrows' then
local m = args[i + 1]
if m == 'on' or m == 'enable' then
state.autowheelbarrows = true
i = i + 1
elseif m == 'off' or m == 'disable' then
state.autowheelbarrows = false
i = i + 1
else
qerror('invalid autowheelbarrows option: ' .. tostring(m))
end
elseif a == '--radius' then
i = i + 1
state.radius = tonumber(args[i]) or state.radius
elseif a == '--max-items' then
i = i + 1
state.max_items = tonumber(args[i]) or state.max_items
elseif a == '--mode' then
i = i + 1
local m = args[i]
if m == 'any' or m == 'sametype' or m == 'samesubtype' or m == 'identical' then
state.mode = m
else
qerror('invalid mode: ' .. tostring(m))
end
end
i = i + 1
end
end
local cmd = args[1]
if cmd == 'enable' then
parse_options(2)
enable(true)
elseif cmd == 'disable' then
enable(false)
elseif cmd == 'status' or not cmd then
print((state.enabled and 'multihaul is enabled' or 'multihaul is disabled'))
print(('radius=%d max-items=%d mode=%s autowheelbarrows=%s debug=%s')
:format(state.radius, state.max_items, state.mode, state.autowheelbarrows and 'on' or 'off', state.debug_enabled and 'on' or 'off'))
elseif cmd == 'config' then
parse_options(2)
persist_state()
print(('multihaul config: radius=%d max-items=%d mode=%s autowheelbarrows=%s debug=%s')
:format(state.radius, state.max_items, state.mode, state.autowheelbarrows and 'on' or 'off', state.debug_enabled and 'on' or 'off'))
elseif cmd == 'unstuckjobs' then
finish_jobs_without_wheelbarrow()
else
qerror('Usage: multihaul [enable|disable|status|config|unstuckjobs] [--radius N] [--max-items N] [--mode MODE] [--autowheelbarrows on|off] [--debug on|off]')
end