16 return x > 0 ?
"left" : x < 0 ?
"right" :
"center";
32 const double jitter = .5 + double(
random) / 4294967296.0;
35 float Sample(
int health,
double seconds, uint32_t lampSeed,
bool simulator =
false)
37 health = (std::clamp)(health, -1, 127);
38 if (!std::isfinite(seconds))
return 0;
43 random = lampSeed ? lampSeed : 1;
45 if (health > 0 && health < 63)
51 meanOn = .2 * std::pow(simulator ? 900.0 : 15.0, (health - 1) / 61.0);
57 if (health <= 0)
return 0;
58 if (health >= 63)
return .5f + (health - 63) / 128.f;
59 unsigned transitions = 0;
74 if ((i < modelCount) != (health[i] >= 0)) mask |= 1u << i;
81 inline int ImpactHealth(
int health,
float clearanceKm,
float closingMps,
82 float damageScale = 1,
int damageLimit = 127)
84 if (health <= 0 || !std::isfinite(clearanceKm) || !std::isfinite(closingMps)
85 || clearanceKm > .00015f || closingMps <= .75f)
return health;
86 const float severity = (closingMps - .75f) * (closingMps - .75f) * .5f * damageScale;
87 const int damage =
static_cast<int>((std::min)(float(damageLimit), std::ceil(severity)));
88 return (std::max)(0, health - damage);
95 int Apply(
int health,
float clearanceKm,
float closingMps,
double seconds)
97 if (!std::isfinite(seconds))
return health;
99 const int damage = 127 -
ImpactHealth(127, clearanceKm, closingMps);
100 if (damage == 0 || health <= 0)
return health;
101 if (
lastContact < 0 || seconds < lastContact || seconds - lastContact > .5)
107 const int additionalDamage = (std::max)(0, damage -
appliedDamage);
109 return (std::max)(0, health - additionalDamage);
114 if (health <= 0 || hullLoss <= 0 || !std::isfinite(seconds) ||
lastContact < 0 ||
115 seconds < lastContact || seconds - lastContact > .5)
return health;
123 const int damage = (std::min)(127, (std::min)(127, hullLoss) * 2);
124 const int additionalDamage = (std::max)(0, damage -
appliedDamage);
126 return (std::max)(0, health - additionalDamage);
140 const std::array<Light, MaxLights>& lights,
unsigned count)
142 unsigned nearest = 0;
143 float distance = (std::numeric_limits<float>::max)();
144 for (
unsigned i = 0; i < count && i <
MaxLights; ++i)
146 const auto delta = origin - lights[i].origin;
148 if (candidate < distance) { nearest = i; distance = candidate; }
155 return std::isfinite(origin.x) && std::isfinite(origin.y) && std::isfinite(origin.z)
156 && std::isfinite(length) && length > 1e-12f;
160 template<
class Effects>
161 unsigned FromModel(
const Effects& effects, std::array<Light, MaxLights>& lights)
164 int first = -1, second = -1;
165 float separation = -1;
166 for (
int i = 0; i < static_cast<int>(effects.size()); ++i)
168 const auto& a = effects[i];
169 if (a.power != 2.f || !
Valid(a.position, a.normal))
continue;
170 if (first < 0) first = i;
171 for (
int j = i + 1; j < static_cast<int>(effects.size()); ++j)
173 const auto& b = effects[j];
174 if (b.power != 2.f || !
Valid(b.position, b.normal))
continue;
175 const auto delta = a.position - b.position;
179 if (distance > separation) { first = i; second = j; separation = distance; }
183 for (
const int index : {first, second})
185 if (index < 0)
continue;
186 const auto& effect = effects[index];
187 auto& light = lights[count++];
188 light.origin = effect.position;
190 light.color = effect.diffuse;
196 Light light = source;
205 const std::array<Light, MaxLights>& lights,
unsigned count,
206 const std::array<float, MaxLights>& brightness,
const RiseMath::Matrix& modelToWorld)
208 std::array<RiseMath::Vector4, MaxLights> sources{};
209 for (
unsigned i = 0; i < count && i <
MaxLights; ++i)
215 sources[i] = {origin.x, origin.y, origin.z, brightness[i]};
222 template<
class Vertex>
void FogMesh(
const Light& light, Vertex* output,
bool spill)
225 const float horizontal = spill ? angles.z : angles.x;
226 const float vertical = spill ? angles.w : angles.y;
228 static const auto circle = [] {
229 std::array<RiseMath::Vector2,FogSegments+1> values{};
233 values[i]={std::sin(angle),std::cos(angle)};
237 for (
unsigned ring=0;ring<
FogRings;++ring)
242 uint32_t colors[2]{};
243 for (
unsigned end=0;end<2;++end)
245 const float distance=end ? farDistance : nearDistance;
249 for (
unsigned segment=0;segment<
FogSegments;++segment)
252 for (
unsigned corner=0;corner<4;++corner)
254 const float distance=corner<2 ? nearDistance : farDistance;
255 const auto point=circle[segment+(corner%2)];
256 auto& vertex=corners[corner];
257 vertex.x=point.x*horizontal*distance;
260 vertex.diffuse=colors[corner/2];
262 for (
unsigned corner : {0u,1u,2u,1u,3u,2u}) *output++=corners[corner];
269 if (axial <= 0)
return 0;
unsigned FromModel(const Effects &effects, std::array< Light, MaxLights > &lights)
constexpr unsigned MaxLights
bool Valid(const RiseMath::Vector3 &origin, const RiseMath::Vector3 &direction)
unsigned InstallationMismatch(unsigned modelCount, const char *health)
float IlluminanceLux(const Profile &profile, float distanceKm)
int ImpactHealth(int health, float clearanceKm, float closingMps, float damageScale=1, int damageLimit=127)
float Intensity(const Light &light, const RiseMath::Vector3 &delta)
constexpr unsigned FogVertexCount
Vector OrthonormalUp(Vector up, const Vector &direction)
std::array< RiseMath::Vector4, MaxLights > EmissionSources(const std::array< Light, MaxLights > &lights, unsigned count, const std::array< float, MaxLights > &brightness, const RiseMath::Matrix &modelToWorld)
constexpr unsigned FogRings
constexpr unsigned DeviceSlots[MaxLights]
constexpr unsigned FogSegments
float Angular(const Profile &profile, float horizontalSlope, float verticalSlope, float axialDistanceKm)
void FogMesh(const Light &light, Vertex *output, bool spill)
const char * BodySide(float x)
unsigned NearestLamp(const RiseMath::Vector3 &origin, const std::array< Light, MaxLights > &lights, unsigned count)
Light Transform(const Light &source, const RiseMath::Matrix &modelToWorld)
float Vec3Length(const Vector3 *a)
Vector3 * Vec3Cross(Vector3 *out, const Vector3 *a, const Vector3 *b)
constexpr auto Min(A first, B second)
Vector3 * Vec3TransformCoord(Vector3 *out, const Vector3 *a, const Matrix *m)
float Vec3LengthSq(const Vector3 *a)
Vector3 * Vec3TransformNormal(Vector3 *out, const Vector3 *a, const Matrix *m)
float Vec3Dot(const Vector3 *a, const Vector3 *b)
Vector3 * Vec3Normalize(Vector3 *out, const Vector3 *a)
float Sample(int health, double seconds, uint32_t lampSeed, bool simulator=false)
int Apply(int health, float clearanceKm, float closingMps, double seconds)
int ApplyFrameDamage(int health, int hullLoss, double seconds)
RiseMath::Vector3 direction
PackedVector Angles() const