Rise
The Vieneo Province
Loading...
Searching...
No Matches
ReentryRadio.h
Go to the documentation of this file.
1#pragma once
2#include <algorithm>
3#include <cmath>
4#include <cstddef>
5#include <cwchar>
6
7namespace ReentryRadio
8{
9 inline float Clamp(float value)
10 {
11 return std::isfinite(value) ? (std::max)(0.f, (std::min)(1.f, value)) : 0.f;
12 }
13
14 // GAMMA is a diagnostic loss fraction, not electron density or an RF cutoff.
15 // Without an override, retain the existing TAT * dynamic-pressure calibration.
16 inline float UpdateBlackout(float thermalLoad, float& gammaOverride, bool simulator)
17 {
18 if (simulator)
19 {
20 gammaOverride = -1.f;
21 return 0.f;
22 }
23 if (std::isfinite(gammaOverride) && gammaOverride >= 0.f) return Clamp(gammaOverride);
24 return Clamp((thermalLoad - 140000.f) / 1400000.f);
25 }
26
27 inline size_t CharacterWidth(const wchar_t* text)
28 {
29 // Keep UTF-16 surrogate pairs together when selecting damaged characters.
30 return text[0] >= 0xd800 && text[0] <= 0xdbff &&
31 text[1] >= 0xdc00 && text[1] <= 0xdfff ? 2u : 1u;
32 }
33
34 template<class Random> void Garble(wchar_t* text, float blackout, Random random)
35 {
36 const float loss = Clamp(blackout);
37 if (!text || loss == 0.f) return;
38 size_t characters = 0;
39 for (const wchar_t* p = text; *p; p += CharacterWidth(p)) ++characters;
40 size_t affected = static_cast<size_t>(static_cast<float>(characters) * loss);
41 for (wchar_t* p = text; *p && affected; --characters)
42 {
43 const size_t width = CharacterWidth(p);
44 // Sample without replacement. Even an RNG endpoint of 1 cannot
45 // write past the terminator or leave characters readable at loss=1.
46 if (affected == characters || static_cast<double>(random()) * characters < affected)
47 {
48 for (size_t unit = 0; unit < width; ++unit) p[unit] = L'?';
49 --affected;
50 }
51 p += width;
52 }
53 }
54}
size_t CharacterWidth(const wchar_t *text)
float Clamp(float value)
Definition ReentryRadio.h:9
float UpdateBlackout(float thermalLoad, float &gammaOverride, bool simulator)
void Garble(wchar_t *text, float blackout, Random random)