@name Instant Chassis
@inputs [Wirelink Camera]:wirelink
@outputs [FLHoloA FRHoloA RLHoloA RRHoloA]:vector RearBrake RForceX  VelX DeltaTurn FrontLUp Clutch MaxFLGrip MaxFRGrip MaxRLGrip FrontUp Turn Return Forcea FrontSkid RearSkid FLVelY RLVelY Force DesiredAccel Torque EngDelta EngineFriction Speed MPH NeededRPM [Body Chip Pod Driver]:entity Angles:angle FWheelRPM RWheelRPM EngineRPM WheelRPM ActualRPM Diff Accel Cruise CruiseSpeed CruiseDiff
@outputs CamAngle:angle Mode Gear
@persist Active  Shift Alt R Space Mouse2 CamZoom Zoom CamActive FrontL FrontR RearL RearR
@persist Force Spawn GearP GearR GearS GearClk Sign W FLSus FRSus RLSus RRSus
@persist Pi Third Lock Starter Steer Hold Flood Speed FrontVelX RearVelX Power Auto [EngineStart EnginePower EngineIdle ShiftSound]:string GearRatio Ratio Gas
@persist ChangeUp ChangeDown Mass Downforce WheelX FrontWheelY RearWheelY StartHeight HeightSum FrontGrip RearGrip FBurnout RBurnout Skid
@persist FWD RWD FWSteer RWSteer FTyreSize RTyreSize FWheelScale RWheelScale FToe RToe [FBox RBox]:vector FWheelDiff RWheelDiff FWheelZ RWheelZ FWheelL RWheelL FSpin RSpin Crash [FLWheel FRWheel RLWheel RRWheel FLEnt FREnt RLEnt RREnt]:entity
@persist [FLPos FRPos RLPos RRPos]:vector
@persist MaxFLGrip MaxFRGrip MaxRLGrip MaxRRGrip

if(dupefinished()|first()){Chip = entity(),Body=entity():isWeldedTo(),Spawn = 0,rangerPersist(1), rangerHitEntities(1), rangerFilter(players()),rangerFilter(Body),rangerFilter(Body:getConstraints())}


# -- CIPHER_ULTRA'S INSTANT CHASSIS -- #

# -- Guidelines -- #

# 1. This E2 will work on most props, but I recommend flat squares or cubes such as: 
#      * models/props_phx/construct/metal_plate1.mdl
#      * models/hunter/plates/plate2x2.mdl
#  A longer wheelbase usually works better with bigger props, and short wheelbases with smaller props. 

# 2. Make sure any other constrained props (like a collision box or car body) have a total mass the same or heavier than the base prop.
#    Try to balance weights equally over the chassis for best handling.  

# 3. I also recommend parenting all props and using the 'Main' prop,
#    (The one the chip is placed on) as the Parent.
#    Parenting makes contraptions more stable and reduces lag.

# 4. For advanced users: It is also best to leave some props unparented to
#    create a collision box around the vehicle, such as bumpers.


if(Spawn < 17){interval(100)


# -- CUSTOMISATION -- #

    Mass = 2000 #--------------------> The total mass of all constrained props with physics (Base prop is set to half of this value).
    Downforce = 4

    
# -- Suspension Settings -- #

    Height = 16    #--------------------> The height from the ground you want the chassis to be
    StartHeight = 1    #---------------> The start height of the suspension rangers...
                                      #-> (Only need to lower if the any bodywork, like wheel arches,
                                      #-> are very close to the top of the wheels.
  
# -- Wheel Settings -- #

    WheelX = 70    #--------------------> The length of the wheelbase #60 75
    FrontWheelY = 36    #---------------> The width between the front wheels 38
    RearWheelY = 36   #----------------> The width between the rear wheels
    
    FrontWheelSize = 2    #-------------> The size of the wheel middle # 2.4
    FrontWheelWidth = 0.7    #------------> The width of the wheel middle # 0.8
    RearWheelSize = 2   #--------------> The size of the wheel middle
    RearWheelWidth = 0.7   #-------------> The width of the wheel middle
        

    WheelMaterial = "phoenix_storms/tire_side"
    TyreMaterial = "/models/dav0r/hoverball"#"rubber2"
    
    FrontGrip = 5
    RearGrip = 5
    
# -- Drivetrain Settings -- #

    FWD = 0 # Front Wheel Drive: 1=YES / 0=NO
    RWD = 1 # Rear Wheel Drive: 1=YES / 0=NO
    FWSteer = 1 # Front Wheels Steer: 1=YES / 0=NO
    RWSteer = 0 # Rear Wheels Steer: 1=YES / 0=NO
    


# -- Gearbox Settings -- #
        
    Auto = 1
    ChangeUp = 5950
    ChangeDown = 2000
    GearRatio = 1.7

# -- Sound Settings -- #

    EngineStart = "/vehicles/digger_startengine1.wav"
    EnginePower = "/engines/l4/escort_onhigh.wav"
    EngineIdle = "/engines/l4/escort_onverylow.wav"
    ShiftSound = "engines/turbo/turbo_hiss2.wav"




# -- END of settings -- #





# ----------------------------------------------------------------------------------------#


# -- No need to change any of the following code -- #
# -- Only advanced E2 users should change the following code! -- #


# ----------------------------------------------------------------------------------------#

    
    Body:setMass(Mass/2)
    Power =  20 
    Ratio=1
    Pi = 3.141592
    Third = 100/1.5
    Zoom = -WheelX*2.5
    if(FWD & RWD){Power = Power/2}
    
    FTyreSize = FrontWheelSize * 1.1   #---------> The size of the tyres [Don't need to change]
    RTyreSize = RearWheelSize * 1.1   #---------->          # The size of the tyres [Don't need to change]
    FTyreWidth = FrontWheelWidth * 9    #-> # The width of the tyres [Don't need to change]
    RTyreWidth = RearWheelWidth * 9    #--> The width of the tyres [Don't need to change]
    
    FWheelScale = holoScaleUnits(5):x() * Pi * 1.1
    RWheelScale = holoScaleUnits(7):x() * Pi * 1.1
    
    FWheelZ = FTyreSize*5.8
    RWheelZ = RTyreSize*5.8

    
    FBox =  vec(FWheelZ,FWheelZ,FWheelZ*0)
    RBox =  vec(RWheelZ,RWheelZ,RWheelZ*0)
      
    FWheelDiff = 15 / (holoScaleUnits(5):x())
    RWheelDiff = 15 / (holoScaleUnits(7):x())
    
    
    HeightSum =  (Height + StartHeight)
    
    FLPos = vec(WheelX,FrontWheelY,StartHeight)
    FRPos = vec(WheelX,-FrontWheelY,StartHeight)
    RLPos = vec(-WheelX,RearWheelY,StartHeight)
    RRPos = vec(-WheelX,-RearWheelY,StartHeight)
    

    FToe  = atan((FrontWheelY/4)/WheelX)/2
    RToe  = atan((RearWheelY/4)/WheelX)/2
    
    
    Spawn++
    holoCreate(Spawn)
                   
    if(Spawn < 5){holoModel(Spawn,"cylinder") # Wheel middle
        holoScale(1,vec(FrontWheelSize,FrontWheelSize,FrontWheelWidth))
        holoScale(2,vec(FrontWheelSize,FrontWheelSize,FrontWheelWidth))
        holoScale(3,vec(RearWheelSize,RearWheelSize,RearWheelWidth))
        holoScale(4,vec(RearWheelSize,RearWheelSize,RearWheelWidth))
        if(Spawn == 1 | Spawn == 3){holoAng(Spawn,Body:toWorld(ang(0,0,90)))}
        else{holoAng(Spawn,Body:toWorld(ang(0,0,-90)))}
         holoMaterial(Spawn,WheelMaterial)
        holoParent(Spawn,Body)
    }elseif(Spawn < 9){holoModel(Spawn,"hq_torus") # Tyres
        holoAng(Spawn,Body:toWorld(ang(0,0,90)))
        holoScale(5,vec(FTyreSize,FTyreSize,FTyreWidth))
        holoScale(6,vec(FTyreSize,FTyreSize,FTyreWidth))
        holoScale(7,vec(RTyreSize,RTyreSize,RTyreWidth))
        holoScale(8,vec(RTyreSize,RTyreSize,RTyreWidth))
        holoMaterial(Spawn, TyreMaterial)
        holoColor(Spawn,vec(250,50,50))
        holoParent(5,1)
        holoParent(6,2)
        holoParent(7,3)
        holoParent(8,4)
        
        
        FLWheel = holoEntity(1)
        FRWheel = holoEntity(2)
        RLWheel = holoEntity(3)
        RRWheel = holoEntity(4)
        
    }elseif(Spawn < 13){
    
    holoModel(9,"dome")
    holoModel(10,"hq_cylinder")
    
    holoModel(12,"hq_rcube_thick")
    holoScale(9,vec(0.34,0.34,1))
    holoScale(10,vec(0.1,0.1,0.8))
   
    if(Auto){holoModel(11,"hq_sphere"),holoScale(11,vec(0.26,0.24,0.3)),holoScale(12,vec(0.8,0.5,0.3)),holoMaterial(12,"metal5"),holoAlpha(9,0)}
    else{holoModel(11,"hq_sphere"),holoScale(11,vec(0.24,0.24,0.24)),holoScale(12,vec(0.8,0.6,0.2)),holoMaterial(12,"phoenix_storms/road")}
    holoMaterial(9,"phoenix_storms/road")
    holoMaterial(10,"models/weapons/rifle_shell/rifleshellside")
    holoMaterial(11,"beer/wiremod/gate_e2")
    
    holoColor(9,vec(150,150,150))
    holoColor(12,vec(150,150,150))
    
    holoPos(10,holoEntity(9):toWorld(vec(0,0,6)))
    holoPos(11,holoEntity(9):toWorld(vec(0,0,10)))
    holoPos(12,Chip:toWorld(vec(0,0,1)))
    holoAng(Spawn,Body:toWorld(ang(0,0,0)))
    holoAng(11,Body:toWorld(ang(-90,0,0)))
    
    holoParent(9,Chip)
    holoParent(10,9)
    holoParent(11,9)
    holoParent(12,Chip)
    #Chip:setAlpha(0)
        

    
    }elseif(Spawn < 17){
    holoParent(Spawn,Chip)
    holoAlpha(Spawn,0)
    }else{
        
    holoCreate(17,Body:toWorld(FLPos),vec(),Body:toWorld(ang()))
    holoCreate(18,Body:toWorld(FRPos),vec(),Body:toWorld(ang()))
    holoCreate(19,Body:toWorld(RLPos),vec(),Body:toWorld(ang()))
    holoCreate(20,Body:toWorld(RRPos),vec(),Body:toWorld(ang()))
    holoParent(17,Body)
    holoParent(18,Body)
    holoParent(19,Body)
    holoParent(20,Body)
    holoParent(1,17)
    holoParent(2,18)
    holoParent(3,19)
    holoParent(4,20)
    
        FLEnt = holoEntity(17)
        FREnt = holoEntity(18)
        RLEnt = holoEntity(19)
        RREnt = holoEntity(20)
    }

             
}else{

# -- Controls and Active Code -- #

if(!Lock){
PodActive = Wirelink:number("Active")
}

if(PodActive){
    if(!Active & Starter < 70){Starter++}
    else{Active = 1}
}elseif(Active){Active = 0}
    
 if(Active){
    if($Active){Pod = Wirelink:entity("Entity"), Driver = Pod:driver()} 
    W = Wirelink:number("W")
    A = Wirelink:number("A")
    S = Wirelink:number("S")
    D = Wirelink:number("D")
    R = Wirelink:number("R")
    Space = Wirelink:number("Space")
    Shift = Wirelink:number("Shift")
    Alt = Wirelink:number("Alt")
    Mouse2 = Wirelink:number("Mouse2")
    Flood = Chip:isUnderWater()
    

        
if(Active & ($Active | $Flood) & !Flood){
    
    Body:soundPlay(1,2,EngineStart)
    Body:soundPlay(2,0,EnginePower)
    Body:soundPlay(3,0,EngineIdle)

,soundStop(5),soundStop(6)
   Starter = 1
    EngineRPM = 400

}elseif(Flood & $Flood){Hold = 2
soundStop(1),soundStop(2),Body:soundPlay(5,5,"vehicles/jetski/jetski_off.wav")
Chip:soundPlay(6,5,"ambient/energy/newspark10.wav")
}
if(inrange(Starter,1,41)){Starter++,if(EngineRPM<900){EngineRPM+=40}else{EngineRPM-=160},soundVolume(1,20),soundPitch(1,200)}
if(Starter > 40){soundStop(1),if(EngineRPM < 2000){EngineRPM += 100}else{Starter=0}}
    
}else{
if(Hold != 1){Hold = 1}

runOnChat(1)

if(chatClk(owner())){LastSaid=owner():lastSaid():explode(" "),
    if(LastSaid:string(1)=="/lock"){Lock = !Lock
    Wirelink:setNumber("Lock",Lock)
    if(Lock){print("Locked")}
    else{print("Unlocked"),soundStop(31)}
    if($Lock){soundPlay(7,2,"/ui/buttonclickrelease.wav"),soundPitch(7,55)}
    }
}


}

# -- END of Controls and Active Code -- #

runOnTick(1)
if(tickClk()){
    
#### Variables ###
Angles = Body:angles()
Up = Body:up()
Vel = Body:velL()
WVel = Body:velL():setZ(Downforce*Speed)*0.001#:rotate(ang(10,0,0))*0.001##Body:vel()*-5




## Steering

if(Active){
    SteerLock = 35-Speed*0.01

Yaw = Body:angVel():yaw()
Return = clamp(Yaw*0.015*sign(VelX),-1,1)

Steer +=  (((A-D)*clamp(abs(Speed),400,2000)*0.002) - Steer)*0.1

   if(!A & !D & inrange(Turn,-0.1,0.1) & Turn != 0){Turn = 0}
else{Turn = clamp(Turn + (Steer - Return), -SteerLock,SteerLock)}
#Turn = clamp(Turn + ((Pod:toLocal(Driver:eyeAngles()):yaw()-90)-Turn)*0.1,-SteerLock,SteerLock)
DeltaTurn += clamp(abs($Turn*400) - DeltaTurn,-10,100)

}

# -- Velocities -- #

FLHoloA = FLEnt:pos()
FRHoloA = FREnt:pos()
RLHoloA = RLEnt:pos()
RRHoloA = RREnt:pos()

FLVel = FLWheel:toLocal(FLHoloA + $FLHoloA):rotate(ang(0,-FSpin,0))*Third
FRVel = FRWheel:toLocal(FRHoloA + $FRHoloA):rotate(ang(0,FSpin,0))*Third
RLVel = RLWheel:toLocal(RLHoloA + $RLHoloA):rotate(ang(0,-RSpin,0))*Third
RRVel = RRWheel:toLocal(RRHoloA + $RRHoloA):rotate(ang(0,RSpin,0))*Third


     FrontVelX =  (FLVel:x() + FRVel:x())/2
     RearVelX =  (RLVel:x() + RRVel:x())/2
     VelX = (FrontVelX + RearVelX)/2
     Speed = VelX * Sign
    
    FLVelY = FLVel:z()
    FRVelY = FRVel:z()
    RLVelY = RLVel:z()
    RRVelY = RRVel:z()


if(Speed > 20){MPH = round(toUnit("mph",Speed),1)}elseif(MPH){MPH = 0}

# -- Suspension -- #

            RangerFL = rangerOffsetHull(HeightSum,FLHoloA,-Up,FBox)
            FrontL =  RangerFL:distance()
             

            RangerFR = rangerOffsetHull(HeightSum,FRHoloA,-Up,FBox)
            FrontR =  RangerFR:distance()
                        

            RangerRL = rangerOffsetHull(HeightSum,RLHoloA,-Up,RBox)
            RearL =  RangerRL:distance()
            

            RangerRR = rangerOffsetHull(HeightSum,RRHoloA,-Up,RBox)
            RearR =  RangerRR:distance() 

       
#holoPos(17,FLHoloA-FrontL*Up)

    if(Clutch != 1 & EngineRPM > 1000){Wobble = clamp(EngDelta*2,0,1-Clutch)}
 
                      
                    FLSus = HeightSum - FrontL
                    FRSus = HeightSum - FrontR
                    RLSus = HeightSum - RearL
                    RRSus = HeightSum - RearR
                    

#FrontNormal = 1-(RangerFL:hitNormal() + RangerFR:hitNormal())/2
#RearNormal = 1-(RangerRL:hitNormal() + RangerRR:hitNormal())/2

# -- Driving Code -- #

    
if(Active & !Flood){
    


if(S){Hold = clamp(Hold + S*0.1,0,1)}elseif(Hold){Hold = 0}

    


if(Auto){
if($Shift|$Alt){Mode = clamp(Mode+(Shift-Alt),0,3),Cruise=0,
                    if(Mode==1){Gear=-1}
                    elseif(Mode==3 & !Gear){Gear=1}
                    elseif(!Mode | Mode==2){Gear=0}
                }
if(Mode==3){
    if(Auto == 1){# Normal
        if((ActualRPM*Ratio) > ChangeUp ){Gear = clamp(Gear+1,1,5)}
        elseif(EngineRPM  < ChangeDown | Torque > 599){Gear = clamp(Gear-1,1,5)}
    }else{        # CVT
        Ratio = Ratio + (clamp((1800+Diff*2000)/WheelRPM,2,11)*0.4 - Ratio)*0.01
    }


#Cruise Control#
if(GearClk == 3){
if(R & $R){Cruise = !Cruise, CruiseSpeed=MPH}
if(Cruise & !W){CruiseDiff = clamp((CruiseSpeed - MPH)*0.5,-1,1)
if(!inrange(CruiseSpeed - MPH,-10,10)){Cruise=0}}elseif(CruiseDiff){CruiseDiff = 0}
}
}

}
else{
if($Shift|$Alt){Gear = clamp(Gear+(Shift-Alt),-1,5)}
}

if(changed(Gear)){Sign = sign(Gear),Ratio = (11-Gear*GearRatio*Sign)*0.4,GearClk=0}
#### GearStick ####

if(!GearClk){
  
  if(Clutch>0){Clutch-=0.2}else{
    
    if(GearP > 1){GearP--}
    elseif(GearP < -1){GearP++}
    else{
    
    GearClk = 1
    GearS=0
    soundStop(20)
    soundStop(21)
}

}   
}elseif(GearClk == 1){
if(Gear > 1 & $GearClk & Clutch == 0){soundPlay(10,0,ShiftSound)}

if($GearClk & Gear < 0){soundPlay(6,0,"vehicles/tank_turret_loop1.wav")}
elseif($GearClk & Gear >= 0){soundStop(6)}
    if(Gear == 1 | Gear == 2){GearR-=(GearR > -5),if(GearR == -5){GearClk=2}}
    elseif(Gear == 0 | Gear == 3 | Gear == 4){GearR-=sign(GearR),if(GearR == 0){GearClk=2}}
    elseif(Gear == 5 | Gear == -1){GearR+=(GearR < 5),if(GearR == 5){GearClk=2}
    

        }

        if(changed(GearR)&GearS==0){timer("Gear",10),GearS=1}
        elseif(changed(GearP)&GearS==0){timer("Gear2",10),GearS=1}
        
        
}elseif(GearClk != 3){

    if(Gear == 1 | Gear == 3 | Gear == 5){GearP+=(GearP < 8)}
    elseif(Gear){GearP-=(GearP > -8)}
    
    if(GearP == 8 | GearP == -8 | !Gear){
        
        if(Clutch | !Clutch){GearClk = 3}

    }
}


    if(changed(GearP)|changed(GearR)){
        if(Auto){holoAng(9,Body:toWorld(ang(16-Mode*8,0,0)))}
        else{holoAng(9,Body:toWorld(ang(GearP*2,0,GearR*2)))}
    }

if(RWD & !FWD){ActualRPM = Sign * (RearVelX/(0.15 * Pi))}
elseif(FWD & !RWD){ActualRPM = Sign * (FrontVelX/(0.15 * Pi))}
else{ActualRPM = Sign * (VelX/(0.15 * Pi))}


    

 if(Gear & !Starter){
    
   
      if(GearClk == 3){
        
        
         Gas = (W + CruiseDiff)*6
            
          if(ActualRPM * Ratio < 6000 & Gas & !S){
        DesiredAccel = (Gas-2) 
}elseif(S){DesiredAccel = Accel}
elseif(Speed>10){DesiredAccel = -1}
else{DesiredAccel = 0}



    Accel =  $Speed *sign(Speed)
    if(Speed < 100 & Space & W){Diff = -sign(Speed)*2}
    else{Diff = clamp(DesiredAccel - Accel,-2,4)}

   
# -- Wheelspin -- #

if(!Clutch | (FWD&!FrontSkid) | (RWD&!RearSkid)){NeededRPM = ActualRPM} 
elseif(Clutch){
    
if(!S){

    NeededRPM = ( ActualRPM  * (1-Clutch)  + ((EngineRPM+W*100)/Ratio) * (Clutch))
}else{NeededRPM = 0}

} 
     
# -- Wheels -- #  

    WheelFriction = (NeededRPM*(1-Hold) - WheelRPM)
    WheelRPM+= clamp(WheelFriction,-50,50)
#WheelRPM = NeededRPM

    
        if(abs(Gear)!=1){
            Torque =  1800/Ratio - WheelRPM
    }elseif(Torque){Torque = 0}
    
# -- Clutch -- #

if(Torque < 600){
    if(Space & Speed >=100){Clutch = 0}
    elseif(WheelRPM > 500){Clutch = clamp((WheelRPM*Ratio)/1600,0,1)}
    else{Clutch = clamp((WheelRPM*Ratio*W)/1600,W*0.1,1)}
 }else{Clutch = 0}

# -- Force -- # 1
if(Clutch){ 
    MaxForce = abs(Diff)
    Forcea = clamp(Forcea + Diff*0.01, -MaxForce,MaxForce)
   
       
}elseif(Forcea){Forcea = 0}
  
 Force = Forcea * Power *  Sign

# -- Engine -- #

        if(Clutch < 1){
            if(!Clutch){EngineFriction = -20}else{
    EngineFriction = ((900 + Forcea*2000)*(1-Clutch) + WheelRPM*Ratio*Clutch) - EngineRPM 
    }

        
    }else{
     EngineFriction = WheelRPM*Ratio - EngineRPM
    
   # if(FWD*abs(FrontVel) < MaxFrontGrip & RWD*abs(Force/100) < MaxRearGrip){
  #  EngineFriction = ActualRPM*Ratio - EngineRPM} 
   #else{EngineFriction = Force * 500 - EngineRPM}
    }
    




        }else{WheelRPM = ActualRPM,Force=0, EngineFriction = -10}
}else{Force=0,if(inrange(EngineRPM,900,6000)){EngineFriction = (W-0.1) * 150}}


if(EngineRPM <= 900){EngineRPM+=1}elseif(EngineRPM >= 6000){EngineRPM-=150}else{

EngineRPM = EngineRPM + clamp(EngineFriction,-50,50)}


if(!(FrontSkid & S)){FrontWheelX = FrontVelX}
else{FrontWheelX = 0}
    
if(!(RearSkid & S)){RearWheelX = RearVelX}
else{RearWheelX = 0}


if(FWD & Gear){
FWheelRPM =  ((WheelRPM * Pi)  / FWheelScale)*Sign
}else{FWheelRPM = (FrontWheelX / FWheelScale)*6}


if(RWD & Gear & !Space){
RWheelRPM =  ((WheelRPM * Pi)  / RWheelScale)*Sign
}else{RWheelRPM = !Space * (RearWheelX / RWheelScale)*6}

       
FSpin=angnorm(FSpin+(FWheelRPM))
RSpin=angnorm(RSpin+(RWheelRPM))
    
###################################################################################### 

if(!inrange(Starter,1,40)){
if(Starter){EngSnd = EngineRPM/1000}
else{
    
    EngSnd = EngineRPM/10000
}

if(EngineRPM < 6000){EngDelta = clamp($EngineRPM,0,30)}
if(Starter|EngDelta > 2){soundVolume(2,EngSnd+EngDelta*0.03),soundVolume(3,0),soundPitch(2,EngineRPM/60)}
else{soundVolume(3,EngSnd),soundVolume(2,0), soundPitch(3,EngineRPM/30)}
}else{soundVolume(2,0),soundVolume(3,0)}


         
    if(Gear < 0){
soundPitch(6,50+WheelRPM*0.02)
soundVolume(6,clamp(WheelRPM*0.001,0,0.5))}

}elseif($Active|$Flood){EngineRPM = 0,WheelRPM = 0, Force = 0, Diff = 0, Hold = 1,soundStop(2),soundStop(3),soundStop(6)}
else{Accel =  $Speed}

# -- END of Driving Code -- #
   
 
    if(!inrange(Speed,-20,20) | !Active){Stable = -Hold}
    elseif(!Gas){Stable = -Hold - 0.05}
                        
    if(Stable){
        FrontBrake = Stable * FrontVelX
        RearBrake = (Stable - Space) * RearVelX
        }
        
        
FForceX = Force*FWD + FrontBrake
RForceX = Force*RWD + RearBrake    

FLForce = abs(FForceX)+abs(FLVelY)+DeltaTurn
FRForce = abs(FForceX)+abs(FRVelY)+DeltaTurn
RLForce = abs(RForceX)+abs(RLVelY)
RRForce = abs(RForceX)+abs(RRVelY)



FrontLUp = clamp(FLSus +  $FLSus*4,0,HeightSum)
MaxFLGripA = FrontGrip * FrontLUp
FLSkid = (abs(FForceX) > MaxFLGripA | abs(FLVelY+DeltaTurn) > MaxFLGripA)
if(FLSkid){MaxFLGrip = MaxFLGripA - clamp((FLForce-MaxFLGripA),0,MaxFLGripA/1.5)}
elseif(MaxFLGrip != MaxFLGripA){MaxFLGrip = MaxFLGripA}

FrontRUp = clamp(FRSus +  $FRSus*4,0,HeightSum)
MaxFRGripA = FrontGrip * FrontRUp
FRSkid = (abs(FForceX) > MaxFRGripA | abs(FRVelY+DeltaTurn) > MaxFRGripA)
if(FRSkid){MaxFRGrip = MaxFRGripA - clamp((FRForce-MaxFRGripA),0,MaxFRGripA/1.5)}
elseif(MaxFRGrip != MaxFRGripA){MaxFRGrip = MaxFRGripA}

RearLUp =  clamp(RLSus + $RLSus*4,0,HeightSum)
MaxRLGripA = RearGrip * RearLUp
RLSkid = (abs(RForceX) > MaxRLGripA | abs(RLVelY) > MaxRLGripA)
if(RLSkid){MaxRLGrip = MaxRLGripA - clamp((RLForce-MaxRLGripA),0,MaxRLGripA/1.5)}
elseif(MaxRLGrip != MaxRLGripA){MaxRLGrip = MaxRLGripA}

RearRUp =  clamp(RRSus + $RRSus*4,0,HeightSum)
MaxRRGripA = RearGrip * RearRUp
RRSkid = (abs(RForceX) > MaxRRGripA | abs(RRVelY) > MaxRRGripA)
if(RRSkid){MaxRRGrip = MaxRRGripA - clamp((RRForce-MaxRRGripA),0,MaxRRGripA/1.5)}
elseif(MaxRRGrip != MaxRRGripA){MaxRRGrip = MaxRRGripA}



    FRotate = Angles+ang(0,Turn*FWSteer,0)
    RRotate = Angles+ang(0,Turn*RWSteer,0)
    

    Body:applyOffsetForce(( (vec(clamp(vec2(FForceX,FLVelY)/16,-MaxFLGrip ,MaxFLGrip),
    FrontLUp + Wobble)-WVel) * Mass):rotate(FRotate),FLHoloA-FrontL*Up) 
    

    Body:applyOffsetForce(( (vec(clamp(vec2(FForceX,-FRVelY)/16 ,-MaxFRGrip ,MaxFRGrip),
    FrontRUp)-WVel) * Mass):rotate(FRotate),FRHoloA-FrontR*Up) 
    

    Body:applyOffsetForce(( (vec(clamp(vec2(RForceX,RLVelY)/16,-MaxRLGrip,MaxRLGrip),
    RearLUp)-WVel) * Mass):rotate(RRotate),RLHoloA-RearL*Up)
  
  
    Body:applyOffsetForce(( (vec(clamp(vec2(RForceX,-RRVelY)/16,-MaxRRGrip,MaxRRGrip),
    RearRUp)-WVel) * Mass):rotate(RRotate),RRHoloA-RearR*Up) 
    


            
# -- Wheel Animations -- #


    FLHoloPos = FLHoloA + Up * (FWheelZ-FrontL)
    FRHoloPos = FRHoloA + Up * (FWheelZ-FrontR)
    RLHoloPos = RLHoloA + Up * (RWheelZ-RearL)
    RRHoloPos = RRHoloA + Up * (RWheelZ-RearR)           

    
    holoPos(1,FLHoloPos)
    holoPos(2,FRHoloPos)
    holoPos(3,RLHoloPos)
    holoPos(4,RRHoloPos)


   AbsPosTurn = abs(Turn/90)
if(FWSteer){

FrontLeftTurn = Turn + FToe * AbsPosTurn
FrontRightTurn = Turn - FToe * AbsPosTurn

}

if(RWSteer){

RearLeftTurn = -Turn + RToe * AbsPosTurn
RearRightTurn = -Turn - RToe * AbsPosTurn

}
       
         holoAng(1,Body:toWorld(ang(FSpin,FrontLeftTurn,90)))
         holoAng(2,Body:toWorld(ang(FSpin,FrontRightTurn,-90)))
         holoAng(3,Body:toWorld(ang(RSpin,RearLeftTurn,90)))
         holoAng(4,Body:toWorld(ang(RSpin,RearRightTurn,-90)))
        
    

        
 

if(FBurnout & $FBurnout){Body:soundPlay(43,0,"/sfx/skidding.wav")#"vehicles/v8/skid_normalfriction.wav")
    
        holoPos(13,FLHoloPos-Up*FTyreSize*6.2)
        holoPos(14,FRHoloPos-Up*FTyreSize*6.2)
        holoParent(13,holoEntity(1)), holoParent(14,holoEntity(2))
        holoEntity(13):setTrails(FTyreSize*5,FTyreSize*100,1.5,"trails/smoke",vec(220,220,240),100)
        holoEntity(14):setTrails(FTyreSize*5,FTyreSize*100,1.5,"trails/smoke",vec(220,220,240),100)  
            
}elseif(!FBurnout & $FBurnout){holoUnparent(13),holoUnparent(14),soundStop(3)}

if(RBurnout & $RBurnout){Body:soundPlay(44,0,"/sfx/skidding.wav")#ambient/gas/steam_loop1.wav
    
        holoPos(15,RLHoloPos-Up*RTyreSize*6.2)
        holoPos(16,RRHoloPos-Up*RTyreSize*6.2)           
        holoParent(15,holoEntity(3)), holoParent(16,holoEntity(4))
        holoEntity(15):setTrails(RTyreSize*5,RTyreSize*100,1.5,"trails/smoke",vec(220,220,240),100)
        holoEntity(16):setTrails(RTyreSize*5,RTyreSize*100,1.5,"trails/smoke",vec(220,220,240),100)  

}elseif(!RBurnout & $RBurnout){holoUnparent(15),holoUnparent(16),soundStop(4)}

#if(MaxFGrip < MaxFrontGrip){soundPitch(3,(MaxFrontGrip-MaxFGrip)*40-80)}
#if(MaxRGrip < MaxRearGrip){soundPitch(4,(MaxRearGrip-MaxRGrip)*40-80)}
     #soundPitch(3,(MaxGrip-MaxRGrip)*1)         
# -- END of Wheel Animations -- #

if(abs(Accel) > 40){Crash = 1}elseif(Crash){Crash = 0}
if(Crash & $Crash){soundPlay(30,0,"vehicles/v8/vehicle_impact_heavy1.wav"),if(Lock){soundPlay(31,0,"/npc/turret_floor/alarm.wav")}}


if(Mouse2 & $Mouse2){CamActive = !CamActive,Camera:setNumber("Activated",CamActive),Camera:setEntity("Parent",Body)}

if(CamActive & Active){
     CamAngle = CamAngle + angnorm(Angles+ang(0,-90,0) + Pod:toLocal(Driver:eyeAngles())-CamAngle)*0.2

       ZoomIn= Wirelink:number("NextWeapon")
       ZoomOut= Wirelink:number("PrevWeapon")
    CamPos =  Body:toWorld(vec(0,0,50))+CamAngle:forward()*CamZoom
    if(CamPos:isInWorld()){
Zoom = clamp(Zoom - (ZoomIn-ZoomOut)*10,-500,0)
CamZoom  -= (CamZoom - Zoom)*0.1
                }else{CamZoom=10}
                    
                    
                    Camera:setVector("Position",CamPos)
                    Camera:setAngle("Angle",CamAngle)
                    
                }
    
}#end tick

if(clk("Gear")&GearS){Chip:soundPlay(20,0,"physics/cardboard/cardboard_box_impact_soft5.wav")}
elseif(clk("Gear2")&GearS){Chip:soundPlay(21,0,"physics/cardboard/cardboard_box_impact_soft4.wav")}




}


# -- CIPHER_ULTRA 2012 -- #

