@name ApplyForceTesting
@inputs 
@persist [E P]:entity FC FM 
@outputs 
runOnTick(1)
runOnChat(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)
    holoModel(1,"cube")
    holoAng(1,E:toWorld(ang(0,0,-45)))
    holoPos(1,E:toWorld(vec(20,40,5.5)))
    holoDisableShading(1,1)
    holoParent(1,E)
}

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()
    
    FM+=0.0035
} else {
    FM-=0.0035
}

FM = clamp(FM,0,1)

if (changed(FM)) {
    local N = FM*255
    
    holoColor(1,vec(255-N,N,0))
    holoScale(1,vec(1,0.01,FM))
    holoPos(1,E:toWorld(vec(17.7,36.2,14.2)))
}

E:applyForce((E:up()*(FC+((Spc-Shift)*4))-(E:vel()*vec(0.0025,0.0025,0.005)))*E:mass()*FM)
E:applyAngForce((ang((M2-M1)*30,(A-D)*60,(S-W)*30)-(E:angles()*ang(1.5,0,1.5))-E:angVel()*0.75)*E:mass()*FM)

if (chatClk(owner()) & owner():lastSaid():find("!stuck")) {
    hideChat(1)
    entity():isWeldedTo():setPos(entity():isWeldedTo():pos()+vec(0,0,30))
    entity():isWeldedTo():setAng(ang(0,0,0))
}

#[Sounds

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

Spin = clamp(Spin,20,115)

if (changed(Active)) {
    if (Active) {
        soundPlay(0,0,"NPC_AttackHelicopter.Rotors")
        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)
