//Focus on the car used by the script owner
rcar $car=get scriptowner car
rcarmodel $temoin
rcarmodel $temoin2
shared float $esp
float $slipangle0
float $slipangle1
float $slipangle2
float $slipangle3
float $brake0 = 4000
float $brake1 = 4000
float $brake2 = 3000
float $brake3 = 3000
float $brake0stat = 4000
float $brake1stat = 4000
float $brake2stat = 3000
float $brake3stat = 3000
float $wheelfront = 0.25
float $wheelrear = 0.25
float $wheel0_vel
float $wheel1_vel
float $wheel2_vel
float $wheel3_vel
float $spin = -0.12
float $spin_rear = -0.10
float $brake
float $speed
float $steerangle
float $throttle
float $frequence = 0.0025
float $gforce[3]
float $gforce_lat
float $finalbrake
float $slipratio0
float $slipratio1
float $slipratio2
float $slipratio3
float $finalthrottle
float $unthrottle = 0
int $mode = 0
float $compteur = 0
int $rpm
string $reload = "reload scripts"
int $load = 0
float $braking
while 1
{
	$brake = get $car brake
	$throttle = get $car throttle
	$rpm = get $car rpm
	$temoin2 = get generic model 49 of $car
	$temoin = get generic model 24 of $car
	set $temoin scriptcontrolled 1 //when ESP kicks in
	set $temoin2 scriptcontrolled 1 //Warning message when it's disabled
	$steerangle = get $car steerangle // y<0 means right direction, > 0 means left, same for lateral gforce
	$gforce = get $car gforce
	$gforce_lat=$gforce.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
	$wheel0_vel = get system "car0.wheel0.rotvel"
	$wheel1_vel = get system "car0.wheel1.rotvel"
	$wheel2_vel = get system "car0.wheel2.rotvel"
	$wheel3_vel = get system "car0.wheel3.rotvel"
	$speed=get $car tachovelocity

	if $load == 0
	{
		//send "reload scripts" to console
		$load = 1
	}

	//activation
	if is key (65) pressed //Key A
	{
		if $mode == 1
		{
		$mode = 0
		}
		else
		{
		$mode = 0
		}
		wait(200)
  	}
	if $mode == 0
	{
		$esp = 0
		$finalbrake = $brake
		$unthrottle = 0
		set $temoin2 position float[3]{0.0,0.0,0.0}
	}
	if $mode > 0
	{
		set $temoin2 position float[3]{0,-10,0}
	}
	if $speed > 12
	{
		$mode = 0
	}

	// ESP part
 	if $esp > 1
	{
		$esp = 1
	}	
	if $speed < 4 //disable system under 14km/h
	{
		$esp = 0
		$unthrottle = 0 
	}
	if $rpm < 850
	{
		$finalthrottle = $throttle
	}
	//front axle
	if $slipangle0 > $wheelfront or $slipangle1 > $wheelfront and $brake < 0.05 and $mode > 0
	{
			$compteur++
			if $compteur > 1
			{
					set $temoin position float[3]{0.0,-10.0,0.0}
					$compteur = 0
			}
			else
			{
				set $temoin position float[3]{0.0,0.0,0.0}
			}
			if $gforce_lat > 0.05 and $steerangle > 0.05 //wheel2 is braked, left oversteer
			{
				$esp+=$frequence
				$brake0=0
				$brake1=0
				$brake3=0
				$finalthrottle=$finalthrottle*0.00
				if $slipratio2 < $spin_rear
				{
					$brake2-=100000*$frequence
					if $brake2 < 0
					{
						$brake2=0
					}
				}
				else
				{
					$brake2+=100000*$frequence
					if $brake2 > $brake2stat
					{
						$brake2=$brake2stat
					}
				}
				wait(0.1)		
			}
			if $gforce_lat > 0.05 and $steerangle < 0.05 //wheel3 is braked, right oversteer
			{
				$esp+=$frequence
				$brake0=0
				$brake1=0
				$brake2=0
				$finalthrottle=$finalthrottle*0.00
				if $slipratio3 < $spin_rear
				{
					$brake3-=100000*$frequence
					if $brake3 < 0
					{
						$brake3=0
					}
				}
				else
				{
					$brake3+=100000*$frequence
					if $brake3 > $brake3stat
					{
						$brake3=$brake3stat
					}
				}
				wait(0.1)		
			}
			if $gforce_lat < - 0.05 and $steerangle < -0.05 //wheel3 is braked, right oversteer
			{
				$esp+=$frequence
				$brake0=0
				$brake1=0
				$brake2=0
				$finalthrottle=$finalthrottle*0.00
				if $slipratio3 < $spin_rear
				{
					$brake3-=100000*$frequence
					if $brake3 < 0
					{
						$brake3=0
					}
				}
				else
				{
					$brake3+=100000*$frequence
					if $brake3 > $brake3stat
					{
						$brake3=$brake3stat
					}
				}
				wait(0.1)
			}
			if $gforce_lat < - 0.05 and $steerangle > 0.05 //wheel2 is braked, right oversteer
			{
				$esp+=$frequence
				$brake0=0
				$brake1=0
				$brake3=0
				$finalthrottle=$finalthrottle*0.00
				if $slipratio3 < $spin_rear
				{
					$brake2-=100000*$frequence
					if $brake2 < 0
					{
						$brake2=0
					}
				}
				else
				{
					$brake2+=100000*$frequence
					if $brake2 > $brake2stat
					{
						$brake2=$brake2stat
					}
				}
				wait(0.1)
			}
	}
	//rear axle
	if $slipangle2 > $wheelrear or $slipangle2 > $wheelrear and $brake < 0.05 and $mode > 0
	{
			$compteur++
			if $compteur > 1
			{
					set $temoin position float[3]{0.0,-10.0,0.0}
					$compteur = 0
			}
			else
			{
				set $temoin position float[3]{0.0,0.0,0.0}
			}
			if $gforce_lat > 0.05 //wheel1 is braked, left understeer
			{
				$esp+=$frequence
				$brake0=0
				$brake1+=40000*$frequence
				$brake2=0
				$brake3=0
				$finalthrottle=$finalthrottle*0.00
				if $slipratio1 < -1*$spin
				{
					$brake1-=400000*$frequence
					if $brake1 < 0
					{
						$brake1=0
					}
				}
				else
				{
					$brake1+=400000*$frequence
					if $brake1 > $brake1stat
					{
						$brake1=$brake1stat
					}
				}
				wait(0.1)
			}
			if $gforce_lat < - 0.05 //wheel0 is braked, right understeer
			{
				$esp+=$frequence
				$brake0+=40000*$frequence
				$brake1=0
				$brake2=0
				$brake3=0
				$finalthrottle=$finalthrottle*0.00
				if $slipratio0 < -1*$spin
				{
					$brake0-=100000*$frequence
					if $brake0 < 0
					{
						$brake0=0
					}
				}
				else
				{
					$brake0+=100000*$frequence
					if $brake0 > $brake0stat
					{
						$brake0=$brake0stat
					}
				}
				wait(0.1)
			}
	}
	if $slipangle0 < $wheelfront and $slipangle1 < $wheelfront and $slipangle2 < $wheelrear and $slipangle3 < $wheelrear
	{
		$esp=0
		$brake0=$brake0stat
		$brake1=$brake1stat
		$brake2=$brake2stat
		$brake3=$brake3stat
		$finalthrottle=$throttle
		set $temoin position float[3]{0.0,-10.0,0.000}
	}
	if $brake > 0.1
	{
		$brake0=$brake0stat
		$brake1=$brake1stat
		$brake2=$brake2stat
		$brake3=$brake3stat
		//$mode = 1
	}



	//TC part
	if $speed > 4  and $mode > 0 and $throttle > 0.10
	{ 	
		//front axle
		if abs($wheel0_vel+$wheel1_vel) > 1.04*abs($wheel2_vel+$wheel3_vel)
		{
			$unthrottle+=0.1
			$esp+=0.01
			set $temoin position float[3]{0.0,0.0,0.0}
			if abs($wheel0_vel) > abs($wheel2_vel+3)
			{
				$brake0=100000*$frequence
				$brake1=0
				$brake2=0
				$brake3=0
			}
			if abs($wheel1_vel) > abs($wheel3_vel+3)
			{
				$brake0=0
				$brake1=100000*$frequence
				$brake2=0
				$brake3=0
			}
		}
		else
		{
			$unthrottle = 0
		}
		if abs($wheel2_vel+$wheel3_vel) > 1.202*abs($wheel0_vel+$wheel0_vel)
		{
			$braking+=0.1
			set $temoin position float[3]{0.0,0.0,0.0}
			if abs($wheel2_vel) > abs($wheel0_vel)
			{
				$brake2=5000000*$frequence
				$brake1=0
				$brake0=0
				$brake3=0
			}
			if abs($wheel3_vel) > abs($wheel1_vel)
			{
				$brake0=0
				$brake3=5000000*$frequence
				$brake2=0
				$brake1=0
			}
		}
		else
		{
			$braking = 0
		}
	}
	if $speed < 4
	{
		$braking = 0
	}

	//Abs part


	if $brake > 0 and $speed > 5
	{
		float $unbrake_front = 0.25
		float $unbrake_rear = 0.20
		if $slipratio0 < $spin
		{
			$brake0*=$unbrake_front
			if $brake0 < 0
			{
				$brake0=0
			}
			$esp=0
		}
		else
		{
			$brake0/=$unbrake_front
 			$esp=0
			if $brake0 > $brake0stat
			{
				$brake0=$brake0stat
			}
		}
		if $slipratio1 < $spin
		{
			$brake1*=$unbrake_front
			if $brake1 < 0
			{
				$brake1=0
			}
			$esp=0
		}
		else
		{
			$brake1/=$unbrake_front
			$esp=0
			if $brake1 > $brake1stat
			{
				$brake1=$brake1stat
			}
		}
		if $slipratio2 < $spin_rear
		{
			$brake2*=$unbrake_rear
			if $brake2 < 0
			{
				$brake2=0
			}
			$esp=0
		}
		else
		{
			$brake2/=$unbrake_rear
			$esp=0
			if $brake2 > $brake2stat
			{
				$brake2=$brake2stat
			}
		}
		if $slipratio3 < $spin_rear
		{
			$brake3*=$unbrake_rear
			if $brake3 < 0
			{
				$brake3=0
			}
			$esp=0
		}
		else
		{
			$brake3/=$unbrake_rear
			$esp=0
			if $brake3 > $brake3stat
			{
				$brake3=$brake3stat
			}
		}
		if $esp > 0 and $mode > 0
		{
			set $temoin position float[3]{0.0,0.0,0.00}
			$compteur++
			if $compteur > 1
			{
				set $temoin position float[3]{0.0,-10.0,0.00}	
				$compteur = 0
			}
		}
	}
	$finalbrake=$brake+$esp+$braking
	if $finalbrake > 1
	{
		$finalbrake = 1
	}
	if $finalthrottle < 0
	{
		$finalthrottle = 0
	}
	$finalthrottle=$throttle-$unthrottle
	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
	interrupt
}