Does dfhack.settimeout actually work in Lua code? I've tried to adapt the way Putnam was using it for his growth size bug fix, and nearest I can tell it's not working in my script at least. The script runs once at DF startup (I put it in dfhack.init), and the debugging text never fires after that point. For reference, here's the code:
local args = {...}
function count_children(unit_id)
local children = 0
for key, value in pairs(df.global.world.units.all) do
if value.relations.mother_id == unit_id then
children = children + 1
end
end
return children
end
function terminate_pregnancy(unit)
if unit.relations.pregnancy_timer > 0 then
unit.relations.pregnancy_ptr:delete()
unit.relations.pregnancy_ptr = nil
unit.relations.pregnancy_timer = 0
unit.relations.pregnancy_mystery = -1
return 1 -- Terminated a pregnancy
end
-- Wasn't pregnant
return 0
end
function family_planning(children_min, children_max)
local terminated_pregnancies = 0
for key, value in pairs(df.global.world.units.all) do
local children = 0
-- Only married females matter
if (value.sex == 0 and value.relations.spouse_id ~= -1) then
children = count_children(value.id)
end
math.randomseed(value.id)
local maximum_children = math.random(children_min, children_max)
local caste = df.creature_raw.find(value.race).caste[value.caste]
local menopause_age = math.random(caste.misc.maxage_min * 0.66, caste.misc.maxage_max * 0.66)
local age = df.global.cur_year - value.relations.birth_year
if age > menopause_age or children > maximum_children then
-- This is awkward syntax, but I have no idea how to pass local values by reference in Lua
terminated_pregnancies = terminated_pregnancies + terminate_pregnancy(value)
end
end
print("Terminated " .. terminated_pregnancies .. " pregnancies.")
end
if df.isnull(args[2]) then
print('You did not specify two parameters, so a child cap of 2-4 is assumed.')
args[1] = 2
args[2] = 4
end
function apply_family_planning()
family_planning(tonumber(args[1]), tonumber(args[2]))
dfhack.timeout(1, "months", apply_family_planning)
end
apply_family_planning()
Ignore the fact that I'm doing unsafe things with the random number generator.