@name Chaqua Scatter Missiles
@persist [E O]:entity A Ub
@persist Max M:array Fp Fi Pr RsO Intrpl
interval(100)
if(first())
{
    Intrpl = 100/(500/33)
    
    Max = 100
    
    E = entity()
    O = owner()
    
    Ub = 1
    
    holoCreate(1,E:toWorld(vec()),vec(3))
    holoModel(1,"models/props_combine/combine_mortar01b.mdl")
    holoMaterial(1,"models/shiny")
    holoColor(1,vec(40))
    holoParent(1,E)
    
    holoCreate(2,E:toWorld(vec(0,0,165)),vec(3))
    holoModel(2,"hq_sphere")
    holoMaterial(2,"models/shiny")
    holoColor(2,vec(40))
    holoParent(2,E)
    
    holoCreate(3,holoEntity(2):toWorld(vec(8,0,0)),vec(3),E:toWorld(ang(-90,0,0)))
    holoModel(3,"models/Items/combine_rifle_ammo01.mdl")
    holoMaterial(3,"models/shiny")
    holoColor(3,vec(40))
    holoParent(3,2)
    
    holoCreate(4,holoEntity(2):toWorld(vec(72,0,0)),vec(3,3,12),E:toWorld(ang(90,0,0)))
    holoModel(4,"hq_tube_thin")
    holoMaterial(4,"models/shiny")
    holoColor(4,vec(40))
    holoParent(4,2)
}

holoAng(3,holoEntity(3):toWorld(ang(0,10,0)))

A = O:pos():distance(E:pos()) < 100 & !(O:weapon():type() == "gmod_tool" | O:weapon():type() == "weapon_physgun")
if(changed(A))
{
    soundPlay("pa",1,A ? "buttons/button18.wav" : "buttons/button19.wav")
    
    holoColor(3,(A ? vec(1,2,1) : vec(2))*20)
}
if(A)
{
    holoAng(2,(O:aimPos()-holoEntity(2):pos()):toAngle())
    
    if(Ub & O:keyAttack1() & !Fi)
    {
        local CL = M:count() < Max & propCanCreate()
        
        soundPlay("load",CL ? 0.2 : 0.057,CL ? "buttons/button24.wav" : "buttons/button16.wav")
        soundPitch("load",100 + (50*CL))
        
        if(CL)
        {
            Pr = 1
            
            #local TE = propSpawn("models/props_phx/amraam.mdl",holoEntity(2):pos(),1)
            local TE = propSpawn("models/props_junk/propane_tank001a.mdl",holoEntity(2):pos(),1)
            TE:propNotSolid(1)
            TE:setColor(0,0,0,0)
            
            M:pushEntity(TE)
            
            local TH = holoCreate(10+TE:id(), holoEntity(2):pos(), vec(0)) #Makes a holo to stop chain reaction explosions
            TH:setTrails(10,100,3,"trails/smoke",vec(255),255)
        }
        
        Ub = 0
        timer("ub", 300)
    }
    
    if(Pr) #Primes it so when you let go of M1 you fire
    {
        Fi = Fp = !O:keyAttack1()
        Pr = O:keyAttack1()
    }
    
    if(changed(O:keyReload())&O:keyReload()) #Resets if clearing bad missiles doesn't work
    {
        if(RsO)
        {
            M:propDelete()
            reset()
        }else
        {
            RsO = 1
            timer("rso",300)
        }
    }
}

if(clk("rso"))
{
    RsO = 0
}

if(clk("ub"))
{
    stoptimer("ub")
    Ub = 1
}

if(changed(Fi))
{
    holoColor(1,(Fi ? vec(2,1,1) : vec(2)) * 20)
}
if(Fi)
{
    if(changed(Fi))
    {
        soundPlay("sh",1.792,"weapons/sentry_rocket.wav")
    }
    
    if(M:count() > 0)
    {
        rangerFilter(M)
        foreach(I, TE:entity = M)
        {
            local TH = holoEntity(10+TE:id())
            
            local RD = rangerOffset(75 * Intrpl,TH:pos(),TH:forward())
            if(RD:hit())
            {
                TE:setPos(TH:pos())
                TE:propBreak()
                M:remove(I)
                holoDelete(10+TE:id())
            }else
            {
                if(Fp) #Sets up missiles to be in different directions
                {
                    holoPos(10+TE:id(),holoEntity(2):toWorld(vec(random(36,144),random(-15,15),random(-15,15))))
                    local R = 2
                    holoAng(10+TE:id(),holoEntity(2):toWorld(ang(random(-R,R),random(-R,R),random(-R,R))))
                    holoEntity(10+TE:id()):soundPlay(TE:id(),0,"weapons/rpg/rocket1.wav")
                    
                    Fp = !(I == M:count()) #Turns zero once all missiles have been set up, allowing them to move forward
                }else
                {
                    holoPos(10+TE:id(),TH:toWorld(vec(25 * Intrpl,0,0)))
                    #local R = 0.0625 * Intrpl
                    local R = 0.1 * Intrpl
                    holoAng(10+TE:id(),TH:toWorld(ang(random(-R,R),random(-R,R),random(-R,R))))
                }
            }
        }
    }else
    {
        Fi = 0
    }
}
