Rise
The Vieneo Province
Toggle main menu visibility
Loading...
Searching...
No Matches
TerrainSnow.h
Go to the documentation of this file.
1
#pragma once
2
#include "
../Protocol/TerrainWeather.h
"
3
#include <unordered_map>
4
#include <algorithm>
5
6
// Values remain attached to canonical plot coordinates across independent grids.
7
// Terrain raster colors are never mutated by weather.
8
namespace
TerrainSnow
9
{
10
inline
uint32_t
Key
(
VECTOR2SHORT
p) {
return
uint16_t(p.
u
) | (uint32_t(uint16_t(p.
v
)) << 16); }
11
struct
Surface
{
float
depthMm
=0,
ageHours
=0,
waterMm
=0; };
12
inline
RiseMath::Vector4
Tint
(
const
Surface
& surface,
float
latitude,
float
sunLatitude)
13
{
14
const
float
polar=std::clamp((fabsf(latitude)-fabsf(sunLatitude)-45.f)/20.f,0.f,1.f);
15
const
float
cover=(std::max)(std::clamp(surface.
depthMm
/40.f,0.f,1.f),polar);
16
const
float
age=std::clamp(surface.
ageHours
/240.f,0.f,1.f);
17
return
{(232.f-32.f*age)*cover/255.f,(239.f-34.f*age)*cover/255.f,
18
(250.f-35.f*age)*cover/255.f,cover};
19
}
20
class
Store
21
{
22
struct
Cell
{
Surface
value
,
target
; uint64_t
touched
=0; };
23
std::unordered_map<uint32_t,Cell>
cells
;
24
uint64_t
generation
=0;
25
public
:
26
void
Reset
(){
cells
.clear();
generation
=0;}
27
size_t
Size
()
const
{
return
cells
.size();}
28
void
Begin
(){++
generation
;}
29
void
Retain
(
VECTOR2SHORT
p){
auto
it=
cells
.find(
Key
(p));
if
(it!=
cells
.end())it->second.touched=
generation
;}
30
void
Target
(
VECTOR2SHORT
p,
const
SRenderWeather
& sample)
31
{
32
if
(!sample.
HasVisibility
())
return
;
33
const
Surface
target{float(sample.
snowDepthMm
),float(sample.
snowAgeHours
),sample.
surfaceWaterTenthsMm
*.1f};
34
auto
result=
cells
.try_emplace(
Key
(p),
Cell
{target,target,
generation
});
35
result.first->second.target=target;result.first->second.touched=
generation
;
36
}
37
// Call after retaining current weather AND terrain footprints: <=8192 entries.
38
void
End
(){
for
(
auto
i=
cells
.begin();i!=
cells
.end();)
if
(i->second.touched!=
generation
)i=
cells
.erase(i);
else
++i;}
39
void
Advance
(
float
elapsed)
40
{
41
if
(!std::isfinite(elapsed)||elapsed<=0)
return
;
42
const
float
blend=1.f-expf(-elapsed/3.f);
43
auto
approach=[blend](
float
& current,
float
target){
44
current+=(target-current)*blend;
if
(fabsf(target-current)<.001f)current=target;
45
};
46
for
(
auto
& entry:
cells
){
47
auto
& c=entry.second;
48
approach(c.value.depthMm,c.target.depthMm);approach(c.value.ageHours,c.target.ageHours);
49
approach(c.value.waterMm,c.target.waterMm);
50
}
51
}
52
const
Surface
*
At
(
VECTOR2SHORT
p)
const
53
{
auto
it=
cells
.find(
Key
(p));
return
it==
cells
.end()?
nullptr
:&it->second.value;}
54
};
55
}
TerrainWeather.h
TerrainSnow::Store
Definition
TerrainSnow.h:21
TerrainSnow::Store::Retain
void Retain(VECTOR2SHORT p)
Definition
TerrainSnow.h:29
TerrainSnow::Store::cells
std::unordered_map< uint32_t, Cell > cells
Definition
TerrainSnow.h:23
TerrainSnow::Store::End
void End()
Definition
TerrainSnow.h:38
TerrainSnow::Store::At
const Surface * At(VECTOR2SHORT p) const
Definition
TerrainSnow.h:52
TerrainSnow::Store::generation
uint64_t generation
Definition
TerrainSnow.h:24
TerrainSnow::Store::Size
size_t Size() const
Definition
TerrainSnow.h:27
TerrainSnow::Store::Target
void Target(VECTOR2SHORT p, const SRenderWeather &sample)
Definition
TerrainSnow.h:30
TerrainSnow::Store::Advance
void Advance(float elapsed)
Definition
TerrainSnow.h:39
TerrainSnow::Store::Reset
void Reset()
Definition
TerrainSnow.h:26
TerrainSnow::Store::Begin
void Begin()
Definition
TerrainSnow.h:28
TerrainSnow
Definition
TerrainSnow.h:9
TerrainSnow::Tint
RiseMath::Vector4 Tint(const Surface &surface, float latitude, float sunLatitude)
Definition
TerrainSnow.h:12
TerrainSnow::Key
uint32_t Key(VECTOR2SHORT p)
Definition
TerrainSnow.h:10
RiseMath::Vector4
Definition
ClientMath.h:44
SRenderWeather
Definition
TerrainWeather.h:11
SRenderWeather::HasVisibility
bool HasVisibility() const
Definition
TerrainWeather.h:26
SRenderWeather::snowDepthMm
unsigned char snowDepthMm
Definition
TerrainWeather.h:16
SRenderWeather::snowAgeHours
unsigned char snowAgeHours
Definition
TerrainWeather.h:18
SRenderWeather::surfaceWaterTenthsMm
unsigned char surfaceWaterTenthsMm
Definition
TerrainWeather.h:17
TerrainSnow::Store::Cell
Definition
TerrainSnow.h:22
TerrainSnow::Store::Cell::touched
uint64_t touched
Definition
TerrainSnow.h:22
TerrainSnow::Store::Cell::value
Surface value
Definition
TerrainSnow.h:22
TerrainSnow::Store::Cell::target
Surface target
Definition
TerrainSnow.h:22
TerrainSnow::Surface
Definition
TerrainSnow.h:11
TerrainSnow::Surface::depthMm
float depthMm
Definition
TerrainSnow.h:11
TerrainSnow::Surface::ageHours
float ageHours
Definition
TerrainSnow.h:11
TerrainSnow::Surface::waterMm
float waterMm
Definition
TerrainSnow.h:11
VECTOR2SHORT
Definition
ServerTypes.h:173
VECTOR2SHORT::v
short v
Definition
ServerTypes.h:174
VECTOR2SHORT::u
short u
Definition
ServerTypes.h:174
Viewscreen
TerrainSnow.h
Generated on
for Rise by
1.18.0