@name Rocket
@inputs Launch Rdy
@outputs F
@persist [E BP B St3E St5E St6E]:entity
@persist V St A BL
@model models/sprops/geometry/fdisc_36.mdl
if(first())
{
    runOnTick(1)
    
    E = entity()
    E:setColor(0,0,0,0)
    E:setMass(5000)
    
    holoCreate(17,E:toWorld(vec(0,15,0)))
    holoCreate(1001,holoEntity(17):toWorld(vec(10,0,10)))
    holoCreate(1002,holoEntity(17):toWorld(vec(10,0,-10)))
    holoCreate(1003,holoEntity(17):toWorld(vec(-10,0,10)))
    holoCreate(1004,holoEntity(17):toWorld(vec(-10,0,-10)))
    holoCreate(1101,holoEntity(1001):toWorld(vec(0,-15,0)))
    holoCreate(1102,holoEntity(1002):toWorld(vec(0,-15,0)))
    holoCreate(1103,holoEntity(1003):toWorld(vec(0,-15,0)))
    holoCreate(1104,holoEntity(1004):toWorld(vec(0,-15,0)))
    holoCreate(18,holoEntity(17):toWorld(vec(0,60,0))) #Booster tube
    holoCreate(19,holoEntity(18):toWorld(vec(0,75,0))) #Booster-seperator; engine cap
    holoCreate(20,holoEntity(19):toWorld(vec(0,-33,0)))
    holoCreate(15,holoEntity(17):toWorld(vec(0,116,0)))
    holoCreate(40,holoEntity(15):toWorld(vec()))
    holoCreate(16,holoEntity(15):toWorld(vec(0,52,0)))
    holoCreate(1,holoEntity(15):toWorld(vec(0,121,0)))
    holoCreate(2,holoEntity(1):toWorld(vec(0,66,0)))
    holoCreate(11,holoEntity(1):toWorld(vec(0,0,25)))
    holoCreate(12,holoEntity(1):toWorld(vec(0,0,-25)))
    holoCreate(13,holoEntity(1):toWorld(vec(25,0,0)))
    holoCreate(14,holoEntity(1):toWorld(vec(-25,0,0)))
    BP = holoCreate(21,holoEntity(2):toWorld(vec(0,-60,0)))
    holoCreate(31,holoEntity(1):toWorld(vec(0,-67.5,0)))

    holoModel(17,"models/sprops/geometry/fdisc_48.mdl")
    holoModel(1001,"models/sprops/misc/tubes_thin/size_1/t_tube_12x30.mdl")
    holoModel(1002,"models/sprops/misc/tubes_thin/size_1/t_tube_12x30.mdl")
    holoModel(1003,"models/sprops/misc/tubes_thin/size_1/t_tube_12x30.mdl")
    holoModel(1004,"models/sprops/misc/tubes_thin/size_1/t_tube_12x30.mdl")
    holoModel(18,"models/sprops/misc/tubes/size_4/tube_48x120.mdl")
    holoModel(19,"models/sprops/misc/tubes/size_4/tube_48x66.mdl")
    holoModel(20,"models/sprops/geometry/fdisc_48.mdl")
    holoModel(15,"models/sprops/misc/domes/size_3/dome_36x60.mdl")
    holoModel(16,"models/sprops/misc/domes/size_4/dome_48x24.mdl")
    holoModel(1,"models/sprops/misc/tubes/size_4/tube_48x132.mdl")
    holoModel(2,"models/sprops/misc/domes/size_4/dome_48x60.mdl")
    holoModel(11,"models/sprops/triangles/right/size_4/rtri_48x72.mdl")
    holoModel(12,"models/sprops/triangles/right/size_4/rtri_48x72.mdl")
    holoModel(13,"models/sprops/triangles/right/size_4/rtri_48x72.mdl")
    holoModel(14,"models/sprops/triangles/right/size_4/rtri_48x72.mdl")
    holoModel(21,"models/sprops/misc/domes/size_1/dome_12x48.mdl")
    holoModel(31,"models/sprops/geometry/fdisc_48.mdl")
    
    holoAng(15,E:toWorld(ang(0,0,-90)))
    holoAng(16,E:toWorld(ang(0,0,90)))
    holoAng(2,holoEntity(1):toWorld(ang(0,0,-90)))
    holoAng(11,holoEntity(1):toWorld(ang(0,90,0)))
    holoAng(12,holoEntity(1):toWorld(ang(180,-90,0)))
    holoAng(13,holoEntity(1):toWorld(ang(0,90,90)))
    holoAng(14,holoEntity(1):toWorld(ang(180,-90,90)))
    holoAng(21,E:toWorld(ang(0,0,-90)))
    
    holoMaterial(1001,"phoenix_storms/metalset_1-2")
    holoMaterial(1002,"phoenix_storms/metalset_1-2")
    holoMaterial(1003,"phoenix_storms/metalset_1-2")
    holoMaterial(1004,"phoenix_storms/metalset_1-2")
    holoMaterial(15,"phoenix_storms/metalset_1-2")
    holoMaterial(17,"phoenix_storms/side")
    holoMaterial(18,"phoenix_storms/side")
    holoMaterial(19,"phoenix_storms/side")
    holoMaterial(20,"phoenix_storms/side")
    holoMaterial(15,"phoenix_storms/metalset_1-2")
    holoMaterial(16,"phoenix_storms/metalset_1-2")
    holoMaterial(1,"phoenix_storms/side")
    holoMaterial(2,"phoenix_storms/top")
    holoMaterial(11,"phoenix_storms/top")
    holoMaterial(12,"phoenix_storms/top")
    holoMaterial(13,"phoenix_storms/top")
    holoMaterial(14,"phoenix_storms/top")
    holoMaterial(21,"phoenix_storms/bomb")
    holoMaterial(31,"phoenix_storms/side")
    
    holoColor(40,vec4())
    holoColor(1101,vec4())
    holoColor(1102,vec4())
    holoColor(1103,vec4())
    holoColor(1104,vec4())
    
    holoAlpha(21,150)
    
    holoParent(17,E)
    holoParent(1001,17)
    holoParent(1002,17)
    holoParent(1003,17)
    holoParent(1004,17)
    holoParent(1101,1001)
    holoParent(1102,1002)
    holoParent(1103,1003)
    holoParent(1104,1004)
    holoParent(18,17)
    holoParent(19,15)
    holoParent(20,19)
    holoParent(15,E)
    holoParent(16,15)
    holoParent(1,15)
    holoParent(2,1)
    holoParent(11,1)
    holoParent(12,1)
    holoParent(13,1)
    holoParent(14,1)
    holoParent(21,1)
    holoParent(31,1)
    holoParent(40,15)
}

if(!BL)
{
    findByClass("gmod_wire_expression2")
    findByModel("models/sprops/misc/domes/size_1/dome_12x48.mdl")
    findSortByDistance(E:pos())
    if(find():pos():distance(E:pos()) < 300 & find():getMaterial() == "phoenix_storms/bomb")
    {
        B = find()
        B:createWire(E, "Detonate", "F")
        E:createWire(B, "Rdy", "Rdy")
        B:setPos(BP:pos())
        B:setAng(BP:angles())
        timer("bl", 100)
    }
    if(clk("bl"))
    {
        stoptimer("bl")
        B:parentTo(BP)
        BL = 1
    }
}

if(Launch)
{
    A = 1
    St = 1
}

if(A)
{
    V += 0.05
    switch(St)
    {
        case 1,
            if(changed(St))
            {
                holoEntity(1101):setTrails(12, 400, 8, "trails/smoke", vec(200), 255)
                holoEntity(1102):setTrails(12, 400, 8, "trails/smoke", vec(200), 255)
                holoEntity(1103):setTrails(12, 400, 8, "trails/smoke", vec(200), 255)
                holoEntity(1104):setTrails(12, 400, 8, "trails/smoke", vec(200), 255)
            }
            E:setPos(E:toWorld(vec(0, V, 0)))
            E:setAng(E:toWorld(ang(0, 0, 0.02)))
            St += E:pos():z() > 1500
        break
        
        case 2,
            if(changed(St))
            {
                holoUnparent(1101)
                holoUnparent(1102)
                holoUnparent(1103)
                holoUnparent(1104)
                holoUnparent(15)
                E:propFreeze(0)
                timer("st2",5000)
                
            }
            holoPos(15, holoEntity(15):toWorld(vec(0, 0, V)))
            holoAng(15, holoEntity(15):toWorld(ang(0, 0, 0.02)))
            St += holoEntity(15):pos():z() > 3000
        break
        
        case 3,
            if(changed(St))
            {
                St3E = propSpawn("models/sprops/geometry/fdisc_48.mdl", holoEntity(19):toWorld(vec(0, -33, 0)), holoEntity(19):angles(), 0)
                holoUnparent(19)
                holoParent(19, St3E)
                timer("st3", 10000)
                holoEntity(40):setTrails(36, 700, 6, "trails/smoke", vec(200), 255)
            }
            holoPos(15, holoEntity(15):toWorld(vec(0, 0, V)))
            holoAng(15, holoEntity(15):toWorld(ang(0, 0, 0.5)))
            St += holoEntity(15):angles():roll() >= 60
        break
        
        case 4,
            if(changed(St))
            {
                holoUnparent(40)
            }
            holoPos(15, holoEntity(15):toWorld(vec(0, 0, V)))
            holoAng(15, holoEntity(15):toWorld(ang(0, 0, 0.005)))
            timer("st4",1000)
            St += clk("st4")
        break
        
        case 5,
            if(changed(St))
            {
                stoptimer("st4")
                St5E = propSpawn("models/sprops/geometry/fdisc_48.mdl", holoEntity(15):toWorld(vec()), holoEntity(15):toWorld(ang(90,0,0)), 0)
                St5E:setColor(0, 0, 0, 0)
                holoUnparent(15)
                holoParent(15, St5E)
                holoUnparent(1)
                timer("st5",10000)
            }
            holoPos(1, holoEntity(1):toWorld(vec(0, V, 0)))
            holoAng(1, holoEntity(1):toWorld(ang(0, 0, 0.25)))
            St += holoEntity(1):angles():roll() >= 175
        break
        
        case 6,
            if(changed(St))
            {
                St6E = propSpawn("models/sprops/geometry/fdisc_48.mdl", holoEntity(2):toWorld(vec()), holoEntity(2):toWorld(ang()), 0)
                St6E:setColor(0, 0, 0, 0)
                holoUnparent(2)
                holoParent(2, St6E)
                St6E:applyForce(St6E:up()*V*20*St6E:mass())
                St6E:applyAngForce(ang(0,0,20)*St6E:mass())
                timer("st6e",10000)
                St++
            }
        break
        
        case 7,
            if(changed(St))
            {
                holoUnparent(21)
                BP:setTrails(12,0,3,"trails/smoke",vec(200),175)
                V = 50
            }
            local R = 5*(BP:angles():roll() < 90 | BP:angles():pitch() < 90)
            holoPos(1,holoEntity(1):toWorld(vec(0,40,0)))
            holoAng(1,holoEntity(1):toWorld(ang(0,0,0.005)))
            holoPos(21,holoEntity(21):toWorld(vec(0,0,V)))
            holoAng(21,holoEntity(21):toWorld(ang(random(-0.05,0.05),0,R+random(-0.05,0.05))))
            rangerFilter(array(BP,B,St3E,St5E,St6E))
            local Rd = rangerOffset(125,B:pos(),B:up())
            if(Rd:hit())
            {
                St++
            }
        break
        
        case 8,
            if(changed(St))
            {
                F = 1
            }
            if(!B:isValid())
            {
                selfDestruct()
            }
        break
    }
    if(clk("st2"))
    {
        stoptimer("st2")
        holoDelete(17)
        holoDelete(1001)
        holoDelete(1002)
        holoDelete(1003)
        holoDelete(1004)
        holoDelete(1101)
        holoDelete(1102)
        holoDelete(1103)
        holoDelete(1104)
        holoDelete(18)
        E:setColor(0,0,0,0)
        E:propFreeze(1)
        E:propNotSolid(1)
        propSpawn("models/props_phx/amraam.mdl", E:pos(), 1):propBreak()
    }
    if(clk("st3"))
    {
        stoptimer("st3")
        propSpawn("models/props_phx/amraam.mdl", St3E:pos(), 1):propBreak()
        holoDelete(19)
        St3E:propDelete()
    }
    if(clk("st5"))
    {
        stoptimer("st5")
        propSpawn("models/props_phx/amraam.mdl", St5E:pos(), 1):propBreak()
        holoDelete(15)
        St5E:propDelete()
    }
    if(clk("st6e"))
    {
        stoptimer("st6e")
        propSpawn("models/props_phx/amraam.mdl", St6E:pos(), 1):propBreak()
        holoDelete(2)
        St6E:propDelete()
    }
}

