Rise
The Vieneo Province
Loading...
Searching...
No Matches
Tests/TerrainHandoffReplay.h

Source at documentation-generation time. See Test catalog for fixture associations.

1// Offline production grid/request/receiver replay. Terrain samples are synthetic;
2// positions are read from Bill's video at 2:24 and the 2:26 blackout.
3static RiseMath::Vector3 HandoffPosition(float latitude, float longitude)
4{
5 const float lat = RiseMath::ToRadian(latitude), lon = RiseMath::ToRadian(longitude);
6 return RiseMath::Vector3(sinf(lon)*cosf(lat), -cosf(lon)*cosf(lat), sinf(lat)) * (radiusC+7.47f);
7}
8
9static void CheckTerrainHandoff(Viewscreen& view, grid& terrain, Networking& network, RowReceiver& receiver)
10{
11 const auto before = HandoffPosition(35.300f,45.064f);
12 const auto after = HandoffPosition(35.292f,45.066f);
13 Check(view.CalculateCube(before) != view.CalculateCube(after), "video route does not cross a cube face");
14 std::cout << "Video crossing N35.300 E45.064 -> N35.292 E45.066: face "
15 << int(view.CalculateCube(before)) << " -> " << int(view.CalculateCube(after)) << '\n';
16
17 for (bool reverse : {false,true})
18 {
19 terrain.ClearGrid(); network.requests.clear();
20 playerships[0].position = reverse ? after : before;
21 terrain.CalculateCubeAndTcp(); terrain.RequestWholeGrid(); FullGrid(terrain);
22 const auto oldTcp = terrain.tcp; const char oldCube = terrain.cube;
23 view.BCLoffsetu = 3; view.BCLoffsetv = 5;
24 std::vector<LOCALGRID2> snapshot(4096);
25 memcpy(snapshot.data(),gridarray,sizeof gridarray);
26 const int valid = ValidVertices(); Check(valid > 2048,"initial corner terrain missing");
27 auto unchanged = [&] { return memcmp(snapshot.data(),gridarray,sizeof gridarray)==0; };
28
29 playerships[0].position = reverse ? before : after;
30 const auto requestCount = network.requests.size(); terrain.Process();
31 Check(network.requests.size()==requestCount+1 && network.requests.back().f_w==0,
32 "face crossing did not request one complete grid");
33 Check(terrain.wholeGridPending && terrain.dataReady && terrain.geometryReady && unchanged(),
34 "face request dropped the last complete terrain before its replacement arrived");
35 Check(terrain.cube==oldCube && terrain.tcp.u==oldTcp.u && terrain.tcp.v==oldTcp.v,
36 "visible grid relabelled with replacement coordinates");
37 Check(view.BCLoffsetu==3 && view.BCLoffsetv==5,
38 "face request reset visible cloud coordinates");
39
40 // Delay the response for 120 frames and fly back across the boundary.
41 playerships[0].position = reverse ? after : before;
42 for (int frame=0;frame<120;++frame) terrain.Process();
43 terrain.RequestWholeGrid();
44 Check(network.requests.size()==requestCount+1 && unchanged() && ValidVertices()==valid,
45 "delayed response caused an empty grid or overlapping whole-grid requests");
46
47 SRenderRow row{}; row.tcp=oldTcp; row.side=1;
48 for (auto& v:row.gridarray) { v=Sample(); v.elevation=900; }
49 receiver.Receive(row);
50 Check(unchanged() && terrain.dataPending==64 && terrain.geometryReady,
51 "old-face row corrupted the retained terrain or pending full response");
52
53 auto wrong = std::make_unique<SRenderGrid>(); wrong->tcp=terrain.requestedTcp; ++wrong->tcp.u;
54 terrain.ReceiveWholeGrid(*wrong);
55 Check(terrain.wholeGridPending && unchanged(),"unmatched whole grid replaced visible terrain");
56
57 const char requestedFace=terrain.requestedCube; const auto requestedCenter=terrain.requestedTcp;
58 auto packet=std::make_unique<SRenderGrid>(); packet->tcp=requestedCenter;
59 for(auto& column:packet->gridarray) for(auto& v:column) v=Sample();
60 terrain.ReceiveWholeGrid(*packet);
61 Check(!terrain.wholeGridPending && terrain.dataReady && !terrain.geometryReady && terrain.wholeGridReceived,
62 "complete response was not queued for a terrain upload");
63 Check(terrain.cube==requestedFace && terrain.tcp.u==requestedCenter.u && terrain.tcp.v==requestedCenter.v,
64 "response used the moving ship's face instead of the requested face");
65 Check(ValidVertices()>2048 && view.BCLoffsetu==0 && view.BCLoffsetv==0,
66 "replacement terrain or cloud origin was not installed");
67 terrain.Process();
68 Check(network.requests.size()==requestCount+1,"movement overtook the new grid's first upload");
69 // Model the omitted upload; the next frame must catch up to the ship.
70 terrain.wholeGridReceived=false; terrain.geometryReady=true; terrain.Process();
71 Check(terrain.wholeGridPending && network.requests.size()==requestCount+2,
72 "terrain did not catch up after crossing back during the wait");
73 FullGrid(terrain);
74
75 // Rows from remote previous centers must never index past the array.
76 memcpy(snapshot.data(),gridarray,sizeof gridarray);
77 for(int side=1;side<=4;++side) for(int delta:{-200,200})
78 {
79 row.side=static_cast<unsigned char>(side); row.tcp=terrain.tcp;
80 row.tcp.u+=delta; row.tcp.v+=delta; terrain.dataPending=1;
81 receiver.Receive(row); Check(unchanged(),"out-of-range row wrote into the active grid");
82 }
83 }
84
85 // 000COMMANDW0 needs a refresh even when its destination is in the same cell.
86 terrain.PrepareForReposition();
87 Check(terrain.resetWeatherOnNextGrid && !terrain.dataReady && !terrain.wholeGridPending,
88 "reposition did not invalidate collision data and arm the terrain refresh");
89 terrain.Process();
90 Check(terrain.wholeGridPending && terrain.resetWeatherOnNextGrid,
91 "reposition did not request fresh terrain");
92 const auto repositionRequests=network.requests.size();
93 for(int frame=0;frame<120;++frame) terrain.Process();
94 terrain.RequestWholeGrid();
95 Check(network.requests.size()==repositionRequests,"pending reposition repeatedly requested terrain");
96 auto unrelated=std::make_unique<SRenderGrid>(); unrelated->tcp=terrain.requestedTcp; ++unrelated->tcp.v;
97 terrain.ReceiveWholeGrid(*unrelated);
98 Check(terrain.resetWeatherOnNextGrid,"unrelated response consumed the reposition flag");
99 FullGrid(terrain);
100 Check(!terrain.resetWeatherOnNextGrid,"destination grid did not consume the terrain refresh flag");
101 terrain.RequestWholeGrid(); FullGrid(terrain);
102 Check(!terrain.resetWeatherOnNextGrid,"ordinary refresh rearmed reposition");
103
104 // Genuine environment clearing still discards terrain and cancels its request.
105 playerships[0].position=after; terrain.CalculateCubeAndTcp(); terrain.RequestWholeGrid();
106 auto late=std::make_unique<SRenderGrid>(); late->tcp=terrain.requestedTcp;
107 g_bAboveClouds=true; terrain.Process();
108 Check(!terrain.dataReady && !terrain.wholeGridPending && ValidVertices()==0,"above-cloud clear retained terrain");
109 terrain.ReceiveWholeGrid(*late);
110 Check(!terrain.dataReady,"cancelled response resurrected an above-cloud grid");
111 g_bAboveClouds=false; terrain.Process();
112 Check(terrain.wholeGridPending && !terrain.dataReady,"descent did not request fresh terrain");
113 FullGrid(terrain); terrain.ClearGrid(); network.requests.clear();
114 std::cout << "PASS terrain handoff: both video directions, 120-frame delay, reversal, stale rows, reposition, cancellation and descent\n";
115}
SCRIPT_CONTROL SCRIPT_STATE const void int
short BCLoffsetv
Definition Viewscreen.h:419
short BCLoffsetu
Definition Viewscreen.h:419
char CalculateCube(RiseMath::Vector3 position) const
Definition grid.h:14
bool geometryReady
Definition grid.h:37
void ClearGrid()
Definition grid.cpp:78
char requestedCube
Definition grid.h:20
VECTOR2SHORT requestedTcp
Definition grid.h:21
bool wholeGridPending
Definition grid.h:40
void ReceiveWholeGrid(const SRenderGrid &datapacket)
Definition grid.cpp:219
char cube
Definition grid.h:45
bool dataReady
Definition grid.h:36
void CalculateCubeAndTcp()
Definition grid.cpp:101
void Process()
Definition grid.cpp:310
void RequestWholeGrid()
Definition grid.cpp:279
void PrepareForReposition()
Definition grid.cpp:271
bool wholeGridReceived
Definition grid.h:22
VECTOR2SHORT tcp
Definition grid.h:44
bool resetWeatherOnNextGrid
Definition grid.h:41
char dataPending
Definition grid.h:39
s_network_objects playerships[MAX_SCAN]
Definition globals.cpp:112
bool g_bAboveClouds
Definition globals.cpp:16
LOCALGRID2 gridarray[64][64]
Definition globals.cpp:100
#define radiusC
Definition globals.h:87
SRenderWeather Sample(const unsigned char *p)
auto ToRadian(T value)
Definition ClientMath.h:13
SRenderVertex gridarray[64]
VECTOR2SHORT tcp
unsigned char side
RiseMath::Vector3 position
Definition globals.h:511