forked from QW-Group/ezquake-source
-
Notifications
You must be signed in to change notification settings - Fork 0
/
com_msg.c
689 lines (562 loc) · 13.6 KB
/
com_msg.c
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
/*
Copyright (C) 1996-1997 Id Software, Inc.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
$Id: com_msg.c,v 1.6 2006-07-24 15:13:28 disconn3ct Exp $
*/
#include "quakedef.h"
/*
==============================================================================
MESSAGE IO FUNCTIONS
==============================================================================
Handles byte ordering and avoids alignment errors
*/
#ifdef FTE_PEXT_FLOATCOORDS
int msg_coordsize = 2; // 2 or 4.
int msg_anglesize = 1; // 1 or 2.
float MSG_FromCoord(coorddata c, int bytes)
{
switch(bytes)
{
case 2: //encode 1/8th precision, giving -4096 to 4096 map sizes
return LittleShort(c.b2)/8.0f;
case 4:
return LittleFloat(c.f);
default:
Host_Error("MSG_FromCoord: not a sane size");
return 0;
}
}
coorddata MSG_ToCoord(float f, int bytes) //return value should be treated as (char*)&ret;
{
coorddata r;
switch(bytes)
{
case 2:
r.b4 = 0;
if (f >= 0)
r.b2 = LittleShort((short)(f*8+0.5f));
else
r.b2 = LittleShort((short)(f*8-0.5f));
break;
case 4:
r.f = LittleFloat(f);
break;
default:
Host_Error("MSG_ToCoord: not a sane size");
r.b4 = 0;
}
return r;
}
coorddata MSG_ToAngle(float f, int bytes) //return value is NOT byteswapped.
{
coorddata r;
switch(bytes)
{
case 1:
r.b4 = 0;
if (f >= 0)
r.b[0] = (int)(f*(256.0f/360.0f) + 0.5f) & 255;
else
r.b[0] = (int)(f*(256.0f/360.0f) - 0.5f) & 255;
break;
case 2:
r.b4 = 0;
if (f >= 0)
r.b2 = LittleShort((int)(f*(65536.0f/360.0f) + 0.5f) & 65535);
else
r.b2 = LittleShort((int)(f*(65536.0f/360.0f) - 0.5f) & 65535);
break;
// case 4:
// r.f = LittleFloat(f);
// break;
default:
Host_Error("MSG_ToAngle: not a sane size");
r.b4 = 0;
}
return r;
}
#endif
// writing functions
void MSG_WriteChar (sizebuf_t *sb, int c)
{
byte *buf;
#ifdef PARANOID
if (c < -128 || c > 127)
Sys_Error ("MSG_WriteChar: range error");
#endif
buf = SZ_GetSpace (sb, 1);
buf[0] = c;
}
void MSG_WriteByte (sizebuf_t *sb, int c)
{
byte *buf;
#ifdef PARANOID
if (c < 0 || c > 255)
Sys_Error ("MSG_WriteByte: range error");
#endif
buf = SZ_GetSpace (sb, 1);
buf[0] = c;
}
void MSG_WriteShort (sizebuf_t *sb, int c)
{
byte *buf;
#ifdef PARANOID
if (c < ((short)0x8000) || c > (short)0x7fff)
Sys_Error ("MSG_WriteShort: range error");
#endif
buf = SZ_GetSpace (sb, 2);
buf[0] = c & 0xff;
buf[1] = c >> 8;
}
void MSG_WriteLong (sizebuf_t *sb, int c)
{
byte *buf;
buf = SZ_GetSpace (sb, 4);
buf[0] = c & 0xff;
buf[1] = (c >> 8) & 0xff;
buf[2] = (c >> 16) & 0xff;
buf[3] = c >> 24;
}
void MSG_WriteFloat (sizebuf_t *sb, float f)
{
union {
float f;
int l;
} dat;
dat.f = f;
dat.l = LittleLong (dat.l);
SZ_Write (sb, &dat.l, 4);
}
void MSG_WriteString (sizebuf_t *sb, const char *s)
{
if (!s || !*s)
SZ_Write (sb, "", 1);
else
SZ_Write (sb, s, strlen(s)+1);
}
void MSG_WriteUnterminatedString (sizebuf_t *sb, char *s)
{
if (s && *s)
SZ_Write (sb, s, strlen(s));
}
void MSG_WriteCoord (sizebuf_t *sb, float f)
{
#ifdef FTE_PEXT_FLOATCOORDS
coorddata i = MSG_ToCoord(f, msg_coordsize);
SZ_Write (sb, (void*)&i, msg_coordsize);
#else
MSG_WriteShort (sb, (int)(f * 8));
#endif
}
void MSG_WriteAngle16 (sizebuf_t *sb, float f)
{
MSG_WriteShort (sb, Q_rint(f * 65536.0 / 360.0) & 65535);
}
void MSG_WriteAngle (sizebuf_t *sb, float f)
{
#ifdef FTE_PEXT_FLOATCOORDS
if (msg_anglesize == 2)
MSG_WriteAngle16(sb, f);
// else if (msg_anglesize==4)
// MSG_WriteFloat(sb, f);
else
#endif
MSG_WriteByte (sb, Q_rint(f * 256.0 / 360.0) & 255);
}
void MSG_WriteDeltaUsercmd (sizebuf_t *buf, struct usercmd_s *from, struct usercmd_s *cmd)
{
int bits;
// send the movement message
bits = 0;
if (cmd->angles[0] != from->angles[0])
bits |= CM_ANGLE1;
if (cmd->angles[1] != from->angles[1])
bits |= CM_ANGLE2;
if (cmd->angles[2] != from->angles[2])
bits |= CM_ANGLE3;
if (cmd->forwardmove != from->forwardmove)
bits |= CM_FORWARD;
if (cmd->sidemove != from->sidemove)
bits |= CM_SIDE;
if (cmd->upmove != from->upmove)
bits |= CM_UP;
if (cmd->buttons != from->buttons)
bits |= CM_BUTTONS;
if (cmd->impulse != from->impulse)
bits |= CM_IMPULSE;
MSG_WriteByte (buf, bits);
if (bits & CM_ANGLE1)
MSG_WriteAngle16 (buf, cmd->angles[0]);
if (bits & CM_ANGLE2)
MSG_WriteAngle16 (buf, cmd->angles[1]);
if (bits & CM_ANGLE3)
MSG_WriteAngle16 (buf, cmd->angles[2]);
if (bits & CM_FORWARD)
MSG_WriteShort (buf, cmd->forwardmove);
if (bits & CM_SIDE)
MSG_WriteShort (buf, cmd->sidemove);
if (bits & CM_UP)
MSG_WriteShort (buf, cmd->upmove);
if (bits & CM_BUTTONS)
MSG_WriteByte (buf, cmd->buttons);
if (bits & CM_IMPULSE)
MSG_WriteByte (buf, cmd->impulse);
MSG_WriteByte (buf, cmd->msec);
}
//Writes part of a packetentities message.
//Can delta from either a baseline or a previous packet_entity
void MSG_WriteDeltaEntity (entity_state_t *from, entity_state_t *to, sizebuf_t *msg, qbool force, unsigned int fte_extensions)
{
int bits, i;
#ifdef PROTOCOL_VERSION_FTE
int evenmorebits = 0;
unsigned int required_extensions = 0;
#endif
// send an update
bits = 0;
for (i=0 ; i<3 ; i++)
if ( to->origin[i] != from->origin[i] )
bits |= U_ORIGIN1<<i;
if ( to->angles[0] != from->angles[0] )
bits |= U_ANGLE1;
if ( to->angles[1] != from->angles[1] )
bits |= U_ANGLE2;
if ( to->angles[2] != from->angles[2] )
bits |= U_ANGLE3;
if ( to->colormap != from->colormap )
bits |= U_COLORMAP;
if ( to->skinnum != from->skinnum )
bits |= U_SKIN;
if ( to->frame != from->frame )
bits |= U_FRAME;
if ( to->effects != from->effects )
bits |= U_EFFECTS;
if (to->modelindex != from->modelindex) {
bits |= U_MODEL;
#ifdef FTE_PEXT_ENTITYDBL
if (to->modelindex > 255) {
evenmorebits |= U_FTE_MODELDBL;
required_extensions |= FTE_PEXT_MODELDBL;
}
#endif
}
if (bits & U_CHECKMOREBITS)
bits |= U_MOREBITS;
if (to->flags & U_SOLID)
bits |= U_SOLID;
// Taken from FTE
if (msg->cursize + 40 > msg->maxsize)
{ //not enough space in the buffer, don't send the entity this frame. (not sending means nothing changes, and it takes no bytes!!)
*to = *from;
return;
}
//
// write the message
//
if (!to->number) {
Sys_Error("Unset entity number");
}
#ifdef PROTOCOL_VERSION_FTE
if (to->number >= 512)
{
if (to->number >= 1024)
{
if (to->number >= 1024 + 512) {
evenmorebits |= U_FTE_ENTITYDBL;
required_extensions |= FTE_PEXT_ENTITYDBL;
}
evenmorebits |= U_FTE_ENTITYDBL2;
required_extensions |= FTE_PEXT_ENTITYDBL2;
if (to->number >= 2048) {
Sys_Error("Entity number >= 2048");
}
}
else {
evenmorebits |= U_FTE_ENTITYDBL;
required_extensions |= FTE_PEXT_ENTITYDBL;
}
}
if (evenmorebits&0xff00)
evenmorebits |= U_FTE_YETMORE;
if (evenmorebits&0x00ff)
bits |= U_FTE_EVENMORE;
if (bits & 511)
bits |= U_MOREBITS;
#endif
if (to->number >= MAX_EDICTS)
{
/*SV_Error*/
Con_Printf ("Entity number >= MAX_EDICTS (%d), set to MAX_EDICTS - 1\n", MAX_EDICTS);
to->number = MAX_EDICTS - 1;
}
#ifdef PROTOCOL_VERSION_FTE
if (evenmorebits && (fte_extensions & required_extensions) != required_extensions) {
return;
}
#endif
if (!bits && !force) {
return; // nothing to send!
}
i = (to->number & U_CHECKMOREBITS) | (bits&~U_CHECKMOREBITS);
if (i & U_REMOVE)
Sys_Error ("U_REMOVE");
MSG_WriteShort (msg, i);
if (bits & U_MOREBITS)
MSG_WriteByte (msg, bits&255);
#ifdef PROTOCOL_VERSION_FTE
if (bits & U_FTE_EVENMORE)
MSG_WriteByte (msg, evenmorebits&255);
if (evenmorebits & U_FTE_YETMORE)
MSG_WriteByte (msg, (evenmorebits>>8)&255);
#endif
if (bits & U_MODEL)
MSG_WriteByte (msg, to->modelindex & 255);
if (bits & U_FRAME)
MSG_WriteByte (msg, to->frame);
if (bits & U_COLORMAP)
MSG_WriteByte (msg, to->colormap);
if (bits & U_SKIN)
MSG_WriteByte (msg, to->skinnum);
if (bits & U_EFFECTS)
MSG_WriteByte (msg, to->effects);
if (bits & U_ORIGIN1)
MSG_WriteCoord (msg, to->origin[0]);
if (bits & U_ANGLE1)
MSG_WriteAngle(msg, to->angles[0]);
if (bits & U_ORIGIN2)
MSG_WriteCoord (msg, to->origin[1]);
if (bits & U_ANGLE2)
MSG_WriteAngle(msg, to->angles[1]);
if (bits & U_ORIGIN3)
MSG_WriteCoord (msg, to->origin[2]);
if (bits & U_ANGLE3)
MSG_WriteAngle(msg, to->angles[2]);
}
/********************************** READING **********************************/
int msg_readcount;
qbool msg_badread;
void MSG_BeginReading (void)
{
msg_readcount = 0;
msg_badread = false;
}
int MSG_GetReadCount(void)
{
return msg_readcount;
}
// returns -1 and sets msg_badread if no more characters are available
int MSG_ReadChar (void)
{
int c;
if (msg_readcount + 1 > net_message.cursize) {
msg_badread = true;
return -1;
}
c = (signed char)net_message.data[msg_readcount];
msg_readcount++;
return c;
}
int MSG_ReadByte (void)
{
int c;
if (msg_readcount + 1 > net_message.cursize) {
msg_badread = true;
return -1;
}
c = (unsigned char)net_message.data[msg_readcount];
msg_readcount++;
return c;
}
int MSG_ReadShort (void)
{
int c;
if (msg_readcount + 2 > net_message.cursize) {
msg_badread = true;
return -1;
}
c = (short)(net_message.data[msg_readcount]
+ (net_message.data[msg_readcount + 1] << 8));
msg_readcount += 2;
return c;
}
int MSG_ReadLong (void)
{
int c;
if (msg_readcount + 4 > net_message.cursize) {
msg_badread = true;
return -1;
}
c = net_message.data[msg_readcount]
+ (net_message.data[msg_readcount + 1] << 8)
+ (net_message.data[msg_readcount + 2] << 16)
+ (net_message.data[msg_readcount + 3] << 24);
msg_readcount += 4;
return c;
}
float MSG_ReadFloat (void)
{
union {
byte b[4];
float f;
int l;
} dat;
dat.b[0] = net_message.data[msg_readcount];
dat.b[1] = net_message.data[msg_readcount + 1];
dat.b[2] = net_message.data[msg_readcount + 2];
dat.b[3] = net_message.data[msg_readcount + 3];
msg_readcount += 4;
dat.l = LittleLong (dat.l);
return dat.f;
}
char *MSG_ReadString (void)
{
static char string[2048];
unsigned int l;
int c;
l = 0;
do {
c = MSG_ReadByte ();
if (c == 255) // skip these to avoid security problems
continue; // with old clients and servers
if (c == -1 || c == 0) // msg_badread or end of string
break;
string[l] = c;
l++;
} while (l < sizeof(string)-1);
string[l] = 0;
return string;
}
char *MSG_ReadStringLine (void)
{
static char string[2048];
unsigned int l;
int c;
l = 0;
do {
c = MSG_ReadByte ();
if (c == 255)
continue;
if (c == -1 || c == 0 || c == '\n')
break;
string[l] = c;
l++;
} while (l < sizeof(string) - 1);
string[l] = 0;
return string;
}
float MSG_ReadCoord (void)
{
#ifdef FTE_PEXT_FLOATCOORDS
coorddata c = {0};
MSG_ReadData(&c, msg_coordsize);
return MSG_FromCoord(c, msg_coordsize);
#else // FTE_PEXT_FLOATCOORDS
return MSG_ReadShort() * (1.0 / 8);
#endif // FTE_PEXT_FLOATCOORDS
}
float MSG_ReadFloatCoord(void)
{
float f;
MSG_ReadData(&f, 4);
return LittleFloat(f);
}
void MSG_WriteLongCoord(sizebuf_t* sb, float f)
{
f = LittleFloat(f);
MSG_WriteFloat(sb, f);
}
float MSG_ReadAngle16 (void)
{
return MSG_ReadShort() * (360.0 / 65536);
}
float MSG_ReadAngle (void)
{
#ifdef FTE_PEXT_FLOATCOORDS
switch(msg_anglesize)
{
case 1:
return MSG_ReadChar() * (360.0/256);
case 2:
return MSG_ReadAngle16();
// case 4:
// return MSG_ReadFloat();
default:
Host_Error("MSG_ReadAngle: Bad angle size\n");
return 0;
}
#else // FTE_PEXT_FLOATCOORDS
return MSG_ReadChar() * (360.0 / 256);
#endif // FTE_PEXT_FLOATCOORDS
}
#define CM_MSEC (1 << 7) // same as CM_ANGLE2
void MSG_ReadDeltaUsercmd (usercmd_t *from, usercmd_t *move, int protoversion)
{
int bits;
memcpy (move, from, sizeof(*move));
bits = MSG_ReadByte ();
if (protoversion <= 26) {
// read current angles
if (bits & CM_ANGLE1)
move->angles[0] = MSG_ReadAngle16 ();
move->angles[1] = MSG_ReadAngle16 (); // always sent
if (bits & CM_ANGLE3)
move->angles[2] = MSG_ReadAngle16 ();
// read movement
if (bits & CM_FORWARD)
move->forwardmove = MSG_ReadChar() << 3;
if (bits & CM_SIDE)
move->sidemove = MSG_ReadChar() << 3;
if (bits & CM_UP)
move->upmove = MSG_ReadChar() << 3;
} else {
// read current angles
if (bits & CM_ANGLE1)
move->angles[0] = MSG_ReadAngle16 ();
if (bits & CM_ANGLE2)
move->angles[1] = MSG_ReadAngle16 ();
if (bits & CM_ANGLE3)
move->angles[2] = MSG_ReadAngle16 ();
// read movement
if (bits & CM_FORWARD)
move->forwardmove = MSG_ReadShort ();
if (bits & CM_SIDE)
move->sidemove = MSG_ReadShort ();
if (bits & CM_UP)
move->upmove = MSG_ReadShort ();
}
// read buttons
if (bits & CM_BUTTONS)
move->buttons = MSG_ReadByte ();
if (bits & CM_IMPULSE)
move->impulse = MSG_ReadByte ();
// read time to run command
if (protoversion <= 26 && (bits & CM_MSEC)) {
move->msec = MSG_ReadByte ();
}
else if (protoversion >= 27) {
move->msec = MSG_ReadByte (); // always sent
}
}
void MSG_ReadData (void *data, int len)
{
int i;
for (i = 0 ; i < len ; i++)
((byte *)data)[i] = MSG_ReadByte ();
}
void MSG_ReadSkip(int bytes)
{
for ( ; !msg_badread && bytes > 0; bytes--)
{
MSG_ReadByte ();
}
}