From d388fbf665677ac5c08761a93e51521e36de0c47 Mon Sep 17 00:00:00 2001 From: Alistair-Afton Date: Thu, 17 Sep 2026 22:52:16 +0200 Subject: [PATCH] quickfix: repair unassigned site_id on reclaimed fortresses --- data/init/onMapLoad.default.init | 2 ++ docs/changelog.txt | 1 + library/lua/quickfix.lua | 17 +++++++++++++++++ test/modules/quickfix_fortress.lua | 26 ++++++++++++++++++++++++++ 4 files changed, 46 insertions(+) create mode 100644 test/modules/quickfix_fortress.lua diff --git a/data/init/onMapLoad.default.init b/data/init/onMapLoad.default.init index 44986a044c..fb5912fea4 100644 --- a/data/init/onMapLoad.default.init +++ b/data/init/onMapLoad.default.init @@ -4,3 +4,5 @@ # Please do not edit this file directly. It will be overwritten with new # defaults when you update DFHack. Instead, add your configuration to # dfhack-config/init/onMapLoad.init + +lua require('quickfix').repair_site_id() diff --git a/docs/changelog.txt b/docs/changelog.txt index da5a2d4d7c..e9f9c4c381 100644 --- a/docs/changelog.txt +++ b/docs/changelog.txt @@ -76,6 +76,7 @@ Template for new versions: - `timestream`: deal properly with units who have breathing difficulties - `stocks`: overlay now resets scroll position when collapsing categories so the item list is no longer left blank and unscrollable - Fixed persistent site data API (``dfhack.persistent.saveSiteData``/``getSiteData``) failing on newly reclaimed fortresses until the first save +- ``quickfix``: repair ``plotinfo.site_id`` on load when DF leaves it unassigned on reclaimed fortresses ## Misc Improvements - Added ``Coord2d`` and ``Coord3d`` C++ templates, providing a standard set operations for 2-tuples and 3-tuples of any numeric type diff --git a/library/lua/quickfix.lua b/library/lua/quickfix.lua index f46d03ef66..59880862b2 100644 --- a/library/lua/quickfix.lua +++ b/library/lua/quickfix.lua @@ -27,4 +27,21 @@ function set_entity_race_references() print(('quickfix: fixed %d unset entity race reference(s)'):format(count)) end +-- on reclaimed fortresses DF fails to assign plotinfo.site_id until the +-- first save, breaking anything that needs the site id; fortress_site is +-- set at embark, so restore the invariant from it +-- called from onMapLoad.default.init +function repair_site_id() + local plotinfo = df.global.plotinfo + if plotinfo.site_id ~= -1 then + return + end + local site = plotinfo.main.fortress_site + if not site then + return + end + plotinfo.site_id = site.id + print(('quickfix: repaired unassigned site_id (now %d)'):format(site.id)) +end + return _ENV diff --git a/test/modules/quickfix_fortress.lua b/test/modules/quickfix_fortress.lua new file mode 100644 index 0000000000..104ba9d650 --- /dev/null +++ b/test/modules/quickfix_fortress.lua @@ -0,0 +1,26 @@ +config.mode = 'fortress' +config.target = 'core' + +local quickfix = require('quickfix') + +function test.repair_site_id_restores_fortress_site() + local plotinfo = df.global.plotinfo + local orig = plotinfo.site_id + local site = plotinfo.main.fortress_site + expect.ne(nil, site, 'no fortress site on this map') + if not site then return end + + return dfhack.with_finalize(function() + plotinfo.site_id = orig + end, function() + plotinfo.site_id = -1 + quickfix.repair_site_id() + expect.eq(site.id, plotinfo.site_id) + end) +end + +function test.repair_site_id_noop_when_assigned() + local orig = df.global.plotinfo.site_id + quickfix.repair_site_id() + expect.eq(orig, df.global.plotinfo.site_id) +end