@name Self Control Chip
@inputs N0 N1 N2 N3 N4 N5 N6 N7 N8 N9 Enter Del Plus Minus Astrix Slash
@outputs On RAP
@persist Aim On RAP E:entity [Num Master]:entity [Tool Old]:string 
@persist [Keys Input]:array Width Material:string Color:vector Self:entity

@model models/hunter/plates/plate.mdl


if(first()|duped()){
    RAP=0
    E=entity():isConstrainedTo()
    if(!E:owner():trusts(entity():owner())){
        print("you need to get prop protection for this to work!")
    }
    
    #ifdef entity:createWire(entity,string,string,number,vector,string)
        Self=entity()
        Color=vec(255,255,255)
        Material="arrowire2"
        Width=0.5
        Num=Self:isConstrainedTo()
        Keys=array() Input=array()
        Keys:pushString("0")
        Keys:pushString("1")
        Keys:pushString("2")
        Keys:pushString("3")
        Keys:pushString("4")
        Keys:pushString("5")
        Keys:pushString("6")
        Keys:pushString("7")
        Keys:pushString("8")
        Keys:pushString("9")
        Keys:pushString("/")
        Keys:pushString("*")
        Keys:pushString("-")
        Keys:pushString("+")
        Keys:pushString("enter")
        Keys:pushString(".")
        
        Input:pushString("N0")
        Input:pushString("N1")
        Input:pushString("N2")
        Input:pushString("N3")
        Input:pushString("N4")
        Input:pushString("N5")
        Input:pushString("N6")
        Input:pushString("N7")
        Input:pushString("N8")
        Input:pushString("N9")
        Input:pushString("Slash")
        Input:pushString("Astrix")
        Input:pushString("Minus")
        Input:pushString("Plus")
        Input:pushString("Enter")
        Input:pushString("Del")
        timer("finish",500)
        for(C=1,11){
            Self:createWire(Num,Input[C,string],Keys[C,string],Width,Color,Material)
            
        }
        #else
            print("you'll need to wire this up manually.")
            E:chatPrint("you'll need to wire this up manually.")
    #endif
    print("type /help for options")
}
#ifdef entity:createWire(entity,string,string,number,vector,string)
    if(clk("finish")){
        for(C=12,16){
            Self:createWire(Num,Input[C,string],Keys[C,string],Width,Color,Material)
        }
    }
#endif
if(!first()){
    runOnChat(1)
    Speak="/say "
    Spawn="/spawn"
    Undo="/undo"
    Relative="/RAP"
    Command="/cmd"
    On1="on"
    On0="off"
    Help="/help"
    
    
    Master=Num:owner()
    Chat=owner():lastSaid()
    ChatE=E:owner():lastSaid()
    ChatM=Num:owner():lastSaid()
    ChatM=ChatM:replace("//","/")
    
    #prints the tool name of the tool the player is holding if it's new
    #the controlling player gets the message
    Old=Tool
    Tool=owner():tool()
    if(Tool!=Old){chatPrint(owner():name()+" Is holding "+Tool)}
    
    
    
    if(chatClk()){
        if(On){
            if(ChatM:find(Speak)){concmd("say "+ChatM:replace(Speak,""))}
            if(ChatM:find(Spawn)){concmd("gm_spawn "+ChatM:explode(" ")[2,string])}
            if(ChatM:find(Undo)){concmd("undo")}
            if(ChatM:find(Relative)&ChatM:find(On1)){RAP=1}#Relative Aim Position
            if(ChatM:find(Relative)&ChatM:find(On0)){RAP=0}#Relative Aim Position
            if(ChatM:find(Command)){concmd(ChatM:replace(Command,""))}
            if(ChatM:find(Help)){
                Ty="type "
                Master:chatPrint(Ty+Speak+" 'text here' to cause slave to speak.")
                Master:chatPrint(Ty+Spawn+" 'model path here' to cause slave to spawn props.")
                Master:chatPrint(Ty+Undo+" to cause slave to undo last item, careful not to undo this chip.")
                Master:chatPrint(Ty+Relative+" 'on'/'off' to activate or deactivate Relative Aim Positioning.")
                Master:chatPrint(Ty+Command+" 'convar' to issue a console command for the slave. Tool switches and more.")
                Master:chatPrint(Ty+Help+" displays this list again.")
            }
        }
        if(ChatM=="SCC on"){On=1 concmd("say I'm being mind controlled!")}
        if(ChatM=="SCC off"){On=0 Clean=1 concmd("say I'm free!")}
        if(Chat=="SCC release"){On=0 Clean=1}
        if(Chat=="SCC aim off"){Aim=0}
        if(Chat=="SCC remove"){Clean=1 Destruct=1}
        if(Clean){concmd("-attack;-attack2;-forward;-back;-left;-right;-moveleft;-moveright;-jump;-duck;-use;-reload")}
    }
    if(~N7){
    if(N7){On=!On}
    if(On){concmd("say I'm being mind controlled!")}else{concmd("say I'm free!")}
    }
    if(On){
        P="+" #used with convars
        N="-" #also used with convars
        if(~N0){
        
            V="jump" #v for variable
            if(N0){concmd(P+V)}else{concmd(N+V)}
        }
        
        
        if(~N1){
            V="moveleft"
            if(N1){concmd(P+V)}else{concmd(N+V)}
        }
        
        if(~N2){
            V="back"
            if(N2){concmd(P+V)}else{concmd(N+V)}
        }
        
        if(~N3){
            V="moveright"
            if(N3){concmd(P+V)}else{concmd(N+V)}
        }
        
        if(~N4){
            V="left"
            if(N4){concmd(P+V)}else{concmd(N+V)}
        }
        
        if(~N5){
            V="use"
            if(N5){concmd(P+V)}else{concmd(N+V)}
        }
        
        
        if(~N6){
            V="right"
            if(N6){concmd(P+V)}else{concmd(N+V)}
        }
        
    #    if(~N7){
    #        V="invnext"
    #        if(N7){concmd(V)}
    #        #if(N7){concmd(P+V)}else{concmd(N+V)}
    #    
    #    }
        
        if(~N8){
            V="forward"
            if(N8){concmd(P+V)}else{concmd(N+V)}
        }
        
        if(~N9){
            V="reload"
            if(N9){concmd(P+V)}else{concmd(N+V)}
        }
        
        if(~Enter){
            V="attack"
            if(Enter){concmd(P+V)}else{concmd(N+V)}
        }
        
        if(~Del){
            V="attack2"
            if(Del){concmd(P+V)}else{concmd(N+V)}
        }
        
        if(~Slash){
            V="duck"
            if(Slash){concmd(P+V)}else{concmd(N+V)}
        }
        
        if(~Plus){
            V="invnext"
            if(Plus){concmd(V)}
            #if(N7){concmd(P+V)}else{concmd(N+V)}
        }
        if(~Minus){
            V="invprev"
            if(Minus){concmd(V)}
            #if(Minus){concmd(P+V)}else{concmd(N+V)}
        }
        
        if(~Astrix){
        V="reload"
        if(Astrix){Aim=!Aim}
        }
        
        if(Aim){
        timer("aim",30)
        Self=owner()
        Target=entity():isConstrainedTo():owner()
        if(!RAP){
            Ang=angnorm((Target:aimPos()-Self:shootPos()):toAngle())
        }else{
            Ang=Target:eyeAngles()
        }
        
        
        Self:setEyeAngle(Ang)
        }
    }
    if(Destruct){selfDestruct()}
}
