@name Touch Security E2 Re-amped
@inputs X Y Use Target:entity CS:wirelink Entrance:entity Keypad1 Keypad2
@outputs Yout Xout 
@persist Xout Yout TitleRGB Number1 Fire Safety Distance:array DestroyD Title:string Page Log:array Destroy:array Time2:array Name:string Job:string Status1:string Status2:string
@model 
runOnChat(1)
interval(2000)


Ran = random(0,999)
S = owner():lastSaid():explode(" ")
Players = players()
E = entity()
findByClass("Player")
P=findClosest(entity():pos())
Dist=P:pos():distance(entity():pos())
PD = findClosest(owner():pos())
OP = PD:pos():distance(entity():pos())



if( Dist <= DestroyD & P:keyUse() & Fire == 1){
    hint("E2 is Being sabotgoed! 20 Secs before its gone !",10)
    Fire = 0
    Destroy[1,string] = P:name()
    timer("Begin",5000)
    concmd("say /advert "+Destroy[1,string]+" Has Sabotoged My E2 !")
}

if(clk("Begin")){
    Log:clear()
    hint("Logs Deleted",10)
    timer("2nd",5000)
}
if(P:aimEntity() == entity() & owner():keyReload() & OP <= DestroyD & Fire == 0){
    timer("STOP",100)
    Fire = 1
}
if(clk("STOP")){
    hint("You Saved your E2 !",10)
    concmd("say /advert I have saved my E2 !")
    Fire = 1
    stoptimer("2nd")
    stoptimer("3rd")
    stoptimer("Begin")
}
if(clk("2nd")){
    Title = Destroy[1,string]+" is a hacker !"
    hint("Title Is Sabotoged",10)
    TitleRGB = Ran
    timer("3rd",10000)
}
if(clk("3rd")){
    hint("E2 Destroyed !",10)
    selfDestruct()
}

GMT = -4


Health1 = Entrance:health()
Armor1 = Entrance:armor()

function string currentTime(){
    Time = time("hour")+GMT+":"+time("min")+":"+time("sec")
    return Time
}
Day1 = time("day")
Month2 = time("month")
if(first()){
    DestroyD = 60
    Fire = 1
    Safety = 1
    CS[2041] = 1
    timer("1",500)
    Destroy:clear()
    Title = "Generic Title"
    TitleRGB = 999 
}
if(clk("1")){
    timer("1",500)
    CS[2041] = 1
}

Xout = round(X*100)
Yout = round(Y*100)
if(~Entrance & Entrance){
    hint(Entrance:name()+" Went in your entrance or is near",10)
    Log:pushEntity(Entrance)
    Time2:pushString(currentTime())
}
if(~Target & Page<5){
    if(!Target & Page != 3){
        Page = 0
    } elseif(Target){
        Page = 1
    }
}
if(Page == 0){
    CS:writeString(Title, 15-round(Title:length()/2), 8, TitleRGB)
    CS:writeString(currentTime(),11,6,Ran)
}
if(Page == 1){
    CS:writeString("=============Main=============",0,0,TitleRGB)
    CS:writeString("-=Logs=-",11,2,9)
    CS:writeString("-=Status=-",10,4,9)
    CS:writeString(currentTime(),11,6,Ran)
}
if(Page == 2){
    CS:writeString("=============Logs=============",0,0,TitleRGB)
    CS:writeString("Back",13,16,9)
    for(I=1,Log:count()){
        CS:writeString(I+". "+Log[I,entity]:name()+" "+Time2[I,string],0,I,TitleRGB)
    }
}
if(Page == 3){
    CS:writeString("============Status============",0,0,TitleRGB)
    CS:writeString("Name: "+Name,0,1,34)
    CS:writeString("Job: "+Job,0,2,34)
    CS:writeString("Health: ",0,3,34)
    CS:writeString("Armor: ",0,4,34)
    CS:writeString("Keypad_1: "+Status1,0,5,34)
    CS:writeString("Keypad_2: "+Status2,0,6,34)
    CS:writeString("==============================",0,8,TitleRGB)
    CS:writeString("-=Back=-",11,10,34)
    
if((~Keypad1 & Keypad1) & Entrance){
    hint(Entrance:name()+" has Cracked the First Keypad",10)
}
if((~Keypad2 & Keypad2) & Entrance){
    hint(Entrance:name()+" has Cracked the Secound Keypad",10)
}
if(~Keypad1 & Keypad1){
    hint("Keypad #1 Cracked",10)
}elseif(~Keypad2 & Keypad2){
    hint("Keypad #2 Cracked",10)
}
if(!Entrance){
    CS:writeString("Safe",0,7,900,999)
}
if(Entrance){
    CS:writeString("Someone in Entrance",0,7,900,999,900)
}
if(Entrance){
    Name = Entrance:name()
}else{
    Name = "No Target"
}
if(Entrance){
    Job = Entrance:team():teamName()
}else{
    Job = "No Target"
}
if(Keypad1){
    Status1 = "Open"
}else{
    Status1 = "Closed"
}
if(Keypad2){
    Status2 = "Open"
}else{
    Status2 = "Closed"
}
if(Health1 >= 1){
         if(Health1 == 100){
          CS:writeString("Full Heath",8,3,50)
         }elseif(Health1 >= 75){
            CS:writeString("Three Forths full",8,3,50)
           }elseif(Health1 >= 50){
           CS:writeString("Half Full",8,3,68)
             }elseif(Health1 >= 25){
           CS:writeString("Low Health",8,3,900)
    }
}elseif(!Entrance){
    CS:writeString("No Target",8,3,50)
}
if(Armor1 >= 1){
        if(Armor1 == 100){
            CS:writeString("Full",8,4,50)
        }
        if(Armor1 >= 75){
            CS:writeString("Three fourths",8,4,50)
        }
        if(Armor1 >= 50){
            CS:writeString("Half full",8,4,50)
        }
        if(Armor1 >= 25){
            CS:writeString("Low Armor",8,4,50)
        }
}elseif(Armor1 == 0){
    CS:writeString("No Armor",8,4,50)
}
}

if(Xout >= -18 & Yout >= -21 & Target & Page == 3){
    if(Xout <= 24 & Yout <= -13 & Target & Page == 3){
        CS:writeString("-=Back=-",11,10,34,900)
        if(~Use & Use){
            Page = 1
            CS[2041] = 1
        }
    }
}

if(Xout >= -14 & Yout >= -83 & Target & Page == 2){
    if(Xout <= 14 & Yout <= -75 & Target & Page == 2){
        if(~Use & Use){
            Page = 1
            CS[2041] = 1
        }
        CS:writeString("Back",13,16,9,900)
    }
}
if(Xout >= -32 & Yout >= 41 & Target & Page == 1){
    if(Xout <= 45 & Yout <= 49 & Target & Page == 1){
            CS:writeString("-=Status=-",10,4,9,900)
            if(~Use & Use){
                CS[2041] = 1
                Page = 3
            }
        }
    }
if(Xout >= -25 & Yout >= 62 & Target & Page == 1){
    if(Xout <= 27 & Yout <= 71 & Target & Page == 1){
        CS:writeString("-=Logs=-",11,2,9,900)
        if(~Use & Use){
            CS[2041] = 1
            Page = 2
        }
    }
}
if(!owner():isAlive() & Safety == 1){
    hint("Logs Cleared since you died",10)
    Safety = 0
    timer("Death",8000)
    Log:clear()
}
if(clk("Death")){
    Safety = 1
}
if(S[1,string] == "-dist" & S[2,string] & chatClk(owner())){
    Number1 = S[2,string]:toNumber()
    if(Number1 < 60){
        hint("Distance needs a minimun of 60 set it or higher",10)
        DestroyD = 60
    }else{
        DestroyD = Number1
        hint("Destroy Distance set to: "+DestroyD:toString(),10)
    }
    hideChat(1)
}

if(S[1,string] == "-title" & chatClk(owner())) {
    S:remove(1)
    TitleRGB = S[1,string]:toNumber()
    S:remove(1)
    Title = S:concat(" ")
    hideChat(1)
}
if(Log:count() == 16){
    hint("Maxed Logs Reached, Clearing Logs!",10)
    Log:clear()
}
