JFLib

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 NetState the 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]) -> NetState a state of kind with these fields (the rest 0)

  • struct Ent an entity of a snapshot

    • id: 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 new
    • state: NetState -- the values
  • struct Snapshot the replicated world of a tick

    • tick: 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) -> i32 a - b for ticks that wrap (positive: a is later)

impl Snapshot

  • fn new(tick: u32) -> Snapshot an empty snapshot of a tick

  • fn get(&self, id: u16) -> Option<&Ent> the entity with this id

  • fn 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 out

  • fn sort(&mut self) -> bool sort by id (a builder that pushed in another order); false when two share an id

  • struct Schema the field count of every kind (both sides of a game share it)

impl Schema

  • const fn new() -> Schema no kinds

  • fn with(mut self, kind: u8, fields: usize) -> Schema the same with kind having fields fields (1 ..= MAX_FIELDS)

  • fn fields(&self, kind: u8) -> usize the fields of a kind (0: not a kind of this game)

  • enum Op what a change does

    • Update -- fields of an entity the base has changed
    • Add -- an entity the base does not have (or has under another generation or kind)
    • Remove -- an entity of the base is gone
  • struct Change one change from a base to a snapshot

    • id: u16 -- the entity
    • op: Op -- what happens to it
    • bits: 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 from base (None: nothing) to cur, in id order

  • fn encoded_bits(chosen: &[Change]) -> usize the exact bits of encoding chosen (in id order): the count and every change with its id gap

  • fn select(changes: &[Change], order: &[usize], budget: usize) -> Vec<Change> the changes of order (indices into changes, the most important first) that fit budget bits, in id order. Greedy by importance with the id gaps counted at 10 bits, then measured exactly and the least important dropped while too large

  • fn encode(w: &mut BitWriter, schema: &Schema, base: Option<&Snapshot>, cur: &Snapshot, baseline: Option<&Snapshot>, chosen: &[Change]) write chosen (from diff, in id order) against base

  • fn apply(base: Option<&Snapshot>, cur: &Snapshot, chosen: &[Change]) -> Snapshot base 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 against base into the snapshot of tick (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>) -> usize the bits of the whole snapshot encoded against base without a budget (the size of a join's state)

Source: docs/API.md in the JFLib repository.