vkmEngine 1.0.15
A C++ game engine · vkmengine.com
Loading...
Searching...
No Matches
schema.h
1#pragma once
2
3#include <cstdint>
4#include <string>
5#include <string_view>
6#include <vector>
7
8#include "ecs/scene.h"
9#include "net/wire/bit_stream.h"
10
11namespace Vkm::Engine {
12
20inline constexpr uint32_t MAX_COMPONENT_BYTES = 512;
21
29template <typename T>
30bool sameOnWire(const T& a, const T& b) {
31 std::vector<uint8_t> first;
32 std::vector<uint8_t> second;
33 BitWriter one(first, MAX_COMPONENT_BYTES);
34 BitWriter two(second, MAX_COMPONENT_BYTES);
35 netEncode(a, one);
36 netEncode(b, two);
37 one.finish();
38 two.finish();
39 return one.bitCount() == two.bitCount() && first == second;
40}
41
49template <typename T>
50void netKeep(T& held, const T& incoming) {
51 (void)held;
52 (void)incoming;
53}
54
60struct NetType {
61 std::string name;
62
64 bool (*has)(const Scene&, EntityId) = nullptr;
65
67 void (*encode)(const Scene&, EntityId, BitWriter&) = nullptr;
68
70 bool (*decode)(Scene&, EntityId, BitReader&) = nullptr;
71
73 void (*remove)(Scene&, EntityId) = nullptr;
74};
75
84class NetSchema {
85 public:
91 static constexpr size_t MAX_TYPES = 32;
92
93 public:
94 NetSchema() = default;
95 ~NetSchema() = default;
96
97 NetSchema(const NetSchema& other) = delete;
98 NetSchema& operator=(const NetSchema& other) = delete;
99
100 NetSchema(NetSchema && other) = delete;
101 NetSchema& operator=(NetSchema && other) = delete;
102
103 public:
105 static NetSchema& get();
106
107 public:
117 template <typename T>
118 void replicate(std::string name);
119
121 const std::vector<NetType>& types() const { return m_types; }
122
123 size_t size() const { return m_types.size(); }
124
133 int indexOf(std::string_view name) const;
134
144 uint32_t fingerprint() const;
145
151 std::string describe() const;
152
159 void clear() { m_types.clear(); }
160
161 private:
162 void add(NetType type);
163
164 private:
165 std::vector<NetType> m_types;
166};
167
168template <typename T>
169void NetSchema::replicate(std::string name) {
170 NetType type;
171 type.name = std::move(name);
172
173 // Stateless thunks: plain function pointers, nothing owned.
174 type.has = [](const Scene& scene, EntityId entity) { return scene.has<T>(entity); };
175 type.encode = [](const Scene& scene, EntityId entity, BitWriter& writer) {
176 netEncode(scene.get<T>(entity), writer);
177 };
178 type.decode = [](Scene& scene, EntityId entity, BitReader& reader) {
179 T* held = scene.tryGet<T>(entity);
180 if (!held) {
181 // Added, not required: a client learns of a component by being sent one.
182 netDecode(scene.add<T>(entity, T{}), reader);
183 return !reader.failed();
184 }
185 // A value the wire cannot tell from the one held stays as held: a scene's authored
186 // position is exact, and its quantised copy would only move it by a rounding.
187 T incoming = *held;
188 netDecode(incoming, reader);
189 if (reader.failed()) return false;
190 if (sameOnWire(*held, incoming)) netKeep(*held, incoming);
191 else *held = std::move(incoming);
192 return true;
193 };
194 type.remove = [](Scene& scene, EntityId entity) { scene.remove<T>(entity); };
195
196 add(std::move(type));
197}
198
199} // namespace Vkm::Engine
Reads back what a BitWriter wrote, in the same order.
Definition bit_stream.h:145
Packs values to the bit into a byte buffer.
Definition bit_stream.h:19
static NetSchema & get()
The registry every end consults. Filled by ScriptModule::setupNetwork, read thereafter.
void clear()
Drops everything.
Definition schema.h:159
static constexpr size_t MAX_TYPES
Most component types one game may replicate.
Definition schema.h:91
const std::vector< NetType > & types() const
Everything registered, in wire order.
Definition schema.h:121
void replicate(std::string name)
Add T to what replicates.
Definition schema.h:169
int indexOf(std::string_view name) const
Wire index of name, or -1.
uint32_t fingerprint() const
A number that differs when the lists differ, or the codecs' layout does.
std::string describe() const
Every name in order, for the log line that follows a refusal.
Central registry managing entities and an open set of component types.
Definition scene.h:29
T * tryGet(EntityId entity)
The entity's T, or null when it has none.
Definition scene.h:206
T & get(EntityId entity)
Get a mutable reference to an entity's component of type T.
Definition scene.h:176
bool has(EntityId entity) const
Check if an entity has a component of type T.
Definition scene.h:162
auto & add(EntityId entity, T &&component)
Give an entity a component it does not have yet.
Definition scene.h:128
void remove(EntityId entity)
Take a component of type T off an entity.
Definition scene.h:145
Names one entity of one Scene, for as long as that slot holds it.
Definition entity.h:23
One replicated component type, reduced to what the wire needs.
Definition schema.h:60
std::string name
The serializable identity, as everywhere else in the engine.
Definition schema.h:61
bool(* has)(const Scene &, EntityId)
Does this entity carry one? Decides its bit in a snapshot's mask.
Definition schema.h:64
bool(* decode)(Scene &, EntityId, BitReader &)
Decode onto this entity, adding the component when it is missing.
Definition schema.h:70
void(* remove)(Scene &, EntityId)
Take this entity's component away: the server holds it no longer.
Definition schema.h:73
void(* encode)(const Scene &, EntityId, BitWriter &)
Encode this entity's component. Asked only of an entity has said yes to.
Definition schema.h:67