@name Drone Swarm
@persist [Target GoTo]:vector DrnCount I Attack [O E]:entity Drones:array N Charging
@model models/props_c17/clock01.mdl

interval(100)

if(first())
{
    runOnLast(1)
    
    E = entity()
    O = owner()
    
    DrnCount = 30
    
    rangerPersist(1)
    
    timer("spwn", 300)
}

if(Charging)
{
    if(clk("spwn"))
    {
        if(propCanCreate())
        {
            local TE = propSpawn("models/props_borealis/bluebarrel001.mdl", E:pos() + vec(0, 0, 50), 0)
            TE:setMass(50000)
            TE:setColor(0, 0, 0, 0)
            Drones:pushEntity(TE)
            
            holoCreate(TE:id(), TE:pos(), vec(0.5), TE:angles())
            holoParent(TE:id(), TE)
            holoModel(TE:id(), "models/perftest/gman.mdl")
        }
        
        stoptimer("spwn")
        if(Drones:count() < DrnCount)
        {
            timer("spwn", 300)
        }else
        {
            Charging = 0
        }
    }
}else
{
    if(Drones:count() < DrnCount)
    {
        timer("spwn", 300)
        Charging = 1
    }
}

if(changed(Drones:count()))
{
    rangerFilter(Drones)
}

Attack = O:keyUse()
Target = Attack ? O:eyeTrace():pos() + vec(0, 0, 35) : O:shootPos() + vec(0, 0, 300-(200*O:keyDuck()))

foreach(I, Drone:entity = Drones)
{
    local TT = I == 1 ? Target : Drones[I - 1,entity]:pos()
    if(Attack)
    {
        #Cluster Mode
        if(I > 6)
        {
            local TE = Drones[1 + (I % 6), entity]
            if(!inrange(Drone:pos():distance(TE:pos()), 200, 350))
            {
                Drone:applyForce(((TE:pos() - Drone:pos()):normalized()*(1025) - Drone:vel()) * Drone:mass())
            }
        }else
        {
            Drone:applyForce((((TT + ((TT - Drone:pos()):normalized():rotate(ang(0, 90, 0)) * (14 * DrnCount) * (!Attack & I == 1))) - Drone:pos()):normalized()*(1025) - Drone:vel()) * Drone:mass())
        }
    }else
    {
        #Snake Mode
        Drone:applyForce((((TT + ((TT - Drone:pos()):normalized():rotate(ang(0, 90, 0)) * (14 * DrnCount) * (!Attack & I == 1))) - Drone:pos()):normalized()*(1025) - Drone:vel()) * Drone:mass())
    }
    
    Drone:applyAngForce(((-Drone:angles())*2 - Drone:angVel()*1.125) * Drone:mass())
    holoAng(Drone:id(), (TT - Drone:pos()):toAngle())
}

N++
if(!(N % 2))
{
    for(I = 1, Drones:count())
    {
        if(!Drones[I,entity]:isValid())
        {
            Drones:remove(I)
        }
    }
}

if(last())
{
    Drones:propDelete()
}

#print(4, toString(ops()))
