Rise
The Vieneo Province
Loading...
Searching...
No Matches
EarPressure.h
Go to the documentation of this file.
1#pragma once
2
3#include <algorithm>
4#include <cmath>
5#include <cstdint>
6
7// Gameplay tuning from Mantis 2098, not a medical model. Pressure is in mbar.
9{
10 std::uint32_t randomState;
11 bool initialized = false;
12 unsigned int character = 0;
13 float middlePressure = 0.0f;
14 float baseline = 0.0f;
15 float threshold = 0.0f;
16 float cooldown = 0.0f;
17 float muffle = 0.0f;
18 double secondsSincePop = -1.0;
19 static constexpr double PopReleaseSeconds = 0.35;
21 float muffleReleaseStart = 0.0f;
22
23 float Random(float low, float high)
24 {
25 randomState = randomState * 1664525u + 1013904223u;
26 return low + (high - low) * static_cast<float>(randomState >> 8) / 16777215.0f;
27 }
28
29public:
31 {
32 float pressureMb = 0.0f;
33 float middlePressureMb = 0.0f;
34 float differentialMb = 0.0f; // Signed: positive means rising cabin pressure.
35 float thresholdMb = 0.0f;
36 double secondsSincePreviousPop = -1.0; // First pop after initialization/reset.
37 };
38
39 explicit EarPressure(std::uint32_t seed = 2098u) : randomState(seed) {}
40
41 void Reset()
42 {
43 initialized = false;
46 secondsSincePop = -1.0;
47 }
48
49 float Muffle() const { return muffle; }
50 float Threshold() const { return threshold; }
51 float Baseline() const { return baseline; }
52
53 bool Update(bool active, unsigned int characterId, float pressureMb, int age, int sex, float seconds,
54 PopDiagnostics* diagnostics = nullptr)
55 {
56 if (!active || !std::isfinite(pressureMb) || pressureMb < 0.0f ||
57 !std::isfinite(seconds) || seconds < 0.0f)
58 {
59 Reset();
60 return false;
61 }
62
63 if (!initialized || character != characterId)
64 {
65 character = characterId;
66 // Children take precedence over sex; unknown age/sex uses adult tuning.
67 baseline = age > 0 && age < 18 ? Random(1.5f, 2.2f) :
68 sex == 2 ? Random(1.8f, 2.6f) : Random(2.0f, 3.0f);
69 threshold = baseline + Random(-0.2f, 0.2f);
70 middlePressure = pressureMb;
73 initialized = true;
74 secondsSincePop = -1.0;
75 return false;
76 }
77 // Re-anchor after a suspended/stalled session, without an artificial pop.
78 if (seconds > 1.0f)
79 {
80 middlePressure = pressureMb;
83 secondsSincePop = -1.0;
84 return false;
85 }
86 if (seconds == 0.0f)
87 return false;
88
89 if (secondsSincePop >= 0.0)
90 secondsSincePop += seconds;
91 cooldown = (std::max)(0.0f, cooldown - seconds);
92 float differential = std::fabs(pressureMb - middlePressure);
93 bool popped = differential >= threshold && cooldown == 0.0f;
94 if (popped)
95 {
96 // Capture the triggering values before equalization and the next draw.
97 if (diagnostics)
98 *diagnostics = {pressureMb, middlePressure, pressureMb - middlePressure,
100 secondsSincePop = 0.0;
101 middlePressure = pressureMb;
102 // Draw once per equalization, never once per frame.
103 threshold = baseline + Random(-0.2f, 0.2f);
104 cooldown = 0.35f;
105 // Keep the existing filter for the pop; release starts on the next update.
108 return true;
109 }
110 if (muffleReleaseRemaining > 0.0)
111 {
112 muffleReleaseRemaining = (std::max)(0.0, muffleReleaseRemaining - seconds);
113 const float t = static_cast<float>(muffleReleaseRemaining / PopReleaseSeconds);
114 // Smoothstep reaches exactly zero in 0.35 s, without a sudden first-frame drop.
115 muffle = muffleReleaseStart * t * t * (3.0f - 2.0f * t);
116 return false;
117 }
118 float target = (std::min)(2.5f, (std::max)(0.0f, differential - 0.5f));
119 const float timeConstant = target < muffle ? 0.017f : 0.2f;
120 muffle += (target - muffle) * (1.0f - std::exp(-seconds / timeConstant));
121 if (muffle < 0.001f)
122 muffle = 0.0f;
123 return false;
124 }
125};
float Baseline() const
Definition EarPressure.h:51
double muffleReleaseRemaining
Definition EarPressure.h:20
float Muffle() const
Definition EarPressure.h:49
unsigned int character
Definition EarPressure.h:12
float middlePressure
Definition EarPressure.h:13
static constexpr double PopReleaseSeconds
Definition EarPressure.h:19
float baseline
Definition EarPressure.h:14
void Reset()
Definition EarPressure.h:41
float cooldown
Definition EarPressure.h:16
bool Update(bool active, unsigned int characterId, float pressureMb, int age, int sex, float seconds, PopDiagnostics *diagnostics=nullptr)
Definition EarPressure.h:53
float Random(float low, float high)
Definition EarPressure.h:23
bool initialized
Definition EarPressure.h:11
float muffleReleaseStart
Definition EarPressure.h:21
EarPressure(std::uint32_t seed=2098u)
Definition EarPressure.h:39
std::uint32_t randomState
Definition EarPressure.h:10
float Threshold() const
Definition EarPressure.h:50
float threshold
Definition EarPressure.h:15
double secondsSincePop
Definition EarPressure.h:18