|
| 1 | +--@ module=true |
| 2 | + |
| 3 | +local utils = require('utils') |
| 4 | + |
| 5 | +-- from observing bugged saves, this condition appears to be unique to stuck armies |
| 6 | +local function is_army_stuck(army) |
| 7 | + return army.controller_id ~= 0 and not army.controller |
| 8 | +end |
| 9 | + |
| 10 | +-- if army is currently camping, we'll need to go up the chain |
| 11 | +local function get_top_controller(controller) |
| 12 | + if not controller then return end |
| 13 | + if controller.master_id == controller.id then return controller end |
| 14 | + return df.army_controller.find(controller.master_id) |
| 15 | +end |
| 16 | + |
| 17 | +local function is_army_valid_and_returning(army) |
| 18 | + local controller = get_top_controller(army.controller) |
| 19 | + if not controller or controller.goal ~= df.army_controller_goal_type.SITE_INVASION then |
| 20 | + return false, false |
| 21 | + end |
| 22 | + return true, controller.data.goal_site_invasion.flag.RETURNING_HOME |
| 23 | +end |
| 24 | + |
| 25 | +-- need to check all squad positions since some members may have died |
| 26 | +local function get_squad_army(squad) |
| 27 | + if not squad then return end |
| 28 | + for _,sp in ipairs(squad.positions) do |
| 29 | + local hf = df.historical_figure.find(sp.occupant) |
| 30 | + if not hf then goto continue end |
| 31 | + local army = df.army.find(hf.info and hf.info.whereabouts and hf.info.whereabouts.army_id or -1) |
| 32 | + if army then return army end |
| 33 | + ::continue:: |
| 34 | + end |
| 35 | +end |
| 36 | + |
| 37 | +-- called by gui/notify notification |
| 38 | +function scan_fort_armies() |
| 39 | + local stuck_armies, outbound_army, returning_army = {}, nil, nil |
| 40 | + local govt = df.historical_entity.find(df.global.plotinfo.group_id) |
| 41 | + if not govt then return stuck_armies, outbound_army, returning_army end |
| 42 | + |
| 43 | + for _,squad_id in ipairs(govt.squads) do |
| 44 | + local squad = df.squad.find(squad_id) |
| 45 | + local army = get_squad_army(squad) |
| 46 | + if not army then goto continue end |
| 47 | + if is_army_stuck(army) then |
| 48 | + table.insert(stuck_armies, {squad=squad, army=army}) |
| 49 | + elseif not returning_army then |
| 50 | + local valid, returning = is_army_valid_and_returning(army) |
| 51 | + if valid then |
| 52 | + if returning then |
| 53 | + returning_army = {squad=squad, army=army} |
| 54 | + else |
| 55 | + outbound_army = {squad=squad, army=army} |
| 56 | + end |
| 57 | + end |
| 58 | + end |
| 59 | + ::continue:: |
| 60 | + end |
| 61 | + return stuck_armies, outbound_army, returning_army |
| 62 | +end |
| 63 | + |
| 64 | +local function unstick_armies() |
| 65 | + local stuck_armies, outbound_army, returning_army = scan_fort_armies() |
| 66 | + if #stuck_armies == 0 then return end |
| 67 | + if not returning_army then |
| 68 | + local instructions = outbound_army |
| 69 | + and ('Please wait for %s to complete their objective and run this command again when they are on their way home.'):format( |
| 70 | + dfhack.df2console(dfhack.military.getSquadName(outbound_army.squad.id))) |
| 71 | + or 'Please send a squad out on a mission that will return to the fort, and'.. |
| 72 | + ' run this command again when they are on the way home.' |
| 73 | + qerror(('%d stuck arm%s found, but no returning armies found to rescue them!\n%s'):format( |
| 74 | + #stuck_armies, #stuck_armies == 1 and 'y' or 'ies', instructions)) |
| 75 | + return |
| 76 | + end |
| 77 | + local returning_squad_name = dfhack.df2console(dfhack.military.getSquadName(returning_army.squad.id)) |
| 78 | + for _,stuck in ipairs(stuck_armies) do |
| 79 | + print(('fix/stuck-squad: Squad rescue operation underway! %s is rescuing %s'):format( |
| 80 | + returning_squad_name, dfhack.military.getSquadName(stuck.squad.id))) |
| 81 | + for _,member in ipairs(stuck.army.members) do |
| 82 | + local nemesis = df.nemesis_record.find(member.nemesis_id) |
| 83 | + if not nemesis or not nemesis.figure then goto continue end |
| 84 | + local hf = nemesis.figure |
| 85 | + if hf.info and hf.info.whereabouts then |
| 86 | + hf.info.whereabouts.army_id = returning_army.army.id |
| 87 | + end |
| 88 | + utils.insert_sorted(returning_army.army.members, member, 'nemesis_id') |
| 89 | + ::continue:: |
| 90 | + end |
| 91 | + stuck.army.members:resize(0) |
| 92 | + utils.insert_sorted(get_top_controller(returning_army.army.controller).assigned_squads, stuck.squad.id) |
| 93 | + end |
| 94 | +end |
| 95 | + |
| 96 | +if dfhack_flags.module then |
| 97 | + return |
| 98 | +end |
| 99 | + |
| 100 | +unstick_armies() |
0 commit comments