Rise
The Vieneo Province
Loading...
Searching...
No Matches
LightningChain.h
Go to the documentation of this file.
1#pragma once
2#include "LightningTiming.h"
3#include "LightningStrike.h"
5#include <array>
6
7// One packet describes one connected discharge. Only shared world endpoints,
8// seed and chain count determine its shape; the observer affects sound only.
10{
12 struct Segment
13 {
14 Vector3 start{},end{};
15 uint32_t seed=0;
16 float begin=0,finish=1;
17 bool groundContact=false;
18 float Growth(float progress) const
19 {return (std::clamp)((progress-begin)/(finish-begin),0.f,1.f);}
20 Vector3 SoundSource(Vector3 observer) const
21 {
22 if(groundContact)return end;
23 const auto axis=end-start,delta=observer-start;
24 const float t=(std::clamp)(RiseMath::Vec3Dot(&delta,&axis)/RiseMath::Vec3LengthSq(&axis),0.f,1.f);
25 return start+axis*t;
26 }
27 };
28 struct Path
29 {
30 std::array<Segment,LightningTiming::MaxChainedBolts+1> segments{};
31 int count=0;
32 };
34 {
36 Vector3 groundpos{},cloudpos{}; // Active section's end/start for scene lighting.
39 double Duration() const {return boltDuration*path.count;}
40 double SegmentStart(int index) const {return boltDuration*index;}
41 void Activate(int index)
42 {
43 const auto packetSeed=seed;
45 seed=packetSeed;activeSegment=index;
46 groundpos=path.segments[index].end;cloudpos=path.segments[index].start;
47 }
48 void Start(uint32_t packetSeed,const Path& geometry)
49 {
50 *this={};
51 if(geometry.count<=0 || geometry.count>int(geometry.segments.size()))return;
52 path=geometry;
55 Activate(0);
56 }
57 void Update(double elapsed)
58 {
59 if(!inuse || !std::isfinite(elapsed) || elapsed<=0)return;
60 const double remaining=Duration()-sequenceAge;
61 sequenceAge=elapsed>=remaining ? Duration() : sequenceAge+elapsed;
62 const int index=(std::min)(int(sequenceAge/boltDuration),path.count-1);
63 if(index!=activeSegment)Activate(index);
65 {age=boltDuration;growth=1;flashBrightness=0;inuse=false;return;}
66 // Evaluate the current section at absolute sequence time, so frame
67 // stalls skip elapsed sections instead of replaying them later.
70 inuse=true; // The chain owns lifetime, including rounded section-end tails.
71 }
72 };
73 inline Path Build(Vector3 ground,Vector3 cloud,uint32_t seed,int chains,float cloudTopRadius)
74 {
75 using namespace ProceduralLightning;
76 Path result;
77 const float baseRadius=Length(cloud),groundRadius=Length(ground);
78 if(!Finite(ground) || !Finite(cloud) || groundRadius<1 || baseRadius<=groundRadius
79 || Length(cloud-ground)>40 || !std::isfinite(cloudTopRadius) || cloudTopRadius<baseRadius)return result;
80 chains=(std::clamp)(chains,0,LightningTiming::MaxChainedBolts);
81 Random random{seed^0x6c8e9cf5u};
82 const auto up=Unit(cloud);
83 const auto east=Unit(Cross(std::abs(up.y)<.9f ? Vector3(0,1,0) : Vector3(1,0,0),up));
84 const auto north=Cross(up,east);
85 float heading=random.Next()*6.283185307f;
86 std::array<Vector3,LightningTiming::MaxChainedBolts+1> nodes{};
87 nodes[chains]=cloud;
88 // Work backwards from the packet's ground column. No additional terrain
89 // lookups, camera vectors, local weather overrides or gameplay RNG.
90 for(int i=chains-1;i>=0;--i)
91 {
93 const float span=LightningTiming::CloudHopMinKm
95 const auto direction=east*std::cos(heading)+north*std::sin(heading);
96 const float rise=random.Next()*(std::min)(LightningTiming::CloudHopMaxRiseKm,(cloudTopRadius-baseRadius)*.5f);
97 nodes[i]=Unit(nodes[i+1]+direction*span)*(baseRadius+rise);
98 }
99 result.count=chains+1;
100 float total=0;
101 for(int i=0;i<result.count;++i)
102 {
103 auto& segment=result.segments[i];
104 segment.start=nodes[i];segment.end=i<chains ? nodes[i+1] : ground;
105 segment.seed=i==chains ? seed : seed+0x9e3779b9u*uint32_t(i+1);
106 segment.groundContact=i==chains;
107 segment.begin=total;total+=Length(segment.end-segment.start);segment.finish=total;
108 }
109 for(int i=0;i<result.count;++i)
110 {result.segments[i].begin/=total;result.segments[i].finish/=total;}
111 return result;
112 }
113}
Path Build(Vector3 ground, Vector3 cloud, uint32_t seed, int chains, float cloudTopRadius)
constexpr float CloudHopMaxRiseKm
constexpr int MaxChainedBolts
constexpr float CloudHopMinKm
constexpr float CloudHopMaxKm
constexpr float CloudHopTurnRadians
Vector3 Unit(Vector3 v)
float Length(Vector3 v)
Vector3 Cross(Vector3 a, Vector3 b)
float Vec3LengthSq(const Vector3 *a)
Definition ClientMath.h:94
float Vec3Dot(const Vector3 *a, const Vector3 *b)
Definition ClientMath.h:93
double SegmentStart(int index) const
void Update(double elapsed)
void Start(uint32_t packetSeed, const Path &geometry)
std::array< Segment, LightningTiming::MaxChainedBolts+1 > segments
Vector3 SoundSource(Vector3 observer) const
float Growth(float progress) const
void Start(uint32_t strikeSeed, double durationSeconds=0)
void Update(double elapsed)