SuR.Zombies_In_Wave = 8
SuR.Time_Before_Wave = 0
SuR.Wave_Started = false
SuR.Wave_Changing = false
SuR.Current_Wave = 0
SuR.Enemy_To_Spawn = 0
SuR.Game_Started = false
SuR.Delay_For_Spawn_Zombie = 0
SuR.Delay_For_Spawn_Loot = 0
SuR.Delay_Before_Lose = 0
SuR.Max_Zombies_On_Map = SuR.Config.Max_Zombies_On_Map
SuR.TimerCountdown = false
SuR.AllowSpawn = false
SuR.DeployTime = CurTime()+SuR.Config.Delay_Before_Deploy
SuR.SpecialInfectedChance = 20
SuR.ActiveMutator = 0
SuR.Current_Boss = nil
SuR.ExfilWave = false
SuR.ExfilSuccess = false
SuR.ObjectiveFuelNeed = 1
SuR.ObjectiveFuel = 0
SuR.DisableMusic = false

function SuR:ChangeStateOfGame(state)
    if state then
        hook.Call("SuR.GameState", nil, true)
        SuR.Delay_For_Spawn_Zombie = 0
        SuR.Delay_For_Spawn_Loot = CurTime()+SuR.Config.Loot_Spawn_Delay
        SuR.Zombies_In_Wave = SuR.Config.Start_Zombie_Count
        if SuR.Config.Open_Func_Doors then
            for _, d in ipairs(ents.FindByClass("func_door")) do
                d:Fire('Open')
                d:Fire('Lock')
            end
        end
        if SuR.Config.Delete_Map_Items then 
            SuR:DeleteItems()
        end
        if SuR.Config.Delete_Map_Logic then
            SuR:DeleteLogic()
        end
        for _, ply in ipairs(player.GetAll()) do
            ply:SetTeam(1)
        end
        if SuR.Config.Objective_Mode then
            SuR:StartObjectiveWave()
        else
            SuR:ChangeWave()
            SuR:ShowTips("survival")
        end
        SuR:EnablePropDamage()
        SuR:SpawnZone()
        SuR:SpawnInvisWalls()
    else
        SuR:StopExfil()
        timer.Remove("SuR.ObjectiveGasLeak")
        hook.Call("SuR.GameState", nil, false)
        SuR.Game_Started = false
        SuR.Wave_Changing = false
        SuR.ExfilSuccess = false
        SuR.DisableMusic = false
        SuR.Time_Before_Wave = 0
        SuR.Wave_Started = false
        SuR.Current_Wave = 0
        SuR.Enemy_To_Spawn = 0
        SuR.Delay_For_Spawn_Zombie = 0
        SuR.Delay_For_Spawn_Loot = 0
        SuR.ActiveMutator = 0
        SuR.AllowSpawn = false
        SuR.SpecialInfectedChance = 20
        SuR.Zombies_In_Wave = SuR.Config.Start_Zombie_Count
        SuR.ObjectiveFuelNeed = 1
        SuR.ObjectiveFuel = 0
        SuR:SendDataToClient("Wave", 0)
        SuR:SendDataToClient("WaveState", false, 1)
        SuR:SendDataToClient("Mutator", 0)
        for _, ply in ipairs(player.GetAll()) do
            if ply:Alive() then ply:Kill() end
            timer.Simple(0.1, function()
                if !IsValid(ply) then return end
                ply:SetTeam(1)
                ply:SetModel("models/player/Group03/male_0"..math.random(1,9)..".mdl")
                if #SuR.Config.Allowed_PlayerModels > 0 then
                    ply:SetModel(SuR.Config.Allowed_PlayerModels[math.random(1,#SuR.Config.Allowed_PlayerModels)])
                end
                local str = ply:GetNWString('SuR_PlayerModel', '')
                if str != '' then
                    ply:SetModel(str)
                end
            end)
        end
        game.CleanUpMap()
    end
end

function SuR:SpawnZone()
    local radius, position = SuR.Config.Zone_Radius, SuR.Config.Zone_Center_Position
    if radius > 250 then
        local z = ents.Create("sur_zone")
        z:SetPos(position)
        z:Spawn()
    end
end

function SuR:SpawnInvisWalls()
    for k, v in pairs(SuR.Config.Invisible_Walls) do
        local ent = ents.Create("prop_dynamic")
        ent:SetPos(v.pos)
        ent:SetAngles(v.ang)
        ent:SetModel(v.model)
        ent:Spawn()
        ent:SetSolid(SOLID_VPHYSICS)
        ent:PhysicsInit(SOLID_VPHYSICS)
        if IsValid(ent:GetPhysicsObject()) then
            ent:GetPhysicsObject():EnableMotion(false)
        end
        ent:SetNoDraw(true)
    end
end

function SuR:StartWave()
    SuR:PlaySoundOnClient("surrounded/wave_start.mp3")
    SuR.Wave_Started = true
    SuR.Wave_Changing = false
    SuR:SendDataToClient("Wave", SuR.Current_Wave)
    SuR:SendDataToClient("WaveState", true, 1)
    SuR:MessageOnClient(2)
    if SuR.Config.Versus_Mode and SuR.Current_Wave == 1 and player.GetCount() > 1 and SuR:CurrentPlayerZombies() == 0 then
        local ply = table.Random(player.GetAll())
        ply:SuR_MakeZombie()
        SuR:MessageOnClient(14, nil, ply)
    end

    if SuR.ActiveMutator == 11 then
        SuR:StartGasExfil()
    end

    if SuR.Current_Wave == 1 then
        SuR:ShowTips("waves")
    end

    if SuR.Config.Versus_Mode then
        for _, ply in ipairs(player.GetAll()) do
            if ply:IsZombie() and !ply:Alive() then
                ply:Spawn()
            end
        end
    end

    local boss = SuR.Config.Bosses[SuR.Current_Wave]
    if istable(boss) then
        local pos = SuR:GetRandomPos(SuR.Config.Enable_Spawn_In_Building_Zombies, SuR.Config.Zombie_Spawn_Radius_Min, SuR.Config.Zombie_Spawn_Radius_Max)
        if math.random(1,10) == 1 or !isvector(pos) then
            pos = SuR:GetRandomPos(!SuR.Config.Enable_Spawn_In_Building_Zombies, SuR.Config.Zombie_Spawn_Radius_Min, SuR.Config.Zombie_Spawn_Radius_Max)
        end
    
        if isvector(pos) then
            local ent = ents.Create(boss.class)
            if boss.class == "npc_vj_lnr_butcher_chainsaw" then
                ent.AnimTbl_Run = {ACT_RUN}
            end
            ent:SetPos(pos+Vector(0,0,4))
            ent.NoRemove = true
            ent:Spawn()
            local hp = boss.health+((player.GetCount()-1)*(boss.health/10))
            ent:SetMaxHealth(hp)
            ent:SetHealth(hp)
            if SuR.ActiveMutator == 4 then
                ent:SetHealth(ent:Health()/4)
            end
            ent.IsZombie = true
            ent.FollowPlayer = false
            ent.IsSpecial = true
            ent.IsBoss = true
            ent:RewriteLNR(true)
            ent.FlinchChance = 50
            ent.CustomOnFlinch_BeforeFlinch = function() end
            ent.CustomOnTakeDamage_AfterDamage = function() end
            SuR.Enemy_To_Spawn = SuR.Enemy_To_Spawn - 1
            SuR.Current_Boss = ent
            SuR:ShowTips("bosses")
            timer.Create("ZombieMoveLogic"..ent:EntIndex(), 30, 0, function()
                if IsValid(ent) then
                    local ply = SuR:PickRandomPlayer(true)
                    if IsValid(ply) then
                        local pos = ply:GetPos()
                        ent:SetLastPosition(pos)
                        ent:VJ_TASK_GOTO_LASTPOS()
                    end
                else
                    timer.Remove("ZombieMoveLogic"..ent:EntIndex())
                end
            end)

            timer.Simple(3, function()
                if !IsValid(ent) then return end

                net.Start("SuR.Boss")
                net.WriteString(boss.name)
                net.WriteEntity(ent)
                net.Broadcast()
            end)
        end
    end
end

function SuR:StartObjectiveWave()
    SuR.Game_Started = true
    SuR.Time_Before_Wave = math.huge
    for _, ply in ipairs(player.GetAll()) do
        ply:SetTeam(1)
        ply:Spawn()
        ply:ScreenFade(SCREENFADE.IN, color_black, 1, 0)
    end
    BroadcastLua([[SuR:StartPhrase() SuR:PlayMusicOther("")]])
    SuR.Enemy_To_Spawn = math.huge
    SuR.Config.Versus_Mode = false

    local count = math.Clamp(math.floor(8*(player.GetCount()/2)), 4, SuR.Config.Objective_Mode_Max_Cans)
    local gen_count = math.Clamp(math.floor(count/4), 1, #SuR.Config.Objective_Mode_Positions["generator"])
    local to_fuel = math.ceil(count/gen_count)
    local pos_tab = table.Copy(SuR.Config.Objective_Mode_Positions["generator"])
    SuR.ObjectiveFuel = 0
    SuR.ObjectiveFuelNeed = count
    table.Shuffle(pos_tab)
    SuR:SendDataToClient("MaxFuel", count)
    SuR:SendDataToClient("Fuel", 0)
    for i = 1, gen_count do
        local tab = pos_tab[i]
        if not tab then continue end

        local ent = ents.Create("sur_generator")
        ent.ToFuel = to_fuel
        ent.obj = true
        ent:SetPos(tab.pos)
        ent:SetAngles(tab.ang)
        ent:Spawn()
    end
    timer.Simple(15, function()
        if not SuR.Game_Started then return end
        SuR.Wave_Started = true
        for i = 1, count do
            SuR:SpawnFuel()
        end
    end)
    timer.Create("SuR.ObjectiveGasLeak", math.random(180,1200), 1, function()
        if 25 < math.random(1,100) or SuR.ObjectiveFuelNeed == SuR.ObjectiveFuel then return end 
        SuR.ActiveMutator = 11
        SuR:SendDataToClient("Mutator", SuR.ActiveMutator)
        SuR:StartGasExfil()
    end)
    SuR.SpecialInfectedChance = 40
    SuR.Current_Wave = SuR.Config.Objective_Mode_Wave
    SuR.Wave_Changing = false
    SuR:SendDataToClient("Wave", SuR.Current_Wave)
    SuR:SendDataToClient("WaveState", true, 1)
end

function SuR:ChangeWave()
    SuR:SendDataToClient("WaveState", false)
    SuR:SendDataToClient("Mutator", 0)
    SuR.ActiveMutator = 0
    local rnd = math.random(1,12)
    if SuR.Config.Mutator_Chance >= math.random(1,100) then
        SuR.ActiveMutator = rnd
        if rnd == 7 then
            for _, e in ipairs(ents.FindByClass("sur_loot")) do
                e:Remove()
            end
        end
    end
    SuR.Time_Before_Wave = CurTime()+math.max(SuR.Config.Delay_Before_Wave, 20)
    SuR.Wave_Started = false
    SuR.Wave_Changing = true
    SuR.TimerCountdown = false
    SuR.Current_Wave = SuR.Current_Wave + 1
    timer.Simple(10, function()
        if not SuR.Game_Started then return end

        if SuR.Current_Wave == 3 or SuR.Current_Wave == 6 or SuR.Current_Wave == 10 or SuR.Current_Wave == 15 then
            SuR:MessageOnClient(11)
        end
    end)
    timer.Simple(20, function()
        if not SuR.Game_Started or SuR.ActiveMutator == 0 then return end

        SuR:SendDataToClient("Mutator", SuR.ActiveMutator)
        SuR:MessageOnClient(12)
    end)
    SuR.Zombies_In_Wave = math.floor(SuR.Zombies_In_Wave+SuR.Config.Zombie_Multiplier_Per_Wave+(SuR.Config.Zombie_Multiplier_Per_Player*player.GetCount()))
    SuR.Enemy_To_Spawn = SuR.Zombies_In_Wave
    SuR.SpecialInfectedChance = math.Clamp(SuR.Current_Wave*5, 1, 75)
    if !SuR.Game_Started then
        SuR.Game_Started = true
        BroadcastLua([[SuR:PlayMusicOther(table.Random(SuR.Config.MusicWave["Start"])) SuR:StartPhrase()]])
        SuR:MessageOnClient(1)
        SuR.Zombies_In_Wave = SuR.Zombies_In_Wave
    else
        SuR:PlaySoundOnClient("surrounded/wave_complete.mp3")
        SuR:MessageOnClient(3)
    end
    if !SuR.Config.Versus_Mode or SuR.Current_Wave == 1 then
        for _, ply in ipairs(player.GetAll()) do
            if !ply:Alive() then
                ply:SetTeam(1)
                ply:Spawn()
                ply:ScreenFade(SCREENFADE.IN, color_black, 1, 0)
            end
            if SuR.Current_Wave != 1 then
                ply:AddXP(SuR.Config.XP_Giving["wave_complete"])
            end
        end
    end
end

function SuR:GetRandomClass(type)
    local class = ""
    local boolean = false
    if type == "zombie" then
        class = SuR.Config.Zombies.walker[math.random(1,#SuR.Config.Zombies.walker)]
        if SuR.Current_Wave >= SuR.Config.Runners_On_Wave and math.random(1,2) == 1 or SuR.Current_Wave >= SuR.Config.Only_Runners_On_Wave or SuR.ExfilWave then
            class = SuR.Config.Zombies.runner[math.random(1,#SuR.Config.Zombies.runner)]
        end
        if SuR.SpecialInfectedChance >= math.random(1,100) or SuR.ActiveMutator == 2 then
            local tab = SuR:GiveRandomTableWithChance(SuR.Config.Zombies.special, function(tab)
                return tab.wave <= SuR.Current_Wave or SuR.ExfilWave
            end)
            if istable(tab) then
                class = tab.class
                boolean = true
            end
        end
    end
    if type == "loot" then
        local tab, num = SuR:GiveRandomTableWithChance(LootItems)
        class = num
    end
    if type == "weapon" then
        class = SuR.Config.TierWeapons["Tier2"][math.random(1,#SuR.Config.TierWeapons["Tier2"])]
        if SuR.Config.Loot_Tier3SpawnWave <= SuR.Current_Wave and math.random(1,3) > 1 then
            class = SuR.Config.TierWeapons["Tier3"][math.random(1,#SuR.Config.TierWeapons["Tier3"])]
        end
        if SuR.Config.Loot_Tier4SpawnWave <= SuR.Current_Wave and math.random(1,3) == 1 then
            class = SuR.Config.TierWeapons["Tier4"][math.random(1,#SuR.Config.TierWeapons["Tier4"])]
        end
        if SuR.Config.Loot_Tier5SpawnWave <= SuR.Current_Wave and math.random(1,4) == 1 then
            class = SuR.Config.TierWeapons["Tier5"][math.random(1,#SuR.Config.TierWeapons["Tier5"])]
        end
        if math.random(1,100) <= SuR.Config.Melee_Spawn_Chance then
            class = SuR.Config.TierWeapons["Other"][math.random(1,#SuR.Config.TierWeapons["Other"])]
        end
        if math.random(1,100) <= SuR.Config.Other_Spawn_Chance then
            class = SuR.Config.TierWeapons["Melee"][math.random(1,#SuR.Config.TierWeapons["Melee"])]
        end
    end
    return class, boolean
end

function SuR:SpawnLoot(pos)
    if SuR.Delay_For_Spawn_Loot > CurTime() or !SuR.Game_Started or SuR.Wave_Started and !isvector(pos) or SuR.ActiveMutator == 7 then return end

    local tab = ents.GetAll()
    local count = 0
    for i=1,#tab do
        local ent = tab[i]
        if not ent.IsLoot then continue end
        count = count + 1
    end
    if count >= player.GetCount()*SuR.Config.Loot_Max_Per_Player then return end

    SuR.Delay_For_Spawn_Loot = CurTime()+SuR.Config.Loot_Spawn_Delay
    
    local weapon = nil
    local type = "loot"
    if not SuR.Config.Objective_Mode and math.random(1,6) == 1 and SuR.Current_Wave > 1 or SuR.Config.Objective_Mode and math.random(1,10) == 1 then
        type = "weapon"
    end
    local class = SuR:GetRandomClass(type)
    if !isvector(pos) then
        pos = SuR:FindSpawnPositions(table.Random(SuR.Config.Loot_SpawnPositions), 250)
    end

    if type == "weapon" then
        local cl = class
        local tab = weapons.Get(class)
        if tab then
            weapon = {
                name = tab.PrintName,
                class = cl,
                model = tab.WorldModel,
                usefunc = function(self, ply)
                    ply:Give(cl)
                end
            }
        end
    end

    if isvector(pos) then
        local ent = ents.Create("sur_loot")
        ent.LootType = class
        ent.Weapon = weapon
        ent:SetPos(pos)
        ent:Spawn()
        ent.IsLoot = true
    end
end

function SuR:SpawnFuel(pos)
    if !isvector(pos) then
        pos = table.Random(SuR.Config.Objective_Mode_Positions["fuel"])
    end

    local weapon = {
        name = "Jerry Can",
        class = "sur_fuel",
        model = "models/surrounded/obj/w_JerryCanBlue.mdl",
        usefunc = function(self, ply)
            ply:Give("sur_fuel")
        end
    }

    local ent = ents.Create("sur_loot")
    ent.Weapon = weapon
    ent:SetPos(pos)
    ent.NoRemove = true
    ent.obj = true
    ent:Spawn()
    if IsValid(ent:GetPhysicsObject()) then
        ent:GetPhysicsObject():SetVelocity(VectorRand(-100,100))
    end
    ent.IsLoot = true
end

function SuR:SpawnEnemy()
    if SuR.Delay_For_Spawn_Zombie > CurTime() or !SuR.Game_Started or SuR.Enemy_To_Spawn == 0 or !SuR.Wave_Started or SuR:CurrentAliveEnemies() >= SuR.Max_Zombies_On_Map then return end

    local del = CurTime()+(SuR.Config.Zombie_Spawn_Delay/(SuR.Current_Wave/2))
    if SuR.ExfilWave or SuR.Config.Objective_Mode then
        del = CurTime()+1
    end
    SuR.Delay_For_Spawn_Zombie = del
    local pos = SuR:GetRandomPos(SuR.Config.Enable_Spawn_In_Building_Zombies, SuR.Config.Zombie_Spawn_Radius_Min, SuR.Config.Zombie_Spawn_Radius_Max)
    if math.random(1,10) == 1 or !isvector(pos) then
        pos = SuR:GetRandomPos(!SuR.Config.Enable_Spawn_In_Building_Zombies, SuR.Config.Zombie_Spawn_Radius_Min, SuR.Config.Zombie_Spawn_Radius_Max)
    end
    local class, isspecial = SuR:GetRandomClass("zombie")

    if isvector(pos) then
        local ent = ents.Create(class)
        ent:SetPos(pos+Vector(0,0,4))
        ent.TimeBeforeDeath = CurTime()+120
        ent:Spawn()
        ent:SetHealth(ent:Health()+math.floor(2*SuR.Current_Wave*(SuR.Config.Zombie_Multiplier_Per_Wave+SuR.Config.Zombie_Multiplier_Per_Player)))
        if SuR.ActiveMutator == 4 then
            ent:SetHealth(ent:Health()/4)
        end
        ent.IsZombie = true
        ent.FollowPlayer = false
        ent.IsSpecial = isspecial
        ent:RewriteLNR(isspecial)
        SuR.Enemy_To_Spawn = SuR.Enemy_To_Spawn - 1
        timer.Create("ZombieMoveLogic"..ent:EntIndex(), 30, 0, function()
            if IsValid(ent) then
                local ply = SuR:PickRandomPlayer(true)
                if IsValid(ply) then
                    local pos = ply:GetPos()
                    ent:SetLastPosition(pos)
                    ent:ClearCondition(COND_ENEMY_UNREACHABLE)
                    ent:VJ_TASK_GOTO_LASTPOS()
                end
            else
                timer.Remove("ZombieMoveLogic"..ent:EntIndex())
            end
        end)
    end
end

hook.Add("SetupPlayerVisibility", "SuR_FixBossBar", function()
    if IsValid(SuR.Gas_Entity) then
        AddOriginToPVS(SuR.Gas_Entity:GetPos())
    end
    if not SuR.Config.Allow_PVS_Info then return end
	if IsValid(SuR.Current_Boss) then
		AddOriginToPVS(SuR.Current_Boss:GetPos())
	end
    if SuR.Config.Objective_Mode then
        for _, ent in ipairs(ents.FindByClass("sur_*")) do
            if !ent.obj and ent:GetModel() != "models/surrounded/obj/w_jerrycanblue.mdl" then return end
            AddOriginToPVS(ent:GetPos())
        end
    end
    for _, ply in ipairs(player.GetAll()) do
        if !ply:IsSurvivor() then return end
        AddOriginToPVS(ply:GetPos())
    end
end)

local clear_delay = 0
local airdrop_delay = 0
hook.Add("Think", "SuR_GameLogic", function()
    if SuR.Game_Started and SuR.Current_Wave > 0 then
        if SuR.Time_Before_Wave < CurTime() and not SuR.Wave_Started then
            SuR:StartWave()
            hook.Call("SuR.WaveState", nil, SuR.Current_Wave, true)
        end

        if not SuR.TimerCountdown and SuR.Time_Before_Wave <= CurTime()+10 and not SuR.Wave_Started then
            SuR.TimerCountdown = true
            SuR:Countdown(10) 
        end
        
        if SuR.Wave_Started then
            SuR:SpawnEnemy()

            if SuR.ExfilWave and SuR.Enemy_To_Spawn <= 0 then
                SuR.Enemy_To_Spawn = 1
            end

            if airdrop_delay < CurTime() and SuR.ActiveMutator == 8 then
                SuR:CallSupport(nil, 1)
                airdrop_delay = CurTime()+60
            elseif SuR.ActiveMutator != 8 then
                airdrop_delay = CurTime()+60
            end
            
            if SuR:CurrentAliveEnemies() == 0 and SuR.Enemy_To_Spawn <= 0 then
                SuR:ChangeWave()
                hook.Call("SuR.WaveState", nil, SuR.Current_Wave, false)
            end

            if SuR:CurrentAliveEnemies() <= 3 then
                local tab = ents.FindByClass("npc_*")
                for i=1,#tab do
                    local ent = tab[i]
                    if not ent.IsZombie then continue end
                    ent.TimeOverDeath = ent.TimeOverDeath - FrameTime()
                    if ent.TimeOverDeath < 0 and not ent.NoRemove then
                        ent:Remove()
                    end
                end
            end

            SuR:SendDataToClient("Enemies", SuR:CurrentAliveEnemies()+SuR.Enemy_To_Spawn)

            if clear_delay < CurTime() then
                RunConsoleCommand("ai_clear_bad_links")
                clear_delay = CurTime()+1
            end
        end

        if SuR.Config.Objective_Mode then
            SuR:SendDataToClient("MaxFuel", SuR.ObjectiveFuelNeed)
            SuR:SendDataToClient("Fuel", SuR.ObjectiveFuel)
        end

        local tab = SuR:CurrentFighters(player.GetCount() == 1)
        if tab > 0 then
            SuR.Delay_Before_Lose = CurTime()+10 
        end

        if !SuR.Wave_Started then
            SuR.Delay_Before_Lose = CurTime()+10 
        end

        local tab = ents.FindByClass("npc_*")
        for i=1,#tab do
            local ent = tab[i]
            if not ent.IsZombie then continue end
            local en = ent:GetEnemy()
            if ent.ExecutingTarget then
                if !IsValid(ent.ExecutingModel) then
                    ent:SetPos(ent.ExecutingPos)
                end
            end
            if !ent:IsInWorld() and not ent.ExecutingTarget then
                ent:TakeDamage(100)
            end
            if !ent.NoRemove then
                if IsValid(en) and !ent:HasCondition(31) and not SuR.Config.Objective_Mode then
                    ent.TimeBeforeDeath = CurTime()+60
                end
                if ent.TimeBeforeDeath < CurTime() then
                    ent:TakeDamage(100)
                end
            end
            if ent.ExecutingTarget then
                ent:SetState(VJ_STATE_FREEZE)
            end
            ent:SetNWBool('flinching', ent.Flinching)
        end

        if SuR.Delay_Before_Lose > CurTime() then
            SuR:SpawnLoot()
        else
            net.Start("SuR.GameOverScreen")
            net.WriteFloat(SuR.Current_Wave)
            net.WriteBool(SuR.ExfilSuccess)
            net.Broadcast()
            SuR:ChangeStateOfGame(false)
            timer.Simple(SuR.Config.Game_Over_Screen_Delay, function()
                if SuR.Game_Started then return end
                SuR:ShowDeployCutscene(player.GetAll(), true)
            end)
        end
        SuR.DeployTime = CurTime()+SuR.Config.Delay_Before_Deploy
    end
    if not SuR.Game_Started then
        if player.GetCount() > 0 and not SuR.DisableLogic then
            if SuR.DeployTime < CurTime() then
                SuR:ChangeStateOfGame(true)
            end
        else
            SuR.DeployTime = CurTime()+SuR.Config.Delay_Before_Deploy+30
        end
    end
    SuR:SendDataToClient("Deploying", !SuR.Game_Started)
    SuR:SendDataToClient("DeployTime", SuR.DeployTime)
    SuR:SendDataToClient("ExfilWave", SuR.ExfilWave)
    SuR:SendDataToClient("DisableMusic", SuR.DisableMusic)
end)

hook.Add("OnEntityCreated", "SuR_NPCChanges", function(ent)
	if ent:GetClass() == "npc_vj_lnr_evo" or ent:GetClass() == "npc_vj_lnr_ravager" then
		ent.LeapDistance = 400 
		ent.LeapToMeleeDistance = 400
        ent.NextLeapAttackTime = 10
        ent.NextAnyAttackTime_Leap = 10
	end
end)

concommand.Add("sur_startgame", function(ply)
    if !ply:IsSuperAdmin() or SuR.Game_Started then return end
    SuR:ChangeStateOfGame(true)
    SuR:ShowDeployCutscene(player.GetAll(), false)
end)

concommand.Add("sur_skipwave", function(ply, smd, args)
    if !ply:IsSuperAdmin() or !SuR.Game_Started then return end

    local arg = tonumber(args[1])
    if isnumber(arg) then
        SuR.Current_Wave = arg-1
    end
    SuR.Enemy_To_Spawn = 0
    local tab = ents.FindByClass("npc_*")
    for i=1,#tab do
        local ent = tab[i]
        if not ent.IsZombie then continue end
        ent:Remove()
    end
end)