On this page
Ghosts: how objects reach the client
A ghost is a client's copy of a server object. The server decides, per connection, which objects that client needs, and sends each one's state field by field, only the fields that changed. No packet is ever sent twice; what was lost is sent again as it is now.
The stream#
The ghost stream is the third of a packet's three streams. Write bf2_w32ded.exe!0x00558df0, read 0x00557f20:
| Field | Bits | Notes |
|---|---|---|
present | 1 | 0 when there is neither a control block nor an entry |
tick | 32 | the server's tick; the receiver's time is tick / 30 s |
count | 8 | entries, at most 40 a packet (0x00549300) |
has_control | 1 | a control block follows |
| entries | removals first, then updates by priority |
The writer reserves the 42 header bits and fills them in at the end, when it knows how many entries fitted.
An entry#
Write 0x00556df0, read 0x00556f10: kind:2 id:16, and for an update flag:1 size:11 and size bits of state.
kind | Meaning |
|---|---|
| 0 | nothing |
| 1 | update: create the ghost if the client does not know the id, then apply the state |
| 2 | a read error |
| 3 | remove the ghost |
The size field is 11 bits wide on a player's connection and 13 on the demo recorder's, which is a connection like any other (id 0x81) with room for bigger first states. Because every entry carries its size, a client that does not know an object can skip it and still read the rest of the packet.
Networkables and dirty masks#
A ghost has up to 12 networkables: the components of the object that have state to send (a soldier's stance and weapon, a vehicle's engine, an armour component's hit points). Each networkable has a 32-bit dirty mask in the ghost's record, one bit per field or group of fields.
0x00559ff0writes every networkable through itswrite(stream, 0, mask, mode);0x0055a240reads.- The mode says who the state is for: a ghost update, a first state, or the owner's own object (mode 3, in the control block, with every mask bit set).
- When a packet is lost, the fields it carried are marked dirty again, minus the fields that were sent again since (
0x005591f0). A field that changed three times while a packet was in flight is sent once.
Every networkable layout of the three games is decoded (bf2-net netstate.rs, netstate_2142.rs and the Heroes layouts) and listed field by field in the generated network reference.
The writer fills a packet by priority until the connection's payload size is reached. An object whose dirty mask names more fields than it needs can fill every packet by itself. Battlefield 2's destroyable objects did exactly that in the port once: an oversized mask starved the player's own ghost. When something "lags" for one player only, look at what else is in his packets (learnings).
The scope pass#
Per server frame, for every connection whose packet timer is due (GameServer::sendToClients bf2_w32ded.exe!0x0044b2e0):
- The control object is handed to the ghost manager (
ServerConnection::updateControlObject0x0054bd80). - The scope pass, when the connection's scope timer has run out (then 0.1 s again), for at most 20
connections a frame:
resetGhostStates(0x00558180): every ghost that is not an "always" ghost is marked for removal; "always" ghosts take priority 255;ghostObject(0x00558340) for every object in scope: the mark comes off and the object gets its priority (below).
- The packet: removals, then updates in priority order, until the payload is full or 40 entries are written.
Static objects whose first networkable has a negative base priority are "always" ghosts: every client has them from the join on (destroyable objects among them).
The priority rule#
The scope pass asks one function for each object (0x0054bee0; ours scope_priority in crates/bf2-net/src/netrecord.rs, verified bit for bit against 12,341 results of the original):
beyond the view distance: 0 (the object leaves the scope)
p = min(|velocity|^2 x 0.000625, 1) moving things first: 1 from 40 m/s on
+ inView 1 inside the player's view cone, or nearer than 10 m
+ (1 - distance / viewDistance) near things first
priority = (p + 0.3 for a projectile) x the networkable's base priority (0x0054c0e0)The view is the player's scope camera; the view distance is the server's, at least 500. Battlefield 2142 has no "nearer than 10 m" rule, and gives a projectile that asks for it a larger radius.
A ghost's priority is added up packet after packet until the ghost is written; the writer takes the highest sums first, and a written ghost starts again at 0. So nothing in scope starves: a far, still object's small priority still adds up to its turn. A new ghost, a removal and a field lost with a packet go in with the priority 10000, ahead of everything.
How fast: the connection type#
A connection's packet rate and payload size come from its connection type (GameServer::setConnectionType 0x004437c0), a row of a table that each game has its own copy of:
| Types 0 and 1 | Type 2 | Types 3 to 5 | |
|---|---|---|---|
Battlefield 2 (0x007902c0) | 4 packets/s, 320 bytes | 10 packets/s | 20 packets/s |
Battlefield 2142 (BF2142.exe data 0x00928408) | 4 packets/s, 260 bytes | 10 packets/s | 20 packets/s |
Heroes 1.126 (data 0x00f110b0) | 4 packets/s | 10 packets/s | 10 packets/s |
A new connection has type 0 until the client's ConnectionType event arrives. Heroes also has a setting, GSUpdateRate, that overrides the connection's own rate when it is not 0; it is registered and shipped as 10.
On the client#
bf2-client creates an object for an update of an unknown id, applies the state of a known one, and removes on a removal. A ghost's state arrives at the server's packet rate, not at the tick rate, so the client keeps the last states and draws each remote object a little in the past, between two of them (the interpolation delay); only the player's own object is predicted ahead (the control block).
How it is tested#
- The ghost manager (
crates/bf2-net/src/ghostmgr.rs, 18 tests): stream managers and their indices, the priority heap, removal, and the control block that is sent whole until acknowledged and then as deltas. - Records and dirty masks (
netrecord.rs, 41 tests): for each kind of networkable, which change sets which mask bit and what the record writes, per game and per server build. - The world's side (
crates/bf2-world/src/ghosts/server.rs, 18 tests): ghost ids that continue through a map change, the scope walk in the manager's order, the owned groups, the extra fields of 2142. - Against the original, in play: the differentials compare what the port's server and the original's send back for the same inputs (test rigs); scoping and priorities over a whole round are held by scenarios' runs, not by a test of their own.
Proven only by a scenario's run, not by a test of their own: several client-side functions that create and remove ghosts.
In the code#
| What | Where |
|---|---|
| the ghost manager | crates/bf2-net/src/ghostmgr.rs (GhostManager) |
| the world's side: which objects are in scope | crates/bf2-world/src/ghosts.rs (WorldGhosts) |
| the server's hook | crates/bf2-server/src/ghosts.rs (ServerGhosts) |
| networkable layouts | crates/bf2-net/src/netstate.rs, netstate_2142.rs |
| the client's ghosts | crates/bf2-client/src/ghost_vehicles.rs and its neighbours |
Notes: notes/subsystems/network.md ("Stream 2: ghosts", "Ghost manager"), notes/tracks/multiplayer.md.
docs/dev/network/ghosts.md · built from bf2-rs e3b4f05 · part of the Battlefield 2 / 2142 / Heroes reimplementation. Not affiliated with EA or DICE.