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 Movementa figure's movement (see the module header)type State: Copy + PartialEq + Default + core::fmt::Debugthe movement state (on the grid; Default: before the server said where the figure is)const FIELDS: usizethe fields of the state at the start of the figure's replicated entityfn step(&self, map: &physics::World, state: &Self::State, input: &NetInput, dt: f64) -> Self::Statethe state after one tick ofinput(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::Statethe state of FIELDS integersfn position(state: &Self::State) -> Vec3where the figure is (the error measure, the camera)
enum Outcomewhat reconcile foundStale-- the server's state is older than one already confirmed (a late snapshot)Match-- the prediction for that tick was exactCorrected { error: f64, shift: f64 }-- it differed byerrormetres there; the replay moved the current state byshiftmetresReset-- there was no prediction for that tick (the first state, or history ran out): started from it
struct PredictStatswhat a predictor countedsteps: u64-- ticks predictedchecks: u64-- server states comparedmismatches: u64-- of those, different (Corrected)resets: u64-- starts without a prediction to compare (Reset)replayed: u64-- ticks applied again in replayserrors: 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 ticksfn known(&self) -> boolwhether 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::Statepredicttickwithinput-> the state after itfn 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 oftick, if still in the historyfn reconcile(&mut self, mover: &M, map: &physics::World, tick: u32, server: M::State, dt: f64) -> Outcomethe server's state oftick: compare, and on a difference go back to it and replayfn confirmed(&self) -> Option<(u32, M::State)>the newest confirmed tick and statefn pending(&self) -> usizeticks predicted beyond the confirmed onefn stats(&self) -> &PredictStatswhat it countedfn clear(&mut self)forget everything (a new figure, a new room)struct Smootherthe 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). Abovesnapmetres (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 snapsnaps: u64-- see corrections
impl Smoother
fn new(decay_ms: f64, snap: f64) -> Smootheran offset of 0 decaying withdecay_ms, snapping abovesnapmetresfn correct(&mut self, jump: Vec3)the predicted position jumped byjump(new minus old): the offset takes it backfn reset(&mut self)forget the offset (a new figure, a reset of the prediction)fn advance(&mut self, dt: f64)dtseconds passedfn offset(&self) -> Vec3what a frame adds to the predicted position (blended by alpha) of the own figure