JFLib

game/src/net/predict.rs#

Source: lib/game/src/net/predict.rs

predict -- the client predicts its own figure: every tick it applies its input at once, and when the
server's state for a tick comes it compares; on a difference it goes back to the server's state and
applies its inputs since then again (reconciliation)

  Movement      a figure's movement: the tick's input in, the next state out.  The same code runs on the
                server and in the prediction, so it must give the same bits: f64 with + - * /, the square
                root and the trigonometry of lib/physics::math, no mul_add, no unordered containers, no
                time; and its state must stay on the grid of quant.rs (it is replicated as integers, and
                the server's state must be what the snapshot says).  Whatever the step needs from the past
                (the buttons held before, for edges) belongs in the state.  The reference is
                walker::Walker; G7h's character controller takes its place
  Predictor     the inputs and predicted states since the newest confirmed tick; step, reconcile, the
                current and previous state (a frame blends the two by the tick's alpha)
  Smoother      what a frame draws after a correction: the jump of the predicted position becomes an offset
                that decays in a short time (and snaps above a distance); the predicted state stays exact

A prediction is exact when the server used the client's own inputs: reconcile then finds the same
state and only drops history.  It differs after a missed input (the server repeated the last one), a
teleport, damage or anything else the server did to the figure; then the replay puts the figure where
the server's state and the client's later inputs lead.
  • trait Movement a figure's movement (see the module header)

    • type State: Copy + PartialEq + Default + core::fmt::Debug the movement state (on the grid; Default: before the server said where the figure is)
    • const FIELDS: usize the fields of the state at the start of the figure's replicated entity
    • fn step(&self, map: &physics::World, state: &Self::State, input: &NetInput, dt: f64) -> Self::State the state after one tick of input (dt: the tick's length in seconds)
    • fn to_fields(state: &Self::State, out: &mut [i32]) the state as FIELDS integers (lossless)
    • fn from_fields(f: &[i32]) -> Self::State the state of FIELDS integers
    • fn position(state: &Self::State) -> Vec3 where the figure is (the error measure, the camera)
  • enum Outcome what reconcile found

    • Stale -- the server's state is older than one already confirmed (a late snapshot)
    • Match -- the prediction for that tick was exact
    • Corrected { error: f64, shift: f64 } -- it differed by error metres there; the replay moved the current state by shift metres
    • Reset -- there was no prediction for that tick (the first state, or history ran out): started from it
  • struct PredictStats what a predictor counted

    • steps: u64 -- ticks predicted
    • checks: u64 -- server states compared
    • mismatches: u64 -- of those, different (Corrected)
    • resets: u64 -- starts without a prediction to compare (Reset)
    • replayed: u64 -- ticks applied again in replays
    • errors: Vec<f32> -- the error of every mismatch in metres (for percentiles; the first 100 000)
    • shifts: Vec<f32> -- the shift of the current state of every mismatch
  • struct Predictor<M: Movement> the history of a prediction (see the module header)

impl<M: Movement> Predictor<M>

  • fn new() -> Predictor<M> nothing predicted; history of up to 256 ticks

  • fn known(&self) -> bool whether a server state has come (before it the figure stands at the default state)

  • fn step(&mut self, mover: &M, map: &physics::World, tick: u32, input: NetInput, dt: f64) -> M::State predict tick with input -> the state after it

  • fn current(&self) -> Option<M::State> the newest predicted state (the base without history)

  • fn previous(&self) -> Option<M::State> the state one tick before the current one (for the alpha of a frame)

  • fn at(&self, tick: u32) -> Option<M::State> the predicted state of tick, if still in the history

  • fn reconcile(&mut self, mover: &M, map: &physics::World, tick: u32, server: M::State, dt: f64) -> Outcome the server's state of tick: compare, and on a difference go back to it and replay

  • fn confirmed(&self) -> Option<(u32, M::State)> the newest confirmed tick and state

  • fn pending(&self) -> usize ticks predicted beyond the confirmed one

  • fn stats(&self) -> &PredictStats what it counted

  • fn clear(&mut self) forget everything (a new figure, a new room)

  • struct Smoother the drawn position of the own figure after corrections (see the module header). A correction moves the predicted position by a jump; the offset takes the jump back, so the picture does not move at that moment, and decays like exp(-t / decay) (a rational approximation, the same for any frame rate within a few percent). Above snap metres (a teleport, a return after death) the offset is dropped: the picture jumps with the state. The prediction itself never sees the offset: it stays bit for bit the server's.

    • decay_ms: f64 -- the time constant of the decay (ms; 0: no smoothing)
    • snap: f64 -- a correction (with what is still left) larger than this is not smoothed (m)
    • corrections: u64 -- corrections taken in, and of those dropped by snap
    • snaps: u64 -- see corrections

impl Smoother

  • fn new(decay_ms: f64, snap: f64) -> Smoother an offset of 0 decaying with decay_ms, snapping above snap metres
  • fn correct(&mut self, jump: Vec3) the predicted position jumped by jump (new minus old): the offset takes it back
  • fn reset(&mut self) forget the offset (a new figure, a reset of the prediction)
  • fn advance(&mut self, dt: f64) dt seconds passed
  • fn offset(&self) -> Vec3 what a frame adds to the predicted position (blended by alpha) of the own figure

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