Skip to content

Commit 33c6bce

Browse files
authored
Merge pull request #1330 from master-spike/emigration_timeout_persist
Persist the last cycle tick in `emigration` for a more consistent gameplay experience
2 parents 80e1a98 + 536edcf commit 33c6bce

File tree

2 files changed

+31
-12
lines changed

2 files changed

+31
-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

+30-12
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,27 @@
11
--@module = true
22
--@enable = true
33

4+
local utils = require('utils')
5+
46
local GLOBAL_KEY = 'emigration' -- used for state change hooks and persistence
57

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

814
function isEnabled()
9-
return enabled
15+
return state.enabled
1016
end
1117

1218
local function persist_state()
13-
dfhack.persistent.saveSiteData(GLOBAL_KEY, {enabled=enabled})
19+
dfhack.persistent.saveSiteData(GLOBAL_KEY, state)
1420
end
1521

22+
local TICKS_PER_MONTH = 33600
23+
local TICKS_PER_YEAR = 12 * TICKS_PER_MONTH
24+
1625
function desireToStay(unit,method,civ_id)
1726
-- on a percentage scale
1827
local value = 100 - unit.status.current_soul.personality.stress / 5000
@@ -191,27 +200,36 @@ function checkmigrationnow()
191200
else
192201
for _, civ_id in pairs(merchant_civ_ids) do checkForDeserters('merchant', civ_id) end
193202
end
203+
204+
state.last_cycle_tick = dfhack.world.ReadCurrentTick() + TICKS_PER_YEAR * dfhack.world.ReadCurrentYear()
194205
end
195206

196207
local function event_loop()
197-
if enabled then
198-
checkmigrationnow()
199-
dfhack.timeout(1, 'months', event_loop)
208+
if state.enabled then
209+
local current_tick = dfhack.world.ReadCurrentTick() + TICKS_PER_YEAR * dfhack.world.ReadCurrentYear()
210+
if current_tick - state.last_cycle_tick < TICKS_PER_MONTH then
211+
local timeout_ticks = state.last_cycle_tick - current_tick + TICKS_PER_MONTH
212+
dfhack.timeout(timeout_ticks, 'ticks', event_loop)
213+
else
214+
checkmigrationnow()
215+
dfhack.timeout(1, 'months', event_loop)
216+
end
200217
end
201218
end
202219

203220
dfhack.onStateChange[GLOBAL_KEY] = function(sc)
204221
if sc == SC_MAP_UNLOADED then
205-
enabled = false
222+
state.enabled = false
206223
return
207224
end
208225

209226
if sc ~= SC_MAP_LOADED or df.global.gamemode ~= df.game_mode.DWARF then
210227
return
211228
end
212229

213-
local persisted_data = dfhack.persistent.getSiteData(GLOBAL_KEY, {enabled=false})
214-
enabled = persisted_data.enabled
230+
state = get_default_state()
231+
utils.assign(state, dfhack.persistent.getSiteData(GLOBAL_KEY, state))
232+
215233
event_loop()
216234
end
217235

@@ -230,11 +248,11 @@ if dfhack_flags and dfhack_flags.enable then
230248
end
231249

232250
if args[1] == "enable" then
233-
enabled = true
251+
state.enabled = true
234252
elseif args[1] == "disable" then
235-
enabled = false
253+
state.enabled = false
236254
else
237-
print('emigration is ' .. (enabled and 'enabled' or 'not enabled'))
255+
print('emigration is ' .. (state.enabled and 'enabled' or 'not enabled'))
238256
return
239257
end
240258

0 commit comments

Comments
 (0)