'##############
'Engine Script
'##############

'------------------------------------------------------------------------------------------


'----------------------
'	Trigger
'----------------------

'-------------------------------------------
'	Ключ зажигания (для клавиатуры)
'-------------------------------------------
{trigger:kw_m_enginestart}
	(L.L.engine_on) !
	{if}
		(L.L.key_first_start) !
		{if}
			0.4 (S.L.cp_key_rot)
			1 (S.L.key_first_start)
		{else}
			(L.L.key_antirepeat) 0 =
			{if}
				0.8 (S.L.cp_key_rot)
				0 (S.L.engine_starter_antirepeat)
				1 (S.L.key_antirepeat)
			{else}
				(L.L.lights_svecha) !
				{if}
					1 (S.L.cp_key_rot) (S.L.eng_on)
					0 (S.L.engine_starter_antirepeat)
				{endif}
			{endif}
		{endif}
	{else}
		0.4 (S.L.cp_key_rot)
		0 (S.L.engine_starter_antirepeat) (S.L.eng_on) (S.L.key_antirepeat)
		0 (S.L.Svecha_antirepeat) (S.L.Svecha_timer)
	{endif}
{end}

{trigger:kw_m_enginestart_off}
	(L.L.eng_on)
	{if}
		0.8 (S.L.cp_key_rot)
	{endif}
	0 (S.L.starter)
{end}

'-------------------------------------------
'	Ключ зажигания
'-------------------------------------------

{trigger:cp_key_mov_drag}
	(L.L.Svecha_timer) 2 <
	{if}
		0.8 (S.L.cp_key_rot_max)
	{else}
		1 (S.L.cp_key_rot_max)
	{endif}
	
	(L.S.mouse_y) -70 / (L.L.cp_key_rot) + 0 max (L.L.cp_key_rot_max) min (S.L.cp_key_rot) s1

	l1 0.1 <
	{if}
		0 (S.L.cp_key_rot_mode) (S.L.key_first_start)
	{else}
		l1 0.6 <
		{if}
			2 (S.L.cp_key_rot_mode)
			0 (S.L.engine_injection_on) (S.L.Svecha_antirepeat) (S.L.Svecha_timer) (S.L.key_antirepeat) (S.L.eng_on)
			2 (S.L.Svecha_timer)
			0 (S.L.lights_svecha)
		{else}
			4 (S.L.cp_key_rot_mode)
		{endif}
	{endif}
	
	l1 0.9 >
	{if}
		(L.L.engine_on) !
		{if}
			(L.L.starter_rot) 3 / (S.L.engine_n)
			(L.L.antrieb_getr_gangwahl) 1 = (L.L.elec_busbar_main) &&
			(L.L.engine_tank_content) &&
			{if}
				(L.L.starter) (L.L.delay_start) <
				{if}
					(L.L.starter) (L.S.Timegap) + (S.L.starter)
				{else}
					1 (S.L.engine_on) (S.L.engine_injection_on) (S.L.engine_starter_antirepeat)
					(T.L.ev_enginestart)
					300 (S.L.engine_n)
				{endif}
			{endif}
		{endif}
	{endif}
{end}

{trigger:cp_key_mov_off}

'	Привязка ключа к фиксированным положениям
	(L.L.cp_key_rot_mode) 5 / (S.L.cp_key_rot)
	0 (S.L.starter)
{end}


{trigger:veh_tank}
	(L.L.engine_tank_content) (L.S.Timegap) 3 * + 190 min (S.L.engine_tank_content)
{end}

{macro:engine_init}
	50 200 random + (S.L.engine_tank_content)
	(L.S.Weather_Temperature) (S.L.engine_temperature) (S.L.engine_temperature_envir)
{end}


{macro:engine_frame}

	(L.L.engine_last_n) 300 >=
	(L.L.engine_n) 300 < &&
	{if}
		(M.L.engine_stall)
		0 (S.L.engine_on)
	{endif}

	(L.L.engine_n) (S.L.engine_last_n)

	(L.L.engine_n) 100 < (L.L.engine_injection_on) ! &&
	{if}
		0 (S.L.engine_n) 
	{endif}

	(L.L.bremse_kompressor)	(L.L.engine_on) &&
	{if}	
		10 (S.L.engine_M_additional_load)
	{else}
		0 (S.L.engine_M_additional_load)
	{endif}

	(M.L.engine_temperature)

	(M.L.engine_consumption)

	(L.L.throttle) (S.L.engine_internal_throttle)



'	Override durch Haltestellenbremse ODER Motorabstellung

	(L.L.bremse_halte_sw) (L.L.door_20h_sw) || (C.L.engine_e-gas) &&
	(L.L.engine_injection_on) ! ||
	{if}
		0 (S.L.engine_internal_throttle)
	{endif}

	(C.L.engine_speedcontrol)
	{if}
		(M.L.engine_speedcontrol)
	{endif}
	
	(M.L.key_ignition)

'MCQ: calculating tank content percent and write into pre defined variable for publishing on red line:
	(L.L.engine_tank_content) 190 / (S.L.tank_percent)

'	Motormanagement durch ASR-Eingriff, sofern vorhanden

	(C.L.engine_ASR)
	(L.L.cp_ASR_off) ! &&
	(L.L.elec_busbar_main) &&
	{if}
		(L.L.Wheel_RotationSpeed_1_L) (L.L.Wheel_RotationSpeed_1_R) + 2 / abs (L.L.Wheel_RotationSpeed_0_L) (L.L.Wheel_RotationSpeed_0_R) + 2 / 10 + abs >
		(L.L.Wheel_RotationSpeed_1_L) 1 > &&
		(L.L.Wheel_RotationSpeed_1_R) 1 > &&
		(L.L.engine_internal_throttle) 0 > &&
		{if}
			(L.L.engine_ASR_eingriff) !
			{if}
				(T.L.ev_bremse_ABS_0R_on)
			{endif}	
			1 (S.L.engine_ASR_eingriff)
		{endif}
		(L.L.Wheel_RotationSpeed_1_L) (L.L.Wheel_RotationSpeed_1_R) + 2 / abs (L.L.Wheel_RotationSpeed_0_L) (L.L.Wheel_RotationSpeed_0_R) + 2 / 8 + abs <
		{if}
			(L.L.engine_ASR_eingriff)
			{if}
				(T.L.ev_bremse_ABS_0R_off)
			{endif}	
			0 (S.L.engine_ASR_eingriff)
		{endif}

		(L.L.engine_ASR_eingriff)
		{if}
			0 (S.L.engine_internal_throttle)
		{endif}

	{else}
'		Wenn ASR aus, dann Dauerleuchten
		(L.L.elec_busbar_main) 
		{if}
			(L.L.engine_ASR_eingriff) !
			{if}
				(T.L.ev_bremse_ABS_0R_on)
			{endif}	
			1 (S.L.engine_ASR_eingriff)
		{else}
			(L.L.engine_ASR_eingriff)
			{if}
				(T.L.ev_bremse_ABS_0R_off)
			{endif}	
			0 (S.L.engine_ASR_eingriff)
		{endif}
	{endif}

	(L.L.elec_busbar_main)	
	(L.L.navigator_menu) &&	
    (L.L.engine_n) 300 >= && 
	{if}
	    1 (S.L.tahometr_300) 
		{else}
		0 (S.L.tahometr_300)		
	{endif}	

	(L.L.elec_busbar_main)	
	(L.L.navigator_menu) &&	
    (L.L.engine_n) 400 >= &&
	{if}
	    1 (S.L.tahometr_400) 
		{else}
		0 (S.L.tahometr_400)		
	{endif}	

	(L.L.elec_busbar_main)	
	(L.L.navigator_menu) &&	
    (L.L.engine_n) 500 >= &&
	{if}
	    1 (S.L.tahometr_500) 
		{else}
		0 (S.L.tahometr_500)		
	{endif}	

	(L.L.elec_busbar_main)	
	(L.L.navigator_menu) &&	
    (L.L.engine_n) 600 >= &&
	{if}
	    1 (S.L.tahometr_600) 
		{else}
		0 (S.L.tahometr_600)		
	{endif}	

	(L.L.elec_busbar_main)	
	(L.L.navigator_menu) &&	
	(L.L.engine_n) 700 >= &&
	{if}
	    1 (S.L.tahometr_700) 
		{else}
		0 (S.L.tahometr_700)		
	{endif}	

	(L.L.elec_busbar_main)	
	(L.L.navigator_menu) &&	
    (L.L.engine_n) 800 >= &&
	{if}
	    1 (S.L.tahometr_800) 
		{else}
		0 (S.L.tahometr_800)		
	{endif}	

	(L.L.elec_busbar_main)	
	(L.L.navigator_menu) &&	
    (L.L.engine_n) 900 >= &&
	{if}
	    1 (S.L.tahometr_900) 
		{else}
		0 (S.L.tahometr_900)		
	{endif}	

	(L.L.elec_busbar_main)	
	(L.L.navigator_menu) &&	
	(L.L.engine_n) 1000 >= &&
	{if}
	    1 (S.L.tahometr_1000) 
		{else}
		0 (S.L.tahometr_1000)		
	{endif}	

	(L.L.elec_busbar_main)	
	(L.L.navigator_menu) &&	
    (L.L.engine_n) 1100 >= &&
	{if}
	    1 (S.L.tahometr_1100) 
		{else}
		0 (S.L.tahometr_1100)		
	{endif}	

	(L.L.elec_busbar_main)	
	(L.L.navigator_menu) &&	
    (L.L.engine_n) 1200 >= &&
	{if}
	    1 (S.L.tahometr_1200) 
		{else}
		0 (S.L.tahometr_1200)		
	{endif}	

	(L.L.elec_busbar_main)	
	(L.L.navigator_menu) &&	
	(L.L.engine_n) 1300 >= &&
	{if}
	    1 (S.L.tahometr_1300) 
		{else}
		0 (S.L.tahometr_1300)		
	{endif}	

	(L.L.elec_busbar_main)	
	(L.L.navigator_menu) &&	
    (L.L.engine_n) 1400 >= &&
	{if}
	    1 (S.L.tahometr_1400) 
		{else}
		0 (S.L.tahometr_1400)		
	{endif}	

	(L.L.elec_busbar_main)	
	(L.L.navigator_menu) &&	
    (L.L.engine_n) 1500 >= &&
	{if}
	    1 (S.L.tahometr_1500) 
		{else}
		0 (S.L.tahometr_1500)		
	{endif}	

	(L.L.elec_busbar_main)	
	(L.L.navigator_menu) &&
    (L.L.engine_n) 1600 >= &&
	{if}
	    1 (S.L.tahometr_1600) 
		{else}
		0 (S.L.tahometr_1600)		
	{endif}	

	(L.L.elec_busbar_main)	
	(L.L.navigator_menu) &&	
    (L.L.engine_n) 1700 >= &&
	{if}
	    1 (S.L.tahometr_1700) 
		{else}
		0 (S.L.tahometr_1700)		
	{endif}	

	(L.L.elec_busbar_main)	
	(L.L.navigator_menu) &&	
    (L.L.engine_n) 1800 >= &&
	{if}
	    1 (S.L.tahometr_1800) 
		{else}
		0 (S.L.tahometr_1800)		
	{endif}

	(L.L.elec_busbar_main)	
	(L.L.navigator_menu) &&	
    (L.L.engine_n) 1900 >= &&
	{if}
	    1 (S.L.tahometr_1900) 
		{else}
		0 (S.L.tahometr_1900)		
	{endif}	

	(L.L.elec_busbar_main)	
	(L.L.navigator_menu) &&	
    (L.L.engine_n) 2000 >= && 
	{if}
	    1 (S.L.tahometr_2000) 
		{else}
		0 (S.L.tahometr_2000)		
	{endif}	

	(L.L.elec_busbar_main)	
	(L.L.navigator_menu) &&	
    (L.L.engine_n) 2100 >= &&
	{if}
	    1 (S.L.tahometr_2100) 
		{else}
		0 (S.L.tahometr_2100)		
	{endif}	

{end}

	
{macro:engine_moment}


'	Reaktionszeit der Einspritzpumpe
	(L.L.engine_internal_throttle) (L.L.engine_throttle_injection) >
	{if}
		(L.L.engine_throttle_injection) (L.S.Timegap) 3 * + (L.L.engine_internal_throttle) min (S.L.engine_throttle_injection)
	{else}
		(L.L.engine_throttle_injection) (L.S.Timegap) 3 * - (L.L.engine_internal_throttle) max (S.L.engine_throttle_injection)
	{endif}

	(L.L.engine_injection_on) (L.L.engine_on) &&
	{if}
		(L.L.engine_throttle_injection) s1 (L.L.engine_n) s0 (F.L.engine_M_maxThrottle) * 1 l1 - l0 (F.L.engine_M_minThrottle) * + (S.L.engine_M)
	{else}
		(L.L.engine_n) 1 >
		{if}
			(C.L.engine_coldM) (S.L.engine_M)
		{else}
			0 (S.L.engine_M)
		{endif}
	{endif}

{end}


{macro:key_ignition}
'	Скорость схватывания зажигания, зависит от температуры двигателя и оборотов стартера.
	(L.L.engine_temperature) 0 <
	{if}
		(L.L.engine_temperature) abs 10 / 2 max (S.L.delay_start_temperature)
	{else}
		1.1 (S.L.delay_start_temperature)
	{endif}
	
	(L.L.starter_rot) 1000 / (S.L.delay_start_starter_spd)
	
	(L.L.delay_start_temperature) (L.L.delay_start_starter_spd) - (S.L.delay_start)
	
'	Свеча накала(визуально онли, горит лампочка)
	(L.L.cp_key_rot) 0.8 =
	(L.L.engine_temperature) -5 < &&
	(L.L.Svecha_antirepeat) ! &&
	(L.L.elec_busbar_main) &&
	{if}
		(L.L.Svecha_timer) 2 <
		{if}
			(L.L.Svecha_timer) (L.S.Timegap) + (S.L.Svecha_timer)
			1 (S.L.lights_svecha)
		{else}
			0 (S.L.lights_svecha)
			1 (S.L.Svecha_antirepeat)
		{endif}
	{endif}
	
	(L.L.cp_key_rot) 0.8 <
	{if}
		2 (S.L.Svecha_timer)
		0 (S.L.lights_svecha)
	{endif}
	

	(L.L.cp_key_rot) 0.9 > (L.L.elec_busbar_main) &&
	{if}
		1 (S.L.starter_sound)
		(L.L.elec_V_battery) (F.L.starter_rot) (S.L.starter_rot)
'	Если долго крутить стартер аккумулятор сдохнет.
		(L.L.elec_V_battery) 0.75 >
		{if}
			0.7 (S.L.elec_V_battery)
		{else}
			(L.L.elec_V_battery) (L.S.Timegap) 0.004 * - 0 max (S.L.elec_V_battery)
		{endif}
	{else}
		0 (S.L.starter_sound)
	{endif}
	
	(L.L.cp_key_rot) 0.4 >= (L.L.elec_busbar_main) &&
	(S.L.lights_pribori)

	(L.L.cp_key_rot) 1 =
	{if}
		(L.L.engine_starter_antirepeat) !
		{if}
			(L.L.engine_on) !
			{if}
				(L.L.starter_rot) 3 / (S.L.engine_n)
				(L.L.antrieb_getr_gangwahl) 1 = (L.L.elec_busbar_main) &&
				(L.L.engine_tank_content) &&
				{if}
					(L.L.starter) (L.L.delay_start) <
					{if}
						(L.L.starter) (L.S.Timegap) + (S.L.starter)
					{else}
						1 (S.L.engine_on) (S.L.engine_injection_on) (S.L.engine_starter_antirepeat)
						(T.L.ev_enginestart)
						300 (S.L.engine_n)
					{endif}
				{endif}
			{endif}
		{endif}
	{endif}
	
	(L.L.cp_key_rot) 0.4 =
	{if}
		(L.L.engine_starter_antirepeat) !
		{if}
			(L.L.engine_on)
			{if}
				0 (S.L.engine_injection_on)
				1 (S.L.engine_starter_antirepeat)
			{endif}
		{endif}
	{endif}
{end}

{macro:engine_stall}
	
	(T.L.ev_engineshutdown)
	
	0 (S.L.engine_on) (S.L.engine_injection_on)
{end}


{macro:engine_acceleration}
'	Berechnung der Drehzahlbeschleunigung:

	(L.L.engine_M) (L.L.antrieb_getr_M_an) (L.L.engine_M_additional_load) + - (C.L.engine_J) /

'	... Berechnung der Motordrehzahl anhand der Drehzahlbeschleunigung:

	(L.S.Timegap) * 30 * pi / (L.L.engine_n) + (S.L.engine_n)
{end}


{macro:engine_consumption}
'					      	=2*PI/60
	(L.L.engine_M) 0 max (L.L.engine_n) * 0.10472 * s0
	(L.L.engine_on)
	{if}
		l0 (C.L.engine_power_idle) + (S.L.engine_power)
	{else}
		l0 (S.L.engine_power)
	{endif}
	
	(L.L.engine_energytransfer) (L.L.engine_power) (L.S.Timegap) * 1000 / 3600 / + (S.L.engine_energytransfer)
	(L.L.engine_energy_used) (L.L.engine_power) (L.S.Timegap) * 1000 / 3600 / (L.L.engine_n) (F.L.engine_efficiency_rpm) (L.L.engine_throttle_injection) (F.L.engine_efficiency_throttle) / / s0 + (S.L.engine_energy_used)
	(L.L.engine_fuel_used) l0 (C.L.engine_fuel_value) / s1 + (S.L.engine_fuel_used)
	
	l1 (L.S.Timegap) / 3600 * (S.L.engine_consumption_h)

	(L.L.engine_tank_content) l1 - 0 max (S.L.engine_tank_content)
	0 <= 
	(L.L.engine_failure_general) ||
	{if}
		0 (S.L.engine_on) (S.L.engine_injection_on)
	{endif}
	
{end}

{macro:engine_speedcontrol}

	(L.L.engine_n) (C.L.engine_governed_idle_RPM) <
	{if}
		(L.L.engine_additional_throttle) (L.S.Timegap) 0.2 * + (S.L.engine_additional_throttle)
	{else}
		(L.L.engine_additional_throttle) (L.S.Timegap) 0.2 * - 0 max (S.L.engine_additional_throttle)
	{endif}

	(L.L.engine_internal_throttle) (L.L.engine_additional_throttle) + 1 min 0 max (S.L.engine_internal_throttle)

{end}


{macro:engine_temperature}

	(L.L.engine_power) (C.L.engine_Qrate_engine_factor) * (S.L.engine_Qrate_engine)


	(L.L.elec_busbar_main) (L.L.engine_on) &&
	{if}
		(L.L.engine_temperature) (C.L.luefterthermostat_opn) >
		(L.L.cp_motorkuehlung_sw) ||
		{if}
			1 (S.L.engine_luefter)
		{else}
			(L.L.engine_temperature) (C.L.luefterthermostat_cls) <
			(L.L.cp_motorkuehlung_sw) ! &&
			{if}
			0 (S.L.engine_luefter)
			{endif}
		{endif}
	{else}
		0 (S.L.engine_luefter)
	{endif}

	(L.L.engine_temperature) (C.L.kuehlerthermostat_opn) >
	{if}
		1 (S.L.engine_thermostat_open)
	{else}
		(L.L.engine_temperature) (C.L.kuehlerthermostat_cls) <
		{if}
			0 (S.L.engine_thermostat_open)
		{endif}
	{endif}

	
'	--------------------------------------------------
'	Berechnung der Raten:
'	Вычисление скорости:

	(L.L.engine_temperature) (L.L.engine_temperature_envir) - (C.L.engine_Qrate_cooling_factor) * (S.L.engine_Qrate_cooling)
	(L.L.engine_temperature_envir) (L.S.Weather_Temperature) - (C.L.engine_Qrate_Veloc_factor_basic) (C.L.engine_Qrate_veloc_factor) (L.L.velocity) abs * + * (S.L.engine_Qrate_veloc)

	(L.L.engine_temperature) (L.S.Weather_Temperature) - (C.L.engine_Qrate_fan_factor) * (L.L.engine_n) * (S.L.engine_Qrate_fan)


	(L.L.engine_thermostat_open)
	{if}
		(L.L.engine_luefter) !
		{if}	
'			Reduktion der Kьhlwirkung durch nur leer mitdrehenden Lьfter
			(L.L.engine_Qrate_fan) 0.4 * (S.L.engine_Qrate_fan)	
		{endif}
	{else}			
'		Nullwirkung des Lьfters durch Kьhlwasserkurzschluss	
		0 (S.L.engine_Qrate_fan)
	{endif}


'	--------------------------------------------------
'	Berechnung der Temperaturverдnderungen:
'	Расчет изменения температуры:

	(L.L.engine_Qrate_engine) (L.L.engine_Vrate_motortuer) - (L.L.engine_Qrate_cooling) - (L.L.engine_Qrate_fan) - (L.L.cabinair_Qrate_engine_fanheatcooling) - (C.L.engine_cvm_engine) / (L.S.Timegap) * (L.L.engine_temperature) + (S.L.engine_temperature)
	(L.L.engine_Qrate_cooling) (L.L.engine_Qrate_veloc) - (C.L.engine_cvm_envir) / (L.S.Timegap) * (L.L.engine_temperature_envir) + (S.L.engine_temperature_envir) 


'--------------------------------------------------




'	Kochendes Kьhlwasser
	(L.L.engine_temperature) (F.L.kuehlwassersmoke) (S.L.engine_kuehlwasser_freq)


	

	
{end}




