Rise
The Vieneo Province
Loading...
Searching...
No Matches
TerrainWeather.h
Go to the documentation of this file.
1#pragma once
2#include "ServerTypes.h"
3#include <array>
4#include <cstddef>
5#include <cstring>
6#include <cmath>
7
8// Independent plot weather. Surface elevation comes from the loaded terrain.
9#pragma pack(push, 4) // matches the managed wire packing cap; the cell aligns to 2
11{
12 unsigned char precipitation = 0; // surface level * 10, 20..160
13 unsigned char turbulence = 0; // surface level * 255, 0..255
14 short ceiling = 0; // meters AGL
15
16 unsigned char snowDepthMm = 0; // physical snow depth, 0..255 mm
17 unsigned char surfaceWaterTenthsMm = 0; // independent water depth, 0..25.5 mm
18 unsigned char snowAgeHours = 0; // 0..255 hours; zero includes fresh snow
19 unsigned char unused = 0; // reserved, ignored by consumers
20
21 uint16_t visibility = 0; // meters, 1..42160; zero means unavailable
22 unsigned char windDirectionRadians = 0; // surface direction * 25
23 unsigned char windSpeedMs = 0; // surface speed, whole meters/second
24
25 bool Available() const { return precipitation >= 20 && precipitation <= 160; }
26 bool HasVisibility() const { return Available() && ceiling >= 0 && visibility > 0 && visibility <= 42160; }
27};
29{
30 VECTOR2SHORT tcp; // unwrapped center on cube; center sample is [31][31]
31 uint8_t cube = 0, side = 0; // 0 full, 1 north, 2 south, 3 east, 4 west
32 uint16_t requestToken = 0;
33 uint32_t revision = 0, baseRevision = 0;
34};
37#pragma pack(pop)
38
39namespace PlotWeather
40{
41 constexpr unsigned RequestType = 45;
42 static_assert(sizeof(SRenderWeather) == 12 && alignof(SRenderWeather) == 2
43 && offsetof(SRenderWeather, precipitation) == 0 && offsetof(SRenderWeather, turbulence) == 1
44 && offsetof(SRenderWeather, ceiling) == 2 && offsetof(SRenderWeather, snowDepthMm) == 4
45 && offsetof(SRenderWeather, surfaceWaterTenthsMm) == 5 && offsetof(SRenderWeather, snowAgeHours) == 6
46 && offsetof(SRenderWeather, unused) == 7 && offsetof(SRenderWeather, visibility) == 8
47 && offsetof(SRenderWeather, windDirectionRadians) == 10 && offsetof(SRenderWeather, windSpeedMs) == 11,
48 "Weather cell layout");
49 static_assert(sizeof(SPlotWeatherHeader) == 16 && offsetof(SPlotWeatherHeader, cube) == 4
50 && offsetof(SPlotWeatherHeader, side) == 5 && offsetof(SPlotWeatherHeader, requestToken) == 6
51 && offsetof(SPlotWeatherHeader, revision) == 8 && offsetof(SPlotWeatherHeader, baseRevision) == 12, "Weather header layout");
52 static_assert(sizeof(SPlotWeatherRow) == 784 && offsetof(SPlotWeatherRow, samples) == 16, "Weather strip layout");
53 static_assert(sizeof(SPlotWeatherGrid) == 49168 && offsetof(SPlotWeatherGrid, samples) == 16, "Weather grid layout");
54 static_assert(sizeof(SRenderVertex) == 12 && sizeof(SRenderPoint) == 16
55 && sizeof(SRenderRow) == 776 && sizeof(SRenderGrid) == 49156, "Terrain wire format stays unchanged");
56
57 inline uint16_t U16(const unsigned char* p) { return uint16_t(p[0]) | uint16_t(p[1]) << 8; }
58 inline uint32_t U32(const unsigned char* p) { return uint32_t(U16(p)) | uint32_t(U16(p + 2)) << 16; }
59 inline short I16(const unsigned char* p)
60 {
61 const int value = U16(p);
62 return static_cast<short>(value >= 32768 ? value - 65536 : value);
63 }
64 inline bool Newer(uint32_t value, uint32_t previous)
65 { return value != previous && uint32_t(value - previous) < 0x80000000u; }
66
67 inline bool ParseHeader(const unsigned char* p, size_t length, SPlotWeatherHeader& result)
68 {
69 if (!p || (length != sizeof(SPlotWeatherGrid) && length != sizeof(SPlotWeatherRow))) return false;
70 result.tcp = VECTOR2SHORT(I16(p), I16(p + 2));
71 result.cube = p[4]; result.side = p[5]; result.requestToken = U16(p + 6);
72 result.revision = U32(p + 8); result.baseRevision = U32(p + 12);
73 if (result.cube > 5 || result.tcp.u == 30000 || result.tcp.v == 30000 || !result.revision) return false;
74 if (length == sizeof(SPlotWeatherGrid)) return result.side == 0 && result.baseRevision == 0;
75 return result.side >= 1 && result.side <= 4 && result.baseRevision != 0
76 && Newer(result.revision, result.baseRevision);
77 }
78 inline SRenderWeather Sample(const unsigned char* p)
79 {
80 SRenderWeather result{};
81 result.precipitation = p[0]; result.turbulence = p[1]; result.ceiling = I16(p + 2);
82 result.snowDepthMm = p[4]; result.surfaceWaterTenthsMm = p[5];
83 result.snowAgeHours = p[6]; result.unused = p[7];
84 result.visibility = U16(p + 8);
85 result.windDirectionRadians = p[10]; result.windSpeedMs = p[11];
86 return result.HasVisibility() ? result : SRenderWeather{};
87 }
88
89
91 {
94 float turbulence = 0; // normalized 0..1
95 float surfaceWindDirectionRadians = 0, surfaceWindSpeedMs = 0; // never replaces altitude-specific Weather1 wind
96 };
97
98 // Same cube projection as CalculateCGC, retaining the fractional coordinate.
99 // Samples are grid control points, not cell centers. Altitude cancels out.
100 inline bool Coordinates(const RiseMath::Vector3& position, unsigned cube, double& u, double& v)
101 {
102 const double x = position.x, y = position.y, z = position.z;
103 if (!std::isfinite(x) || !std::isfinite(y) || !std::isfinite(z)) return false;
104 double a = 0, b = 0, denominator = 0, offsetU = 0, offsetV = 0;
105 switch (cube)
106 {
107 case 0: denominator = -y; a = x; b = z; break;
108 case 1: denominator = x; a = y; b = z; offsetU = 2038; break;
109 case 2: denominator = -x; a = -y; b = z; offsetU = -2038; break;
110 case 3: denominator = y; a = -x; b = z; offsetU = 4076; break;
111 case 4: denominator = -z; a = x; b = -y; offsetV = -2038; break;
112 case 5: denominator = z; a = x; b = y; offsetV = 2038; break;
113 default: return false;
114 }
115 if (!(denominator > 0)) return false;
116 u = (a / denominator + 1) * 1019 + offsetU;
117 v = (b / denominator + 1) * 1019 + offsetV;
118 return std::isfinite(u) && std::isfinite(v);
119 }
120
121 enum class Result { Invalid, Ignored, NeedFull, Full, Row };
122 class Store
123 {
124 std::array<SRenderWeather, 64 * 64> samples{};
126 bool ready = false;
127 public:
128 void Reset() { samples = {}; header = {}; ready = false; }
129 bool Ready() const { return ready; }
130 const SPlotWeatherHeader& Header() const { return header; }
131 const SRenderWeather* At(unsigned cube, VECTOR2SHORT tcp, uint16_t token) const
132 {
133 if (!ready || header.cube != cube || header.requestToken != token) return nullptr;
134 const int q = 31 + tcp.u - header.tcp.u, r = 31 + tcp.v - header.tcp.v;
135 if (q < 0 || q >= 64 || r < 0 || r >= 64) return nullptr;
136 return &samples[q * 64 + r];
137 }
138
139 bool Interpolate(unsigned cube, double u, double v, uint16_t token, LocalSample& result) const
140 {
141 if (!ready || header.cube != cube || header.requestToken != token
142 || !std::isfinite(u) || !std::isfinite(v)) return false;
143 const double q = 31 + u - header.tcp.u, r = 31 + v - header.tcp.v;
144 if (q < 0 || q > 63 || r < 0 || r > 63) return false;
145 const int q0 = static_cast<int>(std::floor(q)), r0 = static_cast<int>(std::floor(r));
146 const double fq = q - q0, fr = r - r0;
147 double ceiling = 0, visibility = 0, precipitation = 0, snow = 0, age = 0, water = 0, turbulence = 0;
148 double windX = 0, windY = 0;
149 for (int dq = 0; dq <= 1; ++dq) for (int dr = 0; dr <= 1; ++dr)
150 {
151 const double weight = (dq ? fq : 1 - fq) * (dr ? fr : 1 - fr);
152 if (weight == 0) continue; // exact valid edge/control points need no absent neighbor
153 if (q0 + dq >= 64 || r0 + dr >= 64) return false;
154 const auto& sample = samples[(q0 + dq) * 64 + r0 + dr];
155 // Hold the previous local conditions if any contributing corner is
156 // unavailable. Never blend hidden/void zeroes or extrapolate a seam.
157 if (!sample.HasVisibility()) return false;
158 ceiling += weight * sample.ceiling;
159 visibility += weight * sample.visibility;
160 precipitation += weight * sample.precipitation * .1;
161 turbulence += weight * sample.turbulence / 255.0;
162 // Snow and water coexist; age zero is also valid for fresh snow.
163 snow += weight * sample.snowDepthMm; age += weight * sample.snowAgeHours;
164 water += weight * sample.surfaceWaterTenthsMm * .1;
165 // Blend vectors so directions across 0/2pi agree and calm cells carry no bearing.
166 const double direction = sample.windDirectionRadians / 25.0;
167 windX += weight * sample.windSpeedMs * std::cos(direction);
168 windY += weight * sample.windSpeedMs * std::sin(direction);
169 }
170 const double windSpeed = std::hypot(windX, windY);
171 double windDirection = windSpeed > 1e-9 ? std::atan2(windY, windX) : 0;
172 if (windDirection < 0) windDirection += 6.2831853071795864769;
173 result = {static_cast<float>(ceiling),
174 static_cast<float>(visibility), static_cast<float>(precipitation),
175 static_cast<float>(snow), static_cast<float>(age), static_cast<float>(water),
176 static_cast<float>(turbulence), static_cast<float>(windDirection),
177 static_cast<float>(windSpeed > 1e-9 ? windSpeed : 0)};
178 return true;
179 }
180
181 Result Accept(const unsigned char* p, size_t length, uint16_t expectedToken)
182 {
184 if (!ParseHeader(p, length, next)) return Result::Invalid;
185 if (next.requestToken != expectedToken || (header.revision && !Newer(next.revision, header.revision)))
186 return Result::Ignored;
187 if (next.side == 0)
188 {
189 // All bounds/header checks precede publication; no rendering runs
190 // between decoding and publication in the frame's packet dispatcher.
191 for (size_t i = 0; i < samples.size(); ++i) samples[i] = Sample(p + 16 + i * sizeof(SRenderWeather));
192 header = next; ready = true;
193 return Result::Full;
194 }
195 if (!ready || next.requestToken != header.requestToken || next.cube != header.cube
196 || next.baseRevision != header.revision) return Result::NeedFull;
197 const int du = int(next.tcp.u) - header.tcp.u, dv = int(next.tcp.v) - header.tcp.v;
198 const int expectedU = next.side == 3 ? 1 : next.side == 4 ? -1 : 0;
199 const int expectedV = next.side == 2 ? 1 : next.side == 1 ? -1 : 0;
200 if (du != expectedU || dv != expectedV) return Result::NeedFull;
201
202 if (du)
203 {
204 auto* destination = samples.data() + (du < 0 ? 64 : 0);
205 const auto* source = samples.data() + (du > 0 ? 64 : 0);
206 memmove(destination, source, 63 * 64 * sizeof(SRenderWeather));
207 const int q = du > 0 ? 63 : 0;
208 for (int r = 0; r < 64; ++r) samples[q * 64 + r] = Sample(p + 16 + r * sizeof(SRenderWeather));
209 }
210 else
211 {
212 const int r = dv > 0 ? 63 : 0;
213 for (int q = 0; q < 64; ++q)
214 {
215 auto* column = samples.data() + q * 64;
216 memmove(column + (dv < 0 ? 1 : 0), column + (dv > 0 ? 1 : 0), 63 * sizeof(SRenderWeather));
217 column[r] = Sample(p + 16 + q * sizeof(SRenderWeather));
218 }
219 }
220 header = next;
221 return Result::Row;
222 }
223 };
224}
SCRIPT_CONTROL SCRIPT_STATE const void int
Result Accept(const unsigned char *p, size_t length, uint16_t expectedToken)
bool Interpolate(unsigned cube, double u, double v, uint16_t token, LocalSample &result) const
SPlotWeatherHeader header
std::array< SRenderWeather, 64 *64 > samples
const SRenderWeather * At(unsigned cube, VECTOR2SHORT tcp, uint16_t token) const
const SPlotWeatherHeader & Header() const
constexpr unsigned RequestType
short I16(const unsigned char *p)
bool Newer(uint32_t value, uint32_t previous)
bool ParseHeader(const unsigned char *p, size_t length, SPlotWeatherHeader &result)
uint16_t U16(const unsigned char *p)
SRenderWeather Sample(const unsigned char *p)
uint32_t U32(const unsigned char *p)
bool Coordinates(const RiseMath::Vector3 &position, unsigned cube, double &u, double &v)
SRenderWeather samples[64][64]
SPlotWeatherHeader header
SPlotWeatherHeader header
SRenderWeather samples[64]
bool Available() const
unsigned char windDirectionRadians
bool HasVisibility() const
unsigned char unused
unsigned char precipitation
unsigned char snowDepthMm
uint16_t visibility
unsigned char turbulence
unsigned char snowAgeHours
unsigned char surfaceWaterTenthsMm
unsigned char windSpeedMs