--[[
---------------------------------------------------------------------------
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 = "Stop-n-Go"

require("defines");

-- configuration variables
AutoBellWhistle = true;
Interval = 0.20;	-- Default Stop-n-Go interval in Smiles
-- Counter Labels
Counter01Label = "Stop-n-Go";
STOP_N_GO = COUNTER01;
-- starting time
DTOStart = 0;

--[[---------------------------------------------------------------------------------------]]
function setup(Engine, TIU)
--
print("This script will not work correctly with RTCEngine");
MyEngineNo = Engine;
print("setup() : TIU " .. TIU .. " Engine " .. MyEngineNo .. " Debug " .. Debug());
print(string.format("Run Time = %.2f seconds", RunTime()));
ChangeInterval(MyEngineNo, TIU);

--[[--------------------------------------------]]
local EngineNameList = {};	-- table to hold all of the Engine numbers and Names
local EngineNameIndex = 1;
local EngineNameCount = 1;

for iEng = 1, 99 do	-- check all possible engines on one TIU
	if (Exists(iEng)) then
		local Name, PS;
		Name, PS = GetName(iEng);
		--print(iEng .. " " .. Name .. " PS" .. PS);
		EngineNameList[iEng] = iEng .. " " .. Name .. " PS" .. PS;
		if (MyEngineNo == iEng) then
			EngineNameIndex = EngineNameCount;
			--print("EngineNameIndex = " .. EngineNameIndex);
			end;
		EngineNameCount = EngineNameCount + 1;		
		end
	end
--
-- Ask the dispatcher for the engine number
--
print("Select Engine");
local Enum = InputDropDown(title, "Select Engine", EngineNameIndex, EngineNameList);
--print("Enum ", Enum);
local typ = type(Enum);
--print("Type ", typ);

if (typ == "boolean" and Enum == false) then
	return Stop("Canceled");
	end;
		
if (Enum ~= nil and Enum ~= "") then	-- if the result is a string, then use first number in the string, otherwise, ignore it
	-- pull first integer found in the string
	local EngNo = string.match(Enum, "%d+");
	if (EngNo ~= nil) then
		MyEngineNo = tonumber(EngNo);
		else
		return Stop("Invalid Engine Number");
		end;
	else
	if (typ == "nil") then
		-- nil returned only if parameters are invalid
		return Stop("Invalid Parameters to InputDropDown");
		else
		MyEngineNo = 1;
		end
	end	
--[[--------------------------------------------]]
print("Engine Number = " .. MyEngineNo);

Smoke(OFF, 0, MyEngineNo, TIU);
EngineStartUp(MyEngineNo, TIU);

math.randomseed(os.time());	-- random seed

Sleep(20.9);
print(string.format("Run Time = %.2f seconds", RunTime()));
DTOStart = DTO(MyEngineNo, TIU);
print(string.format("Trip Odometer Start %.2f Smiles", DTOStart));
SetSpeed(15 + math.random(15), 2, MyEngineNo, TIU);

Sleep(1.3);
print(string.format("Run Time = %.2f seconds", RunTime()));
return true;
end
--[[---------------------------------------------------------------------------------------]]
function tick(Engine, TIU)
-- Sleep() should never be called from this function
if (Debug() > 0) then print("tick() : TIU " .. TIU .. " Engine " .. MyEngineNo .. " Debug " .. Debug()); end
-- check for traveling "Interval" Smiles and then stop the engine
DTONow = DTO(MyEngineNo, TIU);
if (Debug() > 0) then print(string.format("Run Time = %.2f seconds", RunTime())); end
print(string.format("Trip Odometer %.2f Smiles [DTO at Start = %.2f Smiles]", DTONow, DTOStart));
if (DTONow - DTOStart > Interval) then
	BumpCounter(STOP_N_GO);	-- keep count
	Beep();
	-- stop the engine
	print(string.format(" ** Trip Odometer Delta at  %.2f Smiles", DTONow - DTOStart));
	SetSpeed(0, 0, MyEngineNo, TIU);
	DTOStart = DTOStart + Interval;
	SetSpeed(15 + math.random(15), 12, MyEngineNo, TIU);
	if (Debug() > 0) then print(string.format("Run Time = %.2f seconds", RunTime())); end
	return true;
	end
return true;	-- false=stop calling tick(), true=continue to tick
end
--[[---------------------------------------------------------------------------------------]]
function cleanup(Engine, TIU)
--
print("cleanup() : TIU " .. TIU .. " Engine " .. MyEngineNo .. " Debug " .. Debug());
--
SetSpeed(0, 0, MyEngineNo, TIU);
DTOFinish = DTO(MyEngineNo, TIU);
-- wait for dispatch list to empty
while(CountDispatchList() > 0) do Sleep(1); end
print(string.format("Run Time = %.2f seconds", RunTime()));
print(string.format("Trip Odometer Finish %.2f Smiles", DTOFinish));
Sleep(15);
EngineShutDown(MyEngineNo, TIU);
return true;
end
--[[---------------------------------------------------------------------------------------]]
function PlayBell(Eng, TIU)
	print("PlayBell(): Eng " .. Eng .. " TIU " .. TIU .. " Debug " .. Debug());
	print(string.format("Run Time = %.2f seconds", RunTime()));
	Bell(ON, 0.5, Eng, TIU);
	Bell(OFF, 4.6, Eng, TIU);
	return true;
end
Function01Name, Function01Label = PlayBell, "Sound Bell";
--[[---------------------------------------------------------------------------------------]]
function ShowDTO(Eng, TIU)
	print(string.format("Run Time = %.2f seconds", RunTime()));
	print(string.format("Trip Odometer Show %.2f Smiles", DTO(Eng, TIU)));
	return true;
end
Function10Name, Function10Label = ShowDTO, "Trip Odometer";
--[[---------------------------------------------------------------------------------------]]
function ChangeInterval(Eng, TIU)
--	
print("Set Stop-n-Go Interval (Smiles)");
local INum = InputDropDown(title, "Set Stop-n-Go Interval", 1, {"0.20 Smiles", "0.40 Smiles", "0.60 Smiles", "0.80 Smiles"});
--print("INum ", INum);
typ = type(INum);
--print("Type ", typ);

if (typ == "boolean" and INum == false) then
	return Stop("Canceled");
	end;
		
if (INum ~= nil and INum ~= "") then	-- if the result is a string, then use first number in the string, otherwise, ignore it
	-- pull first number found in the string
	local IntvSmiles = string.match(INum, "[%d%.]+");
	if (IntvSmiles ~= nil) then
		Interval = tonumber(IntvSmiles);
		else
		return Stop("Invalid Interval");
		end;
	else
	if (typ == "nil") then
		-- nil returned only if parameters are invalid
		return Stop("Invalid Parameters to InputDropDown");
		else
		--Interval is not changed
		end
	end	
		
print("Interval = " .. Interval .. " Smiles");
return true;
end
Function09Name, Function09Label = ChangeInterval, "Interval";
--[[---------------------------------------------------------------------------------------]]
