--[[
---------------------------------------------------------------------------
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 = "Speed Test"

require("defines");
require("functions");

DETECTED = true;
NOTDETECTED = false;


-- all tables in Lua are 1 based, that is, T[1] is the first item in the list
EngineDetectionOrder = {};	-- clear the tables
EnginesUsedNum = {};
EnginesUsedFlag = {};
EngineSpeedSave = {};
Got = {};
-- Counter Labels
Counter01Label = "Engines Detected";
Counter02Label = "Not Used";
-- table to hold all of the tags detected in sequence
Tags = {};


--[[---------------------------------------------------------------------------------------]]
function setup(Engine, TIU)
	-- this function is called once when the [Run Script] button is pushed
	print("setup()");
	-- Engine number selected on the Main window is not used
	if (Debug() > 0) then print(string.format("Debug level %d", Debug())); end
	if (Debug() > 0) then print("Debug level " .. tonumber(Debug())); end
	-- initialize variables
	TagCount = 0;
	iEngineCount = 0;
	iEngineDetectionCount = 0;
	
	-- initialize the layout

	-- Start All Active Engines - 1-99 (DCS 2-100)
	print("Starting All Active Engines");

	for iEng = 1, 99 do	-- check all possible engines on one TIU
		if (Exists(iEng)) then
			if (IsActive(iEng)) then
				print("Engine " .. iEng .. " Starting");
				EngineStartUp(iEng, TIU);
				Sleep(2);
				end
			end
			
		if (IsEngineRunning(iEng)) then	-- IsEngineRunning() REQUIRES use of StartUpActiveEngines() or EngineStartUp()
			print(string.format("Initalizing Engine %d", iEng));
			-- save the numbers of all started engines
			iEngineCount = iEngineCount + 1;			-- Count the number of engines running
			EnginesUsedNum[iEngineCount] = iEng;		-- EnginesUsedXXXX[] is 1 based
			EnginesUsedFlag[iEngineCount] = NOTDETECTED;-- flag as not yet detected in motion
			-- Set smoke off
			Smoke(OFF, 2+(iEngineCount), iEng, TIU);
			-- Cab Chatter Off
			CabChat(OFF, 3+(iEngineCount), iEng, TIU);
			-- Set Acc/Dec rate to 1/1 Smph/sec
			Rate(1, 1, (iEngineCount), iEng, TIU);
			end
		end
	print(string.format("Engines Started %d", iEngineCount));
	-- Start each train moving forward
	for iDex = 1, iEngineCount do	-- for each running engine, set the speed to 12 Smph
		local iEng = EnginesUsedNum[iDex];	-- EnginesUsedXXXX[] is 1 based
		print(string.format("SetSpeed for Engine : %d", iEng));
		SetSpeed(20, 10+(iDex*2), iEng, TIU);
		end
	return true;	-- false=setup failed, true=setup succeeded
	end
--[[---------------------------------------------------------------------------------------]]
function cleanup(Engine, TIU)
	-- this function is called once when the user presses the [STOP] button
	print("cleanup()");
	for idx,Engine in ipairs(EnginesUsedNum) do		
		print(idx .. " Shutdown Engine " .. Engine);
		EngineShutdown(Engine, TIU);
		end	
	return true;		-- false=cleanup failed, true=cleanup succeeded
	end
--[[---------------------------------------------------------------------------------------]]
function StopAll(Eng, TIU)
	for idx,Engine in ipairs(EnginesUsedNum) do		
		print(idx .. " Stopping Engine " .. Engine);
		SetSpeed(0, (idx*2), Engine, TIU);
		end	
	return true;
end
Function01Name, Function01Label = StopAll, "Stop All";
--[[---------------------------------------------------------------------------------------]]
function StartAll(Eng, TIU)
	for idx,Engine in ipairs(EnginesUsedNum) do		
		print(idx .. " Starting Engine " .. Engine);
		SetSpeed(20, (idx*2), Engine, TIU);
		end	
	return true;
end
Function02Name, Function02Label = StartAll, "Start All";
--[[---------------------------------------------------------------------------------------]]
function Forward(Eng, TIU)
	for idx,Engine in ipairs(EnginesUsedNum) do		
		print(idx .. " Forward Engine " .. Engine);
		SetDirection(FORWARD, (idx*2), Engine, TIU);
		end	
	return true;
end
Function03Name, Function03Label = Forward, "Forward";
--[[---------------------------------------------------------------------------------------]]
function Reverse(Eng, TIU)
	for idx,Engine in ipairs(EnginesUsedNum) do		
		print(idx .. " Reverse Engine " .. Engine);
		SetDirection(REVERSE, (idx*2), Engine, TIU);
		end	
	return true;
end
Function04Name, Function04Label = Reverse, "Reverse";
--[[---------------------------------------------------------------------------------------]]