// This script contains functionality to allow fieldop bots to dispense ammo at spawn

if(ScriptGoals.dispense_ammo == null)
{
	ScriptGoals.dispense_ammo = table();
	
	// The name for this goal
	ScriptGoals.dispense_ammo.Name = "GOAL_DISPENSE_AMMO";
	
	////////////////////////////////////////////////////////////////////////////////
	// A debug flag for enabling/disabling printed messages. Useful for debugging this goal.
	ScriptGoals.dispense_ammo.Debug = true;
	
	ScriptGoals.dispense_ammo.DispenseAmmo = function()
	{
		if( ScriptGoals.dispense_ammo.Debug )
		{
			print("ScriptGoals.dispense_ammo.DispenseAmmo");
		}
		
		// Is goal disabled?
		if( map.DispenseAmmoDisable )
		{
			if( ScriptGoals.dispense_ammo.Debug )
			{
				print("ScriptGoals.dispense_ammo.DispenseAmmo Disabled");
			}
			
			exit();
		}
		
		if( ScriptGoals.dispense_ammo.Debug )
		{
			print("ScriptGoals.dispense_ammo.DispenseAmmo Enabled");
		}
		
		this.scriptgoal = ScriptGoals.dispense_ammo.Name;
		
		// Let's wait for 5 seconds after we spawn, so the bot doesn't do all of this right at the spawn point.
		sleep(5.0);
	
		// Take brief control over the bot. This tells the bot that it is script controlled
		// so that the internal logic of the bot will be skipped.
		// Since this behavior also deals with selecting and using a weapon, we set their weapons
		// to script controlled. Without this their internal weapon system will continue to run,
		// interfering with the script.
		//this.SetScriptControlled(true);
		this.SetScriptControlledWeapons(true);
	
		// Say something annoying so we know the script got here.
		this.SayTeam("Ammo for everyone!");
		
		// The reason we loop is because weapon switching isn't instantaneous, so the loop causes the script to
		// keep trying to select the weapon until it is the current weapon.
		dowhile( this.GetCurrentWeapon() != WEAPON.AMMO_PACK )
		{
			this.SelectWeapon(WEAPON.AMMO_PACK);			
			// give it time to switch weapons.
			yield(); 
		}
		
		// This loop will make the bot Fire the current weapon for 5 seconds(5000 ms)
		// To keep the bot from looking too stupid, let's also make the loop depend on the bot not having a target.
		// By doing this, the loop will end if the bot acquires a target, allowing this function to finish and the bot to attack normally.
		endTime = GetTime() + 5000; // GetTime() is in milliseconds
		dowhile(GetTime() < endTime && !this.HasTarget())
		{
			// Since the script has taken control of the bot, the bot won't do any turning itself, causing it to keep facing the same direction
			// that it was facing when we took control. Obviously this makes them look a bit retarted, so lets have the script make the bot
			// turn toward the direction the bot is moving. We calculate a point to turn towards by
			this.TurnToFacing( this.GetVelocity() );
		
			// Hold down the fire button.
			this.FireWeapon();
			yield();		
		}		

		// This is VERY important. If you forget to disable the script controlled flags
		// when your scripts end(if they end that is), the bot will stand idle and do nothing.
		// Also remember to do this at any point in your script that do an early return;
		this.SetScriptControlledWeapons(false);
		//this.SetScriptControlled(false);
	};
	
};