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

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

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

// 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()
{
	if (this.RevivedTime && GetTime() - this.RevivedTime < 4 || Map.DontDispenseAmmo)
		{ return; }

	if (Map.DispenseAmmoTime)
		{ sleep(Map.DispenseAmmoTime); }
	else
		{ sleep(3); }

	this.Priority = 1.0;
};

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


this.Update = function()
{
	this.AddWeaponRequest(WEAPON.AMMO_PACK);
	this.BlockForWeaponChange(WEAPON.AMMO_PACK);

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

	this.Finished();
};

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

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