@name Heli
@inputs  Pod:wirelink Cam:wirelink
@outputs Fire
@persist Acc T [Z H] Str [Angle Gat] Rotation Spin SpinningDown
@persist [TarPos Colour Colour2]:vector TarAng:angle [Chair Driver E]:entity
@persist W A S D R Shift Alt Space Active M1 M2
@model models/hunter/blocks/cube025x025x025.mdl
runOnTick(1)
if (first()|dupefinished()) {
    T = 0
    O = owner()
    Z = entity():pos():z() + 5
    Colour = vec(255,255,0)
    Colour2 = vec()
    Rotation = random(360)
    findIncludePlayerProps(O)
    findByModel("models/hunter/plates/plate1x1.mdl")
    E = findClosest(entity():pos())
    if (E==noentity()) {selfDestructAll()}
    for (I=1,30) {
        holoCreate(I)
        holoMaterial(I, "models/shiny")
    }
    holoColor(1, Colour2)
    holoPos(1, E:toWorld(vec(0,15,55)))
    holoScaleUnits(1, vec(200,5,1.5))
    holoParent(1, E)
    holoColor(2, Colour)
    holoPos(2, holoEntity(1):toWorld(vec(100,0,0)))
    holoScaleUnits(2, vec(10,6,2))
    holoParent(2, 1)
    holoColor(3, Colour)
    holoPos(3, holoEntity(1):toWorld(vec(-100,0,0)))
    holoScaleUnits(3, vec(10,6,2))
    holoParent(3, 1)
    
    holoColor(4, Colour2)
    holoPos(4, E:toWorld(vec(0,80,30)))
    holoScaleUnits(4, vec(1,4,40))
    holoParent(4, E)
    holoColor(5, Colour)
    holoPos(5, holoEntity(4):toWorld(vec(0,0,20)))
    holoScaleUnits(5, vec(1.5,5,7))
    holoParent(5, 4)
    holoColor(6, Colour)
    holoPos(6, holoEntity(4):toWorld(vec(0,0,-20)))
    holoScaleUnits(6, vec(1.5,5,7))
    holoParent(6, 4)
    
    holoColor(7, Colour2)
    holoAng(7, E:toWorld(ang(0,90,0)))
    holoPos(7, E:toWorld(vec(20,0,-20)))
    holoScaleUnits(7, vec(60,5,2))
    holoParent(7, E)
    holoColor(8, Colour2)
    holoAng(8, E:toWorld(ang(0,90,0)))
    holoPos(8, E:toWorld(vec(-20,0,-20)))
    holoScaleUnits(8, vec(60,5,2))
    holoParent(8, E)
    
    holoColor(9, Colour)
    holoAng(9, E:toWorld(ang(0,90,0)))
    holoPos(9, holoEntity(7):toWorld(vec(30,0,0)))
    holoScaleUnits(9, vec(10,6,3))
    holoParent(9, 7)
    holoColor(10, Colour)
    holoAng(10, E:toWorld(ang(0,90,0)))
    holoPos(10, holoEntity(7):toWorld(vec(-30,0,0)))
    holoScaleUnits(10, vec(10,6,3))
    holoParent(10, 7)
    holoColor(11, Colour)
    holoAng(11, E:toWorld(ang(0,90,0)))
    holoPos(11, holoEntity(8):toWorld(vec(30,0,0)))
    holoScaleUnits(11, vec(10,6,3))
    holoParent(11, 8)
    holoColor(12, Colour)
    holoAng(12, E:toWorld(ang(0,90,0)))
    holoPos(12, holoEntity(8):toWorld(vec(-30,0,0)))
    holoScaleUnits(12, vec(10,6,3))
    holoParent(12, 8)
    
    holoEntity(9):setTrails(15,0,1.2,"trails/laser",Colour, 255)
    holoEntity(11):setTrails(15,0,1.2,"trails/laser",Colour, 255)
    
    
    for (I = 13,30) {
        holoColor(I, Colour2)   
    }
    #Turret #1
    holoModel(13, "hq_cylinder")
    holoAng(13, E:toWorld(ang(0,0,90)))
    holoPos(13, E:toWorld(vec(12,2,5)))
    holoScaleUnits(13,vec(7,7,12)) 
    holoColor(13, Colour)
    holoParent(13, E)
    
    for (I = 14, 19) {
        holoScaleUnits(I, vec(2.1,2.1,35))
        holoModel(I, "hq_tube")
        holoAng(I, E:toWorld(ang((I-13)*(360/6),0,90)))
        holoPos(I, holoEntity(13):toWorld(vec(0,0,20)) + holoEntity(I):forward()*2.2)
        holoParent(I, 13)
    }
    
    holoModel(20, "hq_cylinder")
    holoAng(20, holoEntity(13):angles())
    holoPos(20, holoEntity(13):toWorld(vec(0,0,35.3)))
    holoScaleUnits(20,vec(7,7,1.5)) 
    holoColor(20, Colour)
    holoParent(20, 13)
    
    holoModel(21, "hq_cylinder")
    holoAng(21, holoEntity(13):angles())
    holoPos(21, holoEntity(13):toWorld(vec(0,0,30)))
    holoScaleUnits(21,vec(7,7,1.5)) 
    holoColor(21, Colour)
    holoParent(21, 13)
    
    #Turret #1
    holoModel(22, "hq_cylinder")
    holoAng(22, E:toWorld(ang(0,0,90)))
    holoPos(22, E:toWorld(vec(-12,2,5)))
    holoScaleUnits(22,vec(7,7,12)) 
    holoColor(22, Colour)
    holoParent(22, E)
    
    for (I = 23, 28) {
        holoScaleUnits(I, vec(2.1,2.1,35))
        holoModel(I, "hq_tube")
        holoAng(I, E:toWorld(ang((I-13)*(360/6),0,90)))
        holoPos(I, holoEntity(22):toWorld(vec(0,0,20)) + holoEntity(I):forward()*2.2)
        holoParent(I, 22)
    }
    
    holoModel(29, "hq_cylinder")
    holoAng(29, holoEntity(22):angles())
    holoPos(29, holoEntity(22):toWorld(vec(0,0,35.3)))
    holoScaleUnits(29,vec(7,7,1.5)) 
    holoColor(29, Colour)
    holoParent(29, 22)
    
    holoModel(30, "hq_cylinder")
    holoAng(30, holoEntity(22):angles())
    holoPos(30, holoEntity(22):toWorld(vec(0,0,30)))
    holoScaleUnits(30,vec(7,7,1.5)) 
    holoColor(30, Colour)
    holoParent(30, 22)
    
    #Colours/Alphas/Cam
    entity():setMaterial("models/shiny")
    entity():setColor(vec())
    E:setColor(vec4())
    findByModel("models/nova/airboat_seat.mdl")
    Chair = findClosest(E:pos())
    Chair:setMaterial("models/shiny")
    Chair:setColor(Colour2)
    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.07}
Spin = clamp(Spin, 0, 30)
Rotation += Spin
if (Rotation>36000) {Rotation=0}
holoAng(1, 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()
if (changed(Driver)&Driver!=noentity()) {
    Chair:hintDriver("Hello " + Driver:name() + "! The controls are:", 15000)
    Chair:hintDriver("WASD - Forward/Back and Yaw", 15000)
    Chair:hintDriver("Mouse buttons - Strafe", 15000)
    Chair:hintDriver("Space - Ascend", 15000)
    Chair:hintDriver("Alt - Descend", 15000)
    Chair:hintDriver("Shift - Move Fast", 15000)
    Chair:hintDriver("R - Fire guns", 15000)
}

### Sound ##################################################  #########################################
if (changed(Active)) {
    if (Active) {
        E:soundPlay(0,0,"/vehicles/airboat/fan_blade_idle_loop1.wav")
        E:soundPlay(1,0,"buttons/button17.wav")
        E:soundPlay(4,0,"ambient/machines/fluorescent_hum_1.wav")
        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,"/fans/fan4.wav"), soundVolume(2, 0.2)} 
    else                {soundStop(2)}
}
soundPitch(0, Spin + sqrt(E:vel():length()/2))
soundPitch(2, sqrt(Spin)*45)
soundPitch(3, 250)
soundPitch(4, Gat)

if (changed(Gat>25)) {
    if (Gat>25) {
        E:soundPlay(3,0,"/weapons/airboat/airboat_gun_loop2.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.15}
elseif (Alt & !Space)           {Z -= Shift ? 0.4 : 0.15}
else                            {Z -= Z/15}
if (!Shift & Z>3.5)         {Z -= 0.2} 
Z = clamp(Z, -15 , 15)
if (Active) {H += Z} else {H -= clamp((20-Spin), 0, 20)}
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)
}
