@name Heli
@inputs  Pod:wirelink Cam:wirelink
@outputs Fire
@persist Acc T [Z H] Str [Angle Gat] Rotation Spin SpinningDown Frame
@persist [TarPos Colour Colour2]:vector TarAng:angle [Chair Driver E]:entity
@persist W A S D R Shift Alt Space Active M1 M2
runOnTick(1)
if (first()|dupefinished()) {
    T = 0
    O = owner()
    Z = entity():pos():z() + 5
    Colour = vec(255,255,0)
    Colour2 = vec()
    Rotation = random(360)
    Frame = 1
    findIncludePlayerProps(O)
    findByModel("models/hunter/plates/plate1x1.mdl")
    E = findClosest(entity():pos())
    if (E==noentity()) {selfDestructAll()}
    
    holoCreate(100)
   # holoModel(100,"models/combine_helicopter.mdl")
    holoPos(100,E:toWorld(vec(0,-5,0)))
holoClipEnabled(100,1)
holoClip(100,1,vec(-5,-20,35),vec(0,0,35),0)
    holoAng(100,ang(0,0,0))
    holoColor(100,vec(0,255,0))
    holoScale(100,vec(1.1,1.1,1.1))
    holoParent(100,E)
    holoCreate(101)
    holoModel(101,"models/combine_helicopter.mdl")
    holoPos(101,E:toWorld(vec(0,0,0)))
    holoAng(101,ang(0,0,0))
    holoParent(101,E)
    holoAnim(101,"idle",Frame)
if(Frame<1 & Active){
Frame += 0.003
}else{Frame=0}
 
    #Colours/Alphas/Cam
    E:setMaterial("models/props/props_pipes/pipeset_metal")
    findByModel("models/nova/airboat_seat.mdl")
    Chair = findClosest(E:pos())
    Cam:setVector("Position", E:pos() + E:up()*100 - E:right()*240)
    Cam:setEntity("Parent", E)
    Cam:setAngle("Angle", E:toWorld(ang(14,-90,0)))
}

### Turret Spin #####################################################################################
Angle += Gat
holoAng(13, E:toWorld(ang(-Angle,0,90)))
holoAng(22, E:toWorld(ang(Angle,0,90)))

### Blade Spin ######################################################################################
if (Active) {Spin += 0.17} else {Spin -= 0.17}
Spin = clamp(Spin, 0, 90)
Rotation += Spin
if (Rotation>36000) {Rotation=0}
holoAng(100, E:toWorld(ang(0,Rotation,0)))
holoAng(4, E:toWorld(ang(0,0,Rotation)))

### Pod Controller ##################################################################################
W           = Pod["W",          number]
A           = Pod["A",          number]
S           = Pod["S",          number]
D           = Pod["D",          number]
R           = Pod["R",          number]
Shift       = Pod["Shift",      number]
Alt         = Pod["Alt",        number]
Space       = Pod["Space",      number]
Active      = Pod["Active",     number]
M1          = Pod["Mouse1",     number]
M2          = Pod["Mouse2",     number]
Cam:setNumber("Activated", Active)
Driver = Chair:driver()

### Sound ###########################################################################################
if (changed(Active)) {
    if (Active) {
        E:soundPlay(0,0,"vehicles/chopper_rotor2.wav")
        E:soundPlay(1,0,"vehicles/loud_helicopter_lp_01.wav")
        E:soundPlay(4,0,"vehicles/loud_helicopter_lp_01.wav")
        soundPlay(5,0,"Phx.Turbine") soundPitch(5,50) soundVolume(5,0.5)
        soundVolume(4, 0.5)
    } 
    else        {
      #  soundPurge()
        E:soundPlay(1,0,"buttons/button16.wav")
    }
}
if (!Active & Spin!=0) {SpinningDown = 1} else {SpinningDown=0}
if (changed(SpinningDown)) {
    if (SpinningDown)   {E:soundPlay(2,0,"vehicles/v8/v8_throttle_off_slow_loop2.wav"), soundVolume(2, 0.2)} 
    else                {soundStop(2)}
}
soundPitch(0, Spin + sqrt(E:vel():length()+100))
soundPitch(2, sqrt(Spin)*45)
soundPitch(3, 250)
soundPitch(4, Spin + sqrt(E:vel():length()+100))

if (changed(Gat>25)) {
    if (Gat>25) {
        E:soundPlay(3,0,"/vehicles/digger_startengine1.wav")
        Fire = 1
    } else {
        soundStop(3)
        Fire = 0
    }
}

### Rangers #########################################################################################
rangerFilter(E)
rangerHitWater(1)
RD1 = rangerOffset(E:pos(), E:pos():setZ(-100000))
RD2 = rangerOffset(E:toWorld(vec(0,-30,0)), E:pos():setZ(10000))

### Smoothers #######################################################################################
#Guns
if     (R) {Gat += 0.3}
else       {Gat -= Gat/40}
Gat = clamp(Gat, 0, 30)

#Turning
if     (A) {T += 0.03}
elseif (D) {T -= 0.03}
else       {T -= T/20}
T = clamp(T, -1, 1)
 
#Accelerating
if     (W & !S)                 {Acc += Shift ? 0.1 : 0.05}
elseif (S & !W & Acc>0.7)       {Acc -= Acc/5}
elseif (S & !W)                 {Acc -= 0.04}
else                            {Acc -= Acc/50}
if (!Shift & Acc>3.5)       {Acc -= 0.1} 
Acc = clamp(Acc, -5 , 10)

#Strafing
if     (M1 & !M2)               {Str += Shift ? 0.1 : 0.05}
elseif (M2 & !M1)               {Str -= Shift ? 0.1 : 0.05}
else                            {Str -= Str/30}
if (!Shift & abs(Str)>3.5)  {Str -= Str/25} 
Str = clamp(Str, -10 , 10)

#Height
if     (Space & !Alt)           {Z += Shift ? 0.4 : 0.05}
elseif (Alt & !Space)           {Z -= Shift ? 0.4 : 0.05}
else                            {Z -= Z/15}
if (!Shift & Z>3.5)         {Z -= 0.2} 
Z = clamp(Z, -15 , 15)
if (Active) {H += Z} else {H = clamp((30-Spin), 0, 0)}
H = clamp(H, RD1:pos():z() + (Active ? 40 : 22), RD2:hit() ? RD2:pos():z()-50 : 10000)

### Position and Angle ##############################################################################
if (!E:isUnderWater()) {
    TarPos = E:pos():setZ(H) + E:right():setZ(0):normalized()*Acc*30 + E:forward():setZ(0):normalized()*Str*20
    TarAng = ang(Str*4+T*26, E:angles():yaw() + (3+(E:vel():length()/4000))*T, Acc*1.8)
    
       E:applyForce(((TarPos - E:pos())    * 9  - E:vel())    * E:mass())
    E:applyAngForce(((TarAng - E:angles()) * 40 - E:angVel()) * E:mass()*4)
}
