Rise
The Vieneo Province
Loading...
Searching...
No Matches
Tests/LightningChainTests.h

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

1#include "../Viewscreen/LightningChain.h"
2static void TestLightningChain()
3{
4 using namespace LightningChain;
5 using namespace ProceduralLightning;
6 for(Vector3 up:{Vector3(1,0,0),Vector3(-1,0,0),Vector3(0,1,0),Vector3(0,-1,0),Vector3(0,0,1),Unit({1,2,3})})
7 for(uint32_t seed=0;seed<256;++seed)for(int chains:{0,1,2})
8 {
9 const auto ground=up*2940.f,cloud=up*2942.f;
10 const auto a=Build(ground,cloud,seed,chains,2949.f),b=Build(ground,cloud,seed,chains,2949.f);
11 Check(a.count==chains+1,"packet chain count mapped incorrectly");
12 Check(a.segments[0].begin==0 && a.segments[chains].finish==1,"growth does not span entire chain");
13 for(int i=0;i<a.count;++i)
14 {
15 const auto& s=a.segments[i];const auto& repeat=b.segments[i];
16 Check(s.start==repeat.start && s.end==repeat.end && s.seed==repeat.seed,"seeded chain is unstable");
17 Check(i==0 || (s.start==a.segments[i-1].end && s.begin==a.segments[i-1].finish),
18 "adjacent sections have a spatial or growth gap");
19 Check(s.groundContact==(i==chains) && (i<chains || (s.start==cloud && s.end==ground)),
20 "chain does not have exactly one ground contact at packet position");
21 Check(Finite(s.start) && Finite(s.end) && s.finish>s.begin,"invalid chain segment");
22 Check(s.Growth(s.begin)==0 && s.Growth(s.finish)==1,"segment growth endpoints wrong");
23 if(i<chains)Check(Length(s.start)>=2942.f-.001f && Length(s.end)>=2942.f-.001f
24 && Length(s.start)<=2942.f+LightningTiming::CloudHopMaxRiseKm+.001f,
25 "cloud hop left its cloud layer");
26 const float midway=(s.begin+s.finish)*.5f;
27 for(int j=0;j<a.count;++j)
28 Check(j<i ? a.segments[j].Growth(midway)==1 : j>i ? a.segments[j].Growth(midway)==0
29 : std::abs(a.segments[j].Growth(midway)-.5f)<.00001f,"leader did not traverse chain in order");
30 if(seed<8)
31 {
32 const auto mesh=Generate(s.start-s.end,s.seed);
33 Check(!mesh.vertices.empty() && mesh.vertices.front().position==s.start-s.end
34 && mesh.vertices[256].position==Vector3(0,0,0),"render mesh detached from chain junction");
35 }
36 }
37 }
38 const Segment cloudHop{{0,10,0},{10,10,0},7,0,1,false};
39 Check(cloudHop.SoundSource({-2,0,0})==Vector3(0,10,0)
40 && cloudHop.SoundSource({12,0,0})==Vector3(10,10,0)
41 && cloudHop.SoundSource({4,0,0})==Vector3(4,10,0),"cloud thunder source is not on the audible channel");
42 Check(Build({0,2940,0},{0,2942,0},1,-1,2949).count==1
43 && Build({0,2940,0},{0,2942,0},1,99,2949).count==3,"untrusted chain count not bounded");
44 Check(Build({}, {0,2942,0},1,2,2949).count==0
45 && Build({0,2940,0},{0,2939,0},1,2,2949).count==0
46 && Build({0,2940,0},{0,2942,0},1,2,2941).count==0,"invalid world endpoints accepted");
47
48 for(uint32_t seed=0;seed<256;++seed)for(int chains:{0,1,2})
49 {
50 const auto path=Build({0,2940,0},{0,2942,0},seed,chains,2949);
51 LightningStrike::Animation single;single.Start(seed);
52 Animation sequence;sequence.Start(seed,path);
53 Check(sequence.Duration()==single.Duration()*(chains+1),"chain duration is not an exact bolt multiple");
54 for(int section=0;section<path.count;++section)
55 {
56 LightningStrike::Animation pulse;pulse.Start(path.segments[section].seed,single.Duration());
57 const double local=pulse.strokes[0].start+pulse.strokes[0].attack+pulse.strokes[0].hold*.5;
58 const double absolute=section*single.Duration()+local;
59 sequence.Update(absolute-sequence.sequenceAge);
60 Check(sequence.inuse && sequence.activeSegment==section && sequence.flashBrightness==1
61 && sequence.growth==1,"section did not receive its own complete flash envelope");
62 Check(sequence.groundpos==path.segments[section].end && sequence.cloudpos==path.segments[section].start,
63 "scene illumination stayed at a different section");
64 Check(sequence.seed==seed && sequence.strokeCount>=3 && sequence.strokeCount<=4,
65 "sequence lost packet identity or approved stroke count");
66 Check(std::abs(sequence.LightningStrike::Animation::Duration()-single.Duration())<1e-12,
67 "section did not preserve the sampled bolt duration");
68 }
69 sequence.Update(sequence.Duration()-sequence.sequenceAge);
70 Check(!sequence.inuse && sequence.flashBrightness==0,"expired chain continued flashing");
71 sequence.Start(seed,path);sequence.Update(10);
72 Check(!sequence.inuse && sequence.sequenceAge==sequence.Duration(),"pause replayed skipped chain sections");
73 sequence.Start(seed,path);sequence.Update(sequence.boltDuration*.7);
74 Check(sequence.activeSegment==0 && sequence.inuse,"first section ended prematurely");
75 sequence.Update(-1);sequence.Update(std::nan(""));
76 Check(sequence.activeSegment==0 && sequence.inuse,"invalid frame time advanced sequence");
77 for(int fps:{30,60,144})
78 {
79 sequence.Start(seed,path);
80 const double step=1./fps;
81 for(int frame=0;frame<fps && sequence.inuse;++frame)
82 {
83 sequence.Update(step);
84 Animation jumped;jumped.Start(seed,path);jumped.Update(sequence.sequenceAge);
85 Check(sequence.activeSegment==jumped.activeSegment && sequence.inuse==jumped.inuse
86 && std::abs(sequence.flashBrightness-jumped.flashBrightness)<1e-5f,
87 "chain playback depends on frame rate");
88 }
89 }
90 }
91}
Path Build(Vector3 ground, Vector3 cloud, uint32_t seed, int chains, float cloudTopRadius)
constexpr float CloudHopMaxRiseKm
Vector3 Unit(Vector3 v)
Mesh Generate(Vector3 cloudOffset, uint32_t seed)
float Length(Vector3 v)
path
Definition erin.py:153
void Update(double elapsed)
void Start(uint32_t packetSeed, const Path &geometry)
Vector3 SoundSource(Vector3 observer) const
void Start(uint32_t strikeSeed, double durationSeconds=0)
std::array< Stroke, Timing::MaxStrokes > strokes