Rise
The Vieneo Province
Toggle main menu visibility
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.
8
class
EarPressure
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;
20
double
muffleReleaseRemaining
= 0.0;
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
29
public
:
30
struct
PopDiagnostics
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
;
44
muffle
=
cooldown
=
muffleReleaseStart
= 0.0f;
45
muffleReleaseRemaining
= 0.0;
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;
71
muffle
=
cooldown
=
muffleReleaseStart
= 0.0f;
72
muffleReleaseRemaining
= 0.0;
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;
81
muffle
=
cooldown
=
muffleReleaseStart
= 0.0f;
82
muffleReleaseRemaining
= 0.0;
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
,
99
threshold
,
secondsSincePop
};
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.
106
muffleReleaseRemaining
=
PopReleaseSeconds
;
107
muffleReleaseStart
=
muffle
;
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
};
EarPressure::Baseline
float Baseline() const
Definition
EarPressure.h:51
EarPressure::muffleReleaseRemaining
double muffleReleaseRemaining
Definition
EarPressure.h:20
EarPressure::Muffle
float Muffle() const
Definition
EarPressure.h:49
EarPressure::character
unsigned int character
Definition
EarPressure.h:12
EarPressure::middlePressure
float middlePressure
Definition
EarPressure.h:13
EarPressure::PopReleaseSeconds
static constexpr double PopReleaseSeconds
Definition
EarPressure.h:19
EarPressure::baseline
float baseline
Definition
EarPressure.h:14
EarPressure::Reset
void Reset()
Definition
EarPressure.h:41
EarPressure::cooldown
float cooldown
Definition
EarPressure.h:16
EarPressure::Update
bool Update(bool active, unsigned int characterId, float pressureMb, int age, int sex, float seconds, PopDiagnostics *diagnostics=nullptr)
Definition
EarPressure.h:53
EarPressure::muffle
float muffle
Definition
EarPressure.h:17
EarPressure::Random
float Random(float low, float high)
Definition
EarPressure.h:23
EarPressure::initialized
bool initialized
Definition
EarPressure.h:11
EarPressure::muffleReleaseStart
float muffleReleaseStart
Definition
EarPressure.h:21
EarPressure::EarPressure
EarPressure(std::uint32_t seed=2098u)
Definition
EarPressure.h:39
EarPressure::randomState
std::uint32_t randomState
Definition
EarPressure.h:10
EarPressure::Threshold
float Threshold() const
Definition
EarPressure.h:50
EarPressure::threshold
float threshold
Definition
EarPressure.h:15
EarPressure::secondsSincePop
double secondsSincePop
Definition
EarPressure.h:18
EarPressure::PopDiagnostics
Definition
EarPressure.h:31
EarPressure::PopDiagnostics::thresholdMb
float thresholdMb
Definition
EarPressure.h:35
EarPressure::PopDiagnostics::differentialMb
float differentialMb
Definition
EarPressure.h:34
EarPressure::PopDiagnostics::middlePressureMb
float middlePressureMb
Definition
EarPressure.h:33
EarPressure::PopDiagnostics::secondsSincePreviousPop
double secondsSincePreviousPop
Definition
EarPressure.h:36
EarPressure::PopDiagnostics::pressureMb
float pressureMb
Definition
EarPressure.h:32
EarPressure.h
Generated on
for Rise by
1.18.0