Skip to content

Commit

Permalink
Merge pull request #1293 from myk002/myk_assign
Browse files Browse the repository at this point in the history
[assign-minecarts] handle case where minecart is assigned but is missing
  • Loading branch information
myk002 authored Sep 6, 2024
2 parents 22675fc + b7d8a37 commit fe17323
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
24 changes: 14 additions & 10 deletions assign-minecarts.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
--@ module = true

local argparse = require('argparse')
local utils = require('utils')

function get_free_vehicles()
local free_vehicles = {}
Expand All @@ -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)
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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)))
Expand Down
1 change: 1 addition & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit fe17323

Please sign in to comment.