R-Type
Networked shoot'em up built on Aegis2D
Loading...
Searching...
No Matches
rtype::ShipPrediction Class Reference

Where the client's own ship is, before the server says so. More...

#include </__w/project-rtype/project-rtype/client/network/include/rtype/ship_prediction.hpp>

Public Member Functions

void start (std::uint32_t ship, aegis2d::Vec2 position)
 Starts predicting a ship from a position of the server.
 
void stop () noexcept
 Stops predicting: the ship is gone, or the player out of lives.
 
void apply (const InputRecord &input, float delta)
 Moves the ship by an input the client sends, and keeps the input.
 
void acknowledge (std::uint16_t sequence)
 Drops the inputs the server applied.
 
float reconcile (aegis2d::Vec2 server, std::optional< std::uint16_t > acknowledged, float delta)
 Corrects the prediction with a snapshot (server reconciliation).
 
std::uint64_t corrections () const noexcept
 Number of reconciliations that moved the ship by more than correction_threshold: the prediction was wrong.
 
float last_correction () const noexcept
 How far the last reconciliation moved the ship.
 
void smooth (float seconds) noexcept
 Brings the drawn ship towards the predicted one, after a correction.
 
std::optional< aegis2d::Vec2 > drawn () const noexcept
 Where to draw the ship: position(), plus what is left of the corrections.
 
std::optional< std::uint32_t > ship () const noexcept
 Network id of the ship predicted.
 
std::optional< aegis2d::Vec2 > position () const noexcept
 Where the ship is, its inputs applied.
 
const std::deque< InputRecord > & pending () const noexcept
 The inputs applied since start() that the server has not acknowledged.
 

Static Public Attributes

static constexpr std::size_t max_pending = 120
 Inputs kept at most: 2 seconds at 60 per second. Older ones are dropped, as no snapshot acknowledges them on a link that bad.
 
static constexpr float correction_threshold = 0.25F
 Distance from which a reconciliation counts as a correction, in logical pixels.
 
static constexpr float smoothing_speed = 120.0F
 Speed at which the drawn ship catches up with a correction, in logical pixels per second: 2 pixels a frame at 60 frames per second, so a correction of a few pixels takes a few frames instead of a jump.
 
static constexpr float snap_distance = 32.0F
 Corrections from this distance are not smoothed: the ship is far from where it was predicted (a hit, a long stall), and gliding there would show it in the wrong place for long.
 

Detailed Description

Where the client's own ship is, before the server says so.

Starts from a position of the ship the server sent (start()); each input the client sends then moves it at once with step_ship(), the server's own movement, and is kept until a snapshot acknowledges it (acknowledge()): the inputs the server has not applied yet, which server reconciliation replays. The client draws its ship at position() instead of interpolating it 100 ms in the past, so it answers the keys within a frame whatever the round trip time. Not thread-safe: use it from the client's thread.

Member Function Documentation

◆ acknowledge()

void rtype::ShipPrediction::acknowledge ( std::uint16_t  sequence)

Drops the inputs the server applied.

Parameters
sequenceThe last input applied, from the player record of a snapshot; the inputs up to it, by aegis2d::net::sequence_greater_than(), are dropped.

◆ apply()

void rtype::ShipPrediction::apply ( const InputRecord &  input,
float  delta 
)

Moves the ship by an input the client sends, and keeps the input.

Does nothing before start(). The oldest input is dropped beyond max_pending.

Parameters
inputThe input, numbered as in its Input datagram.
deltaDuration of a tick in seconds, as the server steps.

◆ corrections()

std::uint64_t rtype::ShipPrediction::corrections ( ) const
noexcept

Number of reconciliations that moved the ship by more than correction_threshold: the prediction was wrong.

Returns
The count since the construction.

◆ drawn()

std::optional< aegis2d::Vec2 > rtype::ShipPrediction::drawn ( ) const
noexcept

Where to draw the ship: position(), plus what is left of the corrections.

A reconciliation moves position() at once, but the drawn ship stays where it was and smooth() brings it there, unless the correction is snap_distance or more. start() and stop() leave no gap.

Returns
The center to draw, or nothing before start() or after stop().

◆ last_correction()

float rtype::ShipPrediction::last_correction ( ) const
noexcept

How far the last reconciliation moved the ship.

Returns
The distance in logical pixels, 0 before the first.

◆ pending()

const std::deque< InputRecord > & rtype::ShipPrediction::pending ( ) const
noexcept

The inputs applied since start() that the server has not acknowledged.

Returns
Oldest first.

◆ position()

std::optional< aegis2d::Vec2 > rtype::ShipPrediction::position ( ) const
noexcept

Where the ship is, its inputs applied.

Returns
The predicted center, or nothing before start() or after stop().

◆ reconcile()

float rtype::ShipPrediction::reconcile ( aegis2d::Vec2  server,
std::optional< std::uint16_t >  acknowledged,
float  delta 
)

Corrects the prediction with a snapshot (server reconciliation).

Drops the inputs the snapshot acknowledges, puts the ship where the server had it once it applied them, then replays the inputs kept, each with step_ship(), as apply() did: when the server applied the inputs as the client predicted, the ship ends where it was. Does nothing before start().

Parameters
serverPosition of the ship in the snapshot.
acknowledgedThe last input of the player the server applied, from its player record; nothing when it has applied none, so every input kept is replayed.
deltaDuration of a tick in seconds, as given to apply().
Returns
How far the ship moved, 0 when the prediction was right.

◆ ship()

std::optional< std::uint32_t > rtype::ShipPrediction::ship ( ) const
noexcept

Network id of the ship predicted.

Returns
The id, or nothing before start() or after stop().

◆ smooth()

void rtype::ShipPrediction::smooth ( float  seconds)
noexcept

Brings the drawn ship towards the predicted one, after a correction.

Call it once a frame: the gap a reconciliation left (see drawn()) shrinks by smoothing_speed per second, so the ship never jumps by more than its own move plus 2 pixels a frame.

Parameters
secondsTime since the last call.

◆ start()

void rtype::ShipPrediction::start ( std::uint32_t  ship,
aegis2d::Vec2  position 
)

Starts predicting a ship from a position of the server.

Forgets the inputs kept: the server's position already holds them.

Parameters
shipNetwork id of the ship.
positionCenter of the ship, from a snapshot or its Spawn.

◆ stop()

void rtype::ShipPrediction::stop ( )
noexcept

Stops predicting: the ship is gone, or the player out of lives.

Member Data Documentation

◆ correction_threshold

constexpr float rtype::ShipPrediction::correction_threshold = 0.25F
staticconstexpr

Distance from which a reconciliation counts as a correction, in logical pixels.

Snapshots round positions to 1/8 pixel (RFC section 4.3): two positions of the same path, each rounded, may differ by up to 1/8 pixel on each axis without any error of the prediction.

◆ max_pending

constexpr std::size_t rtype::ShipPrediction::max_pending = 120
staticconstexpr

Inputs kept at most: 2 seconds at 60 per second. Older ones are dropped, as no snapshot acknowledges them on a link that bad.

◆ smoothing_speed

constexpr float rtype::ShipPrediction::smoothing_speed = 120.0F
staticconstexpr

Speed at which the drawn ship catches up with a correction, in logical pixels per second: 2 pixels a frame at 60 frames per second, so a correction of a few pixels takes a few frames instead of a jump.

◆ snap_distance

constexpr float rtype::ShipPrediction::snap_distance = 32.0F
staticconstexpr

Corrections from this distance are not smoothed: the ship is far from where it was predicted (a hit, a long stall), and gliding there would show it in the wrong place for long.


The documentation for this class was generated from the following file: