@name Hely
@inputs Pod:wirelink 
@persist Hover Pos:vector Ang:angle 
runOnTick(1)

E = entity():isWeldedTo()

Speed = 11
UpDown = 5

if (first() | dupefinished()) {
    E:setMass(5000)
    
    Pos = entity():isWeldedTo():pos()
    Ang = entity():isWeldedTo():angles()
    
    function number swivel(A:number,B:number) {
        local C1=(B+(360-A))%360
        local C2=C1-360
        return abs(C1)<abs(C2) ? C1 : C2
    }
    
    function angle swivel(A:angle,B:angle) {
        return ang(
            swivel(A:pitch(),B:pitch()),
            0,
            swivel(A:roll(),B:roll())
        )
    }
    
    function number entity:rotat(Ang:angle,Speed:normal) {
        This:applyAngForce((swivel(This:angles(),Ang) - This:angVel()) * This:mass())
        return 1
    }
    
    function number entity:rotat(Ang:angle) {
        This:rotat(Ang,1)
        return 1
    }
    
    function number entity:move(Vec:vector,Speed:normal) {
        This:applyForce(((Vec-This:pos())-This:vel())*This:mass()*Speed)
        return 1
    }
    
    function number entity:move(Vec:vector) {
        This:move(Vec,1)
        return 1
    }
    
    function number entity:moveNoReset(Vec:vector,Speed:normal,Slowdown:normal) {
        This:applyForce(((Vec-This:pos()-This:vel()*Slowdown))*This:mass()*Speed)
        return 1
    }
}

local Seat = Pod["Entity",entity]
local Driver = Seat:driver()
local Active = Driver:isPlayer()
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 M1 = Driver:keyAttack1()
local M2 = Driver:keyAttack2()

if (Active & !E:isFrozen()) {
    Pos+=
    (
        (W ? E:right() + E:up()/3 : vec()) +
        (S ? E:right()*-1 + E:up()/3 : vec()) +
        (M1 ? E:forward() + E:up()/4 : vec()) +
        (M2 ? E:forward()*-1 + E:up()/4 : vec())
    ) * Speed
    
    Pos+=
    (
        (Space+Hover ? E:up() : vec()) +
        (Shift ? E:up()*-1 : vec())
    ) * UpDown
    
    E:applyAngForce((ang((M1-M2)*10+(A-D)*10,(A-D)*40,(W-S)*30+(A-D)*10)-(E:angles()*ang(1.5,0,1.5))-E:angVel()*0.75)*E:mass())
}

rangerFilter(E)
rangerFilter(Seat)
rangerHitWater(1)
rangerFilter(players())
Alti = rangerOffset(50000,Pos + entity():up()*-20, vec(0,0,-1))
if (Alti:distance() < 12) {Hover = 1} else {Hover = 0}

if (E:isPlayerHolding()) {Pos = E:pos()}

if (!Active) {E:rotat(Ang) E:move(Alti:pos() + vec(0,0,12))}

E:move(Pos)
