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

require("defines");

local EngineNo = 99
local MyTIUNo = 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 : " .. MyTIUNo);
	assert((MyTIUNo >= 1 and MyTIUNo <= 5),"TIU Number out of range");
	-- initialize the layout as needed
	-- switches and accessories
	--local 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()
			local TIUNo = GetTIUNo(iEng);
			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(2);
			print(string.format("loop() Sleep(2) Complete at %.2f", RunTime()));
			Smoke(OFF, 2, iEng, TIUNo);
			end
		end
	print(string.format("Engines Started %d", iEngineCount));
	Sleep(5);
	-- 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
		local TIUNo = GetTIUNo(iEng);
		print(string.format("Whistle queued for Engine : %d", iEng));
		Whistle(ON, 7, iEng, TIUNo);
		Whistle(OFF, 10, iEng, TIUNo);
		Sleep(2);
		print(string.format("loop() Sleep(2) Complete at %.2f", RunTime()));
		end
	print("loop() Complete - Press [STOP] to end the script");
	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
--[[---------------------------------------------------------------------------------------]]
