@name Hoverking mk3
@inputs Pod:wirelink 
@outputs TEST1 TEST2 
@persist Mass Height HoverForceMult HeightShutoff MomentumStopMult Active HoverForceMult2 
@persist RotationStopMult ThrustForwardMult Yaw ThrustTurnMult MaxTurnSharpness 
@persist Craft:entity Ang:angle Corner:array 
@persist [Forward V1 V2 V3 V4]:vector 
@model models/hunter/plates/plate1x2.mdl
interval(20)

if (first() | dupefinished()) {
    Height            = 35   #Change hover height
    HoverForceMult    = 1    #Change if ship is wobbly or for tiny tweeks
    HoverForceMult2   = 1    #Change to alter amount of upward force, Less = MORE FORCE, 1.6 is average
    HeightShutoff     = 0    #More is higher before velocity slowdown shuts off
    MomentumStopMult  = 6    #Less is more auto-stop power
    RotationStopMult  = 1    #Less is more auto-stop power
    ThrustForwardMult = 45   #More is more thrust (More = higher speed)
    ThrustTurnMult    = 8    #More is more turn
    MaxTurnSharpness  = 10   #More is a sharper turn, don't go much higher than 15 or so
    
    Craft = entity()
    Craft:setMass(3000)
    
    Mass = Craft:mass()
    
    rangerPersist(1)
    rangerHitWater(1)
    rangerFilter(Craft)
    rangerFilter(players())
    
    holoCreate(1)
    holoCreate(2)
    holoCreate(3)
    holoCreate(4)
}

Forward  = Craft:right()
CraftPos = Craft:pos()
CraftAng = Craft:angles()

if (inrange(CraftAng,ang(-90,-180,-90),ang(90,180,90))) {
    R1 = rangerOffset(20000,Craft:toWorld(vec(24,-47,0)),vec(0,0,-1))
    D1 = R1:distance()
    R2 = rangerOffset(20000,Craft:toWorld(vec(-24,-47,0)),vec(0,0,-1))
    D2 = R2:distance()
    R3 = rangerOffset(20000,Craft:toWorld(vec(24,47,0)),vec(0,0,-1))
    D3 = R3:distance() 
    R4 = rangerOffset(20000,Craft:toWorld(vec(-24,47,0)),vec(0,0,-1))
    D4 = R4:distance() 
    AverageHeight = (D1+D2+D3+D4)/4
    
    if (D1 < Height) {
        V1 = (vec(0,0,Height - D1)) * Mass / HoverForceMult2
        Craft:applyOffsetForce(V1 + $V1 * HoverForceMult,Craft:toWorld(vec(24,-47,0)))
    }
    
    if (D2 < Height) {
        V2 = (vec(0,0,Height - D2)) * Mass / HoverForceMult2
        Craft:applyOffsetForce(V2 + $V2 * HoverForceMult,Craft:toWorld(vec(-24,-47,0)))
    }
    
    if (D3 < Height) {
        V3 = (vec(0,0,Height - D3)) * Mass / HoverForceMult2
        Craft:applyOffsetForce(V3 + $V3 * HoverForceMult,Craft:toWorld(vec(24,47,0)))
    }
    
    if (D4 < Height) {
        V4 = (vec(0,0,Height - D4)) * Mass / HoverForceMult2
        Craft:applyOffsetForce(V4 + $V4 * HoverForceMult,Craft:toWorld(vec(-24,47,0)))
    }
    
    if (AverageHeight < Height + HeightShutoff) {
        V5 = ((CraftPos - Craft:vel()/2) - CraftPos) * Mass / MomentumStopMult
        Craft:applyForce(V5)
    }
    
    A1 = ((CraftAng - Craft:angVel()/RotationStopMult) - CraftAng) * Mass
    Craft:applyAngForce(A1)
    
    Active = Pod["Active",number]
    
    if (Active) {
        local W          = Pod["W",number]
        local A          = Pod["A",number]
        local S          = Pod["S",number]
        local D          = Pod["D",number]
        local R          = Pod["R",number]
        local Light      = Pod["Light",number]
        local Space      = Pod["Space",number]
        local Shift      = Pod["Shift",number]
        local Alt        = Pod["Alt",number]
        local Mouse1     = Pod["Mouse1",number]
        local Mouse2     = Pod["Mouse2",number]
        local PrevWeapon = Pod["PrevWeapon",number]
        local NextWeapon = Pod["NextWeapon",number]
        
        if (AverageHeight < Height + HeightShutoff) {
            if (Shift) {
                if (W) {
                    V6 = ((CraftPos + Forward * 1000) - CraftPos) * Mass / 1000 * ThrustForwardMult * 2
                    Craft:applyForce(V6)
                } elseif (S) {
                    V6 = ((CraftPos - Forward * 1000) - CraftPos) * Mass / 1000 * ThrustForwardMult * 2
                    Craft:applyForce(V6)
                }
            } else {
                if (W) {
                    V6 = ((CraftPos + Forward * 1000) - CraftPos) * Mass / 1000 * ThrustForwardMult
                    Craft:applyForce(V6)
                } elseif (S) {
                    V6 = ((CraftPos - Forward * 1000) - CraftPos) * Mass/1000 * ThrustForwardMult
                    Craft:applyForce(V6)
                }
            }
        }
        
        if (A & S) {
            if (Yaw > -MaxTurnSharpness) {Yaw--}
        } elseif (D & S){
            if (Yaw < MaxTurnSharpness) {Yaw++}
        } elseif (A) {
            if (Yaw < MaxTurnSharpness){Yaw++}
        } elseif (D) {
            if (Yaw > -MaxTurnSharpness) {Yaw--}
        } else {
            if (Yaw > 0) {Yaw--}
            if (Yaw < 0) {Yaw++}
        }
        
        if (Yaw != 0) {
            Ang = (CraftAng:setYaw(CraftAng:yaw() + Yaw) - CraftAng) * Mass * ThrustTurnMult
            Craft:applyAngForce(Ang + $Ang * 5)
        }
    } elseif ($Active) {Yaw = 0}
}

holoPos(1,R1:pos())
holoPos(2,R2:pos())
holoPos(3,R3:pos())
holoPos(4,R4:pos())
