@name Interactive Targeting
#@inputs N
#@outputs Orders:string
@outputs Target:array Passoff:entity Formation Self On Kill
@outputs ZSC:table
#@outputs TID T1 T2 T3 Dist:array Count Counted Targets:array
@outputs Command:string Network
@persist Players:array Chat1:string Last Dist:array Passoff:entity Targets:array
@persist Play:array A B C D Self Net1 Net0 Network Command:string
@persist Once MultiCom:array On PA:array
if(ops()>4800){exit()}
Time = 500
interval(Time)
gSetGroup("ZapSat")
if(duped()){reset()}
if (first()) {Passoff = entity():owner() Network=0 Self=0}
entity():setColor(0,140,255)
Box = entity():isConstrainedTo()
if (Network) {   
    if (Self==1) {Command = "Zap1" Box:setColor(255,0,0)
}
if (Self==2) {Command = "Zap2" Box:setColor(0,255,0)
  
}
if (Self==3) {Command = "Zap3" Box:setColor(0,0,255)
}
if (Self==4) {Command = "Zap4" Box:setColor(255,255,0)
}
}
if (Self==0) {Command = "Zap"  Box:setColor(255,255,255)
Orders = ""
}
Pa=")"
Menu = "menu"
Attack = "get" 
List = "list"
if(first()){
    PA=array()
    PA:pushString("1")
    PA:pushString("2")
    PA:pushString("3")
    PA:pushString("4")
    PA:pushString("5")
    PA:pushString("6")
    PA:pushString("7")
    PA:pushString("8")
    PA:pushString("9")
    PA:pushString("0")
}
#P1 = "1"
#P2 = "2"
#P3 = "3"
#P4 = "4"
#P5 = "5"
#P6 = "6"
#P7 = "7"
#P8 = "8"
#P9 = "9"
#P10 = "0"
PKC = "PK"
NKC = "NK"
Pass = "pass"
Back = "back"
ModeQ = "mode"
S = " "
Wide = "zw" 
Form="form"
Tight = "zt"
NetOn = "linkup"
NetOff = "unlink"
CommandMaster = "Zap0"
PKMode="Player Kill Mode Enabled"
NKMode="NPC Kill Mode Enabled"
Cs=Command+": "
Orb = "Now Orbiting: "
Breaker="<"
Power="power"
Suicide="destruct"

ChatO = Passoff:lastSaid()
Chat = entity():owner():lastSaid()
runOnChat(1)
K=chatClk()
#K = curtime() - entity():owner():lastSaidWhen() < (Time*1)/1000
if (K) {Once=1}
Com = Chat:find(Command)
M = Chat:find(Menu)
A = Chat:find(Attack)
L = Chat:find(List)
CA=array()
for(C=1,10){
    CA[C,number]=Chat:find(PA[C,string])
}
#C1 = Chat:find(P1)
#C2 = Chat:find(P2)
#C3 = Chat:find(P3)
#C4 = Chat:find(P4)
#C5 = Chat:find(P5)
#C6 = Chat:find(P6)
#C7 = Chat:find(P7)
#C8 = Chat:find(P8)
#C9 = Chat:find(P9)
#C10 = Chat:find(P10)
PK = Chat:find(PKC)
NK = Chat:find(NKC)
if(duped()|first()){NK=1}
P = Chat:find(Pass)
BK = Chat:find(Back)
Mode = Chat:find(ModeQ)
CM = Chat:find(CommandMaster)
ModeT = ChatO:find(Tight)
ModeW = ChatO:find(Wide)
Form1=Chat:find(Form)
Net1 = Chat:find(NetOn)
Net0 = Chat:find(NetOff)
Powered = Chat:find(Power)
Break = Chat:find(Breaker)
Suicided = Chat:find(Suicide)
if (K&Com) {
    if (ModeT) {Formation = 1}
    if (ModeW) {Formation = 0}
    if (Net1&C&Once) {Network = 1 print(Cs+"Networking Enabled") Once=0 }
    if (Net0&CM&Once) {Network = 0 print(Cs+"Networking Disabled") Once=0}
    if (CM&Break) {MultiCom = Chat:explode(Breaker)}
    if (MultiCom:count()!=0&Self==1) {Orders=MultiCom:string(2)}
    if (MultiCom:count()!=0&Self==2) {Orders=MultiCom:string(3)}
    if (MultiCom:count()!=0&Self==3) {Orders=MultiCom:string(4)}
    if (MultiCom:count()!=0&Self==4) {Orders=MultiCom:string(5)}
    if (Break&MultiCom:count()) {
    CA=array()
    for(C=1,10){
        CA[C,number]=Orders:find(PA[C,string])
    }
    #C1 = Orders:find(P1)
    #C2 = Orders:find(P2)
    #C3 = Orders:find(P3)
    #C4 = Orders:find(P4)
    #C5 = Orders:find(P5)
    #C6 = Orders:find(P6)
    #C7 = Orders:find(P7)
    #C8 = Orders:find(P8)
    #C9 = Orders:find(P9)
    #C10 = Orders:find(P10)
    }
}


Origin = entity():owner():pos()
findInSphere(Origin,1000000)
findSortByDistance(Origin)
findIncludeClass("player")
Play = findToArray()
if ((Com|CM)&K){
    if (Mode&PK) {print(Cs+PKMode)}
    if (Mode&NK) {print(Cs+NKMode)}
    if (PK) {print(Cs+PKMode)}
    if (NK) {print(Cs+NKMode)}
    if (Powered&CA[1,number]){On=!On print(Cs+"Toggling Power state")}
    if (Powered&CA[10,number]){print(Cs+"Skipping power toggle")}
    if (Suicided&CA[1,number]){print(Cs+"Self Destructing") selfDestructAll()}
    if (Form1&CA[1,number]){Formation=1 print(Cs+"Tight Formation")}
    if (Form1&CA[10,number]){Formation=0 print(Cs+"Wide Formation")}
}
if (Network) {
    An = gGetNum("An")
    Bn = gGetNum("Bn")
    Cn = gGetNum("Cn")
    Dn = gGetNum("Dn")
    if (Network & Self==0 & An==0) {Self = 1 An = 1}
    if (Network & Self==0 & Bn==0) {Self = 2 Bn = 2}
    if (Network & Self==0 & Cn==0) {Self = 3 Cn = 3}
    if (Network & Self==0 & Dn==0) {Self = 4 Dn = 4}
    gSetNum("An",An)
    gSetNum("Bn",Bn)
    gSetNum("Cn",Cn)
    gSetNum("Dn",Dn)
} else {
    Self=An=Bn=Cn=Dn=0
    gDeleteNum("An")
    gDeleteNum("Bn")
    gDeleteNum("Cn")
    gDeleteNum("Dn")
}



if ((CM|Com)&K){
    if(PK) {Kill=1}
    if(NK) {Kill=0}
    if(M){ 
    Players=Play:clone()
    Count=1
    PC=Play:count()
    Pad=Pa+S
    Pl0="Targets: "
    Concat=array()
    Concat:pushString("Targets: ")
    for(C=0,9){
        if(PC>C){Concat:pushString(PA[C+1,string]+Pad+Play[C+1,entity]:name())}
    }
    #if(PC>0){Pl1=P1+Pad+Play:entity(1):name()+S}else{Pl1=""}
    #if(PC>1){Pl2=P2+Pad+Play:entity(2):name()+S}else{Pl2=""}
    #if(PC>2){Pl3=P3+Pad+Play:entity(3):name()+S}else{Pl3=""}
    #if(PC>3){Pl4=P4+Pad+Play:entity(4):name()+S}else{Pl4=""}
    #if(PC>4){Pl5=P5+Pad+Play:entity(5):name()+S}else{Pl5=""}
    #if(PC>5){Pl6=P6+Pad+Play:entity(6):name()+S}else{Pl6=""}
    #if(PC>6){Pl7=P7+Pad+Play:entity(7):name()+S}else{Pl7=""}
    #if(PC>7){Pl8=P8+Pad+Play:entity(8):name()+S}else{Pl8=""}
    #if(PC>8){Pl9=P9+Pad+Play:entity(9):name()+S}else{Pl9=""}
    #if(PC>9){Pl10=P10+Pad+Play:entity(10):name()+S}else{Pl10=""}
    #print(Cs+Pl0+Pl1+Pl2+Pl3+Pl4+Pl5+Pl6+Pl7+Pl8+Pl9+Pl10)
    print(Concat:concat("\n"))
    }
    if (A) {
    Targets = array()
        for(C=1,10){
            if(CA[C,number]){Targets:pushEntity(Players[C,entity])}
        }
    if (P) {
        if (BK){Passoff = entity():owner()} else {
        for(C=1,10){
            if(CA[C,number]){
            E=0
                for(D=1,10){
                    if(D!=C&CA[D,number]){E=1
                }
                if(!E){
                    Passoff=Players[C,entity]
                    if(Once){Once=0 print(Cs+Orb+Players[C,entity]:name())}
                }
                }
            }
        }
        }
    }
}
#    
#if (C1 & !(C2|C3|C4|C5|C6|C7|C8|C9|C10)) {
#Passoff = Players:entity(1) 
#if (Once) {Once=0 print(Cs+Orb+Players:entity(1):name())}}
#
#if (C2 & !(C1|C3|C4|C5|C6|C7|C8|C9|C10)) {
#Passoff = Players:entity(2) 
#if (Once) {Once=0 print(Cs+Orb+Players:entity(2):name())}}
#
#if (C3 & !(C2|C1|C4|C5|C6|C7|C8|C9|C10)) {
#Passoff = Players:entity(3) 
#if (Once) {Once=0 print(Cs+Orb+Players:entity(3):name())}}
#
#if (C4 & !(C2|C3|C1|C5|C6|C7|C8|C9|C10)) {
#Passoff = Players:entity(4) 
#if (Once) {Once=0 print(Cs+Orb+Players:entity(4):name())}}
#
#if (C5 & !(C2|C3|C4|C1|C6|C7|C8|C9|C10)) {
#Passoff = Players:entity(5) 
#if (Once) {Once=0 print(Cs+Orb+Players:entity(5):name())}}
#
#if (C6 & !(C2|C3|C4|C5|C1|C7|C8|C9|C10)) {
#Passoff = Players:entity(6) 
#if (Once) {Once=0 print(Cs+Orb+Players:entity(6):name())}}
#
#if (C7 & !(C2|C3|C4|C5|C6|C1|C8|C9|C10)) {
#Passoff = Players:entity(7) 
#if (Once) {Once=0 print(Cs+Orb+Players:entity(7):name())}}
#
#if (C8 & !(C1|C3|C4|C5|C6|C7|C1|C9|C10)) {
#Passoff = Players:entity(8) 
#if (Once) {Once=0 print(Cs+Orb+Players:entity(8):name())}}
#
#if (C9 & !(C2|C1|C4|C5|C6|C7|C8|C1|C10)) {
#Passoff = Players:entity(9) 
#if (Once) {Once=0 print(Cs+Orb+Players:entity(9):name())}}
#
#if (C10 & !(C2|C3|C1|C5|C6|C7|C8|C9|C1)) {
#Passoff = Players:entity(10) 
#if (Once) {Once=0 print(Cs+Orb+Players:entity(10):name())}}
#}
#}

#E = 1
#if (C1) {Targets:setEntity(E,Players:entity(1)) E+=1}
#if (C2) {Targets:setEntity(E,Players:entity(2)) E+=1}
#if (C3) {Targets:setEntity(E,Players:entity(3)) E+=1}
#if (C4) {Targets:setEntity(E,Players:entity(4)) E+=1}
#if (C5) {Targets:setEntity(E,Players:entity(5)) E+=1}
#if (C6) {Targets:setEntity(E,Players:entity(6)) E+=1}
#if (C7) {Targets:setEntity(E,Players:entity(7)) E+=1}
#if (C8) {Targets:setEntity(E,Players:entity(8)) E+=1}
#if (C9) {Targets:setEntity(E,Players:entity(9)) E+=1}
#if (C10) {Targets:setEntity(E,Players:entity(10)) E+=1}
}
#TID = Targets:entity(N):id()
if (Targets:count()==0) {Error1 = Cs+"No Targets Spesified"} else {Error1 = ""}
if ((CM|C)& L & K) {print(Cs+Targets:concat()+Error1)}

Count = Targets:count()
Blank=1000000
#D1=D2=D3=D4=D5=D6=D7=D8=D9=D10=Blank
Dist = array()

#Co=1
HA=array()
for(C=1,Count){
    Dist[C,number]=Blank
    HA[C,number]=Targets[C,entity]:health()
}
#H1=Targets:entity(Co):health() Co++
#H2=Targets:entity(Co):health() Co++
#H3=Targets:entity(Co):health() Co++
#H4=Targets:entity(Co):health() Co++
#H5=Targets:entity(Co):health() Co++
#H6=Targets:entity(Co):health() Co++
#H7=Targets:entity(Co):health() Co++
#H8=Targets:entity(Co):health() Co++
#H9=Targets:entity(Co):health() Co++
#H10=Targets:entity(Co):health() Co++
for(C=1,Count){
    if(Count>(C-1)&HA[C,number]){
        Dist:pushNumber(Targets[C,entity]:pos():distance(Origin))
    }
}

#if (Count>0&H1) {D1 = Targets:entity(1):pos():distance(Origin)} 
#if (Count>1&H2) {D2 = Targets:entity(2):pos():distance(Origin)} 
#if (Count>2&H3) {D3 = Targets:entity(3):pos():distance(Origin)} 
#if (Count>3&H4) {D4 = Targets:entity(4):pos():distance(Origin)} 
#if (Count>4&H5) {D5 = Targets:entity(5):pos():distance(Origin)} 
#if (Count>5&H6) {D6 = Targets:entity(6):pos():distance(Origin)} 
#if (Count>6&H7) {D7 = Targets:entity(7):pos():distance(Origin)} 
#if (Count>7&H8) {D8 = Targets:entity(8):pos():distance(Origin)} 
#if (Count>8&H9) {D9 = Targets:entity(9):pos():distance(Origin)} 
#if (Count>9&H10) {D10 = Targets:entity(10):pos():distance(Origin)}
#Dist:setNumber(1,D1)
#Dist:setNumber(2,D2)
#Dist:setNumber(3,D3)
#Dist:setNumber(4,D4)
#Dist:setNumber(5,D5)
#Dist:setNumber(6,D6)
#Dist:setNumber(7,D7)
#Dist:setNumber(8,D8)
#Dist:setNumber(9,D9)
#Dist:setNumber(10,D10)
#K1 = Dist:minIndex()
#Dist:setNumber(K1,Blank)
#K2 = Dist:minIndex()
#Dist:setNumber(K2,Blank)
#K3 = Dist:minIndex()
Target=array()
for(C=1,3){
    Target[C,entity]=Targets[Dist:minIndex(),entity]
    Dist[Dist:minIndex(),number]=Blank   
}

#T1 = Targets:entity(K1):id()
#T2 = Targets:entity(K2):id()
#T3 = Targets:entity(K3):id()



#Counted=Dist:count()




#Target:setEntity(1,entity(T1))
#Target:setEntity(2,entity(T2))
#Target:setEntity(3,entity(T3))

Dis=0
#ZSC:setNumber("Formation",Formation)
#ZSC:setNumber("On",On)
#ZSC:setEntity("Pass",Passoff)
#ZSC:setNumber("Disposable",Dis)





