Rise
The Vieneo Province
Loading...
Searching...
No Matches
CollisionDamage.h
Go to the documentation of this file.
1#pragma once
2#include <algorithm>
3#include <cmath>
4
6{
7 // Existing directional crash limits, in the client's acceleration units.
8 // This is gameplay severity, not an atmospheric-density correction.
9 inline float Severity(float x, float y, float z)
10 {
11 if (!std::isfinite(x) || !std::isfinite(y) || !std::isfinite(z)) return 0;
12 return (std::max)({std::fabs(x) / 9.f,
13 y < 0 ? -y / 15.f : y / 20.f,
14 z < 0 ? -z / 22.5f : z / 33.2f});
15 }
16
17 inline float FaultProbability(float severity)
18 {
19 // Preserve 25% at the crash threshold; twice as severe gives 50%,
20 // four times as severe guarantees an eligible cockpit-control upset.
21 return (std::clamp)(severity * .25f, 0.f, 1.f);
22 }
23
24 inline float ImpactSeverity(float x, float y, float z, float seconds)
25 {
26 if (!(seconds > 0) || !std::isfinite(seconds)) return 0;
27 // Express the impulse on the existing 60 Hz severity scale. A solver
28 // stop's acceleration is delta-v/dt; undo dt so FPS cannot change damage.
29 // This reference interval is tuning, not a material crush duration.
30 return Severity(x, y, z) * seconds * 60.f;
31 }
32
33 inline int HullDamage(float severity)
34 {
35 if (!std::isfinite(severity) || severity <= 1) return 0;
36 // Deterministic gameplay curve: 24 health points per severity unit
37 // above the existing threshold. The recorded ~4.6 C-2 corner impacts
38 // remove ~86/127 points; there is no random structural-survival roll.
39 return (std::clamp)(int(std::round((std::min)(severity - 1, 127.f / 24.f) * 24.f)), 1, 127);
40 }
41
42 inline bool TriggersFault(float severity, unsigned roll, unsigned maxRoll)
43 {
44 return double(roll) < double(FaultProbability(severity)) * (double(maxRoll) + 1);
45 }
46
47 inline float HornFaultProbability(float frontalSeverity, int hullBefore, int hullAfter)
48 {
49 // A persistent horn needs a damaged switch/relay/control circuit, not
50 // a momentary occupant jolt. New frontal crush is our available proxy.
51 // This probability is gameplay tuning, not a measured failure rate.
52 if (!std::isfinite(frontalSeverity) || frontalSeverity <= 1 ||
53 hullBefore <= 0 || hullAfter < 0 || hullAfter >= hullBefore) return 0;
54 return FaultProbability(frontalSeverity) * (std::min)(127, hullBefore - hullAfter) / 127.f;
55 }
56
57 inline bool TriggersHornFault(float frontalSeverity, int hullBefore, int hullAfter,
58 unsigned roll, unsigned maxRoll)
59 {
60 return double(roll) < double(HornFaultProbability(frontalSeverity, hullBefore, hullAfter))
61 * (double(maxRoll) + 1);
62 }
63
64 inline int WindshieldAfterFrameDamage(int windshield, int hullBefore, int hullAfter)
65 {
66 // Hull loss is the available proxy for frame distortion, not a glass
67 // material model. Transfer that loss without an independent glass roll.
68 if (windshield <= 0 || hullBefore <= 0 || hullAfter < 0 || hullAfter >= hullBefore)
69 return windshield;
70 return (std::max)(0, windshield - (hullBefore - hullAfter));
71 }
72
73 // A continuing shock is one event, even when the solver reports it at
74 // every render frame. A half-second without a severe sample rearms it,
75 // matching the existing lamp contact grouping.
76 struct Episode
77 {
78 double lastContact = -1;
79 bool Begin(double seconds)
80 {
81 if (!std::isfinite(seconds)) return false;
82 const bool fresh = lastContact < 0 || seconds < lastContact || seconds - lastContact > .5;
83 lastContact = seconds;
84 return fresh;
85 }
86 };
87
89 {
91 bool Begin(double seconds)
92 {
93 const bool fresh = Episode::Begin(seconds);
94 if (fresh) appliedDamage = 0;
95 return fresh;
96 }
97 int Apply(int health, float severity)
98 {
99 if (health <= 0) return health;
100 const int target = HullDamage(severity);
101 const int loss = (std::min)(health, (std::max)(0, target - appliedDamage));
102 appliedDamage += loss;
103 return health - loss;
104 }
105 };
106}
SCRIPT_CONTROL SCRIPT_STATE const void int
float Severity(float x, float y, float z)
int HullDamage(float severity)
bool TriggersHornFault(float frontalSeverity, int hullBefore, int hullAfter, unsigned roll, unsigned maxRoll)
float FaultProbability(float severity)
bool TriggersFault(float severity, unsigned roll, unsigned maxRoll)
float HornFaultProbability(float frontalSeverity, int hullBefore, int hullAfter)
float ImpactSeverity(float x, float y, float z, float seconds)
int WindshieldAfterFrameDamage(int windshield, int hullBefore, int hullAfter)
bool Begin(double seconds)
int Apply(int health, float severity)