Rise
The Vieneo Province
Toggle main menu visibility
Loading...
Searching...
No Matches
WaypointProtocol.h
Go to the documentation of this file.
1
#pragma once
2
3
#include "
Protocol/ServerTypes.h
"
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.
12
static_assert
(
sizeof
(
Swaypoint::text
) == 128,
"Waypoint names must occupy 64 UTF-16 code units."
);
13
static_assert
(offsetof(
Swaypoint
, group) == 128,
"Waypoint group wire offset changed."
);
14
static_assert
(offsetof(
Swaypoint
, location) == 132,
"Waypoint location wire offset changed."
);
15
static_assert
(offsetof(
Swaypoint
,
id
) == 156,
"Waypoint ID wire offset changed."
);
16
static_assert
(offsetof(
Swaypoint
, heading) == 160,
"Waypoint heading wire offset changed."
);
17
static_assert
(offsetof(
Swaypoint
, length) == 164,
"Waypoint length wire offset changed."
);
18
static_assert
(offsetof(
Swaypoint
, source) == 172,
"Waypoint source wire offset changed."
);
19
static_assert
(offsetof(
Swaypoint
, orientation) == 176,
"Waypoint orientation wire offset changed."
);
20
static_assert
(offsetof(
Swaypoint
, uniqueid) == 208,
"Waypoint unique ID wire offset changed."
);
21
static_assert
(
sizeof
(
Swaypoint
) == 212,
"Waypoint data must use 212 bytes of the payload."
);
22
static_assert
(offsetof(
SPacketHuge
, type) == 256 &&
sizeof
(
SPacketHuge
) == 260,
23
"Waypoint packet must retain its 256-byte payload and 260-byte wire size."
);
24
25
namespace
WaypointProtocol
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.
29
class
FolderOrder
30
{
31
public
:
32
using
SavedState
= std::array<unsigned char, 4 + MAX_TAB>;
33
34
FolderOrder
()
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
68
SavedState
Save
()
const
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.
98
class
DownloadState
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.
150
class
PendingSelection
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
}
ServerTypes.h
MAX_TAB
#define MAX_TAB
Definition
ServerTypes.h:203
WaypointProtocol::DownloadState
Definition
WaypointProtocol.h:99
WaypointProtocol::DownloadState::Complete
bool Complete()
Definition
WaypointProtocol.h:122
WaypointProtocol::DownloadState::complete
bool complete
Definition
WaypointProtocol.h:133
WaypointProtocol::DownloadState::Attempts
unsigned Attempts() const
Definition
WaypointProtocol.h:129
WaypointProtocol::DownloadState::Begin
bool Begin()
Definition
WaypointProtocol.h:101
WaypointProtocol::DownloadState::attempts
unsigned attempts
Definition
WaypointProtocol.h:134
WaypointProtocol::DownloadState::started
bool started
Definition
WaypointProtocol.h:132
WaypointProtocol::DownloadState::ReceivedRecord
void ReceivedRecord()
Definition
WaypointProtocol.h:118
WaypointProtocol::DownloadState::idleSeconds
float idleSeconds
Definition
WaypointProtocol.h:135
WaypointProtocol::DownloadState::TryRequest
bool TryRequest(bool loginComplete, float elapsedSeconds)
Definition
WaypointProtocol.h:108
WaypointProtocol::FolderOrder::SavedState
std::array< unsigned char, 4+MAX_TAB > SavedState
Definition
WaypointProtocol.h:32
WaypointProtocol::FolderOrder::Remove
bool Remove(short tab, short count, const std::vector< Swaypoint > &waypoints)
Definition
WaypointProtocol.h:58
WaypointProtocol::FolderOrder::Tab
short Tab(unsigned char group) const
Definition
WaypointProtocol.h:42
WaypointProtocol::FolderOrder::FolderOrder
FolderOrder()
Definition
WaypointProtocol.h:34
WaypointProtocol::FolderOrder::CanRemove
bool CanRemove(short tab, short count, const std::vector< Swaypoint > &waypoints) const
Definition
WaypointProtocol.h:49
WaypointProtocol::FolderOrder::Save
SavedState Save() const
Definition
WaypointProtocol.h:68
WaypointProtocol::FolderOrder::Load
bool Load(const SavedState &saved)
Definition
WaypointProtocol.h:75
WaypointProtocol::FolderOrder::Group
int Group(short tab) const
Definition
WaypointProtocol.h:40
WaypointProtocol::FolderOrder::groups
std::array< unsigned char, MAX_TAB > groups
Definition
WaypointProtocol.h:92
WaypointProtocol::PendingSelection
Definition
WaypointProtocol.h:151
WaypointProtocol::PendingSelection::TryResolve
bool TryResolve(const std::vector< Swaypoint > &waypoints, int ¤tIndex)
Definition
WaypointProtocol.h:157
WaypointProtocol::PendingSelection::Clear
void Clear()
Definition
WaypointProtocol.h:154
WaypointProtocol::PendingSelection::Set
void Set(int id)
Definition
WaypointProtocol.h:153
WaypointProtocol::PendingSelection::Id
int Id() const
Definition
WaypointProtocol.h:155
WaypointProtocol::PendingSelection::waypointId
int waypointId
Definition
WaypointProtocol.h:175
waypoints
Definition
waypoints.h:9
WaypointProtocol
Definition
WaypointProtocol.h:26
WaypointProtocol::SetText
void SetText(Swaypoint &waypoint, const WCHAR *text)
Definition
WaypointProtocol.h:178
WaypointProtocol::Select
SClientPacket Select(int waypointId, bool streetViewConfigured)
Definition
WaypointProtocol.h:138
SClientPacket
Definition
ServerTypes.h:580
SClientPacket::f_z
float f_z
Definition
ServerTypes.h:583
SClientPacket::type
unsigned char type
Definition
ServerTypes.h:581
SClientPacket::f_y
float f_y
Definition
ServerTypes.h:583
SPacketHuge
Definition
ServerTypes.h:934
Swaypoint
Definition
ServerTypes.h:891
Swaypoint::text
WCHAR text[64]
Definition
ServerTypes.h:892
WaypointProtocol.h
Generated on
for Rise by
1.18.0