Rise
The Vieneo Province
Loading...
Searching...
No Matches
GridLightning.h
Go to the documentation of this file.
1#pragma once
3#include <algorithm>
4#include <cmath>
5#include <cstdint>
6#include <random>
7#include <vector>
8
9// Below-cloud strikes share one regional budget. Grid size is never an event-rate multiplier.
11{
12 constexpr float PrecipitationThreshold = 6.f, RadiusKm = 64.f;
13 constexpr float CooldownRadiusKm = 6.f, CooldownSeconds = 8.f;
14 constexpr double DistributionInterval = .25;
15 // Full-coverage level-16 rain: 60 strike events/minute before modifiers.
16 constexpr double MaximumRate = 1.;
18 struct Cell
19 {
20 short u = 0, v = 0;
22 float precipitation = 2, area = 1;
23 };
24 struct Event { Cell cell; int seed = 0, chains = 0; };
25 inline bool Before(const Cell& a, const Cell& b)
26 { return a.u < b.u || (a.u == b.u && a.v < b.v); }
27 inline double Strength(float precipitation)
28 {
29 if (!std::isfinite(precipitation) || precipitation <= PrecipitationThreshold) return 0;
30 const double p = (std::min)(16.f, precipitation) - PrecipitationThreshold;
31 return p * p;
32 }
33 inline float Distance(const Vector3& a, const Vector3& b)
34 { const auto delta=a-b; return RiseMath::Vec3Length(&delta); }
35 class System
36 {
37 struct Discharge { Vector3 position{}; double until = 0; };
38 std::vector<Cell> cells;
39 std::vector<double> cumulative;
40 std::vector<Discharge> recent;
41 std::mt19937 random;
42 double time = 0, rebuildAt = 0, remaining = 1, mass = 0, baseRate = 0, rate = 0;
43 uint64_t rebuilds = 0;
44 double Uniform() { return (double(random()) + .5) / 4294967296.; }
45 double Interval() { return -std::log(Uniform()); }
46 void Rebuild(const Vector3& observer)
47 {
48 // Retain every active cooldown: rapid bursts can exceed eight strikes.
49 recent.erase(std::remove_if(recent.begin(),recent.end(),
50 [this](const Discharge& discharge) { return discharge.until<=time; }),recent.end());
51 cumulative.resize(cells.size());
52 double area = 0, wetArea = 0, intensity = 0, rawMass = 0;
53 mass = 0;
54 for (size_t i = 0; i < cells.size(); ++i)
55 {
56 const auto& cell = cells[i];
57 const auto offset = cell.ground - observer;
59 {
60 const float p = cell.precipitation;
61 const double weight = cell.area * Strength(p);
62 area += cell.area;
63 if (weight > 0)
64 {
65 wetArea += cell.area;
66 const double scaled = (std::min)(16.f,p) - 2;
67 intensity += cell.area * scaled * scaled / 196.;
68 rawMass += weight;
69 double recovery = 1;
70 for (const auto& discharge : recent) if (discharge.until > time)
71 {
72 const auto delta = cell.ground - discharge.position;
74 recovery = 0; // short, local refractory interval
75 }
76 mass += weight * recovery;
77 }
78 }
79 cumulative[i] = mass;
80 }
81 // Precipitation scales the regional maximum. sqrt(coverage) makes a compact
82 // approaching storm visible without summing thousands of sector clocks.
83 baseRate = area > 0 && wetArea > 0 && rawMass > 0
84 ? MaximumRate * (intensity / wetArea) * std::sqrt(wetArea / area) * (mass / rawMass) : 0;
86 ++rebuilds;
87 }
88 public:
89 explicit System(uint32_t seed = 0x42434c57u) : random(seed) { remaining = Interval(); }
90 void Seed(uint32_t seed) { random.seed(seed); Reset(); }
91 void Reset()
92 {
93 cells.clear(); cumulative.clear(); recent.clear();
95 }
96 void Pause()
97 {
99 }
100 void SetCells(std::vector<Cell> next)
101 {
102 next.erase(std::remove_if(next.begin(),next.end(),[](const Cell& cell) {
103 return !std::isfinite(cell.area) || cell.area <= 0
104 || !std::isfinite(cell.precipitation) || cell.precipitation < 2 || cell.precipitation > 16
105 || !std::isfinite(cell.ground.x) || !std::isfinite(cell.ground.y) || !std::isfinite(cell.ground.z)
106 || !std::isfinite(cell.cloud.x) || !std::isfinite(cell.cloud.y) || !std::isfinite(cell.cloud.z);
107 }),next.end());
108 std::sort(next.begin(),next.end(),Before);
109 next.erase(std::unique(next.begin(),next.end(),[](const Cell& a,const Cell& b) {
110 return a.u == b.u && a.v == b.v;
111 }),next.end());
112 cells = std::move(next); rebuildAt = time;
113 // Ordinary fulls and strips preserve the event clock and geographic cooldowns.
114 }
115 const Cell* Find(short u, short v) const
116 {
117 Cell key; key.u=u; key.v=v;
118 const auto found=std::lower_bound(cells.begin(),cells.end(),key,Before);
119 return found!=cells.end() && found->u==u && found->v==v ? &*found : nullptr;
120 }
121 const Cell* Choose(double fraction) const
122 {
123 if (mass<=0 || !std::isfinite(fraction) || fraction<0 || fraction>=1) return nullptr;
124 const size_t index=size_t(std::upper_bound(cumulative.begin(),cumulative.end(),fraction*mass)-cumulative.begin());
125 return index<cells.size()?&cells[index]:nullptr;
126 }
127 void Observe(const Vector3& ground)
128 {
129 recent.push_back({ground,time+CooldownSeconds});
130 rebuildAt = time;
131 }
132 bool Update(double elapsed, const Vector3& observer, int players, float snow,
133 float turbulence, Event& event)
134 {
135 if (!std::isfinite(elapsed) || elapsed <= 0) return false;
136 if (elapsed > 1) { time += elapsed; Pause(); return false; } // no catch-up burst
137 time += elapsed;
138 if (time >= rebuildAt) Rebuild(observer);
139 const float safeSnow=std::isfinite(snow)?(std::clamp)(snow,0.f,1.f):0.f;
140 rate=baseRate*(1-.9*std::pow(safeSnow,1.5))/(std::max)(1,players);
141 if (mass <= 0 || rate <= 0) return false;
142 remaining -= rate * elapsed;
143 if (remaining > 0) return false;
144 remaining += Interval();
145 const auto* selected=Choose(Uniform());
146 if (!selected) return false;
147 if (Distance(selected->ground,observer)>=RadiusKm) { rebuildAt=time; return false; }
148 event.cell = *selected;
149 event.seed = int(random() & 0x7fff); // exact legacy float wire representation
150 const float activity = ((event.cell.precipitation-2)/14 + (std::clamp)(turbulence,0.f,1.f))*.5f;
151 const double chance = .15 + .3*std::pow(activity,1.5);
152 event.chains = Uniform() < chance ? 1+int(random()%2) : 0;
153 Observe(event.cell.ground);
154 return true;
155 }
156 double Rate() const { return rate; }
157 double Mass() const { return mass; }
158 size_t CellCount() const { return cells.size(); }
159 uint64_t RebuildCount() const { return rebuilds; }
160 };
161}
SCRIPT_CONTROL SCRIPT_STATE const void int
void Rebuild(const Vector3 &observer)
void SetCells(std::vector< Cell > next)
std::vector< Discharge > recent
System(uint32_t seed=0x42434c57u)
void Observe(const Vector3 &ground)
const Cell * Choose(double fraction) const
void Seed(uint32_t seed)
const Cell * Find(short u, short v) const
bool Update(double elapsed, const Vector3 &observer, int players, float snow, float turbulence, Event &event)
size_t CellCount() const
std::vector< Cell > cells
std::vector< double > cumulative
uint64_t RebuildCount() const
char area[3]
Definition globals.cpp:87
bool Before(const Cell &a, const Cell &b)
constexpr float CooldownRadiusKm
constexpr float CooldownSeconds
constexpr float PrecipitationThreshold
constexpr double DistributionInterval
constexpr double MaximumRate
constexpr float RadiusKm
double Strength(float precipitation)
float Distance(const Vector3 &a, const Vector3 &b)
float Vec3Length(const Vector3 *a)
Definition ClientMath.h:95
float Vec3LengthSq(const Vector3 *a)
Definition ClientMath.h:94