From 536edcfbfe6aa18b2711b638c954e43b6cdbebb7 Mon Sep 17 00:00:00 2001 From: master-spike Date: Sun, 27 Oct 2024 06:14:17 +0000 Subject: [PATCH] `emigration`: persists last cycle tick so that the behaviour is more consistent with respect to save-and-reloads --- changelog.txt | 1 + emigration.lua | 42 ++++++++++++++++++++++++++++++------------ 2 files changed, 31 insertions(+), 12 deletions(-) diff --git a/changelog.txt b/changelog.txt index 384f2560a..fc7ee69c1 100644 --- a/changelog.txt +++ b/changelog.txt @@ -40,6 +40,7 @@ Template for new versions: - `fix/loyaltycascade`: allow the fix to work on non-dwarven citizens - `control-panel`: fix setting numeric preferences from the commandline - `gui/quickfort`: fix build mode evluation rules to allow placement of various furniture and constructions on tiles with stair shapes or without orthagonal floor. +- `emigration`: save-and-reload no longer resets the emigration cycle timeout, making gameplay more consistent ## Misc Improvements - `control-panel`: Add realistic-melting tweak to control-panel registry diff --git a/emigration.lua b/emigration.lua index 3721535a3..1d1dea305 100644 --- a/emigration.lua +++ b/emigration.lua @@ -1,18 +1,27 @@ --@module = true --@enable = true +local utils = require('utils') + local GLOBAL_KEY = 'emigration' -- used for state change hooks and persistence -enabled = enabled or false +local function get_default_state() + return {enabled=false, last_cycle_tick=0} +end + +state = state or get_default_state() function isEnabled() - return enabled + return state.enabled end local function persist_state() - dfhack.persistent.saveSiteData(GLOBAL_KEY, {enabled=enabled}) + dfhack.persistent.saveSiteData(GLOBAL_KEY, state) end +local TICKS_PER_MONTH = 33600 +local TICKS_PER_YEAR = 12 * TICKS_PER_MONTH + function desireToStay(unit,method,civ_id) -- on a percentage scale local value = 100 - unit.status.current_soul.personality.stress / 5000 @@ -191,18 +200,26 @@ function checkmigrationnow() else for _, civ_id in pairs(merchant_civ_ids) do checkForDeserters('merchant', civ_id) end end + + state.last_cycle_tick = dfhack.world.ReadCurrentTick() + TICKS_PER_YEAR * dfhack.world.ReadCurrentYear() end local function event_loop() - if enabled then - checkmigrationnow() - dfhack.timeout(1, 'months', event_loop) + if state.enabled then + local current_tick = dfhack.world.ReadCurrentTick() + TICKS_PER_YEAR * dfhack.world.ReadCurrentYear() + if current_tick - state.last_cycle_tick < TICKS_PER_MONTH then + local timeout_ticks = state.last_cycle_tick - current_tick + TICKS_PER_MONTH + dfhack.timeout(timeout_ticks, 'ticks', event_loop) + else + checkmigrationnow() + dfhack.timeout(1, 'months', event_loop) + end end end dfhack.onStateChange[GLOBAL_KEY] = function(sc) if sc == SC_MAP_UNLOADED then - enabled = false + state.enabled = false return end @@ -210,8 +227,9 @@ dfhack.onStateChange[GLOBAL_KEY] = function(sc) return end - local persisted_data = dfhack.persistent.getSiteData(GLOBAL_KEY, {enabled=false}) - enabled = persisted_data.enabled + state = get_default_state() + utils.assign(state, dfhack.persistent.getSiteData(GLOBAL_KEY, state)) + event_loop() end @@ -230,11 +248,11 @@ if dfhack_flags and dfhack_flags.enable then end if args[1] == "enable" then - enabled = true + state.enabled = true elseif args[1] == "disable" then - enabled = false + state.enabled = false else - print('emigration is ' .. (enabled and 'enabled' or 'not enabled')) + print('emigration is ' .. (state.enabled and 'enabled' or 'not enabled')) return end