@name Capture the flag
@outputs RP BP C:entity R:array B:array Rem Mode Index Name:string BAtBase RAtBase CarrierR:entity CarrierB:entity
@persist Pr Pb Fr:entity Fb:entity  Or:vector Ob:vector
#Thank you for trying out my E2, this is still a work in progress, so any bugs you notice will
#most likely be fixed in the future.

#to get started type /pr to place the red flag. then /pb to place the blue flag.

#once the flags have been placed there is no way to move them without respawning or reloading the E2. 

#if there is an error such as a duplicate of a player on the other team, or on the same team-
#- then just type /rem, then >b or >r for the team. then the index in that array and it will remove-
#- that index from the array.

# i strongly suggest that you DO NOT tamper with anything below this point, as it is very sensitive-
#- to correct operation

























interval(25)
runOnChat(1)
if (first()){
    concmd("say [CTF]: CTF V0.1 By AJ10017. Type /R to join Red. Type /B to join Blue.")
}

for (I=1,players():count()){
    C=players()[I,entity]
    
    
    
    #CTF section
    
    
        if (B[I,entity]:pos():distance(Or)<100&RAtBase==1){
            CarrierB=B[I,entity]
            RAtBase=0
            concmd("say [CTF]: "+CarrierB:name()+" has taken Red teams flag!")    
    }

    
        if (R[I,entity]:pos():distance(Ob)<100&BAtBase==1){
            CarrierR=R[I,entity]
            BAtBase=0
            concmd("say [CTF]: "+CarrierR:name()+" has taken Blue teams flag!")
    }

    
    #Chat section
    if (chatClk(C)&C:lastSaid():lower()=="/r"){
        B:remove(I)
        
        R:pushEntity(C)
        
        concmd("say [CTF]: "+C:name()+" Joined team Red!")
    }
    if (chatClk(C)&C:lastSaid():lower()=="/b"){
        R:remove(I)
        
        B:pushEntity(C)
        
        concmd("say [CTF]: "+C:name()+" Joined team Blue!")
    }
    if (chatClk(C)&C:lastSaid():lower()=="/leave"){
        if (C==R[I,entity]){
            R:remove(I)
            
            concmd("say [CTF]: "+C:name()+" has left team Red!")
        }
        if (C==B[I,entity]){
            B:remove(I)
            
            concmd("say [CTF]: "+C:name()+" has left team Blue!")
        }
    }
    
    if (B[I,entity]==R[I,entity]){
        R:remove(I)
    }
    if (R[I,entity]==B[I,entity]){
        B:remove(I)
    }
}

if (chatClk(owner())&owner():lastSaid():lower()=="/rem"){
    hint("what team? B or R",9)
    hideChat(3)
    Rem=1
}

if (Rem==1){
    if (chatClk(owner())&owner():lastSaid():lower()==">b"){
        hint("What index? 1 to "+B:count(),9)
        hideChat(3)
        Mode=1
        Rem=0
    }
    if (chatClk(owner())&owner():lastSaid():lower()==">r"){
        hint("What index? 1 to "+R:count(),9)
        hideChat(3)
        Mode=2
        Rem=0
    }
}


    if (Mode>0&Rem==0){
        Index=owner():lastSaid():toNumber()
        if (chatClk(owner())&Mode==1&Index>0){
            Index=owner():lastSaid():toNumber()
        hint("Index Value = "+Index,6)
        hint("attempting force remove variable...",6)
            Name=B[Index,entity]:name()
            B:remove(Index)
            timer("T1",1000)
            Mode=0
            }
        if (chatClk(owner())&Mode==2&Index>0){
            Index=owner():lastSaid():toNumber()
        hint("Index Value = "+Index,6)
        hint("attempting force remove variable...",6)
            Name=R[Index,entity]:name()
            R:remove(Index)
            timer("T2",1000)
            Mode=0
            }
}

if (clk("T1")){concmd("say [CTF]: owner removed player index: "+Index+" from team array: Blue. Player Name = "+Name)
Index=0
Name=""
}
if (clk("T2")){concmd("say [CTF]: owner removed player index: "+Index+" from team array: Red. Player Name = "+Name)
Index=0
Name=""
}

#flag placement

if (chatClk(owner())&owner():lastSaid():lower()=="/pr"&Pr==0){
    holoCreate(1)
    holoModel(1,"models/props_trainstation/TrackSign07.mdl")
    holoDisableShading(1,1)
    holoColor(1,vec(255,0,0))
    holoMaterial(1,"models/debug/debugwhite")
    holoPos(1,owner():aimPos()+vec(0,0,50))
    Or=holoEntity(1):pos()
    holoAng(1,ang(0,(holoEntity(1):pos()-owner():pos()):toAngle():yaw()+0,0))
    Fr=holoEntity(1)
    Pr=1
    RAtBase=1
    holoCreate(3)
    holoModel(3,"models/props_junk/sawblade001a.mdl")
    holoColor(3,vec(255,100,100))
    holoDisableShading(3,1)
    holoMaterial(3,"models/debug/debugwhite")
    holoPos(3,owner():aimPos())
}
    
if (chatClk(owner())&owner():lastSaid():lower()=="/pb"&Pb==0){
    holoCreate(2)
    holoModel(2,"models/props_trainstation/TrackSign07.mdl")
    holoDisableShading(2,1)
    holoColor(2,vec(0,0,255))
    holoMaterial(2,"models/debug/debugwhite")
    holoPos(2,owner():aimPos()+vec(0,0,50))
    Ob=holoEntity(2):pos()
    holoAng(2,ang(0,(holoEntity(2):pos()-owner():pos()):toAngle():yaw()+0,0))
    Fb=holoEntity(2)
    Pb=1
    BAtBase=1
    holoCreate(4)
    holoModel(4,"models/props_junk/sawblade001a.mdl")
    holoColor(4,vec(100,100,255))
    holoDisableShading(4,1)
    holoMaterial(4,"models/debug/debugwhite")
    holoPos(4,owner():aimPos())
}
if (Pr==1&Pb==1){    
if (CarrierR:isAlive()&BAtBase==0){
    holoPos(2,CarrierR:pos()+vec(0,0,200))
}
if (CarrierB:isAlive()&RAtBase==0){
    holoPos(1,CarrierB:pos()+vec(0,0,200))
}
    if (CarrierB:pos():distance(Ob)<100){
        BP+=1
        CarrierB=noentity()
        holoPos(1,Or)
        RAtBase=1
        
    }
    if (CarrierR:pos():distance(Or)<100){
        RP+=1
        CarrierR=noentity()
        holoPos(2,Ob)
        BAtBase=1
        
    }
if (changed(RP)&RP>0){concmd("say [CTF]: Red Scores! Scores are now Red: "+RP+". Blue: "+BP+".")}
if (changed(BP)&BP>0){concmd("say [CTF]: Blue Scores! Scores are now Red: "+RP+". Blue: "+BP+".")}
}

holoAng(1,holoEntity(1):angles()+ang(0,5,0))
holoAng(2,holoEntity(2):angles()+ang(0,5,0))


if (CarrierB:isAlive()==0&RAtBase==0&CarrierB!=noentity()){
    CarrierB=noentity()
    RAtBase=1
    holoPos(1,Or)
    concmd("say [CTF]: Red Flag dropped and returned to Red Base due to flag carrier death!")
}
if (CarrierR:isAlive()==0&BAtBase==0&CarrierR!=noentity()){
    CarrierR=noentity()
    BAtBase=1
    holoPos(2,Ob)
    concmd("say [CTF]: Blue Flag dropped and returned to Blue Base due to flag carrier death!")
}
