@name Heli Controlling
@inputs Pod:wirelink [RLG LLG Rotor Base]:entity 
@outputs 
@persist Spin Rotation State Flyable Kill Pos:vector Ang:angle 
@trigger 
interval(15)
runOnChat(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 R = Driver:keyReload()
local Space = Driver:keyJump()
local Shift = Driver:keySprint()
local M1 = Driver:keyAttack1()
local M2 = Driver:keyAttack2()

if (first()) {
    Kill = 0
    Pos = Base:pos()
    Ang = Base: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
    }
    
    RLG:setAlpha(0)
    LLG:setAlpha(0)
    
    holoCreate(0)
    holoModel(0,"models/sprops/rectangles/size_1_5/rect_6x192x3.mdl")
    holoPos(0,Rotor:toWorld(vec(0,0,5)))
    holoAng(0,Rotor:toWorld(ang(0,0,0)))
    holoParent(0,Rotor)
    
    holoCreate(1)
    holoModel(1,"models/sprops/cylinders/size_3/cylinder_6x108.mdl")
    holoPos(1,RLG:toWorld(vec(0,0,0)))
    holoAng(1,RLG:toWorld(ang(90,90,90)))
    holoParent(1,RLG)
    
    holoCreate(2)
    holoModel(2,"models/sprops/cylinders/size_3/cylinder_6x108.mdl")
    holoPos(2,LLG:toWorld(vec(0,0,0)))
    holoAng(2,LLG:toWorld(ang(90,90,90)))
    holoParent(2,LLG)
    
    holoCreate(3)
    holoModel(3,"models/sprops/misc/fittings/bend_long_45_6.mdl")
    holoPos(3,RLG:toWorld(vec(-63.8,0,3.9)))
    holoAng(3,RLG:toWorld(ang(35,0,180)))
    holoParent(3,RLG)
    
    holoCreate(4)
    holoModel(4,"models/sprops/misc/fittings/bend_long_45_6.mdl")
    holoPos(4,LLG:toWorld(vec(-63.8,0,3.9)))
    holoAng(4,LLG:toWorld(ang(35,0,180)))
    holoParent(4,LLG)
    
    holoCreate(5)
    holoModel(5,"models/sprops/cylinders/size_2/cylinder_3x48.mdl")
    holoPos(5,RLG:toWorld(vec(44.99999975,-12,24)))
    holoAng(5,RLG:toWorld(ang(0,0,28)))
    holoParent(5,RLG)
    
    holoCreate(6)
    holoModel(6,"models/sprops/cylinders/size_2/cylinder_3x48.mdl")
    holoPos(6,LLG:toWorld(vec(44.99999975,12,24)))
    holoAng(6,LLG:toWorld(ang(0,0,-28)))
    holoParent(6,LLG)
    
    holoCreate(7)
    holoModel(7,"models/sprops/triangles_thin/regular/t_etri_12x12.mdl")
    holoPos(7,RLG:toWorld(vec(44.99999975,-1.4,6)))
    holoAng(7,RLG:toWorld(ang(0,0,28)))
    holoParent(7,RLG)
    
    holoCreate(8)
    holoModel(8,"models/sprops/triangles_thin/regular/t_etri_12x12.mdl")
    holoPos(8,LLG:toWorld(vec(44.99999975,1.4,6)))
    holoAng(8,LLG:toWorld(ang(0,0,-28)))
    holoParent(8,LLG)
}

if (chatClk(owner()) & owner():lastSaid():lower():find("!r")) {
    hideChat(1)
    reset()
}

rangerFilter(Rotor) R1 = rangerOffset(0,holoEntity(0):pos() + vec(cos(curtime() * 300) * 96.0000155,sin(curtime() * 300) * 96.0000155,0),Rotor:forward())
rangerFilter(Rotor) R2 = rangerOffset(0,holoEntity(0):pos() + vec(-cos(curtime() * 300) * 96.0000155,-sin(curtime() * 300) * 96.0000155,0),Rotor:forward())

if (changed(R1:hit() | R2:hit() | Base:isUnderWater()) & R1:hit() | R2:hit() | Base:isUnderWater()) {
    Kill = 1
    soundStop(0)
}

if(changed(R) & R) {
    if(State == 0) {State = 1} else {State = 0}
     timer("wait",14000)
}

if (clk("wait")) {if(Flyable == 0) {Flyable = 1} else {Flyable = 0}}

if (State == 1) {Spin += 0.1} else {Spin -= 0.1}

if (changed(R)) {
    if (R) {
        Rotor:soundPlay(0,0,"NPC_AttackHelicopter.Rotors")
    }
}

soundPitch(0,Spin)
soundVolume(0,Spin-20)

if (Spin <= 10) {soundStop(0)}

if (Kill == 0) {
    Spin = clamp(Spin,20,115)
    Rotation += Spin-20
    
    holoAng(0,Rotor:toWorld(ang(0,Rotation/3,0)))
    
    if (Flyable == 1) {
        Pos+=
        (
            (W ? Base:forward()*-1 : vec()) +
            (W ? Base:up()/20 : vec()) +
            (S ? Base:forward() : vec()) +
            (S ? Base:up()/20 : vec())
        ) * 6
        
        Pos+=
        (
            (M1 ? Base:right() : vec()) +
            (M2 ? Base:right()*-1 : vec()) +
            (Space ? Base:up() : vec()) +
            (Shift ? Base:up()*-1 : vec())
        ) * 3
        
        Base:applyAngForce((ang((M1-M2)*30,(A-D)*80,(W-S)*2)-(Base:angles()*ang(50,0,50))-Base:angVel()*2)*Base:mass())
    }
    
    if (!Active) {Base:rotat(Ang)}
    
    Base:move(Pos)
}
