@name Car
@inputs Pod:wirelink
@outputs 
@persist Ang:angle V:vector BrakeV:vector Z SavedYaw Activated Braked Music MusicTimer SavedMusicTimer RestartTimer

Active = Pod:number("Active")

Owner = owner()

E = entity()

Target = Owner:aimPos()

Pos = E:pos()

Angles = E:angles()

if (Active) {
    if (!Activated) {
        Z = Pos:z()+50
        SavedYaw = Angles:yaw()
        Activated = 1
        if (RestartTimer > curtime()) {
            MusicTimer = curtime()+SavedMusicTimer 
        } else {
            MusicTimer = curtime()
            Music = 0
        }
        soundPitch("Car",100)       
    }
    
    if (MusicTimer <= curtime()) {
        if (!Music) {
            soundPlay("Car", 0, "music/portal_still_alive.mp3")
            Music++
            MusicTimer = curtime()+180
        } elseif (Music == 1) {
            soundPlay("Car", 0, "music/HL2_song23_SuitSong3.mp3")
            Music++
            MusicTimer = curtime()+45
        } elseif (Music == 2) {
            soundPlay("Car", 0, "music/HL2_song25_Teleporter.mp3")
            Music = 0
            MusicTimer = curtime()+50
        }
    }
    
    MusicTimer2 = MusicTimer - curtime()
    
    if (SavedYaw <-175 | SavedYaw > 175) {
        SavedYaw = Angles:yaw()
    }
    Pitch = -Angles:pitch()
    Yaw = SavedYaw-Angles:yaw()
    Roll = -Angles:roll()
    #Weaker Mult makes it not as stable, higher might cause it to wobble or spaz.
    Ang = ang(Pitch,Yaw,Roll)*40000
        
    #Inputs
    W = Pod:number("W")
    A = Pod:number("A")
    S = Pod:number("S")
    D = Pod:number("D")
    Mouse1 = Pod:number("Mouse1")
    Mouse2 = Pod:number("Mouse2")
    R = Pod:number("R")
    Space = Pod:number("Space")
    Shift = Pod:number("Shift")

    if (Space) {
        if (!Braked) {
            #Weaker Mult makes it not as stable, higher might cause it to wobble or spaz.
            BrakeV = Pos
            SavedYaw = Angles:yaw()
            Braked = 1
        }
        V = (BrakeV - Pos)*1000
    } else {
        #Weaker Mult makes it not as stable, higher might cause it to wobble or spaz.
        V = (vec(0,0,Z)-vec(0,0,Pos:z()))*1000
        if (Braked) {
            Z = Pos:z()
            SavedYaw = Angles:yaw()
            Braked = 0
        }
        
        #Hover (The higher value the faster it chanegs height)
        if (Mouse1 & Shift & !Mouse2) {
            Z -= 20
        } elseif (Mouse1 & !Mouse2) {
            Z -= 3
        } elseif (Mouse2 & Shift & !Mouse1) {
            Z += 20
        } elseif (Mouse2 & !Mouse1) {
            Z += 3
        }
        
        #Propulsion (The higher mult, the faster it goes)
        if (Shift & W & !S) {
            applyForce(E:right()*10000)
        } elseif (W & !S) {
            applyForce(E:right()*1000)
        } elseif (Shift & S & !W) {
            applyForce(-E:right()*10000)
        } elseif (S & !W) {
            applyForce(-E:right()*1000)
        } elseif (!W & !S) {
            #AirBrake (the higher divider, the slower it brakes)
            applyForce(-E:vel()*E:mass()/5)
        }
    
        #Turning (The higher yaw value the stronger turn)
        if (Shift & D & !A) {
            applyAngForce(ang(0,-150000,0))
            SavedYaw = Angles:yaw()
        } elseif (D & !A) {
            applyAngForce(ang(0,-50000,0))
            SavedYaw = Angles:yaw()
        } elseif (Shift & A & !D) {
            applyAngForce(ang(0,150000,0))
            SavedYaw = Angles:yaw()
        } elseif (A & !D) {
            applyAngForce(ang(0,50000,0))
            SavedYaw = Angles:yaw()
        }
    }
    #Pos & Gyro-stabilization Forcer
    applyForce(V+$V*5)
    applyAngForce(Ang+$Ang*5)
} else {
    if (Activated) {
        Braked = 0
        Activated = 0
        SavedMusicTimer = MusicTimer-curtime()
        soundPitch("Car",0)
        RestartTimer = curtime()+100
    }
}

runOnTick(1)
