Rise
The Vieneo Province
Toggle main menu visibility
Loading...
Searching...
No Matches
GridLightning.h
Go to the documentation of this file.
1
#pragma once
2
#include "
../Rendering/ClientMath.h
"
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.
10
namespace
GridLightning
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.;
17
using
RiseMath::Vector3
;
18
struct
Cell
19
{
20
short
u
= 0,
v
= 0;
21
Vector3
ground
{},
cloud
{};
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;
58
if
(
RiseMath::Vec3LengthSq
(&offset) <
RadiusKm
*
RadiusKm
)
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
;
73
if
(
RiseMath::Vec3LengthSq
(&delta) <
CooldownRadiusKm
*
CooldownRadiusKm
)
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;
85
rebuildAt
=
time
+
DistributionInterval
;
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();
94
time
=
rebuildAt
=
mass
=
baseRate
=
rate
= 0;
remaining
=
Interval
();
95
}
96
void
Pause
()
97
{
98
rate
=
baseRate
=
mass
= 0;
rebuildAt
=
time
;
remaining
=
Interval
();
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
}
ClientMath.h
int
SCRIPT_CONTROL SCRIPT_STATE const void int
Definition
Controls.cpp:5032
GridLightning::System::Uniform
double Uniform()
Definition
GridLightning.h:44
GridLightning::System::Rebuild
void Rebuild(const Vector3 &observer)
Definition
GridLightning.h:46
GridLightning::System::SetCells
void SetCells(std::vector< Cell > next)
Definition
GridLightning.h:100
GridLightning::System::recent
std::vector< Discharge > recent
Definition
GridLightning.h:40
GridLightning::System::System
System(uint32_t seed=0x42434c57u)
Definition
GridLightning.h:89
GridLightning::System::Observe
void Observe(const Vector3 &ground)
Definition
GridLightning.h:127
GridLightning::System::Choose
const Cell * Choose(double fraction) const
Definition
GridLightning.h:121
GridLightning::System::Seed
void Seed(uint32_t seed)
Definition
GridLightning.h:90
GridLightning::System::Mass
double Mass() const
Definition
GridLightning.h:157
GridLightning::System::Pause
void Pause()
Definition
GridLightning.h:96
GridLightning::System::Find
const Cell * Find(short u, short v) const
Definition
GridLightning.h:115
GridLightning::System::rebuildAt
double rebuildAt
Definition
GridLightning.h:42
GridLightning::System::Update
bool Update(double elapsed, const Vector3 &observer, int players, float snow, float turbulence, Event &event)
Definition
GridLightning.h:132
GridLightning::System::CellCount
size_t CellCount() const
Definition
GridLightning.h:158
GridLightning::System::baseRate
double baseRate
Definition
GridLightning.h:42
GridLightning::System::random
std::mt19937 random
Definition
GridLightning.h:41
GridLightning::System::mass
double mass
Definition
GridLightning.h:42
GridLightning::System::Rate
double Rate() const
Definition
GridLightning.h:156
GridLightning::System::cells
std::vector< Cell > cells
Definition
GridLightning.h:38
GridLightning::System::Reset
void Reset()
Definition
GridLightning.h:91
GridLightning::System::remaining
double remaining
Definition
GridLightning.h:42
GridLightning::System::rate
double rate
Definition
GridLightning.h:42
GridLightning::System::time
double time
Definition
GridLightning.h:42
GridLightning::System::cumulative
std::vector< double > cumulative
Definition
GridLightning.h:39
GridLightning::System::Interval
double Interval()
Definition
GridLightning.h:45
GridLightning::System::RebuildCount
uint64_t RebuildCount() const
Definition
GridLightning.h:159
GridLightning::System::rebuilds
uint64_t rebuilds
Definition
GridLightning.h:43
area
char area[3]
Definition
globals.cpp:87
GridLightning
Definition
GridLightning.h:11
GridLightning::Before
bool Before(const Cell &a, const Cell &b)
Definition
GridLightning.h:25
GridLightning::CooldownRadiusKm
constexpr float CooldownRadiusKm
Definition
GridLightning.h:13
GridLightning::CooldownSeconds
constexpr float CooldownSeconds
Definition
GridLightning.h:13
GridLightning::PrecipitationThreshold
constexpr float PrecipitationThreshold
Definition
GridLightning.h:12
GridLightning::DistributionInterval
constexpr double DistributionInterval
Definition
GridLightning.h:14
GridLightning::MaximumRate
constexpr double MaximumRate
Definition
GridLightning.h:16
GridLightning::RadiusKm
constexpr float RadiusKm
Definition
GridLightning.h:12
GridLightning::Strength
double Strength(float precipitation)
Definition
GridLightning.h:27
GridLightning::Distance
float Distance(const Vector3 &a, const Vector3 &b)
Definition
GridLightning.h:33
RiseMath::Vec3Length
float Vec3Length(const Vector3 *a)
Definition
ClientMath.h:95
RiseMath::Vec3LengthSq
float Vec3LengthSq(const Vector3 *a)
Definition
ClientMath.h:94
GridLightning::Cell
Definition
GridLightning.h:19
GridLightning::Cell::ground
Vector3 ground
Definition
GridLightning.h:21
GridLightning::Cell::v
short v
Definition
GridLightning.h:20
GridLightning::Cell::u
short u
Definition
GridLightning.h:20
GridLightning::Cell::precipitation
float precipitation
Definition
GridLightning.h:22
GridLightning::Cell::cloud
Vector3 cloud
Definition
GridLightning.h:21
GridLightning::Cell::area
float area
Definition
GridLightning.h:22
GridLightning::Event
Definition
GridLightning.h:24
GridLightning::Event::chains
int chains
Definition
GridLightning.h:24
GridLightning::Event::seed
int seed
Definition
GridLightning.h:24
GridLightning::Event::cell
Cell cell
Definition
GridLightning.h:24
GridLightning::System::Discharge
Definition
GridLightning.h:37
GridLightning::System::Discharge::until
double until
Definition
GridLightning.h:37
GridLightning::System::Discharge::position
Vector3 position
Definition
GridLightning.h:37
RiseMath::Vector3
Definition
ClientMath.h:38
Viewscreen
GridLightning.h
Generated on
for Rise by
1.18.0