GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — UZDoom/src/d_protocol.cpp at trunk · UZDoom/UZDoom

Expand file tree

/

d_protocol.cpp

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
345 lines (317 loc) · 8.54 KB
/

d_protocol.cpp

Copy path

File metadata and controls

345 lines (317 loc) · 8.54 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/*
** d_protocol.cpp
**
** Basic network packet creation routines and simple IFF parsing
**
**---------------------------------------------------------------------------
**
** Copyright 1998-2016 Marisa Heit
** Copyright 2016 Christoph Oelckers
** Copyright 2017-2025 GZDoom Maintainers and Contributors
** Copyright 2025-2026 UZDoom Maintainers and Contributors
**
** SPDX-License-Identifier: GPL-3.0-or-later
**
**---------------------------------------------------------------------------
**
** Code written prior to 2026 is also licensed under:
**
** SPDX-License-Identifier: BSD-3-Clause
**
**---------------------------------------------------------------------------
**
*/
#include "d_protocol.h"
#include "d_net.h"
#include "doomstat.h"
#include "cmdlib.h"
#include "serializer.h"
FSerializer& Serialize(FSerializer& arc, const char* key, usercmd_t& cmd, usercmd_t* def)
{
// This used packed data with the old serializer but that's totally counterproductive when
// having a text format that is human-readable. So this compression has been undone here.
// The few bytes of file size it saves are not worth the obfuscation.
if (arc.BeginObject(key))
{
arc("buttons", cmd.buttons)
("pitch", cmd.pitch)
("yaw", cmd.yaw)
("roll", cmd.roll)
("forwardmove", cmd.forwardmove)
("sidemove", cmd.sidemove)
("upmove", cmd.upmove)
.EndObject();
}
return arc;
}
void UnpackUserCmd(usercmd_t& cmd, const usercmd_t* basis, TArrayView<uint8_t>& stream)
{
if (basis != nullptr)
{
if (basis != &cmd)
memcpy(&cmd, basis, sizeof(usercmd_t));
}
else
{
memset(&cmd, 0, sizeof(usercmd_t));
}
uint8_t flags = ReadInt8(stream);
if (flags)
{
// We can support up to 29 buttons using 1 to 4 bytes to store them. The most
// significant bit of each button byte is a flag to indicate whether or not more buttons
// should be read in excluding the last one which supports all 8 bits.
if (flags & UCMDF_BUTTONS)
{
uint8_t in = ReadInt8(stream);
uint32_t buttons = (cmd.buttons & ~0x7F) | (in & 0x7F);
if (in & MoreButtons)
{
in = ReadInt8(stream);
buttons = (buttons & ~(0x7F << 7)) | ((in & 0x7F) << 7);
if (in & MoreButtons)
{
in = ReadInt8(stream);
buttons = (buttons & ~(0x7F << 14)) | ((in & 0x7F) << 14);
if (in & MoreButtons)
{
in = ReadInt8(stream);
buttons = (buttons & ~(0xFF << 21)) | (in << 21);
}
}
}
cmd.buttons = buttons;
}
if (flags & UCMDF_PITCH)
cmd.pitch = ReadInt16(stream);
if (flags & UCMDF_YAW)
cmd.yaw = ReadInt16(stream);
if (flags & UCMDF_FORWARDMOVE)
cmd.forwardmove = ReadInt16(stream);
if (flags & UCMDF_SIDEMOVE)
cmd.sidemove = ReadInt16(stream);
if (flags & UCMDF_UPMOVE)
cmd.upmove = ReadInt16(stream);
if (flags & UCMDF_ROLL)
cmd.roll = ReadInt16(stream);
}
}
void PackUserCmd(const usercmd_t& cmd, const usercmd_t* basis, TArrayView<uint8_t>& stream)
{
uint8_t flags = 0;
auto flagsPosition = TArrayView(stream.Data(), 1);
usercmd_t blank;
if (basis == nullptr)
{
memset(&blank, 0, sizeof(blank));
basis = &blank;
}
AdvanceStream(stream, 1); // Make room for the flags.
uint32_t buttons_changed = cmd.buttons ^ basis->buttons;
if (buttons_changed != 0)
{
uint8_t bytes[4] = { uint8_t(cmd.buttons & 0x7F),
uint8_t((cmd.buttons >> 7) & 0x7F),
uint8_t((cmd.buttons >> 14) & 0x7F),
uint8_t((cmd.buttons >> 21) & 0xFF) };
flags |= UCMDF_BUTTONS;
if (buttons_changed & 0xFFFFFF80)
{
bytes[0] |= MoreButtons;
if (buttons_changed & 0xFFFFC000)
{
bytes[1] |= MoreButtons;
if (buttons_changed & 0xFFE00000)
bytes[2] |= MoreButtons;
}
}
WriteInt8(bytes[0], stream);
if (bytes[0] & MoreButtons)
{
WriteInt8(bytes[1], stream);
if (bytes[1] & MoreButtons)
{
WriteInt8(bytes[2], stream);
if (bytes[2] & MoreButtons)
WriteInt8(bytes[3], stream);
}
}
}
if (cmd.pitch != basis->pitch)
{
flags |= UCMDF_PITCH;
WriteInt16(cmd.pitch, stream);
}
if (cmd.yaw != basis->yaw)
{
flags |= UCMDF_YAW;
WriteInt16 (cmd.yaw, stream);
}
if (cmd.forwardmove != basis->forwardmove)
{
flags |= UCMDF_FORWARDMOVE;
WriteInt16 (cmd.forwardmove, stream);
}
if (cmd.sidemove != basis->sidemove)
{
flags |= UCMDF_SIDEMOVE;
WriteInt16(cmd.sidemove, stream);
}
if (cmd.upmove != basis->upmove)
{
flags |= UCMDF_UPMOVE;
WriteInt16(cmd.upmove, stream);
}
if (cmd.roll != basis->roll)
{
flags |= UCMDF_ROLL;
WriteInt16(cmd.roll, stream);
}
// Write the packing bits
WriteInt8(flags, flagsPosition);
}
void WriteUserCmdMessage(const usercmd_t& cmd, const usercmd_t* basis, TArrayView<uint8_t>& stream)
{
if (basis == nullptr)
{
if (cmd.buttons
|| cmd.pitch || cmd.yaw || cmd.roll
|| cmd.forwardmove || cmd.sidemove || cmd.upmove)
{
WriteInt8(DEM_USERCMD, stream);
PackUserCmd(cmd, basis, stream);
return;
}
}
else if (cmd.buttons != basis->buttons
|| cmd.yaw != basis->yaw || cmd.pitch != basis->pitch || cmd.roll != basis->roll
|| cmd.forwardmove != basis->forwardmove || cmd.sidemove != basis->sidemove || cmd.upmove != basis->upmove)
{
WriteInt8(DEM_USERCMD, stream);
PackUserCmd(cmd, basis, stream);
return;
}
WriteInt8(DEM_EMPTYUSERCMD, stream);
}
// Reads through the user command without actually setting any of its info. Used to get the size
// of the command when getting the length of the stream.
void SkipUserCmdMessage(TArrayView<uint8_t>& stream)
{
while (true)
{
const uint8_t type = ReadInt8(stream);
if (type == DEM_USERCMD)
{
int skip = 1;
if (stream[0] & UCMDF_PITCH)
skip += 2;
if (stream[0] & UCMDF_YAW)
skip += 2;
if (stream[0] & UCMDF_FORWARDMOVE)
skip += 2;
if (stream[0] & UCMDF_SIDEMOVE)
skip += 2;
if (stream[0] & UCMDF_UPMOVE)
skip += 2;
if (stream[0] & UCMDF_ROLL)
skip += 2;
if (stream[0] & UCMDF_BUTTONS)
{
AdvanceStream(stream, 1);
if (stream[0] & MoreButtons)
{
AdvanceStream(stream, 1);
if (stream[0] & MoreButtons)
{
AdvanceStream(stream, 1);
if (stream[0] & MoreButtons)
AdvanceStream(stream, 1);
}
}
}
AdvanceStream(stream, skip);
break;
}
else if (type == DEM_EMPTYUSERCMD)
{
break;
}
else
{
Net_SkipCommand(type, stream);
}
}
}
void ReadUserCmdMessage(TArrayView<uint8_t>& stream, int player, int tic)
{
const int ticMod = tic % BACKUPTICS;
auto& curTic = ClientStates[player].Tics[ticMod];
usercmd_t& ticCmd = curTic.Command;
const uint8_t* start = stream.Data();
// Skip until we reach the player command. Event data will get read off once the
// tick is actually executed.
int type;
while ((type = ReadInt8(stream)) != DEM_USERCMD && type != DEM_EMPTYUSERCMD)
Net_SkipCommand(type, stream);
// Subtract a byte to account for the fact the stream head is now sitting on the
// user command. This gets cleared first because it previously got cleared after
// executing but that breaks packet-server mode and I have no idea what side effects
// might happen if it's not wiped first.
curTic.Data.SetData(nullptr, 0u);
curTic.Data.SetData(start, int(stream.Data() - start - 1));
if (type == DEM_USERCMD)
{
UnpackUserCmd(ticCmd,
tic > 0 ? &ClientStates[player].Tics[(tic - 1) % BACKUPTICS].Command : nullptr, stream);
}
else
{
if (tic > 0)
memcpy(&ticCmd, &ClientStates[player].Tics[(tic - 1) % BACKUPTICS].Command, sizeof(ticCmd));
else
memset(&ticCmd, 0, sizeof(ticCmd));
}
}
void RunPlayerCommands(int player, int tic)
{
// We don't have the full command yet, so don't run it.
if (gametic % TicDup)
return;
auto& data = ClientStates[player].Tics[tic % BACKUPTICS].Data;
auto stream = data.GetTArrayView();
if (stream.Size())
{
while (stream.Size() > 0)
Net_DoCommand(ReadInt8(stream), stream, player);
}
}
// Demo related functionality
uint8_t* streamPos = nullptr;
// Write the header of an IFF chunk and leave space
// for the length field.
void StartChunk(int id, TArrayView<uint8_t>& stream)
{
WriteInt32(id, stream);
streamPos = stream.Data();
AdvanceStream(stream, 4);
}
// Write the length field for the chunk and insert
// pad byte if the chunk is odd-sized.
void FinishChunk(TArrayView<uint8_t>& stream)
{
if (streamPos == nullptr)
return;
int len = int(stream.Data() - streamPos - 4);
auto streamPosView = TArrayView<uint8_t>(streamPos, 4);
WriteInt32(len, streamPosView);
if (len & 1)
WriteInt8(0, stream);
streamPos = nullptr;
}
// Skip past an unknown chunk. *stream should be
// pointing to the chunk's length field.
void SkipChunk(TArrayView<uint8_t>& stream)
{
int len = ReadInt32(stream);
AdvanceStream(stream, len + (len & 1));
}
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.