1#include "../Viewscreen/LandingLights.h"
2#include <fstream>
3#include <iostream>
4#include <limits>
5#include <stdexcept>
6#include <vector>
7#include "LandingLightFrameFixture.h"
8
9namespace
10{
11 struct Effect
12 {
15 float power = 2;
16 };
17 void Check(bool condition, const char* message)
18 {
19 if (!condition) throw std::runtime_error(message);
20 }
21 template<class T> T Read(std::ifstream& file)
22 {
23 T value{};
24 file.read(reinterpret_cast<char*>(&value), sizeof(value));
25 Check(bool(file), "truncated model");
26 return value;
27 }
28 std::vector<Effect> Model(const char* name, bool overlay)
29 {
30 std::ifstream file(name, std::ios::binary);
31 Check(bool(file), "model open");
32 const auto vertices = Read<uint16_t>(file);
33 file.seekg(2 + vertices * (overlay ? 40 : 32));
34 const auto count = Read<uint16_t>(file);
35 std::vector<Effect> effects;
36 for (unsigned i = 0; i < count; ++i)
37 {
38 Effect effect;
39 effect.position = Read<RiseMath::Vector3>(file);
40 effect.normal = Read<RiseMath::Vector3>(file);
42 Read<float>(file); Read<float>(file);
43 effect.power = Read<float>(file);
44 Read<int16_t>(file);
45 effects.push_back(effect);
46 }
47 return effects;
48 }
49 void CarOpticsCheck(const char* name, unsigned char type, float modelYaw)
50 {
51 std::array<LandingLights::Light,2> lights;
56 const auto base=pitch*yaw;
57 float largestAimError=0;
58 for (auto& light:lights)
59 {
60 const auto origin=light.origin;
62 Check(light.origin==origin,"beam configuration retains the authored lamp position");
63 }
64 for (float heading:{0.f,.7f,1.57f,3.14f})
65 {
68
69
74 for (const auto& source:lights)
75 {
77 const auto difference=lamp.direction-legacyAim;
79
80
89 <<", road "<<roadLight<<", above cutoff "<<skyLight<<'\n';
91 Check(roadLight>.5f,"car low beams illuminate road 20 m ahead");
92 Check(skyLight==0,"car low-beam cutoff removes light above the lamps");
93 Check(
RiseMath::Vec3Dot(&lamp.up,&physicalUp)>.99999f,
"beam up follows physical vehicle up");
94 for(float metres:{1.f,2.f,4.f})
95 {
100 Check(fill>(type==
VehicleType::C2 ? .04f : .02f) && fill<.3f,
"car has dim foreground spill 1-4 m ahead");
101 Check(fill<roadLight*.2f,"foreground spill stays weaker than the main beam");
102 }
107 }
108 }
110 std::cout<<"PASS: "<<name<<" old/new aim difference <= "<<largestAimError
111 <<"; mounted origin (m) "<<mounted.origin.x*1000<<", "<<mounted.origin.y*1000<<", "<<mounted.origin.z*1000
112 <<"; old centered origin (m) 0, 0.09, -1.472\n";
113 }
114 void FrameCheck(const char* name, bool overlay, float modelYaw, float spacing, unsigned char type)
115 {
116 const auto effects=Model(name,overlay);
123 ship.matrixBase=pitch*yaw;
129 Check(a.origin.x*b.origin.x<0 && std::abs(std::abs(a.origin.x-b.origin.x)-spacing)<1e-6f,"car lamps have authored lateral spacing");
130 Check(a.origin.z<-.0014f && b.origin.z<-.0014f,"car lamps are at the nose");
131 LampMaterialFixture material;LampGameFixture game;LampWeatherFixture
weather;
132 game.graphics->g_pEffect=&material;
134 for (float heading:{0.f,.7f,1.57f,3.14f})
135 {
137 BuildExteriorPoseFixture();
138
139 ship.matrixWorld=ship.matrixInvOrientation;
142 for(unsigned i=0;i<2;++i)
143 {
144 const auto authored=i==0 ? a : b;
147
148
153 const auto delta=actual.origin-expectedOrigin;
155 Check(
RiseMath::Vec3Dot(&actual.direction,&expectedAim)>.99999f,
"lamp aim matches exterior after physics update");
156 const auto& uploaded=material.vectors.at("LandingLightDirection")[i];
157 Check(std::abs(uploaded.x-expectedAim.x)+std::abs(uploaded.y-expectedAim.y)+std::abs(uploaded.z-expectedAim.z)<1e-5f,"shader receives transformed aim for each lamp");
158 const auto& position=material.vectors.at("LandingLightPosition")[i];
159 Check(std::abs(position.x-expectedOrigin.x)+std::abs(position.y-expectedOrigin.y)+std::abs(position.z-expectedOrigin.z)<1e-7f,"shader receives separate lamp origins");
160 const auto& up=material.vectors.at("LandingLightUp")[i];
161 Check(std::abs(up.x-expectedUp.x)+std::abs(up.y-expectedUp.y)+std::abs(up.z-expectedUp.z)<1e-5f,"shader receives vehicle-aligned cutoff axis");
163 Check(Rendering::lampDevice.enabled[slot],"both fixed-function lamp slots enabled");
164 const auto& fixed=Rendering::lampDevice.lights[slot];
165 Check(
RiseMath::Vec3Dot(&fixed.Direction,&expectedAim)>.99999f,
"fixed-function uses same exterior aim");
166 Check(
RiseMath::Vec3Dot(&fixed.BeamUp,&expectedUp)>.99999f,
"fixed-function uses same cutoff axis");
167 }
168 }
170 Check(material.count==1 && !Rendering::lampDevice.enabled[3],"one-lamp update disables second slot");
172 Check(material.count==0 && !Rendering::lampDevice.enabled[1],"switching lamps off clears renderer count");
173 }
174 void ModelCheck(const char* name, bool overlay, float yaw, bool angled)
175 {
176 const auto effects = Model(name, overlay);
177 std::array<LandingLights::Light, 2> lights;
182 const auto basis = pitch * turn;
185 Check(left.origin.x * right.origin.x < 0, "lamps remain on opposite sides");
186 for (const auto& light : {left, right})
187 {
188 Check(light.origin.z < -.015f && light.origin.z > -.016f, "lamp stays at model nose");
189 Check(light.origin.y > .0002f && light.origin.y < .0003f, "model height conversion");
190 Check(light.direction.z < -.999f, "beam points forward after model axis conversion");
192 if (angled) Check(std::abs(std::abs(light.direction.x) - .009f) < 1e-5f, "E-11 authored aim preserved");
193 }
199 Check(
RiseMath::Vec3Dot(&rotated.direction, &moved.direction) > .99999f,
"translation cannot change aim");
202 }
203 void ConditionCheck()
204 {
206 for(int health : {-1,0,63,95,127})
207 {
208 const float expected=health<=0 ? 0.f : health==63 ? .5f : health==95 ? .75f : 1.f;
209 for(double seconds : {0.,1.,1000.})
210 Check(state.
Sample(health,seconds,123)==expected,
"healthy lamps dim steadily; absent/dead lamps stay off");
211 }
212
213
215 unsigned independent=0;
216 for(unsigned frame=0;frame<72000;++frame)
217 {
218 const double time=frame/120.0;
219 const float value=fast.
Sample(31,time,123);
220 if(frame%4==0) Check(value==slow.
Sample(31,time,123),
"flicker is independent of frame rate");
221 if(value!=other.
Sample(31,time,456)) ++independent;
222 }
223 Check(independent>1000,"two lamps have independent flicker timing");
224 for(int health : {1,31,62})
225 {
227 unsigned lit=0,edges=0; float previous=-1;
228 for(unsigned sample=0;sample<120000;++sample)
229 {
230 const float value=lamp.
Sample(health,sample*.01,321);
231 Check(value==0 || value==.5f,"damaged contact alternates between off and half brightness");
232 if(value>0) ++lit;
233 if(previous>=0 && value!=previous) ++edges;
234 previous=value;
235 }
236 const float duty=lit/120000.f;
237 if(health==1) Check(duty>.04f && duty<.09f && edges>500,"health 1 flashes briefly every few seconds");
238 if(health==62) Check(duty>.91f && duty<.96f && edges>500,"health 62 visibly drops out every few seconds");
239 if(health==31) Check(duty>.3f && duty<.7f,"middle damage has substantial on and off periods");
240 std::cout<<"PASS: landing health "<<health<<" lit "<<duty*100<<"% over 20 minutes, "<<edges<<" transitions\n";
241 }
242 Check(state.
Sample(1,1001,123)==0,
"severely damaged lamp starts off");
243 Check(state.
Sample(127,1002,123)==1,
"repair restores full brightness immediately");
244 Check(state.
Sample(0,1003,123)==0,
"failure removes light immediately");
245 for (unsigned seed=1;seed<=64;++seed)
246 {
248 bool dark=false,lit=false;
249 for (unsigned frame=0;frame<300;++frame)
250 {
251 const float brightness=lamp.
Sample(59,
double(frame)/60,seed);
252 dark|=brightness==0;lit|=brightness==.5f;
253 }
254 Check(dark && lit,"the latest trace's health 59 has a visible interruption within five seconds");
255 }
257 const int peakHealth=single.
Apply(127,0,14,0);
258 for(unsigned fps : {30u,60u,144u})
259 {
261 int health=127;
262 for(unsigned frame=0;frame<fps*2;++frame)
263 health=contact.
Apply(health,0,frame<fps?7.f:14.f,
double(frame)/fps);
264 Check(health==peakHealth,"contact charges only peak shock at 30/60/144 FPS");
265 Check(contact.
Apply(health,0,14,3)<health,
"separate impact causes further damage");
266 }
267 }
268#include "LandingLightCollisionChecks.h"
269 void DamageFrameCheck()
270 {
278 LampMaterialFixture material;LampGameFixture game;LampWeatherFixture
weather;
279 game.graphics->g_pEffect=&material;
284 Check(material.count==0 && lampLogger.errors.empty(),"pending vdat cannot light lamps or report false mismatches");
289 Check(material.count==1 && Rendering::lampDevice.enabled[1] && !Rendering::lampDevice.enabled[3],"dead first lamp leaves only the second beam active");
290 Check(view.
landingLights[0].origin.x==.001f,
"compacting live beams preserves the original vdat slot");
291 Check(material.vectors.at("LandingLightColor")[0].x==.5f && Rendering::lampDevice.lights[1].Diffuse.r==.5f,"shader and fixed lighting receive half brightness");
293 const auto& emitters=material.vectors.at("HeadlightEmission");
294 Check(emitters[0].w==0 && emitters[1].w==.5f && emitters[0].x<0 && emitters[1].x>0,
295 "lens material retains dead emitter locations and shares the beam brightness");
297 Check(lampLogger.errors.empty(),"installed but dead lamps are not model mismatches");
300 Check(lampLogger.errors.size()==1 && lampLogger.errors[0].find("landing0=-1")!=std::string::npos,"absent vdat lamp with a model beam reports one descriptive error");
304 Check(lampLogger.errors.size()==2,"installed second lamp without a model beam also reports a mismatch");
307 Check(material.count==1 && lampLogger.errors.size()==2,"one installed lamp plus one absent slot is valid");
309 bool sawOn=false,sawOff=false;
310 for(unsigned tick=0;tick<600;++tick)
311 {
314 Check(material.count==int(lit) && Rendering::lampDevice.enabled[1]==lit,"flicker changes the shader count and fixed light together");
316 "lens, sprite and beam receive the same flicker sample");
317 if(lit)
318 {
319 sawOn=true;
320 Check(view.
landingLights[0].color.r==.5f,
"shared rain/cloud/fallback light receives flicker brightness");
321 }
322 else sawOff=true;
323 }
324 Check(sawOn && sawOff,"production frame update exercises both phases of a failing lamp");
328 "simulator retains legacy damaged-lamp timing");
331 "leaving simulator resets the long pending interval to real-world flicker");
334 "entering simulator clears pending real-world flicker");
335 ship.isSimulator=false;
338 Check(material.count==1 && material.vectors.at("LandingLightColor")[0].x==1,"repair restores the rendered beam immediately");
341 Check(material.vectors.at("HeadlightEmission")[0].w==1,
342 "mesh damage state remains available to the separate parked-alarm light switch");
344 Check(material.count==0 && lampLogger.errors.size()==2,"vehicle switch cannot reuse stale lamp damage");
345 std::cout<<"PASS: damage upload, independent slots, sprite mapping, readiness, mismatch deduplication and switches\n";
346 }
347}
349{
350 try
351 {
353 ModelCheck(
"Meshes/E-11.daw",
true, -
RiseMath::Pi,
true);
358 ConditionCheck();
359 CrashTimingChecks();
360 StructuralCrushChecks();
361 LongFrameStructuralChecks();
362 ContactGeometryChecks();
363 DamageFrameCheck();
365 CollisionCheck(
"Meshes/C-2A Dasher.daw",
true,-
RiseMath::Pi);
368 std::array<LandingLights::Light, 2> lights;
369 std::vector<Effect> effects(1);
370 effects[0].normal = {0, 0, -4};
372 Check(lights[0].direction.z == -1, "normalize imported aim");
373 effects[0].normal = {};
375 effects[0].normal = {0, 0, -1};
376 effects[0].position.x = std::numeric_limits<float>::quiet_NaN();
378 auto truck = Model("Meshes/T-120 Steerhead-stock.daw", true);
380 Check(lights[0].origin.y * lights[1].origin.y < 0, "four-lamp model keeps both sides");
381 effects.clear();
383 Check(lights[0].origin ==
RiseMath::Vector3(),
"switching clears previous origins");
387 {
388 Check(std::abs(2*
IlluminanceLux(item.first,item.second)-5) < .001f,
"two-lamp 5 lux throw");
389 Check(std::abs(
IlluminanceLux(item.first,item.second/std::sqrt(2.f))-5) < .001f,
"one-lamp sqrt(2) throw");
390 Check(
IlluminanceLux(item.first,item.first.rangeKm)==0,
"range edge fades to zero");
392 }
393 Check(
Angular(
RoadLow,0,0,.05f)==0,
"road light cannot illuminate above cutoff");
394 Check(
Angular(
RoadLow,0,-.02f,.05f)>.5f,
"road light illuminates below cutoff");
395 Check(
Angular(
RoadLow,.2f,-.02f,.05f)>0,
"road light has a broad horizontal spread");
396 Check(
Angular(
RoadLow,0,-.2f,.05f)>.01f &&
Angular(
RoadLow,0,-.2f,.05f)<.03f,
"road light has weak downward spill beyond its shallow core");
397 Check(
Angular(
RoadLow,0,-1.f,.05f)==0,
"road spill remains bounded below the lamp");
399 Check(
Angular(
Train,.25f,0,.05f)<.005f,
"train spill is weak outside its core");
401 float previousWidth=0;
402 for(float distance : {.002f,.01f,.05f})
403 {
404 float tenth=-1,ninetieth=-1;
405 for(unsigned mm=0;mm<=1000;++mm)
406 {
409 if(tenth<0 && fraction>=.1f) tenth=float(mm);
410 if(fraction>=.9f) { ninetieth=float(mm);break; }
411 }
412 const float width=ninetieth-tenth;
413 Check(tenth>=0 && ninetieth>tenth && width>35,"near wall cutoff has a measurable soft transition");
414 Check(width>previousWidth,"cutoff feather grows in physical width with distance");
415 previousWidth=width;
416 std::cout<<"PASS: cutoff 10-90% transition at "<<distance*1000<<" m spans "<<width<<" mm\n";
417 }
426 Check(std::abs(
Intensity(road,below)-
Intensity(rolled,rotatedBelow))<1e-5f,
"cutoff rotates with vehicle attitude");
427 Check(
Intensity(road,{0,0,.05f})==0,
"nothing behind the lamp is lit");
429 Check(
Intensity(foggy,below)<
Intensity(road,below)*.2f,
"fog attenuates light along the lamp path");
431 Check(std::abs(
RiseMath::Vec3Length(&verticalUp)-1)<1e-6f && verticalUp.y==0,
"vertical lamp gets a finite orthogonal up axis");
432 struct FogVertex {float x,y,z;uint32_t diffuse;};
433 std::array<FogVertex,FogVertexCount> fog{};
435 for (const auto& vertex:fog)
436 {
437 Check(std::isfinite(vertex.x) && vertex.z<0,"finite forward fog mesh");
438 Check(vertex.y<=-vertex.z*road.
profile.
cutoffSlope+1e-7f,
"fog mesh respects low-beam cutoff");
439 if (std::abs(vertex.z+road.
profile.
rangeKm)<1e-6f) Check((vertex.diffuse>>24)==0,
"fog beam fades at profile range");
440 }
441 std::cout << "PASS: vehicle profiles, photometric throw, low-beam cutoff, roll, fog attenuation and exterior mesh\n";
442 std::cout << "PASS: E-10/E-11 origins and aims, world transforms, 0/1/2 lamps, invalid data and four-lamp selection\n";
443 std::cout << "PASS: C-2/C-2A production frame update retains both exterior lamp poses after physics overwrites matrixWorld\n";
444 return 0;
445 }
446 catch (const std::exception& error) { std::cerr << "FAIL: " << error.what() << '\n'; return 1; }
447}
unsigned activeLandingLightCount
std::array< LandingLights::Light, LandingLights::MaxLights > landingLights
std::unique_ptr< weather > ptrWeather
void UpdateLandingLights(double seconds)
s_network_objects playerships[MAX_SCAN]
constexpr Profile AircraftLanding
Light Configure(Light light, unsigned char type, const RiseMath::Matrix &modelBase)
unsigned FromModel(const Effects &effects, std::array< Light, MaxLights > &lights)
float IlluminanceLux(const Profile &profile, float distanceKm)
float Intensity(const Light &light, const RiseMath::Vector3 &delta)
Vector OrthonormalUp(Vector up, const Vector &direction)
constexpr Profile RoadLow
constexpr unsigned DeviceSlots[MaxLights]
constexpr Profile CarHigh
float Angular(const Profile &profile, float horizontalSlope, float verticalSlope, float axialDistanceKm)
constexpr Profile TruckDriving
void FogMesh(const Light &light, Vertex *output, bool spill)
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 * Vec3TransformCoord(Vector3 *out, const Vector3 *a, const Matrix *m)
Matrix * MatrixRotationX(Matrix *out, float value)
Quaternion * QuaternionRotationYawPitchRoll(Quaternion *out, float yaw, float pitch, float roll)
Vector3 * Vec3TransformNormal(Vector3 *out, const Vector3 *a, const Matrix *m)
Matrix * MatrixRotationY(Matrix *out, float value)
Matrix * MatrixRotationYawPitchRoll(Matrix *out, float yaw, float pitch, float roll)
Matrix * MatrixIdentity(Matrix *out)
float Vec3Dot(const Vector3 *a, const Vector3 *b)
Matrix * MatrixRotationZ(Matrix *out, float value)
Matrix * MatrixTranslation(Matrix *out, float x, float y, float z)
float Sample(int health, double seconds, uint32_t lampSeed, bool simulator=false)
int Apply(int health, float clearanceKm, float closingMps, double seconds)
int landingDamageVehicleId
std::array< LandingLights::Condition, LandingLights::MaxLights > landingLightCondition
std::array< float, LandingLights::MaxLights > landingLightBrightness
std::array< LandingLights::Light, LandingLights::MaxLights > landingLights
unsigned landingLightCount