#@name cluster bomb 2
@inputs 
@outputs
@persist [BaseHit StartID BombCount] Base:entity BaseDir:vector BaseAng:angle Deployed:table
@trigger none

if (first()) {
    Base = holoCreate(1), holoModel(1, "cone"), holoScale(1, vec(0.5, 0.5, 0.5))
        holoMaterial(1, "models/items/battery01")

    local N = 300
    
    StartID = N
    BaseHit = -2
    BaseDir = entity():up()*100

    for (I = 0, 9) {
        N++
        local Catapult = holoCreate(N, Base:toWorld(vec(1, 0, -5):rotate(0, 36*I, 0)))
            holoAng(N, Base:toWorld(ang(90, 36*I, 180)))
            holoModel(N, "models/Items/combine_rifle_cartridge01.mdl")
            holoScale(N, vec(1, 1, 0.5))
            holoParent(N, Base)
        
        local NX = N + 10
        local Bomb = holoCreate(NX, Catapult:toWorld(vec(3, 1.35, 0.5)))
            holoAng(NX, Catapult:toWorld(ang(45, 12.5, 0)))
            holoModel(NX, "models/Combine_Helicopter/helicopter_bomb01.mdl")
            holoScale(NX, vec(0.075))
            holoDisableShading(NX, 1)
            holoParent(NX, Catapult)    
            
            Bomb:setTrails(4, 0, 0.25, "trails/physbeam", vec(200,200,255), 255)    
    }

}

interval(60)

if (BaseHit == -2) {
    BaseDir -= vec(0, 0, 6)

    holoPos(1, Base:pos() + BaseDir)
    holoAng(1, BaseDir:toAngle() + ang(90, 0, 0))
    
    local Ranger = rangerOffset(50 + BaseDir:length(), Base:pos(), BaseDir)
    if (Ranger:hit()) {
        holoPos(1, Ranger:position() + Ranger:hitNormal()*1)
        holoAng(1, Ranger:hitNormal():toAngle() - ang(90, 0, 0))
        
        Base:soundPlay("Stick", 0, "@^npc/ministrider/flechette_flesh_impact" + randint(1, 4) + ".wav")
        Base:soundPlay("Open", 0, "@^npc/turret_floor/retract.wav"), soundPitch("Open", 0)
        
        BaseAng = Base:angles()
        BaseDir = BaseAng:up()
    
        BaseHit = -1
    }

} elseif (BaseHit < 0) {
    BaseHit = min(BaseHit + ((BaseHit <= -0.5) ? 0.05 : 0.1), 0)
    
    if (BaseHit >= -0.5) {
        local Angle = abs(BaseHit * 2) * 90
        for (I = 0, 9) {
            local ID = StartID + I + 1
            holoAng(ID, Base:toWorld(ang(Angle, 36*I, 180))) 
        }
        
        soundPitch("Open", 100)
        if (BaseHit == 0) {
            soundStop("Stick")
            soundStop("Open")            
        }
    }
} elseif (BombCount < 10) {
    BaseHit = (BaseHit + 120) % 360
    if (changed(BaseHit) & BaseHit == 0) {
        BombCount++
        
        local BombID = BombCount + StartID + 10
        local Bomb = holoEntity(BombID), holoUnparent(BombID)
        
        Bomb:setTrails(4, 0, 0.25, "trails/physbeam", vec(255), 255)
        Bomb:soundPlay("Bomb" + BombID, 0, "@^npc/attack_helicopter/aheli_mine_drop1.wav", 0.587)

        Bomb["Dir", vector] = Bomb:up() * randint(22.5, 45) * 2

        Deployed[BombCount + "", entity] = Bomb
    }
    
    holoAng(1, BaseAng:rotateAroundAxis(BaseDir, BaseHit))
}

if (BombCount) {
    local Gravity = vec(0, 0, 6)

    foreach (K, V:entity = Deployed) {
        local Ranger = rangerOffset(50, V:pos(), V["Dir", vector])
        if (Ranger:hit()) {
            holoPos(holoIndex(V), Ranger:position() + Ranger:hitNormal())
            
            if (propCanCreate()) {
                holoAlpha(holoIndex(V), 0)
                holoModel(holoIndex(V), "models/bots/boss_bot/boss_tank_part1_destruction.mdl")

                local Prop = propSpawn("models/props_phx/misc/potato_launcher_explosive.mdl", Ranger:position(), 1)
                Prop:propBreak()
                Deployed:removeEntity(K)
            }
        } else {
            V["Dir", vector] = V["Dir", vector] - Gravity
            holoPos(holoIndex(V), V:pos() + V["Dir", vector])        
        }
    }    
}
