11#include "ecs/component/core/transform.h"
12#include "ecs/component/physics/rigidbody.h"
13#include "ecs/entity.h"
14#include "net/prediction/command.h"
15#include "net/prediction/interpolation.h"
16#include "net/prediction/pacing.h"
17#include "net/replication/names.h"
18#include "net/transport/connection.h"
19#include "net/transport/messages.h"
20#include "net/wire/bit_stream.h"
21#include "net/wire/protocol.h"
23namespace Vkm::Engine {
36constexpr uint32_t NET_SPAWN_BUILDS_PER_FRAME = 8;
46 explicit NetClient(
NetCore& core);
49 NetClient(
const NetClient& other) =
delete;
50 NetClient& operator=(
const NetClient& other) =
delete;
52 NetClient(NetClient && other) =
delete;
53 NetClient& operator=(NetClient && other) =
delete;
65 void advance(
float seconds);
84 void lease(
const std::vector<EntityId>& entities);
95 void beginReplayTick() { m_prediction.replaying =
true; }
100 void interpolate(
Scene& scene,
float deltaTime);
123 std::string describe()
const;
126 PlayerId localPlayer()
const {
return m_localPlayer; }
127 EntityId localEntity()
const {
return m_localEntity; }
130 bool isPlaying()
const {
return m_localPlayer != NO_PLAYER; }
132 float roundTrip()
const {
return m_connection ? m_connection->roundTrip() : 0.0f; }
134 size_t leaseCount()
const {
return m_prediction.leases.size(); }
136 const std::vector<InputCommand>& replayCommands()
const {
return m_prediction.replay; }
138 bool replaying()
const {
return m_prediction.replaying; }
140 float predictionError()
const {
return m_prediction.error; }
142 float renderDelay()
const {
return m_interpolation.behindTicks(); }
147 double drawnTick()
const {
return m_interpolation.
renderTick(); }
149 float pacing()
const {
return m_prediction.pacing.
scale(); }
157 glm::vec3 position{0.0f};
175 struct DrawCorrection {
191 bool hasBody =
false;
200 std::vector<Predicted> predicted;
202 std::vector<InputCommand> unacknowledged;
203 std::vector<InputCommand> replay;
204 bool replaying =
false;
216 std::vector<Lease> leases;
217 std::vector<EntityId> acquired;
218 std::vector<EntityId> released;
221 std::vector<Held> held;
222 std::vector<DrawCorrection> corrections;
224 bool warnedNoReplay =
false;
226 void clear() { *
this = Prediction{}; }
235 glm::vec3 drawn{0.0f};
239 bool onWelcome(Scene& scene, BitReader& reader);
240 bool onRefuse(BitReader& reader);
242 void onSnapshot(Scene& scene, BitReader& reader);
254 void holdPredicted(Scene& scene, std::vector<DrawnFrom>& takenFrom);
266 void buildSpawned(Scene& scene, ResourceManager& resources);
279 void buildInto(Scene& scene, ResourceManager& resources, EntityId root,
const NetSpawn& spawn);
290 void reconcile(Scene& scene, uint32_t confirmedTick);
301 void restoreHeld(Scene& scene);
302 void correctDraw(EntityId entity,
const glm::vec3& drawn,
const glm::vec3& simulated);
303 void offsetDrawn(Scene& scene, EntityId entity,
const glm::vec3& by);
304 glm::vec3 drawOffsetFor(EntityId entity)
const;
312 bool adoptServerTick(uint32_t serverTick);
317 std::unique_ptr<NetConnection> m_connection;
319 PlayerId m_localPlayer = NO_PLAYER;
320 EntityId m_localEntity;
321 uint64_t m_token = 0;
323 bool m_warnedTickJump =
false;
326 Prediction m_prediction;
328 NetInterpolation m_interpolation;
329 std::vector<EntityId> m_touched;
330 std::vector<EntityId> m_lost;
bool drawingServerTicks() const
Whether a snapshot has arrived, so drawnTick() is the server's.
Definition net_client.h:145
bool isPlaying() const
From the Welcome until the session ends.
Definition net_client.h:130
void ageLeases()
Count every lease down a tick, outside a replay, and hand back the lapsed.
void close()
Tell the server goodbye and forget everything this session held but what it made.
void adoptWorld(const Scene &scene)
The world as loaded; see NetSession::setWorld.
Definition net_client.h:115
void sweepEnded(Scene &scene)
Destroy what the session last closed had made (NetNames::sweepEnded).
Definition net_client.h:118
void endReplay(const Scene &scene)
Finish the replay and measure what the picture owes for it.
void endTick(const Scene &scene, uint32_t tick)
Record where this end's own entity ended tick.
void removeDrawCorrection(Scene &scene)
Take back what applyDrawCorrection applied.
void beginTick(const InputCommand &command)
Keep command until the server says it ran it.
void endSession()
Mark what this session made to go at the next sweepEnded: the session is over.
Definition net_client.h:63
void forgetWorld()
Drop everything naming an entity; see NetSession::forgetWorld.
bool simulates(const Scene &scene, EntityId entity) const
See NetSession::simulates.
bool receive(Scene &scene, ResourceManager &resources)
Take what has arrived, one datagram at a time, then build what was spawned.
void open(const NetAddress &server)
Start talking to server. Nothing is sent until the first tick has run.
bool sendMessage(const uint8_t *bytes, size_t size)
See NetSession::sendMessage.
void send()
Send a Hello until welcomed, then the commands the server has not heard.
void applyDrawCorrection(Scene &scene, float deltaTime)
Work a little of every outstanding correction off, and apply the rest.
EntityId entityFor(const Scene &scene, uint32_t slot) const
See NetSession::entityFor.
Definition net_client.h:112
void lease(const std::vector< EntityId > &entities)
See NetSession::lease.
double renderTick() const
Where the clock that decides what is drawn currently stands, in ticks.
Definition interpolation.h:154
float scale() const
How much faster than wall time to run ticks.
Definition pacing.h:76
Type-erased resource registry with typed handles and generational lifetimes.
Definition resource_manager.h:46
Central registry managing entities and an open set of component types.
Definition scene.h:29
Names one entity of one Scene, for as long as that slot holds it.
Definition entity.h:23
Where a datagram came from, or is going to: an IPv4 host and a port.
Definition net_address.h:13
What a session's server and client halves share: the socket, what the two ends agreed,...
Definition net_session.h:55
What a server built at this root after the scene loaded, so every client can build the same.
Definition net_spawn.h:19