
gSmokeColourR = 1.0
gSmokeColourG = 1.0
gSmokeColourB = 1.0
gEmitRate = 0.05

gTimerValue = 0.0
TIMEOUT_1000 = 8.0
gIsExpert = false


function Initialise ()

	gSmokeColourR, gSmokeColourG, gSmokeColourB = Call( "Exhaust01:GetEmitterColour" )

	gIsExpert = Call ( "IsExpertMode" ) ~= 0
	gDriven = 0
	gTimerActive = 0
	Call( "BeginUpdate" )
	
end


function OnControlValueChange ( name, index, value )

	if Call( "*:ControlExists", name, index ) then

		Call( "*:SetControlValue", name, index, value );

	end

	if ( name == "AWSReset" and value > 0 ) then
		Call ( "*:SetControlValue", "AWS", 0, 0 )
		Call ( "*:SetControlValue", "AWSWarnCount", 0, 0 )
		Call ( "*:SetControlValue", "EmergencyBrake", 0, 0 )
		gTimerActive = 0

	end

	if name == "Regulator" then

		if value < 0.20 then
			gSmokeColourR = 0.8
			gSmokeColourG = 0.8
			gSmokeColourB = 0.8
			gEmitRate = 0.05

		elseif value < 0.4 then
			gSmokeColourR = 0.5
			gSmokeColourG = 0.5
			gSmokeColourB = 0.5
			gEmitRate = 0.03
				
		elseif value < 0.8 then
			gSmokeColourR = 0.2
			gSmokeColourG = 0.2
			gSmokeColourB = 0.2
			gEmitRate = 0.01

		else
			gSmokeColourR = 0.0
			gSmokeColourG = 0.0
			gSmokeColourB = 0.0
			gEmitRate = 0.005

		end
		
		Call ( "Exhaust 01:SetEmitterColour", gSmokeColourR, gSmokeColourG, gSmokeColourB )
		Call ( "Exhaust 01:SetEmitterRate", gEmitRate )

	end

end


function Update( time )

	if t == nil then 		
		n = 1
		m = 0
		FlashState = 1
		Delay = 22

		t = { "Bwd_DoorFlashL_01:Activate",
	 	 "Bwd_DoorFlashL_02:Activate",
		 "Bwd_DoorFlashR_01:Activate",
		 "Bwd_DoorFlashR_02:Activate",
		 "Fwd_DoorFlashL_01:Activate",
		 "Fwd_DoorFlashL_02:Activate",
		 "Fwd_DoorFlashR_01:Activate",
		 "Fwd_DoorFlashR_02:Activate" }	
	end	



	if Call( "*:GetControlValue", "DynamicBrake", 0) > 0 then
		Call( "*:SetControlValue", "DynBrakeFan", 0, 1)	
	else
		Call( "*:SetControlValue", "DynBrakeFan", 0, 0)	
	end


	if Call( "*:GetControlValue", "Regulator", 0) > 0.8 then
		EF2 = 1
		EF3 = 1
	elseif Call( "*:GetControlValue", "Regulator", 0) > 0.2 then
		EF2 = 1
		EF3 = 0
	else
		EF2 = 0
		EF3 = 0
	end

	Call( "*:SetControlValue", "EngFan02", 0, EF2)
	Call( "*:SetControlValue", "EngFan03", 0, EF3)


	if gTimerActive == 1 then
		gTimerValue = gTimerValue - time
		if ( gTimerValue <= 0.0 ) then
			Call ( "*:SetControlValue", "EmergencyBrake", 0, 1)
		end
	end

	if Call( "GetIsPlayer" ) == 1 then
		if ( Call( "GetIsEngineWithKey" ) == 1 ) then
			if gDriven == 0 then
				gDriven = 1
				Call( "*:SetControlValue", "Active", 0, 1 )
			end
		else
			if gDriven == 1 then
				gDriven = 0
				Call( "*:SetControlValue", "Active", 0, 0 )
			end
		end
	end

	if Call( "*:GetControlValue", "DoorsOpenCloseLeft", 0) == 1 then
		a = 1
	elseif Call( "*:GetControlValue", "DoorsOpenCloseRight", 0) == 1 then
		a = 3
	else	
		a = 0
	end

	if a > 0 then
		if FlashState == 0 then
			FlashState = 1
			Delay = 22 -- Forsinkelse
			if Call( "*:GetControlValue", "Reverser", 0) < 0 then 
				b = 0	
			else
				b = 4
			end
		end

		Delay = Delay - time		
		d = a + b

		if Delay <= 0.0 then
			Delay = 0.4 -- Blink interval
			n, m = m, n
			Call( t[d], n )
			Call( t[d+1], m )
		end

	else
		if FlashState == 1 then
			Call( t[1], 0 )
			Call( t[2], 0 )
			Call( t[3], 0 )
			Call( t[4], 0 )
			Call( t[5], 0 )
			Call( t[6], 0 )
			Call( t[7], 0 )
			Call( t[8], 0 )
			FlashState = 0
		end
	end
end


function OnCustomSignalMessage ( arg )
	if ( gIsExpert ) then
		if ( arg == "1000" ) then
			Call ( "*:SetControlValue", "AWS", 0, 1 )
			Call ( "*:SetControlValue", "AWSWarnCount", 0, 1)
			gTimerValue = TIMEOUT_1000
			gTimerActive = 1

		elseif (arg == "2000" ) then
			local canPass = Call ( "GetRequestedSPAD" )
			if canPass == 0 then
				Call ( "*:SetControlValue", "EmergencyBrake", 0, 1)
			end
		end
	end
end

