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

The world as the server describes it (RFC sections 4.3 to 4.5). More...

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

Public Types

using Clock = std::chrono::steady_clock
 Clock of the forgetting of destroyed ids.
 

Public Member Functions

void apply (const GameEvent &event, Clock::time_point now)
 Applies an event.
 
void apply (const Snapshot &snapshot)
 Applies the records of one Snapshot datagram.
 
void apply (const Snapshot &snapshot, Clock::time_point received)
 Applies a Snapshot datagram and times its tick, for server_tick().
 
std::optional< double > server_tick (Clock::time_point now) const
 Server tick at a time, estimated from the snapshots received.
 
std::optional< double > render_tick (Clock::time_point now) const
 Tick at which to draw the world: server_tick() interpolation_delay(now) ago.
 
Clock::duration interpolation_delay (Clock::time_point now) const noexcept
 How far behind the server the remote entities are drawn, following the jitter.
 
aegis2d::Vec2 position_at (aegis2d::ecs::Entity entity, double tick) const
 Where to draw an entity at a tick: its trail interpolated (interpolate()).
 
std::vector< GameEvent > take_events ()
 Events applied since the last call, in order: those ignored (late, unknown kind) are not in it.
 
void join (std::uint8_t player, std::string name)
 Records the local player, whose own PlayerJoined the server never sends (RFC section 4.6: it goes to the others).
 
void prune (Clock::time_point now)
 Forgets the destroyed ids whose Destroy is forget_after old.
 
std::optional< aegis2d::ecs::Entity > entity (std::uint32_t id) const
 Entity of a network id.
 
std::size_t entities () const noexcept
 Number of entities alive.
 
const std::array< std::optional< RemotePlayer >, tuning::max_players > & players () const noexcept
 The players of the game, by number.
 
aegis2d::ecs::Registry & registry () noexcept
 The world, to draw it.
 
const aegis2d::ecs::Registry & registry () const noexcept
 The world, to draw it.
 

Static Public Attributes

static constexpr std::chrono::seconds forget_after {10}
 Time after which a destroyed id is forgotten (RFC section 4.5).
 
static constexpr std::chrono::milliseconds initial_interpolation_delay {100}
 How far behind the server the remote entities are drawn at first: two snapshot intervals at 20 Hz, until the snapshots' arrivals are measured.
 
static constexpr std::chrono::milliseconds min_interpolation_delay {50}
 Shortest interpolation delay: one snapshot interval at 20 Hz, enough when every snapshot arrives on time.
 
static constexpr std::chrono::milliseconds max_interpolation_delay {200}
 Longest interpolation delay, however late the snapshots.
 
static constexpr std::chrono::seconds jitter_window {2}
 How long the lateness of the snapshots is remembered for the delay.
 
static constexpr double delay_decrease = 0.01
 Speed at which the delay goes down once the snapshots arrive more regularly: 10 ms per second, so the entities drawn speed up by 1 %, which no one sees.
 
static constexpr double delay_increase = 0.5
 Speed at which the delay goes up when the snapshots come later: half a millisecond per millisecond, so the entities drawn slow down to half speed for a moment, but never stop or go back.
 

Detailed Description

The world as the server describes it (RFC sections 4.3 to 4.5).

Events create and destroy the entities: a Spawn creates one with a Transform, a Velocity, a Sprite of its kind's size, a NetworkId and Replicated, and a Destroy removes it. Snapshots only update them (position, velocity, frame), never create or destroy one. As events and snapshots arrive in any order, the world keeps for each network id the tick of its last Spawn or Destroy, and:

  • ignores a Spawn or a Destroy older than the last one applied to the id, so a Destroy arriving before its Spawn wins;
  • ignores the snapshot records of an id it does not know, or destroyed at the record's tick or later, or older than the last record applied to it;
  • forgets a destroyed id forget_after its Destroy, after which the id may be reused;
  • ignores entities of a kind it does not know.

The players come from the player records of the snapshots, PlayerJoined, PlayerLeft and Death. Not thread-safe: use it from the client's thread.

Member Typedef Documentation

◆ Clock

using rtype::ClientWorld::Clock = std::chrono::steady_clock

Clock of the forgetting of destroyed ids.

Member Function Documentation

◆ apply() [1/3]

void rtype::ClientWorld::apply ( const GameEvent &  event,
Clock::time_point  now 
)

Applies an event.

Parameters
eventEvent from the server, read by read_event().
nowCurrent time, for the forgetting of destroyed ids.

◆ apply() [2/3]

void rtype::ClientWorld::apply ( const Snapshot &  snapshot)

Applies the records of one Snapshot datagram.

Parameters
snapshotRecords read by read_snapshot().

◆ apply() [3/3]

void rtype::ClientWorld::apply ( const Snapshot &  snapshot,
Clock::time_point  received 
)

Applies a Snapshot datagram and times its tick, for server_tick().

Parameters
snapshotRecords read by read_snapshot().
receivedWhen the datagram arrived.

◆ entities()

std::size_t rtype::ClientWorld::entities ( ) const
noexcept

Number of entities alive.

Returns
The count.

◆ entity()

std::optional< aegis2d::ecs::Entity > rtype::ClientWorld::entity ( std::uint32_t  id) const

Entity of a network id.

Parameters
idNetwork id.
Returns
The local entity, or std::nullopt if the id is unknown or destroyed.

◆ interpolation_delay()

Clock::duration rtype::ClientWorld::interpolation_delay ( Clock::time_point  now) const
noexcept

How far behind the server the remote entities are drawn, following the jitter.

Each snapshot is timed against the fastest arrival (server_tick()): how late it came is its lateness. The delay aims at one snapshot interval (50 ms) plus 1.25 times the largest lateness of the last jitter_window, within min_interpolation_delay and max_interpolation_delay, so the tick drawn always has a snapshot after it: no entity stops for lack of snapshots. It moves towards its aim with time, up by delay_increase and down by delay_decrease, so the drawing slows down or speeds up but never jumps back in time.

Parameters
nowTime of the frame.
Returns
The delay, initial_interpolation_delay before the first snapshot.

◆ join()

void rtype::ClientWorld::join ( std::uint8_t  player,
std::string  name 
)

Records the local player, whose own PlayerJoined the server never sends (RFC section 4.6: it goes to the others).

Parameters
playerPlayer number from LoginAccepted.
nameName the client logged in with.

◆ players()

const std::array< std::optional< RemotePlayer >, tuning::max_players > & rtype::ClientWorld::players ( ) const
noexcept

The players of the game, by number.

Returns
An empty slot for a number with no player.

◆ position_at()

aegis2d::Vec2 rtype::ClientWorld::position_at ( aegis2d::ecs::Entity  entity,
double  tick 
) const

Where to draw an entity at a tick: its trail interpolated (interpolate()).

Parameters
entityLocal entity of the world.
tickTick from render_tick().
Returns
The position, or its Transform if it has no trail.

◆ prune()

void rtype::ClientWorld::prune ( Clock::time_point  now)

Forgets the destroyed ids whose Destroy is forget_after old.

Parameters
nowCurrent time.

◆ registry() [1/2]

const aegis2d::ecs::Registry & rtype::ClientWorld::registry ( ) const
noexcept

The world, to draw it.

Returns
The registry of the local entities.

◆ registry() [2/2]

aegis2d::ecs::Registry & rtype::ClientWorld::registry ( )
noexcept

The world, to draw it.

Returns
The registry of the local entities.

◆ render_tick()

std::optional< double > rtype::ClientWorld::render_tick ( Clock::time_point  now) const

Tick at which to draw the world: server_tick() interpolation_delay(now) ago.

Parameters
nowTime of the frame.
Returns
The tick, or nothing before the first snapshot.

◆ server_tick()

std::optional< double > rtype::ClientWorld::server_tick ( Clock::time_point  now) const

Server tick at a time, estimated from the snapshots received.

Each snapshot tells the tick of the server when it left; the estimate keeps the fastest arrival seen (the least delayed) and drifts slowly towards later ones, so a late datagram does not move it back and forth.

Parameters
nowTime to estimate the tick of.
Returns
The tick, fractional, or nothing before the first snapshot.

◆ take_events()

std::vector< GameEvent > rtype::ClientWorld::take_events ( )

Events applied since the last call, in order: those ignored (late, unknown kind) are not in it.

The client shows them: an explosion and a sound for a kill, a notice when a player leaves. Read before the player of a PlayerLeftEvent is gone from players(): the player is removed when the event is applied.

Returns
The events; the list is then empty.

Member Data Documentation

◆ delay_decrease

constexpr double rtype::ClientWorld::delay_decrease = 0.01
staticconstexpr

Speed at which the delay goes down once the snapshots arrive more regularly: 10 ms per second, so the entities drawn speed up by 1 %, which no one sees.

◆ delay_increase

constexpr double rtype::ClientWorld::delay_increase = 0.5
staticconstexpr

Speed at which the delay goes up when the snapshots come later: half a millisecond per millisecond, so the entities drawn slow down to half speed for a moment, but never stop or go back.

◆ forget_after

constexpr std::chrono::seconds rtype::ClientWorld::forget_after {10}
staticconstexpr

Time after which a destroyed id is forgotten (RFC section 4.5).

◆ initial_interpolation_delay

constexpr std::chrono::milliseconds rtype::ClientWorld::initial_interpolation_delay {100}
staticconstexpr

How far behind the server the remote entities are drawn at first: two snapshot intervals at 20 Hz, until the snapshots' arrivals are measured.

◆ jitter_window

constexpr std::chrono::seconds rtype::ClientWorld::jitter_window {2}
staticconstexpr

How long the lateness of the snapshots is remembered for the delay.

◆ max_interpolation_delay

constexpr std::chrono::milliseconds rtype::ClientWorld::max_interpolation_delay {200}
staticconstexpr

Longest interpolation delay, however late the snapshots.

◆ min_interpolation_delay

constexpr std::chrono::milliseconds rtype::ClientWorld::min_interpolation_delay {50}
staticconstexpr

Shortest interpolation delay: one snapshot interval at 20 Hz, enough when every snapshot arrives on time.


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