@name TOW missile
@persist F
@persist [E Dri M B]:entity
@persist A:angle
@model models/hunter/plates/plate1x1.mdl
if(first())
{
    E = entity()
    E:setColor(0,0,0,0)
    
    runOnTick(1)
    
    B = holoCreate(100,E:toWorld(vec(0,0,45)))
    holoParent(100,E)
    holoAlpha(100,0)
    
    holoCreate(1,B:toWorld(vec(0,0,24)))
    holoModel(1,"hq_tube")
    holoScale(1,vec(1.2,1.2,6))
    holoParent(1,100)
    holoColor(1,vec(59,79,39))
    holoDisableShading(1,1)
    
    holoCreate(2,holoEntity(1):toWorld(vec(0,0,-34.5)))
    holoModel(2,"hq_cylinder")
    holoScale(2,vec(1.2,1.2,0.25))
    holoParent(2,1)
    holoColor(2,vec(59,79,39))
    holoDisableShading(2,1)
    
    holoCreate(3,E:toWorld(vec(0,0,40)))
    holoModel(3,"hq_sphere")
    holoScale(3,vec(0.75,0.75,1))
    holoParent(3,E)
    holoColor(3,vec(59,79,39))
    holoDisableShading(3,1)
    
    holoCreate(4,E:toWorld(vec(0,0,36)))
    holoModel(4,"hq_cylinder")
    holoScale(4,vec(0.7,0.7,0.2))
    holoParent(4,E)
    holoColor(4,vec(59,79,39))
    holoDisableShading(4,1)
    
    holoCreate(5,E:toWorld(vec(cos(0)*18,sin(0)*18,15)))
    holoAng(5,ang(90,0,0)+(holoEntity(4):pos()-holoEntity(5):pos()):toAngle())
    holoModel(5,"hq_cylinder")
    holoScale(5,vec(0.5,0.5,5))
    holoParent(5,E)
    holoColor(5,vec(59,79,39))
    holoDisableShading(5,1)
    
    holoCreate(6,E:toWorld(vec(cos(120)*18,sin(120)*18,15)))
    holoAng(6,ang(90,0,0)+(holoEntity(4):pos()-holoEntity(6):pos()):toAngle())
    holoModel(6,"hq_cylinder")
    holoScale(6,vec(0.5,0.5,5))
    holoParent(6,E)
    holoColor(6,vec(59,79,39))
    holoDisableShading(6,1)
    
    holoCreate(7,E:toWorld(vec(cos(240)*18,sin(240)*18,15)))
    holoAng(7,ang(90,0,0)+(holoEntity(4):pos()-holoEntity(7):pos()):toAngle())
    holoModel(7,"hq_cylinder")
    holoScale(7,vec(0.5,0.5,5))
    holoParent(7,E)
    holoColor(7,vec(59,79,39))
    holoDisableShading(7,1)
    
    holoAng(100,owner():eyeAngles()+ang(90,0,0))
    
    function entity:turnTo(Tarang:angle)
    {
        local V = This:toLocal(rotationVector(quat(Tarang)/quat(This))+This:pos())
        This:applyTorque((500*V - 150*This:angVelVector())*This:inertia()/3)
    }
}

if(Dri:isValid())
{
    if(Dri:pos():distance(E:pos())>50)
    {
        Dri = noentity()
    }else
    {
        A = Dri:eyeAngles()+ang(90,0,0)
    }
    
    holoAng(100,A)
    
    if(!M:isValid())
    {
        if(changed(Dri:keyUse())&Dri:keyUse())
        {
            soundPlay("fire",3,"weapons/rocket_blackbox_shoot.wav")
            M = propSpawn("models/props_phx/amraam.mdl",B:toWorld(vec(6,0,80)),B:angles()-ang(90,0,0),0)
            M:propGravity(0)
            M:setTrails(10,0,2,"trails/smoke",vec(175),255)
        }
    }
}else
{
    if(findCanQuery())
    {
        findByClass("player")
        findSortByDistance(E:pos())
        if(find():pos():distance(E:pos())<50&&find():team()==owner():team())
        {
            Dri = find()
        }
    }
}

if(changed(Dri:isValid()))
{
    if(Dri:isValid())
    {
        soundStop("dis")
        soundPlay("con",1,"buttons/blip2.wav")
    }else
    {
        soundStop("con")
        soundPlay("dis",1,"buttons/button8.wav")
    }
}

if(M:isValid())
{
    local R = rangerOffset(300,M:pos(),M:forward())
    if(R:hit())
    {
        M:propBreak()
    }
    M:applyForce(M:toWorldAxis(-M:velL()*vec(0,1,1)*M:mass()))
    M:applyForce(M:forward()*100*M:mass())
    if(Dri:isValid())
    {
        M:turnTo((B:toWorld(vec(0,0,2000+B:pos():distance(M:pos())))-M:pos()):toAngle())
    }
}
