BF2.US bf2-rsdeveloper wiki Parity Discord bf2.us
On this page

Weapons: a fire arm's components, one update, one shot

BF22142Heroes

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.

GenericFireArm: one update asks the components; one shot tells them fire the trigger; makes the batch ammo rounds, magazines, reload deviation how far a shot strays recoil the kick zoom levels, the scope target lock-on, painting sound fire, reload, deploy animation hands and weapon the weapon's update, 30 a second busy? -> tell the fire component the ammo -> fire when due -> reload -> zoom -> recoil, target, animation a shot: for each projectile the deviation, then recoil, target and zoom; after the batch the ammo once, the animation, the sound bf2_w32ded.exe!0x00585440 (update), 0x00584a80 (fire), 0x005840b0 (the fire event), 0x00586050 (onCreated)
The fire arm in the middle asks its components once a tick; a shot is one event that all of them hear.

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:

ComponentClassesDoes
fireSingleFireComp, MultiFireComp, LPFireComp, ThrownFireComp, ExplosivesFireComp, SpawnObjectFireCompthe trigger's state machine; makes the projectiles of a shot
ammoDefaultAmmoComp, ReplenishingAmmoComprounds, magazines, the reload's state machine
deviationSoldierDeviationComp, SimpleDeviationComp, TargetDeviationComphow far a shot strays (ticks and deviation)
recoilSoldierBasedRecoilComp, WeaponBasedRecoilCompthe kick
zoomDefaultZoomComp, ToggleCameraCompzoom levels, the scope's camera
targetSimpleTargetComp, LPTargetComp, MultiFireArmTargetCompwhat the weapon aims at: lock-on, laser painting
soundDefaultSoundCompfire, reload, deploy sounds (the sound engine)
animationDefaultAnimationComp, ThrownAnimationCompthe 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:

  1. Activation. A weapon that was just taken in hand or put away handles that first.
  2. 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.
  3. The fire component is told the ammo: the rounds in the magazine and the magazines left.
  4. A shot that is due is fired (below).
  5. 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.
  6. The fire component's update, and its request to fire: taken when nothing is busy and the ammo accepts.
  7. Zoom: a changed zoom level goes to the recoil, the deviation (its zoom modifier) and the target.
  8. Recoil, target, animation.
  9. 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):

  1. For a soldier's weapon: the team checks, the zoom's and the target's consent.
  2. The fire component gets the ammo's state and whether the zoom allows a shot.
  3. The fire component makes the batch (SingleFireComp 0x005a1d70 -> FireComp::fireProjectiles 0x005c0e00 -> spawnProjectile 0x005c0770 for each member): one bullet, or a shotgun's pellets.
  4. The dispatcher (0x005840b0) builds a fire event for each projectile and calls onFire on the components.
Fire event fieldMeaning
counthow many projectiles the batch has
projectilethe object
indexthis projectile's place in the batch
sub-projectileset 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).

a shot (once per batch, not per pellet)
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
Battlefield Heroes

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#

WhatWhere
fire templates and the ratecrates/bf2-gameplay/src/fire/
ammo, deviation, recoil, heatcrates/bf2-gameplay/src/weapon/ (ammo.rs, deviation.rs, recoil.rs, heat.rs)
thrown weapons, zoom, the replenishing ammo of the medic and ammo bagscrates/bf2-gameplay/src/tools/
projectiles in the worldcrates/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.

Source: docs/dev/weapons/firearm.md · built from bf2-rs e3b4f05 · part of the Battlefield 2 / 2142 / Heroes reimplementation. Not affiliated with EA or DICE.