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

Ghosts: how objects reach the client

BF22142Heroes

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.

One GhostManager per connection (bf2_w32ded.exe!0x00558b20) World objects each with up to 12 networkables Scope pass (every 0.1 s) resetGhostStates, then ghostObject Ghost records id:16, 12 dirty masks of 32 bits Packet writer removals first, then updates by priority Entry kind:2 id:16 flag:1 size:11 + state Client creates, updates or removes the ghost Reliability without re-sending packets delivered: the acknowledge mask of a later packet names it; the fields it carried stay clean lost: the fields that packet carried are marked dirty again, minus the fields re-sent since (0x005591f0) so a later packet carries the newest state of those fields, never the old bytes at most 40 entries a packet; at most 20 connections get a scope pass per frame
One ghost manager per connection: objects come into scope, get a record with dirty masks, and leave in packets by priority.

The stream#

The ghost stream is the third of a packet's three streams. Write bf2_w32ded.exe!0x00558df0, read 0x00557f20:

FieldBitsNotes
present10 when there is neither a control block nor an entry
tick32the server's tick; the receiver's time is tick / 30 s
count8entries, at most 40 a packet (0x00549300)
has_control1a control block follows
entriesremovals 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.

kindMeaning
0nothing
1update: create the ghost if the client does not know the id, then apply the state
2a read error
3remove 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.

  • 0x00559ff0 writes every networkable through its write(stream, 0, mask, mode); 0x0055a240 reads.
  • 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.

One greedy object can starve a player

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):

  1. The control object is handed to the ghost manager (ServerConnection::updateControlObject 0x0054bd80).
  2. 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).
  3. 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):

text
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 1Type 2Types 3 to 5
Battlefield 2 (0x007902c0)4 packets/s, 320 bytes10 packets/s20 packets/s
Battlefield 2142 (BF2142.exe data 0x00928408)4 packets/s, 260 bytes10 packets/s20 packets/s
Heroes 1.126 (data 0x00f110b0)4 packets/s10 packets/s10 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#

WhatWhere
the ghost managercrates/bf2-net/src/ghostmgr.rs (GhostManager)
the world's side: which objects are in scopecrates/bf2-world/src/ghosts.rs (WorldGhosts)
the server's hookcrates/bf2-server/src/ghosts.rs (ServerGhosts)
networkable layoutscrates/bf2-net/src/netstate.rs, netstate_2142.rs
the client's ghostscrates/bf2-client/src/ghost_vehicles.rs and its neighbours

Notes: notes/subsystems/network.md ("Stream 2: ghosts", "Ghost manager"), notes/tracks/multiplayer.md.

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