-----------------------------------------
-- FT CONTUSION (MILITARY RP)
-- Собственность Олега Закона и Scripty
-----------------------------------------
util.AddNetworkString("StartConcussionEffect")
util.AddNetworkString("ConcussionSlowdown")

local EFFECT_DURATION = 10
local SLOW_MULT = 0.45

local RADIUS_HL2 = 550
local RADIUS_LVS = 550
local RADIUS_SWBOMB = 900

local function ApplyConcussion(ply)
    if not IsValid(ply) or not ply:Alive() then return end
    
    -- Не применяем контузию, если игрок в обычном транспорте или в технике LVS
    if ply:InVehicle() then return end
    if ply.lvsGetVehicle and IsValid(ply:lvsGetVehicle()) then return end

    net.Start("StartConcussionEffect")
    net.Send(ply)

    net.Start("ConcussionSlowdown")
    net.WriteFloat(SLOW_MULT)
    net.WriteFloat(EFFECT_DURATION)
    net.Send(ply)
end

local function IsTFABombProjectileClass(cls)
    if not cls or cls == "" then return false end
    return string.StartWith(cls, "tfa_bomb")
end

hook.Add("EntityTakeDamage", "Concussion_TFA_Damage", function(target, dmginfo)
    if not target:IsPlayer() then return end

    local attacker  = dmginfo:GetAttacker()
    local inflictor = dmginfo:GetInflictor()

    local aclass = IsValid(attacker)  and attacker:GetClass()  or ""
    local iclass = IsValid(inflictor) and inflictor:GetClass() or ""

    if IsTFABombProjectileClass(aclass) or IsTFABombProjectileClass(iclass) then
        ApplyConcussion(target)
        return
    end

    if IsValid(attacker) and attacker:IsPlayer() then
        local wep = attacker:GetActiveWeapon()
        if IsValid(wep) and wep.IsTFADOINade then
            ApplyConcussion(target)
            return
        end
    end
end)

hook.Add("EntityRemoved", "Concussion_TFA_Radius", function(ent)
    if not IsValid(ent) then return end

    local cls = ent:GetClass()
    if not IsTFABombProjectileClass(cls) then return end

    local pos = ent:GetPos()

    for _, ply in ipairs(player.GetAll()) do
        if ply:GetPos():Distance(pos) <= RADIUS_HL2 then
            ApplyConcussion(ply)
        end
    end
end)

hook.Add("EntityTakeDamage", "Concussion_HL2", function(target, dmginfo)
    if not target:IsPlayer() then return end
    if not dmginfo:IsExplosionDamage() then return end

    ApplyConcussion(target)
end)

hook.Add("EntityTakeDamage", "Concussion_LVS_Damage", function(target, dmginfo)
    if not target:IsPlayer() then return end

    local attacker = dmginfo:GetAttacker()
    if not IsValid(attacker) then return end

    local class = attacker:GetClass()
    if class == "lvs_item_explosive" or string.find(class, "lvs_item_explosive") then
        attacker._DidExplode = true
        ApplyConcussion(target)
    end
end)

hook.Add("EntityRemoved", "Concussion_LVS_Radius", function(ent)
    if not IsValid(ent) then return end

    local class = ent:GetClass()
    if class ~= "lvs_item_explosive" and not string.find(class, "lvs_item_explosive") then return end
    if not ent._DidExplode then return end

    local pos = ent:GetPos()

    for _, ply in ipairs(player.GetAll()) do
        if ply:GetPos():Distance(pos) <= RADIUS_LVS then
            ApplyConcussion(ply)
        end
    end
end)

hook.Add("OnEntityCreated", "SWBombV3_ExplosionDetector", function(ent)
    if not IsValid(ent) then return end

    if ent:GetClass() == "swbomb_explosion" then
        local pos = ent:GetPos()

        for _, bomb in ipairs(ents.FindInSphere(pos, 200)) do
            if bomb.SWBombV3 then
                bomb._DidExplode = true
            end
        end
    end
end)

hook.Add("EntityTakeDamage", "Concussion_SWBomb_Damage", function(target, dmginfo)
    if not target:IsPlayer() then return end

    local attacker = dmginfo:GetAttacker()
    if not IsValid(attacker) then return end

    if attacker.SWBombV3 then
        attacker._DidExplode = true
        ApplyConcussion(target)
    end
end)

hook.Add("EntityRemoved", "Concussion_SWBomb_Radius", function(ent)
    if not IsValid(ent) then return end
    if not ent.SWBombV3 then return end
    if not ent.Exploded then return end 

    local pos = ent:GetPos()

    for _, ply in ipairs(player.GetAll()) do
        if ply:GetPos():Distance(pos) <= RADIUS_SWBOMB then
            ApplyConcussion(ply)
        end
    end
end)

-----------------------------------------
-- FT CONTUSION (MILITARY RP)
-- Собственность Олега Закона и Scripty
-----------------------------------------
