Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
89 changes: 89 additions & 0 deletions internal/notify/notifications.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
{
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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
Loading