include("shared.lua")

AddCSLuaFile("shared.lua")

function ENT:Initialize()
    self:SetModel("models/hunter/blocks/cube025x025x025.mdl")
    self:SetNoDraw(true)
    self:DrawShadow(false)
    self:SetNWBool('Ready', true)
    if !istable(DMZ_ExfilPoints[game.GetMap()][self.ID]) then
        self:Remove()
        return
    end
    self:SetPos(DMZ_ExfilPoints[game.GetMap()][self.ID]["Main"])
end

hook.Add("PlayerButtonDown", "DMZ_Exfil", function(ply, but)
    local ent = nil 
    for _, en in ipairs(ents.FindInSphere(ply:GetPos(), 500)) do
        if en:GetClass() == "dmz_exfil_point" then
            ent = en
            break
        end
    end
    if IsValid(ply:GetActiveWeapon()) and ply:GetActiveWeapon():GetClass() == "agrp_flare_gun" then
        if but == KEY_G and IsValid(ent) and ent:GetNWBool('Ready') then
            ply:GetActiveWeapon():Remove()
            CallExfil(ent, ply)
        end
    end
end)

function CallExfil(ent, ply)
    ent:EmitSound("weapons/flaregun/fire.wav")
    ent:SetNWBool('Ready', false)

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

    BroadcastLua([[
        local part = EffectData()
        part:SetOrigin(Entity(]]..ply:EntIndex()..[[):GetPos()+Vector(0,0,100))
        util.Effect("dmz_signal_rocket", part)
    ]])

    local he = ents.Create("dmz_exfil_helo")
    he.Caller = ply
    he.Points = DMZ_ExfilPoints[game.GetMap()][ent.ID]
    he.CallEnt = ent
    timer.Simple(30, function()
        if !IsValid(he) then return end
        he:Spawn()
    end)
end