R-Type
Networked shoot'em up built on Aegis2D
Loading...
Searching...
No Matches
ship_prediction.hpp
Go to the documentation of this file.
1
3#pragma once
4#include <aegis2d/core/math.hpp>
5
6#include <rtype/input.hpp>
7
8#include <cstddef>
9#include <cstdint>
10#include <deque>
11#include <optional>
12
13namespace rtype {
14
23public:
26 static constexpr std::size_t max_pending = 120;
27
32 static constexpr float correction_threshold = 0.25F;
33
37 static constexpr float smoothing_speed = 120.0F;
38
42 static constexpr float snap_distance = 32.0F;
43
48 void start(std::uint32_t ship, aegis2d::Vec2 position);
49
51 void stop() noexcept;
52
57 void apply(const InputRecord& input, float delta);
58
62 void acknowledge(std::uint16_t sequence);
63
74 float reconcile(aegis2d::Vec2 server, std::optional<std::uint16_t> acknowledged, float delta);
75
79 [[nodiscard]] std::uint64_t corrections() const noexcept;
80
83 [[nodiscard]] float last_correction() const noexcept;
84
90 void smooth(float seconds) noexcept;
91
97 [[nodiscard]] std::optional<aegis2d::Vec2> drawn() const noexcept;
98
101 [[nodiscard]] std::optional<std::uint32_t> ship() const noexcept;
102
105 [[nodiscard]] std::optional<aegis2d::Vec2> position() const noexcept;
106
109 [[nodiscard]] const std::deque<InputRecord>& pending() const noexcept;
110
111private:
112 std::optional<std::uint32_t> ship_;
113 aegis2d::Vec2 position_;
114 std::deque<InputRecord> pending_;
115 std::uint64_t corrections_ = 0;
116 float last_correction_ = 0.0F;
117 // drawn() minus position_: the part of the corrections not shown yet.
118 aegis2d::Vec2 gap_;
119};
120
121} // namespace rtype
Where the client's own ship is, before the server says so.
Definition ship_prediction.hpp:22
static constexpr float correction_threshold
Distance from which a reconciliation counts as a correction, in logical pixels.
Definition ship_prediction.hpp:32
void apply(const InputRecord &input, float delta)
Moves the ship by an input the client sends, and keeps the input.
static constexpr float smoothing_speed
Speed at which the drawn ship catches up with a correction, in logical pixels per second: 2 pixels a ...
Definition ship_prediction.hpp:37
const std::deque< InputRecord > & pending() const noexcept
The inputs applied since start() that the server has not acknowledged.
std::optional< std::uint32_t > ship() const noexcept
Network id of the ship predicted.
void acknowledge(std::uint16_t sequence)
Drops the inputs the server applied.
void start(std::uint32_t ship, aegis2d::Vec2 position)
Starts predicting a ship from a position of the server.
std::uint64_t corrections() const noexcept
Number of reconciliations that moved the ship by more than correction_threshold: the prediction was w...
std::optional< aegis2d::Vec2 > drawn() const noexcept
Where to draw the ship: position(), plus what is left of the corrections.
void smooth(float seconds) noexcept
Brings the drawn ship towards the predicted one, after a correction.
float reconcile(aegis2d::Vec2 server, std::optional< std::uint16_t > acknowledged, float delta)
Corrects the prediction with a snapshot (server reconciliation).
static constexpr std::size_t max_pending
Inputs kept at most: 2 seconds at 60 per second. Older ones are dropped, as no snapshot acknowledges ...
Definition ship_prediction.hpp:26
void stop() noexcept
Stops predicting: the ship is gone, or the player out of lives.
float last_correction() const noexcept
How far the last reconciliation moved the ship.
static constexpr float snap_distance
Corrections from this distance are not smoothed: the ship is far from where it was predicted (a hit,...
Definition ship_prediction.hpp:42
std::optional< aegis2d::Vec2 > position() const noexcept
Where the ship is, its inputs applied.
Inputs of a player sent to the server (RFC section 4.2).
R-Type game code, built on the Aegis2D engine.
Definition enemy_scripts.hpp:16
One input of the player: the actions held at one tick of the client.
Definition input.hpp:29