Rise
The Vieneo Province
Loading...
Searching...
No Matches
PersonWaitingPolicy.h
Go to the documentation of this file.
1#pragma once
2
3#include <algorithm>
4#include <array>
5#include <cmath>
6#include <cstdint>
7
8// ANI2 adapter policy. One instance belongs to one avatar, never to the camera
9// or a process-wide timer. No file parsing, rendering, or legacy ANI dependency.
11{
12 // Left/right name the avatar's anatomical foot receiving the weight, for
13 // both motion profiles. They are independent actions, never a mirrored view.
15 constexpr std::size_t GestureCount = static_cast<std::size_t>(Gesture::Count);
19
21 {
22 // The loader opts in only after resolving all clips for an action.
23 std::array<bool, GestureCount> available = {};
24 std::array<double, GestureCount> weights = {{2.5, 2.5, 1.0, 2.0}};
25 std::array<double, GestureCount> cooldownSeconds = {{12.0, 12.0, 45.0, 30.0}};
28 double minIntervalSeconds = 8.0;
29 double maxIntervalSeconds = 18.0;
30 double minHipHoldSeconds = 3.0;
31 double maxHipHoldSeconds = 7.0;
32 double interruptBlendSeconds = 0.35;
33 double hipExitBlendSeconds = 0.35;
34 };
35
36 struct Input
37 {
38 double elapsedSeconds = 0.0;
39 // True only for a grounded, standing avatar or a gesture owned by this
40 // scheduler. False in a seat, a walk, a fall, a transition, or a vehicle.
41 bool eligible = false;
43 // Applies to the scheduler's last PlayClip or BlendToStanding command.
44 // Do not set this on a loop wrap of HipHold.
45 bool finished = false;
46 };
47
48 struct Command
49 {
53 // PlayClip with a blend first blends to the fixed first frame, then
54 // plays the entire authored clip from time zero. The host acknowledges
55 // finished only after both phases; clip time does not advance in blend.
56 double blendSeconds = 0.0;
57 };
58
60 {
61 public:
62 explicit Scheduler(std::uint64_t avatarId, std::uint64_t spawnSalt,
63 const Configuration& configuration = Configuration())
64 : settings_(configuration)
65 {
66 auto seed = avatarId + 0x9e3779b97f4a7c15ULL + spawnSalt;
67 seed = (seed ^ (seed >> 30)) * 0xbf58476d1ce4e5b9ULL;
68 seed = (seed ^ (seed >> 27)) * 0x94d049bb133111ebULL;
69 seed ^= seed >> 31;
70 random_ = static_cast<std::uint32_t>(seed ^ (seed >> 32));
71 if (random_ == 0) random_ = 0x6d2b79f5U;
72 remainingCooldown_.fill(0.0);
73 }
74
75 void Reset()
76 {
79 timer_ = 0.0;
80 // Keep the random stream and cooldown history across movement, so
81 // stopping again cannot reproduce the same timed gesture forever.
82 }
83
84 Command Update(const Input& input)
85 {
86 const double dt = std::isfinite(input.elapsedSeconds) && input.elapsedSeconds > 0.0
87 ? input.elapsedSeconds : 0.0;
88 for (auto& cooldown : remainingCooldown_)
89 cooldown = std::max(0.0, cooldown - dt);
90
91 if (!input.eligible)
92 {
93 Reset();
94 return {};
95 }
97
99 BeginDelay(true);
100
101 // The first frame with movement input takes control immediately.
102 // The host owns the bounded blend/exit and acknowledges its finish.
104 {
105 if (state_ == State::Delay)
106 return BeginMovement();
107 if ((state_ == State::Gesture && !input.finished) || state_ == State::HipEntry)
108 {
112 }
113 if (state_ == State::HipHold)
114 {
118 }
119 }
120
122 && input.finished)
123 {
125 return BeginMovement();
126 BeginDelay(false);
127 return {};
128 }
129 if (state_ == State::HipEntry && input.finished)
130 {
134 }
135 if (state_ == State::HipHold)
136 {
137 timer_ -= dt;
138 if (timer_ <= 0.0)
139 {
141 // A randomized hold time can end on any breathing phase.
144 }
145 return {};
146 }
147 if (state_ != State::Delay)
148 return {};
149
150 timer_ -= dt;
151 if (timer_ > 0.0)
152 return {};
153 const int selected = ChooseGesture();
154 if (selected < 0)
155 {
156 // All clips can be cooling down or absent. Stay neutral, and
157 // retry later; unavailable clips are never fabricated.
158 timer_ = 1.0;
159 return {};
160 }
161 previous_ = selected;
162 remainingCooldown_[selected] = std::max(0.0, Finite(settings_.cooldownSeconds[selected], 0.0));
163 if (selected == static_cast<int>(Gesture::HandOnHip))
164 {
167 }
169 return {CommandKind::PlayClip,
170 selected == static_cast<int>(Gesture::WeightShiftLeft) ? Clip::WeightShiftLeft :
171 selected == static_cast<int>(Gesture::WeightShiftRight) ? Clip::WeightShiftRight : Clip::CheckWatch};
172 }
173
174 private:
175 enum class State { Inactive, Delay, Gesture, HipEntry, HipHold, HipExit, NeutralBlend };
177 std::uint32_t random_;
178 std::array<double, GestureCount> remainingCooldown_;
181 double timer_ = 0.0;
182 int previous_ = -1;
183
184 static double Finite(double value, double fallback)
185 {
186 return std::isfinite(value) ? value : fallback;
187 }
188 static double Positive(double value, double fallback)
189 {
190 return std::isfinite(value) && value > 0.0 ? value : fallback;
191 }
192 double UnitRandom()
193 {
194 random_ ^= random_ << 13;
195 random_ ^= random_ >> 17;
196 random_ ^= random_ << 5;
197 return static_cast<double>(random_) / 4294967296.0;
198 }
199 double Range(double minimum, double maximum, double defaultMinimum, double defaultMaximum)
200 {
201 const double low = Positive(minimum, defaultMinimum);
202 const double high = std::max(low, Positive(maximum, defaultMaximum));
203 return low + (high - low) * UnitRandom();
204 }
213 {
214 const auto movement = pending_;
215 Reset();
216 return {CommandKind::BeginMovement, Clip::None, movement};
217 }
219 {
220 std::array<double, GestureCount> weights = {};
221 int availableCount = 0;
222 for (int i = 0; i != static_cast<int>(GestureCount); ++i)
223 if (settings_.available[i] && remainingCooldown_[i] <= 0.0 &&
224 std::isfinite(settings_.weights[i]) && settings_.weights[i] > 0.0)
225 {
226 weights[i] = settings_.weights[i];
227 ++availableCount;
228 }
229 if (availableCount > 1 && previous_ >= 0)
230 weights[previous_] = 0.0;
231 double total = 0.0;
232 for (const auto weight : weights) total += weight;
233 if (total <= 0.0 || !std::isfinite(total)) return -1;
234 const double choice = UnitRandom() * total;
235 double cumulative = 0.0;
236 for (int i = 0; i != static_cast<int>(GestureCount); ++i)
237 {
238 cumulative += weights[i];
239 if (choice < cumulative && weights[i] > 0.0) return i;
240 }
241 return -1;
242 }
243 };
244
245 struct Pose
246 {
247 std::array<std::array<double, 3>, 16> anglesDegrees = {};
248 std::array<double, 3> translationMeters = {};
250 };
251
252 inline double SmoothBlendProgress(double elapsedSeconds, double durationSeconds)
253 {
254 if (!std::isfinite(elapsedSeconds) || elapsedSeconds <= 0.0) return 0.0;
255 if (!std::isfinite(durationSeconds) || durationSeconds <= 0.0) return 1.0;
256 const double t = std::max(0.0, std::min(1.0, elapsedSeconds / durationSeconds));
257 return t * t * t * (t * (6.0 * t - 15.0) + 10.0);
258 }
259
260 // Capture 'from' once when the command is received, before changing clips.
261 // For an authored exit, 'to' is its fixed first frame. Only after this blend
262 // completes does the host start sampling the full exit from time zero.
263 // Blend intrinsic grounding and camera height with the same easing as joints.
264 // Keep accumulated world/root placement outside this pose and unchanged.
265 inline Pose BlendPose(const Pose& from, const Pose& to, double elapsedSeconds, double durationSeconds)
266 {
267 const double alpha = SmoothBlendProgress(elapsedSeconds, durationSeconds);
268 if (alpha <= 0.0) return from;
269 if (alpha >= 1.0) return to;
270 Pose result;
271 for (std::size_t joint = 0; joint != result.anglesDegrees.size(); ++joint)
272 for (std::size_t axis = 0; axis != 3; ++axis)
273 {
274 const double start = from.anglesDegrees[joint][axis];
275 const double delta = std::remainder(to.anglesDegrees[joint][axis] - start, 360.0);
276 result.anglesDegrees[joint][axis] = start + delta * alpha;
277 }
278 for (std::size_t axis = 0; axis != 3; ++axis)
279 result.translationMeters[axis] = from.translationMeters[axis] +
280 (to.translationMeters[axis] - from.translationMeters[axis]) * alpha;
283 return result;
284 }
285}
@ None
Definition ServerTypes.h:97
std::array< double, GestureCount > remainingCooldown_
Command Update(const Input &input)
Scheduler(std::uint64_t avatarId, std::uint64_t spawnSalt, const Configuration &configuration=Configuration())
static double Positive(double value, double fallback)
static double Finite(double value, double fallback)
double Range(double minimum, double maximum, double defaultMinimum, double defaultMaximum)
double SmoothBlendProgress(double elapsedSeconds, double durationSeconds)
constexpr std::size_t GestureCount
Pose BlendPose(const Pose &from, const Pose &to, double elapsedSeconds, double durationSeconds)
std::array< double, GestureCount > weights
std::array< double, GestureCount > cooldownSeconds
std::array< bool, GestureCount > available
std::array< std::array< double, 3 >, 16 > anglesDegrees
std::array< double, 3 > translationMeters