if isClient() then return end
if SandboxVars.coldwinters.GeneratorFailures == false then return end

local function isWinterMonth()
    local m = getGameTime():getMonth()
    return (m == 11) or (m == 0) or (m == 1) or (m == 2)
end

local function checkGeneratorsNearPlayer(player)
    if not player then return end
    local x, y, z = player:getX(), player:getY(), player:getZ()
    local cell = getCell()

    for dx = -35, 35 do
        for dy = -35, 35 do
            local sq = cell:getGridSquare(x + dx, y + dy, z)
            if sq then
                local objects = sq:getObjects()
                for i = 0, objects:size() - 1 do
                    local obj = objects:get(i)
                    if obj and obj.setActivated and obj.isActivated and obj:isActivated() then
                        if ZombRandFloat(0.0, 100.0) < 0.14 then  -- chance
                            obj:setActivated(false)
                        end
                    end
                end
            end
        end
    end
end

local function onEveryHour()
    if not isWinterMonth() then return end

    local playerCount = getNumActivePlayers() or 0
    for i = 0, playerCount - 1 do
        local player = getSpecificPlayer(i)
        if player then
            checkGeneratorsNearPlayer(player)
        end
    end
end

Events.EveryHours.Add(onEveryHour)
