// This script contains useful utility scripts for performing common actions.

if( Util == null )
{
	// Store it neatly under a global table.
	global Util = table();
	
	///////////////////////////////////////////////////////////////////////////////
	
	// The function will make the bot hold down a button for a period of time.
	// The 'this' when called should be a bot instance.
	Util.HoldButtonForTime = function( button, time )
	{
		endTime = GetTime() + time;
		dowhile( endTime > GetTime() )
		{
			this.PressButton( button );
			yield();
		}	
	};
	
	///////////////////////////////////////////////////////////////////////////////
	
	// The function will loop until the bot has successfully selected a given weapon.
	// The 'this' when called should be a bot instance.
	Util.SelectWeapon = function( weapon )
	{	
		dowhile ( this.GetCurrentWeapon() != weapon )
		{
			this.SelectWeapon( weapon );
			yield();
		}
	};
	
	///////////////////////////////////////////////////////////////////////////////
	
	// The DebugBotAim function runs this thread on the bots.
	Util.DebugBotAimThread = function( _targetpos )
	{
		block( EVENT.SPAWNED );
		
		this.SetScriptControlled( true );
		this.EnableShooting( false );
		
		// Repeat until we get there.
		while(1)
		{
			print( "Going to " + _targetpos );
			this.GoTo( _targetpos );
			
			if( block(EVENT.GOAL_SUCCESS, EVENT.GOAL_FAILED) == EVENT.GOAL_SUCCESS )
			{
				break;
			}
		}
		
		this.SetDebugFlag( DEBUG.AIM, true );
				
	};
	
	// This function adds 2 bots to the game, disables their 
	Util.DebugBotAim = function( _range )
	{
		// Pick a default range to use if none is provided.
		if( _range == null )
		{
			_range = 20;
		}
	
		print( "Range " + _range );
		
		KickAll();
	
		AddBot( TEAM.ALLIES, CLASS.SOLDIER );
		AddBot( TEAM.AXIS, CLASS.SOLDIER );
		
		// Get a target position for 2 bots.
		localHost = GetGameEntityFromId(0);
		localHostPos = localHost.GetPosition();
		localHostFacing = localHost.GetFacing();
		
		sleep(5.0);
			
		// calculate 2 points on each side of the localhost to send the bots to.
		aimPoints = table();
		aimPoints[0] = localHostPos + ( localHostFacing * _range );
		aimPoints[1] = localHostPos - ( localHostFacing * _range );
		
		print( "Creating bot threads." );
		iCurrentBot = 0;
		foreach ( gameId and bot in BotTable )
		{
			print( aimPoints[iCurrentBot] );
			bot:thread( Util.DebugBotAimThread, aimPoints[iCurrentBot] );
			iCurrentBot += 1;
			if(iCurrentBot >= 2)
			{
				break;
			}
		}	
	};
	
	///////////////////////////////////////////////////////////////////////////////
	
	Commands["aim_debug"] = function( _params )
	{
		print( "Executing aim debugging script." );	
		Util.DebugBotAim( _params[0] );
	};
	
	///////////////////////////////////////////////////////////////////////////////
	
	Commands["aim_debug_reload"] = function( _params )
	{
		foreach ( gameId and bot in BotTable )
		{
			bot.ReloadProfile();
		}	
	};
	
	///////////////////////////////////////////////////////////////////////////////
	
	Util.WaypointAutoSaveInfo = table();
	Util.WaypointAutoSaveInfo.TimeBetweenSaves = 10;
	Util.WaypointAutoSaveFunction = function(_delay)
	{
		print("Will auto-save waypoints every", _delay, "seconds.");
		
		// Loop as long as waypoint mode is on.
		while( IsWaypointViewOn() )
		{
			ExecCommand( "waypoint_save" );
			sleep( _delay );
		}
		
		print("Waypoint auto saving disabled.");
	};
	Util.EnableWaypointAutoSave = function( _enable, _time )
	{
		if( _enable )
		{
			// Default time between saves. Change this value if you want the save duration something different.
			timeBetweenSaves = 10.0;
		
			print("Executing my waypoint function");
			ExecCommand("waypoint_view 1");
				
			Util.WaypointAutoSaveInfo.SaveThreadId = thread( Util.WaypointAutoSaveFunction, _time );
		}
		else
		{
			// Kill the thread if it exists, and null the 
			if( Util.WaypointAutoSaveInfo.SaveThread != null )
			{
				threadKill( Util.WaypointAutoSaveInfo.SaveThreadId );
				Util.WaypointAutoSaveInfo.SaveThreadId = null;
			}
		}
	};
		
	
	
}
