forked from DFHack/scripts
-
Notifications
You must be signed in to change notification settings - Fork 1
/
assign-minecarts.lua
167 lines (148 loc) · 4.73 KB
/
assign-minecarts.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
-- assigns minecarts to hauling routes
--@ module = true
local argparse = require('argparse')
function get_free_vehicles()
local free_vehicles = {}
for _,vehicle in ipairs(df.global.world.vehicles.active) do
if vehicle and vehicle.route_id == -1 then
table.insert(free_vehicles, vehicle)
end
end
return free_vehicles
end
local function has_minecart(route)
return #route.vehicle_ids > 0
end
local function has_stops(route)
return #route.stops > 0
end
local function get_minecart(route)
if not has_minecart(route) then return end
local vehicle = utils.binsearch(df.global.world.vehicles.active, route.vehicle_ids[0], 'id')
if not vehicle then return end
return df.item.find(vehicle.item_id)
end
local function get_name(route)
return route.name and #route.name > 0 and route.name or ('Route '..route.id)
end
local function get_id_and_name(route)
return ('%d (%s)'):format(route.id, get_name(route))
end
local function assign_minecart_to_route(route, quiet, minecart)
if has_minecart(route) then
return get_minecart(route)
end
if not has_stops(route) then
if not quiet then
dfhack.printerr(
('Route %s has no stops defined. Cannot assign minecart.')
:format(get_id_and_name(route)))
end
return false
end
if not minecart then
minecart = get_free_vehicles()[1]
if not minecart then
if not quiet then
dfhack.printerr('No minecarts available! Please build some.')
end
return false
end
end
route.vehicle_ids:insert('#', minecart.id)
route.vehicle_stops:insert('#', 0)
minecart.route_id = route.id
if not quiet then
print(('Assigned a minecart to route %s.')
:format(get_id_and_name(route)))
end
return df.item.find(minecart.item_id)
end
-- assign first free minecart to the most recently-created route
-- returns assigned minecart (or nil if assignment failed)
function assign_minecart_to_last_route(quiet)
local routes = df.global.plotinfo.hauling.routes
local route_idx = #routes - 1
if route_idx < 0 then
return false
end
local route = routes[route_idx]
return assign_minecart_to_route(route, quiet)
end
local function get_route_by_id(route_id)
for _,route in ipairs(df.global.plotinfo.hauling.routes) do
if route.id == route_id then
return route
end
end
end
local function list()
local routes = df.global.plotinfo.hauling.routes
if 0 == #routes then
print('No hauling routes defined.')
else
print(('Found %d route%s:\n')
:format(#routes, #routes == 1 and '' or 's'))
print('route id minecart? has stops? route name')
print('-------- --------- ---------- ----------')
for _,route in ipairs(routes) do
print(('%-8d %-9s %-9s %s')
:format(route.id,
has_minecart(route) and 'yes' or 'NO',
has_stops(route) and 'yes' or 'NO',
get_name(route)))
end
end
local minecarts = get_free_vehicles()
print(('\nYou have %d unassigned minecart%s.')
:format(#minecarts, #minecarts == 1 and '' or 's'))
end
local function all(quiet)
local minecarts, idx = get_free_vehicles(), 1
local routes = df.global.plotinfo.hauling.routes
for _,route in ipairs(routes) do
if has_minecart(route) then
goto continue
end
if not assign_minecart_to_route(route, quiet, minecarts[idx]) then
return
end
idx = idx + 1
::continue::
end
end
local function do_help(_)
print(dfhack.script_help())
end
local command_switch = {
list=list,
all=all,
}
local function main(args)
local help, quiet = false, false
local command = argparse.processArgsGetopt(args, {
{'h', 'help', handler=function() help = true end},
{'q', 'quiet', handler=function() quiet = true end}})[1]
if help then
command = nil
end
local requested_route_id = tonumber(command)
if requested_route_id then
local route = get_route_by_id(requested_route_id)
if not route then
dfhack.printerr('route id not found: '..requested_route_id)
elseif has_minecart(route) then
if not quiet then
print(('Route %s already has a minecart assigned.')
:format(get_id_and_name(route)))
end
else
assign_minecart_to_route(route, quiet)
end
return
end
(command_switch[command] or do_help)(quiet)
end
if not dfhack_flags.module then
main({...})
end