@name Countdown Timer

@inputs 
@inputs In:table
@outputs Idx T:entity Time [Min Max]:vector Last:entity
@persist EGP:wirelink E:entity On HTimer
runOnChat(1)
runOnTick(50)
if( first() )
{
    
    E = entity():isWeldedTo() ?: entity()
    O = owner()
    
    HTimer=HTimer+1*1
    HT=HTimer
    
    findIncludePlayerProps(O)
    #findByClass("gmod_wire_egp_emitter")
    findByClass("gmod_wire_egp")
    EGPent = findClosest(E:pos())
    EGP = EGPent:wirelink()
    
    function number getID( N )
    {
        return EGP:egpNumObjects() + N
    }
    
    EGP:egpClear()
    
    EGP:egpRoundedBox( getID(1), vec2(256,256), vec2(512,512) )
    EGP:egpColor( getID(0), hsv2rgb( 200, 1, 1 ) )
    EGP:egpAlpha( getID(0), 150 )
    
    EGP:egpTextLayout( getID(1), "name", vec2(), vec2(512,512) )
    EGP:egpAlign( getID(0), 1, 1 )
    EGP:egpFont( getID(0), "", 40 )
    EGP:egpColor( getID(0), vec() )
    ##############
    Time = 60
    ##############
    Bx = E:aabbSize()
    
    V1 = E:toWorld(vec(Bx[1]/2,Bx[2]/2,0))
    V2 = E:toWorld(vec(-Bx[1]/2,-Bx[2]/2,Bx[1]))
    
    Min = vec( min( V1[1], V2[1] ),
                min( V1[2], V2[2] ),
                min( V1[3], V2[3] )
                )
    Max = vec( max( V1[1], V2[1] ),
                max( V1[2], V2[2] ),
                max( V1[3], V2[3] )
                )
                
    ReloadE2 = 0



#    holoCreate( 1, mix( V1, V2, 0.5 ), -positive(V1-V2) / 12, E:angles() )
#    holoParent( 1, E )

    #A = propSpawn("models/props_phx/construct/plastic/plastic_angle_360.mdl", E:pos(), 1)
    E:setAlpha(1) 

    holoCreate(1)
    holoModel(1, "models/props_phx/construct/plastic/plastic_angle_360.mdl")
    holoMaterial(1,"models/gibs/metalgibs/metal_gibs")
    holoPos(1,E:pos()+vec(0,0,-1))
    
    holoCreate(2)
    holoModel(2, "models/props_phx/construct/plastic/plastic_angle_360.mdl")
    holoMaterial(2,"models/gibs/metalgibs/metal_gibs")
    holoPos(2,E:pos()+vec(0,0,0.8))
    holoScale(2, vec(0.8,0.8,0.8))
    
    holoCreate(4)
    holoModel(4, "models/props_trainstation/trainstation_clock001.mdl")
    holoMaterial(4,"models/gibs/metalgibs/metal_gibs")
    holoPos(4,E:pos()+vec(0,0,3))
    holoScale(4, vec(0.7,0.6,0.6))
    holoAng(4,ang(90,0,0))
    
    
    #rangerFilter(A)
    #local R = rangerOffset(1000, A:pos(), A:forward())
    
    lightCreate(1,entity():pos(),vec(20,20,20)) 
    
    findClearBlackList()
    findClearWhiteList()
    
     On = On ? 0 : 1
    
    function sendMessageAll(S:string) {
        luaRunAll("chat.AddText(Color(255, 150, 0), [[[Capture Point] ]], Color(255, 255, 255), [[" + S:replace("%hl", "]], Color(255, 150, 0), [["):replace("%no", "]], Color(255, 255, 255), [[") + "]])")
    }
    
}

interval(50)

if( findCanQuery() )
{
    findByClass("player")
    findClipToBox(Min,Max)
    T = findClosest(E:pos())
}

if( changed(T) )
{   
    luaRunAll("
    hook.Add( 'PreDrawHalos', 'AddHalos', function()
	local staff = {}

	for k, v in pairs( player.GetAll() ) do
		if string.find( v:Nick(), '"+ Last:name() +"' ) then
			table.insert( staff, v )
		end
	end

	halo.Add( staff, Color(255,0,0), 5, 5, 5, true, true )
end )
    ") 
    lightColor(1,vec(20,20,20))
    Time = 60
    ########################
    if( !Time ){ Time = 60 }
    ########################
    if( T )
    {
        Last = T
        EGP:egpSetText( 2, T:name() + "\n\nCOUNTDOWN\n\n"+Time )
        timer("countdown",60)
      
    }
    else
    {
        luaRunAll("hook.Remove('HUDPaint','ThePurgeHUD')")
         luaRunAll("hook.Remove('PreDrawHalos','AddHalos')")
        stoptimer("countdown")
    }
    
}

if( clk("countdown") & Time )
{
    Time = max( 0, Time - 1 )
    if( Time >= 5 )
    {
        soundPitch("count",Time>=0 ? 200 : 100)
        soundVolume("count",Time>=0 ? 0.1 : 1)
        E:soundPlay( "count", 0, "@synth/saw.wav", 0.5 )
    }
    else
    {
        #soundPurge()
        #E:soundPlay( "count", 0, "@buttons/weapon_cant_buy.wav", 0.0 )
    }
        if( Time == 5 )
    {
            luaRunAll("

    sound.PlayURL('http://cdn.frustra.org/sounds/sound_tf2/vo/announcer_begins_5sec.mp3?id=24', '3d', function(st)
        if(IsValid(st)) then
            st:SetPos(Entity(" + owner():id() + "):GetPos())
        end
    end)

    ")
        #E:soundPlay( "count", 0, "@synth/saw.wav", 0.5 )
    }
    else
    {
        #soundPurge()
        #E:soundPlay( "count", 0, "@buttons/weapon_cant_buy.wav", 0.5 )
        #E:soundPlay( "count", 0, "@synth/saw.wav", 0.5 )
    }
      if( Time == 4 )
    {
            luaRunAll("

    sound.PlayURL('http://cdn.frustra.org/sounds/sound_tf2/vo/announcer_begins_4sec.mp3?id=23', '3d', function(st)
        if(IsValid(st)) then
            st:SetPos(Entity(" + owner():id() + "):GetPos())
        end
    end)

    ")
        #E:soundPlay( "count", 0, "@synth/saw.wav", 0.5 )
    }
    else
    {
        #soundPurge()
        #E:soundPlay( "count", 0, "@buttons/weapon_cant_buy.wav", 0.5 )
        #E:soundPlay( "count", 0, "@synth/saw.wav", 0.5 )
    }
      if( Time == 3 )
    {
            luaRunAll("

    sound.PlayURL('http://cdn.frustra.org/sounds/sound_tf2/vo/announcer_begins_3sec.mp3?id=22', '3d', function(st)
        if(IsValid(st)) then
            st:SetPos(Entity(" + owner():id() + "):GetPos())
        end
    end)

    ")
        #E:soundPlay( "count", 0, "@synth/saw.wav", 0.5 )
    }
    else
    {
        #soundPurge()
        #E:soundPlay( "count", 0, "@buttons/weapon_cant_buy.wav", 0.5 )
        #E:soundPlay( "count", 0, "@synth/saw.wav", 0.5 )
    }
      if( Time == 2 )
    {
            luaRunAll("

    sound.PlayURL('http://cdn.frustra.org/sounds/sound_tf2/vo/announcer_begins_2sec.mp3?id=21', '3d', function(st)
        if(IsValid(st)) then
            st:SetPos(Entity(" + owner():id() + "):GetPos())
        end
    end)

    ")
        #E:soundPlay( "count", 0, "@synth/saw.wav", 0.5 )
    }
    else
    {
        #soundPurge()
        #E:soundPlay( "count", 0, "@buttons/weapon_cant_buy.wav", 0.5 )
        #E:soundPlay( "count", 0, "@synth/saw.wav", 0.5 )
    }
      if( Time == 1 )
    {
            luaRunAll("

    sound.PlayURL('http://cdn.frustra.org/sounds/sound_tf2/vo/announcer_begins_1sec.mp3?id=20', '3d', function(st)
        if(IsValid(st)) then
            st:SetPos(Entity(" + owner():id() + "):GetPos())
        end
    end)

    ")
        #E:soundPlay( "count", 0, "@synth/saw.wav", 0.5 )
    }
    else
    {
        #soundPurge()
        #E:soundPlay( "count", 0, "@buttons/weapon_cant_buy.wav", 0.5 )
        #E:soundPlay( "count", 0, "@synth/saw.wav", 0.5 )
    }
    soundPitch("count",Time>=0 ? 200 : 100)
    soundVolume("count",Time>=0 ? 0.1 : 1)
    timer("countdown",1000)
}

if( changed(Time) )
{
    
    #EGP:egpSetText( 2, Last:name() + "\n\nCOUNTDOWN\n\n"+Time )
     luaRunAll(" surface.CreateFont('PurgeFont_Title', {font = 'Coolvetica', size = 40, weight = 4000})
        surface.CreateFont('PurgeFont_Time', {font = 'Coolvetica', size = 35, weight = 1000})
        surface.CreateFont('PurgeFont_Announce', {font = 'Coolvetica', size = 80, weight = 4000})
        hook.Add('HUDPaint', 'ThePurgeHUD', function()
	           draw.SimpleText('" + Time + " Until Capture', 'PurgeFont_Title', ScrW() / 2, ScrH() - 52, Color(0, 150, 0), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER)
        end )")
        if(Time<31){
          luaRunAll(" surface.CreateFont('PurgeFont_Title', {font = 'Coolvetica', size = 40, weight = 4000})
        surface.CreateFont('PurgeFont_Time', {font = 'Coolvetica', size = 35, weight = 1000})
        surface.CreateFont('PurgeFont_Announce', {font = 'Coolvetica', size = 80, weight = 4000})
        hook.Add('HUDPaint', 'ThePurgeHUD', function()
	           draw.SimpleText('" + Time + " Until Capture', 'PurgeFont_Title', ScrW() / 2, ScrH() - 52, Color(255, 150, 0), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER)
        end )")  
        
            luaRunAll("
    hook.Add( 'PreDrawHalos', 'AddHalos', function()
	local staff = {}

	for k, v in pairs( player.GetAll() ) do
		if string.find( v:Nick(), '"+ Last:name() +"' ) then
			table.insert( staff, v )
		end
	end

	halo.Add( staff, Color((255, 150, 0), 5, 5, 5, true, true )
end )
    ")
        }
         if(Time<16){
          luaRunAll(" surface.CreateFont('PurgeFont_Title', {font = 'Coolvetica', size = 40, weight = 4000})
        surface.CreateFont('PurgeFont_Time', {font = 'Coolvetica', size = 35, weight = 1000})
        surface.CreateFont('PurgeFont_Announce', {font = 'Coolvetica', size = 80, weight = 4000})
        hook.Add('HUDPaint', 'ThePurgeHUD', function()
	           draw.SimpleText('" + Time + " Until Capture', 'PurgeFont_Title', ScrW() / 2, ScrH() - 52, Color(255, 0, 0), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER)
        end )") 
         
        luaRunAll("
        
    hook.Add( 'PreDrawHalos', 'AddHalos', function()
	local staff = {}

	for k, v in pairs( player.GetAll() ) do
		if string.find( v:Nick(), '"+ Last:name() +"' ) then
			table.insert( staff, v )
		end
	end

	halo.Add( staff, Color((255, 0, 0), 5, 5, 5, true, true )
end )
    ")
        }
    if(Time==60){
     luaRunAll("hook.Remove('HUDPaint','ThePurgeHUD')")   
    }
    
    if( !Time )
    {
        #lightCreate(2,entity():pos(),vec(20,0,0)) 
        lightColor(1,(teamColor(Last:team())))
       # E:soundPlay("bell",0,"@buttons/bell1.wav")
        if( Last:isValid() )
        {
            EGP:egpSetText( 2, Last:name() + "\n\n Has Captured The Point!" )
                luaRunAll("
        surface.CreateFont('PurgeFont_Title', {font = 'Coolvetica', size = 40, weight = 4000})
        surface.CreateFont('PurgeFont_Time', {font = 'Coolvetica', size = 35, weight = 1000})
        surface.CreateFont('PurgeFont_Announce', {font = 'Coolvetica', size = 80, weight = 4000})
        hook.Add('HUDPaint', 'ThePurgeHUD', function()
	           draw.SimpleText('" + Last:name() + " Captured The Control Point!', 'PurgeFont_Title', ScrW() / 2, ScrH() - 52, Color(255, 150, 0), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER)
        end )
                
    sound.PlayURL('http://cdn.frustra.org/sounds/sound_tf2/vo/announcer_we_captured_control.mp3?id=9', '3d', function(st)
        if(IsValid(st)) then
            st:SetPos(Entity(" + owner():id() + "):GetPos())
        end
    end)
    
    
     hook.Remove('HUDPaint','ThePurgeHUD')
   
    
    ")
    CapPointNum = 1
    sendMessageAll(Last:name()+" has captured the point!")
    #sendMessageAll("Players Team is: "+Last:team())
    sendMessageAll("This is Capture Point "+CapPointNum)
    luaRunAll("")
    

    # E:soundPlay("win",5,"@misc/boring_applause_1.wav")
    # E:soundPlay("win",5,"@misc/happy_birthday.wav")
    # concmd("say \"/advert "+Last:name()+ "  Has Captured The Point!\"")
    # concmd("rp_tellall "+Last:name()+ "  Has Captured The Point!"
        }
        else
        {
            EGP:egpSetText( 2, "THERE IS NO WINNER\nYOU ALL FAIL" )
        }
    }
    
  
}
