@name Energy Launcher
@inputs 
@outputs Missile:entity Refire Reload For Fuel Speed Apply O:entity Fire Miss:array
@persist Charging Charge R ChargeRot Rot Cool OverCharge
@trigger 

runOnTick(1)
runOnLast(1)
rangerPersist(1)
entity():propNotSolid(0)
O=owner()
if (first()|dupefinished())
{

    holoCreate(4)
    holoCreate(50)
    holoCreate(51)
    holoCreate(52)
    holoCreate(53)
    holoCreate(54)
    
    holoCreate(55)
    holoCreate(56)
    holoCreate(57)
    holoCreate(58)
    holoModel(4,"models/weapons/c_models/c_drg_cowmangler/c_drg_cowmangler.mdl")
    holoPos(4,O:attachmentPos("anim_attachment_RH")+vec(0,0,2.8))
    holoAng(4,O:attachmentAng("anim_attachment_RH")+ang(-17,0,0))
    holoParentAttachment(4,O,"anim_attachment_RH")
    
    #rotator thing
    holoModel(51,"models/Items/combine_rifle_ammo01.mdl")
    holoModel(52,"models/Items/combine_rifle_ammo01.mdl")
    holoModel(53,"models/Items/combine_rifle_ammo01.mdl")
    holoModel(54,"models/Items/combine_rifle_ammo01.mdl")
    
    holoPos(50,holoEntity(4):toWorld(vec(21,0,5)))
    holoAng(50,holoEntity(4):angles())
    
    holoPos(51,holoEntity(4):toWorld(vec(18,-5,5)))
    holoPos(52,holoEntity(4):toWorld(vec(18,5,5)))
    holoPos(53,holoEntity(4):toWorld(vec(18,0,0)))
    holoPos(54,holoEntity(4):toWorld(vec(18,0,10)))
    
    holoAng(51,holoEntity(4):toWorld(ang(90,0,0)))
    holoAng(52,holoEntity(4):toWorld(ang(90,0,0)))
    holoAng(53,holoEntity(4):toWorld(ang(90,0,0)))
    holoAng(54,holoEntity(4):toWorld(ang(90,0,0)))
    
    holoParent(50,4)
    
    holoParent(51,50)
    holoParent(52,50)
    holoParent(53,50)
    holoParent(54,50)
    
    holoScale(50,vec(0.7,0.7,0.7))
    holoScale(51,vec(1.3,0.7,1.3))
    holoScale(52,vec(1.3,0.7,1.3))
    holoScale(53,vec(1.3,0.7,1.3))
    holoScale(54,vec(1.3,0.7,1.3))
    
    #charge indicator
    holoPos(55,holoEntity(4):toWorld(vec(10,6,2)))
    holoAng(55,holoEntity(4):toWorld(ang(5,0,0)))
    
    holoPos(56,holoEntity(55):toWorld(vec(-0.2,2.5,0)))
    holoPos(57,holoEntity(55):toWorld(vec(-0.2,1.5,0)))
    holoPos(58,holoEntity(55):toWorld(vec(-0.2,0.5,0)))
    
    holoAng(56,holoEntity(55):angles())
    holoAng(57,holoEntity(55):angles())
    holoAng(58,holoEntity(55):angles())
    
    holoParent(55,4)
    holoParent(56,55)
    holoParent(57,55)
    holoParent(58,55)
    holoScale(55,vec(0.1,0.6,0.3))
    holoScale(56,vec(0.05,0.07,0.1))
    holoScale(57,vec(0.05,0.07,0.175))
    holoScale(58,vec(0.05,0.07,0.25))
    
    holoColor(55,vec(100,100,100),150)
    holoColor(56,vec(0,0,0))
    holoColor(57,vec(0,0,0))  
    holoColor(58,vec(0,0,0)) 
    
    entity():setAlpha(255)
    Refire=1
    Reload=1
        function entity:turnTo(Tarang:angle){
V = This:toLocal(rotationVector(quat(Tarang)/quat(This))+This:pos())
This:applyTorque((125*V - 15*This:angVelVector())*This:inertia()/6) 
}
}
if (changed(Refire)&Refire==0){R=int(random(1,4))
    if(Charge==1){ChargeRot=11.8
        if(R==1){owner():soundPlay(3,1.5,"weapons/titan_sniper/wpn_titansniper_3p_wpnfire_low_energy_2ch_v1_01.wav")}
        if(R==2){owner():soundPlay(3,1.5,"weapons/titan_sniper/wpn_titansniper_3p_wpnfire_low_energy_2ch_v1_02.wav")}
        if(R==3){owner():soundPlay(3,1.5,"weapons/titan_sniper/wpn_titansniper_3p_wpnfire_low_energy_2ch_v1_03.wav")}
    }
    if(Charge==2){
        if(R==1){owner():soundPlay(3,2,"weapons/titan_sniper/stereo/wpn_titansniper_1p_wpnfire_level3_reduced_6ch_01.wav")
            owner():soundPlay(5,3,"weapons/titan_sniper/wpn_titansniper_3p_wpnfire_med_ballistic_2ch_v1_01.wav")}
        if(R==2){owner():soundPlay(3,2,"weapons/titan_sniper/stereo/wpn_titansniper_1p_wpnfire_level3_reduced_6ch_02.wav")
            owner():soundPlay(5,3,"weapons/titan_sniper/wpn_titansniper_3p_wpnfire_med_ballistic_2ch_v1_01.wav")}
        if(R==3){owner():soundPlay(3,2,"weapons/titan_sniper/stereo/wpn_titansniper_1p_wpnfire_level3_reduced_6ch_04.wav")
            owner():soundPlay(5,3,"weapons/titan_sniper/wpn_titansniper_3p_wpnfire_med_ballistic_2ch_v1_01.wav")}
    }
    if(Charge==3){
        if(R==1){owner():soundPlay(3,3,"weapons/titan_sniper/stereo/wpn_titansniper_1p_wpnfire_level4_reduced_6ch_01.wav")}
        if(R==2){owner():soundPlay(3,3,"weapons/titan_sniper/stereo/wpn_titansniper_1p_wpnfire_level4_reduced_6ch_03.wav")}
        if(R==3){owner():soundPlay(3,3,"weapons/titan_sniper/stereo/wpn_titansniper_1p_wpnfire_level4_reduced_6ch_04.wav")}
    }
    holoColor(56,vec(0,0,0))
    holoColor(57,vec(0,0,0))  
    holoColor(58,vec(0,0,0)) 
    soundPitch(3,100)}

if (Fire==1&Refire==1){Refire=0
    Charging=0
    Fire=0
    
    if(Charge==1){Missile=propSpawn("models/props_phx/ww2bomb.mdl",holoEntity(4):pos(),0)}
    if(Charge==2){Missile=propSpawn("models/props_phx/amraam.mdl",holoEntity(4):pos(),0)}
    if(Charge==3){Missile=propSpawn("models/props_phx/mk-82.mdl",holoEntity(4):pos(),0)}
    Missile:setAng(owner():eyeAngles())
    Missile:setAlpha(0)
    Missile:propGravity(0)
    Missile:propNotSolid(1)
    Missile:setTrails(50,0,1,"trails/physbeam",vec(255,255,255),200)
    holoCreate(3)
    holoModel(3,"hq_sphere")
    holoMaterial(3,"models/alyx/emptool_glow")
    holoScale(3,vec(1,1,1))
    holoAlpha(3,0)
    holoColor(3,vec(200,200,255))
    holoPos(3,Missile:pos())
    holoAng(3,Missile:angles())
    holoParent(3,Missile)
    rangerFilter(Missile)
    rangerFilter(owner())
    holoAlpha(2,0)
    Missile:setAng(owner():eyeAngles())
    Miss:pushEntity(Missile)
    Missile:propNotSolid(1)
    #particle("striderbuster_attached_pulse_solid",Missile)
    #particle("finish",Missile)
    #particle("striderbuster_trail",Missile)
timer("un",400)
timer("poof",50)
    timer("Fuel",150)
    if(Charge==1){timer("reload",1000)}
    if(Charge==2){timer("reload",2000)}
    if(Charge==3){timer("reload",3000)}
    timer("1000",1000)
    holoAlpha(2,0)
    
    if(OverCharge==1){
        Missile:setTrails(150,0,1,"trails/physbeam",vec(255,255,255),200)
        timer("OT",50)
    }
}
#overcharge
if(clk("OT")){
    Missile:setPos(owner():aimPos())
    timer("OE",200)
}
if(clk("OE")){
    Missile:propBreak()
}
if(clk("1000")){Missile:propNotSolid(0)}
if (clk("un")){Missile:propNotSolid(0)
    Apply=1}
    if (clk("poof")){
        Missile:applyForce((Missile:forward())*50000)
    Missile:applyForce((Missile:up())*0)
}
if (clk("Fuel")){For=1
    #Missile:soundPlay(2,0,"/weapons/acf_gun/missilefire.wav")
    holoCreate(1)
    holoModel(1,"models/buildables/sapper_dispenser.mdl")

    holoScale(1,vec(0.3,0.42,0.42))
    holoAlpha(1,0)
    holoCreate(10)
    holoModel(10,"models/buildables/sapper_dispenser.mdl")
    holoScale(10,vec(0.3,0.42,0.42))
    holoAlpha(10,0)
    holoCreate(11)
    holoModel(11,"models/buildables/sapper_dispenser.mdl")
    holoScale(11,vec(0.3,0.42,0.42))
    holoAlpha(11,0)
    
    holoPos(1,Missile:toWorld(vec(-75,5,0)))
    holoParent(1,Missile)
    Speed=18000000}
if (For==1){
    
    Missile:applyForce((Missile:forward())*40000)
    Missile:applyAngForce((ang(2,0,0))*100)
    #Missile:turnTo((owner():aimPos() - Missile:pos()):toAngle())
}
if (clk("reload")){Refire=1
    owner():soundPlay(5,1,"PS2/recharge.wav")
    For=0
    Missile:propBreak()
    holoAlpha(2,255)
    Apply=0
    Charge=0
}
if (For==1){Ranger = rangerOffset(200, Missile:pos(),Missile:vel():normalized())}
if (Ranger:hit()){
    holoCreate(10)
    holoPos(10,Missile:pos())
    holoAlpha(10,0)
    #particle("finish",holoEntity(10))
    #particle("portal_rift_flash_01",holoEntity(10))
    soundPitch(10,200)
    soundPitch(11,200)
    Missile:propBreak()
    }


if (owner():weapon():type()=="weapon_rpg"&Refire==1){holoAlpha(2,255)
    owner():weapon():setAlpha(0)
    holoAlpha(4,255)
    holoAlpha(50,0)
    holoAlpha(51,255)
    holoAlpha(52,255)
    holoAlpha(53,255)
    holoAlpha(54,255)
    holoAlpha(55,150)
    holoAlpha(56,255)
    holoAlpha(57,255)
    holoAlpha(58,255)
    #particle("finish",holoEntity(1))
    holoDelete(1)
    holoDelete(10)
    holoDelete(11)
}
if (owner():weapon():type()!="weapon_rpg"){holoAlpha(2,0)
    holoAlpha(4,0)
    holoAlpha(50,0)
    holoAlpha(51,0)
    holoAlpha(52,0)
    holoAlpha(53,0)
    holoAlpha(54,0)
    holoAlpha(55,0)
    holoAlpha(56,0)
    holoAlpha(57,0)
    holoAlpha(58,0)
}


if (owner():weapon():type()=="weapon_rpg"&owner():keyAttack2()==1&Refire==1&Charging==0&Cool==0){Charging=1
    
}
if(Charging==1&changed(owner():keyAttack2())&owner():keyAttack2()==0){Fire=1
    soundStop(10)
    soundStop(11)
    owner():soundPlay(10,2,"weapons/titan_sniper/wpn_titansniperrifle_1p_chargestop_lr_v3_a.wav")
    stoptimer("fi")
    stoptimer("spin")}
if(changed(Charging)&Charging==1){ChargeRot=0
    R=int(random(1,3))
    if(R==1){owner():soundPlay(10,4,"weapons/titan_sniper/wpn_titansniperrifle_1p_chargeup_lr_v3_a.wav")}
    if(R==2){owner():soundPlay(10,4,"weapons/titan_sniper/wpn_titansniperrifle_1p_chargeup_lr_v3_b.wav")}
    if(R==3){owner():soundPlay(10,4,"weapons/titan_sniper/wpn_titansniperrifle_1p_chargeup_lr_v3_c.wav")}
    Charge=1
    OverCharge=0
    timer("spin",2500)
    timer("fullcharge",1000)
}
if(clk("spin")){
    owner():soundPlay(11,3,"weapons/titan_sniper/wpn_titansniperrifle_1p_chargesustain_lr_lp_v3.wav")
    timer("fi",3000)
    
    Charge=3
}
if(clk("fi")){Fire=1
    Charge=3
    OverCharge=1
    owner():soundPlay(15,4,"weapons/titan_sniper/wpn_titansniper_3p_wpnfire_high_energy_2ch_v1_01.wav")
    owner():soundPlay(14,4,"weapons/titan_sniper/wpn_titansniper_1p_wpnfire_med_energy_2ch_v1_01.wav")
    owner():soundPlay(5,2,"weapons/titan_sniper/wpn_titansniper_3p_wpnfire_high_ballisticsweetener_2ch_v1_01.wav")
    soundPitch(15,60)}
if(clk("fullcharge")){Charge=2}
if(changed(owner():keyReload())&owner():keyReload()==1&Charging==1){
    stoptimer("fi")
    stoptimer("spin")
    stoptimer("fullcharge")
    soundPurge()
    Charging = 0
    Charge=0
    Fire=0
    Refire=1
    owner():soundPlay(10,2,"weapons/titan_sniper/wpn_titansniperrifle_1p_chargestop_lr_v3_a.wav")
    Cool=1
    timer("nocool",1500)
}
if(clk("nocool")){Cool=0}
#charge rotate
if(Charging==1){
    ChargeRot+=0.3
}
else{ChargeRot-=0.795}
ChargeRot=clamp(ChargeRot,0,60)
Rot+=ChargeRot
holoAng(50,holoEntity(4):toWorld(ang(0,0,Rot)))

if(last()){holoDeleteAll()}
if(Charge==1&Refire==1){holoColor(56,vec(0,255,0))}
if(Charge==2&Refire==1){holoColor(57,vec(0,255,0))}  
if(Charge==3&Refire==1){holoColor(58,vec(0,255,0))}
if(Charge==0){
    holoColor(56,vec(0,0,0))
    holoColor(57,vec(0,0,0))  
    holoColor(58,vec(0,0,0))
}
