AmmoDef: Difference between revisions

From Valve Developer Community
Jump to navigation Jump to search
(Created page with 'The '''AmmoDef''' stores types of hitscan ammunition. It is accessed from the global <code>CAmmoDef* GetAmmoDef()</code> function, which is conventionally written in the [[G…')
 
m (→‎Registering and retrieving an ammo type: removed the TODO. seemed extremely obvious.)
 
(6 intermediate revisions by 2 users not shown)
Line 1: Line 1:
The '''AmmoDef''' stores types of [[hitscan]] ammunition.
The '''AmmoDef''' defines types of [[hitscan]] ammunition. It is accessed from the global <code>CAmmoDef* GetAmmoDef()</code> function, which is conventionally implemented within the shared [[GameRules]] CPP file.


It is accessed from the global <code>CAmmoDef* GetAmmoDef()</code> function, which is conventionally written in the [[GameRules]] CPP file (but remains independent from them).
== Minimum implementation ==


== Minimum implementation ==
<source lang=cpp>
#include "ammodef.h"


<source lang=cpp>CAmmoDef* GetAmmoDef()
CAmmoDef* GetAmmoDef()
{
{
static CAmmoDef ammo;
static CAmmoDef ammo;
Line 12: Line 13:
if ( bInitialising )
if ( bInitialising )
{
{
// Call ammo.AddAmmoType() here
// Call ammo.AddAmmoType(...) here
bInitialising = false;
bInitialising = false;
}
}


return &ammo;
return &ammo;
}</source>
}
</source>


There is no initialisation function for the AmmoDef in Valve's code, so they instead use [[Wikipedia:Static variable|static variables]] to initialise and return a <code>CAmmoDef</code> object that persists between calls.
There is no initialisation function for the AmmoDef in Valve's code, so they instead use [[Wikipedia:Static variable|static variables]] to initialise and return a <code>CAmmoDef</code> object that persists between calls.
Line 25: Line 27:
'''<code>AddAmmoType()</code>''' registers a new bullet type. It has an overload that allows the definition of player damage, NPC damage and carry amount through [[cvar]]s instead of hardcoded integers.
'''<code>AddAmmoType()</code>''' registers a new bullet type. It has an overload that allows the definition of player damage, NPC damage and carry amount through [[cvar]]s instead of hardcoded integers.


Otherwise, these are the arguments used to set each value, followed after a slash by their accessor function:
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 [[#Other accessors|index]]):


; <code>[[char]] name</code>
; <code>[[char]] name</code>
Line 32: Line 34:
: The ammo's [[damage type]].
: The ammo's [[damage type]].
; <code>int tracerType / TracerType()</code>
; <code>int tracerType / TracerType()</code>
: The type of tracer effect drawn when the ammo is fired. Uses the <code>AmmoTracer_t</code> [[enum]].
: The type of tracer effect drawn when the ammo is fired. Uses the <code>AmmoTracer_t</code> [[W:Enumerated_type|enum]].
:* <code>TRACER_NONE</code>
:* <code>TRACER_NONE</code>
:* <code>TRACER_LINE</code>
:* <code>TRACER_LINE</code>
Line 49: Line 51:
: Generic [[flag]]s with which you can define your own ammo properties. Valve's are:
: Generic [[flag]]s with which you can define your own ammo properties. Valve's are:
:* <code>AMMO_FORCE_DROP_IF_CARRIED</code> - player will drop any held physics objects the ammo hits. Only set up for singleplayer in stock code.
:* <code>AMMO_FORCE_DROP_IF_CARRIED</code> - player will drop any held physics objects the ammo hits. Only set up for singleplayer in stock code.
:* <code>AMMO_INTERPRET_PLRDAMAGE_AS_DAMAGE_TO_PLAYER</code> - if an NPC fires the ammo, the player damage value is the damage done ''to'' players. {{todo|What happens when a player fires the ammo?}}
:* <code>AMMO_INTERPRET_PLRDAMAGE_AS_DAMAGE_TO_PLAYER</code> - if an NPC fires the ammo, the player damage value is the damage done ''to'' players.
; <code>int minSplashSize = 4 / MinSplashSize()</code>
; <code>int minSplashSize = 4 / MinSplashSize()</code>
; <code>int maxSplashSize = 8 / MaxSplashSize()</code>
; <code>int maxSplashSize = 8 / MaxSplashSize()</code>
Line 56: Line 58:
=== Other accessors ===
=== Other accessors ===


; <code>Ammo_t* GetAmmoOfIndex(int)</code>
To use these, you only need to <code>#include "ammodef.h"</code>.
 
; <code>Ammo_t* GetAmmoDef()->GetAmmoOfIndex([[int]])</code>
: Returns an entire ammo definition in one operation.
: Returns an entire ammo definition in one operation.
; <code>int Index(string)</code>
; <code>int GetAmmoDef()->Index([[string]])</code>
: Searches the available ammo types by name. Returns the ammo's index on success or -1 on failure.
: Searches the available ammo types by name. Returns the ammo's index on success or -1 on failure.


[[Category:Programming]]
[[Category:Weapons programming]]

Latest revision as of 19:23, 20 May 2018

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.