@name Heli Farcry4
@inputs EGP:wirelink 
@persist [E P]:entity FC Spin Rotation Direction:string 
@outputs 
runOnTick(1)

if(first()){
    entity():setColor(0,0,0,0)
    
    E = entity():isConstrainedTo()
    if(E:isValid()){
        E:setMass(50000)
        
        if(E:isVehicle()){
            selfDestruct()
        } else {
            P = E:isConstrainedTo()
        }
    } else {
        selfDestruct()
    }
    FC = 9
    
    holoCreate(1)
    holoPos(1,entity():isWeldedTo():toWorld(vec(0,-30,1)))
    holoScale(1,vec(0.3,0.3,0.3))
    holoAlpha(1,0)
    holoParent(1,entity():isWeldedTo())
    
    holoCreate(2)
    holoModel(2,"hq_cylinder")
    holoPos(2,entity():isWeldedTo():toWorld(vec(0,-30,10)))
    holoAng(2,entity():isWeldedTo():toWorld(ang(0,0,0)))
    holoScale(2,vec(0.2,0.2,1.4))
    holoParent(2,1)
    
    holoCreate(3)
    holoModel(3,"models/sprops/misc/fittings/bend_short_45_3.mdl")
    holoPos(3,entity():isWeldedTo():toWorld(vec(0,-29.2,20.4)))
    holoAng(3,entity():isWeldedTo():toWorld(ang(-45,90,0)))
    holoScale(3,vec(0.8,0.8,0.8))
    holoParent(3,2)
    
    holoCreate(4)
    holoModel(4,"models/sprops/misc/fittings/bend_short_45_3.mdl")
    holoPos(4,entity():isWeldedTo():toWorld(vec(0,-24.4,22.1)))
    holoAng(4,entity():isWeldedTo():toWorld(ang(135,90,0)))
    holoScale(4,vec(0.8,0.8,0.8))
    holoParent(4,3)
    
    holoCreate(5)
    holoModel(5,"models/sprops/cylinders/size_2/cylinder_3x6.mdl")
    holoMaterial(5,"sprops/trans/misc/ls_m2")
    holoPos(5,entity():isWeldedTo():toWorld(vec(0,-23.61,26.5)))
    holoAng(5,entity():isWeldedTo():toWorld(ang(0,0,0)))
    holoScale(5,vec(0.8,0.8,0.8))
    holoParent(5,4)
    
    for (I = 2,4) {holoMaterial(I,"sprops/textures/sprops_metal1")}
    
    holoCreate(6)
    holoPos(6,entity():isWeldedTo():toWorld(vec(12,69.4,24.9)))
    holoScale(6,vec(0.3,0.3,0.3))
    holoAlpha(6,0)
    holoParent(6,entity():isWeldedTo())
    
    holoCreate(7)
    holoPos(7,entity():isWeldedTo():toWorld(vec(-12,69.4,24.9)))
    holoScale(7,vec(0.3,0.3,0.3))
    holoAlpha(7,0)
    holoParent(7,entity():isWeldedTo())
    
    holoCreate(8)
    holoModel(8,"models/sprops/rectangles/size_2/rect_12x24x3.mdl")
    holoMaterial(8,"sprops/trans/misc/beam_side")
    holoPos(8,entity():isWeldedTo():toWorld(vec(12,69.4,24.9)))
    holoAng(8,entity():isWeldedTo():toWorld(ang(90,0,0)))
    holoParent(8,6)
    
    holoCreate(9)
    holoModel(9,"models/sprops/rectangles/size_2/rect_12x24x3.mdl")
    holoMaterial(9,"sprops/trans/misc/beam_side")
    holoPos(9,entity():isWeldedTo():toWorld(vec(-12,69.4,24.9)))
    holoAng(9,entity():isWeldedTo():toWorld(ang(90,0,0)))
    holoParent(9,7)
    
    holoCreate(10)
    holoModel(10,"models/xqm/helicopterrotor.mdl")
    holoPos(10,entity():isWeldedTo():toWorld(vec(0,14,60)))
    holoScale(10,vec(2,2,2))
    holoParent(10,entity():isWeldedTo())
    holoClipEnabled(10,1,1)
    holoClip(10,vec(6,0,0),vec(1,0,0),0)
    
    holoCreate(11)
    holoModel(11,"models/xqm/helicopterrotor.mdl")
    holoPos(11,entity():isWeldedTo():toWorld(vec(0,42,25)))
    holoScale(11,vec(0.3,0.3,0.3))
    holoParent(11,entity():isWeldedTo())
    holoClipEnabled(11,1,1)
    holoClip(11,vec(1,0,0),vec(1,0,0),0)
    
    lightCreate(1)
    lightPos(1,entity():isWeldedTo():pos())
    lightParent(1,entity():isWeldedTo())
    lightToggle(1,0)
    
    EGP:egpClear()
}

local Yaw = floor(entity():isWeldedTo():angles():yaw())
local Speed = round(toUnit("mph",entity():isWeldedTo():vel():length()))
    
if(Yaw<157.5&Yaw>112.5) {Direction="NE"}
if(Yaw<112.5&Yaw>67.5) {Direction="E"}
if(Yaw<67.5&Yaw>22.5) {Direction="SE"}
if(Yaw<22.5&Yaw>-22.5) {Direction="S"}
if(Yaw<-22.5&Yaw>-67.5) {Direction="SW"}
if(Yaw<-67.5&Yaw>-112.5) {Direction="W"}
if(Yaw<-112.5&Yaw>-157.5) {Direction="NW"}
if((Yaw<-157.5&Yaw>-180)|(Yaw<180&Yaw>157.5)) {Direction="N"}

EGP:egpText(1,Direction,vec2(405,35))
EGP:egpSize(1,60)

EGP:egpText(2,"Speed: "+Speed,vec2(35,35))
EGP:egpSize(2,60)

Rotation += Spin-20
if (Rotation>36000) {Rotation=0}
holoAng(10,entity():isWeldedTo():toWorld(ang(-90,Rotation,180)))
holoAng(11,entity():isWeldedTo():toWorld(ang(0,90,180+Rotation)))

Driver = P:driver()
Active = P:driver():isValid()

if (Active) {
    W = Driver:keyForward()
    A = Driver:keyLeft()
    S = Driver:keyBack()
    D = Driver:keyRight()
    Spc = Driver:keyJump()
    Shift = Driver:keySprint()
    M1 = Driver:keyAttack1()
    M2 = Driver:keyAttack2()
    R = Driver:keyReload()
    
    if (entity():isWeldedTo():isUnderWater()) {
        E:applyForce(vec(0,0,0))
        Rotation = 0
        soundStop(1)
        soundStop(0)
    } else {
        E:applyForce((E:up()*(FC+((Spc-Shift)*4))-(E:vel()*vec(0.0025,0.0025,0.005)))*E:mass())
    }

    if (changed(R) & R) {
        lightToggle(1,1)
    }
    
    if (W) { holoAng(1,entity():isWeldedTo():toWorld(ang(0,180,-5))) }
    if (S) { holoAng(1,entity():isWeldedTo():toWorld(ang(0,180,5))) }
    if (A) {
        holoAng(1,entity():isWeldedTo():toWorld(ang(-5,180,0)))
        holoAng(8,entity():isWeldedTo():toWorld(ang(90,-10,0)))
        holoAng(9,entity():isWeldedTo():toWorld(ang(90,-10,0)))
    }
    
    if (D) {
        holoAng(1,entity():isWeldedTo():toWorld(ang(5,180,0)))
        holoAng(8,entity():isWeldedTo():toWorld(ang(90,10,0)))
        holoAng(9,entity():isWeldedTo():toWorld(ang(90,10,0)))
    }
    elseif (!W & !S & !A & !D) {
        holoAng(1,entity():isWeldedTo():toWorld(ang(0,180,0)))
        holoAng(8,entity():isWeldedTo():toWorld(ang(90,0,0)))
        holoAng(9,entity():isWeldedTo():toWorld(ang(90,0,0)))
    }
}

E:applyAngForce((ang((M1-M2)*30,(A-D)*60,(W-S)*30)-(E:angles()*ang(1.5,0,1.5))-E:angVel()*0.75)*E:mass()*2)

#Sounds

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

Spin = clamp(Spin,20,115)

if (changed(Active)) {
    if (Active) {
        soundPlay(0,0,"vehicles/airboat/fan_blade_idle_loop1.wav")
        soundPlay(1,2,"vehicles/apc/apc_start_loop3.wav")
    } 
    elseif(changed(!Active)&!Active){
        soundPlay(1,0,"buttons/button16.wav")
    }
}

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