R-Type
Networked shoot'em up built on Aegis2D
Loading...
Searching...
No Matches
client_world.hpp
Go to the documentation of this file.
1
3#pragma once
4#include <aegis2d/core/math.hpp>
5#include <aegis2d/ecs/entity.hpp>
6#include <aegis2d/ecs/registry.hpp>
7
8#include <rtype/events.hpp>
9#include <rtype/snapshot.hpp>
10#include <rtype/tuning.hpp>
11
12#include <array>
13#include <chrono>
14#include <cstddef>
15#include <cstdint>
16#include <deque>
17#include <optional>
18#include <string>
19#include <unordered_map>
20#include <utility>
21#include <vector>
22
23namespace rtype {
24
26struct Replicated {
28 std::uint32_t id = 0;
30 std::uint8_t kind = 0;
32 std::uint8_t owner = no_player;
33};
34
36struct TrailPoint {
38 std::uint32_t tick = 0;
40 aegis2d::Vec2 position{};
41};
42
48struct Trail {
50 static constexpr std::size_t capacity = 4;
52 std::array<TrailPoint, capacity> points{};
54 std::size_t count = 0;
55
59 void push(TrailPoint point) noexcept;
60};
61
69[[nodiscard]] aegis2d::Vec2 interpolate(const Trail& trail, double tick) noexcept;
70
74 std::string name;
76 std::uint32_t ship = no_entity;
78 std::uint8_t lives = 0;
80 std::uint32_t score = 0;
84 std::optional<std::uint16_t> input;
85};
86
103public:
105 using Clock = std::chrono::steady_clock;
106
108 static constexpr std::chrono::seconds forget_after{10};
111 static constexpr std::chrono::milliseconds initial_interpolation_delay{100};
114 static constexpr std::chrono::milliseconds min_interpolation_delay{50};
116 static constexpr std::chrono::milliseconds max_interpolation_delay{200};
118 static constexpr std::chrono::seconds jitter_window{2};
121 static constexpr double delay_decrease = 0.01;
125 static constexpr double delay_increase = 0.5;
126
130 void apply(const GameEvent& event, Clock::time_point now);
131
134 void apply(const Snapshot& snapshot);
135
139 void apply(const Snapshot& snapshot, Clock::time_point received);
140
147 [[nodiscard]] std::optional<double> server_tick(Clock::time_point now) const;
148
152 [[nodiscard]] std::optional<double> render_tick(Clock::time_point now) const;
153
164 [[nodiscard]] Clock::duration interpolation_delay(Clock::time_point now) const noexcept;
165
170 [[nodiscard]] aegis2d::Vec2 position_at(aegis2d::ecs::Entity entity, double tick) const;
171
178 [[nodiscard]] std::vector<GameEvent> take_events();
179
184 void join(std::uint8_t player, std::string name);
185
188 void prune(Clock::time_point now);
189
193 [[nodiscard]] std::optional<aegis2d::ecs::Entity> entity(std::uint32_t id) const;
194
197 [[nodiscard]] std::size_t entities() const noexcept;
198
201 [[nodiscard]] const std::array<std::optional<RemotePlayer>, tuning::max_players>&
202 players() const noexcept;
203
206 [[nodiscard]] aegis2d::ecs::Registry& registry() noexcept;
207
209 [[nodiscard]] const aegis2d::ecs::Registry& registry() const noexcept;
210
211private:
212 struct Known {
213 std::optional<aegis2d::ecs::Entity> entity;
214 // Tick of its last Spawn or Destroy applied, and of its last snapshot record.
215 std::uint32_t event_tick = 0;
216 std::uint32_t record_tick = 0;
217 bool destroyed = false;
218 Clock::time_point destroyed_at;
219 };
220
221 bool spawn(const SpawnEvent& event);
222 void adapt_delay(Clock::duration lateness, Clock::time_point received);
223 bool destroy(const DestroyEvent& event, Clock::time_point now);
224 RemotePlayer& player(std::uint8_t number);
225
226 aegis2d::ecs::Registry registry_;
227 std::unordered_map<std::uint32_t, Known> known_;
228 std::array<std::optional<RemotePlayer>, tuning::max_players> players_;
229 std::vector<GameEvent> applied_;
230 // Estimated time of server tick 0 on the local clock: the fastest arrival seen so far.
231 std::optional<Clock::time_point> tick_zero_;
232 // The lateness of each snapshot of the last jitter_window, by arrival, oldest first.
233 std::deque<std::pair<Clock::time_point, Clock::duration>> lateness_;
234 // The delay at delay_at_, moving towards aim_ since then.
235 Clock::duration delay_ = initial_interpolation_delay;
236 Clock::duration aim_ = initial_interpolation_delay;
237 std::optional<Clock::time_point> delay_at_;
238};
239
240} // namespace rtype
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.