@name Keypad
@inputs N1 N2 N3 N4 N5 N6 N7 N8 N9 N0 Enter Clear
@outputs Entered Open Valid
@persist String:string Valid 
@model models/expression 2/cpu_controller_nano.mdl
@trigger 
if(first()|duped()){
    reset()
    entity():setColor(vec())
    entity():setMaterial("Models/effects/vol_light001")
    if(fileRead("code.txt")){Code=fileRead("Code.txt")}else{
        fileLoad("code.txt")
        timer("wait5",5000)
        timer("wait",10000)
        print("Keypad will be operational in 10 seconds.")
    }
}

Code=fileRead("code.txt")

if(clk("wait5")){print("Keypad will be operational in 5 seconds.")}
if(clk("wait")){
    if(Code){
        print("Keypad is now activated!")
    }else{
        selfDestructAll() 
        print("Code not found! Self destructing!")
    }
}
if(N1){Num="1"}
if(N2){Num="2"}
if(N3){Num="3"}
if(N4){Num="4"}
if(N5){Num="5"}
if(N6){Num="6"}
if(N7){Num="7"}
if(N8){Num="8"}
if(N9){Num="9"}
if(N0){Num="0"}
if(Clear){String=""}
if(String:length()<4){
    String=String+Num
    Entered=String:toNumber()
}
if(String:length()==4){
    Entered=String:toNumber() timer("display",1000)
    if(String==Code){Valid=1 timer("valid",5000)}else{Valid=0}
}
if(String:length()>4){String=""}
if(clk("valid")){Valid=0 String=""}
if(clk("display")){Entered=0 String=""}
if(Valid){
    if(Enter){Open=1}
}else{Open=0}
