So hopefully this isn't completely and utterly wrong. I'm a lua noob and also spoiled on compiler errors.
feeding-timers.lua
-- feeding-timers :
-- Immediately fixes feeding timers for prisoners, patients and infants.
-- feeding-timers enable :
-- Repeats the fix once per month (but not immediately).
-- feeding-timers enable # :
-- Repeats the fix once per # months, minimum 1 (but not immediately).
-- feeding-timers disable :
-- Ceases automatic fixes.
local callback_id = nil
local frequency = 0
local function feeding_timer_fix()
local fixcount = 0
for _,unit in ipairs(df.global.world.units.active) do
if dfhack.units.isCitizen(unit) then
for _,v in pairs(unit.status.misc_traits) do
local didfix = 0
if v.id == 0 then -- I think this should have additional conditions...
v.value = 0 -- GiveWater cooldown set to zero
didfix = 1
end
if v.id == 1 then -- I think this should have additional conditions...
v.value = 0 -- GiveFood cooldown set to zero
didfix = 1
end
fixcount = fixcount + didfix
end
end
end
print("Fixed feeding timers for " .. fixcount .. " citizens.")
if frequency > 0 then
callback_id = dfhack.timeout(frequency, 'months', feeding_timer_fix)
end
end
local args = {...}
if args[1] == 'disable' then
if callback_id ~= nil then -- Is this required or will timeout_active fail gracefully?
timeout_active(callback_id, nil) -- Does this return nil?
end
callback_id = nil
frequency = 0
else if args[1] == 'enable' then
if tonumber(args[2]) then
frequency = args[2]
if frequency < 2 then
frequency = 1
end
else
frequency = 1 -- What's the normal, non-broken food/water interval?
end
if callback_id ~= nil then
timeout_active(callback_id, nil)
end
callback_id = dfhack.timeout(frequency, 'months', feeding_timer_fix)
print('Feeding timers will be fixed at each ' .. frequency .. ' month interval.')
else
feeding_timer_fix()
end