R-Type
Networked shoot'em up built on Aegis2D
Loading...
Searching...
No Matches
snapshot.hpp
Go to the documentation of this file.
1
3#pragma once
4#include <aegis2d/net/packet.hpp>
5
6#include <rtype/compact.hpp>
7#include <rtype/tuning.hpp>
8
9#include <cstddef>
10#include <cstdint>
11#include <optional>
12#include <span>
13#include <vector>
14
15namespace rtype {
16
19enum class EntityKind : std::uint8_t {
21 PlayerShip = 1,
23 PlayerMissile = 2,
25 BydosSlave = 3,
27 EnemyMissile = 4,
28};
29
31inline constexpr std::uint32_t no_entity = 0xFFFFFFFF;
32
34inline constexpr std::size_t snapshot_fixed_bits = 32 + 3 + 16;
37inline constexpr unsigned id_low_bits = 24;
39inline constexpr unsigned id_high_bits = 8;
41inline constexpr unsigned kind_bits = 3;
43inline constexpr unsigned frame_bits = 4;
46inline constexpr std::size_t entity_record_fixed_bits =
50inline constexpr std::uint32_t short_id_gap = (std::uint32_t{1} << varint_group_bits) - 1U;
51
55 std::uint8_t player = 0;
57 std::uint32_t ship = no_entity;
59 std::uint8_t lives = 0;
61 std::uint32_t score = 0;
64 std::optional<std::uint16_t> input;
65
68 bool operator==(const PlayerRecord&) const = default;
69};
70
74 std::uint32_t id = 0;
76 std::uint8_t kind = 0;
78 float x = 0.0F;
80 float y = 0.0F;
82 float vx = 0.0F;
84 float vy = 0.0F;
86 std::uint8_t frame = 0;
87
90 bool operator==(const EntityRecord&) const = default;
91};
92
94struct Snapshot {
96 std::uint32_t tick = 0;
98 std::vector<PlayerRecord> players;
100 std::vector<EntityRecord> entities;
101
104 bool operator==(const Snapshot&) const = default;
105};
106
112[[nodiscard]] std::size_t entity_record_bits(std::uint32_t id_gap) noexcept;
113
126[[nodiscard]] std::vector<std::vector<std::byte>> write_snapshot(const Snapshot& snapshot);
127
134[[nodiscard]] std::optional<Snapshot> read_snapshot(std::span<const std::byte> payload);
135
136} // namespace rtype
Compact encoders of snapshot fields: fixed-point positions, integer velocities and varint ids,...
R-Type game code, built on the Aegis2D engine.
Definition enemy_scripts.hpp:16
std::size_t entity_record_bits(std::uint32_t id_gap) noexcept
Size of an entity record.
std::optional< Snapshot > read_snapshot(std::span< const std::byte > payload)
Decodes the payload of one Snapshot datagram.
constexpr unsigned id_high_bits
High bits of a network id, sent as they are: the generation of Entity handles.
Definition snapshot.hpp:39
constexpr std::uint32_t short_id_gap
Largest gap between the low bits of two records' ids that keeps a record within 10 bytes (77 bits).
Definition snapshot.hpp:50
constexpr std::uint32_t no_entity
Network id that names no entity, for a player out of lives.
Definition snapshot.hpp:31
constexpr unsigned position_bits
Bits of a position, sign included.
Definition compact.hpp:16
constexpr unsigned varint_group_bits
Bits of each group of a varint, followed by one continuation bit.
Definition compact.hpp:39
constexpr std::size_t snapshot_fixed_bits
Bits of the tick, the player count and the entity count of a snapshot payload.
Definition snapshot.hpp:34
EntityKind
Kind of an entity in snapshots and events (RFC section 4.1).
Definition snapshot.hpp:19
@ PlayerShip
A player's ship.
@ EnemyMissile
A missile fired by an enemy.
@ BydosSlave
A Bydos slave.
@ PlayerMissile
A missile fired by a player.
std::vector< std::vector< std::byte > > write_snapshot(const Snapshot &snapshot)
Encodes a snapshot as the payloads of Snapshot datagrams, header excluded.
constexpr unsigned id_low_bits
Low bits of a network id, sent as their gap to those of the previous record: the index of the engine'...
Definition snapshot.hpp:37
constexpr unsigned frame_bits
Bits of the frame of an entity record: frames above 15 are sent as 15.
Definition snapshot.hpp:43
constexpr std::size_t entity_record_fixed_bits
Bits of an entity record besides the gap of its id's low bits: the id's high bits,...
Definition snapshot.hpp:46
constexpr unsigned kind_bits
Bits of the kind of an entity record: kinds 0 to 7.
Definition snapshot.hpp:41
constexpr unsigned velocity_bits
Bits of a velocity, sign included; velocities are sent in whole pixels per second.
Definition compact.hpp:29
What a snapshot says of an entity.
Definition snapshot.hpp:72
float x
Position of its center, in logical pixels.
Definition snapshot.hpp:78
float vx
Velocity, in logical pixels per second.
Definition snapshot.hpp:82
float y
Position of its center, in logical pixels.
Definition snapshot.hpp:80
float vy
Velocity, in logical pixels per second.
Definition snapshot.hpp:84
std::uint8_t frame
Frame of its sprite sheet to show.
Definition snapshot.hpp:86
bool operator==(const EntityRecord &) const =default
Field-by-field equality.
std::uint8_t kind
Kind, an EntityKind value (others are reserved).
Definition snapshot.hpp:76
What a snapshot says of a player.
Definition snapshot.hpp:53
std::optional< std::uint16_t > input
Sequence of the last Input of the player the server applied (InputRecord), or nothing before the firs...
Definition snapshot.hpp:64
bool operator==(const PlayerRecord &) const =default
Field-by-field equality.
std::uint32_t ship
Network id of its ship, or no_entity once out of lives.
Definition snapshot.hpp:57
std::uint8_t lives
Lives left.
Definition snapshot.hpp:59
std::uint8_t player
Player number, 0 to 3.
Definition snapshot.hpp:55
std::uint32_t score
Score.
Definition snapshot.hpp:61
The state of the world at one tick.
Definition snapshot.hpp:94
std::uint32_t tick
Server tick of the state.
Definition snapshot.hpp:96
bool operator==(const Snapshot &) const =default
Field-by-field equality.
std::vector< EntityRecord > entities
Every replicated entity of the world.
Definition snapshot.hpp:100
std::vector< PlayerRecord > players
Every player of the game.
Definition snapshot.hpp:98
Gameplay values of R-Type: play area, speeds, sizes, health, lives and scores.