@name Secret Chat 2
@inputs 
@outputs Self 
@persist Colors:array Last:table Chat2:table ToolSet:string WeaponSet:string Wep
@persist Tool:string Weapon:string
@persist Self #use this number for all your networking needs
@trigger 

#By Captain Maim STEAM_0:1:8017844
GMTOffset=-7  #set this to your GMT offset if needed.

Network="Secret Chat" #change this to setup an entirely unique chat network. 
ChatChannel="Chat" #this is the variable that sets the global variable channel. 

if(!entity():owner()){selfDestruct()}

#chat commands
Command="!tool" #say this to command the chip, say it alone to find the secret chat tool. Default is E2.
Current="cur"   #Say this plus Command to set the secret tool to your current tool. 
#Or with Command & Weapon to set the current weapon to secret chat.
On="on"         #this plus Command and Weap to enable weapon spesific mode. 
Off="off"       #say this plus Command and Weap to disable weapon spesific mode.
Weap="wep" 
Help="help"


if(duped()){reset()}
if(first()){
    print("Type '"+Command+" "+Help+"' for instructions")
    ToolSet="wire_expression2" #default tool
    WeaponSet="gmod_tool" #default weapon
    Wep=1 #default mode
} 
interval(500)
runOnChat(1)
Name="Secret Chat"
if(chatClk()&lastSpoke()==owner()){
    Chat=owner():lastSaid()
    SetH=Chat:find(Help)
    SetT=Chat:find(Command) 
    SetC=Chat:find(Current)   
    SetOn=Chat:find(On)   
    SetOff=Chat:find(Off)
    SetW=Chat:find(Weap)
    if(SetT){
            if(SetH){
            Color1=vec(0,128,255)
            Color2=vec(0,255,128)
            printColor(Color1,"say '"+Command+"' to address the chip. This is added to all the below.")
            printColor(Color2,"say '"+Current+"' without '"+Weap+"' sets the spesific tool to Secret Chat if '"+Weap+"' is off.")
            printColor(Color1,"say '"+Current+"' with '"+Weap+"' to set the spesific weapon if that mod is on.")
            printColor(Color2,"say '"+Weap+"' to see what weapon activates the Secret Chat.")
            printColor(Color1,"say '"+Weap+"' with '"+Off+"' or '"+On+"' to set to spesific tool or spesific weapon mode.")
            printColor(Color2,"say '"+Help+"' to display this message.")
            }
        if(SetW){
            if(SetOn){Wep=1 print(Name+"is in weapon spesific mode")
            }elseif(SetOff){Wep=0 print(Name+"is not in weapon spesific mode")}
            if(SetC){
                WeaponSet=Weapon 
                if(!SetOn|!SetOff){print(Name+" Weapon is set to: "+WeaponSet)}
            }else{print(Name+" Weapon is set to: "+WeaponSet) print("Spesific Weapon mode is: "+Wep)}
        }elseif(SetC){
            ToolSet=Tool
            print(Name+" tool set to: "+Tool)
        }else{print(Name+" Tool is set to: "+ToolSet)}
    }
}

Weapon=owner():weapon():type()
Tool=owner():tool()
if(Wep){
    if(Weapon==WeaponSet){Hide=1}else{Hide=0}
}else{
    if(Tool==ToolSet){Hide=1}else{Hide=0}
}
runOnChat(1)
hideChat(Hide) 
gShare(1) #Share data with everyone else, it's kinda crutial that this stay 1 or you'll just talk to yourself.

#D-Link Network Code Copy and paste this into your contraption
gSetGroup(Network)
runOnLast(1)
Diag=0
NetMax=16 #this is the maximum number of networked items, surplus are removed
ID="ID"
EID=entity():id()
EIDS=toString(EID)
if(!Self){
    NetT=vonDecodeTable(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,vonEncode(NetT))
        if(Diag){print("Self="+Self)} #you can remove this if you want it's diagnostic
}

SelfS=Self:toString() #this is pertains to the chat chip

if(last()){
    NetT=vonDecodeTable(gGetStr(ID))
    NetT[EIDS,string]=""
    gSetStr(ID,vonEncode(NetT))
    if(Diag){print("ID "+Self+" Deleted")} #diagnostic non critical
    print("You've just removed your "+Name+" chip.")
    #This pertains to the chat chip not the D-link code
    Chat2=vonDecodeTable(gGetStr(ChatChannel))
    Chat2[SelfS,string]="" 
    Chat2[SelfS,number]=0
    gSetStr(ChatChannel,vonEncode(Chat2))
    #end chat chip
}
#End of D-Link Network Code

if(first()){ #Generating color array
H=255
M=128
L=0
CB=64
    Colors:pushVector(vec(L,L+CB,H))       #1
    Colors:pushVector(vec(L,H-64,L))       #2
    Colors:pushVector(vec(H,L,L))          #3
    Colors:pushVector(vec(H,H,L))          #4
    Colors:pushVector(vec(H-CB,M-CB,L+CB)) #5
    Colors:pushVector(vec(L,H,M))          #6
    Colors:pushVector(vec(L,M,H))          #7
    Colors:pushVector(vec(M+CB,L,H-CB))    #8
    Colors:pushVector(vec(H-CB,H-CB,H-CB)) #9
    Colors:pushVector(vec(M,M,M))          #10
    Colors:pushVector(vec(M,L,M))          #11
    Colors:pushVector(vec(L,M+CB,M))       #12
    Colors:pushVector(vec(M,M,L))          #13
    Colors:pushVector(vec(M,H,H))          #14
    Colors:pushVector(vec(H,H-CB,M-CB))    #15
    Colors:pushVector(vec(H,M,H-CB))       #16
    printColor(Colors[Self,vector],"Your chat color is: "+Colors[Self,vector])
}
entity():setColor(Colors[Self,vector])

#Time Stamp
O=":"
Steam=owner():steamID()
if(Steam=="STEAM_0:1:19091184"){GMTOffset=-7}
if(Steam=="STEAM_0:0:19519871"){GMTOffset=-8}
if(Steam=="STEAM_0:1:8017844"){GMTOffset=-7-time("isdst")}
if(Steam=="Steam_0:1:16471737"){GMTOffset=-6}


Hour=time("hour")+GMTOffset
if((Hour>=(24+GMTOffset))&(Hour<=23)){Day=time("day")}else{Day=time("day")-1}
if(Hour<0){Hour+=10-GMTOffset*2}
Time="["+Hour+O+time("min")+O+time("sec")+"]"   
#end stamp

Chat2=vonDecodeTable(gGetStr(ChatChannel))

#send chat
if(Hide){
    Chat=owner():lastSaid()
    if(chatClk()&lastSpoke()==owner()){        
        Message=Time+owner():name()+": "+Chat
        Chat2[SelfS,string]=Message
        Chat2[SelfS,number]=1
        gSetStr(ChatChannel,vonEncode(Chat2))
        timer("wait",100)
     }
}
#receive chat

if(chatClk()){
    timer("read",40)
    Chat2=vonDecodeTable(gGetStr(ChatChannel))
}
if(clk("read")){
   for(C=1,NetMax){
        Str=C:toString()
        if(Chat2[Str,number]){
            printColor(Colors[C,vector],Chat2[Str,string])
            
        }
    }#end for
}
#clear send
if(clk("wait")){
    Chat2[SelfS,number]=0
    Chat2[SelfS,string]=""
    gSetStr(ChatChannel,vonEncode(Chat2))
}


