1#include "../Viewscreen/LightningChain.h"
2static void TestLightningChain()
3{
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
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};
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");
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);
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 {
58 const double absolute=section*single.Duration()+local;
61 && sequence.
growth==1,
"section did not receive its own complete flash envelope");
63 "scene illumination stayed at a different section");
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 }
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 {
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
Mesh Generate(Vector3 cloudOffset, uint32_t seed)
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