R-Type
Networked shoot'em up built on Aegis2D
Loading...
Searching...
No Matches
delta_snapshot.hpp
Go to the documentation of this file.
1
3#pragma once
4#include <rtype/snapshot.hpp>
5
6#include <cstddef>
7#include <cstdint>
8#include <optional>
9#include <span>
10#include <vector>
11
12namespace rtype {
13
16enum class EntityChange : std::uint8_t {
18 Updated = 0,
20 Added = 1,
22 Removed = 2,
23};
24
26enum class EntityField : std::uint8_t {
28 X = 1U << 0U,
30 Y = 1U << 1U,
32 Velocity = 1U << 2U,
34 Frame = 1U << 3U,
35};
36
38inline constexpr unsigned change_bits = 2;
40inline constexpr unsigned entity_field_bits = 4;
41
47 std::uint8_t fields = 0;
50
53 bool operator==(const EntityDelta&) const = default;
54};
55
59 std::uint32_t tick = 0;
61 std::optional<std::uint32_t> baseline;
63 std::vector<PlayerRecord> players;
65 std::vector<EntityDelta> entities;
66
69 bool operator==(const DeltaSnapshot&) const = default;
70};
71
76[[nodiscard]] EntityRecord quantized(const EntityRecord& entity) noexcept;
77
90[[nodiscard]] std::vector<std::vector<std::byte>> write_delta_snapshot(const Snapshot& snapshot,
91 const Snapshot* baseline);
92
98[[nodiscard]] std::optional<DeltaSnapshot> read_delta_snapshot(std::span<const std::byte> payload);
99
107[[nodiscard]] std::optional<Snapshot> apply_delta(const Snapshot* baseline,
108 std::span<const DeltaSnapshot> parts);
109
110} // namespace rtype
R-Type game code, built on the Aegis2D engine.
Definition enemy_scripts.hpp:16
EntityChange
What a delta snapshot says of an entity.
Definition delta_snapshot.hpp:16
@ Removed
In the baseline, gone since.
@ Added
Not in the baseline (or no baseline): every field is sent.
@ Updated
Some fields changed since the baseline; EntityDelta::fields says which.
std::optional< DeltaSnapshot > read_delta_snapshot(std::span< const std::byte > payload)
Decodes one payload written by write_delta_snapshot().
std::vector< std::vector< std::byte > > write_delta_snapshot(const Snapshot &snapshot, const Snapshot *baseline)
Encodes a snapshot against a baseline, as the payloads of Snapshot datagrams.
constexpr unsigned change_bits
Bits of the change of a delta record.
Definition delta_snapshot.hpp:38
constexpr unsigned entity_field_bits
Bits of the fields of an EntityChange::Updated record.
Definition delta_snapshot.hpp:40
std::optional< Snapshot > apply_delta(const Snapshot *baseline, std::span< const DeltaSnapshot > parts)
Rebuilds a snapshot from its baseline and every payload of its tick.
EntityRecord quantized(const EntityRecord &entity) noexcept
A record as the client gets it: position and velocity rounded as compact.hpp does,...
EntityField
Fields of an EntityChange::Updated record, one bit each.
Definition delta_snapshot.hpp:26
@ Velocity
vx and vy, together.
Snapshots of the world sent to the clients (RFC section 4.3).
One payload of a snapshot encoded against a baseline.
Definition delta_snapshot.hpp:57
std::optional< std::uint32_t > baseline
Tick of the baseline, or nothing for a full snapshot.
Definition delta_snapshot.hpp:61
bool operator==(const DeltaSnapshot &) const =default
Field-by-field equality.
std::uint32_t tick
Server tick of the state.
Definition delta_snapshot.hpp:59
std::vector< EntityDelta > entities
The entities added, changed or removed since the baseline.
Definition delta_snapshot.hpp:65
std::vector< PlayerRecord > players
Every player of the game, as in a full snapshot.
Definition delta_snapshot.hpp:63
What a delta snapshot says of one entity.
Definition delta_snapshot.hpp:43
EntityChange change
How it differs from the baseline.
Definition delta_snapshot.hpp:45
EntityRecord record
Its id, and the fields sent; the others are 0.
Definition delta_snapshot.hpp:49
bool operator==(const EntityDelta &) const =default
Field-by-field equality.
std::uint8_t fields
For EntityChange::Updated, the EntityField bits of the fields sent.
Definition delta_snapshot.hpp:47
What a snapshot says of an entity.
Definition snapshot.hpp:72
The state of the world at one tick.
Definition snapshot.hpp:94