--------------------------------------------------------------------------------------
-- Engine Script for GG1 electric locomotive
-- 
------------------------------------------------------------
-- Train Simulator 2012
-- (c) RailSimulator.com
------------------------------------------------------------
--
-- Updates controls, reads messages from signals and set engine controls for
-- cab signalling.
--
--------------------------------------------------------------------------------------

-- Boolean.
	TRUE = 1
	FALSE = 0

	ON = 1
	OFF = 0

-- For light flash rate.

	LIGHT_FLASH_ON_SECS = 0.5

-- Cab signal states.

	NONE = 0
	CLR = 1
	CABSPD8 = 2
	CABSPD6 = 3
	APPLIM = 4
	APPMED = 5
	APP = 6
	RESTRICT = 7
	IGNORE = 8


function Initialise ()

	Call( "cablight_A:Activate", 0 );
	Call( "cablight_B:Activate", 0 );

-- If this engine is being driven by player.
	gDriven = FALSE

-- Initial cab signal states.
	gCabSignal = NONE
	gTrackSpeed = DASH

-- State of flashing for appropriate cab signal aspects.
	gTimeSinceLastFlash = 0
	gLightFlashOn = FALSE
	
	Call( "BeginUpdate" )

end


function OnControlValueChange ( name, index, value )

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

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

	end

		if ( Call( "GetIsEngineWithKey" ) == 1 ) then
			if gDriven == FALSE then
				gDriven = TRUE
				Call( "*:SetControlValue", "Active", 0, 1 )
			end
		else
			if gDriven == TRUE then
				gDriven = FALSE
				Call( "*:SetControlValue", "Active", 0, 0 )
			end
		end
		
	if name == "CabLight" then
		
		if value > 0.01 then
		
			Call( "cablight_A:Activate", 1);
			Call( "cablight_B:Activate", 1);
		
		else
	
			Call( "cablight_A:Activate", 0);
			Call( "cablight_B:Activate", 0);
			
		end
		
	end

end


function OnCustomSignalMessage ( Parameter )

	if ( Call( "GetIsPlayer" ) == TRUE ) and ( Call( "GetIsEngineWithKey" ) == TRUE ) then

		newsig = string.sub ( Parameter, 4, 4 )
		if (newsig+0) ~= IGNORE then
			gCabSignal = newsig
		end


		Print( (" consist signal: " .. gCabSignal ) )

		currentSig = Call( "*:GetControlValue", "CabSignal", 0 )


-- Add zeros to comparisons to ensure the strings are converted to type number.

		if (( currentSig + 0 ) ~= ( gCabSignal + 0 )) and ((newsig+0) ~= IGNORE ) then
			Call( "*:SetControlValue", "CabSignal", 0, gCabSignal )
--			Call( "*:SetControlValue", "AWSWarnCount", 0, TRUE )
		end

		if ( (gCabSignal+0) == APPLIM ) then
			Call( "*:SetControlValue", "CabSignal1", 0, OFF )
			Call( "BeginUpdate" )
		elseif ( (gCabSignal+0) == CABSPD8 ) or ( (gCabSignal+0) == CABSPD6 ) then
			Call( "*:SetControlValue", "CabSignal2", 0, OFF )
			Call( "BeginUpdate" )
		elseif (gCabSignal+0) == APPMED then
			Call( "*:SetControlValue", "CabSignal2", 0, ON )
			Call( "*:SetControlValue", "CabSignal1", 0, OFF )
		elseif (gCabSignal+0) == CLR then
			Call( "*:SetControlValue", "CabSignal1", 0, ON )
			Call( "*:SetControlValue", "CabSignal2", 0, OFF )
		elseif (newsig+0) ~= IGNORE then
			Call( "*:SetControlValue", "CabSignal1", 0, OFF )
			Call( "*:SetControlValue", "CabSignal2", 0, OFF )
		end

	end

end


function Update ( time )

	gTimeSinceLastFlash = gTimeSinceLastFlash + time
	if gTimeSinceLastFlash > LIGHT_FLASH_ON_SECS then
		gTimeSinceLastFlash = 0
		if gLightFlashOn == FALSE then
			gLightFlashOn = TRUE
			if ((gCabSignal+0) == CABSPD8) or ((gCabSignal+0) == CABSPD6) then
				Call( "*:SetControlValue", "CabSignal1", 0, ON )
			elseif ((gCabSignal+0) == APPLIM) then
				Call( "*:SetControlValue", "CabSignal2", 0, ON )
			end
		else
			gLightFlashOn = FALSE
			if ((gCabSignal+0) == CABSPD8) or ((gCabSignal+0) == CABSPD6) then
				Call( "*:SetControlValue", "CabSignal1", 0, OFF )
			elseif ((gCabSignal+0) == APPLIM) then
				Call( "*:SetControlValue", "CabSignal2", 0, OFF )
			end

		end
	end

	if ((gCabSignal+0) ~= CABSPD6) and
	   ((gCabSignal+0) ~= CABSPD8) and
	   ((gCabSignal+0) ~= APPLIM) then
		gLightFlashOn = FALSE
		gTimeSinceLastFlash = 0
		Call ("EndUpdate")
	end
	
end
