@name Bowling
@inputs Scr:wirelink Scr2:wirelink Lane:entity
@outputs Start Score Det B1
@persist Name:string Temp:string Count Pos:vector B:entity PT Turn Ball:entity Bowled Moved:array Players:array Scores:array Frame:array Pins:array Startpos:array

@trigger 
runOnTick(1)
runOnChat(1)
if(first()|dupefinished()){
    concmd("evs convar sbox_e2_maxpropspersecond 50")
    #concmd("say [BOWLING] "+owner():name()+" is hosting a bowling game! the maximum is 6 players. type /join to sign up for bowling!")
    Scr:egpClear()
    Scr:egpBoxOutline(1,vec2(256,256),vec2(501,501))
    Scr:egpBox(2,vec2(256,256),vec2(499,499))
    Scr:egpBoxOutline(3,vec2(255,55),vec2(498,100))
    Scr:egpText(4,"BOWLING V1.0",vec2(10,10))
    
    ##top name
    Scr:egpBoxOutline(5,vec2(55,85),vec2(100,40))
    
    ##frames
    Scr:egpBoxOutline(6,vec2(125,85),vec2(40,40))
    Scr:egpBoxOutline(7,vec2(165,85),vec2(40,40))
    Scr:egpBoxOutline(8,vec2(205,85),vec2(40,40))
    Scr:egpBoxOutline(9,vec2(245,85),vec2(40,40))
    Scr:egpBoxOutline(10,vec2(285,85),vec2(40,40))
    Scr:egpBoxOutline(11,vec2(325,85),vec2(40,40))
    Scr:egpBoxOutline(12,vec2(365,85),vec2(40,40))
    Scr:egpBoxOutline(13,vec2(405,85),vec2(40,40))
    Scr:egpBoxOutline(14,vec2(445,85),vec2(40,40))
    Scr:egpBoxOutline(15,vec2(485,85),vec2(40,40))
    
    ##scoregrid
    Scr:egpBoxOutline(16,vec2(55,255),vec2(100,300))
    
    Scr:egpBoxOutline(17,vec2(125,255),vec2(40,300))
    Scr:egpBoxOutline(18,vec2(165,255),vec2(40,300))
    Scr:egpBoxOutline(19,vec2(205,255),vec2(40,300))
    Scr:egpBoxOutline(20,vec2(245,255),vec2(40,300))
    Scr:egpBoxOutline(21,vec2(285,255),vec2(40,300))
    Scr:egpBoxOutline(22,vec2(325,255),vec2(40,300))
    Scr:egpBoxOutline(23,vec2(365,255),vec2(40,300))
    Scr:egpBoxOutline(24,vec2(405,255),vec2(40,300))
    Scr:egpBoxOutline(25,vec2(445,255),vec2(40,300))
    Scr:egpBoxOutline(26,vec2(485,255),vec2(40,300))
    
    Scr:egpBoxOutline(27,vec2(300,130),vec2(1000,50))
    Scr:egpBoxOutline(28,vec2(300,180),vec2(1000,50))
    Scr:egpBoxOutline(29,vec2(300,230),vec2(1000,50))
    Scr:egpBoxOutline(30,vec2(300,280),vec2(1000,50))
    Scr:egpBoxOutline(31,vec2(300,330),vec2(1000,50))
    
    ##top bar
    Scr:egpText(32,"Names",vec2(30,85))
    
    Scr:egpText(33,"1",vec2(120,85))
    Scr:egpText(34,"2",vec2(160,85))
    Scr:egpText(35,"3",vec2(200,85))
    Scr:egpText(36,"4",vec2(240,85))
    Scr:egpText(37,"5",vec2(280,85))
    Scr:egpText(38,"6",vec2(320,85))
    Scr:egpText(39,"7",vec2(360,85))
    Scr:egpText(40,"8",vec2(400,85))
    Scr:egpText(41,"9",vec2(440,85))
    Scr:egpText(42,"10",vec2(480,85))
    
    
    ##names/score
    
    #names
    Scr:egpText(43,"",vec2(15,112))
    Scr:egpText(44,"",vec2(15,162))
    Scr:egpText(45,"",vec2(15,212))
    Scr:egpText(46,"",vec2(15,262))
    Scr:egpText(47,"",vec2(15,312))
    Scr:egpText(48,"",vec2(15,362))
    
    Scr:egpText(51,"total: ",vec2(15,130))
    Scr:egpText(52,"total: ",vec2(15,180))
    Scr:egpText(53,"total: ",vec2(15,230))
    Scr:egpText(54,"total: ",vec2(15,280))
    Scr:egpText(55,"total: ",vec2(15,330))
    Scr:egpText(56,"total: ",vec2(15,380))
    
    #score
        for(I=0,9){
    Scr:egpText(60+(I*10),"",vec2(115+(I*40),120))
    Scr:egpText(61+(I*10),"",vec2(115+(I*40),170))
    Scr:egpText(62+(I*10),"",vec2(115+(I*40),220))
    Scr:egpText(63+(I*10),"",vec2(115+(I*40),270))
    Scr:egpText(64+(I*10),"",vec2(115+(I*40),320))
    Scr:egpText(65+(I*10),"",vec2(115+(I*40),370))
}

    
    Scr:egpColor(2,vec(100,100,255))
    
    Scr:egpFont(4,"Courier New",36)
    
    Scr:egpFont(43,"Courier New",26)
    Scr:egpFont(44,"Courier New",26)
    Scr:egpFont(45,"Courier New",26)
    Scr:egpFont(46,"Courier New",26)
    Scr:egpFont(47,"Courier New",26)
    Scr:egpFont(48,"Courier New",26)
    
    ###SCREEN2
    Scr2:egpClear()
    
    ##top name
    Scr2:egpBoxOutline(5,vec2(55,85),vec2(100,40))
    
    ##frames
    Scr2:egpBoxOutline(6,vec2(125,85),vec2(40,40))
    Scr2:egpBoxOutline(7,vec2(165,85),vec2(40,40))
    Scr2:egpBoxOutline(8,vec2(205,85),vec2(40,40))
    Scr2:egpBoxOutline(9,vec2(245,85),vec2(40,40))
    Scr2:egpBoxOutline(10,vec2(285,85),vec2(40,40))
    Scr2:egpBoxOutline(11,vec2(325,85),vec2(40,40))
    Scr2:egpBoxOutline(12,vec2(365,85),vec2(40,40))
    Scr2:egpBoxOutline(13,vec2(405,85),vec2(40,40))
    Scr2:egpBoxOutline(14,vec2(445,85),vec2(40,40))
    Scr2:egpBoxOutline(15,vec2(485,85),vec2(40,40))
    
    ##scoregrid
    Scr2:egpBoxOutline(16,vec2(55,255),vec2(100,300))
    
    Scr2:egpBoxOutline(17,vec2(125,255),vec2(40,300))
    Scr2:egpBoxOutline(18,vec2(165,255),vec2(40,300))
    Scr2:egpBoxOutline(19,vec2(205,255),vec2(40,300))
    Scr2:egpBoxOutline(20,vec2(245,255),vec2(40,300))
    Scr2:egpBoxOutline(21,vec2(285,255),vec2(40,300))
    Scr2:egpBoxOutline(22,vec2(325,255),vec2(40,300))
    Scr2:egpBoxOutline(23,vec2(365,255),vec2(40,300))
    Scr2:egpBoxOutline(24,vec2(405,255),vec2(40,300))
    Scr2:egpBoxOutline(25,vec2(445,255),vec2(40,300))
    Scr2:egpBoxOutline(26,vec2(485,255),vec2(40,300))
    
    Scr2:egpBoxOutline(27,vec2(300,130),vec2(1000,50))
    Scr2:egpBoxOutline(28,vec2(300,180),vec2(1000,50))
    Scr2:egpBoxOutline(29,vec2(300,230),vec2(1000,50))
    Scr2:egpBoxOutline(30,vec2(300,280),vec2(1000,50))
    Scr2:egpBoxOutline(31,vec2(300,330),vec2(1000,50))
    
    ##top bar
    Scr2:egpText(32,"Names",vec2(30,85))
    
    Scr2:egpText(33,"1",vec2(120,85))
    Scr2:egpText(34,"2",vec2(160,85))
    Scr2:egpText(35,"3",vec2(200,85))
    Scr2:egpText(36,"4",vec2(240,85))
    Scr2:egpText(37,"5",vec2(280,85))
    Scr2:egpText(38,"6",vec2(320,85))
    Scr2:egpText(39,"7",vec2(360,85))
    Scr2:egpText(40,"8",vec2(400,85))
    Scr2:egpText(41,"9",vec2(440,85))
    Scr2:egpText(42,"10",vec2(480,85))
    
    
    ##names/score
    
    #names
    Scr2:egpText(43,"",vec2(15,112))
    Scr2:egpText(44,"",vec2(15,162))
    Scr2:egpText(45,"",vec2(15,212))
    Scr2:egpText(46,"",vec2(15,262))
    Scr2:egpText(47,"",vec2(15,312))
    Scr2:egpText(48,"",vec2(15,362))
    
    Scr2:egpText(51,"total: ",vec2(15,130))
    Scr2:egpText(52,"total: ",vec2(15,180))
    Scr2:egpText(53,"total: ",vec2(15,230))
    Scr2:egpText(54,"total: ",vec2(15,280))
    Scr2:egpText(55,"total: ",vec2(15,330))
    Scr2:egpText(56,"total: ",vec2(15,380))
    
    #score
        for(I=0,9){
    Scr2:egpText(60+(I*10),"",vec2(115+(I*40),120))
    Scr2:egpText(61+(I*10),"",vec2(115+(I*40),170))
    Scr2:egpText(62+(I*10),"",vec2(115+(I*40),220))
    Scr2:egpText(63+(I*10),"",vec2(115+(I*40),270))
    Scr2:egpText(64+(I*10),"",vec2(115+(I*40),320))
    Scr2:egpText(65+(I*10),"",vec2(115+(I*40),370))
}

    
    Scr:egpColor(2,vec(100,100,255))
    
    Scr:egpFont(4,"Courier New",36)
    
    Scr:egpFont(43,"Courier New",26)
    Scr:egpFont(44,"Courier New",26)
    Scr:egpFont(45,"Courier New",26)
    Scr:egpFont(46,"Courier New",26)
    Scr:egpFont(47,"Courier New",26)
    Scr:egpFont(48,"Courier New",26)
}
##joining/leaving
if(Start==0){
if(chatClk()){if(lastSaid():lower()=="/join"){Players:pushEntity(lastSpoke())
    Scr:egpSetText(42+Players:count(),lastSpoke():name():sub(1,6))
    Scr2:egpSetText(42+Players:count(),lastSpoke():name():sub(1,6))}}
if(chatClk()){if(lastSaid():lower()=="/leave"){for(I=1,Players:count()){
            if(lastSpoke():name()==Players[I,entity]:name()){
                concmd("say [BOWLING] "+lastSpoke():name()+" has left bowling!")
                Scr:egpSetText(42+Players:count(),"")
                Scr2:egpSetText(42+Players:count(),"")
                Players:remove(I)
            }
        } 
    }
}
}
##check if player is already in array
if(Players:count()>6){
    concmd("say [BOWLING] sorry "+Players[6,entity]:name()+"! but a maximum of 6 players is allowed")
    Players:remove(7)}
for(I=1,Players:count()){
    Temp=Players[I,entity]:name()
    Count=0
    for(I2=1,Players:count()){
        if(Temp==Players[I2,entity]:name()){Count+=1}
        if(Count>=2){
            Scr:egpSetText(42+I2,"")
            Players:remove(I2)}
    }
}

##scoring/setup

#setup

if(chatClk(owner())){if(owner():lastSaid():lower()  =="/start"){Start=1}}

if(changed(Start)&Start==1){
    Turn=1

    for(I=1,10){
        if(I==1){Pos=vec(5,375,38)}
        if(I==2){Pos=vec(20,425,38)}
        if(I==3){Pos=vec(-12,425,38)}
        if(I==4){Pos=vec(40,475,38)}
        if(I==5){Pos=vec(-32,475,38)}
        if(I==6){Pos=vec(60,525,38)}
        if(I==7){Pos=vec(-52,525,38)}
        if(I==8){Pos=vec(5,475,38)}
        if(I==9){Pos=vec(20,525,38)}
        if(I==10){Pos=vec(-12,525,38)}
        B=propSpawn("models/hunter/blocks/cube025x075x025.mdl",Lane:toWorld(Pos),Lane:toWorld(ang(0,0,90)),1)
        Pins:pushEntity(B)
        Startpos:pushVector(B:pos())
        Moved:pushNumber(0)
        Frame:pushNumber(0)
    }
}
##record scores and reset
if(clk("score")|clk("0")){
    stoptimer("0")
    
    Ball:propDelete()
    for(I=1,10){Pins[I,entity]:propFreeze(1)
    if(Moved[I,number]==1){Pins[I,entity]:propDelete()}
    }
    Det=(Turn-1)+Frame[Turn,number]*10
    ##first ball
    if(Score<10&PT==0){
    B1=Score
    Scr:egpSetText(60+Det,""+Score)
    Scr2:egpSetText(60+Det,""+Score)
    }
    if(Score>=10&PT==0){
    Scr:egpSetText(60+Det,"X")
    Scr2:egpSetText(60+Det,"X")
    Scores[Turn,number]=Scores[Turn,number]+20
    }
    ##second ball
    if(Score<10&PT==1){
    Scr:egpSetText(60+Det,B1+" "+(Score-B1))
    Scr2:egpSetText(60+Det,B1+" "+(Score-B1))
    Scores[Turn,number]=Scores[Turn,number]+Score
    }
    if(Score>=10&PT==1){
    Scr:egpSetText(60+Det,B1+" /")
    Scr2:egpSetText(60+Det,B1+" /")
    Scores[Turn,number]=Scores[Turn,number]+(B1+10)
    }
    Scr:egpSetText(50+Turn,"total: "+Scores[Turn,number])
    Scr2:egpSetText(50+Turn,"total: "+Scores[Turn,number])
    Bowled=0
    if(Score<10){PT+=1}
    if(Score>=10&PT==1){PT=0}
    if(PT>=2){
    PT=0
    for(I=1,10){Pins[I,entity]:propDelete()}}
    timer("con",3000)

}
if(Bowled==1){

for(I=1,10){
    if(Pins[I,entity]:pos():distance(Ball:pos())<100){Pins[I,entity]:propFreeze(0)}
    if(Pins[I,entity]:pos():distance(Startpos[I,vector])>30&Moved[I,number]==0){
        Moved[I,number]=1
        Score+=1
    }   
}
}
if(changed(Score)&Bowled==1){timer("score",3000)}



##bowling

if(Start==1){
    holoCreate(1)
    holoModel(1,"models/props_phx/misc/smallcannonball.mdl")
    
    if(Bowled==0){
    holoPos(1,vec(Players[Turn,entity]:pos():x(),Lane:pos():y()+600,Lane:pos():z()+20))
    holoAng(1,ang(0,Players[Turn,entity]:eyeAngles():yaw(),0))
    
    if(changed(Players[Turn,entity]:keyUse())&Players[Turn,entity]:keyUse()==1){
        holoAlpha(1,0)
        Ball=propSpawn("models/props_phx/misc/smallcannonball.mdl",holoEntity(1):pos(),holoEntity(1):angles(),0)
        Ball:applyForce((Ball:forward())*35000)
        Bowled=1
        timer("0",5000)
    }
}
}


##reset
if(clk("con")){
    if(PT==1){
        holoAlpha(1,255)
    }
    if(PT==0){
        holoAlpha(1,255)
        Frame[Turn,number]=Frame[Turn,number]+1
        PT=0
        Turn+=1
        Bowled=0

        for(I=1,10){
        if(I==1){Pos=vec(5,375,38)}
        if(I==2){Pos=vec(20,425,38)}
        if(I==3){Pos=vec(-12,425,38)}
        if(I==4){Pos=vec(40,475,38)}
        if(I==5){Pos=vec(-32,475,38)}
        if(I==6){Pos=vec(60,525,38)}
        if(I==7){Pos=vec(-52,525,38)}
        if(I==8){Pos=vec(5,475,38)}
        if(I==9){Pos=vec(20,525,38)}
        if(I==10){Pos=vec(-12,525,38)}
        B=propSpawn("models/hunter/blocks/cube025x075x025.mdl",Lane:toWorld(Pos),Lane:toWorld(ang(0,0,90)),1)
        Pins[I,entity]=B
        Startpos[I,vector]=B:pos()
        Moved[I,number]=0
        Score=0
            }
        }
        
}
if(Turn>Players:count()){Turn=1}
