diff --git a/assign-minecarts.lua b/assign-minecarts.lua index 3cff2cd72..53e498d2b 100644 --- a/assign-minecarts.lua +++ b/assign-minecarts.lua @@ -2,6 +2,7 @@ --@ module = true local argparse = require('argparse') +local utils = require('utils') function get_free_vehicles() local free_vehicles = {} @@ -13,16 +14,12 @@ function get_free_vehicles() 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 + if #route.vehicle_ids == 0 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) @@ -37,8 +34,9 @@ local function get_id_and_name(route) end local function assign_minecart_to_route(route, quiet, minecart) - if has_minecart(route) then - return get_minecart(route) + local assigned_minecart = get_minecart(route) + if assigned_minecart then + return assigned_minecart end if not has_stops(route) then if not quiet then @@ -57,6 +55,12 @@ local function assign_minecart_to_route(route, quiet, minecart) return false end end + for _,vehicle_id in ipairs(route.vehicle_ids) do + local vehicle = utils.binsearch(df.global.world.vehicles.all, vehicle_id, 'id') + if vehicle then vehicle.route_id = -1 end + end + route.vehicle_ids:resize(0) + route.vehicle_stops:resize(0) route.vehicle_ids:insert('#', minecart.id) route.vehicle_stops:insert('#', 0) minecart.route_id = route.id @@ -99,7 +103,7 @@ local function list() for _,route in ipairs(routes) do print(('%-8d %-9s %-9s %s') :format(route.id, - has_minecart(route) and 'yes' or 'NO', + get_minecart(route) and 'yes' or 'NO', has_stops(route) and 'yes' or 'NO', get_name(route))) end @@ -113,7 +117,7 @@ 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 + if get_minecart(route) then goto continue end if not assign_minecart_to_route(route, quiet, minecarts[idx]) then @@ -148,7 +152,7 @@ local function main(args) 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 + elseif get_minecart(route) then if not quiet then print(('Route %s already has a minecart assigned.') :format(get_id_and_name(route))) diff --git a/changelog.txt b/changelog.txt index 52c4700c9..50b06fbbd 100644 --- a/changelog.txt +++ b/changelog.txt @@ -53,6 +53,7 @@ Template for new versions: - `gui/sitemap`: show whether a unit is caged - `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 - `position`: report current historical era (e.g., "Age of Myth"), site/adventurer world coords, and mouse map tile coords +- `assign-minecarts`: reassign vehicles to routes where the vehicle has been destroyed (or has otherwise gone missing) ## Documentation - `gui/embark-anywhere`: add information about how the game determines world tile pathability and instructions for bridging two landmasses