4
4
local GLOBAL_KEY = ' emigration' -- used for state change hooks and persistence
5
5
6
6
enabled = enabled or false
7
+ last_cycle_tick = last_cycle_tick or 0
7
8
8
9
function isEnabled ()
9
10
return enabled
10
11
end
11
12
12
13
local function persist_state ()
13
- dfhack .persistent .saveSiteData (GLOBAL_KEY , {enabled = enabled })
14
+ dfhack .persistent .saveSiteData (GLOBAL_KEY , {enabled = enabled , last_cycle_tick = last_cycle_tick })
14
15
end
15
16
16
17
function desireToStay (unit ,method ,civ_id )
@@ -191,12 +192,20 @@ function checkmigrationnow()
191
192
else
192
193
for _ , civ_id in pairs (merchant_civ_ids ) do checkForDeserters (' merchant' , civ_id ) end
193
194
end
195
+
196
+ last_cycle_tick = dfhack .world .ReadCurrentTick () + 403200 * dfhack .world .ReadCurrentYear ()
194
197
end
195
198
196
199
local function event_loop ()
197
200
if enabled then
198
- checkmigrationnow ()
199
- dfhack .timeout (1 , ' months' , event_loop )
201
+ local current_tick = dfhack .world .ReadCurrentTick () + 403200 * dfhack .world .ReadCurrentYear ()
202
+ if last_cycle_tick and (current_tick - last_cycle_tick < 33600 ) then
203
+ local timeout_ticks = last_cycle_tick - current_tick + 33600
204
+ dfhack .timeout (timeout_ticks , ' ticks' , event_loop )
205
+ else
206
+ checkmigrationnow ()
207
+ dfhack .timeout (1 , ' months' , event_loop )
208
+ end
200
209
end
201
210
end
202
211
@@ -210,8 +219,9 @@ dfhack.onStateChange[GLOBAL_KEY] = function(sc)
210
219
return
211
220
end
212
221
213
- local persisted_data = dfhack .persistent .getSiteData (GLOBAL_KEY , {enabled = false })
222
+ local persisted_data = dfhack .persistent .getSiteData (GLOBAL_KEY , {enabled = false , last_cycle_tick = 0 })
214
223
enabled = persisted_data .enabled
224
+ last_cycle_tick = persisted_data .last_cycle_tick
215
225
event_loop ()
216
226
end
217
227
0 commit comments