Rise
The Vieneo Province
Loading...
Searching...
No Matches
WaypointProtocol.h
Go to the documentation of this file.
1#pragma once
2
4
5#include <cstddef>
6#include <array>
7#include <cwchar>
8#include <vector>
9
10// Match Server.NET/RakNet/Swaypoint.cs (Unicode, Pack = 4).
11// SPacketHuge supplies the remaining reserved bytes of the 256-byte payload.
12static_assert(sizeof(Swaypoint::text) == 128, "Waypoint names must occupy 64 UTF-16 code units.");
13static_assert(offsetof(Swaypoint, group) == 128, "Waypoint group wire offset changed.");
14static_assert(offsetof(Swaypoint, location) == 132, "Waypoint location wire offset changed.");
15static_assert(offsetof(Swaypoint, id) == 156, "Waypoint ID wire offset changed.");
16static_assert(offsetof(Swaypoint, heading) == 160, "Waypoint heading wire offset changed.");
17static_assert(offsetof(Swaypoint, length) == 164, "Waypoint length wire offset changed.");
18static_assert(offsetof(Swaypoint, source) == 172, "Waypoint source wire offset changed.");
19static_assert(offsetof(Swaypoint, orientation) == 176, "Waypoint orientation wire offset changed.");
20static_assert(offsetof(Swaypoint, uniqueid) == 208, "Waypoint unique ID wire offset changed.");
21static_assert(sizeof(Swaypoint) == 212, "Waypoint data must use 212 bytes of the payload.");
22static_assert(offsetof(SPacketHuge, type) == 256 && sizeof(SPacketHuge) == 260,
23 "Waypoint packet must retain its 256-byte payload and 260-byte wire size.");
24
26{
27 // Tabs may move when an empty folder is removed. Keep server group IDs stable:
28 // re-uploading every later waypoint would also overwrite server metadata.
30 {
31 public:
32 using SavedState = std::array<unsigned char, 4 + MAX_TAB>;
33
35 {
36 for (int tab = 0; tab < MAX_TAB; ++tab)
37 groups[tab] = static_cast<unsigned char>(tab);
38 }
39
40 int Group(short tab) const { return tab >= 0 && tab < MAX_TAB ? groups[tab] : -1; }
41
42 short Tab(unsigned char group) const
43 {
44 for (short tab = 0; tab < MAX_TAB; ++tab)
45 if (groups[tab] == group) return tab;
46 return -1;
47 }
48
49 bool CanRemove(short tab, short count, const std::vector<Swaypoint>& waypoints) const
50 {
51 // Group zero holds the permanent temporary-waypoint slot.
52 if (count <= 1 || count > MAX_TAB || tab <= 0 || tab >= count) return false;
53 for (const auto& waypoint : waypoints)
54 if (waypoint.group == Group(tab)) return false;
55 return true;
56 }
57
58 bool Remove(short tab, short count, const std::vector<Swaypoint>& waypoints)
59 {
60 if (!CanRemove(tab, count, waypoints)) return false;
61 const auto removedGroup = groups[tab];
62 for (short next = tab; next < count - 1; ++next)
63 groups[next] = groups[next + 1];
64 groups[count - 1] = removedGroup; // available to the next added folder
65 return true;
66 }
67
69 {
70 SavedState saved = { 'W', 'P', 'F', 1 };
71 for (int tab = 0; tab < MAX_TAB; ++tab) saved[4 + tab] = groups[tab];
72 return saved;
73 }
74
75 bool Load(const SavedState& saved)
76 {
77 *this = FolderOrder(); // legacy/invalid configurations use identity order
78 if (saved[0] != 'W' || saved[1] != 'P' || saved[2] != 'F' || saved[3] != 1 || saved[4] != 0)
79 return false;
80 bool seen[MAX_TAB] = {};
81 for (int tab = 0; tab < MAX_TAB; ++tab)
82 {
83 const auto group = saved[4 + tab];
84 if (group >= MAX_TAB || seen[group]) return false;
85 seen[group] = true;
86 }
87 for (int tab = 0; tab < MAX_TAB; ++tab) groups[tab] = saved[4 + tab];
88 return true;
89 }
90
91 private:
92 std::array<unsigned char, MAX_TAB> groups{};
93 };
94
95 // Loading animation progress is independent of login completion. Request one
96 // snapshot per connection, after the server has acknowledged completion, and
97 // retry a silent download without discarding records already received.
99 {
100 public:
101 bool Begin()
102 {
103 if (started) return false;
104 started = true;
105 return true;
106 }
107
108 bool TryRequest(bool loginComplete, float elapsedSeconds)
109 {
110 if (!started || complete || !loginComplete) return false;
111 if (elapsedSeconds > 0.0f) idleSeconds += elapsedSeconds;
112 if (attempts != 0 && idleSeconds < 10.0f) return false;
113 ++attempts;
114 idleSeconds = 0.0f;
115 return true;
116 }
117
118 void ReceivedRecord() { idleSeconds = 0.0f; }
119
120 // The server sends the active ID after the snapshot, including empty
121 // snapshots. Older unordered servers can send that ID before records.
122 bool Complete()
123 {
124 if (attempts == 0 || complete) return false;
125 complete = true;
126 return true;
127 }
128
129 unsigned Attempts() const { return attempts; }
130
131 private:
132 bool started = false;
133 bool complete = false;
134 unsigned attempts = 0;
135 float idleSeconds = 0.0f;
136 };
137
138 inline SClientPacket Select(int waypointId, bool streetViewConfigured)
139 {
140 SClientPacket packet = {};
141 packet.type = 37; // Waypoint management: set active.
142 packet.f_y = static_cast<float>(waypointId);
143 // Server.NET: -1 suppresses road routing; 0 retains legacy client behavior.
144 packet.f_z = streetViewConfigured ? 0.0f : -1.0f;
145 return packet;
146 }
147
148 // Older servers send the active ID and waypoint records without ordering.
149 // Keep the ID until its record arrives; never expose a failed lookup as an index.
151 {
152 public:
153 void Set(int id) { waypointId = id; }
154 void Clear() { waypointId = -1; }
155 int Id() const { return waypointId; }
156
157 bool TryResolve(const std::vector<Swaypoint>& waypoints, int& currentIndex)
158 {
159 if (waypointId < 0)
160 return false;
161
162 for (size_t i = 0; i < waypoints.size(); ++i)
163 {
164 if (waypoints[i].id == waypointId)
165 {
166 currentIndex = static_cast<int>(i);
167 Clear();
168 return true;
169 }
170 }
171 return false;
172 }
173
174 private:
175 int waypointId = -1;
176 };
177
178 inline void SetText(Swaypoint& waypoint, const WCHAR* text)
179 {
180 wcsncpy_s(waypoint.text, _countof(waypoint.text), text, _TRUNCATE);
181 const size_t length = wcslen(waypoint.text);
182 // Truncation must not leave half of a UTF-16 surrogate pair.
183 if (length > 0 && waypoint.text[length - 1] >= 0xD800 && waypoint.text[length - 1] <= 0xDBFF)
184 waypoint.text[length - 1] = L'\0';
185 }
186}
#define MAX_TAB
bool TryRequest(bool loginComplete, float elapsedSeconds)
std::array< unsigned char, 4+MAX_TAB > SavedState
bool Remove(short tab, short count, const std::vector< Swaypoint > &waypoints)
short Tab(unsigned char group) const
bool CanRemove(short tab, short count, const std::vector< Swaypoint > &waypoints) const
bool Load(const SavedState &saved)
std::array< unsigned char, MAX_TAB > groups
bool TryResolve(const std::vector< Swaypoint > &waypoints, int &currentIndex)
void SetText(Swaypoint &waypoint, const WCHAR *text)
SClientPacket Select(int waypointId, bool streetViewConfigured)
unsigned char type
WCHAR text[64]