R-Type 0.1
Loading...
Searching...
No Matches
EntityPayload.hpp
1#pragma once
2
3#include <cstdint>
4
5#include "BigEndian.hpp"
6#include "Position.hpp"
7
8namespace rtype::protocol {
15using NetworkEntityId = std::uint32_t;
16
20enum class DestroyReason : std::uint8_t {
21 Unknown = 0x00,
22 Collision = 0x01,
23 OutOfBounds = 0x02,
24 PlayerDisconnected = 0x03,
25 GameEnded = 0x04
26};
27
48};
49
64};
65
76};
77
78static_assert(sizeof(CreateEntityPayload) ==
79 sizeof(be32) * 2 + sizeof(be16) * 4,
80 "CreateEntityPayload must not contain padding");
81static_assert(sizeof(DestroyEntityPayload) ==
82 sizeof(be32) * 2 + sizeof(DestroyReason),
83 "DestroyEntityPayload must not contain padding");
84static_assert(sizeof(QueryEntityPayload) == sizeof(be32),
85 "QueryEntityPayload must not contain padding");
86
92constexpr bool isValid(const CreateEntityPayload &payload) {
93 return payload.networkId.get() != 0;
94}
95
101constexpr bool isValid(const DestroyEntityPayload &payload) {
102 return payload.networkId.get() != 0 &&
103 static_cast<std::uint8_t>(payload.reason) <=
104 static_cast<std::uint8_t>(DestroyReason::GameEnded);
105}
106
112constexpr bool isValid(const QueryEntityPayload &payload) {
113 return payload.networkId.get() != 0;
114}
115
126inline CreateEntityPayload createEntityPayload(std::uint32_t serverTick,
127 std::uint32_t networkId,
128 std::uint16_t entityType,
129 float x, float y,
130 float rotation = 0.0f) {
131 return {serverTick, networkId, entityType,
133}
134
142inline DestroyEntityPayload destroyEntityPayload(std::uint32_t serverTick,
143 std::uint32_t networkId,
144 DestroyReason reason) {
145 return {serverTick, networkId, reason};
146}
147
149} // namespace rtype::protocol
CreateEntityPayload createEntityPayload(std::uint32_t serverTick, std::uint32_t networkId, std::uint16_t entityType, float x, float y, float rotation=0.0f)
Builds a CreateEntity payload from world values.
Definition EntityPayload.hpp:126
constexpr bool isValid(const DisconnectPayload &payload)
Checks that the reason is a known value.
Definition DisconnectPayload.hpp:41
BigEndian< std::uint16_t > be16
16-bit unsigned integer stored in big-endian order.
Definition BigEndian.hpp:85
be16 encodePosition(float position)
Encodes a position as a 16-bit fixed point value.
Definition Position.hpp:33
BigEndian< std::uint32_t > be32
32-bit unsigned integer stored in big-endian order.
Definition BigEndian.hpp:87
DestroyEntityPayload destroyEntityPayload(std::uint32_t serverTick, std::uint32_t networkId, DestroyReason reason)
Builds a DestroyEntity payload.
Definition EntityPayload.hpp:142
be16 encodeRotation(float radians)
Encodes an angle as a fraction of a full turn on 16 bits.
Definition Position.hpp:59
DestroyReason
Reason carried by a DestroyEntity message.
Definition EntityPayload.hpp:20
std::uint32_t NetworkEntityId
Server assigned identifier of a replicated entity, 0 means no entity.
Definition EntityPayload.hpp:15
@ GameEnded
The game is over.
@ Collision
Removed by gameplay, such as a hit.
@ OutOfBounds
Left the playable area.
@ PlayerDisconnected
Owner of the ship left the game.
@ Unknown
No specific reason.
constexpr T get() const
Returns the value in host byte order.
Definition BigEndian.hpp:76
Payload of a CreateEntity message, 16 bytes, sent by the server to spawn an entity on a client.
Definition EntityPayload.hpp:41
be16 positionY
Vertical position, fixed point encoded.
Definition EntityPayload.hpp:46
be16 entityType
Archetype id of the entity blueprint.
Definition EntityPayload.hpp:44
be32 serverTick
Server tick at sending time.
Definition EntityPayload.hpp:42
be16 positionX
Horizontal position, fixed point encoded.
Definition EntityPayload.hpp:45
be32 networkId
Identifier of the new entity.
Definition EntityPayload.hpp:43
be16 rotation
Rotation, fraction of a full turn.
Definition EntityPayload.hpp:47
Payload of a DestroyEntity message, 9 bytes, sent by the server to remove an entity from a client.
Definition EntityPayload.hpp:60
be32 networkId
Identifier of the removed entity.
Definition EntityPayload.hpp:62
DestroyReason reason
Why the entity is removed.
Definition EntityPayload.hpp:63
be32 serverTick
Server tick at sending time.
Definition EntityPayload.hpp:61
Payload of a QueryEntity message, 4 bytes, sent by a client that received a snapshot for an entity it...
Definition EntityPayload.hpp:74
be32 networkId
Identifier of the unknown entity.
Definition EntityPayload.hpp:75