On this page
Weapons: ticks, deviation and the random table
A fire arm is an object with components: a fire component that decides when a shot leaves, an ammo component that counts rounds, a deviation component that decides how far the shot strays, and more for recoil, zoom and sound. Two things about them surprise people who tune weapons: every timer is a whole number of ticks, and deviation is not a cone but a sum of parts with a Gaussian spread.
A weapon counts in ticks#
Fire, ammo and weapon components keep their timers as integer simulation ticks, 30 a second (the frame and the tick). The template setters convert:
| Property | Conversion | Where |
|---|---|---|
fire.roundsPerMinute | ticks between shots = 1800 / rpm, integer division; the default 600 gives 3 | bf2_w32ded.exe!0x005bf9c0, 0x005c06e0 |
ammo.reloadTime, fire.fireLaunchDelay, ... | (int)(seconds x 30.0f), truncated | 0x005c1a90, 0x005bf9e0 |
Two consequences:
- Rates are quantised. Only divisors of 1800 are what they say: 900, 600, 450, 360, 300. A weapon written as 750 rounds a minute fires every 2 ticks, which is 900; so does 650. (All 94 weapons the original games ship with a
roundsPerMinuteuse a divisor of 1800: the designers knew.) - The client and the server can disagree by a tick.
0.7f x 30is 20.9999996 at a server's 53 bits and truncates to 20; a client rounds the product to 24 bits first, gets 21.0 and keeps 21 (float precision). Of the 168 delay values in the shipped weapons, 15 convert to a tick more on the client: every 0.7 s value (medic bag, ammo bag, grenades) is 21 against 20, a reload of 4.7 s is 141 against 140.
The conversion is fmul and fistp with the rounding control forced to truncate. Ghidra shows ROUND(x * 30), which is wrong for every value whose fraction is a half or more. The port follows the instructions.
Deviation#
SoldierDeviationComp (template class TemplateComponent.SoldierDeviationComp; ours SoldierDeviation in crates/bf2-gameplay/src/weapon/deviation.rs) holds a floor and four parts. A template sets each part with one line:
ObjectTemplate.deviation.minDev 0.3
ObjectTemplate.deviation.setFireDev <max> <add> <sub>
ObjectTemplate.deviation.setTurnDev <max> <addPitch> <addYaw> <sub>
ObjectTemplate.deviation.setSpeedDev <max> <addForward> <addStrafe> <sub>
ObjectTemplate.deviation.setMiscDev <max> <addJump> <sub>
ObjectTemplate.deviation.devModStand 1.0
ObjectTemplate.deviation.devModCrouch 0.7
ObjectTemplate.deviation.devModLie 0.5
ObjectTemplate.deviation.devModZoom 0.6Every tick#
update(input, miscTrigger, firing) (bf2_w32ded.exe!0x005a96e0; ours SoldierDeviation::update), called from the weapon's input handling. The inputs count only beyond a dead zone of 0.01.
speed: acc = (moving forward ? addForward : 0) + (strafing ? addStrafe : 0)
acc == 0 -> speed = max(speed - sub, 0)
speed >= max -> speed = max(speed - sub, max)
else -> speed = min(speed + acc, max) (no sub while moving)
turn: acc = |lookY| x addPitch + |lookX| x addYaw
acc == 0 -> turn = max(turn - sub, 0)
turn >= max -> turn = max(turn - sub, max)
else -> turn = clamp(turn + (acc - sub), 0, max)
misc: trigger and addJump != 0:
misc >= max -> misc = max(misc - sub, max)
else -> misc = clamp(misc + (addJump - sub), 0, max)
else -> misc = max(misc - sub, 0)
fire: not firing -> fire = max(fire - fireDevSub, 0)
dev = (minDev + misc + speed + turn + fire) x zoom modifier x stance modifier- The misc trigger is "jump pressed, or going prone outside a vehicle" (the soldier). A jump that the stance cancels still counts.
- The stance modifier is
devModStandstanding and swimming,devModCrouch,devModLie; the zoom modifier isdevModZoomwhile zoomed, else 1. - A part above its maximum decays toward the maximum, not below it, while the input lasts.
- An owner that is not a soldier (a vehicle's gun) skips the movement parts.
- A bot gets an extra term on top; in co-op its deviation is
(100 - botSkill) x 0.1instead.
Every shot#
DeviationComp::onFire (0x005c2c50; ours SoldierDeviation::on_fire) runs once per projectile, so a shotgun's fireDevAdd adds up per pellet:
dev = a sub-projectile ? template.subProjectileDev : this.deviation
if dev > 0.01:
i = (the input's sequence + the shot's index) & 1023
a = dev x table[i]; b = dev x table[(i + 1) & 1023]
s = weaponSpeed x 0.01
velocity += up x (s x a) + right x (s x b)
fire = min(fireDevAdd + fire, fireDevMax)So deviation is no cone with a hard edge. Each axis gets an independent Gaussian offset whose standard deviation is atan(0.01 x dev): one unit of deviation is about 0.57 degrees. Two thirds of the shots land within one unit on an axis; one in twenty is beyond two.
Try it#
Fire, run, turn, jump, lie down: the bench runs the two blocks above 30 times a second and puts each shot on a target 50 m away. The ring is one standard deviation.
The random table#
The offsets do not come from a random generator at the moment of the shot. They come from a table of 1024 Gaussian numbers (0x0088c6a0), built once by the first deviation component the process constructs (0x005c2ed0): the engine's generator seeded with 0x20fee, 1024 draws, stored as floats. The index is the sequence number of the player's input plus the shot's index, which both sides know, so the client can predict where its own bullet goes.
| Piece | What it is | Where |
|---|---|---|
| the generator | multiply by 24112, then by 26143, each modulo 2^31 - 1 (together the multiplier 630360016) | 0x00580750; ours Lehmer in crates/bf2-gameplay/src/random.rs |
| a unit number | k = ((state | 0x80) >> 7) + 1, u = k x 2^-24 | 0x007d0558 |
uniform(a, b) | (b - a) x k x 2^-24 + a | 0x005807e0 |
exponential(mean) | -(ln(u) x mean) | 0x005889e0 |
gaussian() | Marsaglia's polar method; the second value of each pair is kept in a process-wide cache that every caller shares | 0x00588a10 |
| distributions in templates | TYPE/a/b/symmetric with CRD_NONE, CRD_UNIFORM, CRD_EXPONENTIAL, CRD_NORMAL | 0x005c12e0 |
The 1024 draws are computed on the x87, so 535 of the 1024 entries differ in their low bits between a client (24 bits) and a server (53 bits). The client's predicted bullet therefore leaves in a slightly other direction than the server's. The original has this difference; the port reproduces both tables (deviation_table).
What else is in a fire arm#
| Component | Does | Notes |
|---|---|---|
fire (SingleFireComp, MultiFireComp, ThrownFireComp, ...) | the trigger's state machine, the delay before a shot, burst and batch sizes | the start delay is drawn from a seed of the weapon or the tick |
| ammo | rounds, magazines, reload timers; replenishing ammo for the medic bag, ammo bag, repair tool | Heroes adds the ability's clock (abilities) |
| recoil | the kick on the view | soldier-based recoil is verified |
| zoom | zoom levels and their delays | sets the deviation's zoom flag |
| heat | overheating of mounted guns |
The weapon in a player's hands is selected on the server and reaches its owner through the control block (the control block). To change a weapon's values see add a weapon.
How it is tested#
- Bit for bit against the original's functions, at both precisions: 20,001 values of the seconds-to-ticks conversion (35 differ between client and server, as in the original); the deviation's update and shot follow the listing's stores and compares.
tests/retail_weapons.rsreads every weapon script of an install (5,004 scripts in Battlefield 2 and its expansion) and checks the conversions the port makes of them.- The kit x item x action sweep uses every item of every kit (test rigs).
See also a fire arm's components and a shot. Notes: notes/subsystems/gameplay-weapons.md. Code: crates/bf2-gameplay/src/weapon/ (deviation.rs, ammo.rs, recoil.rs, heat.rs), crates/bf2-gameplay/src/random.rs.
Where the deviated shot goes next: projectiles.
In the code#
The crates this page names, each with its line of docs/release/LAYOUT.md (the list is gathered by the wiki's build from the page itself):
| Crate | What it is |
|---|---|
crates/bf2-gameplay | the behaviour of gameplay objects: weapons, projectiles, soldiers, vehicles |
Files this page names: crates/bf2-gameplay/src/weapon/deviation.rs, crates/bf2-gameplay/src/random.rs.
docs/dev/weapons/deviation.md · built from bf2-rs e3b4f05 · part of the Battlefield 2 / 2142 / Heroes reimplementation. Not affiliated with EA or DICE.