@name Smart Door
@inputs CS:wirelink
@outputs Open
@persist [List List2]:table [O]:entity 
@persist [Corners Modes]:array 
@persist [Mode Lock ManOpen Open Visible CloseTime]

if(first()){
    
    ########################   ONLY EDIT THESE SETTINGS   ########################
    
    CloseTime   = 1.5 ## Time in Seconds for door to be open.
    OwnerOpen   = 0   ## Open the door for owner, ignores list.
    ShowChat    = 0   ## Whether your chat commands will be shown in chat.
    
    ########################   ONLY EDIT THESE SETTINGS   ########################
    
    
    runOnChat(1)
    O = owner()
    holoCreate(1)
    holoAng(1,ang(0,0,0))
    holoColor(1,vec(0,100,0),0)
    holoDisableShading(1,1)
    
    Mode = 3
    Lock = ManOpen = 0
    Modes = array("Exclude","Include","Do Nothing")
    
    function void clearCS(){
        CS:writeCell(2041,1)
        CS:writeString("Listed Players",0,0,119)
        CS:writeString("Mode: "+Modes[Mode,string],0,1,190)
        local Keys = List2:keys()
        for(I=1,Keys:count()){
            CS:writeString(I+". "+List2[Keys:string(I),string],0,1+I,999)
         }
    }    
    function void removePlayer(Ply:entity){
        List:remove(Ply:steamID())
        List2:remove(Ply:steamID())
        hint("Removed Player: "+Ply:name()+" from List.",3)
        clearCS()
    }
    function void addPlayer(Ply:entity){
        List[Ply:steamID(),entity] = Ply
        List2[Ply:steamID(),string] = Ply:name()
        hint("Added Player: "+Ply:name()+" to List.",3)
        clearCS()
    }
    function void findMode(M){
        findClearWhiteList()
        findClearBlackList()
        findIncludeClass("player")
        if(M==1){
            findExcludeEntities(List:toArray())
        }
        elseif(M==2){
            findIncludeEntities(List:toArray())
        }
        elseif(M==3){}
    }
    function void help(){        
    print(_HUD_PRINTCONSOLE,"\n\n")
    print(_HUD_PRINTCONSOLE,"----------------------------------------------------------------")
    print(_HUD_PRINTCONSOLE,"All commands are prefixed with '-'")
    print(_HUD_PRINTCONSOLE,"----------------------------------------------------------------")
    print(_HUD_PRINTCONSOLE,"add:     Adds a player to the list. -add garry")
    print(_HUD_PRINTCONSOLE,"----------------------------------------------------------------")
    print(_HUD_PRINTCONSOLE,"remove:  Removes a player from the list. -remove garry"+"\n"+"    Aliases: rem del delete")
    print(_HUD_PRINTCONSOLE,"----------------------------------------------------------------")
    print(_HUD_PRINTCONSOLE,"clear:   Clears the Entire List.")
    print(_HUD_PRINTCONSOLE,"----------------------------------------------------------------")
    print(_HUD_PRINTCONSOLE,"list:    Lists the current players on the list")
    print(_HUD_PRINTCONSOLE,"----------------------------------------------------------------")
    print(_HUD_PRINTCONSOLE,"corner:  Defines a corner of the Find Box of the door. -corner <1|2> \n    Aliases: c")
    print(_HUD_PRINTCONSOLE,"----------------------------------------------------------------")
    print(_HUD_PRINTCONSOLE,"mode:    Changes the mode which the e2 finds with. -mode <1|2|3>")
    print(_HUD_PRINTCONSOLE,"----------------------------------------------------------------")
    print(_HUD_PRINTCONSOLE,"modes:   Lists the available modes and what they do. -modes")
    print(_HUD_PRINTCONSOLE,"----------------------------------------------------------------")
    print(_HUD_PRINTCONSOLE,"manual:  Manually opens and closes the door (toggled). -manual \n    Aliased: man manu")
    print(_HUD_PRINTCONSOLE,"----------------------------------------------------------------")
    print(_HUD_PRINTCONSOLE,"holo:    Displays a hologram to show where the door is. -holo \n    Aliases: hologram")
    print(_HUD_PRINTCONSOLE,"----------------------------------------------------------------")
    print(_HUD_PRINTCONSOLE,"lock:    Locks the door and disables all access. -lock")
    print(_HUD_PRINTCONSOLE,"----------------------------------------------------------------")
    print(_HUD_PRINTCONSOLE,"help:    Displays this help message in console.")
    print(_HUD_PRINTCONSOLE,"----------------------------------------------------------------")
    }
    clearCS()
}

if(chatClk(O) & O:lastSaid():lower():left(1)=="-"){
    local Args = O:lastSaid():lower():explode(" ")
    local Cmd = Args:string(1):sub(2)
    local Hide = 1
    
    if(Cmd=="add"){
        if(Args:string(2)){
            local Ply = findPlayerByName(Args:string(2))
            if(Ply:isPlayer()){
                if(List:exists(Ply:steamID())){hint("Player: "+Ply:name()+" is already on the list.",3)}else{
                    addPlayer(Ply)
                    clearCS() 
                }
            }
            else{
                hint("Cannot find Player: "+Args:string(2),3)
            }
        }else{hint("Please enter a players name. Example: -add garry",3)}
    }
    elseif(Cmd=="remove" | Cmd=="del" | Cmd=="delete" | Cmd=="rem"){
        if(Args:string(2)){
            local Ply = findPlayerByName(Args:string(2))
            if(Ply:isPlayer()){
                if(!List:exists(Ply:steamID())){hint("Player: "+Ply:name()+" is not on the list.",3)}else{
                    removePlayer(Ply)
                }
            }
            else{
                hint("Cannot find Player: "+Args:string(2),3)
            }
        }else{hint("Please enter a players name. Example: -remove garry",3)}
    }
    elseif(Cmd=="clear"){
        List2 = List = table()
        hint("Cleared list",3)
        clearCS()
    }
    elseif(Cmd=="list"){
        if(List2:count()){
            printTable(List2)
        }else{hint("List is Empty",3)}   
    }
    elseif(Cmd=="corner" | Cmd=="c"){
        local Cmd = Args:string(2):toNumber()
        if(Cmd==1 | Cmd==2){
            Corners[Cmd,vector] = O:aimPos()
            hint("Corner: "+Cmd+" set to your Aim Position",3)
        }else{
            hint("Invalid Corner: "+Cmd,3)
            hint("Valid Corners are: 1 & 2",3)
        }
        if(Corners:vector(1)!=vec(0,0,0) & Corners:vector(2)!=vec(0,0,0)){
            timer("Search",100)
            local C1 = Corners:vector(1)
            local C2 = Corners:vector(2)
            holoScaleUnits(1,vec(abs((C1-C2):x()),abs((C1-C2):y()),abs((C1-C2):z())))
            holoPos(1,((C2+C1)/2))
            Visible = 1
            holoAlpha(1,75)
            }
    }
    elseif(Cmd=="mode"){
        local M = Args:string(2):toNumber()
        if(Args:string(2) & (M==1 | M==2 | M==3)){
            Mode = M
            #findMode(Mode)
            hint("Changed to Mode to: "+Modes:string(Args:string(2):toNumber()),3)
            clearCS()
        }
        else{
            hint("Invalid Mode. Type '-modes' for valid modes.",3)
        }
    }
    elseif(Cmd=="modes"){
        hint("Valid Modes are 1 to exclude, 2 to include, 3 to do nothing.",3)
    }
    elseif( (Cmd=="manual" | Cmd=="man" | Cmd=="manu") & !Lock){
        ManOpen = !ManOpen
        Open = ManOpen
        hint("Door was manually toggled open to: "+(ManOpen ? "Open" : "Closed"),3)
    }
    elseif(Cmd=="holo"){
        Visible = !Visible
        holoAlpha(1,(Visible ? 127 : 0))
        hint("Toggled Hologram's Visibility to: "+(Visible ? "Visible" : "Invisible"),3)        
    }
    elseif(Cmd=="lock"){
        Lock = !Lock
        local LS = (Lock ? "Locked" : "Unlocked")
        hint("The Door is now: "+LS,3)
    }
    elseif(Cmd=="help"){
        help()
        hint("Help has been printed to your console",3)
    }
    else{Hide=0}
    
    hideChat(Hide-ShowChat)
}
if(first()){
hint("Made by Fortifier42",3)
setName(">Smart Door<
by Fortifier42")
}
if(clk("Search")){
    if(!Lock & !ManOpen){
    local PlysC = findInBox(Corners[1,vector],Corners[2,vector])
    findMode(Mode)
    local Plys = findToArray()
    for(I=1,PlysC){
        local Ply = Plys:entity(I)
        if((Ply==owner() & OwnerOpen==1) | (Mode==1 & !List:exists(Ply:steamID())) | (Mode==2 & List:exists(Ply:steamID())) | (Mode==3 & Ply:isPlayer()) ){
            Open = 1
            timer("Close",CloseTime*1000)
            ########################   Door Open Code Goes Here   ########################
            #OpenRapedungeon = 1
            ########################   Door Open Code Goes Here   ########################
            break
        }
        else{
            ########################   Door Deny Code Goes Here   ########################
            #Turrets = Fire
            ########################   Door Deny Code Goes Here   ########################
        }
    }
}
    timer("Search",500)
}

if(clk("Close") & Open==1){
    ########################   Close Code Goes Here   ########################
    Open = 0
    ########################   Close Code Goes Here   ########################
    }
