func float move(rcarmodel $targetObject, float $state, float $position_x, float $position_y, float $position_z, float $speed, float $max_x, float $max_y, float $max_z)
{
	float $position_x_n = $state*($max_x-$position_x) + $position_x
	float $position_y_n = 1.25*$state*($max_y-$position_y) + $position_y
	float $position_z_n = $state*($max_z-$position_z) + $position_z
	float $max_move=0
	if $position_x_n < $max_x
	{
		$position_x_n = $position_x_n - $speed
	}
	if $position_x_n > $max_x
	{
		$position_x_n = $position_x_n + $speed
	}
	if $position_y_n <= $max_y
	{
		$max_move=0
		$position_y_n = $position_y_n - 1*$speed
		if $max_move == 0
		{
			if $position_z_n > $max_z
			{
				$position_z_n = $position_z_n + 0*$speed*interval
			}
		}
	}
	if $position_y_n >= $max_y
	{
		$max_move=1
		$position_y_n = $position_y_n + 1*$speed
		if $max_move ==1
		{
			if $position_z_n < $max_z
			{
				$position_z_n = $position_z_n - 0*$speed*interval
			}
			if $position_z_n > $max_z
			{
				$position_z_n = $position_z_n + 0*$speed*interval
			}
		}
	}
	set $targetObject position float[3]{$position_x_n,$position_y_n,$position_z_n}
	return $position_x_n
}
func float move_and_rotate(rcarmodel $targetObject, float $state, float $position_x, float $position_y, float $position_z, float $speed, float $max_x, float $max_y, float $max_z,float $rotMaskx, float $rotMasky, float $rotMaskz, float $incRate, float $minAngle, float $maxAngle,float $angle)
{
	float $position_x_n = $state*($max_x-$position_x) + $position_x
	float $position_y_n = 1.25*$state*($max_y-$position_y) + $position_y
	float $position_z_n = $state*($max_z-$position_z) + $position_z
	float $max_move=0
	if $position_x_n < $max_x
	{
		$position_x_n = $position_x_n - $speed
	}
	if $position_x_n > $max_x
	{
		$position_x_n = $position_x_n + $speed
	}
	if $position_y_n <= $max_y
	{
		$max_move=0
		$position_y_n = $position_y_n - 1*$speed
		if $max_move == 0
		{
			if $position_z_n > $max_z
			{
				$position_z_n = $position_z_n + 0*$speed*interval
			}
		}
	}
	if $position_y_n >= $max_y
	{
		$max_move=1
		$position_y_n = $position_y_n + 1*$speed
		if $max_move ==1
		{
			if $position_z_n < $max_z
			{
				$position_z_n = $position_z_n - 0*$speed*interval
			}
			if $position_z_n > $max_z
			{
				$position_z_n = $position_z_n + 0*$speed*interval
			}
		}
	}
	float $targetAngle = $state*($maxAngle-$minAngle) + $minAngle
	if $targetAngle > $angle
	{
		$angle = $angle + $incRate
	}
	if $targetAngle < $angle
	{
		$angle = $angle - $incRate
	}
	if ($angle) < ($minAngle)
	{
		$angle = $minAngle
	}
	if ($angle) > ($maxAngle)
	{
		$angle = $maxAngle
	}
	if $maxAngle < $minAngle
	{
		echo "negatif"
		$incRate = -1.0*$incRate
	}
	set $targetObject position float[3]{$position_x_n,$position_y_n,$position_z_n}
	set $targetObject rotation float[3]{$angle*$rotMaskx,$angle*$rotMasky,$angle*$rotMaskz}
	//set $targetObject position float[3]{$position_x_n,$position_y_n,$position_z_n}
	return $position_x_n
}
func float rotate(rcarmodel $targetObject, float $targetState, float $angle, float $posx, float $posy, float $posz, float $rotMaskx, float $rotMasky, float $rotMaskz, float $incRate, float $decRate, float $minAngle, float $maxAngle)
{
	float $targetAngle = $targetState*($maxAngle-$minAngle) + $minAngle
	if $targetAngle > $angle
	{
		$angle = $angle + $incRate
	}
	if $targetAngle < $angle
	{
		$angle = $angle - $decRate
	}
	if $angle < $minAngle
	{
		$angle = $minAngle
	}
	if $angle > $maxAngle
	{
		$angle = $maxAngle
	}
	if $maxAngle < $minAngle
	{
		//echo "negatif"
		$incRate = -1.0*$incRate
		$decRate = -1.0*$decRate
	}
	set $targetObject position float[3]{$posx,$posy,$posz}
	set $targetObject rotation float[3]{$angle*$rotMaskx,$angle*$rotMasky,$angle*$rotMaskz}
	return $angle
}
rcar $car = get scriptowner car
rcar $localcar = get local car
rcarmodel $toit = get generic model 20 of $car
rcarmodel $toit_2 = get generic model 21 of $car
set $toit scriptcontrolled 1
set $toit_2 scriptcontrolled 1
set $toit position float[3]{0.0, 0.81, -0.17}
set $toit_2 position float[3]{0.0, -10000, 0.00}
float $toit_move
float $toit_rot
float $toit_pos
float $toit_rot_2
float $toit_move_2
float $offset = 0.00
float $state = 0.00
float $state2 = 0.0
float $toggle2 = 1
int $mode2
float $speed = 0.00035
int $fermeture = 0
int $ouverture = 0
while 1
{
	if $car == $localcar
	{
		//echo $mode2
		//echo $offset
		//Touche X pour fermer
		if is key 88 pressed
		{
			$ouverture = 0
			$fermeture = 1
			$offset-=0.250
			if $offset < -0.01
			{
				$offset = 0.1
			}
			if $offset < 0.25
			{
				$mode2 = 1
				$speed = 0.00035
			}
			if $offset >= 0.25
			{
				$mode2 = 2
				$speed = 0.00008
			}
			wait 250
		}
		//Touche W pour ouvrir
		if is key 87 pressed
		{
			$ouverture = 1
			$fermeture = 0
			if $offset < 1.00
			{
				$offset+=0.250
			}
			if $offset <= 0.25
			{
				$mode2 = 1
				$speed = 0.00035
			}
			if $offset > 0.25
			{
				$mode2 = 2
				$speed = 0.00008
			}
			wait 250
		}
		if $ouverture == 1
		{
			//echo "ouverture"
			//echo $mode2
			if $mode2 == 1
			{
				if $state2 < 1.00
				{
					$state2 = $state2 + $speed*$toggle2
				}	
				//Rotation du toit
				//echo "rotation"
				$toit_rot = rotate($toit, $state2, $toit_rot, 0.0, 0.81, -0.17, 0.0, 1.0, 0.0, 0.00006, 0.00006, 0.00, 0.09)
				$toit_move = $toit_move = move($toit_2, $state2, 0, -100000,0, 0, 0, -10000000,0)
				$toit_rot_2 = rotate($toit_2, 0.001*$state2, $toit_rot_2, 0.0, -100000.0, -0.48, 0.0, 1.0, 0.0, 0.0000005, 0.0000005, 0.0, -0.16*$offset)
				if $state2 < 0
				{
					$state2 = 0
				}
				if $state2 > 1
				{
					$state2 = 1
				}
			}
			if $mode2 == 2
			{
				//echo "ouverture"
				//Ouverture du toit
				float $origine_x = 0.00
				float $origine_y = 0.360
				float $origine_z = -0.480
				float $middle = ($state)/0.75
				float $offset_y = (0.030*sinh(($middle-0.75)*-1.00))+0.04
				if $state <= $offset
				{
					$state = $state + $speed*$toggle2
				}
				if $state < 0
				{
					$state = 0
				}
				if $state > 1
				{
					$state = 1
				}
				$toit_rot_2 = rotate($toit_2, 0.001*$state, $toit_rot_2, 0.0, 0.360, -0.48, 0.0, 1.0, 0.0, 0.0000005, 0.0000005, 0.0, -0.16*$offset)
				$toit_rot = rotate($toit, $state, $toit_rot, 0.0, -100000000, -0.17, 0.0, 1.0, 0.0, 0.00006, 0.00006, 0.00, 0.09)
				$toit_move = move($toit_2, $state, $origine_x, $origine_y, $origine_z, 0.001, $origine_x, $origine_y+$offset_y,$origine_z-(0.40*1.00))
			}
		}
		if $fermeture == 1
		{
			//echo "ouverture"
			//echo $mode2
			if $mode2 == 1
			{
				if $state2 > 0.00
				{
					$state2 = $state2 + $speed*$toggle2*-1.00
				}	
				//Rotation du toit
				//echo "rotation"
				$toit_rot = rotate($toit, $state2, $toit_rot, 0.0, 0.81, -0.17, 0.0, 1.0, 0.0, 0.00006, 0.00006, 0.00, 0.09)
				$toit_move = $toit_move = move($toit_2, $state2, 0, -100000,0, 0, 0, -10000000,0)
				$toit_rot_2 = rotate($toit_2, 0.001*$state, $toit_rot_2, 0.0, -100000.0, -0.48, 0.0, 1.0, 0.0, 0.0000005, 0.0000005, 0.0, -0.16*$offset)
				if $state2 < 0
				{
					$state2 = 0
				}
				if $state2 > 1
				{
					$state2 = 1
				}
			}
			if $mode2 == 2
			{
				//echo "ouverture"
				//Ouverture du toit
				float $origine_x = 0.00
				float $origine_y = 0.360
				float $origine_z = -0.480
				float $middle = ($state)/0.75
				float $offset_y = (0.030*sinh(($middle-0.75)*-1.00))+0.04
				//echo $offset
				//echo $state
				if $state >= $offset
				{
					$state = $state + $speed*$toggle2*-1.00
				}
				if $state < 0
				{
					$state = 0
				}
				if $state > 1
				{
					$state = 1
				}
				$toit_rot_2 = rotate($toit_2, 0.001*$state, $toit_rot_2, 0.0, 0.360, -0.48, 0.0, 1.0, 0.0, 0.0000005, 0.0000005, 0.0, 0.13*$offset)
				$toit_rot = rotate($toit, $state, $toit_rot, 0.0, -10000000, -0.17, 0.0, 1.0, 0.0, 0.00006, 0.00006, 0.00, 0.09)
				//$toit_move = move_and_rotate($toit_2, $state, $origine_x, $origine_y, $origine_z, 0.001, $origine_x, $origine_y+(0.04*1.00),$origine_z-(0.60*1.00),0.0, 1.0, 0.0, 0.0005, 0.00, -0.10,$toit_rot_2)
				$toit_move = move($toit_2, $state, $origine_x, $origine_y, $origine_z, 0.001, $origine_x, $origine_y+$offset_y,$origine_z-(0.40*1.00))
				$toit_move_2 =0
			}
		}
	}
	interrupt
}