--[[
---------------------------------------------------------------------------
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 = "Global Variables Test"
require("defines");
require("functions");

local ErrorCount = 0;

--[[---------------------------------------------------------------------------------------]]
function setup(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("setup()");
	for x = 1, 15 do		-- clear the first 15 global integers
		SetGlobalInt(x, 0);	-- ClearGlobals() will clear all of the global variables
		local val = GetGlobalInt(x);
		print("GetGlobalInt(" .. x .. ") (zero) = ", val)
		if (val ~= 0) then
			print("Global Int " .. x .. " not set correctly (0)");
			ErrorCount = ErrorCount + 1;
			end;
		end;
	SetGlobalInt(3, 23456);
	SetGlobalInt(99, 33);
	
	if (GetGlobalInt(3) ~= 23456) then
		print("Global Int 3 not set correctly (23456)");
		ErrorCount = ErrorCount + 1;
		end;
	if (GetGlobalInt(99) ~= 33) then
		print("Global Int 99 not set correctly (33)");
		ErrorCount = ErrorCount + 1;
		end;
	
	SetGlobalString(4, "My String");
	if (GetGlobalString(4) ~= "My String") then
		print('Global String 4 not set correctly ("My String")');
		ErrorCount = ErrorCount + 1;
		end;

	-- RunTime() must be converted to an integer
	SetGlobalString(8, string.format("The time is %d seconds", math.floor(RunTime() + 0.5)));
		
	print(string.format("RunTime = %.2f seconds", RunTime()));
	return true;		-- false=setup failed, true=setup succeeded
	end
--[[---------------------------------------------------------------------------------------]]	
function loop(Engine, TIU)
	-- must call Sleep() at least once in this function
	print("loop()");
	print("GetGlobalInt(3) (23456) = ", GetGlobalInt(3));
	GlobalInt99 = GetGlobalInt(99);
	print("GetGlobalInt(99) (++) = ", GlobalInt99);
	GlobalInt99 = GlobalInt99 + 1;
	SetGlobalInt(99, GlobalInt99);
	
	if (GetGlobalInt(3) ~= 23456) then
		print('Global Int 3 not set correctly ("23456")');
		ErrorCount = ErrorCount + 1;
		end;
	if (GetGlobalInt(99) ~= GlobalInt99) then
		print("Global Int 99 not set correctly (" .. GlobalInt99 .. ")");
		ErrorCount = ErrorCount + 1;
		end;
	
	SetGlobalString(4, "My String");
	if (GetGlobalString(4) ~= "My String") then
		print('Global String 4 not set correctly ("My String")');
		ErrorCount = ErrorCount + 1;
		end;

	print("GetGlobalString(4) ('MyString') = ", GetGlobalString(4));
	print("GetGlobalString(8) = ", GetGlobalString(8));
	--print("GetGlobalString(11) = ", GetGlobalString(11));	-- gets "index out of range" error	
	for x = 1, 15 do
		print("GetGlobalInt(" .. x .. ") = ", GetGlobalInt(x))
		end;
	Sleep(5);
	SetGlobalString(8, string.format("The time is %d seconds", math.floor(RunTime() + 0.5)));
	print("loop exit");	
	return true;		-- false=loop failed, true=loop succeeded
	end
--[[---------------------------------------------------------------------------------------]]
function cleanup(Engine, TIU)
	-- this function is called once when the user presses the [STOP] button
	if (ErrorCount == 0) then
		printc(clGreen, "No Errors");
		else
		printc(clRed, ErrorCount .. " Errors");
		end;	
	print(string.format("cleanup() Complete at %.2f", RunTime()));
	return true;		-- false=cleanup failed, true=cleanup succeeded
	end
--[[---------------------------------------------------------------------------------------]]