AddCSLuaFile()

function SWEP:CanDeployBipod()
    return !self:HasFlag("Lowered") 
        && !self:HasFlag("Drawing") 
        && !self:HasFlag("Holstering") 
        && !self:HasFlag("Customizing") 
        && CurTime() >= self:GetNextMeleeTime() 
        && (!self:HasFlag("Reloading") || self:HasFlag("Aiming"))
        && CurTime() >= self:GetNextInspectTime()
end

function SWEP:BipodLogic()
    if (CLIENT && game.SinglePlayer()) then
        return
    end

    if (self:GetBipod() == nil || !self:CanDeployBipod()) then
        if (self:HasFlag("BipodDeployed")) then
            self:EmitSound("ViewModel.BipodExit")
        end

        self:RemoveFlag("BipodDeployed")
        return
    end

    local bDeployed = false

    if (!self:IsOwnerMoving() && self:GetOwner():IsOnGround()) then
        local pos = self:GetOwner():EyePos() + Angle(0, self:GetOwner():EyeAngles().y, 0):Forward() * 16
        
        if (!self:GetOwner():Crouching()) then
            local tr = util.TraceHull({
                start = pos,
                endpos = pos - Vector(0, 0, 32),
                mins = Vector(-16, -16, 0),
                maxs = Vector(16, 16, 2),
                filter = player.GetAll(),
                mask = MASK_PLAYERSOLID
            })
            
            bDeployed = tr.Hit && !tr.StartSolid
        else
            bDeployed = true
        end
    end

    if (self:HasFlag("BipodDeployed") != bDeployed) then
        if (bDeployed) then
            self:EmitSound("ViewModel.BipodDeploy")

            if (!self:HasFlag("Aiming")) then
                self:PlayViewModelAnimation(self.Animations.Bipod_Deploy != nil && "Bipod_Deploy" || "Jog_Out")
            end
        else
            self:EmitSound("ViewModel.BipodExit")

            if (!self:HasFlag("Aiming")) then
                self:PlayViewModelAnimation(self.Animations.Bipod_Leave != nil && "Bipod_Leave" || "Jump")
            end
        end
    end

    if (bDeployed) then
        self:AddFlag("BipodDeployed")
    else
        self:RemoveFlag("BipodDeployed")
    end
end