Partial class for BaseWeapon handling ammo-related properties and actions. It exposes aliases for engine ammo types, checks and takes ammo from magazines or player inventory, supports infinite ammo setting, and handles secondary ammo resources.
public partial class BaseWeapon
{
/// <summary>
/// Is this weapon ammo for itself? eg tripmine, grenades
/// </summary>
[Property, Feature( "Ammo" )] public bool IsSelfAmmo { get; set; } = false;
/// <summary>
/// The <see cref="BaseAmmoResource"/> for this weapon. Alias for the engine's
/// <see cref="BaseCombatWeapon.PrimaryAmmoType"/>.
/// </summary>
public BaseAmmoResource AmmoResource
{
get => PrimaryAmmoType;
set => PrimaryAmmoType = value;
}
/// <summary>
/// A secondary ammo resource, which can be null. Used for alt-fire modes. Alias for the engine's
/// <see cref="BaseCombatWeapon.SecondaryAmmoType"/>.
/// </summary>
public BaseAmmoResource SecondaryAmmoResource
{
get => SecondaryAmmoType;
set => SecondaryAmmoType = value;
}
/// <summary>
/// Can we switch to this gun?
/// </summary>
/// <returns></returns>
public override bool CanSwitch()
{
return HasAmmo() || CanReload();
}
/// <summary>
/// The infinite ammo cheat works by never actually spending the reserve - magazines still drain,
/// so reloads keep their rhythm, they just always have something to draw from.
/// </summary>
protected override int TakeReserveAmmo( BaseAmmoResource ammoType, int amount )
{
if ( GameSettings.InfiniteAmmo )
return amount;
return base.TakeReserveAmmo( ammoType, amount );
}
/// <summary>
/// Takes ammo from the weapon's magazine, or its reserve when it doesn't use one.
/// </summary>
/// <param name="count"></param>
/// <returns></returns>
public bool TakeAmmo( int count )
{
if ( !UsesAmmo ) return true;
return TakePrimaryAmmo( count );
}
/// <summary>
/// Takes ammo from the player's reserve pool for a specific ammo type. Useful if a weapon has an
/// alt fire that feeds from its own type.
/// </summary>
/// <param name="count"></param>
/// <param name="ammoType"></param>
/// <returns></returns>
public bool TakeAmmo( int count, BaseAmmoResource ammoType )
{
if ( !UsesAmmo ) return true;
var inventory = Inventory;
if ( inventory is null || ammoType is null )
return false;
if ( inventory.GetAmmo( ammoType ) < count )
return false;
return inventory.TakeAmmo( ammoType, count ) >= count;
}
/// <summary>
/// Do we have ammo for the weapon's ammo type?
/// </summary>
/// <returns></returns>
public bool HasAmmo()
{
return HasPrimaryAmmo();
}
/// <summary>
/// Do we have ammo for a specific ammo type? Useful if a weapon has an alt fire.
/// </summary>
/// <param name="ammoType"></param>
/// <returns></returns>
public bool HasAmmo( BaseAmmoResource ammoType )
{
if ( !UsesAmmo ) return true;
var inventory = Inventory;
if ( inventory is null || ammoType is null )
return false;
return inventory.GetAmmo( ammoType ) > 0;
}
}