@name UltiMultiDoor
@inputs Keypad1 Keypad2 Clk Entity:entity Screen:wirelink
@persist Anim BasePos:vector Dist Speed Trusted:array E:entity L
@persist All H:entity Open Angle:angle P:array Distance:array A:array Target:entity D Z
@outputs Door
if (first()|dupefinished()){
    runOnTick(1)
    runOnChat(1)
    
    #Variables
    Model="models/hunter/plates/plate3x4.mdl"
    Material="phoenix_storms/dome"
    Dist=90
    Speed=1
    Trusted:pushEntity(owner())
    All=1
    
    Check=ranger(10)
    ######ADD#####
S = owner():lastSaid():explode(" ")
if(S[1,string]==".add" & chatClk(owner())){
    TP = findPlayerByName(S[2,string])
    if(TP:isPlayer()){
        Target = TP
        P:pushEntity(TP) 
           }
    
}
    
    
    
    #######MINUS########
    if(S[1,string]==".remove"& chatClk(owner())){
    TP = findPlayerByName(S[2,string])
    if(TP:isPlayer()){
        while(Z < 20){
        if(TP == P[Z,entity])
        {
        P:remove(Z)
        }
        Z++
}
}
Z = 0
}
    
    
    ###################
    Distance1 = round(((entity():pos() - P[1,entity]:pos()):length()))
Distance2 = round(((entity():pos() - P[2,entity]:pos()):length()))
Distance3 = round(((entity():pos() - P[3,entity]:pos()):length()))
Distance4 = round(((entity():pos() - P[4,entity]:pos()):length()))
Distance5 = round(((entity():pos() - P[5,entity]:pos()):length()))
Distance6 = round(((entity():pos() - P[6,entity]:pos()):length()))
Distance7 = round(((entity():pos() - P[7,entity]:pos()):length()))
Distance8 = round(((entity():pos() - P[8,entity]:pos()):length()))
Distance9 = round(((entity():pos() - P[9,entity]:pos()):length()))
Distance10 = round(((entity():pos() - P[10,entity]:pos()):length()))
Distance11 = round(((entity():pos() - P[11,entity]:pos()):length()))
Distance12 = round(((entity():pos() - P[12,entity]:pos()):length()))
Distance13 = round(((entity():pos() - P[13,entity]:pos()):length()))
Distance14 = round(((entity():pos() - P[14,entity]:pos()):length()))
Distance15 = round(((entity():pos() - P[15,entity]:pos()):length()))
Distance16 = round(((entity():pos() - P[16,entity]:pos()):length()))
Distance17 = round(((entity():pos() - P[17,entity]:pos()):length()))
Distance18 = round(((entity():pos() - P[18,entity]:pos()):length()))
Distance19 = round(((entity():pos() - P[19,entity]:pos()):length()))
Distance20 = round(((entity():pos() - P[20,entity]:pos()):length()))
Screen:egpText(1,"1) " + P[1,entity]:name(),vec2(30,50))
Screen:egpText(2,"2) " + P[2,entity]:name(),vec2(30,80))
Screen:egpText(3,"3) " + P[3,entity]:name(),vec2(30,110))
Screen:egpText(4,"4) " + P[4,entity]:name(),vec2(30,140))
Screen:egpText(5,"5) " + P[5,entity]:name(),vec2(30,170))
Screen:egpText(6,"6) " + P[6,entity]:name(),vec2(30,200))
Screen:egpText(7,"7) " + P[7,entity]:name(),vec2(30,230))
Screen:egpText(8,"8) " + P[8,entity]:name(),vec2(30,260))
Screen:egpText(9,"9) " + P[9,entity]:name(),vec2(30,290))
Screen:egpText(10,"10) " + P[10,entity]:name(),vec2(30,320))
Screen:egpText(11,"Distance: " + Distance1,vec2(310,50))
Screen:egpText(12,"Distance: " + Distance2,vec2(310,80))
Screen:egpText(13,"Distance: " + Distance3,vec2(310,110))
Screen:egpText(14,"Distance: " + Distance4,vec2(310,140))
Screen:egpText(15,"Distance: " + Distance5,vec2(310,170))
Screen:egpText(16,"Distance: " + Distance6,vec2(310,200))
Screen:egpText(17,"Distance: " + Distance7,vec2(310,230))
Screen:egpText(18,"Distance: " + Distance8,vec2(310,260))
Screen:egpText(19,"Distance: " + Distance9,vec2(310,290))
Screen:egpText(20,"Distance: " + Distance10,vec2(310,320))
Screen:egpText(20,"teaming in base:",vec2(130,5))
Screen:egpText(21,"",vec2(80,430))
Screen:egpText(22,"",vec2(70,450))
Screen:egpLine(23,vec2(130,30),vec2(380,30))
Screen:egpText(24,"Command's: .add .remove",vec2(120,360))
Screen:egpFont(1,"ariel",25)
Screen:egpFont(2,"ariel",25)
Screen:egpFont(3,"ariel",25)
Screen:egpFont(4,"ariel",25)
Screen:egpFont(5,"ariel",25)
Screen:egpFont(6,"ariel",25)
Screen:egpFont(7,"ariel",25)
Screen:egpFont(8,"ariel",25)
Screen:egpFont(9,"ariel",25)
Screen:egpFont(10,"ariel",25)
Screen:egpFont(11,"ariel",25)
Screen:egpFont(12,"ariel",25)
Screen:egpFont(13,"ariel",25)
Screen:egpFont(14,"ariel",25)
Screen:egpFont(15,"ariel",25)
Screen:egpFont(16,"ariel",25)
Screen:egpFont(17,"ariel",25)
Screen:egpFont(18,"ariel",25)
Screen:egpFont(19,"ariel",25)
Screen:egpFont(20,"ariel",25)
Screen:egpFont(21,"ariel",25)
Screen:egpFont(22,"ariel",25)
Screen:egpFont(24,"ariel",25)
Door = 0
if(Distance1<D){Door = 1
}elseif(Distance2<D){Door = 1
}elseif(Distance3<D){Door = 1
}elseif(Distance4<D){Door = 1
}elseif(Distance5<D){Door = 1
}elseif(Distance6<D){Door = 1
}elseif(Distance7<D){Door = 1
}elseif(Distance8<D){Door = 1
}elseif(Distance9<D){Door = 1
}elseif(Distance10<D){Door = 1
}elseif(Distance11<D){Door = 1
}elseif(Distance12<D){Door = 1
}elseif(Distance13<D){Door = 1
}elseif(Distance14<D){Door = 1
}elseif(Distance15<D){Door = 1
}elseif(Distance16<D){Door = 1
}elseif(Distance17<D){Door = 1
}elseif(Distance18<D){Door = 1
}elseif(Distance19<D){Door = 1
}elseif(Distance20<D){Door = 1
}
 ################   
    #Door assignment
    if (->Entity){
        E=Entity
        Angle=E:angles()
        print("Using wired entity!")
    } elseif (entity():isWeldedTo():type()=="prop_physics"){
        E=entity():isWeldedTo()
        Angle=E:angles()
        print("Using pre-placed prop!")
    } elseif (Check:hit()) {
        E=Check:entity()
        Angle=E:angles()
        print("Using existing door!")
    } else {
        T1=ranger(1000)
        P1=rangerOffset(1000,entity():pos(),vec(0,0,-1)):pos()
        P2=rangerOffset(1000,entity():pos(),vec(0,0,1)):pos()
        P4=P2+entity():up()*T1:distance()
        MID=(P1+P4)/2
        Angle=entity():angles()+ang(0,90,0)
        E=propSpawn(Model,MID,entity():angles()+ang(0,90,0),1)
        E:setMaterial(Material)
        print("Spawning door...")
    }
    
    #Size calculations
    BasePos=E:pos()
    holoCreate(1,BasePos)
    holoAlpha(1,0)
    H=holoEntity(1)
    Top=E:nearestPoint(E:pos()+vec(0,0,1000))
    Bot=E:nearestPoint(E:pos()-vec(0,0,1000))
    L=Bot:distance(Top)
}

if (chatClk(owner())){
    if (owner():aimEntity()==E){
        S1=owner():lastSaid():explode(" ")[1,string]
        S2=owner():lastSaid():explode(" ")[2,string]
        
        if (S1=="!LD"){
            All=All ? 0 : 1
            if (All){
                print(4,"Door unlocked!")
            } else {
                print(4,"Door secured!")
        }
        }
    }
}

if (Keypad1|Keypad2|Clk){
    Open=1
} else {
    if (All){
        for (I=1,players():count()){
            TP=players()[I,entity]
            Open=0
            if (TP:shootPos():distance(H:pos())<Dist){
                Open=1
                break
            }
        }
    } else {
        for (I=1,Trusted:count()){
            TP=Trusted[I,entity]
            Open=0
            if (TP:shootPos():distance(H:pos())<Dist){
                Open=1
                break
            }
        }
    }
}

if (changed(Open)&!(first()|dupefinished())){
    if (Open){
        H:soundPlay(1,2,"doors/doormove2.wav")
    }
    if (!Open){
        H:soundPlay(1,2,"doors/doormove3.wav")
    }
}

if (Open){
    if (Anim<L){
        Anim=Anim+Speed
    }
} elseif (!Open){
    if (changed(Open)){
        
    }
    if (Anim>0){
        Anim=Anim-Speed
    }
}

E:setPos(BasePos+vec(0,0,Anim))
E:setAng(Angle)
E:propFreeze(1)

#[ @name TheV
 @inputs Screen:wirelink
 @outputs Door
 @persist P:array Distance:array A:array Target:entity D Z
 @trigger

runOnChat(1)
interval(100)
#Distance
D = 75
#
S = owner():lastSaid():explode(" ")
if(S[1,string]==".add" & chatClk(owner())){
    TP = findPlayerByName(S[2,string])
    if(TP:isPlayer()){
        Target = TP
        P:pushEntity(TP) 
           }
    
}
if(S[1,string]==".remove"& chatClk(owner())){
    TP = findPlayerByName(S[2,string])
    if(TP:isPlayer()){
        while(Z < 20){
        if(TP == P[Z,entity])
        {
        P:remove(Z)
        }
        Z++
}
}
Z = 0
}
Distance1 = round(((entity():pos() - P[1,entity]:pos()):length()))
Distance2 = round(((entity():pos() - P[2,entity]:pos()):length()))
Distance3 = round(((entity():pos() - P[3,entity]:pos()):length()))
Distance4 = round(((entity():pos() - P[4,entity]:pos()):length()))
Distance5 = round(((entity():pos() - P[5,entity]:pos()):length()))
Distance6 = round(((entity():pos() - P[6,entity]:pos()):length()))
Distance7 = round(((entity():pos() - P[7,entity]:pos()):length()))
Distance8 = round(((entity():pos() - P[8,entity]:pos()):length()))
Distance9 = round(((entity():pos() - P[9,entity]:pos()):length()))
Distance10 = round(((entity():pos() - P[10,entity]:pos()):length()))
Distance11 = round(((entity():pos() - P[11,entity]:pos()):length()))
Distance12 = round(((entity():pos() - P[12,entity]:pos()):length()))
Distance13 = round(((entity():pos() - P[13,entity]:pos()):length()))
Distance14 = round(((entity():pos() - P[14,entity]:pos()):length()))
Distance15 = round(((entity():pos() - P[15,entity]:pos()):length()))
Distance16 = round(((entity():pos() - P[16,entity]:pos()):length()))
Distance17 = round(((entity():pos() - P[17,entity]:pos()):length()))
Distance18 = round(((entity():pos() - P[18,entity]:pos()):length()))
Distance19 = round(((entity():pos() - P[19,entity]:pos()):length()))
Distance20 = round(((entity():pos() - P[20,entity]:pos()):length()))
Screen:egpText(1,"1) " + P[1,entity]:name(),vec2(30,50))
Screen:egpText(2,"2) " + P[2,entity]:name(),vec2(30,80))
Screen:egpText(3,"3) " + P[3,entity]:name(),vec2(30,110))
Screen:egpText(4,"4) " + P[4,entity]:name(),vec2(30,140))
Screen:egpText(5,"5) " + P[5,entity]:name(),vec2(30,170))
Screen:egpText(6,"6) " + P[6,entity]:name(),vec2(30,200))
Screen:egpText(7,"7) " + P[7,entity]:name(),vec2(30,230))
Screen:egpText(8,"8) " + P[8,entity]:name(),vec2(30,260))
Screen:egpText(9,"9) " + P[9,entity]:name(),vec2(30,290))
Screen:egpText(10,"10) " + P[10,entity]:name(),vec2(30,320))
Screen:egpText(11,"Distance: " + Distance1,vec2(310,50))
Screen:egpText(12,"Distance: " + Distance2,vec2(310,80))
Screen:egpText(13,"Distance: " + Distance3,vec2(310,110))
Screen:egpText(14,"Distance: " + Distance4,vec2(310,140))
Screen:egpText(15,"Distance: " + Distance5,vec2(310,170))
Screen:egpText(16,"Distance: " + Distance6,vec2(310,200))
Screen:egpText(17,"Distance: " + Distance7,vec2(310,230))
Screen:egpText(18,"Distance: " + Distance8,vec2(310,260))
Screen:egpText(19,"Distance: " + Distance9,vec2(310,290))
Screen:egpText(20,"Distance: " + Distance10,vec2(310,320))
Screen:egpText(20,"teaming in base:",vec2(130,5))
Screen:egpText(21,"",vec2(80,430))
Screen:egpText(22,"",vec2(70,450))
Screen:egpLine(23,vec2(130,30),vec2(380,30))
Screen:egpText(24,"Command's: .add .remove",vec2(120,360))
Screen:egpFont(1,"ariel",25)
Screen:egpFont(2,"ariel",25)
Screen:egpFont(3,"ariel",25)
Screen:egpFont(4,"ariel",25)
Screen:egpFont(5,"ariel",25)
Screen:egpFont(6,"ariel",25)
Screen:egpFont(7,"ariel",25)
Screen:egpFont(8,"ariel",25)
Screen:egpFont(9,"ariel",25)
Screen:egpFont(10,"ariel",25)
Screen:egpFont(11,"ariel",25)
Screen:egpFont(12,"ariel",25)
Screen:egpFont(13,"ariel",25)
Screen:egpFont(14,"ariel",25)
Screen:egpFont(15,"ariel",25)
Screen:egpFont(16,"ariel",25)
Screen:egpFont(17,"ariel",25)
Screen:egpFont(18,"ariel",25)
Screen:egpFont(19,"ariel",25)
Screen:egpFont(20,"ariel",25)
Screen:egpFont(21,"ariel",25)
Screen:egpFont(22,"ariel",25)
Screen:egpFont(24,"ariel",25)
Door = 0
if(Distance1<D){Door = 1
}elseif(Distance2<D){Door = 1
}elseif(Distance3<D){Door = 1
}elseif(Distance4<D){Door = 1
}elseif(Distance5<D){Door = 1
}elseif(Distance6<D){Door = 1
}elseif(Distance7<D){Door = 1
}elseif(Distance8<D){Door = 1
}elseif(Distance9<D){Door = 1
}elseif(Distance10<D){Door = 1
}elseif(Distance11<D){Door = 1
}elseif(Distance12<D){Door = 1
}elseif(Distance13<D){Door = 1
}elseif(Distance14<D){Door = 1
}elseif(Distance15<D){Door = 1
}elseif(Distance16<D){Door = 1
}elseif(Distance17<D){Door = 1
}elseif(Distance18<D){Door = 1
}elseif(Distance19<D){Door = 1
}elseif(Distance20<D){Door = 1
}
]#


A=owner():lastSaid():explode(" ")
if(A[1,string]=="!unlock" & chatClk(owner())){
     entity():propNotSolid( 0 )
    entity():setColor(vec(0,255,0))
    entity():setAlpha(255)
    hideChat(1)
}

A=owner():lastSaid():explode(" ")
if(A[1,string]=="!lock" & chatClk(owner())){
     entity():propNotSolid( 1 )
    entity():setColor(vec(0,0,0))
    entity():setAlpha(0)
    hideChat(1)
}
