group2 0.1.0
CSE 125 Group 2
Loading...
Searching...
No Matches
LagCompensation.hpp File Reference

RAII guard for hitbox-rewind lag compensation. More...

#include "ecs/components/CollisionShape.hpp"
#include "ecs/components/Hitbox.hpp"
#include "ecs/components/HitboxHistory.hpp"
#include "ecs/components/LagCompTarget.hpp"
#include "ecs/components/Position.hpp"
#include "ecs/components/Velocity.hpp"
#include "ecs/physics/Raycast.hpp"
#include "ecs/registry/Registry.hpp"
#include <cstdint>
#include <glm/common.hpp>
#include <glm/vec3.hpp>
#include <utility>
#include <vector>
Include dependency graph for LagCompensation.hpp:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

class  systems::RewindHitboxesGuard
 RAII handle that restores hitbox capsules on scope exit after a rewindHitboxes call swapped them for historical samples. More...

Namespaces

namespace  systems
 Client-only input sampling system — split into two halves so mouse look can run every iterate() (smooth camera at any FPS) while movement keys run once per physics tick group (server-consistent).

Functions

RewindHitboxesGuard systems::rewindHitboxes (Registry &registry, entt::entity shooter, const glm::vec3 *rayOrigin=nullptr, const glm::vec3 *rayDirection=nullptr, float rayMaxDistance=0.0f)
 Rewind every other player's hitbox capsules to where they were on shooter's screen at fire time.

Detailed Description

RAII guard for hitbox-rewind lag compensation.

When the server processes an attacker's hitscan input, rewindHitboxes(registry, shooter) swaps every other player's HitboxInstance::capsules for the historical snapshot that was current on the attacker's screen at fire time (looked up via HitboxHistory). On scope exit the guard's destructor restores the live capsules so subsequent simulation work isn't affected.

The rewind target is read from a per-shooter LagCompTarget component populated by the server's lag-comp scheduler each tick: targetServerTick = currentServerTick - clamp(rttMs/2 → ticks, 0, k_maxLagCompTicks). Zero-target = no rewind (used for new connections that haven't completed a ping round trip and for loopback / sub-tick RTTs).

Cross-binary behaviour

LagCompensation.hpp is shared between server and client TUs because WeaponSystem.cpp (which calls rewindHitboxes) is shared. On the client, no entity ever has a LagCompTarget or HitboxHistory component — the rewind helper short-circuits to a no-op guard. The same code path runs in both binaries; only the server populates the inputs that make it do real work.

Design notes

  • The guard is move-only. Copy would silently double-restore.
  • The destructor must be exception-safe so a throwing raycast doesn't skip the restore. Today no raycast throws, but the contract is cheap to honour.
  • The "find best sample" is O(k_capacity) = O(32). With ~30 players × ~12 capsules each, the per-hitscan rewind is well under the runWeapon budget.