AddCSLuaFile("shared.lua")
AddCSLuaFile("cl_init.lua")
include("shared.lua")
AddCSLuaFile("autorun/mur_drone_fx.lua")

if SERVER then

function ENT:Initialize()
    self:SetModel("models/shtormer/sania.mdl")
    self:PhysicsInit(SOLID_VPHYSICS)
    self:SetMoveType(MOVETYPE_VPHYSICS)
    self:SetSolid(SOLID_VPHYSICS)
    self:SetNWBool("JamEnabled", true)

    local phys = self:GetPhysicsObject()
    if IsValid(phys) then phys:Wake() end

    self.HP = self.HP or 500
end

function ENT:OnTakeDamage(dmginfo)
    local dmg = dmginfo:GetDamage()
    self.HP = self.HP - dmg
    if self.HP <= 0 then
        self:Remove()
    end
end

-- Глушение дронов
function ENT:JamDrone(drone)
    if not IsValid(drone) then return end

    local dist = drone:GetPos():Distance(self:GetPos())
    local jammed = dist <= self.JamDistance

    drone:SetNWBool("Jammed", jammed)

    if jammed then
        drone:SetNWFloat("JamStrength", 1 - dist / self.JamDistance)
    else
        drone:SetNWFloat("JamStrength", 0)
    end
end

hook.Add("KeyPress", "SaniaToggle", function(ply, key)
    if key ~= IN_USE then return end

    local tr = ply:GetEyeTrace()
    local ent = tr.Entity

    if not IsValid(ent) then return end
    if ent:GetClass() ~= "sw_sania" then return end
    if tr.HitPos:Distance(ply:GetPos()) > 150 then return end

    local newState = not ent:GetNWBool("JamEnabled")
    ent:SetNWBool("JamEnabled", newState)

    if newState then
        ply:ChatPrint("РЭБ: ВКЛЮЧЕНА")
        ent:EmitSound("buttons/button3.wav", 75, 120)
    else
        ply:ChatPrint("РЭБ: ВЫКЛЮЧЕНА")
        ent:EmitSound("buttons/button19.wav", 75, 80)
    end
end)



function ENT:Think()
    local sph = ents.FindInSphere(self:GetPos(), self.JamDistance)
    if not self:GetNWBool("JamEnabled") then
        for drone, _ in pairs(self.JammedDrones or {}) do
            if IsValid(drone) then
                drone:SetNWBool("Jammed", false)
                drone:SetNWFloat("JamStrength", 0)
            end
        end
        self.JammedDrones = {}
        self:NextThink(CurTime() + 0.1)
        return true
    end
    self.JammedDrones = self.JammedDrones or {}

    -- временная таблица для отметки "кто сейчас в зоне"
    local inZone = {}

    for i = 1, #sph do
        local ent = sph[i]
        if not IsValid(ent) then continue end

        local class = ent:GetClass()

        -- 1) Игрок в UAV
        if ent:IsPlayer() then
            local veh = ent:lvsGetVehicle()
            if IsValid(veh) then
                local uav = veh:GetNWEntity("UAVControl")
                if IsValid(uav) then
                    local dist = veh:GetPos():Distance(self:GetPos())

                    if dist < self.JamDistance then
                        if not self._savedRates then
                            self._savedRates = {
                                pitch = veh.TurnRatePitch,
                                yaw   = veh.TurnRateYaw,
                                roll  = veh.TurnRateRoll
                            }
                        end

                        local mul = math.max(0.1, dist / self.FullJamDistance)

                        veh.TurnRatePitch = self._savedRates.pitch * mul
                        veh.TurnRateYaw   = self._savedRates.yaw   * mul
                        veh.TurnRateRoll  = self._savedRates.roll  * mul
                    else
                        if self._savedRates then
                            veh.TurnRatePitch = self._savedRates.pitch
                            veh.TurnRateYaw   = self._savedRates.yaw
                            veh.TurnRateRoll  = self._savedRates.roll
                            self._savedRates = nil
                        end
                    end
                end
            end
        end

        -- 2) Дроны
        if class == "mur_drone_entity"
        or class == "mur_drone_grenade"
        or class == "mur_drone_spy" then

            inZone[ent] = true
            self.JammedDrones[ent] = true

            local dist = ent:GetPos():Distance(self:GetPos())
            ent:SetNWBool("Jammed", true)
            ent:SetNWFloat("JamStrength", 1 - dist / self.JamDistance)
        end
    end

    for drone, _ in pairs(self.JammedDrones) do
        if not IsValid(drone) then
            self.JammedDrones[drone] = nil
            continue
        end

        if not inZone[drone] then
            drone:SetNWBool("Jammed", false)
            drone:SetNWFloat("JamStrength", 0)
            self.JammedDrones[drone] = nil
        end
    end

    self:NextThink(CurTime() + 0.1)
    return true
end

end
