// This script provides functionality for the bots to pickup close weapons, health, and ammo if they aren't at max levels
this.Name = "PickupItems";
this.Parent = "HighLevel";
this.AlwaysRecieveEvents = true;
this.PickupPriority = 0.9;
this.Range = 200;
this.Frequency = 5; // 1 to 10, 0 to disable

this.LimitToNoEntityFlag(ENTFLAG.INWATER, ENTFLAG.UNDERWATER);

this.Initialize = function()
{
	this.WatchForEntityCategory({
		Radius = this.Range,
		Category = CAT.PICKUP,
		
		// cs: lets pretend bots hear the pack being dropped behind them ...
		// it's reasonable since this is limited to a 200 radius. this should
		// improve the deliver supplies goal success rate.
		//RequireLOS = TRACE.VISIBLE,
	});
};

this.Enter = function()
{
	this.AddFinishCriteria(this.TargetEntity, "deleted");
	this.AddFinishCriteria(this.TargetEntity,"hasentflag",ENTFLAG.DISABLED);
};

this.Update = function()
{
	this.Bot.HoldButton(BTN.SPRINT, 3);
	pos = GetEntPosition(this.TargetEntity);
	if (pos) {
		this.Goto(pos, 32);
	}
	this.Finished();
};

this.Exit = function()
{
	this.Bot.ReleaseButton(BTN.SPRINT);
	this.TargetEntity = null;
};

this.Events[EVENT.ENT_ENTER_RADIUS] = function(ent)
{
	if (!this.Bot || this.IsActive() || RandInt(1, 10) >= this.Frequency) {
		return;
	}

	entCat = this.GetPickupCategory(ent);

	switch(entCat)
	{
		case CAT.PICKUP_WEAPON:
		case CAT.PICKUP_AMMO:
		{
			if (this.NeedsAmmo(ent, entCat)) {
				this.TargetEntity = ent;
				this.Priority = this.PickupPriority;
			}
		}
		case CAT.PICKUP_HEALTH:
		{
			if (this.Bot.Health < this.Bot.MaxHealth) {
				this.TargetEntity = ent;
				this.Priority = 1.2 - 0.7 * this.Bot.Health / this.Bot.MaxHealth;
			}
		}
	}
};

this.NeedsAmmo = function(ent, entCat)
{
	if (entCat == CAT.PICKUP_WEAPON) {
		weapClass = GetEntClass(ent);
		if (weapClass) {
			weaponId = GetWeaponIdFromClassId(weapClass);

			// it's a weapon, but not the right type
			if (!weaponId || !this.Bot.HasWeapon(weaponId)) {
				return false;
			}
		}
	}
	else {
		weaponId = this.Bot.GetCurrentWeapon();
	}

	// simple check first
	this.Bot.GetMostDesiredAmmo(Util.DesireTable);
	if(Util.DesireTable.Desire > 0) {
		return true;
	}

	// the above check only accounts for current clip
	if (this.Bot.GetCurrentAmmo(Util.AmmoTable, 0, weaponId)) {
		if (Util.AmmoTable.CurrentAmmo < Util.AmmoTable.MaxAmmo) {
			return true;
		}
	}

	return false;
};

this.GetPickupCategory = function(ent)
{
	if (GetEntCategory(ent, CAT.PICKUP_HEALTH)) {
		return CAT.PICKUP_HEALTH;
	}

	if (GetEntCategory(ent, CAT.PICKUP_WEAPON)) {
		return CAT.PICKUP_WEAPON;
	}

	if (GetEntCategory(ent, CAT.PICKUP_AMMO)) {
		return CAT.PICKUP_AMMO;
	}

	return -1;
};