rcar $car = get scriptowner car
rcar $localcar = get local car
//Static info
float $steerangle
float $maxsteerangle
float $PI = 3.141592654
float $max_rear_steer = 3.25
shared float $esp
shared int $right_oversteer
shared int $right_understeer
shared int $left_oversteer
shared int $left_understeer
float $steering = 0.01
//Variables
float $speed
float $currentsteer
float $current_rearsteer
float $rearsteer_target
float $effective_steer
float $final_steer
//When the steer cannot be modified
float $securit = 60
while 1
{
	if $car == $localcar
	{
		//Script
		$speed = get $car tachovelocity*3.60
		$steerangle = get $car steerangle
		$maxsteerangle = get $car max steerangle
		$currentsteer = abs(2.00*($steerangle/$maxsteerangle))
		$current_rearsteer = (get system "car0.wheel2.toe")
		$current_rearsteer = ($current_rearsteer*180.0)/$PI
		if abs($speed) > $securit and $esp == 0
		{
			$max_rear_steer = 2.00
			$rearsteer_target = (1.0*pow($currentsteer)to(1.00)*$max_rear_steer)
		}
		if abs($speed) < $securit and $esp == 0
		{
			$max_rear_steer = 3.25
			if $steerangle < 0
			{
				$rearsteer_target = (pow($currentsteer/0.5)to(1.00)*$max_rear_steer)
			}
			if $steerangle > 0
			{
				$rearsteer_target = (pow($currentsteer/0.5)to(1.00)*-$max_rear_steer)
			}
		}
		
		
		//Sur virage a droite, on braque les roues arrieres vers la gauche
		if $right_oversteer > 0 and $esp > 0
		{
			//echo "survirage 4C"
			$max_rear_steer = 3.25
			if abs($rearsteer_target) < abs($max_rear_steer)
			{
				$rearsteer_target=$max_rear_steer*(-0.50*$esp)
			}
			else
			{
				$rearsteer_target = $max_rear_steer
			}
		}
		//Sur virage a gauche, on braque les roues arrieres vers la droite
		if $left_oversteer > 0 and $esp > 0
		{
			//echo "survirage 4C"
			$max_rear_steer = 3.25
			if abs($rearsteer_target) < abs($max_rear_steer)
			{
				$rearsteer_target=$max_rear_steer*(0.50*$esp)
			}
			else
			{
				$rearsteer_target = $max_rear_steer
			}
		}
		
		//Sous virage a droite, on braque les roues arrieres vers la droite
		if $right_understeer > 0 and $esp > 0
		{
			$max_rear_steer = 3.25
			if abs($rearsteer_target) < abs($max_rear_steer)
			{
				$rearsteer_target=$max_rear_steer*(0.1250*$esp)
			}
			else
			{
				$rearsteer_target = $max_rear_steer
			}
			//echo "ca va plus, ss virage"
		}
		//Sous virage a gauche, on braque les roues arrieres vers la gauche
		if $left_understeer > 0 and $esp > 0
		{
			$max_rear_steer = 3.25
			if abs($rearsteer_target) < abs($max_rear_steer)
			{
				$rearsteer_target=$max_rear_steer*(-0.1250*$esp)
			}
			else
			{
				$rearsteer_target = $max_rear_steer
			}
			//echo "ca va plus, ss virage"
		}
		

		if $effective_steer < $rearsteer_target
		{
			$effective_steer+=0.01
		}
		if $effective_steer > $rearsteer_target
		{
			$effective_steer-=0.01
		}
		
		if abs($effective_steer) > 3.250
		{
			if $effective_steer < -3.250 and $effective_steer < 0
			{
				$effective_steer = -3.250
			}
			if $effective_steer > 3.250
			{
				$effective_steer = 3.250
			}
		}
		$final_steer = ($effective_steer/180.0)*$PI
		set system "car0.wheel2.toe" to ($final_steer-0.003926)
		set system "car0.wheel3.toe" to ($final_steer+0.003926)
		//set system "car0.wheel2.toe" to 20
		//set system "car0.wheel3.toe" to 20
		wait (0)
	}
	interrupt
}