Skip to content

Commit fe17323

Browse files
authored
Merge pull request #1293 from myk002/myk_assign
[assign-minecarts] handle case where minecart is assigned but is missing
2 parents 22675fc + b7d8a37 commit fe17323

File tree

2 files changed

+15
-10
lines changed

2 files changed

+15
-10
lines changed

assign-minecarts.lua

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
--@ module = true
33

44
local argparse = require('argparse')
5+
local utils = require('utils')
56

67
function get_free_vehicles()
78
local free_vehicles = {}
@@ -13,16 +14,12 @@ function get_free_vehicles()
1314
return free_vehicles
1415
end
1516

16-
local function has_minecart(route)
17-
return #route.vehicle_ids > 0
18-
end
19-
2017
local function has_stops(route)
2118
return #route.stops > 0
2219
end
2320

2421
local function get_minecart(route)
25-
if not has_minecart(route) then return end
22+
if #route.vehicle_ids == 0 then return end
2623
local vehicle = utils.binsearch(df.global.world.vehicles.active, route.vehicle_ids[0], 'id')
2724
if not vehicle then return end
2825
return df.item.find(vehicle.item_id)
@@ -37,8 +34,9 @@ local function get_id_and_name(route)
3734
end
3835

3936
local function assign_minecart_to_route(route, quiet, minecart)
40-
if has_minecart(route) then
41-
return get_minecart(route)
37+
local assigned_minecart = get_minecart(route)
38+
if assigned_minecart then
39+
return assigned_minecart
4240
end
4341
if not has_stops(route) then
4442
if not quiet then
@@ -57,6 +55,12 @@ local function assign_minecart_to_route(route, quiet, minecart)
5755
return false
5856
end
5957
end
58+
for _,vehicle_id in ipairs(route.vehicle_ids) do
59+
local vehicle = utils.binsearch(df.global.world.vehicles.all, vehicle_id, 'id')
60+
if vehicle then vehicle.route_id = -1 end
61+
end
62+
route.vehicle_ids:resize(0)
63+
route.vehicle_stops:resize(0)
6064
route.vehicle_ids:insert('#', minecart.id)
6165
route.vehicle_stops:insert('#', 0)
6266
minecart.route_id = route.id
@@ -99,7 +103,7 @@ local function list()
99103
for _,route in ipairs(routes) do
100104
print(('%-8d %-9s %-9s %s')
101105
:format(route.id,
102-
has_minecart(route) and 'yes' or 'NO',
106+
get_minecart(route) and 'yes' or 'NO',
103107
has_stops(route) and 'yes' or 'NO',
104108
get_name(route)))
105109
end
@@ -113,7 +117,7 @@ local function all(quiet)
113117
local minecarts, idx = get_free_vehicles(), 1
114118
local routes = df.global.plotinfo.hauling.routes
115119
for _,route in ipairs(routes) do
116-
if has_minecart(route) then
120+
if get_minecart(route) then
117121
goto continue
118122
end
119123
if not assign_minecart_to_route(route, quiet, minecarts[idx]) then
@@ -148,7 +152,7 @@ local function main(args)
148152
local route = get_route_by_id(requested_route_id)
149153
if not route then
150154
dfhack.printerr('route id not found: '..requested_route_id)
151-
elseif has_minecart(route) then
155+
elseif get_minecart(route) then
152156
if not quiet then
153157
print(('Route %s already has a minecart assigned.')
154158
:format(get_id_and_name(route)))

changelog.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ Template for new versions:
5353
- `gui/sitemap`: show whether a unit is caged
5454
- `gui/control-panel`: include option for turning off dumping of old clothes for `tailor`, for players who have magma pit dumps and want to save old clothes from being dumped into the magma
5555
- `position`: report current historical era (e.g., "Age of Myth"), site/adventurer world coords, and mouse map tile coords
56+
- `assign-minecarts`: reassign vehicles to routes where the vehicle has been destroyed (or has otherwise gone missing)
5657

5758
## Documentation
5859
- `gui/embark-anywhere`: add information about how the game determines world tile pathability and instructions for bridging two landmasses

0 commit comments

Comments
 (0)