-
Notifications
You must be signed in to change notification settings - Fork 5
/
hauling.lua
538 lines (423 loc) · 13.7 KB
/
hauling.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
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
--luacheck: in=
function hauling_get_routes()
return execute_with_hauling_menu(function(ws)
local ret = {}
for i,route in ipairs(df.global.ui.hauling.routes) do
local name = haulingroutename(route)
local bad = false
for j,w in ipairs(df.global.ui.hauling.view_bad) do
if df.global.ui.hauling.view_routes[j] == route and istrue(w) then
bad = true
break
end
end
table.insert(ret, { name, route.id, bad })
end
return ret
end)
end
--luacheck: in=number
function hauling_route_info(id)
local route = df.hauling_route.find(id)
if not route then
error('no route '..tostring(id))
end
return execute_with_hauling_menu(function(ws)
local stops = {}
for i,v in ipairs(df.global.ui.hauling.view_stops) do
if v and df.global.ui.hauling.view_routes[i] == route then
table.insert(stops, { stopname(v), v.id, istrue(df.global.ui.hauling.view_bad[i]) })
end
end
local vehicle_info
if #route.vehicle_ids > 0 then
--todo: can be > 1 vehicle ?!
local vehicle = df.vehicle.find(route.vehicle_ids[0])
local vehicle_item = vehicle and df.item.find(vehicle.item_id)
if vehicle_item then
local vehicle_stop = #route.stops > 0 and route.vehicle_stops[0] ~= -1 and route.stops[route.vehicle_stops[0]] or nil
local on_stop = vehicle_stop and (vehicle_stop.pos.x == vehicle_item.pos.x and vehicle_stop.pos.y == vehicle_item.pos.y and vehicle_stop.pos.z == vehicle_item.pos.z) or false
local contained_volume = 0
for i,ref in ipairs(vehicle_item.general_refs) do
if ref._type == df.general_ref_contains_itemst then
local item = df.item.find(ref.item_id)
if item then
contained_volume = contained_volume + item:getVolume()
end
end
end
local fullness = math.ceil(contained_volume / 50000 * 100)
vehicle_info = { itemname(vehicle_item, 1, true), vehicle_item.id, vehicle_stop and vehicle_stop.id or -1, on_stop, fullness }
end
end
return { haulingroutename(route), route.id, stops, vehicle_info or mp.NIL }
end)
end
--luacheck: in=number,number
function hauling_stop_info(routeid, stopid)
local route = df.hauling_route.find(routeid)
if not route then
error('no route '..tostring(routeid))
end
local _,stop = utils.linear_index(route.stops, stopid, 'id')
if not stop then
error('no stop '..tostring(stopid))
end
local conditions = {}
for i,v in ipairs(stop.conditions) do
table.insert(conditions, { v.direction, v.mode, v.load_percent, v.timeout/1200, v.flags.whole });
end
local stockpiles = {}
for i,v in ipairs(stop.stockpiles) do
local bld = df.building.find(v.building_id)
local name = bld and bldname(bld) or '#unknown stockpile#' --todo: handle -1 differently?
table.insert(stockpiles, { name, v.building_id, v.mode.whole });
end
return { stopname(stop), stop.id, conditions, stockpiles }
end
--luacheck: in=number,number,number
function hauling_stop_delete_condition(routeid, stopid, idx)
local route = df.hauling_route.find(routeid)
if not route then
error('no route '..tostring(routeid))
end
for i,stop in ipairs(route.stops) do
if stop.id == stopid then
stop.conditions:erase(idx)
return true
end
end
error('no stop '..tostring(stopid))
end
--luacheck: in=number,number,number
function hauling_stop_link_delete(routeid, stopid, idx)
local route = df.hauling_route.find(routeid)
if not route then
error('no route '..tostring(routeid))
end
for i,stop in ipairs(route.stops) do
if stop.id == stopid then
stop.stockpiles:erase(idx)
return true
end
end
error('no stop '..tostring(stopid))
end
--luacheck: in=number,number,number,number
function hauling_stop_link_set_mode(routeid, stopid, idx, mode)
local route = df.hauling_route.find(routeid)
if not route then
error('no route '..tostring(routeid))
end
for i,stop in ipairs(route.stops) do
if stop.id == stopid then
stop.stockpiles[idx].mode.whole = mode
return true
end
end
error('no stop '..tostring(stopid))
end
--luacheck: in=number
function hauling_route_delete(id)
execute_with_hauling_menu(function(ws)
for i,v in ipairs(df.global.ui.hauling.view_routes) do
if v.id == id then
df.global.ui.hauling.cursor_top = i
gui.simulateInput(ws, K'D_HAULING_REMOVE')
return true
end
end
end)
end
--luacheck: in=number,number
function hauling_stop_add()
local ws = dfhack.gui.getCurViewscreen()
if ws._type ~= df.viewscreen_dwarfmodest then
error(errmsg_wrongscreen(ws))
end
if df.global.ui.main.mode ~= df.ui_sidebar_mode.Hauling then
error('invalid mode '..tostring(df.global.ui.main.mode))
end
local hauling = df.global.ui.hauling
if hauling.routes == 0 then
error('no routes')
end
--todo: don't add two stops in one place
gui.simulateInput(ws, K'D_HAULING_NEW_STOP')
return true
end
--luacheck: in=number,number
function hauling_stop_delete(routeid, stopid)
return execute_with_hauling_menu(function(ws)
for i,v in ipairs(df.global.ui.hauling.view_stops) do
if v and v.id == stopid and df.global.ui.hauling.view_routes[i].id == routeid then
df.global.ui.hauling.cursor_top = i
gui.simulateInput(ws, K'D_HAULING_REMOVE')
return true
end
end
end)
end
--luacheck: in=
function hauling_stop_delete_current()
local ws = dfhack.gui.getCurViewscreen()
if ws._type ~= df.viewscreen_dwarfmodest then
error(errmsg_wrongscreen(ws))
end
if df.global.ui.main.mode ~= df.ui_sidebar_mode.Hauling then
error('invalid mode '..tostring(df.global.ui.main.mode))
end
local hauling = df.global.ui.hauling
if not hauling.view_stops[hauling.cursor_top] then
error('no stop selected')
end
--todo: make sure the same route stays selected after deleting a stop
local was_last = hauling.cursor_top == #hauling.view_stops-1 or not hauling.view_stops[hauling.cursor_top+1]
gui.simulateInput(ws, K'D_HAULING_REMOVE')
if hauling.view_stops[hauling.cursor_top] and not was_last then
hauling.cursor_top = hauling.cursor_top - 1
end
return true
end
--luacheck: in=number
function hauling_vehicle_get_choices(routeid)
return execute_with_hauling_menu(function(ws)
for i,v in ipairs(df.global.ui.hauling.view_routes) do
print(v.id)
if v.id == routeid then
df.global.ui.hauling.cursor_top = i
gui.simulateInput(ws, K'D_HAULING_VEHICLE')
if not df.global.ui.hauling.in_assign_vehicle then
error('could not switch to vehicle assign mode')
end
local ret = {}
for i,vehicle in ipairs(df.global.ui.hauling.vehicles) do
if not vehicle then
table.insert(ret, { 'None', -1, -1, mp.NIL })
else
local item = df.item.find(vehicle.item_id)
local itemname = item and itemname(item, 1, true)
local assigned_route = vehicle.route_id ~= -1 and df.hauling_route.find(vehicle.route_id)
table.insert(ret, { itemname, vehicle.id, item.id, assigned_route and haulingroutename(assigned_route) or mp.NIL })
end
end
return ret
end
end
error('no route with id '..tostring(routeid))
end)
end
--luacheck: in=number,number
function hauling_vehicle_assign(routeid, vehicleid)
return execute_with_hauling_menu(function(ws)
for i,v in ipairs(df.global.ui.hauling.view_routes) do
if v.id == routeid then
df.global.ui.hauling.cursor_top = i
gui.simulateInput(ws, K'D_HAULING_VEHICLE')
if not df.global.ui.hauling.in_assign_vehicle then
error('could not switch to vehicle assign mode')
end
local ret = {}
for j,vehicle in ipairs(df.global.ui.hauling.vehicles) do
if (not vehicle and vehicleid == -1) or (vehicle and vehicle.id == vehicleid) then
df.global.ui.hauling.cursor_vehicle = j
gui.simulateInput(ws, K'SELECT')
return true
end
end
error('no vehicle with id '..tostring(vehicleid))
end
end
error('no route with id '..tostring(routeid))
end)
end
--luacheck: in=
function hauling_route_add()
return execute_with_hauling_menu(function(ws)
gui.simulateInput(ws, K'D_HAULING_NEW_ROUTE')
end)
end
--luacheck: in=number,string
function hauling_route_set_name(id, name)
local route = df.hauling_route.find(id)
if not route then
error('no route '..tostring(id))
end
route.name = dfhack.utf2df(name)
end
--luacheck: in=number,bool
function hauling_route_start_edit(id, zoom)
zoom = istrue(zoom)
for i,route in ipairs(df.global.ui.hauling.view_routes) do
if route.id == id then
return execute_with_hauling_menu(function(ws)
df.global.ui.hauling.cursor_top = i
if zoom and #route.stops > 0 then
local minx = 999
local miny = 999
local maxx = 0
local maxy = 0
local bz = route.stops[0].pos.z
for j,stop in ipairs(route.stops) do
local pos = stop.pos
if pos.z == bz then
if pos.x < minx then
minx = pos.x
end
if pos.x > maxx then
maxx = pos.x
end
if pos.y < miny then
miny = pos.y
end
if pos.y > maxy then
maxy = pos.y
end
end
end
local cx = math.floor((minx+maxx)/2)
local cy = math.floor((miny+maxy)/2)
recenter_view(cx, cy, bz)
end
return true
end, true)
end
end
error('no route '..tostring(id))
end
--luacheck: in=
function hauling_route_end_edit()
reset_main()
end
--luacheck: in=number,number,number
function hauling_reorder_stops(id, fromidx, toidx)
local route = df.hauling_route.find(id)
if not route then
error('no hauling route '..tostring(id))
end
--todo: use UI ?
--todo: can be > 1 vehicle ?!
local vehicle_stop = route.stops[route.vehicle_stops[0]]
local stop = route.stops[fromidx]
route.stops:erase(fromidx)
route.stops:insert(toidx, stop)
route.vehicle_stops[0] = utils.linear_index(route.stops, vehicle_stop)
return true
end
hauling_linking_source = nil
function restore_after_hauling_linking()
hauling_linking_source = nil
end
--luacheck: in=number,number
function hauling_stop_linking_begin(routeid, stopid)
local ws = dfhack.gui.getCurViewscreen()
if ws._type ~= df.viewscreen_dwarfmodest then
error(errmsg_wrongscreen(ws))
end
local route = df.hauling_route.find(routeid)
if not route then
error('no hauling route '..tostring(routeid))
end
local _,stop = utils.linear_index(route.stops, stopid, 'id')
if not stop then
error('no stop '..tostring(stopid))
end
query_building()
hauling_linking_source = { routeid=route.id, stopid=stop.id }
return true
end
--luacheck: in=
function hauling_stop_linking_ok()
if not hauling_linking_source then
error('not linking hauling stop')
end
local bld = df.global.world.selected_building
if bld._type ~= df.building_stockpilest then
error('not a stockpile '..tostring(bld))
end
local route = df.hauling_route.find(hauling_linking_source.routeid)
if not route then
error('no hauling route '..tostring(hauling_linking_source.routeid))
end
local _,stop = utils.linear_index(route.stops, hauling_linking_source.stopid, 'id')
if not stop then
error('no stop '..tostring(stopid))
end
local link = df.route_stockpile_link:new()
link.building_id = bld.id
link.mode.take = true
stop.stockpiles:insert('#', link)
bld.linked_stops:insert('#', stop)
df.global.ui.main.mode = df.ui_sidebar_mode.Default
local ret = { hauling_linking_source.routeid, hauling_linking_source.stopid }
restore_after_hauling_linking()
return ret
end
--luacheck: in=
function hauling_stop_linking_cancel()
if not hauling_linking_source then
error('not linking hauling stop')
end
df.global.ui.main.mode = df.ui_sidebar_mode.Default
local ret = { hauling_linking_source.routeid, hauling_linking_source.stopid }
restore_after_hauling_linking()
return ret
end
--luacheck: in=number,number
function hauling_stop_edit_item_settings(routeid, stopid)
stockpile_editing_settings = nil
local route = df.hauling_route.find(routeid)
if not route then
error('no hauling route '..tostring(routeid))
end
local _,stop = utils.linear_index(route.stops, stopid, 'id')
if not stop then
error('no stop '..tostring(stopid))
end
stockpile_editing_settings = stop.settings
return true
end
--luacheck: in=number,number,number,number[]
function hauling_stop_save_condition(routeid, stopid, idx, values)
local route = df.hauling_route.find(routeid)
if not route then
error('no hauling route '..tostring(routeid))
end
local _,stop = utils.linear_index(route.stops, stopid, 'id')
if not stop then
error('no stop '..tostring(stopid))
end
local cond
if idx == -1 then
cond = df.stop_depart_condition:new()
stop.conditions:insert('#', cond)
else
cond = stop.conditions[idx]
end
--table.insert(conditions, { v.direction, v.mode, v.load_percent, v.timeout/1200, v.flags.whole });
cond.direction = values[1]
cond.mode = values[2]
cond.load_percent = values[3]
cond.timeout = values[4] * 1200
cond.flags.whole = values[5]
return true
end
--luacheck: in=number,number,string
function hauling_stop_set_name(routeid, stopid, name)
local route = df.hauling_route.find(routeid)
if not route then
error('no hauling route '..tostring(routeid))
end
local _,stop = utils.linear_index(route.stops, stopid, 'id')
if not stop then
error('no stop '..tostring(stopid))
end
stop.name = dfhack.utf2df(name)
end
--print(pcall(function()return json:encode(hauling_get_routes())end))
--print(pcall(function()return json:encode(hauling_route_info(1))end))
--print(pcall(function()return json:encode(hauling_vehicle_get_choices(2))end))
--print(pcall(function()return json:encode(hauling_vehicle_assign(2,48))end))
--print(pcall(function()return json:encode(hauling_route_new())end))
--print(pcall(function()return json:encode(hauling_route_set_name(130,''))end))
-- print(pcall(function()return json:encode(hauling_route_start_edit(2,true))end))