On this page
The control block and prediction
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.
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):
| Field | Bits | Notes |
|---|---|---|
size | 12 | the bits behind this field: a client that does not know the object skips them |
last_action | 32, signed | the last player action the server applied |
position | 3 raw floats | also the delta reference of the rest of the ghost stream |
object | 16 | the control object's ghost id |
has_second, second | 1 + 16 | a second object (the parachute) |
in_vehicle, has_id, vehicle | 1 + 1 + 16 | the vehicle or kit the soldier is tied to |
from_baseline, baseline | 1 + 3 | full state, or a delta against a stored baseline |
| states | every networkable of the object in mode 3, every mask bit set | |
extra_size, extra | 10 + n | the 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:
| Game | Blocks a second on a LAN type | Why |
|---|---|---|
| Battlefield 2 | 20 | connection types 3 to 5: 20 packets a second |
| Battlefield 2142 | 20 | the same loop (BF2142.exe!0x004b02a0), its own table |
| Heroes | 10 | GSUpdateRate = 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:
- sets the object to the server's state;
- drops its stored actions up to
last_action; - 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.
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.
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.
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#
| What | Where |
|---|---|
| the client's prediction and replay | crates/bf2-client/src/predict.rs (Prediction::control_block_with) |
| the item block | apply_control_item_bf2, apply_control_item_heroes in the same file |
| vehicles | crates/bf2-client/src/vehicle_predict.rs |
| the server's side | crates/bf2-net/src/ghostmgr.rs (set_control_object, write), crates/bf2-server/src/players_input.rs |
| the packet rate | crates/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.
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.