--[[
---------------------------------------------------------------------------
Remote Train Control Program for Windows

© Copyright 2019 by Mark DiVecchio

	This file is part of Remote Train Control.

	Remote Train Control is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	Remote Train Control is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with Remote Train Control.  If not, see <http://www.gnu.org/licenses/>.

 Home Page : http://www.silogic.com/trains/RTC_Running.html
---------------------------------------------------------------------------]]
-- Semicolons are not required in Lua code
title = "PFA Test"
require("defines");

local MyEngineNo, MyTIUNo;

-- This label appear on the tab for the Function Buttons
Tab01Label = "Control";

--[[---------------------------------------------------------------------------------------]]
function setup(Engine, TIU)
	print("setup()");
	MyEngineNo = Engine;	-- Engine number selected on the RTC Main window
	MyTIUNo = GetTIUNo(MyEngineNo);	-- TIU associated with MyEngineNo
	-- debug level, values 0 (off) to 9 (maximumDebug()
	if (Debug() > 0) then print(string.format("Debug level %d", Debug())); end
	-- initialize the layout
	AC, IAC = Counts();
	print("TIU" .. MyTIUNo .. ": " .. AC .. " Active Engines - " .. IAC .. " Inactive Engines");
	return true;		-- false=setup failed, true=setup succeeded
	end
--[[---------------------------------------------------------------------------------------]]
function loop(Engine, TIU)
	-- This function is called once when the [Run Script] button is pushed.
	-- Engine : Engine number selected on the RTC Main window
	-- TIU : TIU number selected on the RTC Main window
	print(string.format("loop(): Engine %d TIU %d", Engine, TIU));
	if (Debug() > 0) then print(string.format("Debug level %d", Debug())); end
	-- EngineStartUp(MyEngineNo, MyTIUNo);
	StartUp(0, MyEngineNo, MyTIUNo);
	Whistle(ON, 10, Engine, MyTIUNo);
	Whistle(OFF, 12, Engine, MyTIUNo);
	print(string.format("loop() Sleep Started at %.2f", RunTime()));
	Sleep(5);
	print(string.format("loop() Sleep(5) Complete at %.2f", RunTime()));
	return false;	-- false=do not loop, true=continue to loop
	end
--[[---------------------------------------------------------------------------------------]]
function cleanup(Engine, TIU)
	-- this function is called once when the user presses the [STOP] button
	--print("cleanup()");
	--EngineShutdown(MyEngineNo, MyTIUNo);
	ShutDown(0, MyEngineNo, MyTIUNo);
	print(string.format("cleanup() Complete at %.2f", RunTime()));
	-- NOP
	return true;		-- false=cleanup failed, true=cleanup succeeded
	end
--[[---------------------------------------------------------------------------------------]]
-- you can create up to 10 functions
--[[---------------------------------------------------------------------------------------]]
function NOP(Eng, TIU)
	print("NOP()");
	print(string.format("Function10() : Eng %d TIU %d Debug %d", Eng, TIU, Debug()));
	return true;
end
Function10Name, Function10Label = NOP, "NOP";
--[[---------------------------------------------------------------------------------------]]
function SW1200(Eng, TIU)
	if (MyEngineNo ~= 4) then
		return false;
		end;
	print("SW1200()");
	TIUNo = GetTIUNo(MyEngineNo);	-- TIU associated with MyEngineNo
	SW1200PFA(0, MyEngineNo, TIUNo);		-- only works with Engine 4, the SW1200
	return true;
end
Function01Name, Function01Label = SW1200, "SW 1200 PFA";
--[[---------------------------------------------------------------------------------------]]
function GP7(Eng, TIU)
	if (MyEngineNo ~= 7 and MyEngineNo ~= 13) then
		return false;
		end;
	print("GP7()");
	TIUNo = GetTIUNo(MyEngineNo);	-- TIU associated with MyEngineNo
	GP7PFA(0, MyEngineNo, TIUNo);		-- only works with Engines 7 and 13, the GP7's
	return true;
end
Function02Name, Function02Label = GP7, "GP7 PFA";
--[[---------------------------------------------------------------------------------------]]

function GP7PFA(when, EngineNo, TIUNo)
--
-- Run the GP7 PFA sequence
--
-- returns additional delay for PFA on success
--
local PFADelay = when + 6 + 42;
SetVolume(MASTER_VOLUME, 70, when + 6, EngineNo, TIUNo);	-- reduce volume for the PFA sequence
-- when + 0
PlaySound(110, when + 6 + 15, EngineNo, TIUNo); 	-- Now Boarding
PlaySound(111, when + 6 + 18, EngineNo, TIUNo); 	-- Ticketed Passengers Only
PlaySound(112, when + 6 + 21, EngineNo, TIUNo); 	-- Let me help you
PlaySound(113, when + 6 + 24, EngineNo, TIUNo); 	-- Let me take your baggage
PlaySound(114, when + 6 + 28, EngineNo, TIUNo); 	-- Thanks
PlaySound(115, when + 6 + 34, EngineNo, TIUNo); 	-- All Aboard
SetVolume(MASTER_VOLUME, 100, PFADelay, EngineNo, TIUNo);
print("P&LE GP7 #1500 and #1501 PFA");
return PFADelay;
end
--[[---------------------------------------------------------------------------------------]]

function SW1200PFA(when, EngineNo, TIUNo)
--
-- Run the SW1200 PFA sequence
--
-- returns additional delay for PFA on success
--
local PFADelay = when + 6 + 77;	-- its a long sequence
SetVolume(MASTER_VOLUME, 45, when + 6, EngineNo, TIUNo);	-- reduce volume for the PFA sequence
-- when + 6 + 20
PlaySound(80, when + 6 + 20, EngineNo, TIUNo);
PlaySound(81, when + 6 + 20 + 5, EngineNo, TIUNo);
PlaySound(82, when + 6 + 20 + 5 + 10, EngineNo, TIUNo);
PlaySound(83, when + 6 + 20 + 5 + 10 + 3, EngineNo, TIUNo);
PlaySound(84, when + 6 + 20 + 5 + 10 + 3 + 4, EngineNo, TIUNo);
PlaySound(85, when + 6 + 20 + 5 + 10 + 3 + 4 + 4, EngineNo, TIUNo);
PlaySound(86, when + 6 + 20 + 5 + 10 + 3 + 4 + 4 + 5, EngineNo, TIUNo);
PlaySound(87, when + 6 + 20 + 5 + 10 + 3 + 4 + 4 + 5 + 3, EngineNo, TIUNo);
PlaySound(88, when + 6 + 20 + 5 + 10 + 3 + 4 + 4 + 5 + 3 + 5, EngineNo, TIUNo);
PlaySound(89, when + 6 + 20 + 5 + 10 + 3 + 4 + 4 + 5 + 3 + 5 + 5, EngineNo, TIUNo);
PlaySound(90, when + 6 + 20 + 5 + 10 + 3 + 4 + 4 + 5 + 3 + 5 + 5 + 4, EngineNo, TIUNo);
SetVolume(MASTER_VOLUME, 100, PFADelay, EngineNo, TIUNo);
print("A&S SW1200 #1208 PFA");
return PFADelay;
end
--[[---------------------------------------------------------------------------------------]]
