Weapons: a fire arm's components, one update, one shot
A rifle, a tank's cannon, a medic bag and a hand grenade are the same class of object:
GenericFireArm. What makes them differ is which components their template creates, and each
component's numbers. The weapon itself is a short loop that asks its components in a fixed order.
The components#
GenericFireArm (template class 0x94d3, object constructor bf2_w32ded.exe!0x00585e20) is an animated bundle whose behaviour lives in template components created by name (the object system). At creation (0x00586050) it looks each one up by its interface id:
| Component | Classes | Does |
|---|---|---|
| fire | SingleFireComp, MultiFireComp, LPFireComp, ThrownFireComp, ExplosivesFireComp, SpawnObjectFireComp | the trigger's state machine; makes the projectiles of a shot |
| ammo | DefaultAmmoComp, ReplenishingAmmoComp | rounds, magazines, the reload's state machine |
| deviation | SoldierDeviationComp, SimpleDeviationComp, TargetDeviationComp | how far a shot strays (ticks and deviation) |
| recoil | SoldierBasedRecoilComp, WeaponBasedRecoilComp | the kick |
| zoom | DefaultZoomComp, ToggleCameraComp | zoom levels, the scope's camera |
| target | SimpleTargetComp, LPTargetComp, MultiFireArmTargetComp | what the weapon aims at: lock-on, laser painting |
| sound | DefaultSoundComp | fire, reload, deploy sounds (the sound engine) |
| animation | DefaultAnimationComp, ThrownAnimationComp | the hands' and the weapon's animations |
Any of them may be missing: a weapon without an ammo component never reloads, one without a deviation component shoots straight. Heroes adds its own classes to the same slots: the ability's ammo component (abilities).
Every component has the same first methods: update(dt, flags), onFire(event), and isBusy().
One update#
The weapon's update (0x00585440), in this order:
- Activation. A weapon that was just taken in hand or put away handles that first.
- Busy? The weapon is busy while its own timer runs or any of ammo, fire, zoom or animation says so, or it is overheated. While nothing is busy a timer counts down, and when it runs out the weapon's active flag is cleared.
- The fire component is told the ammo: the rounds in the magazine and the magazines left.
- A shot that is due is fired (below).
- The ammo component's update: the reload's state machine. A finished reload is told to fire, ammo, animation and sound; a wanted reload starts when fire, zoom and target accept it.
- The fire component's update, and its request to fire: taken when nothing is busy and the ammo accepts.
- Zoom: a changed zoom level goes to the recoil, the deviation (its zoom modifier) and the target.
- Recoil, target, animation.
- The bundle's own update.
With the update's flag 4, the flag the soldier's and the engine's updates read as "a replay" (the control block), the recoil component alone is updated.
The deviation's per-tick update is not in this list: it is called from the weapon's input handling, which the soldier calls every tick with the player's input (the soldier).
One shot#
GenericFireArm::fire (0x00584a80):
- For a soldier's weapon: the team checks, the zoom's and the target's consent.
- The fire component gets the ammo's state and whether the zoom allows a shot.
- The fire component makes the batch (
SingleFireComp0x005a1d70->FireComp::fireProjectiles0x005c0e00->spawnProjectile0x005c0770for each member): one bullet, or a shotgun's pellets. - The dispatcher (
0x005840b0) builds a fire event for each projectile and callsonFireon the components.
| Fire event field | Meaning |
|---|---|
| count | how many projectiles the batch has |
| projectile | the object |
| index | this projectile's place in the batch |
| sub-projectile | set for the last ones of a batch, by a count the fire component gives |
For every projectile the deviation hears the event (so fireDevAdd adds up per pellet); for the projectiles that are not sub-projectiles the recoil, the target and the zoom hear it too. After the loop the ammo hears it once (one round for a shot, however many pellets), then the animation and the sound.
Heat#
A mounted gun overheats. The template gives heatAddWhenFire, coolDownPerSec and overheatPenalty; the weapon keeps its heat, a reference heat and the tick of the reference (ours weapon::heat in bf2-gameplay).
heat = heatAddWhenFire + heat
if heat > 1 and the weapon does not inherit its heat: heat = overheatPenalty (a penalty of 10 or more is 10)
reference = heat; reference tick = now
cooling (while the fire component rests)
heat = max(reference - (now - reference tick) / 30 x coolDownPerSec, 0)isOverheated is heat >= 1, and it is part of the busy test: an overheated weapon cannot fire until it has cooled below 1. With an overheatPenalty above 1 that takes (penalty - 1) / coolDownPerSec seconds.
A detail for prediction: on the host the reference moves to the new value at every call; a client keeps cooling from its last shot. A weapon with inheritHeatFromPrimary reads the primary weapon's heat.
What differs per game#
Heroes keeps the structure and changes the parts: the rate's setter is the same whole-tick rule (0x007958ae), a reload's length takes off the player's ability rate (0x0079b215), the deviation's sum has a zoom factor on minDev (0x0077b2c6), and the damage of a hit has its own chain (damage). The nine default weapons set no fire, turn, speed or misc deviation at all: only minDev.
How it is tested#
- Oracle tests: the original's fire arm driver, zoom, thrown-fire and missile guidance components are run in a process of its own on a list of inputs, and the port must give the same output bit for bit (the oracle) (
crates/bf2-gameplay/tests/it_bf2/tools_oracle.rs), with the shipped weapons' values and for the server and the client. - Ammunition (
crates/bf2-gameplay/src/weapon/ammo.rs, 37 tests): the reload's phases, automatic reload, the magazines a dropped kit keeps in 2142. - Heroes' reload start has a test program of its own in a Heroes process (
reload_start_bfh126.rs), written from the function read to its end.
Three of this page's eleven addresses stand in a test's own text; the driver's others are held by the oracle's digests, which name the component and not each function.
In the code#
| What | Where |
|---|---|
| fire templates and the rate | crates/bf2-gameplay/src/fire/ |
| ammo, deviation, recoil, heat | crates/bf2-gameplay/src/weapon/ (ammo.rs, deviation.rs, recoil.rs, heat.rs) |
| thrown weapons, zoom, the replenishing ammo of the medic and ammo bags | crates/bf2-gameplay/src/tools/ |
| projectiles in the world | crates/bf2-world (projectile) |
Notes: notes/subsystems/gameplay-weapons.md, gameplay-projectiles.md. To change a weapon: add a weapon.
What a shot becomes: projectiles, damage and armor, guided missiles and flares.
docs/dev/weapons/firearm.md · built from bf2-rs e3b4f05 · part of the Battlefield 2 / 2142 / Heroes reimplementation. Not affiliated with EA or DICE.