// This script contains functionality to allow fieldops to dispense ammo at spawn

this.Name = "DispenseAmmo";
this.Parent = "LowLevel";
this.AlwaysRecieveEvents = true;

// possible options
this.LimitToClass(CLASS.FIELDOPS);
this.LimitToNoTarget();
this.LimitToNoEntityFlag(ENTFLAG.MOUNTED);

this.Initialize = function()
{
	Util.ScriptDebugPrint(this.Name, "initialized");
};

// This callback is called when the bot SPAWNS or is REVIVED.
// NOTE: This function won't be called if the bot doesn't match the allowed classes set up above.
this.OnSpawn = function()
{
	// sleep before checking RevivedTime, because RIVIVED event can be called after OnSpawn
	sleep(0.5);
	if (this.RevivedTime && GetTime() - this.RevivedTime < 3)
		{ return; }

	if ( Map && Map.DispenseAmmoTime )
		{ sleep(Map.DispenseAmmoTime - 0.5); }
	else
		{ sleep(2.5); }

	this.Priority = 1.0;
};

this.Enter = function()
{
	this.PacksThrown = 0;
};


this.Update = function()
{
	if ( Map && Map.DontDispenseAmmo )
		{ this.Finished(); }

	this.AddWeaponRequest(Priority.High, WEAPON.AMMO_PACK );
	this.BlockForWeaponChange(WEAPON.AMMO_PACK );

	while(this.PacksThrown < 3 &&
		this.Bot.GetCurrentWeapon() == WEAPON.AMMO_PACK &&
		this.Bot.IsWeaponCharged(WEAPON.AMMO_PACK))
	{
		facing = this.Bot.GetFacing();
		lookPos = (this.Bot.GetEyePosition() + facing * 100) + Vector3(0,0,128);
		this.AddAimRequest(Priority.High, "position", lookPos);
		sleep(0.25);
		this.Bot.HoldButton(BTN.ATTACK1, 0.25);
		sleep(0.1);
		this.ReleaseAimRequest();
		sleep(0.5);
	}

	// This is needed in order to finish the goal. Without it, the goal will continue to have Update called on it.
	this.Finished();
};

this.Events[EVENT.WEAPON_FIRE] = function(weaponId, projectileEntity)
{
	if(weaponId == WEAPON.AMMO_PACK && this.Bot)
	{
		this.PacksThrown += 1;
	}
};

this.Events[EVENT.REVIVED] = function(whoDoneIt)
{
	this.RevivedTime = GetTime();
};
