On this page
Prediction, step by step: one key press there and back
You press W and your soldier moves at once, though the server that decides where he is will not hear of it for a tenth of a second. This page follows that one key press through eight steps: from the input sample to the packet, the server's answer, and the client's replay.
The control block page has the wire format; this is the story in order.
Walk through it#
The stepper's distances are round numbers of its own (0.12 m a tick, 0.05 m when blocked); the order of the steps and what each does are the game's. Set the delay to 6 ticks and the client replays twelve actions inside one tick: that is the cost of a slow connection, paid in CPU and not in waiting.
The eight steps in the code#
| Step | What happens | Original | Port |
|---|---|---|---|
| 1 | the input is sampled and a player action built (138 bits: six axes, 41 buttons, a flag) with the next number | the client's tick BF2.exe!0x005c0460 (ClientGame::physicsFrame), the local input 0x005c0260 | bf2-client predict.rs, bf2-input |
| 2 | the client runs it: Soldier::handlePlayerInput(input, dt, 0) and handleUpdate(dt, 0), then the soldier's own physics response | 0x005adea0, 0x005ab850, 0x00700550 | bf2_physics::soldier_control |
| 3 | the next packet carries the last three actions | stream 0 | bf2-net |
| 4 | the client goes on, one action a tick, each stored | the same tick | Prediction |
| 5 | the server applies the action once and remembers its number | the server's soldier update bf2_w32ded.exe!0x00552b20 | bf2-server |
| 6 | the server writes a control block into the next packet that follows a newly applied action | bf2_w32ded.exe!0x00557270 | bf2-net |
| 7 | the client reads the block: the soldier's state is set from it | GhostManager::readControlObject 0x005b9860, SoldierNetworkable::setNetUpdate 0x0062d4e0 | apply_control_state |
| 8 | every action behind the block's number runs again | GameClient::onControlObjectUpdate 0x004d4b30 | Prediction::control_block |
The local soldier is taken out of the common pass#
A client simulates every object it knows, but the local player's soldier is special. disablePhysics (BF2.exe!0x005b3360) gives it an update frequency that makes the common object pass skip it and marks its physics node so the common node pass skips it too. The prediction code then runs the soldier itself, in the server's order: input, update, the node, the response of the other objects, the soldier's own response (the soldier pass, the terrain test, the impulse, friction), and last the step that stores the previous transformation.
A replayed tick is not a normal tick#
When the client runs a stored action again, three things differ, as the original has it:
handlePlayerInputandhandleUpdateget the flag 4 instead of 0: the air timers do not count down a second time and the weapon gets no input (a replay must not fire a second shot);- the player's triggers are checked with their replay flag;
- the steps that store "the previous transformation" do not run between replayed ticks: the collision tests measure the motion from the control block's transformation.
Get one of these wrong and the client is right most of the time and wrong in a jump, a fall, or on a ladder: the cases where a timer or a previous position decides.
What a correction is#
If the client computed what the server computed, the replay ends where the client already stood, to the last bit, and nothing is seen. If not, the soldier is suddenly where the server's state plus the replay puts him. The causes are of two kinds:
| Cause | Example | Is it a fault? |
|---|---|---|
| the server knew something the client did not | another player stepped into the way; an explosion pushed the soldier | no: this is what the block is for |
| the client's code computes a different number | a last bit of a float, a timer counted twice in the replay | yes: a parity fault, and it shows as a steady shake |
That is why the soldier's control and physics are ported bit for bit at the precision each process runs at: the client at 24 bits of x87 precision, the dedicated server at 53. See float precision.
How it is tested#
- The emulator: the client's own
handlePlayerInputandhandleUpdaterun in the original's code under the emulator against the port: 120 cases and 36,000 ticks at 24 bits, 60 cases and 18,000 ticks at 53, the same bits every tick. - Scenarios: a headless client walks, jumps, climbs and swims against the port's server and the run counts corrections; a run with a storm of corrections fails.
- 2142 and Heroes: the same loop; 2142's differences are listed function by function in the file's head (a turn rate clamp in the input, a breath effect that is presentation only). Heroes sends ten blocks a second where the others send twenty, so a block is older on average when it arrives and the replay a little longer.
A vehicle the player drives is predicted the same way with the vehicle's physics in place of the soldier's. The vehicle replay rig exists because a vehicle has much more state to get wrong.
In the code#
| What | Where |
|---|---|
| the prediction | crates/bf2-client/src/predict.rs |
| the soldier's control | crates/bf2-physics/src/soldier_control.rs |
| the oracle of the client's control | oracle/gen_client_ctl.py, tests/client_ctl_oracle.rs |
Notes: notes/subsystems/client.md ("Client-side prediction"), notes/tracks/multiplayer.md.
docs/dev/network/prediction-walkthrough.md · built from bf2-rs e3b4f05 · part of the Battlefield 2 / 2142 / Heroes reimplementation. Not affiliated with EA or DICE.