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

Prediction, step by step: one key press there and back

BF22142Heroes

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.

One action, there and back client server tick 100 tick 101 tick 102 tick 103 tick 104 tick 105 tick 106 tick 107 1 key, action 100 2 run it now 3 packet: actions 98, 99, 100 4 keeps running: 101, 102, 103 .. 5 applies 100 once 6 writes the block control block: last action 100, the state 7 take the server's state 8 replay 101 .. 106 BF2.exe!0x005c0460 (client tick), 0x005b9860 (block read), 0x004d4b30 (replay); bf2_w32ded.exe!0x00557270 (block write)
The eight steps on two time lines. The client never waits: it runs ahead and is brought back into line when the answer comes.

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#

StepWhat happensOriginalPort
1the input is sampled and a player action built (138 bits: six axes, 41 buttons, a flag) with the next numberthe client's tick BF2.exe!0x005c0460 (ClientGame::physicsFrame), the local input 0x005c0260bf2-client predict.rs, bf2-input
2the client runs it: Soldier::handlePlayerInput(input, dt, 0) and handleUpdate(dt, 0), then the soldier's own physics response0x005adea0, 0x005ab850, 0x00700550bf2_physics::soldier_control
3the next packet carries the last three actionsstream 0bf2-net
4the client goes on, one action a tick, each storedthe same tickPrediction
5the server applies the action once and remembers its numberthe server's soldier update bf2_w32ded.exe!0x00552b20bf2-server
6the server writes a control block into the next packet that follows a newly applied actionbf2_w32ded.exe!0x00557270bf2-net
7the client reads the block: the soldier's state is set from itGhostManager::readControlObject 0x005b9860, SoldierNetworkable::setNetUpdate 0x0062d4e0apply_control_state
8every action behind the block's number runs againGameClient::onControlObjectUpdate 0x004d4b30Prediction::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:

  • handlePlayerInput and handleUpdate get 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:

CauseExampleIs it a fault?
the server knew something the client did notanother player stepped into the way; an explosion pushed the soldierno: this is what the block is for
the client's code computes a different numbera last bit of a float, a timer counted twice in the replayyes: 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 handlePlayerInput and handleUpdate run 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.
Vehicles

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#

WhatWhere
the predictioncrates/bf2-client/src/predict.rs
the soldier's controlcrates/bf2-physics/src/soldier_control.rs
the oracle of the client's controloracle/gen_client_ctl.py, tests/client_ctl_oracle.rs

Notes: notes/subsystems/client.md ("Client-side prediction"), notes/tracks/multiplayer.md.

Source: 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.