@name ByB Printing Press Core
@inputs Scr:wirelink Lockout Printer:entity MaxWith
@outputs Reset Open Backopen Eject
@persist Printers:array Owners:array Players:array LastCheck:array Total:array Draw Menu User:entity Select
@persist White:vector Bg:vector MaxSelect Rate Acc Stored:array CurTot:array Drawing NumLast
@persist Use Using #touchscreen vars
@persist Cursound Nolog:entity
interval(100)
#UP 119, 17
#DOWN 115,18
#SELECT 13
if (first()) {
    White=vec(255,255,255)
    Bg=White/2
    Rate=40/100 #% of money taken
    function options(N) {
        MaxSelect=N
    }
    function clearm() {
        Reset=1
        Select=1
        Use=0
        timer("norst",10)
    }
    function number getCenter(S:string) {
        return round(15-(S:length()/2))
    }
    clearm()
    function sound(S:string) {
        Cursound++
        if (Cursound>7) {Cursound=0}
        Scr:entity():soundPlay(Cursound,0,S)
        for (I=1, 7) {
            if (I!=Cursound) {soundStop(I)}
        }
    }
}


#magic touch scr2x2
Y=((Scr:entity():toLocal(User:aimPos()):rotate(ang()):x()+11)/22*18)+1
    if (!inrange(Y,floor(Y)+0.20,floor(Y)+1)) {Y=-1} else {Y=floor(Y)-1}
    if (first()) {
        function number pIR(N) {
            return Y==N
        }
    }
    
    
if (changed(Lockout)) {clearm()}
if (clk("norst")) {
    Reset=0
    timer("draw",10)
}
if (clk("draw")) {
    Draw=1
}
if (first() | changed(Menu) | changed(Scr)) {
    clearm()
}

if (!User) {
    findByClass("player")
    Player=findClosest(Scr:entity():pos())
    if (Player:aimEntity()==Scr:entity() & Player:keyUse() & Player!=Nolog) {
        User=Player
        clearm()
    }
} elseif (User:pos():distance(Scr:entity():pos())>200) {
    hint(User:name()+" has been auto-logged-out for leaving.",3)
    User=noentity()
    sound("/mvm/mvm_money_vanish.wav")
    Menu=0
    clearm()
} else {
    if (User:keyUse() & !Using) {Use=1 Using=1}
    if (!User:keyUse()) {Use=0 Using=0}

}
if (Menu==0) {
    if (!Lockout) {
    Scr:writeString("Press Anywhere to Begin",getCenter("Press Anywhere to Begin"),8)
    if (User) {
        Menu=1
        Use=0
        hint(User:name()+" has logged into Printer Press.",3)
        Acc=User:id()
        #Scr:entity():soundPlay(8,0,"/ui/gamestartup11.mp3")
        clearm()
    }
} else {
    Scr:writeString("Sorry, we're closed!",getCenter("Sorry, we're closed!"),8)
}
}
if (!User) {
    soundStop(8)
}

if (Menu==1) {
    Scr:writeString("Printer Press",getCenter("Printer Press"),0)
    options(3)
    Scr:writeString("Deposit Printer",getCenter("Deposit Printer"),3,White,Bg*(pIR(3)),pIR(3))
    if (Stored[Acc,number]>0) {
        Scr:writeString("Withdraw Money",getCenter("Withdraw Money"),5,White,Bg*(pIR(5)),pIR(5))
    } else {
        Scr:writeString("Withdraw Money",getCenter("Withdraw Money"),5,vec(255,0,0))
    }
    Scr:writeString("Policy",getCenter("Policy"),7,White,Bg*pIR(7),pIR(7))
    Scr:writeString("Quit",getCenter("Quit"),9,White,Bg*pIR(9),pIR(9))
    Scr:writeString(Stored:sum()+" printers in storage",getCenter(Stored:sum()+" printers in storage"),16)
    Scr:writeString("Magic Touch v1.1 by Scriptis",getCenter("Magic Touch v1.1 by Scriptis"),17)
    if (Use & !changed(Menu)) {
        if (pIR(3)) {Menu=2 clearm()}
        if (pIR(5) & Stored[Acc,number]>0) {Menu=3 clearm()}
        if (pIR(7)) {Menu=5 clearm()}
        if (pIR(9)) {Nolog=User User=noentity() Menu=0 sound("/buttons/button9.wav") hint(Nolog:name()+" has exited Printer Press.",3)}
        
    }
    
}

if (Menu==2) {
    Scr:writeString("Insert printer below",getCenter("Insert printer below"),7)
    Open=1
    if (changed(Menu)) {sound("/buttons/bell1.wav")}
    Scr:writeString("Back",0,17,White,Bg*pIR(17),pIR(17))
    if (pIR(17) & Use) {Menu=1 sound("/ui/freeze_cam.wav") clearm()}
    if (Printer & Printer:type()=="money_printer" & !Owners[Printer:id(),entity] & !Printer:isPlayerHolding()) {
        Open=0
        Owners[Printer:id(),entity]=User
        Stored[Acc,number]=Stored[Acc,number]+1
        Menu=1
        sound("/mvm/mvm_bought_upgrade.wav")
        hint(Owners[Printer:id(),entity]:name()+" deposited a "+(Printer:printerType())+" printer.",3)
    }
} else {Open=0}
    
    

if (Menu==3) {
    if (changed(MaxWith)) {clearm()}
    Scr:writeString("Withdraw",getCenter("Withdraw"),0)
    Scr:writeString("Your Account: $"+(Total[Acc,number]),getCenter("Your Account: $"+(Total[Acc,number])),3)
    Scr:writeString("Back",0,17,White,Bg*pIR(17),pIR(17))
    if (pIR(17) & Use) {Menu=1 sound("/ui/freeze_cam.wav") clearm()}
    if (Total[Acc,number]>0 &  MaxWith>0) {
        Scr:writeString("Withdraw Maximum",getCenter("Withdraw Maximum"),4,White,Bg*pIR(4),pIR(4))
} else {
        Scr:writeString("No money to withdraw.",getCenter("No money to withdraw."),4,vec(255,0,0))
    }
    Scr:writeString("Money in Storage: $"+MaxWith,getCenter("Money in Storage: $"+MaxWith),15)
    if (Use & pIR(4)) {
        Drawing=1
        timer("nodraw",5000)
        if (Total[Acc,number]>MaxWith) {
            Eject=MaxWith
            sound("/buttons/button6.wav")
            hint("Removing $"+MaxWith+" from "+(User:name())+"'s account (MAXWITH)",3)
            Total[Acc,number]=Total[Acc,number]-MaxWith
        } else {
        hint("Removing $"+Total[Acc,number]+" from "+(User:name())+"'s account.",3)
            Eject=Total[Acc,number]
            Total[Acc,number]=0
        
    }
    Menu=1
    clearm()
    }
}

if (Menu==5) {
    if (changed(Menu)) {sound("/npc/scanner/combat_scan5.wav")}
    Scr:writeString("We are NOT RESPONSIBLE for anyDESTROYED or STOLEN printers. USE AT YOUR OWN RISK.         We keep "+(Rate*100)+"% of ALL MONEY.     NO REFUNDS ON ANYTHING EVER!",0,0)
    Scr:writeString("Back",0,17,White,Bg*pIR(17),pIR(17))
    if (pIR(17) & Use) {Menu=1 sound("/ui/freeze_cam.wav") clearm()}
}





if (Menu==2 & User:pos():distance(Scr:entity():pos())>200) {Menu=0 clearm()}
if (Lockout) {Menu=0}
Use=0


if (owner():aimEntity():type()=="money_printer") {
    Cur=owner():aimEntity()
    Owner=Owners[Cur:id(),entity]
    if (!Owner) {Name="Unknown Player"} else {Name=Owner:name()}
    print(_HUD_PRINTCENTER,Name+"'s printer | Contains $"+(Cur:printedMoney()))
    if (Cur:printedMoney()<CurTot[Cur:id(),number]) {
        Players=players()
        for (I=1, Players:count()) {
            if (Players[I,entity]==Owner) {
                Total[Owner:id(),number]=round(Total[Owner:id(),number]+(CurTot[Cur:id(),number]-Cur:printedMoney())*(1-Rate))
                hint("Added $"+round((CurTot[Cur:id(),number]*(1-Rate)))+" to "+(Owner:name())+"'s account",3)
                CurTot[Cur:id(),number]=0
            }
    }
    }
    CurTot[Cur:id(),number]=Cur:printedMoney()
} 

runOnChat(1)
if (chatClk(owner()) & owner():lastSaid():lower()=="-a") {
    hideChat(1)
    hint("You need to insert $"+round((Total:sum()-MaxWith)*1)+" for maximum effeciency.",3)
}

Backopen=!Open
if (clk("nodraw")) {Drawing=0}

if (clk("nc")) {Kick=0}
Use=0

if (Nolog & Nolog:aimEntity()!=Scr:entity()) {
    Nolog=noentity()
}

#scriptis[bybrp#1] 2013
