local playerangle = nil
local function ActiveShove(playerobj)
    local vehicle = playerobj:getVehicle()
    if vehicle == nil then
        return
    end
    if vehicle:getDriver() ~= playerobj then
        return
    end
    local PanzerType = GetPanzerType(vehicle)
    if not PanzerType then
        return
    end
    if not PanzerList[PanzerType].Shove then
        return
    end
    if PanzerType == "Cav" then
        local Shovel = vehicle:getPartById("Cav_Shovel")
        if Shovel and not Shovel:getInventoryItem() then
            return
        end
        if Shovel:getCondition() <= 0 then
            return
        end

    end
    local Shovel = nil
    if SandboxVars.WT.TigerNeedShovel then
        if not HasUnlockThisReasearch(vehicle, PanzerList[PanzerType].Shove) then
            return
        end
        Shovel = vehicle:getPartById(PanzerType .. "_Shovel")
        if Shovel:getCondition() <= 0 then
            return
        end
    end
    playerangle = playerobj:getForwardDirection():getDirection()
    local diff = 0
    if math.abs(playerobj:getDirectionAngle()) > 90 then
        diff = math.abs(playerobj:getDirectionAngle()) - math.abs(vehicle:getAngleY())
    else
        diff = math.abs(playerobj:getDirectionAngle()) + math.abs(vehicle:getAngleY())
    end
    if not (diff >= 88 and diff <= 91) then
        return
    end
    local vehiclewide = 3 * 3
    local vehicleheight = 5 * 2.8

    local vx = vehicle:getX()
    local vy = vehicle:getY()

    local squaretable = {}
    for z = 6, vehicleheight do
        for i = 0, vehiclewide do
            local vx1 = vx + z / 2 * math.cos(playerangle) + i / 3 * math.cos(playerangle + math.pi / 2)
            local vy1 = vy + z / 2 * math.sin(playerangle) + i / 3 * math.sin(playerangle + math.pi / 2)
            squaretable[getCell():getGridSquare(vx1, vy1, 0)] = 1
            local vx2 = vx + z / 2 * math.cos(playerangle) + i / 3 * math.cos(playerangle - math.pi / 2)
            local vy2 = vy + z / 2 * math.sin(playerangle) + i / 3 * math.sin(playerangle - math.pi / 2)
            squaretable[getCell():getGridSquare(vx2, vy2, 0)] = 1
        end
    end
    for k, v in pairs(squaretable) do
        local objects = k:getObjects()
        local zombie = k:getZombie()
        if zombie and vehicle:getSpeed2D() > 0.3 then
            if ZombRand(10) > 6 then
                zombie:knockDown(false)
            end
            zombie:Kill(playerobj)
            if not zombie:getModData().soundbreak then
                zombie:getModData().soundbreak = 1
                if ZombRand(50) == 0 then
                    local part = vehicle:getPartById(PanzerType .. "_Shovel")
                    local ConditionNow = part:getCondition()
                    Panzer:sendVehicleCommandWrapper(playerobj, part, "setPartCondition", {
                        condition = ConditionNow - 1
                    })
                end
            end

        end
        for index = 0, objects:size() - 1 do
            local obj = objects:get(index)
            if obj then
                if instanceof(obj, "IsoTree") then
                    obj:Damage(obj:getMaxHealth())
                    if not obj:getModData().soundbreak then
                        obj:getModData().soundbreak = 1
                        vehicle:playSound("VehicleHitTree")
                    end
                    if ZombRand(100) == 0 then
                        local part = vehicle:getPartById(PanzerType .. "_Shovel")
                        local ConditionNow = part:getCondition()
                        Panzer:sendVehicleCommandWrapper(playerobj, part, "setPartCondition", {
                            condition = ConditionNow - 1
                        })
                    end
                end
            end
        end
    end
end

local EnterFlag = false
local function CheckLocalVehicle()
    local player = getPlayer()
    if player then
        local vehicle = player:getVehicle()
        if (vehicle and GetPanzerType(vehicle)) and not EnterFlag then
            EnterFlag = true
            Events.OnPlayerUpdate.Add(ActiveShove)
        else
            Events.OnPlayerUpdate.Remove(ActiveShove)
            EnterFlag = false
        end
    end

end

Events.EveryOneMinute.Add(CheckLocalVehicle)
