@name EPad2
@persist Code:string Entered:string User:entity Lock Pickpoint Picktime
@persist Menu Private RollOver Use Using Locktime Tries OutA Fails
@persist Waitleft Waitleft2 Cursor Lockpix:array
@inputs Scr:wirelink Ent:entity
@outputs A
@trigger all


Picktime=60 #Pick time in seconds. Leave at 0 to be unpickable.

Code="7417" #4-Digit code.

Private=1 #Star out the code?

Tries=3 #If they miss the code this amount of time, the machine locks.

Locktime=20 # Lock time if the machine gets locked.

OutA=6 #0 for toggle, anything greater to out for that long

Cursor=0 # Toggle Cursor.


##########################################################################################
#beampick lockpick
Lockpix[1,string]="lockpick"
Lockpix[2,string]="beampick"
Lockpix[3,string]="keypad_cracker"
Lockpix[4,string]="door_cracker"
Lockpix[5,string]="weapon_bg_lockpick"


##########################################################################################
#entity():setAlpha(0)
#User=owner() # Debug line
interval(100)
#timer("Draw",10000)
if (!User & Scr["User",entity] & Scr["User",entity]:pos():distance(Scr:entity():pos())<75) {User=Scr["User",entity]} elseif (User & User:pos():distance(Scr:entity():pos())>75) {

#print(User:pos():distance(Scr:entity():pos()))
User=noentity()

}
if (findCanQuery() & !User) {
    findByClass("player")
    Ent=findClosest(Scr:entity():pos())
    if (Ent:pos():distance(Scr:entity():pos())<75 & !User) {User=Ent}
} else {}
if (changed(Scr)) {Draw=1}
if (User:keyUse() & !Using) {Use=1 Using=1}
if (User:keyUse()==0) {Using=0}
#if (changed(User) & User) {print(User:name())}
#if (changed(User) & !User) {print("noentity")}
if (first()) {
            function string stringRepeat(S:string,N) {
                Out=""
                for (I=1,N) {Out=Out+S}
                return Out
            }
            function number pIR(Num:number) {
                W=Scr
                O=User
        if(inrange(W:egpCursor(O):x(),W:egpPos(Num):x()-(W:egpSize(Num):x()/2),W:egpPos(Num):x()+(W:egpSize(Num):x()/2))&
        inrange(W:egpCursor(O):y(),W:egpPos(Num):y()-(W:egpSize(Num):y()/2),W:egpPos(Num):y()+(W:egpSize(Num):y()/2))) {
            RollOver=Num
            return 1
        } else {
            return 0
        }
    }
}
if (changed(Menu)) {
    Draw=1
    stoptimer("Draw")
    #timer("Draw",10000)
}
if (Draw) {
    Scr:egpClear()
    Use=0
    Scr:egpBox(40,vec2(256,256),vec2(512,512))
    Scr:egpColor(40,vec(64,64,64))
    #Scr:egpMaterial(40,"skybox/militia_hdrup")
    
}

if (Menu==0) {
    #   1   2   3
    
    #   4   5   6
    
    #   7   8   9
    
    #   C   0   Y
    
    #Center (256)-(Size+Size/2)
    
    #Text at 12+Num
    
    #C=11
    
    #Y=12
    
    #Outlines at 24+Num
    for (I=1, Lockpix:count()) {
        if (User:weapon():type()==Lockpix[I,string] & Picktime>0) {Menu=3}
    }
    if (Draw) {
        Down=0
        Scr:egpRoundedBox(1,vec2(256-120,256-120+Down),vec2(80,80))
        Scr:egpRoundedBox(2,vec2(256,256-120+Down),vec2(80,80))
        Scr:egpRoundedBox(3,vec2(256+120,256-120+Down),vec2(80,80))
        Scr:egpRoundedBox(4,vec2(256-120,256+Down),vec2(80,80))
        Scr:egpRoundedBox(5,vec2(256,256+Down),vec2(80,80))
        Scr:egpRoundedBox(6,vec2(256+120,256+Down),vec2(80,80))
        Scr:egpRoundedBox(7,vec2(256-120,256+120+Down),vec2(80,80))
        Scr:egpRoundedBox(8,vec2(256,256+120+Down),vec2(80,80))
        Scr:egpRoundedBox(9,vec2(256+120,256+120+Down),vec2(80,80))
        Scr:egpRoundedBox(10,vec2(256,480),vec2(50,50))
        Scr:egpRoundedBox(11,vec2(256-120,480),vec2(50,50))
        Scr:egpRoundedBox(12,vec2(256+120,480),vec2(50,50))
        for (I=1, 10) {
        Scr:egpColor(I,vec(128,128,128))
    }
    
    Scr:egpColor(11,vec(110,0,0))
    Scr:egpColor(12,vec(0,110,0))
    
    Scr:egpText(13,"1",Scr:egpPos(1))
    Scr:egpText(14,"2",Scr:egpPos(2))
    Scr:egpText(15,"3",Scr:egpPos(3))
    Scr:egpText(16,"4",Scr:egpPos(4))
    Scr:egpText(17,"5",Scr:egpPos(5))
    Scr:egpText(18,"6",Scr:egpPos(6))
    Scr:egpText(19,"7",Scr:egpPos(7))
    Scr:egpText(20,"8",Scr:egpPos(8))
    Scr:egpText(21,"9",Scr:egpPos(9))
    Scr:egpText(22,"0",Scr:egpPos(10))
    Scr:egpText(23,"C",Scr:egpPos(11))
    Scr:egpText(24,"E",Scr:egpPos(12))
    for (I=1, 12) {
        Scr:egpAlign(12+I,1,1)
        Scr:egpSize(12+I,40)
    }
    
    for (I=1, 12) {
        Scr:egpRoundedBoxOutline(24+I,Scr:egpPos(I),Scr:egpSize(I))
        Scr:egpAlpha(24+I,0)
    }
    Scr:egpRoundedBox(37,vec2(256,40),vec2(276,60))
    Scr:egpColor(37,vec(1,1,1))
    Scr:egpRoundedBox(38,vec2(256,40),vec2(256,40))
    
} # End draw

if (Private) {
Scr:egpText(39,stringRepeat("*",Entered:length()),Scr:egpPos(38))
} else {
    Scr:egpText(39,Entered,Scr:egpPos(38))
}
Scr:egpAlign(39,1,1)
Scr:egpSize(39,40)
Scr:egpFont(39,"")
Scr:egpColor(39,vec(0,0,0))
for (I=1,10) {
    if (pIR(I)) {
        #print(I)
        Scr:egpColor(I,vec(100,100,100))
        Scr:egpColor(I+12,vec(0,0,0))
        Scr:egpAlpha(I+24,255)
    if (Use & Entered:length()<12) {
        Entered=Entered+(I:toString())
        Scr:entity():soundPlay(1,0,"/ui/buttonclickrelease.wav")
    }
    } else {
        Scr:egpColor(I,vec(128,128,128))
        Scr:egpColor(I+12,vec(255,255,255))
        Scr:egpAlpha(I+24,0)
}
}

if (pIR(11)) {
    Scr:egpAlpha(11+24,255)
    Scr:egpColor(11+12,vec(0,0,0))
    if (Use) {Entered="" Scr:entity():soundPlay(1,0,"/buttons/button3.wav")}
} else {Scr:egpAlpha(11+24,0) Scr:egpColor(11+12,vec(255,255,255))}

if (pIR(12)) {
    Scr:egpAlpha(12+24,255)
    Scr:egpColor(12+12,vec(0,0,0))
    ####Code Enter####
    
    if (Use) {
        if (Entered==Code) {
            Entered="" 
            Menu=1 
            if (OutA) {Waitleft=OutA} else {Waitleft=3}
            Scr:entity():soundPlay(1,0,"/buttons/button9.wav")} else 
        {
        Fails++ 
        Entered="" 
        Scr:entity():soundPlay(1,0,"/buttons/weapon_cant_buy.wav")
        if (Fails==Tries) {Waitleft2=Locktime Menu=2 Fails=0 Scr:entity():soundPlay(11,0,"/ambient/alarms/city_firebell_loop1.wav")}
        } 
}    
    
} else {
    Scr:egpAlpha(12+24,0) Scr:egpColor(12+12,vec(255,255,255))
}
    
}
    
    if (Menu==1) {
        if (changed(Menu)) {
        Waitleft=OutA
        if (OutA==0) {Waitleft=3} else {Waitleft=OutA Text="Activated"}
    }
    timer("Wait",1000)
    
    
    
    if (clk("Wait")) {
        Waitleft--
            if (OutA) {
                Scr:egpText(2,Waitleft+" seconds left",vec2(256,300))
                Scr:egpAlign(2,1,1)
            }
        
        }
        if (OutA) {
            A=1
            Text="Activated"
        }
        
        
        
        if (!Waitleft) {
            if (OutA>0) {A=0}
            Menu=0
            }
        if (OutA==0) {
            Text="Toggled"
            if (changed(Menu) & Menu==1) {
            A=!A
        }
        }
        

        if (Draw) {
            Scr:egpText(1,Text,vec2(256,256))
            Scr:egpSize(1,50)
            Scr:egpAlign(1,1,1)
        }
        
    }


if (Menu==2) {
    if (changed(Menu) & Menu==2) {
        Waitleft2=Locktime 
        }
    Scr:egpText(1,"Locked!",vec2(256,256))
    Scr:egpAlign(1,1,1)
    Scr:egpSize(1,50)
    Scr:egpText(2,Waitleft2+" seconds remain.",vec2(256,300))
    Scr:egpAlign(2,1,1)
    timer("Waitleft",1000)
    if (clk("Waitleft")) {Waitleft2--}
if (Waitleft2==0) {Menu=0}
    
} else {soundStop(11)}    


#Lockpicking
if (Menu==3) {
    if (Draw) {
        Scr:egpText(1,"Lockpick",vec2(256,200))
        Scr:egpAlign(1,1,1)
        Scr:egpSize(1,50)
        Scr:egpBox(2,vec2(256,256),vec2(420,50))
        Scr:egpBox(3,vec2(256,256),vec2(400,30))
        Scr:egpColor(2,vec(0,0,0))
    }
    Allowed=0
    for (I=1, Lockpix:count()) {
        if (User:weapon():type()==Lockpix[I,string]) {Allowed=1}
    }
    if (!Allowed) {Menu=0}
    if (!User:keyAttack1()) {Pickpoint=Picktime Scr:egpAlpha(4,0)}
    if (User:keyAttack1()) {
        timer("Pick",1000)
        if (clk("Pick") & Pickpoint>-1) {
            Pickpoint--
            Scr:egpAlpha(4,255)
            Reloadsounds=array()
            Reloadsounds[1,string]="/weapons/357/357_reload1.wav"
            Reloadsounds[2,string]="/weapons/357/357_reload3.wav"
            Reloadsounds[3,string]="/weapons/357/357_reload4.wav"
            Index=Pickpoint
            for (I=1,Picktime) {
                if (Index>10) {Index-=10}
            }
            #setName(Index:toString())
            soundStop(Index)
            Scr:entity():soundPlay(Index,1,Reloadsounds[randint(1,Reloadsounds:count()),string])
            if (Pickpoint<0) {
            if (OutA) {Waitleft=OutA} else {Waitleft=3}
            Scr:entity():soundPlay(1,0,"/buttons/button9.wav")
                Menu=1
                
                }
            }
        Size1=400/Picktime
        if (Pickpoint>=0) {
        Size=Size1*(Picktime-Pickpoint) # How many are left
    }
        Pos=512-((256-(Size-Size1)/2)+(Scr:egpSize(3):x()/2)-Size1/2)
        #256-(370/2)+Size1 is 2 bits in
        #Imagine 370x30 as the screen dimensions
        #What do you do?
        # 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
        # S=(185/2)*Picktime
        # S2= S*(Picktime-Pickpoint)
        # S2*1 S2*2
        
        #256 is center
        #By the time I'm making this, I really want to get some sleep.
        #Too bad I'm forcing myself up UNTIL I get it working.
        
        
        #print(Size)
        #print(Pickoffset)
        Scr:egpBox(4,vec2(Pos,256),vec2(Size,30))
        Scr:egpColor(4,vec(0,128,0))
    }
    
}
    
    
    
    
    
#if (Draw) {print("Draw")}
if (changed(RollOver)) {
    soundVolume(0,200)
    if (RollOver) {
    Scr:entity():soundPlay(0,0,"/ui/buttonrollover.wav")
} else {}
}
if (Draw & Cursor) {
    Scr:egpBox(41,vec2(0,0),vec2(10,2))
    Scr:egpBox(42,vec2(0,0),vec2(2,10))
}
if (Cursor) {
Scr:egpPos(41,Scr:egpCursor(User))
Scr:egpPos(42,Scr:egpCursor(User))

}
Use=0

if (changed(Scr)) {
    entity():setAlpha(0)
    Scr:entity():setMaterial("models/shiny")
}
