diff --git a/changelog.txt b/changelog.txt index 8876a3b0ed..5b464cafee 100644 --- a/changelog.txt +++ b/changelog.txt @@ -32,6 +32,8 @@ Template for new versions: ## Fixes +- `fix/loyaltycascade`: guard against citizens that are not historical figures and emit a warning. + ## Misc Improvements - `caravan`: the ``Bring goods to depot``, ``Trade``, and ``Assign items for display`` overlays now allow searching for items with non-ASCII characters in their description - `caravan`: the ``Trade`` overlay will use the trader's appraisal skill instead of the broker's to round/obfuscate the value of items diff --git a/fix/loyaltycascade.lua b/fix/loyaltycascade.lua index 3409750cb4..bcf5b51cd5 100644 --- a/fix/loyaltycascade.lua +++ b/fix/loyaltycascade.lua @@ -4,7 +4,7 @@ local makeown = reqscript('makeown') -- Checks if a unit is a former member of a given entity as well as it's --- current enemy. +-- current enemy (assumes valid historical figure with entity links). local function getUnitRenegade(unit, entity_id) local unit_entity_links = df.historical_figure.find(unit.hist_figure_id).entity_links local former_index = nil @@ -29,6 +29,8 @@ local function getUnitRenegade(unit, entity_id) return former_index, enemy_index end +-- convert unit to new entity +-- (assumes valid historical figure with entity links) local function convertUnit(unit, entity_id, former_index, enemy_index) local unit_entity_links = df.historical_figure.find(unit.hist_figure_id).entity_links @@ -46,6 +48,13 @@ local function fixUnit(unit) local fixed = false local unit_name = dfhack.units.getReadableName(unit) + + -- utility functions assume valid historical figure id + if not df.historical_figure.find(unit.hist_figure_id) then + print(('%s: skipping citizen without historical figure: %s'):format(dfhack.current_script_name(), unit_name)) + return + end + local former_civ_index, enemy_civ_index = getUnitRenegade(unit, df.global.plotinfo.civ_id) local former_group_index, enemy_group_index = getUnitRenegade(unit, df.global.plotinfo.group_id)