group2 0.1.0
CSE 125 Group 2
Loading...
Searching...
No Matches
ReconciliationSystem.hpp
Go to the documentation of this file.
1
28
29#pragma once
30
31#include "InputRingBuffer.hpp"
39
40#include <SDL3/SDL_log.h>
41
42#include <cstdint>
43#include <entt/entt.hpp>
44
45namespace systems
46{
47
49{
50 std::uint32_t requestedTicks = 0;
51 std::uint32_t replayedTicks = 0;
52 std::uint32_t missingTicks = 0;
53};
54
74 const InputRingBuffer& ring,
75 uint32_t ackedTick,
76 uint32_t currentTick,
77 float dt,
78 const physics::WorldGeometry& world)
79{
80 ReconciliationStats stats{};
81 if (currentTick <= ackedTick)
82 return stats; // already up to date — server state is the latest predicted state.
83 stats.requestedTicks = currentTick - ackedTick;
84
85 // Find the local player. Reconciliation is meaningless without one.
86 auto localView = registry.view<LocalPlayer, InputSnapshot>();
87 auto it = localView.begin();
88 if (it == localView.end())
89 return stats;
90 const entt::entity local = *it;
91
92 // Replay forward, one physics tick per stored input.
93 int replayed = 0;
94 int missing = 0;
95 for (uint32_t t = ackedTick + 1; t <= currentTick; ++t) {
96 const InputSnapshot* stored = ring.find(t);
97 if (!stored) {
98 // Ring overflow or we never sent for this tick — skip. The
99 // gap means our prediction for this tick is lost, but
100 // continuity is preserved (next stored input still applies).
101 ++missing;
102 continue;
103 }
104 registry.replace<InputSnapshot>(local, *stored);
105 // runMovement+runCollision for the local player only. The
106 // PlayerSimState filter (server-only component, only present on
107 // the local player on the client) narrows the iteration
108 // automatically — no per-entity filter needed.
109 runMovement(registry, dt, world);
110 runCollision(registry, dt, world);
111 ++replayed;
112 }
113 stats.replayedTicks = static_cast<std::uint32_t>(replayed);
114 stats.missingTicks = static_cast<std::uint32_t>(missing);
115
116 if (missing > 0) {
117 // Diagnostic: a non-trivial gap in the ring is unusual at sane
118 // RTTs (256-tick capacity covers ~2 s of replay). Log so we can
119 // notice if RTT or stalls drive the buffer beyond capacity.
120 SDL_Log("[reconcile] %d ticks missing from input ring (replayed %d, range [%u..%u])",
121 missing,
122 replayed,
123 ackedTick + 1,
124 currentTick);
125 }
126 return stats;
127}
128
129} // namespace systems
Shared collision system for swept-AABB physics resolution.
Per-tick history of stamped input snapshots, keyed by clientPredictTick.
Per-tick player input snapshot for networking and prediction.
Marker component identifying the locally controlled player entity.
Shared movement system implementing the Titanfall-inspired state machine.
Client-side prediction: run movement+collision locally each tick.
Shared ECS registry type alias for the game engine.
entt::registry Registry
Shared ECS registry type alias.
Definition Registry.hpp:11
Swept AABB and sphere collision queries against world geometry.
Definition InputRingBuffer.hpp:27
const InputSnapshot * find(uint32_t tick) const
Find the input stamped with exactly tick.
Definition InputRingBuffer.hpp:52
Client-only input sampling system — split into two halves so mouse look can run every iterate() (smoo...
Definition DebugUI.hpp:15
void runCollision(Registry &registry, float dt, const physics::WorldGeometry &world)
Run one tick of swept-AABB collision for all physics entities.
Definition CollisionSystem.cpp:410
ReconciliationStats runReconciliation(Registry &registry, const InputRingBuffer &ring, uint32_t ackedTick, uint32_t currentTick, float dt, const physics::WorldGeometry &world)
Replay stored inputs from ackedTick + 1 through currentTick on the local player, restoring its predic...
Definition ReconciliationSystem.hpp:73
void runMovement(Registry &registry, float dt, const physics::WorldGeometry &world)
Apply one tick of player movement physics to all eligible entities.
Definition MovementSystem.cpp:2016
One tick of player input, stamped with the tick it was sampled on.
Definition InputSnapshot.hpp:17
Marker component that tags exactly one entity per client as the locally controlled player.
Definition LocalPlayer.hpp:12
All world collision geometry for one tick.
Definition SweptCollision.hpp:225
Definition ReconciliationSystem.hpp:49
std::uint32_t replayedTicks
Definition ReconciliationSystem.hpp:51
std::uint32_t requestedTicks
Definition ReconciliationSystem.hpp:50
std::uint32_t missingTicks
Definition ReconciliationSystem.hpp:52