// This script contains functionality to allow bots to watch their own ammo and call for ammo when
// they are below a certain value.

if(ScriptGoals.watch_ammo == null)
{
	ScriptGoals.watch_ammo = table();
	
	// A debug flag for enabling/disabling printed messages. Useful for debugging this goal.
	ScriptGoals.watch_ammo.Debug = true;
	
	// Script Goal Options
	ScriptGoals.watch_ammo.call_ammo_delay = 10.0;
	ScriptGoals.watch_ammo.check_ammo_delay = 2.0;
	
	// This table holds all the weapons we want to ignore armor for.
	// We want the weapon Id to be the key in the table, so we set an arbitrary value to go with it.
	ScriptGoals.watch_ammo.ignore_weapons = table();	
	ScriptGoals.watch_ammo.ignore_weapons[WEAPON.AMMO_PACK] = 1;
	ScriptGoals.watch_ammo.ignore_weapons[WEAPON.MEDKIT] = 1;
	ScriptGoals.watch_ammo.ignore_weapons[WEAPON.SATCHEL] = 1;
	ScriptGoals.watch_ammo.ignore_weapons[WEAPON.SYRINGE] = 1;
	ScriptGoals.watch_ammo.ignore_weapons[WEAPON.SMOKE_MARKER] = 1;
	ScriptGoals.watch_ammo.ignore_weapons[WEAPON.KNIFE] = 1;
	ScriptGoals.watch_ammo.ignore_weapons[WEAPON.PLIERS] = 1;
	ScriptGoals.watch_ammo.ignore_weapons[WEAPON.BINOCULAR] = 1;
	ScriptGoals.watch_ammo.ignore_weapons[WEAPON.DYNAMITE] = 1;
	ScriptGoals.watch_ammo.ignore_weapons[WEAPON.LANDMINE] = 1;
	ScriptGoals.watch_ammo.ignore_weapons[WEAPON.ADERNALINE] = 1;
			
	ScriptGoals.watch_ammo.watchAmmoFunc = function()
	{
		if( ScriptGoals.watch_ammo.Debug )
		{
			print("ScriptGoals.watch_ammo.watchAmmoFunc");
		}
		
		while( true )
		{		
			needsAmmo = false;
			
			// Check if we need ammo or not.
			currentWeaponId = this.GetCurrentWeapon();
			
			// Check if the current weapon isn't in the ignore list and it needs ammo.
			if( currentWeaponId && ( ScriptGoals.watch_ammo.ignore_weapons[currentWeaponId] == null ) )
			{
				needsAmmo = true;
			}
			
			// Only do stuff if the bot doesn't already have a script goal.
			if( needsAmmo && ( this.scriptgoal == null ) )
			{
				// Check if we should self-heal
				if( this.GetClass() != CLASS.FIELDOPS )
				{
					this.SayVoice( VOICE.NEED_AMMO );
					sleep( ScriptGoals.watch_ammo.call_ammo_delay );
					continue;
				}
				else
				{
					// This will take temporary control of the bot, and when done, should leave the bot
					// in the state it was initially.
					if( this.IsWeaponCharged( WEAPON.AMMO_PACK ) )
					{
						// Save the current settings.
						wasScriptControlled = this.IsScriptControlled();
						wasScriptControlledWeapons = this.IsScriptControlledWeapons();
						
						// Self Heal
						this.SetScriptControlled( true );
						this.SetScriptControlledWeapons( true );
						
						dowhile( this.GetCurrentWeapon() != WEAPON.AMMO_PACK )
						{
							this.SelectWeapon( WEAPON.AMMO_PACK );        
							yield();
						}
						
						endTime = GetTime() + 3500;
						dowhile( this.IsWeaponCharged( WEAPON.AMMO_PACK ) && ( GetTime() <= endTime ) )
						{
							this.FireWeapon();
							yield();                
						}
						
						// Set it back to the state it was before we took control.
						// This should allow existing goal scripts to continue.
						this.SetScriptControlled( wasScriptControlled );
						this.SetScriptControlledWeapons( wasScriptControlledWeapons );
					}
				}
			}
			
			sleep( ScriptGoals.watch_ammo.check_ammo_delay );
		}
	};
}

// Add the following snippet somewhere, such as a spawn callback to use this goal

// Make sure we don't have the thread already.
//if(this.WatchAmmoThread == null)
//{
//	this.WatchAmmoThread = this:thread(ScriptGoals.watch_ammo.watchAmmoFunc);
//}

