4#include <aegis2d/core/math.hpp>
5#include <aegis2d/ecs/entity.hpp>
6#include <aegis2d/ecs/registry.hpp>
19#include <unordered_map>
52 std::array<TrailPoint, capacity>
points{};
84 std::optional<std::uint16_t>
input;
105 using Clock = std::chrono::steady_clock;
147 [[nodiscard]] std::optional<double>
server_tick(Clock::time_point now)
const;
152 [[nodiscard]] std::optional<double>
render_tick(Clock::time_point now)
const;
184 void join(std::uint8_t player, std::string name);
193 [[nodiscard]] std::optional<aegis2d::ecs::Entity>
entity(std::uint32_t
id)
const;
197 [[nodiscard]] std::size_t
entities() const noexcept;
201 [[nodiscard]] const std::array<std::optional<
RemotePlayer>, tuning::max_players>&
206 [[nodiscard]] aegis2d::ecs::Registry&
registry() noexcept;
209 [[nodiscard]] const aegis2d::ecs::Registry&
registry() const noexcept;
213 std::optional<aegis2d::ecs::Entity>
entity;
215 std::uint32_t event_tick = 0;
216 std::uint32_t record_tick = 0;
217 bool destroyed =
false;
218 Clock::time_point destroyed_at;
222 void adapt_delay(Clock::duration lateness, Clock::time_point received);
223 bool destroy(
const DestroyEvent& event, Clock::time_point now);
226 aegis2d::ecs::Registry registry_;
227 std::unordered_map<std::uint32_t, Known> known_;
229 std::vector<GameEvent> applied_;
231 std::optional<Clock::time_point> tick_zero_;
233 std::deque<std::pair<Clock::time_point, Clock::duration>> lateness_;
237 std::optional<Clock::time_point> delay_at_;
The world as the server describes it (RFC sections 4.3 to 4.5).
Definition client_world.hpp:102
aegis2d::Vec2 position_at(aegis2d::ecs::Entity entity, double tick) const
Where to draw an entity at a tick: its trail interpolated (interpolate()).
static constexpr std::chrono::seconds forget_after
Time after which a destroyed id is forgotten (RFC section 4.5).
Definition client_world.hpp:108
static constexpr double delay_decrease
Speed at which the delay goes down once the snapshots arrive more regularly: 10 ms per second,...
Definition client_world.hpp:121
std::chrono::steady_clock Clock
Clock of the forgetting of destroyed ids.
Definition client_world.hpp:105
static constexpr std::chrono::milliseconds min_interpolation_delay
Shortest interpolation delay: one snapshot interval at 20 Hz, enough when every snapshot arrives on t...
Definition client_world.hpp:114
std::optional< double > server_tick(Clock::time_point now) const
Server tick at a time, estimated from the snapshots received.
Clock::duration interpolation_delay(Clock::time_point now) const noexcept
How far behind the server the remote entities are drawn, following the jitter.
void apply(const GameEvent &event, Clock::time_point now)
Applies an event.
void prune(Clock::time_point now)
Forgets the destroyed ids whose Destroy is forget_after old.
aegis2d::ecs::Registry & registry() noexcept
The world, to draw it.
static constexpr std::chrono::milliseconds initial_interpolation_delay
How far behind the server the remote entities are drawn at first: two snapshot intervals at 20 Hz,...
Definition client_world.hpp:111
void apply(const Snapshot &snapshot, Clock::time_point received)
Applies a Snapshot datagram and times its tick, for server_tick().
std::optional< aegis2d::ecs::Entity > entity(std::uint32_t id) const
Entity of a network id.
std::optional< double > render_tick(Clock::time_point now) const
Tick at which to draw the world: server_tick() interpolation_delay(now) ago.
const std::array< std::optional< RemotePlayer >, tuning::max_players > & players() const noexcept
The players of the game, by number.
static constexpr std::chrono::seconds jitter_window
How long the lateness of the snapshots is remembered for the delay.
Definition client_world.hpp:118
std::size_t entities() const noexcept
Number of entities alive.
void apply(const Snapshot &snapshot)
Applies the records of one Snapshot datagram.
void join(std::uint8_t player, std::string name)
Records the local player, whose own PlayerJoined the server never sends (RFC section 4....
static constexpr double delay_increase
Speed at which the delay goes up when the snapshots come later: half a millisecond per millisecond,...
Definition client_world.hpp:125
static constexpr std::chrono::milliseconds max_interpolation_delay
Longest interpolation delay, however late the snapshots.
Definition client_world.hpp:116
std::vector< GameEvent > take_events()
Events applied since the last call, in order: those ignored (late, unknown kind) are not in it.
Gameplay events sent to the clients (RFC section 4.4): spawns, destructions, deaths,...
constexpr std::size_t max_players
Players in one game, as the subject requires.
Definition tuning.hpp:33
R-Type game code, built on the Aegis2D engine.
Definition enemy_scripts.hpp:16
std::variant< SpawnEvent, DestroyEvent, DeathEvent, PlayerJoinedEvent, PlayerLeftEvent > GameEvent
One of the gameplay events the server sends.
Definition events.hpp:160
constexpr std::uint32_t no_entity
Network id that names no entity, for a player out of lives.
Definition snapshot.hpp:31
constexpr std::uint8_t no_player
Player number meaning "no player", as the owner of an enemy.
Definition events.hpp:59
aegis2d::Vec2 interpolate(const Trail &trail, double tick) noexcept
Position of an entity at a tick, between the positions of its trail.
Snapshots of the world sent to the clients (RFC section 4.3).
An entity disappears, at its last position.
Definition events.hpp:95
What the client knows of a player of the game.
Definition client_world.hpp:72
std::uint32_t score
Score.
Definition client_world.hpp:80
std::uint32_t ship
Network id of its ship, or no_entity once out of lives.
Definition client_world.hpp:76
std::string name
Name from PlayerJoined, empty until it arrives.
Definition client_world.hpp:74
std::optional< std::uint16_t > input
Sequence of the last Input of the player the server applied, or nothing before the first: the most re...
Definition client_world.hpp:84
std::uint8_t lives
Lives left.
Definition client_world.hpp:78
What the client knows of an entity of the server, next to its components.
Definition client_world.hpp:26
std::uint8_t owner
Player it belongs to, or no_player.
Definition client_world.hpp:32
std::uint8_t kind
Kind, an EntityKind value.
Definition client_world.hpp:30
The state of the world at one tick.
Definition snapshot.hpp:94
An entity appears, with its kind, position and velocity.
Definition events.hpp:71
One position of an entity, as a snapshot (or its Spawn) gave it.
Definition client_world.hpp:36
aegis2d::Vec2 position
Center of the entity, in logical pixels.
Definition client_world.hpp:40
std::uint32_t tick
Server tick of the position.
Definition client_world.hpp:38
The latest positions of an entity, oldest first, to draw it between two of them.
Definition client_world.hpp:48
std::array< TrailPoint, capacity > points
Positions, oldest first; only the first count are set.
Definition client_world.hpp:52
void push(TrailPoint point) noexcept
Adds the newest position, dropping the oldest when full.
static constexpr std::size_t capacity
Number of positions kept.
Definition client_world.hpp:50
std::size_t count
Number of positions set.
Definition client_world.hpp:54
Gameplay values of R-Type: play area, speeds, sizes, health, lives and scores.