@name SUAV
@inputs EGP:wirelink
@persist [E V P H Sh]:entity
@persist Fly VSt ETN Lfe
@persist Dri:entity Act W A S D R KZm Spc Alt Shift M1 M2
if(first())
{
    function number angle:nearBy(A:angle,R)
    {
        return inrange(angnorm(round(This-A)),ang(-R),ang(R))
    }
    function entity:turnTo(Tarang:angle)
    {
        local V = This:toLocal(rotationVector(quat(Tarang)/quat(This))+This:pos())
        This:applyTorque((100*V - 30*This:angVelVector())*This:inertia()/3)
    }
    
    runOnTick(1)
    
    E = entity()
    
    P = E:isConstrainedTo()
    if(!P:isVehicle())
    {
        print(4,"Please spawn this E2 on a vehicle.")
        selfDestruct()
    }
}

Dri = P:driver()
Act = Dri:isValid()
W = Dri:keyForward()
A = Dri:keyLeft()
S = Dri:keyBack()
D = Dri:keyRight()
R = Dri:keyReload()
KZm = Dri:keyZoom()
Spc = Dri:keyJump()
Alt = Dri:keyWalk()
Shift = Dri:keySprint()
M1 = Dri:keyAttack1()
M2 = Dri:keyAttack2()

if(EGP:entity():isValid())
{
    if(changed(EGP:entity():isValid()))
    {
        timer("tick",250)
    }
    
    if(clk("tick"))
    {
        stoptimer("tick")
        timer("tick",250)
        
        EGP:egpClear()

        ETN+=100
        Alw = 1
        
        if(findCanQuery())
        {
            findByClass("player")
            findSortByDistance(Dri:shootPos()+E:up()*10)
            Ar = findToArray()
        }
        
        foreach(K, Ply:entity = Ar)
        {
            local I = K*10
            local I2 = I+1
            local I3 = I+2
            if(Ply != Dri)
            {
                EGP:egp3DTracker(I,Ply:boxCenterW())
                EGP:egpTriangleOutline(I2,vec2(cos(ETN)*20,sin(ETN)*20),vec2(cos(ETN+120)*20,sin(ETN+120)*20),vec2(cos(ETN+240)*20,sin(ETN+240)*20))
                EGP:egpText(I3,Ply:name(),EGP:egpPos(I)+vec2(0,25))
                EGP:egpAlign(I3,1)
                EGP:egpParent(I2,I)
                EGP:egpParent(I3,I)
            }
        }
        
        if(Fly)
        {
            local Res = vec2(1300,800)
            EGP:egpBoxOutline(9,Res+vec2(157.5,0),vec2(297.5,80))
            EGP:egpSize(9,5)
            for(I=1,8)
            {
                EGP:egpBox(I,Res+vec2(I*35,0),vec2(30,60))
                EGP:egpAlpha(I,(I<=ceil(Lfe/125))*255)
            }
        }
    }
}

if(Fly)
{
    Vel = V:velL()
    Mass = V:mass()
    V:applyForce(V:toWorldAxis(-Vel*vec(0.025,1,1)*Mass))
    V:applyForce(V:forward()*25*Mass)
    V:turnTo(ang(1,1,1)*Dri:eyeAngles():setRoll(clamp(-V:angVel():yaw()/5,-45,45)))
    
    rangerFilter(V)
    Rd = rangerOffset(70,V:pos(),V:forward())
    if(changed(Rd:hit())&Rd:hit()&!(Rd:entity():isPlayer()|Rd:entity():isNPC()|Rd:entity():isWeapon()))
    {
        timer("ded",400)
        cameraRemove(1)
    }
    
    Lfe-=0.35
    
    if(!V:isValid()|!Act|Lfe<0|clk("ded")|V:isUnderWater())
    {
        cameraRemove(1)
        propSpawn("models/props_phx/misc/potato_launcher_explosive.mdl",V:pos(),1):propBreak()
        Sh:propDelete()
        V:propDelete()
        stoptimer("ded")
        soundStop("fly")
        holoDeleteAll()
        VSt = 0
        Fly = 0
    }
    
    if(changed(Alt)&Alt)
    {
        VSt = !VSt
        holoPos(1,V:toWorld(vec(20-(250*VSt),0,35*VSt)))
        holoVisible(2,Dri,VSt)
    }
}else
{
    if(V:isValid())
    {
        if(clk("start"))
        {
            stoptimer("start")
            V:propFreeze(0)
            Fly = 1
            V:soundPlay("fly",0,"vehicles/digger_grinder_loop1.wav")
            soundPitch("fly",200)
            soundVolume("fly",0.3)
            Lfe = 1000
        }
    }elseif(R)
    {
        V = propSpawn("models/hunter/plates/plate1x1.mdl",Dri:shootPos()+Dri:eye()*150,Dri:eyeAngles(),1)
        V:setAlpha(0)
        V:setMass(400)
        
        H = holoCreate(1,V:toWorld(vec(20,0,0)))
        holoAlpha(1,0)
        holoAng(1,V:angles())
        holoParent(1,V)
        
        holoCreate(2,V:pos())
        holoAng(2,V:toWorld(ang(0,90,0)))
        holoModel(2,"models/xqm/jetbody3.mdl")
        holoScale(2,vec(0.25))
        holoVisible(2,Dri,0)
        holoParent(2,V)
        
        cameraCreate(1,H:pos(),H:angles())
        cameraParent(1,H)
        cameraToggle(1,1,P)
        
        timer("start",50)
    }
}
