@name Swipe Lock
@inputs Scr:wirelink Use:entity
@outputs Correct Wrong Pass:array OriginalPass:array
@persist User:entity OriginalPassSet Finishing OriginalPassSize PassCorrect Cursor Hovering P2 P3 P4 P5 P6 P7 P8 P9 P10
runOnTick(1)

function void render() {
    Scr:egpClear()
    
    ##RENDERING
        ##BACKGROUND
    Scr:egpBox(1, vec2(-10,256), vec2(1100,1000))
    Scr:egpColor(1, vec(0,0,255))
    Scr:egpMaterial(1, "gui/center_gradient")
        
        ##OUTLINES
    Scr:egpCircle(2, vec2(80,80), vec2(70,70))
    Scr:egpColor(2, vec(100,100,100))
    
    Scr:egpCircle(3, vec2(256,80), vec2(70,70))
    Scr:egpColor(3, vec(100,100,100))
    
    Scr:egpCircle(4, vec2(432,80), vec2(70,70))
    Scr:egpColor(4, vec(100,100,100))
    
    Scr:egpCircle(5, vec2(80,256), vec2(70,70))
    Scr:egpColor(5, vec(100,100,100))
    
    Scr:egpCircle(6, vec2(256,256), vec2(70,70))
    Scr:egpColor(6, vec(100,100,100))
    
    Scr:egpCircle(7, vec2(432,256), vec2(70,70))
    Scr:egpColor(7, vec(100,100,100))
    
    Scr:egpCircle(8, vec2(80,432), vec2(70,70))
    Scr:egpColor(8, vec(100,100,100))
    
    Scr:egpCircle(9, vec2(256,432), vec2(70,70))
    Scr:egpColor(9, vec(100,100,100))
    
    Scr:egpCircle(10, vec2(432,432), vec2(70,70))
    Scr:egpColor(10, vec(100,100,100))
    
        ##OUTER CIRCLES
    Scr:egpCircle(11, vec2(80,80), vec2(60,60))
    
    Scr:egpCircle(12, vec2(256,80), vec2(60,60))
    
    Scr:egpCircle(13, vec2(432,80), vec2(60,60))
    
    Scr:egpCircle(14, vec2(80,256), vec2(60,60))
    
    Scr:egpCircle(15, vec2(256,256), vec2(60,60))
    
    Scr:egpCircle(16, vec2(432,256), vec2(60,60))
    
    Scr:egpCircle(17, vec2(80,432), vec2(60,60))
    
    Scr:egpCircle(18, vec2(256,432), vec2(60,60))
    
    Scr:egpCircle(19, vec2(432,432), vec2(60,60))
    
        ## INNER CIRCLES
    Scr:egpCircle(20, vec2(80,80), vec2(20,20))
    Scr:egpColor(20, vec(140,140,140))
    
    Scr:egpCircle(21, vec2(256,80), vec2(20,20))
    Scr:egpColor(21, vec(140,140,140))
    
    Scr:egpCircle(22, vec2(432,80), vec2(20,20))
    Scr:egpColor(22, vec(140,140,140))
    
    Scr:egpCircle(23, vec2(80,256), vec2(20,20))
    Scr:egpColor(23, vec(140,140,140))
    
    Scr:egpCircle(24, vec2(256,256), vec2(20,20))
    Scr:egpColor(24, vec(140,140,140))
    
    Scr:egpCircle(25, vec2(432,256), vec2(20,20))
    Scr:egpColor(25, vec(140,140,140))
    
    Scr:egpCircle(26, vec2(80,432), vec2(20,20))
    Scr:egpColor(26, vec(140,140,140))
    
    Scr:egpCircle(27, vec2(256,432), vec2(20,20))
    Scr:egpColor(27, vec(140,140,140))
    
    Scr:egpCircle(28, vec2(432,432), vec2(20,20))
    Scr:egpColor(28, vec(140,140,140))
}

if(first()) {
    render()
}

if(Use & !User:isValid()) {
    User = Use
}

## UTIL FUNCTIONS
function vector2 getPointOne(ID) {
    return Scr:egpPos(ID) + Scr:egpSize(ID)
}

function vector2 getPointTwo(ID) {
    return Scr:egpPos(ID) - Scr:egpSize(ID)
}

function number getPassID(Cursor:vector2) {
    if(inrange(Cursor, getPointTwo(2), getPointOne(2))) {
        return 2
    } elseif(inrange(Cursor, getPointTwo(3), getPointOne(3))) {
        return 3
    } elseif(inrange(Cursor, getPointTwo(4), getPointOne(4))) {
        return 4
    } elseif(inrange(Cursor, getPointTwo(5), getPointOne(5))) {
        return 5
    } elseif(inrange(Cursor, getPointTwo(6), getPointOne(6))) {
        return 6
    } elseif(inrange(Cursor, getPointTwo(7), getPointOne(7))) {
        return 7
    } elseif(inrange(Cursor, getPointTwo(8), getPointOne(8))) {
        return 8
    } elseif(inrange(Cursor, getPointTwo(9), getPointOne(9))) {
        return 9
    } elseif(inrange(Cursor, getPointTwo(10), getPointOne(10))) {
        return 10
    } else {return 0}
}

function void setMassColor(Vec:vector) {
    local I = 0
    while(I < 10) {
        I = I + 1
        Scr:egpColor(I, Vec)
        if(I == 1) {Scr:egpColor(I, Vec / 4)}
    }
}

function void swipeReset() {
    if(Finishing == 2) {reset()}
    Pass = array()
    Correct = 0
    Wrong = 0
    User = noentity()
    PassCorrect = 0
    while(Cursor != 0) {
        Scr:egpRemove(Cursor + 30)
        Scr:egpRemove(Cursor + 60)
        Cursor = Cursor - 1
    }
    
    P2 = 0
    P3 = 0
    P4 = 0
    P5 = 0
    P6 = 0
    P7 = 0
    P8 = 0
    P9 = 0
    
    render()
    
    Finishing = 0
}

function void finish(Reset) {
    Finishing = 1
    if(Reset == 1) {Finishing = 2}
    timer("FinishTimer", 3000)
}

if(clk("FinishTimer")) {swipeReset()}

##PASSWORD GENERATING

if(User:isValid() & Finishing == 0) {
    if(User:keyUse()) {
        Hovering = getPassID(Scr:egpCursor(User))
        if(Hovering == 2) {
            if(OriginalPassSet == 0) {
                if(changed(OriginalPassSet)) {OriginalPass[OriginalPassSize + 1, number] = 2 soundPlay(1, 0, "misc/doomsday_pickup.wav") OriginalPassSize = OriginalPassSize + 1}
            } else {
                if(P2 == 0) {P2 = 1 Pass[Pass:count() + 1, number] = 2 soundPlay(1, 0, "misc/doomsday_pickup.wav") }
            }
        } elseif(Hovering == 3) {
            if(OriginalPassSet == 0) {
                if(changed(OriginalPassSet)) {OriginalPass[OriginalPassSize + 1, number] = 3 soundPlay(1, 0, "misc/doomsday_pickup.wav") OriginalPassSize = OriginalPassSize + 1}
            } else {
                if(P3 == 0) {P3 = 1 Pass[Pass:count() + 1, number] = 3 soundPlay(1, 0, "misc/doomsday_pickup.wav")}
            }
        } elseif(Hovering == 4) {
            if(OriginalPassSet == 0) {
                if(changed(OriginalPassSet)) {OriginalPass[OriginalPassSize + 1, number] = 4 soundPlay(1, 0, "misc/doomsday_pickup.wav") OriginalPassSize = OriginalPassSize + 1}
            } else {
                if(P4 == 0) {P4 = 1 Pass[Pass:count() + 1, number] = 4 soundPlay(1, 0, "misc/doomsday_pickup.wav")}
            }
        } elseif(Hovering == 5) {
            if(OriginalPassSet == 0) {
                if(changed(OriginalPassSet)) {OriginalPass[OriginalPassSize + 1, number] = 5 soundPlay(1, 0, "misc/doomsday_pickup.wav") OriginalPassSize = OriginalPassSize + 1}
            } else {
                if(P5 == 0) {P5 = 1 Pass[Pass:count() + 1, number] = 5 soundPlay(1, 0, "misc/doomsday_pickup.wav")}
            }
        } elseif(Hovering == 6) {
            if(OriginalPassSet == 0) {
                if(changed(OriginalPassSet)) {OriginalPass[OriginalPassSize + 1, number] = 6 soundPlay(1, 0, "misc/doomsday_pickup.wav") OriginalPassSize = OriginalPassSize + 1}
            } else {
                if(P6 == 0) {P6 = 1 Pass[Pass:count() + 1, number] = 6 soundPlay(1, 0, "misc/doomsday_pickup.wav")}
            }
        } elseif(Hovering == 7) {
            if(OriginalPassSet == 0) {
                if(changed(OriginalPassSet)) {OriginalPass[OriginalPassSize + 1, number] = 7 soundPlay(1, 0, "misc/doomsday_pickup.wav") OriginalPassSize = OriginalPassSize + 1 }
            } else {
                if(P7 == 0) {P7 = 1 Pass[Pass:count() + 1, number] = 7 soundPlay(1, 0, "misc/doomsday_pickup.wav")}
            }
        } elseif(Hovering == 8) {
            if(OriginalPassSet == 0) {
                if(changed(OriginalPassSet)) {OriginalPass[OriginalPassSize + 1, number] = 8 soundPlay(1, 0, "misc/doomsday_pickup.wav") OriginalPassSize = OriginalPassSize + 1}
            } else {
                if(P8 == 0) {P8 = 1 Pass[Pass:count() + 1, number] = 8 soundPlay(1, 0, "misc/doomsday_pickup.wav")}
            }
        } elseif(Hovering == 9) {
            if(OriginalPassSet == 0) {
                if(changed(OriginalPassSet)) {OriginalPass[OriginalPassSize + 1, number] = 9 soundPlay(1, 0, "misc/doomsday_pickup.wav") OriginalPassSize = OriginalPassSize + 1}
            } else {
                if(P9 == 0) {P9 = 1 Pass[Pass:count() + 1, number] = 9 soundPlay(1, 0, "misc/doomsday_pickup.wav")}
            }
        } elseif(Hovering == 10) {
            if(OriginalPassSet == 0) {
                if(changed(OriginalPassSet)) {OriginalPass[OriginalPassSize + 1, number] = 10 soundPlay(1, 0, "misc/doomsday_pickup.wav") OriginalPassSize = OriginalPassSize + 1}
            } else {                
                if(P10 == 0) {P10 = 1 Pass[Pass:count() + 1, number] = 10 soundPlay(1, 0, "misc/doomsday_pickup.wav")}
            }
        }
    } else {
        if(OriginalPassSet == 0) {
            if(OriginalPassSize >= 4) {
                OriginalPassSet = 1
                setMassColor(vec(255,100,0))
                finish(0)
                #soundPlay(1, 0, "misc/killstreak.wav")
            } else {
                setMassColor(vec(150,0,0))
                finish(1)
                #soundPlay(1, 0, "misc/freeze_cam.wav")
            }
        } else {
            PassCorrect = 2
            if(Pass[1, number] == OriginalPass[1, number] & Pass[2, number] == OriginalPass[2, number] & Pass[3, number] == OriginalPass[3, number] & Pass[4, number] == OriginalPass[4, number]) {
                if(Pass:exists(5)) {
                    if(Pass[5, number] == OriginalPass[5, number]) {
                        if(Pass:exists(6)) {
                            if(Pass[6, number] == OriginalPass[6, number]) {
                                if(Pass:exists(7)) {
                                    if(Pass[7, number] == OriginalPass[7, number]) {
                                        if(Pass:exists(8)) {
                                            if(Pass[8, number] == OriginalPass[8, number]) {
                                                if(Pass:exists(9)) {
                                                    if(Pass[9, number] == OriginalPass[9, number]) {
                                                        PassCorrect = 1
                                                    } else {
                                                        PassCorrect = 2
                                                    }
                                                } else {
                                                    PassCorrect = 1
                                                }
                                            } else {
                                                PassCorrect = 1
                                            }
                                        }
                                    } else {
                                        PassCorrect = 1
                                    }
                                } else {
                                    PassCorrect = 1
                                }
                            }
                        } else {
                            PassCorrect = 1
                        }
                    }
                } else {
                    PassCorrect = 1
                }
            } else {PassCorrect = 2}
            
            if(PassCorrect == 1) {
                Correct = 1
                setMassColor(vec(0,255,0))
                finish(0)
                #soundPlay(1, 0, "misc/killstreak.wav")
            } else {
                Wrong = 1
                setMassColor(vec(255,0,0))
                finish(0)
                #soundPlay(1, 0, "misc/freeze_cam.wav")
            }
        }
    }
}

