@name Interactive Targeting
@outputs Target:array Passoff:entity Formation Self On Kill
@outputs Command:string Network 
@persist Players:array Chat1:string Dist:array Passoff:entity Targets:array
@persist Play:array Self Network Command:string
@persist Once MultiCom:array On PA:array ComN:array BoxC:array
if(ops()>4800){exit()}
Time = 500
TarMax=3 #This is the maximum number of targets this chip will output
interval(Time)
gSetGroup("ZapSat")
if(duped()){reset()}
if (first()) {
    Passoff = entity():owner() 
    Network=0 
    Self=0
    ComN=array()
    ComN[0,string]="Zap"
    ComN[1,string]="Zap1"
    ComN[2,string]="Zap2"
    ComN[3,string]="Zap3"
    ComN[4,string]="Zap4"
    BoxC=array()
    BoxC:pushVector(vec(255,0,0))
    BoxC:pushVector(vec(0,255,0))
    BoxC:pushVector(vec(0,0,255))
    BoxC:pushVector(vec(255,255,0))
    BoxC[0,vector]=vec(255,255,255)
    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")
    
}
entity():setColor(0,140,255)
Box = entity():isConstrainedTo()
if (Network) {  
    #D-Link Network Code Copy and paste this into your contraption
    #by Captain Maim
    gSetGroup("Zapsats")
    runOnLast(1)
    Diag=1
    NetMax=4 #this is the maximum number of networked items, surplus are removed
    ID="ID"
    EID=entity():id()
    EIDS=toString(EID)
    if(!Self){
        NetT=glonDecodeTable(gGetStr(ID)) Scan=gGetStr(ID) Brk="<"
            for(C=1,NetMax){if(!Scan:find(Brk+C+Brk)){Self=C break}} 
            if(!Self){if(Diag){print("Self Destructing Excess Clones!")} selfDestructAll()
            }
        NetT[EIDS,string]=Brk+Self+Brk
        gSetStr(ID,glonEncode(NetT))
            if(Diag){print("Self="+Self)} #you can remove this if you want it's diagnostic
    }
    if(last()){
        NetT=glonDecodeTable(gGetStr(ID))
        NetT[EIDS,string]=""
        gSetStr(ID,glonEncode(NetT))
        if(Diag){print("ID "+Self+" Deleted")} #diagnostic non critical
    }
    #End of D-Link Network Code
  #This just does network spesifc things
    for(C=0,4){
        if(Self==C){
            Command=ComN[C,string]
            Box:setColor(BoxC[C,vector])
        }
    }
    
}else{
    Command=ComN[0,string]
    Box:setColor(BoxC[0,vector])
    Orders=""
}
Pa=")"


Menu = "menu"
Attack = "get" 
List = "list"
PKC = "PK"
Bees="Bees"
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"
BKMode="Anti-Bee Defense Mode Enabled"
Cs=Command+": "
Orb = "Now Orbiting: "
Breaker="<"
Power="power"
Suicide="destruct"
Help="help"

ChatO = Passoff:lastSaid()
Chat = entity():owner():lastSaid()
runOnChat(1)
K=chatClk()

if (K) {Once=1}
Com = Chat:find(Command)
MENU = Chat:find(Menu)
BEES=Chat:find(Bees)
ATTACK = Chat:find(Attack)
LIST = Chat:find(List)
CA=array()
#find if the owner said a number and what number
for(C=1,10){CA[C,number]=Chat:find(PA[C,string])}

PK = Chat:find(PKC)
NK = Chat:find(NKC)
PASS = 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)
HELP=Chat:find(Help)

if(duped()|first()){NK=1 K=1 Com=1 MENU=1}
#scan for players
Origin = entity():owner():pos()
findInSphere(Origin,1000000)
findSortByDistance(Origin)
findClipToClass("player")
Play = findToArray()
N0=CA[10,number]
N1=CA[1,number]
if (!Targets:count()) {Error1 = Cs+"No Targets Spesified"} else {Error1 = ""}


#Chat command IF tree
if ((Com|CM)&K){
    if(HELP){
        print("All commands must be proceeded with: "+Com)
        print(Menu+" -generate a list of players in order of distance from owner (important)")
        print(NetOn+" -Turns on networking")
        print(NetOff+" -Disables networking")
        print(PK+" -Player Kill mode")
        print(NK+" -NPC Kill mode")
        print(Pass+S+"<player number from menu> -tells sats to orbit him.")
        print(BK+" -Same as "+Pass+S+"1 -returns zapsats to you.")
        
    }
    if (ModeT) {Formation = 1}
    if (ModeW) {Formation = 0}
    if(Once){
        if (Net1) {Network = 1 print(Cs+"Networking Enabled") Once=0 }
        if (Net0) {Network = 0 print(Cs+"Networking Disabled") Once=0}
    }
    if (Break) {MultiCom = Chat:explode(Breaker)}
    if(MultiCom:count()){
        for(C=1,4){
            if(Self==C){Orders=MultiCom[C+1,string]}
        }
        if (Break) {
        CA=array()
            for(C=1,10){
                CA[C,number]=Orders:find(PA[C,string])
            }
    
        }
    }
    if(Mode){
        
        if (Kill==1) {print(Cs+PKMode)}
        if (Kill==0) {print(Cs+NKMode)}
        if (Kill==2){print(Cs+BKMode)}
    }
    if(Powered){
        if (N1){On=!On print(Cs+"Toggling Power state")}
        if (N0){print(Cs+"Skipping power toggle")}
    }
    if (Suicided&N1){print(Cs+"Self Destructing") selfDestructAll()}
    if(Form1){
        if (N1){Formation=1 print(Cs+"Tight Formation")}
        if (N0){Formation=0 print(Cs+"Wide Formation")}
    }
    if(PK){Kill=1 print(Cs+PKMode)}
    if(NK){Kill=0 print(Cs+NKMode)}
    if(BEES){Kill=2 print(Cs+BKMode)}
    if(LIST){print(Cs+Targets:concat()+Error1)}
    #generate a list of 9 players ranked by distance from you
    if(MENU){ 
        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())}
        }
    print(Concat:concat("\n"))
    }
    #order sats to include the following players as targets
    if (ATTACK) {
    Targets = array()
        for(C=1,10){
            if(CA[C,number]){Targets:pushEntity(Players[C,entity])}
        }
    }
    #Tell sats to orbit someone else
    if (PASS) {
        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())}
                        }
                    }
                }
            }
        }
    }
}



Count = Targets:count()
Blank=1000000

Dist = array()
HA=array()
for(C=1,Count){
    Dist[C,number]=Blank
    HA[C,number]=Targets[C,entity]:health()
}

for(C=1,Count){
    if(Count>(C-1)&HA[C,number]){
        Dist[C,number]=Targets[C,entity]:pos():distance(Origin)
    }
}


Target=array()
for(C=1,TarMax){
    Target[C,entity]=Targets[Dist:minIndex(),entity]
    Dist[Dist:minIndex(),number]=Blank   
}

