diff --git a/changelog.txt b/changelog.txt index cf8410eb5..8d939ae74 100644 --- a/changelog.txt +++ b/changelog.txt @@ -29,6 +29,7 @@ Template for new versions: ## New Tools ## New Features +- `gui/notify`: new notification type: stuck invasion; warns when all units of an announced invasion are stuck off the map, which blocks migrant waves, caravans, and diplomats; click for the option to run `fix/retrieve-units` ## Fixes - `exterminate`: now works in arena mode; all active arena units are valid targets (there is no friendly/dangerous distinction in the arena), corpses are skipped, and the adventure arena player unit is never mass-exterminated diff --git a/internal/notify/notifications.lua b/internal/notify/notifications.lua index f4fe56f81..7effa5ffc 100644 --- a/internal/notify/notifications.lua +++ b/internal/notify/notifications.lua @@ -427,6 +427,67 @@ function Cache_nemesis_all:get_affected_unit_ids() return self.ATTRS.affected_unit_ids end +-- migrant waves, caravans, and diplomats are deferred while an announced +-- (siege), underway (active) invasion from a non-layer-linked civ exists +local function invasion_defers_arrivals(invasion) + if not invasion.flags.active or not invasion.flags.siege then + return false + end + if invasion.civ_id < 0 then return false end + local entity = df.historical_entity.find(invasion.civ_id) + if not entity or not entity.entity_raw then return false end + return not entity.entity_raw.flags[df.entity_raw_flags.LAYER_LINKED] +end + +local stuck_invasion_cache = { + year = -1, + tick = -1, + stuck = {}, +} + +local function invalidate_stuck_invasion_cache() + stuck_invasion_cache.year = -1 + stuck_invasion_cache.tick = -1 + stuck_invasion_cache.stuck = {} +end + +-- detect announced invasions whose units are all stuck off the map. units +-- that are on the map but merely hidden from the player (e.g. ambushers) +-- are not inactive, so they correctly suppress this notification. the scan +-- is throttled since world.units.all can be large. +local function scan_stuck_invasions() + local year = dfhack.world.ReadCurrentYear() + local tick = dfhack.world.ReadCurrentTick() + if year == stuck_invasion_cache.year and tick < stuck_invasion_cache.tick + 200 then + return stuck_invasion_cache.stuck + end + invalidate_stuck_invasion_cache() + stuck_invasion_cache.year = year + stuck_invasion_cache.tick = tick + local blocking = {} + for _, invasion in ipairs(df.global.plotinfo.invasions.list) do + if invasion_defers_arrivals(invasion) then + blocking[invasion.id] = true + end + end + if next(blocking) then + local inv_has_onmap = {} + for _, unit in ipairs(df.global.world.units.all) do + if unit.invasion_id >= 0 and blocking[unit.invasion_id] and + not unit.flags1.inactive and not unit.flags1.dead + then + inv_has_onmap[unit.invasion_id] = true + end + end + for invasion_id in pairs(blocking) do + if not inv_has_onmap[invasion_id] then + table.insert(stuck_invasion_cache.stuck, invasion_id) + end + end + end + return stuck_invasion_cache.stuck +end + -- the order of this list controls the order the notifications will appear in the overlay NOTIFICATIONS_BY_IDX = { { @@ -526,6 +587,33 @@ NOTIFICATIONS_BY_IDX = { dlg.showMessage('Rescue stuck squads', message, COLOR_WHITE) end, }, + { + name='stuck_invasion', + desc='Notifies when an invasion is stuck off the map, blocking migrants and caravans.', + default=true, + dwarf_fn=function() + local stuck = scan_stuck_invasions() + if #stuck == 0 then return end + local text = #stuck == 1 and 'stuck invasion blocking arrivals' + or ('%d stuck invasions blocking arrivals'):format(#stuck) + return {{pen=COLOR_LIGHTRED, text=text}} + end, + on_click=function() + local message = + 'An invasion is stuck: all of its remaining units are trapped off\n' .. + 'the edge of the map and cannot enter or leave. Since the invasion\n' .. + 'cannot end, the game keeps deferring migrant waves, caravans, and\n' .. + 'diplomats, and your fort will stop receiving them.\n\n' .. + 'Running fix/retrieve-units retrieves units that are stuck off the\n' .. + 'map so that the invasion can be fought and resolved.\n\n' .. + 'Would you like to run fix/retrieve-units now?' + dlg.showYesNoPrompt('Stuck invasion blocking arrivals', message, COLOR_WHITE, + function() + dfhack.run_command('fix/retrieve-units') + invalidate_stuck_invasion_cache() + end) + end, + }, { name='auto_train', desc='Notifies when there are no squads set up for training', @@ -802,5 +890,6 @@ dfhack.onStateChange['internal/notify/notifications'] = function(event) or event == SC_MAP_LOADED or event == SC_MAP_UNLOADED then Cache_nemesis_all:invalidate() + invalidate_stuck_invasion_cache() end end