group2 0.1.0
CSE 125 Group 2
Loading...
Searching...
No Matches
AnimSnapshot.hpp
Go to the documentation of this file.
1
37
38#pragma once
39
40#include <algorithm>
41#include <array>
42#include <cmath>
43#include <cstddef>
44#include <cstdint>
45
48{
52 std::uint8_t clipIdRaw = 0xFFu;
53
56 float timeRatio = 0.0f;
57
60 float weight = 0.0f;
61};
62
73{
74 static constexpr std::size_t k_numSlots = 5;
75 std::array<AnimSlot, k_numSlots> slots{};
76};
77
79{
80
85[[nodiscard]] inline float delta(const AnimSnapshot& a, const AnimSnapshot& b)
86{
87 float d = 0.0f;
88 for (std::size_t i = 0; i < AnimSnapshot::k_numSlots; ++i) {
89 const auto& sa = a.slots[i];
90 const auto& sb = b.slots[i];
91 const bool aActive = sa.weight > 0.0f;
92 const bool bActive = sb.weight > 0.0f;
93 if (aActive != bActive) {
94 // Active/inactive mismatch — typical near a transition edge.
95 // Soft penalty so a half-transitioned blend doesn't reject
96 // the whole shot.
97 d += 0.5f;
98 continue;
99 }
100 if (!aActive)
101 continue; // both inactive: nothing to compare.
102 if (sa.clipIdRaw != sb.clipIdRaw) {
103 // Different clip in the same slot — state-machine
104 // disagreement, can't be reconciled by timeRatio fuzziness.
105 d += 1.0f;
106 continue;
107 }
108 const float maxW = std::max(sa.weight, sb.weight);
109 d += std::abs(sa.timeRatio - sb.timeRatio) * maxW;
110 d += std::abs(sa.weight - sb.weight) * 0.5f;
111 }
112 return d;
113}
114
119inline void pack(const AnimSlot& s, std::uint8_t out[4])
120{
121 out[0] = s.clipIdRaw;
122 const float tClamped = std::max(0.0f, std::min(1.0f, s.timeRatio));
123 const float wClamped = std::max(0.0f, std::min(1.0f, s.weight));
124 const auto tq = static_cast<std::uint16_t>(std::lround(tClamped * 65535.0f));
125 const auto wq = static_cast<std::uint8_t>(std::lround(wClamped * 255.0f));
126 out[1] = static_cast<std::uint8_t>(tq & 0xFFu);
127 out[2] = static_cast<std::uint8_t>((tq >> 8) & 0xFFu);
128 out[3] = wq;
129}
130
131inline AnimSlot unpack(const std::uint8_t in[4])
132{
133 AnimSlot s;
134 s.clipIdRaw = in[0];
135 const std::uint16_t tq =
136 static_cast<std::uint16_t>(static_cast<std::uint16_t>(in[1]) | static_cast<std::uint16_t>(in[2] << 8));
137 s.timeRatio = static_cast<float>(tq) / 65535.0f;
138 s.weight = static_cast<float>(in[3]) / 255.0f;
139 return s;
140}
141
143inline constexpr std::size_t k_wireSize = AnimSnapshot::k_numSlots * 4;
144
145inline void packSnapshot(const AnimSnapshot& snap, std::uint8_t out[k_wireSize])
146{
147 for (std::size_t i = 0; i < AnimSnapshot::k_numSlots; ++i)
148 pack(snap.slots[i], &out[i * 4]);
149}
150
151inline AnimSnapshot unpackSnapshot(const std::uint8_t in[k_wireSize])
152{
153 AnimSnapshot s;
154 for (std::size_t i = 0; i < AnimSnapshot::k_numSlots; ++i)
155 s.slots[i] = unpack(&in[i * 4]);
156 return s;
157}
158
159} // namespace anim_snapshot
160
177{
178 bool received = false;
179 std::uint16_t targetClientId = 0xFFFFu;
181};
Definition AnimSnapshot.hpp:79
AnimSlot unpack(const std::uint8_t in[4])
Definition AnimSnapshot.hpp:131
AnimSnapshot unpackSnapshot(const std::uint8_t in[k_wireSize])
Definition AnimSnapshot.hpp:151
float delta(const AnimSnapshot &a, const AnimSnapshot &b)
PR-27 animation-state delta.
Definition AnimSnapshot.hpp:85
constexpr std::size_t k_wireSize
Total wire size of an AnimSnapshot (5 slots × 4 bytes).
Definition AnimSnapshot.hpp:143
void pack(const AnimSlot &s, std::uint8_t out[4])
Pack one AnimSlot into 4 bytes for wire transmission.
Definition AnimSnapshot.hpp:119
void packSnapshot(const AnimSnapshot &snap, std::uint8_t out[k_wireSize])
Definition AnimSnapshot.hpp:145
One slot in an entity's blend stack.
Definition AnimSnapshot.hpp:48
float timeRatio
Normalised playhead position in [0, 1] over the clip's duration.
Definition AnimSnapshot.hpp:56
float weight
Blend weight in [0, 1].
Definition AnimSnapshot.hpp:60
std::uint8_t clipIdRaw
Raw uint8_t cast of ClipId.
Definition AnimSnapshot.hpp:52
Snapshot of an entity's full animation state at one instant.
Definition AnimSnapshot.hpp:73
static constexpr std::size_t k_numSlots
Definition AnimSnapshot.hpp:74
std::array< AnimSlot, k_numSlots > slots
Definition AnimSnapshot.hpp:75
PR-27: transient component placed on the shooter entity for the duration of one runWeapon invocation ...
Definition AnimSnapshot.hpp:177
std::uint16_t targetClientId
Definition AnimSnapshot.hpp:179
bool received
Definition AnimSnapshot.hpp:178
AnimSnapshot targetAnim
Definition AnimSnapshot.hpp:180