@name Slot Machine
@inputs Money Button
@outputs Ang2:angle FadeWin FadeMoney
@persist [Models Ang]:array Index I [E BaseHolo]:entity Spin S1 S2 S3 A1 A2 A3 End
@persist AlwaysWin

if(first() | dupefinished())
{
    runOnChat(1)
    Models = array("models/props_lab/huladoll.mdl",
                    "models/props_junk/PopCan01a.mdl",
                    "models/props_junk/watermelon01.mdl",
                    "models/Gibs/HGIBS.mdl",
                    "models/props_c17/doll01.mdl")
                    
    Ang = array(68,140,212,284,356)
    
    E = entity():isWeldedTo()
    Index = 4
    I = 1
    Spin = 0
    E2= entity()
    E2:setAlpha(0)
    
    AlwaysWin = 0 # debug
                  
    holoCreate(1, E:toWorld(vec(-15,-10,20)))
    holoScaleUnits(1, vec(20,20,15))
    holoModel(1, "hqcylinder")
    holoColor(1, vec(0,0,0))
    holoAng(1, E:toWorld(ang(90,0,0)))
    holoParent(1, E)
    
    holoCreate(2, E:toWorld(vec(0,-10,20)))
    holoScaleUnits(2, vec(20,20,15))
    holoModel(2, "hqcylinder")
    holoColor(2, vec(0,0,0))
    holoAng(2, E:toWorld(ang(90,0,0)))
    holoParent(2, E)
    
    holoCreate(3, E:toWorld(vec(15,-10,20)))
    holoScaleUnits(3, vec(20,20,15))
    holoModel(3, "hqcylinder")
    holoColor(3, vec(0,0,0))
    holoAng(3, E:toWorld(ang(90,0,0)))
    holoParent(3, E)
}

if(Index < Models:count()*3+4)
{
    interval(10)
    if(holoCanCreate())
    {
        MdlCount = Models:count()
        if(inrange(Index, 4, 8)) {BaseHolo = holoEntity(1)}
        elseif(inrange(Index, 9, 13)) {BaseHolo = holoEntity(2)}
        else {BaseHolo = holoEntity(3)}
        
        holoCreate(Index, BaseHolo:toWorld(vec(cos((360/MdlCount)*I),sin((360/MdlCount)*I),0)*10))
        holoModel(Index, Models[I,string])
        holoAng(Index, BaseHolo:toWorld(ang(0,(360/MdlCount)*I,90)))
        holoParent(Index, BaseHolo)
        if(I == 3) {holoScaleUnits(Index, vec(10,10,10))}
        
        Index++
        I++
        I = I > 5 ? 1 : I
    }
}


if(chatClk()){
Cha = lastSaid():explode(" ")

if(Cha[1,string] == "-spin"){
Money = 60
Button = 1    
}
    
}

if((Money >= 50 | Button & ~Button) & !Spin)
{
    Money = 0
    FadeMoney = 1
    timer("Money",800)
    soundPlay(1,0,"buttons/blip1.wav")
    S1 = S2 = S3 = End = 0
    
    timer("S1",randint(1,2)*100)
    timer("S2",random(1,2)*1000)
    timer("S3",random(2,3)*1000)
    
    timer("Stop1",random(5,7)*1000)
    timer("Stop2",random(8,10)*1000)
    timer("Stop3",random(11,13)*1000)

    Spin = 1
}


if(clk("Money")){
FadeMoney = 0    
}

if(Spin)
{
    interval(50)
    if(clk("S1")) {S1=400}
    if(clk("S2")) {S2=400}
    if(clk("S3")) {S3=400}
    
    if(clk("Stop1")) {S1=Ang[randint(1,5),number] E:soundPlay(1,0,"buttons/lightswitch2.wav")}
    if(clk("Stop2")) {S2=Ang[randint(1,5),number] E:soundPlay(2,0,"buttons/lightswitch2.wav")}
    if(clk("Stop3")) {S3=Ang[randint(1,5),number] End = 1 E:soundPlay(2,0,"buttons/lightswitch2.wav")}
    
    if(clk())
    {
    if(!inrange(A1,S1-7,S1+7) & S1) 
    {
        A1 += 15
        if(A1 > 360) {A1 -= 360}
        holoAng(1, holoEntity(1):toWorld(ang(0,15,0)))
    }
    
    if(!inrange(A2,S2-7,S2+7) & S2) 
    {
        A2 += 15
        if(A2 > 360) {A2 -= 360}
        holoAng(2, holoEntity(2):toWorld(ang(0,15,0)))
    }
    
    if(!inrange(A3,S3-7,S3+7) & S3) 
    {
        A3 += 15
        if(A3 > 360) {A3 -= 360}
        holoAng(3, holoEntity(3):toWorld(ang(0,15,0)))
    }
    else
    {
        if(End)
        {
            soundPlay(1,0,"buttons/bell1.wav")
            if(AlwaysWin == 0){
            if((S1 == S2) & (S2 == S3)) {Win = 1} else {Win = 0}
        }else{
     Win = 1   
    }
            Spin = 0
        }
    }
    }
}

if(Win){
Win = 0
FadeWin = 1
E:soundPlay(1,0,"ambient/alarms/city_firebell_loop1.wav")
soundVolume(1,0.7)

E:soundPlay(2,0,"misc/achievement_earned.wav")

timer("Unfree",5000)    
}


if(clk("Unfree")){
soundStop(1)
FadeWin = 0    
}
