@name Swarm
@persist [P T C]:array
@model models/hunter/blocks/cube05x05x025.mdl
interval(50)
if(first())
{
    entity():setMaterial("phoenix_storms/stripes")
    function turtle()
    {
        propSpawnEffect(0)
        TE = propSpawn("models/props/de_tides/vending_turtle.mdl",entity():toWorld(vec(0,0,10)),entity():toWorld(ang(random(0,-10),random(1,360),0)),0)
        P:pushEntity(TE)
        T:pushNumber(random(20,50))
        C:pushNumber(round(random(2,15)))
        TE:applyForce(-TE:right()*200*TE:mass()+TE:up()*50*TE:mass())
        TE:applyAngForce(ang(1,random(-25,25),0))
    }
    timer("a",random(250,500))
    turtle()
    
    runOnChat(1)
}

if(chatClk(owner()))
{
    if(owner():lastSaid():lower()=="/clear")
    {
        hideChat(1)
        foreach(I, V:entity = P)
        {
            V:propDelete()
        }
        selfDestruct()
    }
}

if(clk("a"))
{
    stoptimer("a")
    timer("a",random(250,500))
    turtle()
}

print(4, toString(P:count()))

if(P:count()>0)
{
    for(I=1,P:count())
    {
        TE = P[I,entity]
        TT = T[I,number]
        TC = C[I,number]
        if(TT<1)
        {
            TE:applyForce(-TE:right()*random(200,300)*TE:mass()+TE:up()*random(50,150)*TE:mass())
            TE:applyAngForce(ang(1,random(-25,25),0))
            T[I,number] = random(20,50)
            C[I,number] = C[I,number]-1
            TE:soundPlay("Jump"+I,1.2,"garrysmod/balloon_pop_cute.wav")
        }
        if(TC<1||!inrange(TE:angles():pitch(),-85,85)||!inrange(TE:angles():roll(),-85,95)||!TE:isValid())
        {
            TE:propDelete()
            P:remove(I)
            T:remove(I)
            C:remove(I)
        }
    T[I,number] = T[I,number]-1
    }
}
