Rise
The Vieneo Province
Loading...
Searching...
No Matches
BiometerDiagnostics.h
Go to the documentation of this file.
1#pragma once
2#include <array>
3#include <cmath>
4#include <cstdint>
5#include <cstdio>
6
8{
9 // These are synthetic game-model values, not health measurements. Capture
10 // only the calculation, before its values reach audio or the UI; omit avatar
11 // profile fields, player/vehicle identifiers and position.
12 // Zero BPM is valid at initialization/death; the character's maximum is
13 // the upper bound. A target above that bound is normally capped, not a fault.
14 struct Sample
15 {
16 double time = 0;
17 float dt = 0, previous = 0, integrationStart = 0, integrated = 0, current = 0;
18 float maximum = 0, target = 0, base = 0, vaso = 0, adrenaline = 0, stress = 0;
19 float gx = 0, gy = 0, gz = 0, pressure = 0;
20 float agl = 0, speed = 0, elevation = 0, visibility = 0, heartFrame = 0;
21 bool dead = false, simulator = false;
22 };
23
24 enum Fault : unsigned
25 {
26 Current = 1, Target = 2, Maximum = 4, Inputs = 8,
27 HeartFrame = 16, Overshoot = 32, Baseline = 64, Previous = 128
28 };
29
30 inline unsigned Faults(const Sample& s)
31 {
32 unsigned result = 0;
33 const bool validMaximum = std::isfinite(s.maximum) && s.maximum > 0;
34 if (!std::isfinite(s.current) || s.current < 0 || (validMaximum && s.current > s.maximum)) result |= Current;
35 if (!std::isfinite(s.target) || s.target < 0) result |= Target;
36 if (!s.dead && !validMaximum) result |= Maximum;
37 if (!std::isfinite(s.previous) || s.previous < 0) result |= Previous;
38 if (!std::isfinite(s.base) || s.base <= 0) result |= Baseline;
39 if (!std::isfinite(s.dt) || s.dt < 0 || !std::isfinite(s.gx) || !std::isfinite(s.gy) ||
40 !std::isfinite(s.gz) || !std::isfinite(s.pressure) ||
41 !std::isfinite(s.adrenaline) || !std::isfinite(s.agl) || !std::isfinite(s.speed) ||
42 !std::isfinite(s.elevation) || !std::isfinite(s.visibility)) result |= Inputs;
43 if (!std::isfinite(s.heartFrame) || s.heartFrame < 0 || s.heartFrame >= 29) result |= HeartFrame;
44 // Distinguish a long-step sign reversal from a bad target or carried state.
45 const float responseSeconds = s.target > s.integrationStart ? 5.f : 20.f;
46 if (std::isfinite(s.dt) && s.dt > responseSeconds && std::isfinite(s.target) &&
47 std::isfinite(s.integrationStart) && std::isfinite(s.integrated) &&
48 ((s.target > s.integrationStart && s.integrated > s.target) ||
49 (s.target < s.integrationStart && s.integrated < s.target))) result |= Overshoot;
50 return result;
51 }
52
53 inline const char* Cause(const Sample& s, unsigned faults)
54 {
55 if (faults & Inputs) return "invalid-input";
56 if (faults & Maximum) return "invalid-max-bpm";
57 if (faults & Baseline) return "invalid-base-bpm";
58 if (s.target < 0 && s.gy < -20 && s.vaso < 0) return "negative-g-multiplier";
59 if (!std::isfinite(s.vaso)) return "nonfinite-g-load-term";
60 if (faults & Overshoot) return "frame-step-overshoot";
61 if (faults & Target) return "invalid-target-bpm";
62 if (faults & Previous) return "carried-invalid-bpm";
63 if (faults & Current) return "invalid-result-bpm";
64 return "invalid-heart-frame";
65 }
66
67 class Monitor
68 {
69 std::array<Sample, 4> history{};
70 unsigned cursor = 0, count = 0;
71 std::uint64_t nextReport = 0, affectedFrames = 0;
72 bool active = false;
73
74 public:
75 // Emit context first, then the Error summary: Logger uploads the trace
76 // at the Error call. Sustained faults report at most once per minute.
77 template<class Emit> void Observe(const Sample& sample, std::uint64_t now, Emit emit)
78 {
79 history[cursor] = sample;
80 cursor = (cursor + 1) % unsigned(history.size());
81 if (count < history.size()) ++count;
82 const unsigned faults = Faults(sample);
83 char line[1600];
84 if (!faults)
85 {
86 if (active)
87 {
88 std::snprintf(line, sizeof line, "BIOMETER recovered bpm=%.9g affectedFrames=%llu",
89 sample.current, static_cast<unsigned long long>(affectedFrames));
90 emit(line, false);
91 }
92 active = false;
94 return;
95 }
97 if (active && now < nextReport) return;
98 active = true;
99 nextReport = now + 60000;
100 std::snprintf(line, sizeof line, "BIOMETER context cause=%s flags=0x%x affectedFrames=%llu samples=%u",
101 Cause(sample, faults), faults, static_cast<unsigned long long>(affectedFrames), count);
102 emit(line, false);
103 for (unsigned i = 0; i < count; ++i)
104 {
105 const auto& s = history[(cursor + unsigned(history.size()) - count + i) % history.size()];
106 std::snprintf(line, sizeof line,
107 "BIOMETER sample time=%.9g dt=%.9g previous=%.9g start=%.9g integrated=%.9g current=%.9g "
108 "target=%.9g max=%.9g base=%.9g vaso=%.9g adrenaline=%.9g stress=%.9g "
109 "g=(%.9g,%.9g,%.9g) cabinMb=%.9g "
110 "aglKm=%.9g groundKms=%.9g sunElevation=%.9g visibilityKm=%.9g heartFrame=%.9g "
111 "dead=%d sim=%d",
112 s.time, s.dt, s.previous, s.integrationStart, s.integrated, s.current,
113 s.target, s.maximum, s.base, s.vaso, s.adrenaline, s.stress,
114 s.gx, s.gy, s.gz, s.pressure,
115 s.agl, s.speed, s.elevation, s.visibility, s.heartFrame,
116 int(s.dead), int(s.simulator));
117 emit(line, false);
118 }
119 // Keep the essential diagnosis within the server packet's 190-byte summary.
120 std::snprintf(line, sizeof line,
121 "BIOMETER cause=%s bpm=%.7g prev=%.7g target=%.7g dt=%.7g flags=%x",
122 Cause(sample, faults), sample.current, sample.previous, sample.target, sample.dt, faults);
123 emit(line, true);
124 }
125 };
126}
std::array< Sample, 4 > history
void Observe(const Sample &sample, std::uint64_t now, Emit emit)
unsigned Faults(const Sample &s)
const char * Cause(const Sample &s, unsigned faults)