--------------------------------------------------------------------------------------
-- SD75 Script - Only for TS2012
-- SD75 writen by Ricardo Reppo Rivera - April 2012 - Previous script from SD70, modyfied by Ricardo Reppo Rivera
--------------------------------------------------------------------------------------

IDLERPM = 1000



FULLRPM = 1500

RPM_ID = 1709
RPMD_ID = 1710

function Initialise ()


Call( "CabLight:Activate", 0)



	gPrevRPM = 0
	gPrevRPMDelta = 0
	
	
	gSpeedo = 0

	Call( "BeginUpdate" )

end

function OnControlValueChange ( name, index, value )

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

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

	end
	
	

end

function Update ( time )








-- DIGITAL SPEEDOMETER CONTROL ----------------------
-----------------------------------------------------

gSpeedo = (math.floor(Call("*:GetControlValue", "SpeedometerMPH", 0)))
Call("*:SetControlValue", "Dots", 0, gSpeedo)


gSpeedo = tostring(gSpeedo)

local NumberLen = string.len(gSpeedo)

	if NumberLen == 1 then
				gSpeedo = ("00" .. gSpeedo)

	elseif NumberLen == 2 then
				gSpeedo = ("0" .. gSpeedo)
	end
	
	
_, _, gHundreds, gTens, gUnits = string.find(gSpeedo, "(%d)(%d)(%d)")



if NumberLen == 1 then
	Call("*:SetControlValue", "Units", 0,  tonumber(gUnits))
	Call("*:SetControlValue", "Tens", 0,  (-1))
	Call("*:SetControlValue", "Hundreds", 0,  (-1))
elseif NumberLen == 2 then
	Call("*:SetControlValue", "Units", 0,  tonumber(gUnits))
	Call("*:SetControlValue", "Tens", 0,  tonumber(gTens))
	Call("*:SetControlValue", "Hundreds", 0,  (-1))
elseif NumberLen == 3 then
	Call("*:SetControlValue", "Units", 0,  tonumber(gUnits))
	Call("*:SetControlValue", "Tens", 0,  tonumber(gTens))
	Call("*:SetControlValue", "Hundreds", 0,  tonumber(gHundreds))
end -- of digital speedometer control



if Call("*:GetControlValue", "Blower", 0) == 1 then
Call( "CabLight:Activate", 1)
else
Call( "CabLight:Activate", 0)
end


if Call("*:GetControlValue", "Headlights", 0) == 0 then

Call( "Fwd_Headlight_01:Activate", 0)
Call( "Fwd_Headlight_02:Activate", 0)
Call( "Fwd_Headlight_03:Activate", 0)
Call( "Bwd_Headlight_01:Activate", 0)


Call ( "ActivateNode", "lights_front", 0 )
Call ( "ActivateNode", "lights_rear", 0 )

elseif Call("*:GetControlValue", "Headlights", 0) == 1 then

Call( "Fwd_Headlight_01:Activate", 1)
Call( "Fwd_Headlight_02:Activate", 1)
Call( "Fwd_Headlight_03:Activate", 1)
Call( "Bwd_Headlight_01:Activate", 0)

Call ( "ActivateNode", "lights_front", 1 )
Call ( "ActivateNode", "lights_rear", 0 )

elseif Call("*:GetControlValue", "Headlights", 0) == 2 then

Call( "Fwd_Headlight_01:Activate", 0)
Call( "Fwd_Headlight_02:Activate", 0)
Call( "Fwd_Headlight_03:Activate", 0)
Call( "Bwd_Headlight_01:Activate", 1)


Call ( "ActivateNode", "lights_front", 0 )
Call ( "ActivateNode", "lights_rear", 1 )

elseif Call("*:GetControlValue", "Headlights", 0) == 3 then

Call( "Fwd_Headlight_01:Activate", 1)
Call( "Fwd_Headlight_02:Activate", 1)
Call( "Fwd_Headlight_03:Activate", 1)
Call( "Bwd_Headlight_01:Activate", 1)


Call ( "ActivateNode", "lights_front", 1 )
Call ( "ActivateNode", "lights_rear", 1 )

end




Call ( "ActivateNode", "numbers_light",  Call("*:GetControlValue", "NumbersLights", 0))

if Call("*:GetControlValue", "RightSteps", 0) == 1 then

Call( "StepLight_FR:Activate", 1)
Call( "StepLight_RR:Activate", 1)
Call ( "ActivateNode", "steps_right", 1 )

else

Call( "StepLight_FR:Activate", 0)
Call( "StepLight_RR:Activate", 0)
Call ( "ActivateNode", "steps_right", 0 )

end


if Call("*:GetControlValue", "LeftSteps", 0) == 1 then
Call( "StepLight_FL:Activate", 1)
Call( "StepLight_RL:Activate", 1)
Call ( "ActivateNode", "steps_left", 1 )
else
Call( "StepLight_FL:Activate", 0)
Call( "StepLight_RL:Activate", 0)
Call ( "ActivateNode", "steps_left", 0 )
end




	rpm = Call( "*:GetControlValue", "RPM", 0 )
	rpm_change = Call( "*:GetControlValue", "RPMDelta", 0 )


		Call( "Exhaust:SetEmitterActive", 1 )
	
		Call( "Exhaust:SetEmitterActive", 0 )



	if ( Call( "GetIsPlayer" ) == 1 ) then

		if ( Call( "*:GetControlValue", "Active", 0 ) == 1 ) then
			if ( rpm ~= gPrevRPM ) then
				Call( "SendConsistMessage", RPM_ID, rpm, 1 )
				Call( "SendConsistMessage", RPM_ID, rpm, 0 )
			end
			if ( rpm_change ~= gPrevRPMDelta ) then
				Call( "SendConsistMessage", RPMD_ID, rpm_change, 1 )
				Call( "SendConsistMessage", RPMD_ID, rpm_change, 0 )
			end
		end

		gPrevRPM = rpm
		gPrevRPMDelta = rpm_change

	end

end



function OnConsistMessage ( msg, argument, direction )



	if ( Call( "*:GetControlValue", "Active", 0 ) == 0 ) then

		if ( msg == RPM_ID ) then
			Call( "*:SetControlValue", "RPM", 0, argument )
		elseif ( msg == RPMD_ID ) then
			Call( "*:SetControlValue", "RPMDelta", 0, argument )
		end


		Call( "SendConsistMessage", msg, argument, direction )

	end

end
