//Focus on the car used by the script owner
rcar $car=get scriptowner car
rcar $localcar = get local car
//Temoins
rcarmodel $temoin
rcarmodel $asr_dispo_d
rcarmodel $asr_dispo_a
//Capteurs ESP
float $slipratio0
float $slipratio1
float $slipratio2
float $slipratio3
float $slipangle0
float $slipangle1
float $slipangle2
float $slipangle3
float $gforce[3]
float $gforce_lat
float $lacet
float $brake
float $speed
float $steerangle
float $throttle
int $rpm
float $PI = 3.141592654

//																		Seuils 
//Quand l'ESP augmente son action (derepage trop long)
int $urgence = 400
//Seuil de glissement des roues avant intervention de l'ESP
float $wheelfront = 9.22
$wheelfront = ($wheelfront*$PI)/180.0
float $wheelrear = 8.00
$wheelrear = ($wheelrear*$PI)/180.0
//Seuil de declanchement de l'ABS
float $spin = 0.200
float $spin_rear = 0.200
//Seuil de patinage
float $ratio = 1.10
//Seuil de declanchement de l'ESP (inactif en ligne droite..)
float $seuil_lateral = 0.001
//Combien de % de freinage par iteration
float $frequence = 0.35

//																		Valeurs statiques

//Valeur de freinage pour chaque roue
float $brake0
float $brake1
float $brake2
float $brake3
//Valeur initiales de freinage pour chaque roue (redefinies pour les chassis)
float $brake_front_stat = 3100
float $brake_rear_stat = 1300
float $unbrake_front = 1.10
float $unbrake_rear = 1.50


//Variables
int $right_oversteer = 0
int $right_understeer = 0
int $left_oversteer = 0
int $left_understeer = 0
float $esp
float $finalbrake
float $finalthrottle
float $unthrottle
float $compteur = 0
int $mode = 1
int $action
int $patinage = 0
int $csv_dispo = 0
int $asr_dispo = 0
int $abs_dispo = 1
int $esp_dispo = 0
int $tc = 0
float $wait = 0.005
while 1
{
	if $car == $localcar
	{
		$brake = get $car brake
		$throttle = get $car throttle
		$rpm = get $car rpm
		$asr_dispo_d = get generic model 22 of $car
		$asr_dispo_a = get generic model 23 of $car
		$temoin = get generic model 9 of $car
		//set $temoin scriptcontrolled 1
		//set $asr_dispo_d scriptcontrolled 1
		//set $asr_dispo_a scriptcontrolled 1
		$steerangle = get $car steerangle // y<0 means right direction, > 0 means left, same for lateral gforce
		$gforce = get $car gforce
		$gforce_lat=$gforce.x
		//$lacet = get $car rotvelocity.x
		$slipangle0 = get $car wheel 0 slipangle
		$slipangle1 = get $car wheel 1 slipangle
		$slipangle2 = get $car wheel 2 slipangle
		$slipangle3 = get $car wheel 3 slipangle
		$slipangle0 = abs($slipangle0)
		$slipangle1 = abs($slipangle1)
		$slipangle2 = abs($slipangle2)
		$slipangle3 = abs($slipangle3)
		$slipratio0 = get $car wheel 0 slipratio
		$slipratio1 = get $car wheel 1 slipratio
		$slipratio2 = get $car wheel 2 slipratio
		$slipratio3 = get $car wheel 3 slipratio
		$speed=get $car tachovelocity * 3.61
		float $maxsteerangle = get $car max steerangle
		int $derapage = 0
		$derapage = $left_oversteer+$left_understeer+$right_oversteer+$right_understeer + $patinage
		$ratio = 1.17 + abs(0.20*$steerangle/$maxsteerangle)
		float $offset_z=-0.0020
		
		
		$finalthrottle=$throttle
		$right_oversteer = 0
		$right_understeer = 0
		$left_oversteer = 0
		$left_understeer = 0
		set $temoin position float[3]{0.0,-10.0,$offset_z}
		
		
		//activation
		if is key (65) pressed //Key A
		{
			if $mode <1 and $speed < 38
			{
				$compteur = 0
			}
			else
			{
				$mode = 0
			}
			$compteur = 0
			wait(300)
		}
		if $mode == 0
		{
			$compteur += 1
			$tc = 0
			set $temoin position float[3]{0.0,-100.0,0.0}
			set $asr_dispo_d position float[3]{0.0,0.0,-0.001}
			set $asr_dispo_a position float[3]{0,-10,0.0}
			if $compteur > 4000
			{
				set $asr_dispo_d position float[3]{0,-10,0.0}
			}
		}

		if $mode == 1
		{
			$compteur++
			set $asr_dispo_d position float[3]{0,-10,0.0}
			set $asr_dispo_a position float[3]{0,0.0,-0.001}
			if $compteur > 4000
			{
				set $asr_dispo_a position float[3]{0,-10,0.0}
			}
		}
		if $speed > 38
		{
			$tc = 1
			$mode = 1
			$compteur++
		}
		if $speed > 37.99 and $mode == 0
		{
			$compteur = 0
		}
		// ESP part
		if $esp > 1
		{
			$esp = 1
		}	
		if abs($speed) < 14 //disable system under 14km/h
		{
			$esp = 0
			$finalthrottle = $throttle
			$unthrottle = 0.0
			$derapage = 0.0
			$action = 0
		}
		if $rpm < 900
		{
			$finalthrottle = $throttle
			$unthrottle = 0
		}
		
		
		
		
		
		
		
		
		
		//front axle
		if $slipangle0 > $wheelfront or $slipangle1 > $wheelfront and $mode > -10 and $csv_dispo != 0 and $esp_dispo !=0
		{
			$action++
			//echo "ESP, sous virage"
			if $gforce_lat > $seuil_lateral and $steerangle > 0.05 //wheel2 is braked, left oversteer
			{
				$left_understeer = 1
				$esp=$esp+$frequence
				$brake0=0
				$brake1=0
				$brake2=$brake_rear_stat
				$brake3=0
				$unthrottle-=$frequence
			}
			if $gforce_lat > $seuil_lateral and $steerangle < 0.05 //wheel3 is braked, right oversteer
			{
				$left_oversteer = 1
				$esp=$esp+$frequence
				$brake0=0
				$brake1=$brake_front_stat
				$brake2=0
				$brake3=0
				$unthrottle-=$frequence
			}
			if $gforce_lat < -1.0*$seuil_lateral and $steerangle < -0.05 //wheel3 is braked, right oversteer
			{
				$right_understeer = 1
				$esp=$esp+$frequence
				$brake0=0
				$brake1=0
				$brake2=0
				$brake3=$brake_rear_stat
				$unthrottle-=$frequence
			}
			if $gforce_lat < -1.0*$seuil_lateral and $steerangle > 0.05 //wheel2 is braked, right oversteer
			{
				$right_oversteer = 1
				$esp=$esp+$frequence
				$brake0=0
				$brake1=0
				$brake2=$brake_rear_stat
				$brake3=0
				$unthrottle-=$frequence
			}
		}
		//rear axle
		if abs($slipangle2) > $wheelrear or abs($slipangle3) > $wheelrear and $mode > -10 and $esp_dispo !=0
		{
			//echo "ESP, survirage"
			if $gforce_lat > $seuil_lateral //wheel1 is braked, left oversteer
			{
				$left_oversteer = 1
				$esp=$esp+$frequence
				$brake0=0
				$brake1=$brake_front_stat
				$brake2=0
				$brake3=0
				$unthrottle-=$frequence
			}
			if $gforce_lat < -1.0*$seuil_lateral //wheel0 is braked, right oversteer
			{
				$right_oversteer = 1
				$esp=$esp+$frequence
				$brake0=$brake_front_stat
				$brake1=0
				$brake2=0
				$brake3=0
				$unthrottle-=$frequence
			}
		}


		
		
		
		
		
		//TC part
		if $mode > 0 and abs($speed) > 12.0 and $throttle > 0.0 and $tc !=0 and $brake == 0.00 and $asr_dispo !=0
		{ 
			if abs($speed) > 12.0
			{
				if abs($slipratio0) > abs($spin)
				{
					//echo "roue0"
					$patinage = 1
					$esp+=0.25
					$brake0*=$unbrake_front
					$brake1=0
					$brake2=0
					$brake3=0
					$compteur++
					if $compteur > 1500
					{
						$unthrottle-=0.1*$frequence
					}
				}
				if abs($slipratio1) > abs($spin)
				{
					//echo "roue1"
					$patinage = 1
					$esp+=0.25
					$brake0=0
					$brake1*=$unbrake_front
					$brake2=0
					$brake3=0
					$compteur++
					if $compteur > 1500
					{
						//$unthrottle-=0.1*$frequence
					}
				}
				if abs($slipratio0) < abs($spin)and abs($slipratio1) < abs($spin)
				{
					$patinage = 0
				}
			}
		}
		if abs($speed) < 12.0
		{
			$derapage = 0
			$patinage = 0
			$esp = 0.0
		}
		
		
		
		


		//Abs part
		if abs($speed) > 12.0 and ($brake+$esp) > 0.00 and $abs_dispo !=0
		{
			if abs($slipratio0) > abs($spin)
			{
				$derapage = 1
				if $brake0 > 0
				{
					
					$brake0/=$unbrake_front
				}
			}
			if abs($slipratio0) < abs($spin)
			{
				if $brake0 < $brake_front_stat
				{
					$brake0*=$unbrake_front
				}
			}
			if abs($slipratio1) > abs($spin)
			{
				$derapage = 1
				if $brake1 > 0
				{
					$brake1/=($unbrake_front)
				}
			}
			else
			{
				if $brake1 < $brake_front_stat
				{
					$brake1*=$unbrake_front
				}
			}
			
			//Rear axle
			
			
			if abs($slipratio2) > abs($spin_rear)
			{
				$derapage = 1
				if $brake2 > 0
				{
					$brake2/=$unbrake_rear
				}
			}
			else
			{
				if $brake2 < $brake_rear_stat
				{
					$brake2*=$unbrake_rear
				}
			}
			if abs($slipratio3) > abs($spin_rear)
			{
				$derapage = 1
				if $brake3 > 0
				{
					$brake3/=$unbrake_rear
				}
			}
			else
			{
				if $brake3 < $brake_rear_stat
				{
					$brake3*=$unbrake_rear
				}
			}
		}
		if $derapage == 0
		{
			$esp=0.0
			$unthrottle = 0.0
			$action = 0
			$brake0=$brake_front_stat
			$brake1=$brake_front_stat
			$brake2=$brake_rear_stat
			$brake3=$brake_rear_stat
		}
		if $unthrottle < -1.0
		{
			$unthrottle = -1.0
		}
		$finalbrake=$brake+$esp
		if $finalbrake > 1
		{
			$finalbrake = 1
		}
		if $finalthrottle < 0
		{
			$finalthrottle = 0
		}
		
		
		//Display the light
		if $esp > 0 and $mode > 0
		{
			set $temoin position float[3]{0.0,0.0,$offset_z}
			$compteur++
			if $compteur %2 == 0
			{
				set $temoin position float[3]{0.0,-10.0,0.00}	
			}
		}
		set system "car0.wheel0.max_braking" to $brake0;
		set system "car0.wheel1.max_braking" to $brake1;
		set system "car0.wheel2.max_braking" to $brake2;
		set system "car0.wheel3.max_braking" to $brake3;
		set $car brake $finalbrake
		set $car throttle $finalthrottle+$unthrottle
		wait ($wait)
	}
	interrupt
}