@name HeliControl
@inputs [Pod EGP]:wirelink [Prop1 Prop]:entity 
@outputs Page 
@persist Spin SpinningDown Rotation Distance I L St Yes Direction:string Array:array 
@trigger 
runOnTick(1)

   local Seat = Pod["Entity",entity]
   local Driver = Seat:driver()
   local W = Driver:keyForward()
   local A = Driver:keyLeft()
   local S = Driver:keyBack()
   local D = Driver:keyRight()
   local R = Driver:keyReload()
   local P1 = Driver:keyPressed("1")
   local P2 = Driver:keyPressed("2")
   local P3 = Driver:keyPressed("3")
   local Mouse5 = Driver:keyPressed("mouse_5")
   local Mouse4 = Driver:keyPressed("mouse_4")
   local Mouse1 = Driver:keyAttack1()
   local Mouse2 = Driver:keyAttack2()
   local Space = Driver:keyJump()
   local Shift = Driver:keySprint()
   local Alt = Driver:keyWalk()
   local Active = Driver:isPlayer()
   local Speed = round(toUnit("mph",Prop:vel():length()))
   local EVel = Prop:vel()

local Yaw=floor(Prop:isWeldedTo():angles():yaw())
    
if(Yaw<157.5&Yaw>112.5) {Direction="SW"}
if(Yaw<112.5&Yaw>67.5) {Direction="W"}
if(Yaw<67.5&Yaw>22.5) {Direction="NW"}
if(Yaw<22.5&Yaw>-22.5) {Direction="N"}
if(Yaw<-22.5&Yaw>-67.5) {Direction="NE"}
if(Yaw<-67.5&Yaw>-112.5) {Direction="E"}
if(Yaw<-112.5&Yaw>-157.5) {Direction="SE"}
if((Yaw<-157.5&Yaw>-180)|(Yaw<180&Yaw>157.5)) {Direction="S"}

if (first()) {     
    holoCreate(1) #Main Rudder
    holoModel(1,"models/xqm/helicopterrotor.mdl")
    holoScale(1,vec(3,3,3))
    holoClipEnabled(1,1,1)
    holoClip(1,vec(4,0,0),vec(1,0,0),0)
    
    holoCreate(2) #Tail Rudder
    holoModel(2,"models/xqm/helicopterrotor.mdl")
    holoScale(2,vec(1,1,0.6))
    
    holoCreate(3)
    holoModel(3,"models/effects/sentry1_muzzle/sentry1_muzzle.mdl")
    holoAng(3,Prop:angles()+ang(0,-90,0))
    
    holoCreate(10)
    holoPos(10,Prop:toWorld(vec(0,-70,-18)))
    holoAlpha(10,0)
    holoParent(10,Prop)
    
    Prop1:parentTo(Prop)
}
#Prop1:setPos(holoEntity(10):pos())

holoPos(1,Prop:toWorld(vec(0,38,70)))
holoPos(2,Prop:toWorld(vec(2,200,57.5)))
holoPos(3,Prop:toWorld(vec(0,-80,0)))
Rotation += Spin-20
if (Rotation>36000) {Rotation=0}
holoAng(1,Prop:toWorld(ang(-90,Rotation,180)))
holoAng(2,Prop:toWorld(ang(0,0,Rotation)))
holoAng(10,Prop:angles())

if (Active) {
    Spin += 0.3
} else {
    Spin -= 0.25
}

Spin = clamp(Spin,20,115)

if (!Active & Spin!=0) {SpinningDown = 1} else {SpinningDown=0}
if (changed(SpinningDown)) {
    if (SpinningDown) {
        Prop:soundPlay(2,0,"vehicles/apc/apc_shutdown.wav")
        } else {
            soundStop(2)
        }
}

if (changed(Active)) {
    Prop:applyForce(vec(0,0,Prop:mass()*24.3))#24.3
    Prop:propFreeze(1)
    timer("wait",5000)
    if (Active) {
        Prop:soundPlay(0,0,"NPC_AttackHelicopter.Rotors")
        Prop:soundPlay(1,2,"vehicles/apc/apc_start_loop3.wav")
        Page = 1
    } 
    elseif(changed(!Active)&!Active){
        Prop:soundPlay(1,0,"buttons/button16.wav")
    }
}

if (Active) {
    Prop:applyForce(vec(0,0,Prop:mass()*30.15))
} elseif (!Active) {
    Page = 3
}

if (clk("wait")) {
    Prop:propFreeze(0)
}

if (!W|A|S|D|Space|Alt) {
    Prop:vel()
}

soundPitch(0,Spin + sqrt(Prop:vel():length()+100))
soundVolume(0,Spin-20)

if (Space) {Prop:applyForce(Prop:inertia()+Prop:up()*Prop:mass()*3)}
if (Alt) {Prop:applyForce(Prop:inertia()+-Prop:up()*Prop:mass()*3)}
if (Mouse5) {Prop:applyForce(Prop:inertia()+Prop:forward()*Prop:mass()*2)}
if (Mouse4) {Prop:applyForce(Prop:inertia()+-Prop:forward()*Prop:mass()*2)}
if (W) {Prop:applyForce(Prop:inertia()+Prop:right()*Prop:mass()*3)}
if (S) {Prop:applyForce(Prop:inertia()+-Prop:right()*Prop:mass()*3)}
if (A) {Prop:applyAngForce(Prop:angVel()*Prop:angles()+ang(0,Prop:mass()*70,0))}
if (D) {Prop:applyAngForce(Prop:angVel()*Prop:angles()+ang(0,Prop:mass()*-70,0))}
if (P1) {Page = 1} elseif (P2) {Page = 2} elseif (P3) {Page = 3}

#Stablizer
TarAng = ang(0,Prop:angles():yaw(),0) - ang(Prop:angVelVector())
AngDiff = ang(0,0,0) - Prop:angles()
Prop:applyAngForce(((ang(Prop:angVelVector()) + TarAng) - Prop:angVel())* Prop:mass() + AngDiff*Prop:mass())

if (Page == 1) {
    soundStop(2)
    for (I = 3,9) {holoAlpha(I,0)}
    EGP:egpClear()
#Altitude Indicator
    rangerFilter(Prop)
    rangerHitEntities(0)
    rangerHitWater(1)
    Ranger = rangerOffset(99999,Prop:pos(),-Prop:up())
    Distance = round((Prop:pos() - Ranger:pos()):length(),0)
    EGP:egpText(1,"You are "+Distance+" units from the ground. ",vec2(256,512))
    EGP:egpSize(1,10)
#Compass    
    EGP:egpText(2,Direction,vec2(375,435))
    EGP:egpSize(2,20)
#Speed
    EGP:egpText(3,"Speed: "+Speed,vec2(305,415))
    EGP:egpSize(3,10)
}

if (Page == 2) {
    EGP:egpText(2,Direction,vec2(375,435))
    EGP:egpSize(2,20)
    
    EGP:egpText(3,"Speed: "+Speed,vec2(305,415))
    EGP:egpSize(3,10)
}

if (changed(Page == 2)) {
    if (Page == 2) {
        EGP:egpClear()
        EGP:egpBox(3,vec2(323,466),vec2(86,86))
        EGP:egpMaterial(3,"vgui/hud/xbox_reticle")
        Prop:soundPlay(2,0,"ambient/machines/pneumatic_drill_2.wav")
    }
} elseif(changed(R)&R){Yes=!Yes}
if (changed(Yes)) {
    if (Yes) {
        for (I = 3,9) {holoAlpha(I,255)}
        EGP:egpCircleOutline(4,vec2(323,466),vec2(5,5))
        Prop:soundPlay(3,0,"ambient/machines/pneumatic_drill_4.wav")
    } else {for (I = 3,9) {holoAlpha(I,0)}}
}else {holoAlpha(3,0)}

if (Yes & Mouse1) {
    holoAlpha(3,255)
}

if (changed(Yes & Mouse1) & Yes & Mouse1 == 1) {
    Prop:soundPlay("fire",0,"/npc/combine_gunship/gunship_fire_loop1.wav")
    soundPitch("fire",250)
} elseif (changed(Yes & Mouse1) & Yes & Mouse1 == 0) {
    soundStop("fire")
}

if (changed(!Yes)) {
    if (!Yes) {
        EGP:egpRemove(4)
        Prop:soundPlay(3,0,"ambient/machines/pneumatic_drill_4.wav")
    }
} else {soundStop(4)}

if (Page == 3) {
    EGP:egpClear()
    soundStop(2)
    for (I = 3,9) {holoAlpha(I,0)}
}

#Rockets
#models/props_phx/misc/flakshell_big.mdl
