Rise
The Vieneo Province
Loading...
Searching...
No Matches
Tests/HeadAccelerationTests.cpp

Source at documentation-generation time. See Test catalog for fixture associations.

1#include "../Viewscreen/HeadAcceleration.h"
2#include <cstdio>
3#include <limits>
4#include <stdexcept>
5
6static void Check(bool value, const char* message)
7{
8 if (!value) throw std::runtime_error(message);
9}
10
11int main()
12{
13 try
14 {
16 Check(!guard.Suppress(.016f, false, 0, 1, 0), "Ordinary gameplay was suppressed");
17 guard.Reset(true);
18 // Replay the captured 3.8 ms height-correction impulse 1.6 s after reset.
19 float counterX = 0, counterY = 0, counterZ = 0;
20 for (int i = 0; i < 100; ++i)
21 Check(guard.Suppress(.016f, false, 0, 1, 0), "Terrain wait ended early");
22 const float x = 33390.7305f, y = 65128.1094f, z = -41766.0156f, dt = .00379849994f;
23 const bool suppressed = guard.Suppress(dt, false, x, y, z);
24 if (!suppressed) { counterX += x * dt * 3; counterY += y * dt * 3; counterZ += z * dt * 3; }
25 Check(suppressed && counterX == 0 && counterY == 0 && counterZ == 0,
26 "Captured delayed reposition impulse entered head compensation");
27 for (int i = 0; i < 400; ++i)
28 Check(guard.Suppress(.016f, false, 0, 1, 0), "Missing terrain was treated as settled");
29 Check(guard.Suppress(.016f, true, x, y, z), "Late terrain impulse escaped the guard");
30 Check(guard.Suppress(.016f, true, std::numeric_limits<float>::infinity(), 1, 0), "Infinite force ended settling");
31 Check(guard.Suppress(.016f, true, std::numeric_limits<float>::quiet_NaN(), 1, 0), "NaN force ended settling");
32 Check(!guard.Suppress(.016f, true, 0, 1, 0) && !guard.Active(), "Settled terrain did not restore normal effects");
33 Check(!guard.Suppress(.016f, true, 25, 1, 0), "Normal post-reposition G effects were disabled");
34 guard.Reset(false);
35 for (int i = 0; i < 4; ++i)
36 Check(guard.Suppress(1, false, 0, 0, 0), "Orbit grace period ended early");
37 Check(!guard.Suppress(1, false, 0, 0, 0), "Orbit reset waited forever for ground terrain");
38 guard.Reset(false);
39 Check(guard.Suppress(60, true, 0, 1, 0), "Loading stall consumed the entire settling period");
40 guard.Reset(false);
41 Check(guard.Suppress(std::numeric_limits<float>::quiet_NaN(), true, 0, 1, 0), "NaN dt ended settling");
42 for (int i = 0; i < 4; ++i) guard.Suppress(1, true, 0, 1, 0);
43 guard.Reset(false);
44 Check(guard.Suppress(1, true, 0, 1, 0), "Second reposition failed to restart protection");
45
46 Check(std::fabs(HeadAcceleration::AdvanceCounter(1.f, 2.f, .016f) - 1.048f) < .000001f,
47 "Normal low-G compensation changed");
48 // Bill's ordinary-play collision/death sequence, with no active reset.
49 const float impulses[][3] = {
50 {-.419023126f, -23.5250378f, -1.15176344f},
51 {-.990465522f, 30.8418293f, -1.19344723f},
52 {-138.504349f, 350.838318f, -815.144592f},
53 {-279.723114f, 708.486877f, -1646.20618f},
54 {-.0603145696f, 35.8415947f, -.00206961948f}
55 };
56 float compensation[3] = {0, 1, 0};
57 float pitch = 0, roll = 0;
58 for (const auto& impulse : impulses)
59 {
60 for (int axis = 0; axis < 3; ++axis)
61 {
62 compensation[axis] = HeadAcceleration::AdvanceCounter(compensation[axis], impulse[axis], .033f);
63 Check(std::isfinite(compensation[axis]) && std::fabs(compensation[axis]) <= 16.f,
64 "Normal-play impulse created excessive head debt");
65 }
68 Check(std::fabs(pitch) <= HeadAcceleration::MaximumPitchRadians && std::fabs(roll) <= HeadAcceleration::MaximumRollRadians,
69 "Collision lean exceeded the head limits");
70 }
71 Check(HeadAcceleration::AdvanceCounter(0, std::numeric_limits<float>::infinity(), .016f) == 0,
72 "Infinite collision force poisoned compensation");
73 Check(HeadAcceleration::AdvanceCounter(std::numeric_limits<float>::quiet_NaN(), 1, .016f) > 0,
74 "Invalid prior compensation did not recover");
75 Check(HeadAcceleration::AdvanceCounter(16, -16, 30) == -16,
76 "Long frame overshot compensation");
77 compensation[0] = -78.321991f; compensation[1] = 137.231033f; compensation[2] = -221.474686f;
78 for (int frame = 0; frame < 600; ++frame)
79 for (auto& axis : compensation) axis = HeadAcceleration::AdvanceCounter(axis, 0, 1.f / 60);
80 for (const auto axis : compensation)
81 Check(std::fabs(axis) < .001f, "Captured head debt lingered after forces settled");
83 "Long frame overshot head recovery");
84 puts("PASS: reposition guarding, Bill's normal-play impacts, bounded compensation and lean, nonfinite inputs, long frames and recovery.");
85 return 0;
86 }
87 catch (const std::exception& error)
88 {
89 fprintf(stderr, "FAIL: %s\n", error.what());
90 return 1;
91 }
92}
void Reset(bool terrainRequired)
bool Suppress(float dt, bool terrainReady, float x, float y, float z)
constexpr float MaximumPitchRadians
float AdvanceCounter(float previous, float force, float dt)
constexpr float MaximumRollRadians
float AdvanceLean(float previous, float target, float dt, float limit)