--[[
---------------------------------------------------------------------------
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

-- This script is written to use 3 engines. The Engine List on the Main window
-- should have exactly three active engines and the rest should be inactive.

require("defines");

EngineNo = 99
TIUNo = 99
-- clear the tables
EnginesUsedNum = {};

--[[---------------------------------------------------------------------------------------]]
function setup(Engine, TIU)
	print("setup()");
	EngineNo = Engine;	-- Engine number selected on the RTC Main window
	MyTIUNo = TIU;		-- TIU number selected on the RTC Main window
	-- debug level, values 0 (off) to 9 (maximum)
	if (Debug() > 0) then print(string.format("Debug level %d", Debug())); end
	TagCount = 0;
	iEngineCount = 0;

	print("TIU : " .. TIU);
	assert((TIU >= 1 and TIU <= 5),"TIU Number out of range");
	-- initialize the layout as needed
	-- switches and accessories
	AIUNo = 1;
	--Switch(DIVERGE, 1, AIUNo, MyTIUNo, 3);  -- College East
	--Switch(DIVERGE, 2, AIUNo, MyTIUNo, 5);  -- College West
	return true;		-- false=setup failed, true=setup succeeded
	end
--[[---------------------------------------------------------------------------------------]]
function loop(Engine, TIU)
	-- This function is called when the [Run Script] button is pushed.
	-- Always call Sleep() at least once in this function since it returns true;
		print("loop()");
	-- Start Active Engines - 1-99 (DCS 2-100)
	print("Starting Up Active Engines");
	StartUpActiveEngines(2);

	for iEng = 1, 99 do	-- check all possible engines on one TIU
		if (IsEngineRunning(iEng)) then	-- IsEngineRunning() REQUIRES use of StartUpActiveEngines() or EngineStartUp()
			print(string.format("Initialization queued for Engine %d", iEng));
			-- save the numbers of all started engines
			iEngineCount = iEngineCount + 1;			-- Count the number of engines running
			BumpCounter(COUNTER01)
			EnginesUsedNum[iEngineCount] = iEng;		-- EnginesUsedXXXX[] is 1 based
			-- Set smoke off
			Sleep(1);
			print(string.format("loop() Sleep(1) Complete at %.2f", RunTime()));
			Smoke(OFF, 2, iEng, MyTIUNo);
			end
		end
	print(string.format("Engines Started %d", iEngineCount));
	Sleep(1);
	-- Blow the horn on each engine
--	for iDex = 1, iEngineCount do	-- for each running engine, blow the whistle
		--local iEng = EnginesUsedNum[iDex];	-- EnginesUsedXXXX[] is 1 based
		--print(string.format("Whistle queued for Engine : %d", iEng));
		--Whistle(ON, 7, iEng, MyTIUNo);
		--Whistle(OFF, 10, iEng, MyTIUNo);
		--Sleep(4);
		--print(string.format("loop() Sleep(4) Complete at %.2f", RunTime()));
		--end
	print("loop() Complete");
	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()");
	print("Shutting Down Active Engines");
	ShutDownActiveEngines(2);
	print("Done");
	return true;		-- false=cleanup failed, true=cleanup succeeded
	end
--[[---------------------------------------------------------------------------------------]]
