CheckAmmo

From ZDoom Wiki
Jump to navigation Jump to search

Weapon

virtual bool CheckAmmo(int fireMode, bool autoSwitch, bool requireAmmo = false, int ammocount = -1)

Usage

A virtual function used by weapons to checks if the weapon has enough ammo to fire or to be selected. Returns true if it does. Can be overridden to add custom behavior. Can also be called directly when there's a need to check for ammo but not consume it.

To check and consume ammo, see DepleteAmmo.

Parameters

  • int fireMode
Determines which ammo properties to use. Valid values are PrimaryFire, AltFire, and EitherFire.
  • bool autoSwitch
If true, automatically switches the player's weapon to a new one if out of ammo.
  • bool requireAmmo
If true, WEAPON.AMMO_OPTIONAL/WEAPON.ALT_AMMO_OPTIONAL flags are ignored.
  • int ammocount
If the weapon is defined in DeHackEd and this value is not negative, use it instead of the Weapon.AmmoUse1/Weapon.AmmoUse2 properties.

Return value

  • bool - returns true if the weapon has enough ammo to fire; otherwise returns false.

ZScript definition

Note: The ZScript definition below is for reference and may be different in the current version of GZDoom.The most up-to-date version of this code can be found on GZDoom GitHub.

The base Weapon class defines the function as follows:

	virtual bool CheckAmmo(int fireMode, bool autoSwitch, bool requireAmmo = false, int ammocount = -1)
	{
		int count1, count2;
		int enough, enoughmask;
		int lAmmoUse1;
        int lAmmoUse2 = AmmoUse2;

		if (sv_infiniteammo || (Owner.FindInventory ('PowerInfiniteAmmo', true) != null))
		{
			return true;
		}
		if (fireMode == EitherFire)
		{
			bool gotSome = CheckAmmo (PrimaryFire, false) || CheckAmmo (AltFire, false);
			if (!gotSome && autoSwitch)
			{
				PlayerPawn(Owner).PickNewWeapon (null);
			}
			return gotSome;
		}
		let altFire = (fireMode == AltFire);
		let optional = (altFire? bAlt_Ammo_Optional : bAmmo_Optional);
		let useboth = (altFire? bAlt_Uses_Both : bPrimary_Uses_Both);

		if (!requireAmmo && optional)
		{
			return true;
		}
		count1 = (Ammo1 != null) ? Ammo1.Amount : 0;
		count2 = (Ammo2 != null) ? Ammo2.Amount : 0;

		if (bDehAmmo && Ammo1 == null)
		{
			lAmmoUse1 = 0;
		}
		else if (ammocount >= 0)
		{
			lAmmoUse1 = ammocount;
			lAmmoUse2 = ammocount;
		}
		else
		{
			lAmmoUse1 = AmmoUse1;
		}

		enough = (count1 >= lAmmoUse1) | ((count2 >= lAmmoUse2) << 1);
		if (useboth)
		{
			enoughmask = 3;
		}
		else
		{
			enoughmask = 1 << altFire;
		}
		if (altFire && FindState('AltFire') == null)
		{ // If this weapon has no alternate fire, then there is never enough ammo for it
			enough &= 1;
		}
		if (((enough & enoughmask) == enoughmask) || (enough && bAmmo_CheckBoth))
		{
			return true;
		}
		// out of ammo, pick a weapon to change to
		if (autoSwitch)
		{
			PlayerPawn(Owner).PickNewWeapon (null);
		}
		return false;
	}

Examples

Nuvolachalk.png Note: This article lists no examples. If you make use of this feature in your own project(s) or know of any basic examples that could be shared, please add them. This will make it easier to understand for future authors seeking assistance. Your contributions are greatly appreciated.


See also