@name TF2 Turret
@outputs [E Me Turret]:entity [Amodels Models]:array Phase:string [Ang Angle]:angle Pos:vector Upgrade ModelIndex
@persist T H:entity Ents:array Limit Head:string 
@persist Array:array Distance Result:string Times Damage HoloDist Circle X Y
@model models/weapons/w_models/w_toolbox.mdl


if(first() | dupefinished())
{
    runOnTick(1)
    runOnChat(1)
    
    E = entity()
    Me = owner()   
    Amodels = array("models/buildables/sentry1_heavy.mdl","models/buildables/sentry2_heavy.mdl","models/buildables/sentry3_heavy.mdl")
    Models = array("models/buildables/sentry1_blueprint.mdl","models/buildables/sentry1.mdl","models/buildables/sentry2.mdl","models/buildables/sentry3.mdl","models/buildables/sentry3_rockets.mdl")
    ModelIndex = 1
    
    Turret = holoCreate(1)
    holoParent(1, E)
    holoModel(1,Models[ModelIndex,string])
    holoCreate(0)
   
    Phase = "Deploy"
}
if(Phase == "Deploy")
{
    Ang = (Me:eyeTrace():hitNormal():toAngle() + ang(90,0,0)):setYaw((Me:aimPos() - Me:pos() ):toAngle():yaw())
    holoPos(1, Me:aimPos())
    holoAng(1, Ang)
    
    if(Me:aimPos():distance(Me:pos()) >= 100) #| !inrange(Ang, ang(-45,-360,-45), ang(45,360,45)))
    {
        holoAnim(1, 1)
    }else
    {
        holoAnim(1, 0)
        if(Me:keyAttack1())
        {
            Phase = "Build 1"
            Angle = Ang
            Pos = Me:aimPos()
            
            #holoModel(1, E:model())
          
            holoModel(1,Amodels[ModelIndex,string])
            holoAnim(1,2)
            timer("build",5000)   
            ModelIndex++
        }
    }    
}elseif(Phase == "Active")
{
         

   Pos = Turret:toWorld(vec(0,0,30))
    Ang = -Turret:toLocal((Me:aimPos() - Pos):toAngle())
    
    holoSetPose(1,"aim_pitch", Ang:pitch()) 
    holoSetPose(1,"aim_yaw", Ang:yaw())    
timer("test",3000)

    Upgrade = Me:keyAttack2()
    if(changed(Upgrade) & Upgrade & ModelIndex <= 3)
    {
        holoModel(1, Amodels[ModelIndex,string])
        holoAnim(1, "upgrade")
        timer("build",2000)    
        ModelIndex++
    }
   if (owner():weapon():type()=="weapon_physcannon"){
          holoCreate(3)
            holoModel(3,"hqicosphere2")
            holoAlpha(3,100)
            holoColor(3,vec(240,20,20))
            holoPos(3,Pos)
            holoScale(3,vec(10,10,10))
               Pos = Turret:toWorld(vec(0,0,30))
    Ang = -Turret:toLocal((Me:aimPos() - Pos):toAngle())
    
    holoSetPose(1,"aim_pitch", Ang:pitch()) 
    holoSetPose(1,"aim_yaw", Ang:yaw())    
   stoptimer("test")

if (first()) {T = 1 Limit = 10000}
interval(1)


if (owner():keyAttack1()) {

H = propSpawn("models/props_c17/oildrum001_explosive.mdl",1)



T++

H:setMass(50000)
Ents[T,entity] = H
H:setPos(holoEntity(1):pos() + vec(0,25,75) + entity():forward()*24)
H:setAng(ang(90,0,80) + owner():angles())
H:propFreeze(0)
H:applyForce(owner():eyeAngles():forward()*100000000*H:mass())
H:applyAngForce(-H:angles() + ang(0,curtime()*10000000,900)*H:mass())
timer("boom",200)

}


if (owner():keyReload()) {
Ents:propDelete()
T = 1
}

}else{
holoAlpha(3,0)
   Ang = (Pos + vec(0,0,25) - Me:aimPos()):toAngle() + ang(0,90,0)

 holoSetPose(1,"aim_pitch", clamp(Ang:pitch(), 0, 0)) 
    holoSetPose(1,"aim_yaw", sin(curtime()*20)*90)    
    
    
    timer("check",160)
Distance = 200
Result = ""
Times = 100
findExcludePlayer(owner())
Damage = 20
interval(2000)
if (clk("check")) {
    findInSphere(holoEntity(1):pos(), Distance)
    Array = findToArray()
    for(I=1,Array:count()) {
        if(Array[I,entity]:isPlayer()) {
            if(Array[I,entity]:isAlive()) {
                if (Result != "whip") {         
                    N = Array[I,entity]:pos()         
Ent = propSpawn("models/props_c17/oildrum001_explosive.mdl",N,1)
Ent:propBreak() 
Ent:setPos(holoEntity(1):pos() + vec(0,25,75))
}}}}}


timer("test",3000)
}
}

if(clk("build"))
{
 holoModel(1, Models[ModelIndex,string])
    Phase = "Active"
}
if(clk("test")){
holoEntity(1):soundPlay(20,30,"weapons/sentry_scan.wav")}
if(clk("boom")){
H:propBreak()}
