@name hover
@inputs 
@outputs 
@persist Mode:string [Entity Owner Vehicle Driver]:entity Inertia:angle Mass VehiclePos:vector VehicleAng:angle
@persist [Height FwdSpeed StrSpeed TrnSpeed LiftAngle] [RangerFPos RangerRPos]:vector
@trigger none
@model models/sprops/rectangles/size_5/rect_48x96x3.mdl


#------------------
#-- Setup
if (first()) 
{
    #------------------
    #-- Variables
    Mass = 5000
    
    Height = 100
    FwdSpeed = 1
    StrSpeed = 1
    TrnSpeed = 10
    LiftAngle = -25
    
    VehiclePos = vec(0,0,10)
    VehicleAng = ang(0,-90,0)
    
    RangerFPos = vec(50,0,0)
    RangerRPos = vec(-50,0,0)

    #------------------
    #-- Don't Change
    Mode = "FindVehicle"

    Entity = entity()
    Owner = owner()
    
    Entity:setMass(5000)
    Inertia = shiftL(ang(Entity:inertia()))
    
    
    holoCreate(1)
    holoCreate(2)
    
    interval(500)
}
if (clk())
{
    switch (Mode) {
        #------------------
        case "FindVehicle",
            interval(90)
            if (findCanQuery())
            {
                findIncludePlayerProps(Owner)
                findByClass("prop_vehicle_prisoner_pod")
                findClipToSphere(Owner:aimPos(), 100)
                
                local Temp = find()
                if (Temp) 
                {
                    Temp:propFreeze(1)
                    Temp:setPos(Entity:toWorld(VehiclePos))
                    Temp:setAng(Entity:toWorld(VehicleAng))   
                    
                    if (Temp:pos():distance(Entity:toWorld(VehiclePos)) < 0.25)
                    {
                        Temp:parentTo(Entity)
                        Vehicle = Temp
                        
                        Mode = "RunPhysics"
                    }
                }
            }
        break
        
        #------------------
        case "RunPhysics",
            interval(90)
            
            Driver = Vehicle:driver()
            if (!Driver)
            { 
                #------------------
                local W = Driver:keyForward()
                local A = Driver:keyLeft()
                local S = Driver:keyBack()
                local D = Driver:keyRight()
                
                local Space = Driver:keyJump()
                local Shift = Driver:keySprint()
                
                local Mouse1 = Driver:keyAttack1()
                local Mouse2 = Driver:keyAttack2()
                
                #------------------
                local VelL = Entity:velL()
                local AngV = Entity:angVel()
                
                local FrontR = rangerOffset(Height, Entity:toWorld(RangerFPos), vec(0,0,-1))
                local RearR = rangerOffset(Height, Entity:toWorld(RangerRPos), vec(0,0,-1))
                
                local AxisP = Entity:toLocalAxis(FrontR:pos() - RearR:pos())
                local Dist = mix(FrontR:pos(), RearR:pos(), 0.5):distance(Entity:pos())
                local Hover = max(0, Height - Dist)

                Entity:applyForce( Entity:toWorldAxis( vec(0, 0, Hover) - VelL*vec(1,1,1) )*Mass )

                local Angle = ang(-atan(AxisP[1], AxisP[3]) - 90, Entity:angles():yaw(), 0)
                Entity:applyAngForce((Entity:toLocal(Angle)*300 - AngV*30) * Inertia)

                Entity:propGravity(!Hover)
                

#[    return array(
        Origin:distance((FrontL + FrontR + RearL + RearR) / 4),
        ang(atan(AxisP[1], AxisP[3]) - 90, -bearing(Origin, Entity:angles(), Aim) / Mul, atan(-AxisR[2], AxisR[3]) - 90)
    )]#

            }
        break
    }
}


#[    VelL = E:velL()
    VSV = clamp(-E:velL():y()/7.5,-1500,1500)
    
    if(changed(VSV))
    {
        soundPitch("thr",abs(VSV))
    }
    
    GR = rangerOffset(HoverHeight, E:pos() + (E:forward() * clamp(VSV,0,1500)), vec(0,0,-1))
    FR = rangerOffset(25 + clamp(VSV,0,1500), mix(E:pos(), GR:pos(), 0.5), E:forward())
    
    local Axis = E:toLocalAxis(FR:pos() - GR:pos())
    local Atan = atan(Axis[2], Axis[3])
    
    holoAng(100, E:toWorld(ang(0, 0, Atan)))
    
    

            local Ground = angnorm(Ranger:hitNormal():toAngle() + ang(90, 0, 0))
            local GroundP = Ground:pitch()
            local GroundY = Ground:yaw() - EntYaw
            
            local Angle = ang(cos(GroundY)*GroundP, EntYaw + (AimB - EntB) / clamp(Mph, 5, 10), -sin(GroundY)*GroundP)
            Entity:applyAngForce((Entity:toLocal(Angle)*200 - Entity:angVel()*20) * Inertia)
            
                
                local Pitch = ((Spc * FlyAng * (W-S)) + clamp((E:angles():pitch() - 35) * FR:hit() * W,-90,0))  * (W|S)
                local Yaw = (A-D) * YawSpeed
                local Roll = -E:angVel():yaw() / 6 - (2 * !GR:hit())
                
                
    applyForce(((vec(0,0,(HoverHeight - GR:distance()) * GR:hit()))*0.75 + (E:forward() * (W-S) * SpeedMultiplier * (10 * 1 + Spc*3 +  Shi*4 + FR:hit() * 3)) - E:toWorldAxis(VelL * vec(0.006125,0.025,0.045))) * E:mass() * Ntrpl)    applyAngForce(((ang(Pitch,Yaw,Roll)  - E:angles()*ang(1,0,1))*5 - E:angVel()/ang(2.5,1.5,2.5)) * E:mass() * Ntrpl)
   ]#
