--------------------------------------------------------------------------------------
-- PZB 500Hz Inductor
-- (goes 250m before Hp signal)
-- Modified from Default KUJU PZB Script to make compatible with Default & vR Stock
-- SSD 5 Aug 2012
-- Rev 1.0
--------------------------------------------------------------------------------------

--------------------------------------------------------------------------------------
-- GLOBAL DEFINITIONS

-- Enable/Disable Script Debug Messages
DEBUG 				= false

-- Signal States
CLEAR 				= 0
WARNING 			= 1
BLOCKED 			= 2

-- Signal Messages
SIGNAL_BLOCKED  	= 12;
SIGNAL_CLEARED 		= 13;
SIGNAL_WARNING 		= 14;
SIGNAL_WARNING2 	= 15;

-- Consist Messages

-- Default Stock
MSG_DF_PZB1000		= "1000"
MSG_DF_PZB2000		= "2000"

-- vR Expert Line Stock
CUSTOM_MESSAGE 		= 15;
MSG_VR_PZB500		= "PZB500";
MSG_VR_PZB1000 		= "PZB1000";
MSG_VR_PZB2000 		= "PZB2000";


--------------------------------------------------------------------------------------
-- INITIALISE
--
function Initialise ()
	DebugPrint( ("DEBUG: Initialise") )
	trainPassedSignal = false
	nextSignalMessageState = false
end


--------------------------------------------------------------------------------------
-- DETERMINE CONSIST TYPE
-- Determines whether a consist is fast, slow passenger or freight based on its consist type
--
function DetermineConsistType( )

	local consistType = Call( "GetConsistType" )

	if consistType < 0
	or consistType > 10 then
		Print(("ERROR: DetermineConsistType: consistType out of range - " .. consistType))
	end

	DebugPrint(("DEBUG: DetermineConsistType: consistType " .. consistType))

	if consistType == CONSIST_TYPE_SPECIAL
	or consistType == CONSIST_TYPE_INTERNATIONAL
	or consistType == CONSIST_TYPE_LIGHT_ENGINE
	or consistType == CONSIST_TYPE_EXPRESS_PASSENGER then

		consistType = CONSIST_TYPE_FAST_PASSENGER

	elseif consistType == CONSIST_TYPE_STOPPING_PASSENGER then

		consistType = CONSIST_TYPE_SLOW_PASSENGER

	else

		consistType = CONSIST_TYPE_FREIGHT
	end

	return consistType
end


--------------------------------------------------------------------------------------
-- ON CONSIST PASS
--
function OnConsistPass ( prevFrontDist, prevBackDist, frontDist, backDist, linkIndex )

	-- if the consist is crossing the inductor now
	if ( frontDist > 0 and backDist < 0 ) or ( frontDist < 0 and backDist > 0 ) then

		-- if the consist was previously before siganl then the crossing has just started
		if (prevFrontDist > 0 and prevBackDist > 0) then

			if ( trainPassedSignal ~= true ) then

				trainPassedSignal = true

				-- Request state of next signal
				local nextSignalState = Call( "GetNextSignalState", "", 1, 1, 0 )
				DebugPrint( ("DEBUG: PZB 500Hz OnConsistPass: Crossing started, next signal state is " .. nextSignalState) )

				-- If the signal ahead is blocked
				if ( nextSignalState == BLOCKED ) then
					Call( "SendConsistMessage", CUSTOM_MESSAGE,  MSG_VR_PZB500 )

					-- Get consist speed and figure out how fast they should be going
					local consistSpeed = Call ( "GetConsistSpeed" ) * 3.6	-- convert consist speed from m/s to km/h
					local consistType = DetermineConsistType()
					local speedLimit = 25

					-- Set speed limit according to train class
					if consistType == CONSIST_TYPE_FAST_PASSENGER then

						speedLimit = 45

					elseif consistType == CONSIST_TYPE_SLOW_PASSENGER then

						speedLimit = 35

					elseif consistType == CONSIST_TYPE_FREIGHT then

						speedLimit = 25

					else
						Print(("ERROR: OnConsistPass - unknown consist type " .. consistType))
					end

					DebugPrint( ("DEBUG: PZB 500Hz OnConsistPass: consistSpeed = " .. consistSpeed .. " km/h, speedLimit = " .. speedLimit .. " km/h") )

					-- If the train is going too fast...
					-- NOTE: Give some leeway, as speed is only displayed to nearest 1km/h in guide window
					if consistSpeed > (speedLimit + 1) then
						Call( "SendConsistMessage", SIGNAL_BLOCKED, "overspeed" )
					end
				end
			end
		end
	else
		-- Reset once train has passed
		if ( trainPassedSignal ~= false ) then
			DebugPrint( ("DEBUG: PZB 500Hz OnConsistPass: Crossing finished") )
			trainPassedSignal = false
		end
	end
end

--------------------------------------------------------------------------------------
-- ON SIGNAL MESSAGE
--
function OnSignalMessage( message, parameter, direction, linkIndex )

	if(message == SIGNAL_BLOCKED) then
		nextSignalMessageState = SIGNAL_BLOCKED;
	elseif(message == SIGNAL_WARNING) then
		nextSignalMessageState = SIGNAL_WARNING;
	elseif(message == SIGNAL_CLEARED) then
		nextSignalMessageState = SIGNAL_CLEARED;
	end

	-- Forward every message unchanged, unless it's from a train straddling our link
	if ( parameter ~= "DoNotForward" ) then
		Call( "SendSignalMessage", message, parameter, -direction, 1, linkIndex )
	end
end

--------------------------------------------------------------------------------------
-- JUNCTION STATE CHANGE
-- Left blank - PZB inductors only have one link
--
function OnJunctionStateChange( junction_state, parameter, direction, linkIndex )

end

function DebugPrint( message )
	if (DEBUG) then
		Print( message )
	end
end
