Skip to content

Commit cb03157

Browse files
committed
emigration: persists last cycle tick so that the behaviour is more consistent with respect to save-and-reloads
1 parent 80e1a98 commit cb03157

File tree

2 files changed

+29
-12
lines changed

2 files changed

+29
-12
lines changed

changelog.txt

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ Template for new versions:
4040
- `fix/loyaltycascade`: allow the fix to work on non-dwarven citizens
4141
- `control-panel`: fix setting numeric preferences from the commandline
4242
- `gui/quickfort`: fix build mode evluation rules to allow placement of various furniture and constructions on tiles with stair shapes or without orthagonal floor.
43+
- `emigration`: save-and-reload no longer resets the emigration cycle timeout, making gameplay more consistent
4344

4445
## Misc Improvements
4546
- `control-panel`: Add realistic-melting tweak to control-panel registry

emigration.lua

+28-12
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,23 @@
33

44
local GLOBAL_KEY = 'emigration' -- used for state change hooks and persistence
55

6-
enabled = enabled or false
6+
local function get_default_state()
7+
return {enabled=false, last_cycle_tick=0}
8+
end
9+
10+
state = state or get_default_state()
711

812
function isEnabled()
9-
return enabled
13+
return state.enabled
1014
end
1115

1216
local function persist_state()
13-
dfhack.persistent.saveSiteData(GLOBAL_KEY, {enabled=enabled})
17+
dfhack.persistent.saveSiteData(GLOBAL_KEY, state)
1418
end
1519

20+
local TICKS_PER_MONTH = 33600
21+
local TICKS_PER_YEAR = 12 * TICKS_PER_MONTH
22+
1623
function desireToStay(unit,method,civ_id)
1724
-- on a percentage scale
1825
local value = 100 - unit.status.current_soul.personality.stress / 5000
@@ -191,27 +198,36 @@ function checkmigrationnow()
191198
else
192199
for _, civ_id in pairs(merchant_civ_ids) do checkForDeserters('merchant', civ_id) end
193200
end
201+
202+
state.last_cycle_tick = dfhack.world.ReadCurrentTick() + TICKS_PER_YEAR * dfhack.world.ReadCurrentYear()
194203
end
195204

196205
local function event_loop()
197-
if enabled then
198-
checkmigrationnow()
199-
dfhack.timeout(1, 'months', event_loop)
206+
if state.enabled then
207+
local current_tick = dfhack.world.ReadCurrentTick() + TICKS_PER_YEAR * dfhack.world.ReadCurrentYear()
208+
if current_tick - state.last_cycle_tick < TICKS_PER_MONTH then
209+
local timeout_ticks = state.last_cycle_tick - current_tick + TICKS_PER_MONTH
210+
dfhack.timeout(timeout_ticks, 'ticks', event_loop)
211+
else
212+
checkmigrationnow()
213+
dfhack.timeout(1, 'months', event_loop)
214+
end
200215
end
201216
end
202217

203218
dfhack.onStateChange[GLOBAL_KEY] = function(sc)
204219
if sc == SC_MAP_UNLOADED then
205-
enabled = false
220+
state.enabled = false
206221
return
207222
end
208223

209224
if sc ~= SC_MAP_LOADED or df.global.gamemode ~= df.game_mode.DWARF then
210225
return
211226
end
212227

213-
local persisted_data = dfhack.persistent.getSiteData(GLOBAL_KEY, {enabled=false})
214-
enabled = persisted_data.enabled
228+
state = get_default_state()
229+
utils.assign(state, dfhack.persistent.getSiteData(GLOBAL_KEY, state))
230+
215231
event_loop()
216232
end
217233

@@ -230,11 +246,11 @@ if dfhack_flags and dfhack_flags.enable then
230246
end
231247

232248
if args[1] == "enable" then
233-
enabled = true
249+
state.enabled = true
234250
elseif args[1] == "disable" then
235-
enabled = false
251+
state.enabled = false
236252
else
237-
print('emigration is ' .. (enabled and 'enabled' or 'not enabled'))
253+
print('emigration is ' .. (state.enabled and 'enabled' or 'not enabled'))
238254
return
239255
end
240256

0 commit comments

Comments
 (0)