@name Darkys Space Engine Final
@inputs Seat:entity Pod:entity W A S D Shift Space R Active Alt Mouse1 Mouse2 RPod:entity LPod:entity APod:entity
@outputs CamEnable CamA:angle CamP:vector CamE:entity Cloaked View Turbo Roll YawFix Yaw
@persist Entry Angle:angle Driver:entity DriverWant PodWant
@persist E:entity Vel:vector  
@persist SpeedCounter SpinCounter
@persist Speed1 Speed2 Speed3 SpinDown SpinUp
@model models/hunter/blocks/cube05x05x05.mdl
@trigger all

if (first()) {
    runOnTick(1)
    E = entity()
    E:setMass(50000)
    Entry = 0
    #Podcontroler holo
    holoCreate(1)
    holoModel(1,"cube")
    holoMaterial(1,"beer/wiremod/gate_e2")
    holoPos(1,E:toWorld(vec(-9,0,12)))
    holoAng(1,E:toWorld(ang(0,0,0)))
    holoScale(1,vec(0.3,0.3,0.1))
    holoColor(1,vec(255,255,255))
    holoAlpha(1,255)
    holoShadow(1,0)
    holoParent(1,E)
    #Camcontroler holo
    holoCreate(2)
    holoModel(2,"cube")
    holoMaterial(2,"beer/wiremod/gate_e2")
    holoPos(2,E:toWorld(vec(8.9,0,12)))  
    holoAng(2,E:toWorld(ang(0,0,0)))
    holoScale(2,vec(0.3,0.3,0.1))
    holoColor(2,vec(155,155,155))
    holoAlpha(2,255)
    holoShadow(2,0)
    holoParent(2,E)
    #Glass holo
    holoCreate(3)
    holoModel(3,"hqicosphere2")
    holoMaterial(3,"models/props_combine/health_charger_glass")
    holoPos(3,E:toWorld(vec(0,0,0)))
    holoAng(3,E:toWorld(ang(0,0,0)))
    holoScale(3,vec(2.8,2.8,2.8))
    holoColor(3,vec(255,255,255))
    holoAlpha(3,255)
    holoShadow(3,0)
    holoParent(3,E)
    #Front seat holo
    holoCreate(4)
    holoModel(4,"cube")
    holoMaterial(4,"beer/wiremod/gate_e2")
    holoPos(4,E:toWorld(vec(0,0,13.7)))
    holoAng(4,E:toWorld(ang(0,0,0)))
    holoScale(4,vec(0.9,0.9,0.2))
    holoColor(4,vec(255,255,255))
    holoAlpha(4,255)
    holoShadow(4,0)
    holoParent(4,E)
    #Top holo
    holoCreate(5)
    holoModel(5,"hqcylinder2")
    holoMaterial(5,"beer/wiremod/gate_e2")
    holoPos(5,E:toWorld(vec(-11.5,0,0)))
    holoAng(5,E:toWorld(ang(-90,90,90)))
    holoScale(5,vec(1.9,1.9,0.1))
    holoColor(5,vec(255,255,255))
    holoAlpha(5,255)
    holoShadow(5,0)
    holoParent(5,E)
    #Left holo
    holoCreate(6)
    holoModel(6,"hqcylinder2")
    holoMaterial(6,"beer/wiremod/gate_e2")
    holoPos(6,E:toWorld(vec(0,-11.5,0)))
    holoAng(6,E:toWorld(ang(0,0,90)))
    holoScale(6,vec(1.9,1.9,0.1))
    holoColor(6,vec(255,255,255))
    holoAlpha(6,255)
    holoShadow(6,0)
    holoParent(6,E)
    #Right holo
    holoCreate(7)
    holoModel(7,"hqcylinder2")
    holoMaterial(7,"beer/wiremod/gate_e2")
    holoPos(7,E:toWorld(vec(0,11.5,0)))
    holoAng(7,E:toWorld(ang(0,0,90)))
    holoScale(7,vec(1.9,1.9,0.1))
    holoColor(7,vec(255,255,255))
    holoAlpha(7,255)
    holoShadow(7,0)
    holoParent(7,E)
    #Bottom holo
    holoCreate(8)
    holoModel(8,"hqcylinder2")
    holoMaterial(8,"beer/wiremod/gate_e2")
    holoPos(8,E:toWorld(vec(11.5,0,0)))
    holoAng(8,E:toWorld(ang(-90,90,90)))
    holoScale(8,vec(1.9,1.9,0.1))
    holoColor(8,vec(255,255,255))
    holoAlpha(8,255)
    holoShadow(8,0)
    holoParent(8,E)
    #Front holo
    holoCreate(9)
    holoModel(9,"hqcylinder2")
    holoMaterial(9,"beer/wiremod/gate_e2")
    holoPos(9,E:toWorld(vec(0,0,11.5)))
    holoAng(9,E:toWorld(ang(0,0,0)))
    holoScale(9,vec(1.9,1.9,0.1))
    holoColor(9,vec(255,255,255))
    holoAlpha(9,255)
    holoShadow(9,0)
    holoParent(9,E)
    #Back holo
    holoCreate(10)
    holoModel(10,"hqcylinder2")
    holoMaterial(10,"beer/wiremod/gate_e2")
    holoPos(10,E:toWorld(vec(0,0,-11.5)))
    holoAng(10,E:toWorld(ang(0,0,0)))
    holoScale(10,vec(1.9,1.9,0.1))
    holoColor(10,vec(255,255,255))
    holoAlpha(10,255)
    holoShadow(10,0)
    #Holo Trail add # to remove
    holoEntity(10):setTrails(15,10,5,"trails/laser",vec(255,0,0),255)
    holoParent(10,E)
    #Change entity material
    E:setMaterial("phoenix_storms/fender_chrome")
    E:setColor(255,255,255,255)
    CamA=E:toWorld(ang(-90,0,0))
    CamP=E:toWorld(vec(0,0,10))
    CamE=E
}
#holo rotation remove the # To activate
#holoAng(5,holoEntity(5):toWorld(ang(0,1,0)))
#holoAng(6,holoEntity(6):toWorld(ang(0,-1,0)))
#holoAng(7,holoEntity(7):toWorld(ang(0,-1,0)))
#holoAng(8,holoEntity(8):toWorld(ang(0,1,0)))
#holoAng(9,holoEntity(9):toWorld(ang(0,-1,0)))
#holoAng(10,holoEntity(10):toWorld(ang(0,1,0)))
#cam control
if(Alt & ~Alt){
    View = !View
    if(View){
        CamA=E:toWorld(ang(-90,0,0))
        CamP=E:toWorld(vec(0,0,14))
    }else{
        CamA=E:toWorld(ang(-90,0,0))
        CamP=E:toWorld(vec(0,0,-100))
    }

}
#Sounds   
if(changed(Active) & Active){
LPod:soundPlay(0,0,"ambient/levels/citadel/citadel_drone_loop2.wav")
RPod:soundPlay(1,0,"ambient/levels/citadel/citadel_drone_loop2.wav")
APod:soundPlay(2,0,"ambient/levels/citadel/citadel_drone_loop2.wav")        
}
#Sound on
if(Active){
if(SpinUp<40){SpinUp++}
soundPitch(0,SpinUp*2+round(Speed1/4))
soundPitch(1,SpinUp*2+round(Speed2/4))
soundPitch(2,SpinUp*2+round(Speed3/4))
SpinDown=80
}
#Sound off
if(!Active){
if(SpinDown>0){SpinDown--}
soundPitch(0,SpinDown)
soundPitch(1,SpinDown)
soundPitch(2,SpinDown)
SpinUp=0
}

if(changed(!Active & !SpinDown) & !Active & !SpinDown){soundStop(0) soundStop(1) soundStop(2)}

Speed1=LPod:vel():length()
Speed2=RPod:vel():length()
Speed3=APod:vel():length()
Vel = E:velL()

#Turbo
if( Seat:driver() ){
    Driver = Seat:driver()
    Turbo = clamp(Turbo+(Mouse2-!Mouse2)*2,10,50)
    Angle = Driver:eyeAngles()
#yawfix which has a bug works on most angles remove the # To activate
    #Yaw = $Angle:yaw()*6
    #if(YawFix < Yaw & Yaw > 12) {YawFix += 1}
    #elseif(YawFix > Yaw & Yaw < -12) {YawFix -= 1}
    #else{YawFix = YawFix/2}
    # Perform the accurate angle calculation
    EyeVector = vec(Seat:forward():dot(Seat:driver():eye()),-Seat:right():dot(Seat:driver():eye()),Seat:up():dot(Seat:driver():eye()))
    DesiredDirectionRight = EyeVector:cross(vec(0,0,1)) ####<--- Changed to:###
    DesiredDirection = EyeVector:cross(DesiredDirectionRight)
    LocalDifference1 = vec(E:forward():dot(DesiredDirection),E:right():dot(DesiredDirection),E:up():dot(DesiredDirection))
    LocalDifference2 = vec(E:forward():dot(DesiredDirectionRight),E:right():dot(DesiredDirectionRight),E:up():dot(DesiredDirectionRight))
    DifAngle = angnorm(ang( LocalDifference1:toAngle():pitch(), LocalDifference1:toAngle():yaw() + YawFix, LocalDifference2:toAngle():pitch() ))    
    # Apply angular force
    E:applyAngForce(-ang(-972319.79842469*DifAngle:pitch() + 58148.83048364*E:angVel():pitch(),952258.49474588*DifAngle:yaw() + 58027.662731705*E:angVel():yaw(),-945583.67519025*DifAngle:roll() + 58167.835226727*E:angVel():roll()))
    # Apply positional force
    Force = (W-S)*200000*E:up()*Turbo + (D-A)*400000*E:right() - 3000*E:vel()
    E:applyForce(Force)    
    Force = (Shift-Space)*300000*E:forward()
    E:applyForce(Force)
}
else{
    soundPurge()
    CamEnable = 0    
    # Cancel all angular velocity
    DifAngle = ang(0,0,0)
    E:applyAngForce(-ang(-972319.79842469*DifAngle:pitch() + 58148.83048364*E:angVel():pitch(),952258.49474588*DifAngle:yaw() + 58027.662731705*E:angVel():yaw(),-945583.67519025*DifAngle:roll() + 58167.835226727*E:angVel():roll()))
    Force = -50000*E:vel()
    E:applyForce(Force)
}

if(first()){
    #Change for Preferences#
    #Set Pod Invisible 1/0
    PodWant = 1
    #Set Driver Invisible 1/0 (ONLY WORKS ON OWNER)
    DriverWant = 1
}

if(changed(Active)){
    if(Active){
     
           #Driver
            if(DriverWant){
                Driver = owner() 
                Driver:setMaterial("models/effects/vol_light001")}
    
            #Pod
            if(PodWant){
                Pod:setAlpha(0)
            }
    
    }else{
    
        Pod:setAlpha(255) 
        Driver:setMaterial("")
    }
}
#Made By Darky 
#Thanks to every one who helped enjoy! 
#Instructions just weld whatever props you like to this nocolide them change weight to 1 and change the physical propertys to metal with no gravity and parent for best results.
#The seat is the front cube model if ur wondering where it is
#Controls are wsda shift space mouse 2 alt
