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

The control block and prediction

BF22142Heroes

A player's own object is the one thing a client does not wait for. The client runs the player's input at once, the server runs the same input when it arrives, and the control block tells the client what the server made of it. The client then takes the server's state and runs again the inputs the server has not seen yet.

Prediction: the client runs its own input at once and is corrected by the control block client server tick 0 tick 1 tick 2 tick 3 tick 4 tick 5 tick 6 key: action n stream 0: the last 3 actions applies action n stream 2: control block set state; replay n+1.. control block: last_action:32, position (3 floats), object:16, the object's records in mode 3 (every mask bit) one block per packet that follows a newly applied action: BF2 and 2142 20 a second (LAN), Heroes 10 bf2_w32ded.exe!0x00556a40 (setControlObject), 0x00557270 (write), BF2.exe!0x004d4b30 (onControlObjectUpdate)
An action goes up in the next packet; the block that follows it comes down with the server's state and the number of the last action applied.

Up: player actions#

Every tick the client builds a player action from the input state (six axes, 41 buttons, a flag: 138 bits) and gives it the next sequence number. Every packet carries the last three (stream 0). The server applies an action once and remembers its number.

Down: the control block#

The block is part of the ghost stream (write bf2_w32ded.exe!0x00557270, read 0x005578a0):

FieldBitsNotes
size12the bits behind this field: a client that does not know the object skips them
last_action32, signedthe last player action the server applied
position3 raw floatsalso the delta reference of the rest of the ghost stream
object16the control object's ghost id
has_second, second1 + 16a second object (the parachute)
in_vehicle, has_id, vehicle1 + 1 + 16the vehicle or kit the soldier is tied to
from_baseline, baseline1 + 3full state, or a delta against a stored baseline
statesevery networkable of the object in mode 3, every mask bit set
extra_size, extra10 + nthe player's own data, written by the player object

The object's records go out with every mask bit: the block is the whole truth about the player's object, not a change list. It may be a delta against one of eight baselines the client has acknowledged, which is what keeps it small.

When a block is written#

There is no timer and no change test. Before each packet of a ghosting connection the server hands the player's last applied action to GhostManager::setControlObject (0x00556a40, BF2142.exe!0x0057e8c0, BFHeroes.exe@bfh126!0x00746841), which drops it when it is the action of the last block. writeTo writes a block when an action is left. So:

one control block in every packet that follows a newly applied action.

The rate is therefore the packet rate of the connection type:

GameBlocks a second on a LAN typeWhy
Battlefield 220connection types 3 to 5: 20 packets a second
Battlefield 214220the same loop (BF2142.exe!0x004b02a0), its own table
Heroes10GSUpdateRate = 10 (BFHeroes.exe@bfh126!0x00740cec): every third tick

Heroes' writer (0x007472be) has two more gates, both off in the settings the game shipped with: BWOPCORate for the whole block and BWOPlayerSyncRate for the player's data behind it.

The replay#

On a block (GameClient::onControlObjectUpdate BF2.exe!0x004d4b30) the client:

  1. sets the object to the server's state;
  2. drops its stored actions up to last_action;
  3. runs the actions after it again, one tick each, with the same code the server runs.

When the client's prediction was right, the replay ends where the client already was and nothing is seen. When it was not (a collision the client did not know of, a push), the object moves to where the server says it is: a correction. Our scenarios count corrections; a "correction storm" fails a run.

What must be identical

Prediction only works when the client computes exactly what the server computes. That is why the soldier's and the vehicles' physics are ported bit for bit, at the precision each process runs at (float precision): a last bit that differs becomes a correction a few ticks later.

The weapon in hand#

SoldierNetworkable::setNetUpdate (BF2.exe!0x0062d4e0) runs an item block for every record it applies that is not a rag doll's, and the control block is such a record. The server writes the held item into every control block (SoldierNetworkable::write bf2_w32ded.exe!0x005ddd00). Battlefield 2's player data block names no weapon, so this is the only way the server's held weapon reaches the player who holds it.

BF2
Battlefield 2 and 2142: the key acts at once

The client selects with its own key in the tick of the key; the server's block, 20 a second, only confirms it. One thing to know: after the last grenade the client switches by itself (AmmoComp::fireComplete BF2.exe!0x00622320) before the server does. The blocks the server sent before its own switch still name the grenade, so the client holds rifle, grenade, rifle for a fifth of a second. The original does the same.

Heroes
Heroes: the key waits for the server

GameLogic::selectWeapon (BFHeroes.exe@bfh126!0x00508a9a) asks whether this is the server, so the client's own key selects nothing. The key's action leaves with the next tick, the server selects in the tick that applies it, the block leaves with the connection's next packet (up to 0.1 s) and the client applies it in the frame that reads it: the round trip, plus 0 to 0.1 s, plus a tick on each side. Measured with our client: 136 to 166 ms on loopback, about 223 ms behind 40 ms of delay each way. The same holds for a widget (an ability's key).

How it is tested#

  • Layouts (crates/bf2-net/src/netstate.rs): the soldier state's fields in the listing's order and widths, the delta classes, a flag written only with its mask bit.
  • Prediction (crates/bf2-client/src/predict.rs, 21 tests): a block replays from the client's own state under the server's record; the dead band; which soldiers are simulated within the camera's range.
  • The server's side (crates/bf2-server/src/ghosts.rs): the block's counters and the ammunition status as the original sums them.
  • Server against client, in play: scenarios log each control block the server sends beside the client's prediction for the same tick and assert that they agree (the Heroes seat look: 508 of 509 blocks equal, the other a late repeat).

Eleven of this page's thirteen addresses stand in a test's own text.

In the code#

WhatWhere
the client's prediction and replaycrates/bf2-client/src/predict.rs (Prediction::control_block_with)
the item blockapply_control_item_bf2, apply_control_item_heroes in the same file
vehiclescrates/bf2-client/src/vehicle_predict.rs
the server's sidecrates/bf2-net/src/ghostmgr.rs (set_control_object, write), crates/bf2-server/src/players_input.rs
the packet ratecrates/bf2-net/src/join.rs (write_packet_if_due, packet_rate, connection_type_row)

Tests that hold the cadence: packets_go_out_at_the_connections_rate (join.rs), a_control_block_goes_with_every_new_action_and_not_with_a_repeated_one (ghostmgr.rs), connection_types_take_the_products_table. Notes: notes/tracks/multiplayer.md. The same loop told as a story, with a stepper: prediction, step by step.

A vehicle's seat is what a control block is about: seats and cameras. The other half, for other players' soldiers: lag compensation.

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