diff --git a/caravan.lua b/caravan.lua index c876e4b61b..c429cf7bbd 100644 --- a/caravan.lua +++ b/caravan.lua @@ -20,6 +20,8 @@ specified, then the commands apply to all caravans on the map. annoying merchants, etc.). Also causes caravans to return to the depot if applicable. - ``leave [IDS]``: makes caravans pack up and leave immediately. +- ``unload``: fixes endless unloading at the depot. Run this if merchant pack + animals were startled and now refuse to come to the trade depot. ]====] @@ -95,6 +97,51 @@ function commands.leave(...) end end +local function isDisconnectedPackAnimal(unit) + if unit.following then + local dragger = unit.following + return ( + unit.relationship_ids[ df.unit_relationship_type.Dragger ] == -1 and + dragger.relationship_ids[ df.unit_relationship_type.Draggee ] == -1 + ) + end +end + +local function getPrintableUnitName(unit) + local visible_name = dfhack.units.getVisibleName(unit) + local profession_name = dfhack.units.getProfessionName(unit) + if visible_name.has_name then + return ('%s (%s)'):format(dfhack.TranslateName(visible_name), profession_name) + end + return profession_name -- for unnamed animals +end + +local function rejoin_pack_animals() + print('Reconnecting disconnected pack animals...') + local found = false + for _, unit in pairs(df.global.world.units.active) do + if unit.flags1.merchant and isDisconnectedPackAnimal(unit) then + local dragger = unit.following + print((' %s <-> %s'):format( + dfhack.df2console(getPrintableUnitName(unit)), + dfhack.df2console(getPrintableUnitName(dragger)) + )) + unit.relationship_ids[ df.unit_relationship_type.Dragger ] = dragger.id + dragger.relationship_ids[ df.unit_relationship_type.Draggee ] = unit.id + found = true + end + end + if (found) then + print('All pack animals reconnected.') + else + print('No disconnected pack animals found.') + end +end + +function commands.unload(...) + rejoin_pack_animals() +end + function commands.help() print(dfhack.script_help()) end diff --git a/changelog.txt b/changelog.txt index 10760b55de..ff0a444dc9 100644 --- a/changelog.txt +++ b/changelog.txt @@ -28,6 +28,7 @@ that repo. ## Misc Improvements - `build-now`: buildings that were just designated with `buildingplan` are now handled properly instead of being skipped (as long as there are items available to build the buildings with) +- `caravan`: new ``unload`` command, fixes endless unloading at the depot, by reconnecting merchant pack animals that were disconnected from their owners. - `deteriorate`: new ``now`` command immediately deteriorates all items of the specified types. - `list-agreements`: now displays translated Guild Names, worshiped Deity, age of petition, and "man" to race-specific professions (e.g. "Craftsman" -> "Craftsdwarf") - `workorder`: doesn't check for an assigned manager anymore.