game/src/net/snapshot.rs#
Source: lib/game/src/net/snapshot.rs
snapshot -- the replicated world of one tick and its delta encoding
NetState what is replicated of one entity: a kind (the game's) and up to MAX_FIELDS integers
(already quantised by the game: positions on the grid of quant.rs, angles in steps)
Ent a net id (u16), a generation (u8: a new entity under an old id) and its NetState
Snapshot a tick and its entities, sorted by id
Schema how many fields each kind has (both sides know it; a kind with none is invalid)
diff the changes from a base to a snapshot: Update (fields that differ), Add (new, or a new
generation or kind under an old id), Remove
encode the chosen changes as bits: a count, then per change its id gap, an op (2 bits), and for
an update a mask of the changed fields with their differences (svar); an add sends its
generation, kind and fields against the map's baseline entry (when it has one of the same
kind) or against zero
decode the inverse against the same base (None for anything malformed: never a panic)
select the changes that fit a budget in a given order (the caller's priorities), exactly
measured; apply builds what the client will hold: the base with the chosen changes
The delta is taken against a base the client holds (docs/NETCODE.md): the newest snapshot it confirmed
over UDP, the last one sent over a WebSocket, or the map's baseline before it has any. A change left out
for the budget is simply not in the as-sent snapshot; the next delta carries it.
/// fields an entity may have at most
MAX_FIELDS: usize = 16
/// entities a snapshot may have at most (a decoder refuses more)
MAX_ENTITIES: usize = 8192
struct NetStatethe replicated values of one entity (fields past its kind's count stay 0)kind: u8-- the game's kind of entity (Schema says its field count)fields: [i32; MAX_FIELDS]-- the values, quantised by the game
impl NetState
fn new(kind: u8, f: &[i32]) -> NetStatea state ofkindwith these fields (the rest 0)struct Entan entity of a snapshotid: u16-- the net id (the server's; not the World's Entity)generation: u8-- counts up when the id is given to a new entity, so a client never blends the old into the newstate: NetState-- the values
struct Snapshotthe replicated world of a ticktick: u32-- the server's tick (wraps: compare with tick_diff)ents: Vec<Ent>-- the entities, sorted by id, ids unique
fn tick_diff(a: u32, b: u32) -> i32a - b for ticks that wrap (positive: a is later)
impl Snapshot
fn new(tick: u32) -> Snapshotan empty snapshot of a tickfn get(&self, id: u16) -> Option<&Ent>the entity with this idfn put(&mut self, e: Ent)put an entity in (replacing one with its id)fn remove(&mut self, id: u16) -> Option<Ent>take an entity outfn sort(&mut self) -> boolsort by id (a builder that pushed in another order); false when two share an idstruct Schemathe field count of every kind (both sides of a game share it)
impl Schema
const fn new() -> Schemano kindsfn with(mut self, kind: u8, fields: usize) -> Schemathe same withkindhavingfieldsfields (1 ..= MAX_FIELDS)fn fields(&self, kind: u8) -> usizethe fields of a kind (0: not a kind of this game)enum Opwhat a change doesUpdate-- fields of an entity the base has changedAdd-- an entity the base does not have (or has under another generation or kind)Remove-- an entity of the base is gone
struct Changeone change from a base to a snapshotid: u16-- the entityop: Op-- what happens to itbits: usize-- the bits of its op and body (without the id gap)
fn diff(schema: &Schema, base: Option<&Snapshot>, cur: &Snapshot, baseline: Option<&Snapshot>, out: &mut Vec<Change>)the changes frombase(None: nothing) tocur, in id orderfn encoded_bits(chosen: &[Change]) -> usizethe exact bits of encodingchosen(in id order): the count and every change with its id gapfn select(changes: &[Change], order: &[usize], budget: usize) -> Vec<Change>the changes oforder(indices intochanges, the most important first) that fitbudgetbits, in id order. Greedy by importance with the id gaps counted at 10 bits, then measured exactly and the least important dropped while too largefn encode(w: &mut BitWriter, schema: &Schema, base: Option<&Snapshot>, cur: &Snapshot, baseline: Option<&Snapshot>, chosen: &[Change])writechosen(from diff, in id order) againstbasefn apply(base: Option<&Snapshot>, cur: &Snapshot, chosen: &[Change]) -> Snapshotbase with the chosen changes of cur: what the client holds after decoding them (the as-sent snapshot)fn decode(r: &mut BitReader, schema: &Schema, base: Option<&Snapshot>, baseline: Option<&Snapshot>, tick: u32) -> Option<Snapshot>read the changes againstbaseinto the snapshot oftick(None: malformed, an update or remove of an id the base lacks, a kind the schema does not know, ids out of order)fn full_bits(schema: &Schema, base: Option<&Snapshot>, cur: &Snapshot, baseline: Option<&Snapshot>) -> usizethe bits of the whole snapshot encoded againstbasewithout a budget (the size of a join's state)