@name FalingBricksGame
@outputs [Me E Floor Winner]:entity Size:vector2 [Poses Sounds Bricks]:array I PlyList:table
@outputs Time Mode PlyCount Printed
if(first())
{
    #runOnTick(1)
    runOnChat(1)
    
    E = entity()
    Me = owner()
    Size = vec2(3,3)  
    I=1
    for(X=-Size:x(), Size:x())
    {
        for(Y=-Size:y(), Size:y())
        {
            Poses[I,vector] = vec(X,Y,10)
            I++
        }
    }
    I =1
    
    holoCreate(1,E:toWorld(vec(0,0,500)))
        holoParent(1,E)
        holoModel(1,"plane")
        holoColor(1,vec(255,0,0))
        holoAlpha(1,150)
        holoScaleUnits(1,vec(1000,1000,1))
        Floor = holoEntity(1)
        
    concmd("say [BrickGame]: Please wait while the gameboard generates!")
    Sounds = array("05.mp3", "ravebreak.mp3", "frantic_factory_remix.mp3")
}
if(Bricks:count() < Poses:count())
{
    interval(260)
    P = propSpawn("models/hunter/blocks/cube2x2x05.mdl",E:toWorld(Poses[I,vector]*100),ang(), 1)
    P:setColor(hsv2rgb((360/9)*round(Poses[I,vector]:distance(vec(0,0,10))),1,1))
    P:setMaterial("models/debug/debugwhite")
    P:propGravity(0)
    P:setMass(50000/(round(Poses[I,vector]:distance(vec(0,0,10)))*1.3))
    Bricks:pushEntity(P)
    I++
    print(4,"PLEASE WAIT WHILE THE GAMEBOARD SPAWNS!")
    
}else
{
    if(!Printed)
    {
        hint("Gameboard is Ready, pleayers may join!", 5)
        concmd("say [BrickGame]: Gameboard is Ready, pleayers may join! (type \"/join\" to join)")
        Printed = 1
    }
   if(chatClk()&lastSaid():sub(0,1) == "/")
    {
        LastSaid = lastSaid():sub(2):lower():explode(" ")
        if(lastSpoke() == Me)
        {
            if(LastSaid[1, string]=="del")
            {
                concmd("say [BrickGame]: Game Removed.")
                foreach(I, Ent:entity = Bricks)
                {
                    Ent:propDelete()
                }
                selfDestruct()
            }
            if(LastSaid[1, string]=="go")
            {
                timer("tick",100)
                Time = 5
                concmd("say [BrickGame]: Round starting in 5 seconds!")
            }
            if(LastSaid[1, string]=="stop")
            {
                if(Time)
                {
                    concmd("say [BrickGame]: Round countdown canceld!")
                    Time = 0
                    stoptimer("tick")
                }elseif(Mode)
                {
                    PlyString = ""
                    foreach(I, Ply:entity = PlyList)
                    {
                        PlyString += Ply:name() + ", "
                    }
                    concmd("say [BrickGame]: Round Terminated. Here are the winner(s): "+PlyString)
                }
            }
            if(LastSaid[1, string]=="reset")
            {
                concmd("say [BrickGame]: Bricks reset.")
                foreach(I, Ent:entity = Bricks)
                {
                    Ent:propFreeze(1)
                    Ent:setPos(E:toWorld(Poses[I,vector]*100))
                    Ent:setMass(50000/(round(Poses[I,vector]:distance(vec(0,0,10)))*1.3))
                    Ent:setColor(hsv2rgb((360/9)*round(Poses[I,vector]:distance(vec(0,0,10))),1,1))
                    Ent:setAng(ang())
                }
                if(Mode)
                {
                    PlyString = ""
                    foreach(I, Ply:entity = PlyList)
                    {
                        PlyString += Ply:name() + ", "
                    }
                    concmd("say [BrickGame]: Round Terminated. Here are the winner(s): "+PlyString)
                }
                Mode = 0
            }
        }
        if(LastSaid[1, string] == "join")
        {
            if(!Mode | Time)
            {
                if(PlyList[lastSpoke():steamID(),entity] == noentity())
                {
                    PlyList[lastSpoke():steamID(),entity] = lastSpoke()
                    concmd("say [BrickGame]: "+lastSpoke():name()+" joined the game!")
                    PlyCount++
                }else
                {
                    concmd("say [BrickGame]: "+lastSpoke():name()+" You're already in the game!")
                }
            }else
            {
                concmd("say [BrickGame]: "+lastSpoke():name()+" You can't join mid-game!")
            }
        }
        if(LastSaid[1, string] == "leave")
        {
            if(PlyList[lastSpoke():steamID(),entity] == noentity())
            {
                concmd("say [BrickGame]: "+lastSpoke():name()+" You aren't in the game!")
            }else
            {
                PlyList:remove(lastSpoke():steamID())
                concmd("say [BrickGame]: "+lastSpoke():name()+" left the game!")
                PlyCount--
            }
        }
        
    }
    if(clk("tick"))
    {
        if(Time)
        {
            concmd("say [BrickGame]: Round starting in "+Time+" seconds!")
            Time--
            timer("tick",1000)
        }else
        {
            concmd("say [BrickGame]: Round Started!")
            Sound = Sounds[round(random(1,Sounds:count())),string]
            foreach(I, Ent:entity = Bricks)
            {
                Ent:propFreeze(0)
                Ent:soundPlay(Ent:id(), 0, Sound)
            }
            Mode = 1
        }
    }
    interval(10)
    if(Mode)
    {
        if(PlyCount > 1)
        {
            foreach(I, Ply:entity = PlyList)
            {
                if(Ply != noentity())
                {
                    if(Ply:pos():z() < Floor:pos():z() | Ply:inNoclip() | !Ply:isAlive())
                    {
                        PlyList:remove(Ply:steamID())
                        concmd("say [BrickGame]: "+Ply:name()+" Lost!")
                        PlyCount--
                    }
                }
            }
        }else
        {
            Mode = 0
            foreach(I, Ent:entity = Bricks)
            {
                Ent:propFreeze(1)
                soundStop(Ent:id())
            }
            Winner = noentity()
            foreach(I, Ply:entity = PlyList)
            {
                Winner = Ply
                if(Winner)
                {
                    break
                }
            }
            timer("say",1000)
        }       
    }
    if(clk("say"))
    {
        concmd("say [BrickGame]: "+Winner:name()+" Is the winner! Congradulations!")
         PlyList:remove(Winner:steamID())
        PLyList = table()
        PlyCount = 0
    }
}
