g_pastureHerding = PastureHerding.new()

PastureHerdingLog.dev("main.lua sourced; installing hooks immediately")
g_pastureHerding:earlyInstall()
if HealthTracer ~= nil and type(HealthTracer.installHooks) == "function" then
    HealthTracer.installHooks()
end
PHMenu.install()
SprayerHook.install() -- best-effort early; retried below once specializations exist

local function onMissionStarted()
    g_pastureHerding:load()
    SprayerHook.install() -- Sprayer specialization is guaranteed loaded by now
    -- Retry the foodFactor-capture hook: at main.lua source time the husbandry
    -- spec functions may not exist yet (updateOutput was nil). By now they do.
    if HealthTracer ~= nil and type(HealthTracer.installHooks) == "function" then
        HealthTracer.installHooks()
    end
end

if Mission00 ~= nil and Mission00.loadMapFinished ~= nil then
    Mission00.loadMapFinished = Utils.appendedFunction(Mission00.loadMapFinished, onMissionStarted)
elseif Mission00 ~= nil and Mission00.loadMission00Finished ~= nil then
    Mission00.loadMission00Finished = Utils.appendedFunction(Mission00.loadMission00Finished, onMissionStarted)
end
