AmmoDef

From Valve Developer Community
Jump to: navigation, search

The AmmoDef defines types of hitscan ammunition. It is accessed from the global CAmmoDef* GetAmmoDef() function, which is conventionally implemented within the shared GameRules CPP file.

Minimum implementation

#include "ammodef.h"

CAmmoDef* GetAmmoDef()
{
	static CAmmoDef ammo;
	
	static bool bInitialising = true;
	if ( bInitialising )
	{
		// Call ammo.AddAmmoType(...) here
		bInitialising = false;
	}

	return &ammo;
}

There is no initialisation function for the AmmoDef in Valve's code, so they instead use static variables to initialise and return a CAmmoDef object that persists between calls.

Registering and retrieving an ammo type

AddAmmoType() registers a new bullet type. It has an overload that allows the definition of player damage, NPC damage and carry amount through cvars instead of hardcoded integers.

Otherwise, these are the arguments used to set each value, followed after a slash by their accessor function (each accessor takes as its argument the ammo's index):

char name
The unique name of the ammo. Used in weapon scripts and sometimes in code.
int damageType / DamageType()
The ammo's damage type.
int tracerType / TracerType()
The type of tracer effect drawn when the ammo is fired. Uses the AmmoTracer_t enum.
  • TRACER_NONE
  • TRACER_LINE
  • TRACER_RAIL
  • TRACER_BEAM
  • TRACER_LINE_AND_WHIZ
int plr_dmg / PlrDamage()
int npc_dmg / NPCDamage()
Absolute damage done per-bullet when the ammo is fired by a player or an NPC.
int carry / MaxCarry()
How much of this ammo a player can carry. NPCs normally carry an infinite supply. typedef INFINITE_AMMO = -1.
float physicsForceImpulse / DamageForce()
The length of the vector applied as a physics force to objects the ammo hits, prior to multiplication by the phys_pushscale convar.
Tip.pngTip:Use the BULLET_IMPULSE(grains,ftpersec) macro to convert real-world ballistics data to a VPhysics-ready form. You can find it in any of Valve's gamerules files.
int nFlags / Flags()
Generic flags with which you can define your own ammo properties. Valve's are:
  • AMMO_FORCE_DROP_IF_CARRIED - player will drop any held physics objects the ammo hits. Only set up for singleplayer in stock code.
  • AMMO_INTERPRET_PLRDAMAGE_AS_DAMAGE_TO_PLAYER - if an NPC fires the ammo, the player damage value is the damage done to players.
int minSplashSize = 4 / MinSplashSize()
int maxSplashSize = 8 / MaxSplashSize()
(Optional) The minimum and maximum radius of water splashes created by the ammo. Defaults to 4 and 8.

Other accessors

To use these, you only need to #include "ammodef.h".

Ammo_t* GetAmmoDef()->GetAmmoOfIndex(int)
Returns an entire ammo definition in one operation.
int GetAmmoDef()->Index(string)
Searches the available ammo types by name. Returns the ammo's index on success or -1 on failure.