@name O2's Airplane Chip 
@inputs E:entity Vehicle:wirelink
@outputs Alt Light Ranger:ranger Props:array Active Pitch Roll Yaw Vel Speed Pos:vector Torque Torque2 Engine ESpd On Timer Color:array Alpha I

runOnTick(1)
###SETTINGS###
if(first()|duped()){
    Speed=200
    Torque=30
}
###CODE###
Alt=Vehicle:number("Alt")
Light=Vehicle:number("Light")
if(changed(E:getConstraints():count())|changed(E)){
    Props=E:getConstraints()
    Props:pushEntity(E)
    E:setMass(100)
    for(I=1,Props:count()){
        Color:setVector4(I,Props:entity(I):getColor4())
    }
}
if(changed(E)|first()|dupefinished()){
    soundPurge()
    E:soundPlay(0,0,"ambient/machines/turbine_loop_2.wav")
    E:soundPlay(1,0,"ambient/machines/turbine_loop_2.wav")
    E:soundPlay(2,0,"ambient/machines/turbine_loop_2.wav")
    holoCreate(1)
    holoCreate(3)
    holoCreate(4)
    holoCreate(5)
    holoCreate(6)
    holoAlpha(1,0)
    holoAlpha(3,0)
    holoAlpha(4,0)
    holoAlpha(5,0)
    holoAlpha(6,0)
    holoPos(1,E:massCenter())
    holoPos(3,E:massCenter())
    holoPos(4,E:massCenter())
    holoPos(5,E:massCenter())
    holoPos(6,E:massCenter())
    holoParent(1,E)
    holoParent(2,E)
    holoParent(3,E)
    holoParent(4,E)
    holoParent(5,E)
    holoParent(6,E)
    holoEntity(1):soundPlay(3,0,"ambient/machines/turbine_loop_2.wav")
    holoEntity(3):soundPlay(5,0,"ambient/machines/turbine_loop_2.wav")
    holoEntity(4):soundPlay(6,0,"ambient/machines/turbine_loop_2.wav")
    holoEntity(5):soundPlay(7,0,"ambient/machines/turbine_loop_2.wav")
    holoEntity(6):soundPlay(8,0,"ambient/machines/lab_loop1.wav")
}
rangerFilter(Props)
Ranger=rangerOffset(10^10,E:massCenter(),vec(0,0,-1))
Pos=vec(E:massCenter():x(),E:massCenter():y(),E:massCenter():z())
Pos+=E:right()*Vel
if(Vehicle:number("Active")){
    if(Engine<65){
        Engine+=0.065
    }
    if(Engine>=30){
        On=1
    }
    if(On){
        ESpd=toUnit("km/h",E:vel():length())/5
        if(Vehicle:number("W")){E:applyForce((((Pos-E:massCenter())*10-E:vel()/1.025)*E:mass())*(Engine/65)),if(Vel<Speed){Vel+=Speed/400},if(Vel>Speed){Vel-=Speed/400}}
        else{
            if(E:massCenter():z()<Ranger:position():z()+E:boxSize():z()/1.5){
                Vel=0
            }
        }
        if(E:massCenter():z()<Ranger:position():z()+E:boxSize():z()/1.5){
            E:applyForce(((E:massCenter():setZ(Ranger:position():z()+E:boxSize():z()/1.5)-E:massCenter())*10-E:vel()/10)*E:mass())
            E:applyAngForce((angnorm(ang(0,E:angles():yaw(),0)-E:angles())*5-E:angVel())*E:inertia():length()*5)
            E:applyAngForce(((-ang(0,Yaw,0)*5-E:angVel())*E:inertia():length()*5)*(Engine/65))
            Torque2=Torque*10
            if(Vehicle:number("Space")){E:applyForce(vec(0,0,1)*E:mass()*50)}
            if(Vehicle:number("A")){Yaw=-Torque2/3}
            elseif(Vehicle:number("D")){Yaw=Torque2/3}
            else{Yaw=0}
        }
        else{
            E:applyAngForce(((-ang(Roll,Yaw,Pitch)*1.5-E:angVel()*1.75)*E:inertia():length()*1.5)*(Engine/65))
            Torque2=Torque*3
            if(Vehicle:number("A")){if(Roll>-Torque2){Roll-=Torque2/30}}
            elseif(Vehicle:number("D")){if(Roll<Torque2){Roll+=Torque2/30}}
            else{if(Roll>0){Roll-=Torque2/30},if(Roll<0){Roll+=Torque2/30}}
            if(Vehicle:number("R")){Yaw=-Torque2/2}
            elseif(Vehicle:number("Alt")){Yaw=Torque2/2}
            else{Yaw=0}
            if(Vehicle:number("Space")){if(Pitch<Torque2){Pitch+=Torque2/30}}
            elseif(Vehicle:number("Shift")){if(Pitch>-Torque2){Pitch-=Torque2/30}}
            else{if(Pitch>0){Pitch-=Torque2/30},if(Pitch<0){Pitch+=Torque2/30}}
        }
    }
    if(changed(Vehicle:number("Light"))&Vehicle:number("Light")&Timer<1){
        soundStop(4)
        holoEntity(1):soundPlay(4,1,"items/spawn_item.wav")
        Props=E:getConstraints()
        Props:pushEntity(E)
        for(I=1,Props:count()){
            Color:setVector4(I,Props:entity(I):getColor4())
        }
        Active=1
        I=0
    }
    if(changed(Vehicle:number("Light"))&Vehicle:number("Light")&Timer>=1){
        soundStop(4)
        holoEntity(1):soundPlay(4,1.5,"items/suitchargeok1.wav")
        Props=E:getConstraints()
        Props:pushEntity(E)
        Active=0
        I=0
    }
    if(Active){
        Timer++
        if(Alpha>0){
            Alpha-=5
        }
        for(I=1,Props:count(),perf()){
            Props:entity(I):setAlpha(Alpha)
        }
        Vehicle:entity("Entity"):driver():setMaterial("models/effects/vol_light001")
    }
    else{
        Alpha=255
        Timer=0
        for(I=1,Props:count(),perf()){
            Props:entity(I):setAlpha(Color:vector4(I):w())
        }
        Vehicle:entity("Entity"):driver():setMaterial("")
    }

}
else{
    Vel=0
    Pitch=0
    Roll=0
    Torque2=0
    Alpha=255
    Timer=0
    Active=0
    ESpd=0
    On=0
    if(Engine>0){
        Engine-=0.065
    }
}
soundPitch(0,Engine+ESpd)
soundPitch(1,Engine+ESpd)
soundPitch(2,Engine+ESpd)
soundPitch(3,Engine+ESpd)
soundPitch(5,Engine+ESpd)
soundPitch(6,Engine+ESpd)
soundPitch(7,Engine+ESpd)
soundPitch(8,Engine+ESpd)
if(changed(Vehicle:number("Active"))&!Vehicle:number("Active")){
    for(I=1,Color:count(),perf()){
        Props:entity(I):setColor(Color:vector4(I))
    }
}
