Quake II RTX doxygen  1.0 dev
msg.c
Go to the documentation of this file.
1 /*
2 Copyright (C) 1997-2001 Id Software, Inc.
3 
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
8 
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13 
14 You should have received a copy of the GNU General Public License along
15 with this program; if not, write to the Free Software Foundation, Inc.,
16 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 */
18 
19 #include "shared/shared.h"
20 #include "common/msg.h"
21 #include "common/protocol.h"
22 #include "common/sizebuf.h"
23 #include "common/math.h"
24 
25 /*
26 ==============================================================================
27 
28  MESSAGE IO FUNCTIONS
29 
30 Handles byte ordering and avoids alignment errors
31 ==============================================================================
32 */
33 
34 sizebuf_t msg_write;
35 byte msg_write_buffer[MAX_MSGLEN];
36 
37 sizebuf_t msg_read;
38 byte msg_read_buffer[MAX_MSGLEN];
39 
40 const entity_packed_t nullEntityState;
41 const player_packed_t nullPlayerState;
42 const usercmd_t nullUserCmd;
43 
44 /*
45 =============
46 MSG_Init
47 
48 Initialize default buffers, clearing allow overflow/underflow flags.
49 
50 This is the only place where writing buffer is initialized. Writing buffer is
51 never allowed to overflow.
52 
53 Reading buffer is reinitialized in many other places. Reinitializing will set
54 the allow underflow flag as appropriate.
55 =============
56 */
57 void MSG_Init(void)
58 {
59  SZ_TagInit(&msg_read, msg_read_buffer, MAX_MSGLEN, SZ_MSG_READ);
60  SZ_TagInit(&msg_write, msg_write_buffer, MAX_MSGLEN, SZ_MSG_WRITE);
61 }
62 
63 
64 /*
65 ==============================================================================
66 
67  WRITING
68 
69 ==============================================================================
70 */
71 
72 /*
73 =============
74 MSG_BeginWriting
75 =============
76 */
77 void MSG_BeginWriting(void)
78 {
79  msg_write.cursize = 0;
80  msg_write.bitpos = 0;
81  msg_write.overflowed = qfalse;
82 }
83 
84 /*
85 =============
86 MSG_WriteChar
87 =============
88 */
89 void MSG_WriteChar(int c)
90 {
91  byte *buf;
92 
93 #ifdef PARANOID
94  if (c < -128 || c > 127)
95  Com_Error(ERR_FATAL, "MSG_WriteChar: range error");
96 #endif
97 
98  buf = SZ_GetSpace(&msg_write, 1);
99  buf[0] = c;
100 }
101 
102 /*
103 =============
104 MSG_WriteByte
105 =============
106 */
107 void MSG_WriteByte(int c)
108 {
109  byte *buf;
110 
111 #ifdef PARANOID
112  if (c < 0 || c > 255)
113  Com_Error(ERR_FATAL, "MSG_WriteByte: range error");
114 #endif
115 
116  buf = SZ_GetSpace(&msg_write, 1);
117  buf[0] = c;
118 }
119 
120 /*
121 =============
122 MSG_WriteShort
123 =============
124 */
125 void MSG_WriteShort(int c)
126 {
127  byte *buf;
128 
129 #ifdef PARANOID
130  if (c < ((short)0x8000) || c > (short)0x7fff)
131  Com_Error(ERR_FATAL, "MSG_WriteShort: range error");
132 #endif
133 
134  buf = SZ_GetSpace(&msg_write, 2);
135  buf[0] = c & 0xff;
136  buf[1] = c >> 8;
137 }
138 
139 /*
140 =============
141 MSG_WriteLong
142 =============
143 */
144 void MSG_WriteLong(int c)
145 {
146  byte *buf;
147 
148  buf = SZ_GetSpace(&msg_write, 4);
149  buf[0] = c & 0xff;
150  buf[1] = (c >> 8) & 0xff;
151  buf[2] = (c >> 16) & 0xff;
152  buf[3] = c >> 24;
153 }
154 
155 /*
156 =============
157 MSG_WriteString
158 =============
159 */
160 void MSG_WriteString(const char *string)
161 {
162  size_t length;
163 
164  if (!string) {
165  MSG_WriteByte(0);
166  return;
167  }
168 
169  length = strlen(string);
170  if (length >= MAX_NET_STRING) {
171  Com_WPrintf("%s: overflow: %"PRIz" chars", __func__, length);
172  MSG_WriteByte(0);
173  return;
174  }
175 
176  MSG_WriteData(string, length + 1);
177 }
178 
179 /*
180 =============
181 MSG_WriteCoord
182 =============
183 */
184 
185 #define COORD2SHORT(x) ((int)((x)*8.0f))
186 #define SHORT2COORD(x) ((x)*(1.0f/8))
187 
188 static inline void MSG_WriteCoord(float f)
189 {
191 }
192 
193 /*
194 =============
195 MSG_WritePos
196 =============
197 */
198 void MSG_WritePos(const vec3_t pos)
199 {
200  MSG_WriteCoord(pos[0]);
201  MSG_WriteCoord(pos[1]);
202  MSG_WriteCoord(pos[2]);
203 }
204 
205 /*
206 =============
207 MSG_WriteAngle
208 =============
209 */
210 
211 #define ANGLE2BYTE(x) ((int)((x)*256.0f/360)&255)
212 #define BYTE2ANGLE(x) ((x)*(360.0f/256))
213 
214 void MSG_WriteAngle(float f)
215 {
217 }
218 
219 #if USE_CLIENT
220 
221 /*
222 =============
223 MSG_WriteDeltaUsercmd
224 =============
225 */
226 int MSG_WriteDeltaUsercmd(const usercmd_t *from, const usercmd_t *cmd, int version)
227 {
228  int bits, buttons = cmd->buttons & BUTTON_MASK;
229 
230  if (!from) {
231  from = &nullUserCmd;
232  }
233 
234 //
235 // send the movement message
236 //
237  bits = 0;
238  if (cmd->angles[0] != from->angles[0])
239  bits |= CM_ANGLE1;
240  if (cmd->angles[1] != from->angles[1])
241  bits |= CM_ANGLE2;
242  if (cmd->angles[2] != from->angles[2])
243  bits |= CM_ANGLE3;
244  if (cmd->forwardmove != from->forwardmove)
245  bits |= CM_FORWARD;
246  if (cmd->sidemove != from->sidemove)
247  bits |= CM_SIDE;
248  if (cmd->upmove != from->upmove)
249  bits |= CM_UP;
250  if (cmd->buttons != from->buttons)
251  bits |= CM_BUTTONS;
252  if (cmd->impulse != from->impulse)
253  bits |= CM_IMPULSE;
254 
255  MSG_WriteByte(bits);
256 
257  if (version >= PROTOCOL_VERSION_R1Q2_UCMD) {
258  if (bits & CM_BUTTONS) {
259  if ((bits & CM_FORWARD) && !(cmd->forwardmove % 5)) {
260  buttons |= BUTTON_FORWARD;
261  }
262  if ((bits & CM_SIDE) && !(cmd->sidemove % 5)) {
263  buttons |= BUTTON_SIDE;
264  }
265  if ((bits & CM_UP) && !(cmd->upmove % 5)) {
266  buttons |= BUTTON_UP;
267  }
268  MSG_WriteByte(buttons);
269  }
270  }
271 
272  if (bits & CM_ANGLE1)
273  MSG_WriteShort(cmd->angles[0]);
274  if (bits & CM_ANGLE2)
275  MSG_WriteShort(cmd->angles[1]);
276  if (bits & CM_ANGLE3)
277  MSG_WriteShort(cmd->angles[2]);
278 
279  if (bits & CM_FORWARD) {
280  if (buttons & BUTTON_FORWARD) {
281  MSG_WriteChar(cmd->forwardmove / 5);
282  } else {
283  MSG_WriteShort(cmd->forwardmove);
284  }
285  }
286  if (bits & CM_SIDE) {
287  if (buttons & BUTTON_SIDE) {
288  MSG_WriteChar(cmd->sidemove / 5);
289  } else {
290  MSG_WriteShort(cmd->sidemove);
291  }
292  }
293  if (bits & CM_UP) {
294  if (buttons & BUTTON_UP) {
295  MSG_WriteChar(cmd->upmove / 5);
296  } else {
297  MSG_WriteShort(cmd->upmove);
298  }
299  }
300 
301  if (version < PROTOCOL_VERSION_R1Q2_UCMD) {
302  if (bits & CM_BUTTONS)
303  MSG_WriteByte(cmd->buttons);
304  }
305  if (bits & CM_IMPULSE)
306  MSG_WriteByte(cmd->impulse);
307 
308  MSG_WriteByte(cmd->msec);
309 
310  return bits;
311 }
312 
313 /*
314 =============
315 MSG_WriteBits
316 =============
317 */
318 void MSG_WriteBits(int value, int bits)
319 {
320  int i;
321  size_t bitpos;
322 
323  if (bits == 0 || bits < -31 || bits > 32) {
324  Com_Error(ERR_FATAL, "MSG_WriteBits: bad bits: %d", bits);
325  }
326 
327  if (msg_write.maxsize - msg_write.cursize < 4) {
328  Com_Error(ERR_FATAL, "MSG_WriteBits: overflow");
329  }
330 
331  if (bits < 0) {
332  bits = -bits;
333  }
334 
335  bitpos = msg_write.bitpos;
336  if ((bitpos & 7) == 0) {
337  // optimized case
338  switch (bits) {
339  case 8:
340  MSG_WriteByte(value);
341  return;
342  case 16:
343  MSG_WriteShort(value);
344  return;
345  case 32:
346  MSG_WriteLong(value);
347  return;
348  default:
349  break;
350  }
351  }
352  for (i = 0; i < bits; i++, bitpos++) {
353  if ((bitpos & 7) == 0) {
354  msg_write.data[bitpos >> 3] = 0;
355  }
356  msg_write.data[bitpos >> 3] |= (value & 1) << (bitpos & 7);
357  value >>= 1;
358  }
359  msg_write.bitpos = bitpos;
360  msg_write.cursize = (bitpos + 7) >> 3;
361 }
362 
363 /*
364 =============
365 MSG_WriteDeltaUsercmd_Enhanced
366 =============
367 */
368 int MSG_WriteDeltaUsercmd_Enhanced(const usercmd_t *from,
369  const usercmd_t *cmd,
370  int version)
371 {
372  int bits, delta, count;
373 
374  if (!from) {
375  from = &nullUserCmd;
376  }
377 
378 //
379 // send the movement message
380 //
381  bits = 0;
382  if (cmd->angles[0] != from->angles[0])
383  bits |= CM_ANGLE1;
384  if (cmd->angles[1] != from->angles[1])
385  bits |= CM_ANGLE2;
386  if (cmd->angles[2] != from->angles[2])
387  bits |= CM_ANGLE3;
388  if (cmd->forwardmove != from->forwardmove)
389  bits |= CM_FORWARD;
390  if (cmd->sidemove != from->sidemove)
391  bits |= CM_SIDE;
392  if (cmd->upmove != from->upmove)
393  bits |= CM_UP;
394  if (cmd->buttons != from->buttons)
395  bits |= CM_BUTTONS;
396  if (cmd->msec != from->msec)
397  bits |= CM_IMPULSE;
398 
399  if (!bits) {
400  MSG_WriteBits(0, 1);
401  return 0;
402  }
403 
404  MSG_WriteBits(1, 1);
405  MSG_WriteBits(bits, 8);
406 
407  if (bits & CM_ANGLE1) {
408  delta = cmd->angles[0] - from->angles[0];
409  if (delta >= -128 && delta <= 127) {
410  MSG_WriteBits(1, 1);
411  MSG_WriteBits(delta, -8);
412  } else {
413  MSG_WriteBits(0, 1);
414  MSG_WriteBits(cmd->angles[0], -16);
415  }
416  }
417  if (bits & CM_ANGLE2) {
418  delta = cmd->angles[1] - from->angles[1];
419  if (delta >= -128 && delta <= 127) {
420  MSG_WriteBits(1, 1);
421  MSG_WriteBits(delta, -8);
422  } else {
423  MSG_WriteBits(0, 1);
424  MSG_WriteBits(cmd->angles[1], -16);
425  }
426  }
427  if (bits & CM_ANGLE3) {
428  MSG_WriteBits(cmd->angles[2], -16);
429  }
430 
431  if (version >= PROTOCOL_VERSION_Q2PRO_UCMD) {
432  count = -10;
433  } else {
434  count = -16;
435  }
436 
437  if (bits & CM_FORWARD) {
438  MSG_WriteBits(cmd->forwardmove, count);
439  }
440  if (bits & CM_SIDE) {
441  MSG_WriteBits(cmd->sidemove, count);
442  }
443  if (bits & CM_UP) {
444  MSG_WriteBits(cmd->upmove, count);
445  }
446 
447  if (bits & CM_BUTTONS) {
448  int buttons = (cmd->buttons & 3) | (cmd->buttons >> 5);
449  MSG_WriteBits(buttons, 3);
450  }
451  if (bits & CM_IMPULSE) {
452  MSG_WriteBits(cmd->msec, 8);
453  }
454 
455  return bits;
456 }
457 
458 #endif // USE_CLIENT
459 
460 void MSG_WriteDir(const vec3_t dir)
461 {
462  int best;
463 
464  best = DirToByte(dir);
465  MSG_WriteByte(best);
466 }
467 
468 void MSG_PackEntity(entity_packed_t *out, const entity_state_t *in, qboolean short_angles)
469 {
470  // allow 0 to accomodate empty baselines
471  if (in->number < 0 || in->number >= MAX_EDICTS)
472  Com_Error(ERR_DROP, "%s: bad number: %d", __func__, in->number);
473 
474  out->number = in->number;
475  out->origin[0] = COORD2SHORT(in->origin[0]);
476  out->origin[1] = COORD2SHORT(in->origin[1]);
477  out->origin[2] = COORD2SHORT(in->origin[2]);
478  if (short_angles) {
479  out->angles[0] = ANGLE2SHORT(in->angles[0]);
480  out->angles[1] = ANGLE2SHORT(in->angles[1]);
481  out->angles[2] = ANGLE2SHORT(in->angles[2]);
482  } else {
483  // pack angles8 akin to angles16 to make delta compression happy when
484  // precision suddenly changes between entity updates
485  out->angles[0] = ANGLE2BYTE(in->angles[0]) << 8;
486  out->angles[1] = ANGLE2BYTE(in->angles[1]) << 8;
487  out->angles[2] = ANGLE2BYTE(in->angles[2]) << 8;
488  }
489  out->old_origin[0] = COORD2SHORT(in->old_origin[0]);
490  out->old_origin[1] = COORD2SHORT(in->old_origin[1]);
491  out->old_origin[2] = COORD2SHORT(in->old_origin[2]);
492  out->modelindex = in->modelindex;
493  out->modelindex2 = in->modelindex2;
494  out->modelindex3 = in->modelindex3;
495  out->modelindex4 = in->modelindex4;
496  out->skinnum = in->skinnum;
497  out->effects = in->effects;
498  out->renderfx = in->renderfx;
499  out->solid = in->solid;
500  out->frame = in->frame;
501  out->sound = in->sound;
502  out->event = in->event;
503 }
504 
505 void MSG_WriteDeltaEntity(const entity_packed_t *from,
506  const entity_packed_t *to,
507  msgEsFlags_t flags)
508 {
509  uint32_t bits, mask;
510 
511  if (!to) {
512  if (!from)
513  Com_Error(ERR_DROP, "%s: NULL", __func__);
514 
515  if (from->number < 1 || from->number >= MAX_EDICTS)
516  Com_Error(ERR_DROP, "%s: bad number: %d", __func__, from->number);
517 
518  bits = U_REMOVE;
519  if (from->number & 0xff00)
520  bits |= U_NUMBER16 | U_MOREBITS1;
521 
522  MSG_WriteByte(bits & 255);
523  if (bits & 0x0000ff00)
524  MSG_WriteByte((bits >> 8) & 255);
525 
526  if (bits & U_NUMBER16)
527  MSG_WriteShort(from->number);
528  else
529  MSG_WriteByte(from->number);
530 
531  return; // remove entity
532  }
533 
534  if (to->number < 1 || to->number >= MAX_EDICTS)
535  Com_Error(ERR_DROP, "%s: bad number: %d", __func__, to->number);
536 
537  if (!from)
538  from = &nullEntityState;
539 
540 // send an update
541  bits = 0;
542 
543  if (!(flags & MSG_ES_FIRSTPERSON)) {
544  if (to->origin[0] != from->origin[0])
545  bits |= U_ORIGIN1;
546  if (to->origin[1] != from->origin[1])
547  bits |= U_ORIGIN2;
548  if (to->origin[2] != from->origin[2])
549  bits |= U_ORIGIN3;
550 
551  if (flags & MSG_ES_SHORTANGLES) {
552  if (to->angles[0] != from->angles[0])
553  bits |= U_ANGLE1 | U_ANGLE16;
554  if (to->angles[1] != from->angles[1])
555  bits |= U_ANGLE2 | U_ANGLE16;
556  if (to->angles[2] != from->angles[2])
557  bits |= U_ANGLE3 | U_ANGLE16;
558  } else {
559  if (to->angles[0] != from->angles[0])
560  bits |= U_ANGLE1;
561  if (to->angles[1] != from->angles[1])
562  bits |= U_ANGLE2;
563  if (to->angles[2] != from->angles[2])
564  bits |= U_ANGLE3;
565  }
566 
567  if (flags & MSG_ES_NEWENTITY) {
568  if (to->old_origin[0] != from->origin[0] ||
569  to->old_origin[1] != from->origin[1] ||
570  to->old_origin[2] != from->origin[2])
571  bits |= U_OLDORIGIN;
572  }
573  }
574 
575  if (flags & MSG_ES_UMASK)
576  mask = 0xffff0000;
577  else
578  mask = 0xffff8000; // don't confuse old clients
579 
580  if (to->skinnum != from->skinnum) {
581  if (to->skinnum & mask)
582  bits |= U_SKIN8 | U_SKIN16;
583  else if (to->skinnum & 0x0000ff00)
584  bits |= U_SKIN16;
585  else
586  bits |= U_SKIN8;
587  }
588 
589  if (to->frame != from->frame) {
590  if (to->frame & 0xff00)
591  bits |= U_FRAME16;
592  else
593  bits |= U_FRAME8;
594  }
595 
596  if (to->effects != from->effects) {
597  if (to->effects & mask)
598  bits |= U_EFFECTS8 | U_EFFECTS16;
599  else if (to->effects & 0x0000ff00)
600  bits |= U_EFFECTS16;
601  else
602  bits |= U_EFFECTS8;
603  }
604 
605  if (to->renderfx != from->renderfx) {
606  if (to->renderfx & mask)
607  bits |= U_RENDERFX8 | U_RENDERFX16;
608  else if (to->renderfx & 0x0000ff00)
609  bits |= U_RENDERFX16;
610  else
611  bits |= U_RENDERFX8;
612  }
613 
614  if (to->solid != from->solid)
615  bits |= U_SOLID;
616 
617  // event is not delta compressed, just 0 compressed
618  if (to->event)
619  bits |= U_EVENT;
620 
621  if (to->modelindex != from->modelindex)
622  bits |= U_MODEL;
623  if (to->modelindex2 != from->modelindex2)
624  bits |= U_MODEL2;
625  if (to->modelindex3 != from->modelindex3)
626  bits |= U_MODEL3;
627  if (to->modelindex4 != from->modelindex4)
628  bits |= U_MODEL4;
629 
630  if (to->sound != from->sound)
631  bits |= U_SOUND;
632 
633  if (to->renderfx & RF_FRAMELERP) {
634  bits |= U_OLDORIGIN;
635  } else if (to->renderfx & RF_BEAM) {
636  if (flags & MSG_ES_BEAMORIGIN) {
637  if (to->old_origin[0] != from->old_origin[0] ||
638  to->old_origin[1] != from->old_origin[1] ||
639  to->old_origin[2] != from->old_origin[2])
640  bits |= U_OLDORIGIN;
641  } else {
642  bits |= U_OLDORIGIN;
643  }
644  }
645 
646  //
647  // write the message
648  //
649  if (!bits && !(flags & MSG_ES_FORCE))
650  return; // nothing to send!
651 
652  if (flags & MSG_ES_REMOVE)
653  bits |= U_REMOVE; // used for MVD stream only
654 
655  //----------
656 
657  if (to->number & 0xff00)
658  bits |= U_NUMBER16; // number8 is implicit otherwise
659 
660  if (bits & 0xff000000)
661  bits |= U_MOREBITS3 | U_MOREBITS2 | U_MOREBITS1;
662  else if (bits & 0x00ff0000)
663  bits |= U_MOREBITS2 | U_MOREBITS1;
664  else if (bits & 0x0000ff00)
665  bits |= U_MOREBITS1;
666 
667  MSG_WriteByte(bits & 255);
668 
669  if (bits & 0xff000000) {
670  MSG_WriteByte((bits >> 8) & 255);
671  MSG_WriteByte((bits >> 16) & 255);
672  MSG_WriteByte((bits >> 24) & 255);
673  } else if (bits & 0x00ff0000) {
674  MSG_WriteByte((bits >> 8) & 255);
675  MSG_WriteByte((bits >> 16) & 255);
676  } else if (bits & 0x0000ff00) {
677  MSG_WriteByte((bits >> 8) & 255);
678  }
679 
680  //----------
681 
682  if (bits & U_NUMBER16)
683  MSG_WriteShort(to->number);
684  else
685  MSG_WriteByte(to->number);
686 
687  if (bits & U_MODEL)
688  MSG_WriteByte(to->modelindex);
689  if (bits & U_MODEL2)
690  MSG_WriteByte(to->modelindex2);
691  if (bits & U_MODEL3)
692  MSG_WriteByte(to->modelindex3);
693  if (bits & U_MODEL4)
694  MSG_WriteByte(to->modelindex4);
695 
696  if (bits & U_FRAME8)
697  MSG_WriteByte(to->frame);
698  else if (bits & U_FRAME16)
699  MSG_WriteShort(to->frame);
700 
701  if ((bits & (U_SKIN8 | U_SKIN16)) == (U_SKIN8 | U_SKIN16)) //used for laser colors
702  MSG_WriteLong(to->skinnum);
703  else if (bits & U_SKIN8)
704  MSG_WriteByte(to->skinnum);
705  else if (bits & U_SKIN16)
706  MSG_WriteShort(to->skinnum);
707 
708  if ((bits & (U_EFFECTS8 | U_EFFECTS16)) == (U_EFFECTS8 | U_EFFECTS16))
709  MSG_WriteLong(to->effects);
710  else if (bits & U_EFFECTS8)
711  MSG_WriteByte(to->effects);
712  else if (bits & U_EFFECTS16)
713  MSG_WriteShort(to->effects);
714 
715  if ((bits & (U_RENDERFX8 | U_RENDERFX16)) == (U_RENDERFX8 | U_RENDERFX16))
716  MSG_WriteLong(to->renderfx);
717  else if (bits & U_RENDERFX8)
718  MSG_WriteByte(to->renderfx);
719  else if (bits & U_RENDERFX16)
720  MSG_WriteShort(to->renderfx);
721 
722  if (bits & U_ORIGIN1)
723  MSG_WriteShort(to->origin[0]);
724  if (bits & U_ORIGIN2)
725  MSG_WriteShort(to->origin[1]);
726  if (bits & U_ORIGIN3)
727  MSG_WriteShort(to->origin[2]);
728 
729  if ((flags & MSG_ES_SHORTANGLES) && (bits & U_ANGLE16)) {
730  if (bits & U_ANGLE1)
731  MSG_WriteShort(to->angles[0]);
732  if (bits & U_ANGLE2)
733  MSG_WriteShort(to->angles[1]);
734  if (bits & U_ANGLE3)
735  MSG_WriteShort(to->angles[2]);
736  } else {
737  if (bits & U_ANGLE1)
738  MSG_WriteByte(to->angles[0] >> 8);
739  if (bits & U_ANGLE2)
740  MSG_WriteByte(to->angles[1] >> 8);
741  if (bits & U_ANGLE3)
742  MSG_WriteByte(to->angles[2] >> 8);
743  }
744 
745  if (bits & U_OLDORIGIN) {
746  MSG_WriteShort(to->old_origin[0]);
747  MSG_WriteShort(to->old_origin[1]);
748  MSG_WriteShort(to->old_origin[2]);
749  }
750 
751  if (bits & U_SOUND)
752  MSG_WriteByte(to->sound);
753  if (bits & U_EVENT)
754  MSG_WriteByte(to->event);
755  if (bits & U_SOLID) {
756  if (flags & MSG_ES_LONGSOLID)
757  MSG_WriteLong(to->solid);
758  else
759  MSG_WriteShort(to->solid);
760  }
761 }
762 
763 void MSG_PackPlayer(player_packed_t *out, const player_state_t *in)
764 {
765  int i;
766 
767  out->pmove = in->pmove;
768  out->viewangles[0] = ANGLE2SHORT(in->viewangles[0]);
769  out->viewangles[1] = ANGLE2SHORT(in->viewangles[1]);
770  out->viewangles[2] = ANGLE2SHORT(in->viewangles[2]);
771  out->viewoffset[0] = in->viewoffset[0] * 4;
772  out->viewoffset[1] = in->viewoffset[1] * 4;
773  out->viewoffset[2] = in->viewoffset[2] * 4;
774  out->kick_angles[0] = in->kick_angles[0] * 4;
775  out->kick_angles[1] = in->kick_angles[1] * 4;
776  out->kick_angles[2] = in->kick_angles[2] * 4;
777  out->gunoffset[0] = in->gunoffset[0] * 4;
778  out->gunoffset[1] = in->gunoffset[1] * 4;
779  out->gunoffset[2] = in->gunoffset[2] * 4;
780  out->gunangles[0] = in->gunangles[0] * 4;
781  out->gunangles[1] = in->gunangles[1] * 4;
782  out->gunangles[2] = in->gunangles[2] * 4;
783  out->gunindex = in->gunindex;
784  out->gunframe = in->gunframe;
785  out->blend[0] = in->blend[0] * 255;
786  out->blend[1] = in->blend[1] * 255;
787  out->blend[2] = in->blend[2] * 255;
788  out->blend[3] = in->blend[3] * 255;
789  out->fov = in->fov;
790  out->rdflags = in->rdflags;
791  for (i = 0; i < MAX_STATS; i++)
792  out->stats[i] = in->stats[i];
793 }
794 
795 void MSG_WriteDeltaPlayerstate_Default(const player_packed_t *from, const player_packed_t *to)
796 {
797  int i;
798  int pflags;
799  int statbits;
800 
801  if (!to)
802  Com_Error(ERR_DROP, "%s: NULL", __func__);
803 
804  if (!from)
805  from = &nullPlayerState;
806 
807  //
808  // determine what needs to be sent
809  //
810  pflags = 0;
811 
812  if (to->pmove.pm_type != from->pmove.pm_type)
813  pflags |= PS_M_TYPE;
814 
815  if (to->pmove.origin[0] != from->pmove.origin[0] ||
816  to->pmove.origin[1] != from->pmove.origin[1] ||
817  to->pmove.origin[2] != from->pmove.origin[2])
818  pflags |= PS_M_ORIGIN;
819 
820  if (to->pmove.velocity[0] != from->pmove.velocity[0] ||
821  to->pmove.velocity[1] != from->pmove.velocity[1] ||
822  to->pmove.velocity[2] != from->pmove.velocity[2])
823  pflags |= PS_M_VELOCITY;
824 
825  if (to->pmove.pm_time != from->pmove.pm_time)
826  pflags |= PS_M_TIME;
827 
828  if (to->pmove.pm_flags != from->pmove.pm_flags)
829  pflags |= PS_M_FLAGS;
830 
831  if (to->pmove.gravity != from->pmove.gravity)
832  pflags |= PS_M_GRAVITY;
833 
834  if (to->pmove.delta_angles[0] != from->pmove.delta_angles[0] ||
835  to->pmove.delta_angles[1] != from->pmove.delta_angles[1] ||
836  to->pmove.delta_angles[2] != from->pmove.delta_angles[2])
837  pflags |= PS_M_DELTA_ANGLES;
838 
839  if (to->viewoffset[0] != from->viewoffset[0] ||
840  to->viewoffset[1] != from->viewoffset[1] ||
841  to->viewoffset[2] != from->viewoffset[2])
842  pflags |= PS_VIEWOFFSET;
843 
844  if (to->viewangles[0] != from->viewangles[0] ||
845  to->viewangles[1] != from->viewangles[1] ||
846  to->viewangles[2] != from->viewangles[2])
847  pflags |= PS_VIEWANGLES;
848 
849  if (to->kick_angles[0] != from->kick_angles[0] ||
850  to->kick_angles[1] != from->kick_angles[1] ||
851  to->kick_angles[2] != from->kick_angles[2])
852  pflags |= PS_KICKANGLES;
853 
854  if (to->blend[0] != from->blend[0] ||
855  to->blend[1] != from->blend[1] ||
856  to->blend[2] != from->blend[2] ||
857  to->blend[3] != from->blend[3])
858  pflags |= PS_BLEND;
859 
860  if (to->fov != from->fov)
861  pflags |= PS_FOV;
862 
863  if (to->rdflags != from->rdflags)
864  pflags |= PS_RDFLAGS;
865 
866  if (to->gunframe != from->gunframe ||
867  to->gunoffset[0] != from->gunoffset[0] ||
868  to->gunoffset[1] != from->gunoffset[1] ||
869  to->gunoffset[2] != from->gunoffset[2] ||
870  to->gunangles[0] != from->gunangles[0] ||
871  to->gunangles[1] != from->gunangles[1] ||
872  to->gunangles[2] != from->gunangles[2])
873  pflags |= PS_WEAPONFRAME;
874 
875  if (to->gunindex != from->gunindex)
876  pflags |= PS_WEAPONINDEX;
877 
878  //
879  // write it
880  //
881  MSG_WriteShort(pflags);
882 
883  //
884  // write the pmove_state_t
885  //
886  if (pflags & PS_M_TYPE)
887  MSG_WriteByte(to->pmove.pm_type);
888 
889  if (pflags & PS_M_ORIGIN) {
890  MSG_WriteShort(to->pmove.origin[0]);
891  MSG_WriteShort(to->pmove.origin[1]);
892  MSG_WriteShort(to->pmove.origin[2]);
893  }
894 
895  if (pflags & PS_M_VELOCITY) {
896  MSG_WriteShort(to->pmove.velocity[0]);
897  MSG_WriteShort(to->pmove.velocity[1]);
898  MSG_WriteShort(to->pmove.velocity[2]);
899  }
900 
901  if (pflags & PS_M_TIME)
902  MSG_WriteByte(to->pmove.pm_time);
903 
904  if (pflags & PS_M_FLAGS)
905  MSG_WriteByte(to->pmove.pm_flags);
906 
907  if (pflags & PS_M_GRAVITY)
908  MSG_WriteShort(to->pmove.gravity);
909 
910  if (pflags & PS_M_DELTA_ANGLES) {
911  MSG_WriteShort(to->pmove.delta_angles[0]);
912  MSG_WriteShort(to->pmove.delta_angles[1]);
913  MSG_WriteShort(to->pmove.delta_angles[2]);
914  }
915 
916  //
917  // write the rest of the player_state_t
918  //
919  if (pflags & PS_VIEWOFFSET) {
920  MSG_WriteChar(to->viewoffset[0]);
921  MSG_WriteChar(to->viewoffset[1]);
922  MSG_WriteChar(to->viewoffset[2]);
923  }
924 
925  if (pflags & PS_VIEWANGLES) {
926  MSG_WriteShort(to->viewangles[0]);
927  MSG_WriteShort(to->viewangles[1]);
928  MSG_WriteShort(to->viewangles[2]);
929  }
930 
931  if (pflags & PS_KICKANGLES) {
932  MSG_WriteChar(to->kick_angles[0]);
933  MSG_WriteChar(to->kick_angles[1]);
934  MSG_WriteChar(to->kick_angles[2]);
935  }
936 
937  if (pflags & PS_WEAPONINDEX)
938  MSG_WriteByte(to->gunindex);
939 
940  if (pflags & PS_WEAPONFRAME) {
941  MSG_WriteByte(to->gunframe);
942  MSG_WriteChar(to->gunoffset[0]);
943  MSG_WriteChar(to->gunoffset[1]);
944  MSG_WriteChar(to->gunoffset[2]);
945  MSG_WriteChar(to->gunangles[0]);
946  MSG_WriteChar(to->gunangles[1]);
947  MSG_WriteChar(to->gunangles[2]);
948  }
949 
950  if (pflags & PS_BLEND) {
951  MSG_WriteByte(to->blend[0]);
952  MSG_WriteByte(to->blend[1]);
953  MSG_WriteByte(to->blend[2]);
954  MSG_WriteByte(to->blend[3]);
955  }
956 
957  if (pflags & PS_FOV)
958  MSG_WriteByte(to->fov);
959 
960  if (pflags & PS_RDFLAGS)
961  MSG_WriteByte(to->rdflags);
962 
963  // send stats
964  statbits = 0;
965  for (i = 0; i < MAX_STATS; i++)
966  if (to->stats[i] != from->stats[i])
967  statbits |= 1 << i;
968 
969  MSG_WriteLong(statbits);
970  for (i = 0; i < MAX_STATS; i++)
971  if (statbits & (1 << i))
972  MSG_WriteShort(to->stats[i]);
973 }
974 
975 int MSG_WriteDeltaPlayerstate_Enhanced(const player_packed_t *from,
976  player_packed_t *to,
977  msgPsFlags_t flags)
978 {
979  int i;
980  int pflags, eflags;
981  int statbits;
982 
983  if (!to)
984  Com_Error(ERR_DROP, "%s: NULL", __func__);
985 
986  if (!from)
987  from = &nullPlayerState;
988 
989  //
990  // determine what needs to be sent
991  //
992  pflags = 0;
993  eflags = 0;
994 
995  if (to->pmove.pm_type != from->pmove.pm_type)
996  pflags |= PS_M_TYPE;
997 
998  if (to->pmove.origin[0] != from->pmove.origin[0] ||
999  to->pmove.origin[1] != from->pmove.origin[1])
1000  pflags |= PS_M_ORIGIN;
1001 
1002  if (to->pmove.origin[2] != from->pmove.origin[2])
1003  eflags |= EPS_M_ORIGIN2;
1004 
1005  if (!(flags & MSG_PS_IGNORE_PREDICTION)) {
1006  if (to->pmove.velocity[0] != from->pmove.velocity[0] ||
1007  to->pmove.velocity[1] != from->pmove.velocity[1])
1008  pflags |= PS_M_VELOCITY;
1009 
1010  if (to->pmove.velocity[2] != from->pmove.velocity[2])
1011  eflags |= EPS_M_VELOCITY2;
1012 
1013  if (to->pmove.pm_time != from->pmove.pm_time)
1014  pflags |= PS_M_TIME;
1015 
1016  if (to->pmove.pm_flags != from->pmove.pm_flags)
1017  pflags |= PS_M_FLAGS;
1018 
1019  if (to->pmove.gravity != from->pmove.gravity)
1020  pflags |= PS_M_GRAVITY;
1021  } else {
1022  // save previous state
1023  VectorCopy(from->pmove.velocity, to->pmove.velocity);
1024  to->pmove.pm_time = from->pmove.pm_time;
1025  to->pmove.pm_flags = from->pmove.pm_flags;
1026  to->pmove.gravity = from->pmove.gravity;
1027  }
1028 
1029  if (!(flags & MSG_PS_IGNORE_DELTAANGLES)) {
1030  if (to->pmove.delta_angles[0] != from->pmove.delta_angles[0] ||
1031  to->pmove.delta_angles[1] != from->pmove.delta_angles[1] ||
1032  to->pmove.delta_angles[2] != from->pmove.delta_angles[2])
1033  pflags |= PS_M_DELTA_ANGLES;
1034  } else {
1035  // save previous state
1036  VectorCopy(from->pmove.delta_angles, to->pmove.delta_angles);
1037  }
1038 
1039  if (from->viewoffset[0] != to->viewoffset[0] ||
1040  from->viewoffset[1] != to->viewoffset[1] ||
1041  from->viewoffset[2] != to->viewoffset[2])
1042  pflags |= PS_VIEWOFFSET;
1043 
1044  if (!(flags & MSG_PS_IGNORE_VIEWANGLES)) {
1045  if (from->viewangles[0] != to->viewangles[0] ||
1046  from->viewangles[1] != to->viewangles[1])
1047  pflags |= PS_VIEWANGLES;
1048 
1049  if (from->viewangles[2] != to->viewangles[2])
1050  eflags |= EPS_VIEWANGLE2;
1051  } else {
1052  // save previous state
1053  to->viewangles[0] = from->viewangles[0];
1054  to->viewangles[1] = from->viewangles[1];
1055  to->viewangles[2] = from->viewangles[2];
1056  }
1057 
1058  if (from->kick_angles[0] != to->kick_angles[0] ||
1059  from->kick_angles[1] != to->kick_angles[1] ||
1060  from->kick_angles[2] != to->kick_angles[2])
1061  pflags |= PS_KICKANGLES;
1062 
1063  if (!(flags & MSG_PS_IGNORE_BLEND)) {
1064  if (from->blend[0] != to->blend[0] ||
1065  from->blend[1] != to->blend[1] ||
1066  from->blend[2] != to->blend[2] ||
1067  from->blend[3] != to->blend[3])
1068  pflags |= PS_BLEND;
1069  } else {
1070  // save previous state
1071  to->blend[0] = from->blend[0];
1072  to->blend[1] = from->blend[1];
1073  to->blend[2] = from->blend[2];
1074  to->blend[3] = from->blend[3];
1075  }
1076 
1077  if (from->fov != to->fov)
1078  pflags |= PS_FOV;
1079 
1080  if (to->rdflags != from->rdflags)
1081  pflags |= PS_RDFLAGS;
1082 
1083  if (!(flags & MSG_PS_IGNORE_GUNINDEX)) {
1084  if (to->gunindex != from->gunindex)
1085  pflags |= PS_WEAPONINDEX;
1086  } else {
1087  // save previous state
1088  to->gunindex = from->gunindex;
1089  }
1090 
1091  if (!(flags & MSG_PS_IGNORE_GUNFRAMES)) {
1092  if (to->gunframe != from->gunframe)
1093  pflags |= PS_WEAPONFRAME;
1094 
1095  if (from->gunoffset[0] != to->gunoffset[0] ||
1096  from->gunoffset[1] != to->gunoffset[1] ||
1097  from->gunoffset[2] != to->gunoffset[2])
1098  eflags |= EPS_GUNOFFSET;
1099 
1100  if (from->gunangles[0] != to->gunangles[0] ||
1101  from->gunangles[1] != to->gunangles[1] ||
1102  from->gunangles[2] != to->gunangles[2])
1103  eflags |= EPS_GUNANGLES;
1104  } else {
1105  // save previous state
1106  to->gunframe = from->gunframe;
1107 
1108  to->gunoffset[0] = from->gunoffset[0];
1109  to->gunoffset[1] = from->gunoffset[1];
1110  to->gunoffset[2] = from->gunoffset[2];
1111 
1112  to->gunangles[0] = from->gunangles[0];
1113  to->gunangles[1] = from->gunangles[1];
1114  to->gunangles[2] = from->gunangles[2];
1115  }
1116 
1117  statbits = 0;
1118  for (i = 0; i < MAX_STATS; i++)
1119  if (to->stats[i] != from->stats[i])
1120  statbits |= 1 << i;
1121 
1122  if (statbits)
1123  eflags |= EPS_STATS;
1124 
1125  //
1126  // write it
1127  //
1128  MSG_WriteShort(pflags);
1129 
1130  //
1131  // write the pmove_state_t
1132  //
1133  if (pflags & PS_M_TYPE)
1134  MSG_WriteByte(to->pmove.pm_type);
1135 
1136  if (pflags & PS_M_ORIGIN) {
1137  MSG_WriteShort(to->pmove.origin[0]);
1138  MSG_WriteShort(to->pmove.origin[1]);
1139  }
1140 
1141  if (eflags & EPS_M_ORIGIN2)
1142  MSG_WriteShort(to->pmove.origin[2]);
1143 
1144  if (pflags & PS_M_VELOCITY) {
1145  MSG_WriteShort(to->pmove.velocity[0]);
1146  MSG_WriteShort(to->pmove.velocity[1]);
1147  }
1148 
1149  if (eflags & EPS_M_VELOCITY2)
1150  MSG_WriteShort(to->pmove.velocity[2]);
1151 
1152  if (pflags & PS_M_TIME)
1153  MSG_WriteByte(to->pmove.pm_time);
1154 
1155  if (pflags & PS_M_FLAGS)
1156  MSG_WriteByte(to->pmove.pm_flags);
1157 
1158  if (pflags & PS_M_GRAVITY)
1159  MSG_WriteShort(to->pmove.gravity);
1160 
1161  if (pflags & PS_M_DELTA_ANGLES) {
1162  MSG_WriteShort(to->pmove.delta_angles[0]);
1163  MSG_WriteShort(to->pmove.delta_angles[1]);
1164  MSG_WriteShort(to->pmove.delta_angles[2]);
1165  }
1166 
1167  //
1168  // write the rest of the player_state_t
1169  //
1170  if (pflags & PS_VIEWOFFSET) {
1171  MSG_WriteChar(to->viewoffset[0]);
1172  MSG_WriteChar(to->viewoffset[1]);
1173  MSG_WriteChar(to->viewoffset[2]);
1174  }
1175 
1176  if (pflags & PS_VIEWANGLES) {
1177  MSG_WriteShort(to->viewangles[0]);
1178  MSG_WriteShort(to->viewangles[1]);
1179  }
1180 
1181  if (eflags & EPS_VIEWANGLE2)
1182  MSG_WriteShort(to->viewangles[2]);
1183 
1184  if (pflags & PS_KICKANGLES) {
1185  MSG_WriteChar(to->kick_angles[0]);
1186  MSG_WriteChar(to->kick_angles[1]);
1187  MSG_WriteChar(to->kick_angles[2]);
1188  }
1189 
1190  if (pflags & PS_WEAPONINDEX)
1191  MSG_WriteByte(to->gunindex);
1192 
1193  if (pflags & PS_WEAPONFRAME)
1194  MSG_WriteByte(to->gunframe);
1195 
1196  if (eflags & EPS_GUNOFFSET) {
1197  MSG_WriteChar(to->gunoffset[0]);
1198  MSG_WriteChar(to->gunoffset[1]);
1199  MSG_WriteChar(to->gunoffset[2]);
1200  }
1201 
1202  if (eflags & EPS_GUNANGLES) {
1203  MSG_WriteChar(to->gunangles[0]);
1204  MSG_WriteChar(to->gunangles[1]);
1205  MSG_WriteChar(to->gunangles[2]);
1206  }
1207 
1208  if (pflags & PS_BLEND) {
1209  MSG_WriteByte(to->blend[0]);
1210  MSG_WriteByte(to->blend[1]);
1211  MSG_WriteByte(to->blend[2]);
1212  MSG_WriteByte(to->blend[3]);
1213  }
1214 
1215  if (pflags & PS_FOV)
1216  MSG_WriteByte(to->fov);
1217 
1218  if (pflags & PS_RDFLAGS)
1219  MSG_WriteByte(to->rdflags);
1220 
1221  // send stats
1222  if (eflags & EPS_STATS) {
1223  MSG_WriteLong(statbits);
1224  for (i = 0; i < MAX_STATS; i++)
1225  if (statbits & (1 << i))
1226  MSG_WriteShort(to->stats[i]);
1227  }
1228 
1229  return eflags;
1230 }
1231 
1232 #if USE_MVD_SERVER || USE_MVD_CLIENT
1233 
1234 /*
1235 ==================
1236 MSG_WriteDeltaPlayerstate_Packet
1237 
1238 Throws away most of the pmove_state_t fields as they are used only
1239 for client prediction, and are not needed in MVDs.
1240 ==================
1241 */
1242 void MSG_WriteDeltaPlayerstate_Packet(const player_packed_t *from,
1243  const player_packed_t *to,
1244  int number,
1245  msgPsFlags_t flags)
1246 {
1247  int i;
1248  int pflags;
1249  int statbits;
1250 
1251  if (number < 0 || number >= MAX_CLIENTS)
1252  Com_Error(ERR_DROP, "%s: bad number: %d", __func__, number);
1253 
1254  if (!to) {
1255  MSG_WriteByte(number);
1256  MSG_WriteShort(PPS_REMOVE);
1257  return;
1258  }
1259 
1260  if (!from)
1261  from = &nullPlayerState;
1262 
1263  //
1264  // determine what needs to be sent
1265  //
1266  pflags = 0;
1267 
1268  if (to->pmove.pm_type != from->pmove.pm_type)
1269  pflags |= PPS_M_TYPE;
1270 
1271  if (to->pmove.origin[0] != from->pmove.origin[0] ||
1272  to->pmove.origin[1] != from->pmove.origin[1])
1273  pflags |= PPS_M_ORIGIN;
1274 
1275  if (to->pmove.origin[2] != from->pmove.origin[2])
1276  pflags |= PPS_M_ORIGIN2;
1277 
1278  if (from->viewoffset[0] != to->viewoffset[0] ||
1279  from->viewoffset[1] != to->viewoffset[1] ||
1280  from->viewoffset[2] != to->viewoffset[2])
1281  pflags |= PPS_VIEWOFFSET;
1282 
1283  if (from->viewangles[0] != to->viewangles[0] ||
1284  from->viewangles[1] != to->viewangles[1])
1285  pflags |= PPS_VIEWANGLES;
1286 
1287  if (from->viewangles[2] != to->viewangles[2])
1288  pflags |= PPS_VIEWANGLE2;
1289 
1290  if (from->kick_angles[0] != to->kick_angles[0] ||
1291  from->kick_angles[1] != to->kick_angles[1] ||
1292  from->kick_angles[2] != to->kick_angles[2])
1293  pflags |= PPS_KICKANGLES;
1294 
1295  if (!(flags & MSG_PS_IGNORE_BLEND)) {
1296  if (from->blend[0] != to->blend[0] ||
1297  from->blend[1] != to->blend[1] ||
1298  from->blend[2] != to->blend[2] ||
1299  from->blend[3] != to->blend[3])
1300  pflags |= PPS_BLEND;
1301  }
1302 
1303  if (from->fov != to->fov)
1304  pflags |= PPS_FOV;
1305 
1306  if (to->rdflags != from->rdflags)
1307  pflags |= PPS_RDFLAGS;
1308 
1309  if (!(flags & MSG_PS_IGNORE_GUNINDEX)) {
1310  if (to->gunindex != from->gunindex)
1311  pflags |= PPS_WEAPONINDEX;
1312  }
1313 
1314  if (!(flags & MSG_PS_IGNORE_GUNFRAMES)) {
1315  if (to->gunframe != from->gunframe)
1316  pflags |= PPS_WEAPONFRAME;
1317 
1318  if (from->gunoffset[0] != to->gunoffset[0] ||
1319  from->gunoffset[1] != to->gunoffset[1] ||
1320  from->gunoffset[2] != to->gunoffset[2])
1321  pflags |= PPS_GUNOFFSET;
1322 
1323  if (from->gunangles[0] != to->gunangles[0] ||
1324  from->gunangles[1] != to->gunangles[1] ||
1325  from->gunangles[2] != to->gunangles[2])
1326  pflags |= PPS_GUNANGLES;
1327  }
1328 
1329  statbits = 0;
1330  for (i = 0; i < MAX_STATS; i++)
1331  if (to->stats[i] != from->stats[i])
1332  statbits |= 1 << i;
1333 
1334  if (statbits)
1335  pflags |= PPS_STATS;
1336 
1337  if (!pflags && !(flags & MSG_PS_FORCE))
1338  return;
1339 
1340  if (flags & MSG_PS_REMOVE)
1341  pflags |= PPS_REMOVE; // used for MVD stream only
1342 
1343  //
1344  // write it
1345  //
1346  MSG_WriteByte(number);
1347  MSG_WriteShort(pflags);
1348 
1349  //
1350  // write some part of the pmove_state_t
1351  //
1352  if (pflags & PPS_M_TYPE)
1353  MSG_WriteByte(to->pmove.pm_type);
1354 
1355  if (pflags & PPS_M_ORIGIN) {
1356  MSG_WriteShort(to->pmove.origin[0]);
1357  MSG_WriteShort(to->pmove.origin[1]);
1358  }
1359 
1360  if (pflags & PPS_M_ORIGIN2)
1361  MSG_WriteShort(to->pmove.origin[2]);
1362 
1363  //
1364  // write the rest of the player_state_t
1365  //
1366  if (pflags & PPS_VIEWOFFSET) {
1367  MSG_WriteChar(to->viewoffset[0]);
1368  MSG_WriteChar(to->viewoffset[1]);
1369  MSG_WriteChar(to->viewoffset[2]);
1370  }
1371 
1372  if (pflags & PPS_VIEWANGLES) {
1373  MSG_WriteShort(to->viewangles[0]);
1374  MSG_WriteShort(to->viewangles[1]);
1375  }
1376 
1377  if (pflags & PPS_VIEWANGLE2)
1378  MSG_WriteShort(to->viewangles[2]);
1379 
1380  if (pflags & PPS_KICKANGLES) {
1381  MSG_WriteChar(to->kick_angles[0]);
1382  MSG_WriteChar(to->kick_angles[1]);
1383  MSG_WriteChar(to->kick_angles[2]);
1384  }
1385 
1386  if (pflags & PPS_WEAPONINDEX)
1387  MSG_WriteByte(to->gunindex);
1388 
1389  if (pflags & PPS_WEAPONFRAME)
1390  MSG_WriteByte(to->gunframe);
1391 
1392  if (pflags & PPS_GUNOFFSET) {
1393  MSG_WriteChar(to->gunoffset[0]);
1394  MSG_WriteChar(to->gunoffset[1]);
1395  MSG_WriteChar(to->gunoffset[2]);
1396  }
1397 
1398  if (pflags & PPS_GUNANGLES) {
1399  MSG_WriteChar(to->gunangles[0]);
1400  MSG_WriteChar(to->gunangles[1]);
1401  MSG_WriteChar(to->gunangles[2]);
1402  }
1403 
1404  if (pflags & PPS_BLEND) {
1405  MSG_WriteByte(to->blend[0]);
1406  MSG_WriteByte(to->blend[1]);
1407  MSG_WriteByte(to->blend[2]);
1408  MSG_WriteByte(to->blend[3]);
1409  }
1410 
1411  if (pflags & PPS_FOV)
1412  MSG_WriteByte(to->fov);
1413 
1414  if (pflags & PPS_RDFLAGS)
1415  MSG_WriteByte(to->rdflags);
1416 
1417  // send stats
1418  if (pflags & PPS_STATS) {
1419  MSG_WriteLong(statbits);
1420  for (i = 0; i < MAX_STATS; i++)
1421  if (statbits & (1 << i))
1422  MSG_WriteShort(to->stats[i]);
1423  }
1424 }
1425 
1426 #endif // USE_MVD_SERVER || USE_MVD_CLIENT
1427 
1428 
1429 /*
1430 ==============================================================================
1431 
1432  READING
1433 
1434 ==============================================================================
1435 */
1436 
1438 {
1439  msg_read.readcount = 0;
1440  msg_read.bitpos = 0;
1441 }
1442 
1443 byte *MSG_ReadData(size_t len)
1444 {
1445  byte *buf = msg_read.data + msg_read.readcount;
1446 
1447  msg_read.readcount += len;
1448  msg_read.bitpos = msg_read.readcount << 3;
1449 
1450  if (msg_read.readcount > msg_read.cursize) {
1451  if (!msg_read.allowunderflow) {
1452  Com_Error(ERR_DROP, "%s: read past end of message", __func__);
1453  }
1454  return NULL;
1455  }
1456 
1457  return buf;
1458 }
1459 
1460 // returns -1 if no more characters are available
1461 int MSG_ReadChar(void)
1462 {
1463  byte *buf = MSG_ReadData(1);
1464  int c;
1465 
1466  if (!buf) {
1467  c = -1;
1468  } else {
1469  c = (signed char)buf[0];
1470  }
1471 
1472  return c;
1473 }
1474 
1475 int MSG_ReadByte(void)
1476 {
1477  byte *buf = MSG_ReadData(1);
1478  int c;
1479 
1480  if (!buf) {
1481  c = -1;
1482  } else {
1483  c = (unsigned char)buf[0];
1484  }
1485 
1486  return c;
1487 }
1488 
1489 int MSG_ReadShort(void)
1490 {
1491  byte *buf = MSG_ReadData(2);
1492  int c;
1493 
1494  if (!buf) {
1495  c = -1;
1496  } else {
1497  c = (signed short)LittleShortMem(buf);
1498  }
1499 
1500  return c;
1501 }
1502 
1503 int MSG_ReadWord(void)
1504 {
1505  byte *buf = MSG_ReadData(2);
1506  int c;
1507 
1508  if (!buf) {
1509  c = -1;
1510  } else {
1511  c = (unsigned short)LittleShortMem(buf);
1512  }
1513 
1514  return c;
1515 }
1516 
1517 int MSG_ReadLong(void)
1518 {
1519  byte *buf = MSG_ReadData(4);
1520  int c;
1521 
1522  if (!buf) {
1523  c = -1;
1524  } else {
1525  c = LittleLongMem(buf);
1526  }
1527 
1528  return c;
1529 }
1530 
1531 size_t MSG_ReadString(char *dest, size_t size)
1532 {
1533  int c;
1534  size_t len = 0;
1535 
1536  while (1) {
1537  c = MSG_ReadByte();
1538  if (c == -1 || c == 0) {
1539  break;
1540  }
1541  if (len + 1 < size) {
1542  *dest++ = c;
1543  }
1544  len++;
1545  }
1546  if (size) {
1547  *dest = 0;
1548  }
1549 
1550  return len;
1551 }
1552 
1553 size_t MSG_ReadStringLine(char *dest, size_t size)
1554 {
1555  int c;
1556  size_t len = 0;
1557 
1558  while (1) {
1559  c = MSG_ReadByte();
1560  if (c == -1 || c == 0 || c == '\n') {
1561  break;
1562  }
1563  if (len + 1 < size) {
1564  *dest++ = c;
1565  }
1566  len++;
1567  }
1568  if (size) {
1569  *dest = 0;
1570  }
1571 
1572  return len;
1573 }
1574 
1575 static inline float MSG_ReadCoord(void)
1576 {
1577  return SHORT2COORD(MSG_ReadShort());
1578 }
1579 
1580 void MSG_ReadPos(vec3_t pos)
1581 {
1582  pos[0] = MSG_ReadCoord();
1583  pos[1] = MSG_ReadCoord();
1584  pos[2] = MSG_ReadCoord();
1585 }
1586 
1587 static inline float MSG_ReadAngle(void)
1588 {
1589  return BYTE2ANGLE(MSG_ReadChar());
1590 }
1591 
1592 static inline float MSG_ReadAngle16(void)
1593 {
1594  return SHORT2ANGLE(MSG_ReadShort());
1595 }
1596 
1597 void MSG_ReadDir(vec3_t dir)
1598 {
1599  int b;
1600 
1601  b = MSG_ReadByte();
1602  if (b < 0 || b >= NUMVERTEXNORMALS)
1603  Com_Error(ERR_DROP, "MSG_ReadDir: out of range");
1604  VectorCopy(bytedirs[b], dir);
1605 }
1606 
1607 void MSG_ReadDeltaUsercmd(const usercmd_t *from, usercmd_t *to)
1608 {
1609  int bits;
1610 
1611  if (from) {
1612  memcpy(to, from, sizeof(*to));
1613  } else {
1614  memset(to, 0, sizeof(*to));
1615  }
1616 
1617  bits = MSG_ReadByte();
1618 
1619 // read current angles
1620  if (bits & CM_ANGLE1)
1621  to->angles[0] = MSG_ReadShort();
1622  if (bits & CM_ANGLE2)
1623  to->angles[1] = MSG_ReadShort();
1624  if (bits & CM_ANGLE3)
1625  to->angles[2] = MSG_ReadShort();
1626 
1627 // read movement
1628  if (bits & CM_FORWARD)
1629  to->forwardmove = MSG_ReadShort();
1630  if (bits & CM_SIDE)
1631  to->sidemove = MSG_ReadShort();
1632  if (bits & CM_UP)
1633  to->upmove = MSG_ReadShort();
1634 
1635 // read buttons
1636  if (bits & CM_BUTTONS)
1637  to->buttons = MSG_ReadByte();
1638 
1639  if (bits & CM_IMPULSE)
1640  to->impulse = MSG_ReadByte();
1641 
1642 // read time to run command
1643  to->msec = MSG_ReadByte();
1644 
1645 // read the light level
1646  to->lightlevel = MSG_ReadByte();
1647 }
1648 
1649 void MSG_ReadDeltaUsercmd_Hacked(const usercmd_t *from, usercmd_t *to)
1650 {
1651  int bits, buttons = 0;
1652 
1653  if (from) {
1654  memcpy(to, from, sizeof(*to));
1655  } else {
1656  memset(to, 0, sizeof(*to));
1657  }
1658 
1659  bits = MSG_ReadByte();
1660 
1661 // read buttons
1662  if (bits & CM_BUTTONS) {
1663  buttons = MSG_ReadByte();
1664  to->buttons = buttons & BUTTON_MASK;
1665  }
1666 
1667 // read current angles
1668  if (bits & CM_ANGLE1) {
1669  if (buttons & BUTTON_ANGLE1) {
1670  to->angles[0] = MSG_ReadChar() * 64;
1671  } else {
1672  to->angles[0] = MSG_ReadShort();
1673  }
1674  }
1675  if (bits & CM_ANGLE2) {
1676  if (buttons & BUTTON_ANGLE2) {
1677  to->angles[1] = MSG_ReadChar() * 256;
1678  } else {
1679  to->angles[1] = MSG_ReadShort();
1680  }
1681  }
1682  if (bits & CM_ANGLE3)
1683  to->angles[2] = MSG_ReadShort();
1684 
1685 // read movement
1686  if (bits & CM_FORWARD) {
1687  if (buttons & BUTTON_FORWARD) {
1688  to->forwardmove = MSG_ReadChar() * 5;
1689  } else {
1690  to->forwardmove = MSG_ReadShort();
1691  }
1692  }
1693  if (bits & CM_SIDE) {
1694  if (buttons & BUTTON_SIDE) {
1695  to->sidemove = MSG_ReadChar() * 5;
1696  } else {
1697  to->sidemove = MSG_ReadShort();
1698  }
1699  }
1700  if (bits & CM_UP) {
1701  if (buttons & BUTTON_UP) {
1702  to->upmove = MSG_ReadChar() * 5;
1703  } else {
1704  to->upmove = MSG_ReadShort();
1705  }
1706  }
1707 
1708  if (bits & CM_IMPULSE)
1709  to->impulse = MSG_ReadByte();
1710 
1711 // read time to run command
1712  to->msec = MSG_ReadByte();
1713 
1714 // read the light level
1715  to->lightlevel = MSG_ReadByte();
1716 }
1717 
1718 int MSG_ReadBits(int bits)
1719 {
1720  int i, get;
1721  size_t bitpos;
1722  qboolean sgn;
1723  int value;
1724 
1725  if (bits == 0 || bits < -31 || bits > 32) {
1726  Com_Error(ERR_FATAL, "MSG_ReadBits: bad bits: %d", bits);
1727  }
1728 
1729  bitpos = msg_read.bitpos;
1730  if ((bitpos & 7) == 0) {
1731  // optimized case
1732  switch (bits) {
1733  case -8:
1734  value = MSG_ReadChar();
1735  return value;
1736  case 8:
1737  value = MSG_ReadByte();
1738  return value;
1739  case -16:
1740  value = MSG_ReadShort();
1741  return value;
1742  case 32:
1743  value = MSG_ReadLong();
1744  return value;
1745  default:
1746  break;
1747  }
1748  }
1749 
1750  sgn = qfalse;
1751  if (bits < 0) {
1752  bits = -bits;
1753  sgn = qtrue;
1754  }
1755 
1756  value = 0;
1757  for (i = 0; i < bits; i++, bitpos++) {
1758  get = (msg_read.data[bitpos >> 3] >> (bitpos & 7)) & 1;
1759  value |= get << i;
1760  }
1761  msg_read.bitpos = bitpos;
1762  msg_read.readcount = (bitpos + 7) >> 3;
1763 
1764  if (sgn) {
1765  if (value & (1 << (bits - 1))) {
1766  value |= -1 ^((1 << bits) - 1);
1767  }
1768  }
1769 
1770  return value;
1771 }
1772 
1773 void MSG_ReadDeltaUsercmd_Enhanced(const usercmd_t *from,
1774  usercmd_t *to,
1775  int version)
1776 {
1777  int bits, count;
1778 
1779  if (from) {
1780  memcpy(to, from, sizeof(*to));
1781  } else {
1782  memset(to, 0, sizeof(*to));
1783  }
1784 
1785  if (!MSG_ReadBits(1)) {
1786  return;
1787  }
1788 
1789  bits = MSG_ReadBits(8);
1790 
1791 // read current angles
1792  if (bits & CM_ANGLE1) {
1793  if (MSG_ReadBits(1)) {
1794  to->angles[0] += MSG_ReadBits(-8);
1795  } else {
1796  to->angles[0] = MSG_ReadBits(-16);
1797  }
1798  }
1799  if (bits & CM_ANGLE2) {
1800  if (MSG_ReadBits(1)) {
1801  to->angles[1] += MSG_ReadBits(-8);
1802  } else {
1803  to->angles[1] = MSG_ReadBits(-16);
1804  }
1805  }
1806  if (bits & CM_ANGLE3) {
1807  to->angles[2] = MSG_ReadBits(-16);
1808  }
1809 
1810 // read movement
1811  if (version >= PROTOCOL_VERSION_Q2PRO_UCMD) {
1812  count = -10;
1813  } else {
1814  count = -16;
1815  }
1816 
1817  if (bits & CM_FORWARD) {
1818  to->forwardmove = MSG_ReadBits(count);
1819  }
1820  if (bits & CM_SIDE) {
1821  to->sidemove = MSG_ReadBits(count);
1822  }
1823  if (bits & CM_UP) {
1824  to->upmove = MSG_ReadBits(count);
1825  }
1826 
1827 // read buttons
1828  if (bits & CM_BUTTONS) {
1829  int buttons = MSG_ReadBits(3);
1830  to->buttons = (buttons & 3) | ((buttons & 4) << 5);
1831  }
1832 
1833 // read time to run command
1834  if (bits & CM_IMPULSE) {
1835  to->msec = MSG_ReadBits(8);
1836  }
1837 }
1838 
1839 #if USE_CLIENT || USE_MVD_CLIENT
1840 
1841 /*
1842 =================
1843 MSG_ParseEntityBits
1844 
1845 Returns the entity number and the header bits
1846 =================
1847 */
1848 int MSG_ParseEntityBits(int *bits)
1849 {
1850  int b, total;
1851  int number;
1852 
1853  total = MSG_ReadByte();
1854  if (total & U_MOREBITS1) {
1855  b = MSG_ReadByte();
1856  total |= b << 8;
1857  }
1858  if (total & U_MOREBITS2) {
1859  b = MSG_ReadByte();
1860  total |= b << 16;
1861  }
1862  if (total & U_MOREBITS3) {
1863  b = MSG_ReadByte();
1864  total |= b << 24;
1865  }
1866 
1867  if (total & U_NUMBER16)
1868  number = MSG_ReadShort();
1869  else
1870  number = MSG_ReadByte();
1871 
1872  *bits = total;
1873 
1874  return number;
1875 }
1876 
1877 /*
1878 ==================
1879 MSG_ParseDeltaEntity
1880 
1881 Can go from either a baseline or a previous packet_entity
1882 ==================
1883 */
1884 void MSG_ParseDeltaEntity(const entity_state_t *from,
1885  entity_state_t *to,
1886  int number,
1887  int bits,
1888  msgEsFlags_t flags)
1889 {
1890  if (!to) {
1891  Com_Error(ERR_DROP, "%s: NULL", __func__);
1892  }
1893 
1894  if (number < 1 || number >= MAX_EDICTS) {
1895  Com_Error(ERR_DROP, "%s: bad entity number: %d", __func__, number);
1896  }
1897 
1898  // set everything to the state we are delta'ing from
1899  if (!from) {
1900  memset(to, 0, sizeof(*to));
1901  } else if (to != from) {
1902  memcpy(to, from, sizeof(*to));
1903  }
1904 
1905  to->number = number;
1906  to->event = 0;
1907 
1908  if (!bits) {
1909  return;
1910  }
1911 
1912  if (bits & U_MODEL) {
1913  to->modelindex = MSG_ReadByte();
1914  }
1915  if (bits & U_MODEL2) {
1916  to->modelindex2 = MSG_ReadByte();
1917  }
1918  if (bits & U_MODEL3) {
1919  to->modelindex3 = MSG_ReadByte();
1920  }
1921  if (bits & U_MODEL4) {
1922  to->modelindex4 = MSG_ReadByte();
1923  }
1924 
1925  if (bits & U_FRAME8)
1926  to->frame = MSG_ReadByte();
1927  if (bits & U_FRAME16)
1928  to->frame = MSG_ReadShort();
1929 
1930  if ((bits & (U_SKIN8 | U_SKIN16)) == (U_SKIN8 | U_SKIN16)) //used for laser colors
1931  to->skinnum = MSG_ReadLong();
1932  else if (bits & U_SKIN8)
1933  to->skinnum = MSG_ReadByte();
1934  else if (bits & U_SKIN16)
1935  to->skinnum = MSG_ReadWord();
1936 
1937  if ((bits & (U_EFFECTS8 | U_EFFECTS16)) == (U_EFFECTS8 | U_EFFECTS16))
1938  to->effects = MSG_ReadLong();
1939  else if (bits & U_EFFECTS8)
1940  to->effects = MSG_ReadByte();
1941  else if (bits & U_EFFECTS16)
1942  to->effects = MSG_ReadWord();
1943 
1944  if ((bits & (U_RENDERFX8 | U_RENDERFX16)) == (U_RENDERFX8 | U_RENDERFX16))
1945  to->renderfx = MSG_ReadLong();
1946  else if (bits & U_RENDERFX8)
1947  to->renderfx = MSG_ReadByte();
1948  else if (bits & U_RENDERFX16)
1949  to->renderfx = MSG_ReadWord();
1950 
1951  if (bits & U_ORIGIN1) {
1952  to->origin[0] = MSG_ReadCoord();
1953  }
1954  if (bits & U_ORIGIN2) {
1955  to->origin[1] = MSG_ReadCoord();
1956  }
1957  if (bits & U_ORIGIN3) {
1958  to->origin[2] = MSG_ReadCoord();
1959  }
1960 
1961  if ((flags & MSG_ES_SHORTANGLES) && (bits & U_ANGLE16)) {
1962  if (bits & U_ANGLE1)
1963  to->angles[0] = MSG_ReadAngle16();
1964  if (bits & U_ANGLE2)
1965  to->angles[1] = MSG_ReadAngle16();
1966  if (bits & U_ANGLE3)
1967  to->angles[2] = MSG_ReadAngle16();
1968  } else {
1969  if (bits & U_ANGLE1)
1970  to->angles[0] = MSG_ReadAngle();
1971  if (bits & U_ANGLE2)
1972  to->angles[1] = MSG_ReadAngle();
1973  if (bits & U_ANGLE3)
1974  to->angles[2] = MSG_ReadAngle();
1975  }
1976 
1977  if (bits & U_OLDORIGIN) {
1978  MSG_ReadPos(to->old_origin);
1979  }
1980 
1981  if (bits & U_SOUND) {
1982  to->sound = MSG_ReadByte();
1983  }
1984 
1985  if (bits & U_EVENT) {
1986  to->event = MSG_ReadByte();
1987  }
1988 
1989  if (bits & U_SOLID) {
1990  if (flags & MSG_ES_LONGSOLID) {
1991  to->solid = MSG_ReadLong();
1992  } else {
1993  to->solid = MSG_ReadWord();
1994  }
1995  }
1996 }
1997 
1998 #endif // USE_CLIENT || USE_MVD_CLIENT
1999 
2000 #if USE_CLIENT
2001 
2002 /*
2003 ===================
2004 MSG_ParseDeltaPlayerstate_Default
2005 ===================
2006 */
2007 void MSG_ParseDeltaPlayerstate_Default(const player_state_t *from,
2008  player_state_t *to,
2009  int flags)
2010 {
2011  int i;
2012  int statbits;
2013 
2014  if (!to) {
2015  Com_Error(ERR_DROP, "%s: NULL", __func__);
2016  }
2017 
2018  // clear to old value before delta parsing
2019  if (!from) {
2020  memset(to, 0, sizeof(*to));
2021  } else if (to != from) {
2022  memcpy(to, from, sizeof(*to));
2023  }
2024 
2025  //
2026  // parse the pmove_state_t
2027  //
2028  if (flags & PS_M_TYPE)
2029  to->pmove.pm_type = MSG_ReadByte();
2030 
2031  if (flags & PS_M_ORIGIN) {
2032  to->pmove.origin[0] = MSG_ReadShort();
2033  to->pmove.origin[1] = MSG_ReadShort();
2034  to->pmove.origin[2] = MSG_ReadShort();
2035  }
2036 
2037  if (flags & PS_M_VELOCITY) {
2038  to->pmove.velocity[0] = MSG_ReadShort();
2039  to->pmove.velocity[1] = MSG_ReadShort();
2040  to->pmove.velocity[2] = MSG_ReadShort();
2041  }
2042 
2043  if (flags & PS_M_TIME)
2044  to->pmove.pm_time = MSG_ReadByte();
2045 
2046  if (flags & PS_M_FLAGS)
2047  to->pmove.pm_flags = MSG_ReadByte();
2048 
2049  if (flags & PS_M_GRAVITY)
2050  to->pmove.gravity = MSG_ReadShort();
2051 
2052  if (flags & PS_M_DELTA_ANGLES) {
2053  to->pmove.delta_angles[0] = MSG_ReadShort();
2054  to->pmove.delta_angles[1] = MSG_ReadShort();
2055  to->pmove.delta_angles[2] = MSG_ReadShort();
2056  }
2057 
2058  //
2059  // parse the rest of the player_state_t
2060  //
2061  if (flags & PS_VIEWOFFSET) {
2062  to->viewoffset[0] = MSG_ReadChar() * 0.25f;
2063  to->viewoffset[1] = MSG_ReadChar() * 0.25f;
2064  to->viewoffset[2] = MSG_ReadChar() * 0.25f;
2065  }
2066 
2067  if (flags & PS_VIEWANGLES) {
2068  to->viewangles[0] = MSG_ReadAngle16();
2069  to->viewangles[1] = MSG_ReadAngle16();
2070  to->viewangles[2] = MSG_ReadAngle16();
2071  }
2072 
2073  if (flags & PS_KICKANGLES) {
2074  to->kick_angles[0] = MSG_ReadChar() * 0.25f;
2075  to->kick_angles[1] = MSG_ReadChar() * 0.25f;
2076  to->kick_angles[2] = MSG_ReadChar() * 0.25f;
2077  }
2078 
2079  if (flags & PS_WEAPONINDEX) {
2080  to->gunindex = MSG_ReadByte();
2081  }
2082 
2083  if (flags & PS_WEAPONFRAME) {
2084  to->gunframe = MSG_ReadByte();
2085  to->gunoffset[0] = MSG_ReadChar() * 0.25f;
2086  to->gunoffset[1] = MSG_ReadChar() * 0.25f;
2087  to->gunoffset[2] = MSG_ReadChar() * 0.25f;
2088  to->gunangles[0] = MSG_ReadChar() * 0.25f;
2089  to->gunangles[1] = MSG_ReadChar() * 0.25f;
2090  to->gunangles[2] = MSG_ReadChar() * 0.25f;
2091  }
2092 
2093  if (flags & PS_BLEND) {
2094  to->blend[0] = MSG_ReadByte() / 255.0f;
2095  to->blend[1] = MSG_ReadByte() / 255.0f;
2096  to->blend[2] = MSG_ReadByte() / 255.0f;
2097  to->blend[3] = MSG_ReadByte() / 255.0f;
2098  }
2099 
2100  if (flags & PS_FOV)
2101  to->fov = MSG_ReadByte();
2102 
2103  if (flags & PS_RDFLAGS)
2104  to->rdflags = MSG_ReadByte();
2105 
2106  // parse stats
2107  statbits = MSG_ReadLong();
2108  for (i = 0; i < MAX_STATS; i++)
2109  if (statbits & (1 << i))
2110  to->stats[i] = MSG_ReadShort();
2111 }
2112 
2113 
2114 /*
2115 ===================
2116 MSG_ParseDeltaPlayerstate_Default
2117 ===================
2118 */
2119 void MSG_ParseDeltaPlayerstate_Enhanced(const player_state_t *from,
2120  player_state_t *to,
2121  int flags,
2122  int extraflags)
2123 {
2124  int i;
2125  int statbits;
2126 
2127  if (!to) {
2128  Com_Error(ERR_DROP, "%s: NULL", __func__);
2129  }
2130 
2131  // clear to old value before delta parsing
2132  if (!from) {
2133  memset(to, 0, sizeof(*to));
2134  } else if (to != from) {
2135  memcpy(to, from, sizeof(*to));
2136  }
2137 
2138  //
2139  // parse the pmove_state_t
2140  //
2141  if (flags & PS_M_TYPE)
2142  to->pmove.pm_type = MSG_ReadByte();
2143 
2144  if (flags & PS_M_ORIGIN) {
2145  to->pmove.origin[0] = MSG_ReadShort();
2146  to->pmove.origin[1] = MSG_ReadShort();
2147  }
2148 
2149  if (extraflags & EPS_M_ORIGIN2) {
2150  to->pmove.origin[2] = MSG_ReadShort();
2151  }
2152 
2153  if (flags & PS_M_VELOCITY) {
2154  to->pmove.velocity[0] = MSG_ReadShort();
2155  to->pmove.velocity[1] = MSG_ReadShort();
2156  }
2157 
2158  if (extraflags & EPS_M_VELOCITY2) {
2159  to->pmove.velocity[2] = MSG_ReadShort();
2160  }
2161 
2162  if (flags & PS_M_TIME)
2163  to->pmove.pm_time = MSG_ReadByte();
2164 
2165  if (flags & PS_M_FLAGS)
2166  to->pmove.pm_flags = MSG_ReadByte();
2167 
2168  if (flags & PS_M_GRAVITY)
2169  to->pmove.gravity = MSG_ReadShort();
2170 
2171  if (flags & PS_M_DELTA_ANGLES) {
2172  to->pmove.delta_angles[0] = MSG_ReadShort();
2173  to->pmove.delta_angles[1] = MSG_ReadShort();
2174  to->pmove.delta_angles[2] = MSG_ReadShort();
2175  }
2176 
2177  //
2178  // parse the rest of the player_state_t
2179  //
2180  if (flags & PS_VIEWOFFSET) {
2181  to->viewoffset[0] = MSG_ReadChar() * 0.25f;
2182  to->viewoffset[1] = MSG_ReadChar() * 0.25f;
2183  to->viewoffset[2] = MSG_ReadChar() * 0.25f;
2184  }
2185 
2186  if (flags & PS_VIEWANGLES) {
2187  to->viewangles[0] = MSG_ReadAngle16();
2188  to->viewangles[1] = MSG_ReadAngle16();
2189  }
2190 
2191  if (extraflags & EPS_VIEWANGLE2) {
2192  to->viewangles[2] = MSG_ReadAngle16();
2193  }
2194 
2195  if (flags & PS_KICKANGLES) {
2196  to->kick_angles[0] = MSG_ReadChar() * 0.25f;
2197  to->kick_angles[1] = MSG_ReadChar() * 0.25f;
2198  to->kick_angles[2] = MSG_ReadChar() * 0.25f;
2199  }
2200 
2201  if (flags & PS_WEAPONINDEX) {
2202  to->gunindex = MSG_ReadByte();
2203  }
2204 
2205  if (flags & PS_WEAPONFRAME) {
2206  to->gunframe = MSG_ReadByte();
2207  }
2208 
2209  if (extraflags & EPS_GUNOFFSET) {
2210  to->gunoffset[0] = MSG_ReadChar() * 0.25f;
2211  to->gunoffset[1] = MSG_ReadChar() * 0.25f;
2212  to->gunoffset[2] = MSG_ReadChar() * 0.25f;
2213  }
2214 
2215  if (extraflags & EPS_GUNANGLES) {
2216  to->gunangles[0] = MSG_ReadChar() * 0.25f;
2217  to->gunangles[1] = MSG_ReadChar() * 0.25f;
2218  to->gunangles[2] = MSG_ReadChar() * 0.25f;
2219  }
2220 
2221  if (flags & PS_BLEND) {
2222  to->blend[0] = MSG_ReadByte() / 255.0f;
2223  to->blend[1] = MSG_ReadByte() / 255.0f;
2224  to->blend[2] = MSG_ReadByte() / 255.0f;
2225  to->blend[3] = MSG_ReadByte() / 255.0f;
2226  }
2227 
2228  if (flags & PS_FOV)
2229  to->fov = MSG_ReadByte();
2230 
2231  if (flags & PS_RDFLAGS)
2232  to->rdflags = MSG_ReadByte();
2233 
2234  // parse stats
2235  if (extraflags & EPS_STATS) {
2236  statbits = MSG_ReadLong();
2237  for (i = 0; i < MAX_STATS; i++) {
2238  if (statbits & (1 << i)) {
2239  to->stats[i] = MSG_ReadShort();
2240  }
2241  }
2242  }
2243 
2244 }
2245 
2246 #endif // USE_CLIENT
2247 
2248 #if USE_MVD_CLIENT
2249 
2250 /*
2251 ===================
2252 MSG_ParseDeltaPlayerstate_Packet
2253 ===================
2254 */
2255 void MSG_ParseDeltaPlayerstate_Packet(const player_state_t *from,
2256  player_state_t *to,
2257  int flags)
2258 {
2259  int i;
2260  int statbits;
2261 
2262  if (!to) {
2263  Com_Error(ERR_DROP, "%s: NULL", __func__);
2264  }
2265 
2266  // clear to old value before delta parsing
2267  if (!from) {
2268  memset(to, 0, sizeof(*to));
2269  } else if (to != from) {
2270  memcpy(to, from, sizeof(*to));
2271  }
2272 
2273  //
2274  // parse the pmove_state_t
2275  //
2276  if (flags & PPS_M_TYPE)
2277  to->pmove.pm_type = MSG_ReadByte();
2278 
2279  if (flags & PPS_M_ORIGIN) {
2280  to->pmove.origin[0] = MSG_ReadShort();
2281  to->pmove.origin[1] = MSG_ReadShort();
2282  }
2283 
2284  if (flags & PPS_M_ORIGIN2) {
2285  to->pmove.origin[2] = MSG_ReadShort();
2286  }
2287 
2288  //
2289  // parse the rest of the player_state_t
2290  //
2291  if (flags & PPS_VIEWOFFSET) {
2292  to->viewoffset[0] = MSG_ReadChar() * 0.25f;
2293  to->viewoffset[1] = MSG_ReadChar() * 0.25f;
2294  to->viewoffset[2] = MSG_ReadChar() * 0.25f;
2295  }
2296 
2297  if (flags & PPS_VIEWANGLES) {
2298  to->viewangles[0] = MSG_ReadAngle16();
2299  to->viewangles[1] = MSG_ReadAngle16();
2300  }
2301 
2302  if (flags & PPS_VIEWANGLE2) {
2303  to->viewangles[2] = MSG_ReadAngle16();
2304  }
2305 
2306  if (flags & PPS_KICKANGLES) {
2307  to->kick_angles[0] = MSG_ReadChar() * 0.25f;
2308  to->kick_angles[1] = MSG_ReadChar() * 0.25f;
2309  to->kick_angles[2] = MSG_ReadChar() * 0.25f;
2310  }
2311 
2312  if (flags & PPS_WEAPONINDEX) {
2313  to->gunindex = MSG_ReadByte();
2314  }
2315 
2316  if (flags & PPS_WEAPONFRAME) {
2317  to->gunframe = MSG_ReadByte();
2318  }
2319 
2320  if (flags & PPS_GUNOFFSET) {
2321  to->gunoffset[0] = MSG_ReadChar() * 0.25f;
2322  to->gunoffset[1] = MSG_ReadChar() * 0.25f;
2323  to->gunoffset[2] = MSG_ReadChar() * 0.25f;
2324  }
2325 
2326  if (flags & PPS_GUNANGLES) {
2327  to->gunangles[0] = MSG_ReadChar() * 0.25f;
2328  to->gunangles[1] = MSG_ReadChar() * 0.25f;
2329  to->gunangles[2] = MSG_ReadChar() * 0.25f;
2330  }
2331 
2332  if (flags & PPS_BLEND) {
2333  to->blend[0] = MSG_ReadByte() / 255.0f;
2334  to->blend[1] = MSG_ReadByte() / 255.0f;
2335  to->blend[2] = MSG_ReadByte() / 255.0f;
2336  to->blend[3] = MSG_ReadByte() / 255.0f;
2337  }
2338 
2339  if (flags & PPS_FOV)
2340  to->fov = MSG_ReadByte();
2341 
2342  if (flags & PPS_RDFLAGS)
2343  to->rdflags = MSG_ReadByte();
2344 
2345  // parse stats
2346  if (flags & PPS_STATS) {
2347  statbits = MSG_ReadLong();
2348  for (i = 0; i < MAX_STATS; i++) {
2349  if (statbits & (1 << i)) {
2350  to->stats[i] = MSG_ReadShort();
2351  }
2352  }
2353  }
2354 }
2355 
2356 #endif // USE_MVD_CLIENT
2357 
2358 
2359 /*
2360 ==============================================================================
2361 
2362  DEBUGGING STUFF
2363 
2364 ==============================================================================
2365 */
2366 
2367 #ifdef _DEBUG
2368 
2369 #define SHOWBITS(x) Com_LPrintf(PRINT_DEVELOPER, x " ")
2370 
2371 #if USE_CLIENT
2372 
2373 void MSG_ShowDeltaPlayerstateBits_Default(int flags)
2374 {
2375 #define S(b,s) if(flags&PS_##b) SHOWBITS(s)
2376  S(M_TYPE, "pmove.pm_type");
2377  S(M_ORIGIN, "pmove.origin");
2378  S(M_VELOCITY, "pmove.velocity");
2379  S(M_TIME, "pmove.pm_time");
2380  S(M_FLAGS, "pmove.pm_flags");
2381  S(M_GRAVITY, "pmove.gravity");
2382  S(M_DELTA_ANGLES, "pmove.delta_angles");
2383  S(VIEWOFFSET, "viewoffset");
2384  S(VIEWANGLES, "viewangles");
2385  S(KICKANGLES, "kick_angles");
2386  S(WEAPONINDEX, "gunindex");
2387  S(WEAPONFRAME, "gunframe");
2388  S(BLEND, "blend");
2389  S(FOV, "fov");
2390  S(RDFLAGS, "rdflags");
2391 #undef S
2392 }
2393 
2394 void MSG_ShowDeltaPlayerstateBits_Enhanced(int flags, int extraflags)
2395 {
2396 #define SP(b,s) if(flags&PS_##b) SHOWBITS(s)
2397 #define SE(b,s) if(extraflags&EPS_##b) SHOWBITS(s)
2398  SP(M_TYPE, "pmove.pm_type");
2399  SP(M_ORIGIN, "pmove.origin[0,1]");
2400  SE(M_ORIGIN2, "pmove.origin[2]");
2401  SP(M_VELOCITY, "pmove.velocity[0,1]");
2402  SE(M_VELOCITY2, "pmove.velocity[2]");
2403  SP(M_TIME, "pmove.pm_time");
2404  SP(M_FLAGS, "pmove.pm_flags");
2405  SP(M_GRAVITY, "pmove.gravity");
2406  SP(M_DELTA_ANGLES, "pmove.delta_angles");
2407  SP(VIEWOFFSET, "viewoffset");
2408  SP(VIEWANGLES, "viewangles[0,1]");
2409  SE(VIEWANGLE2, "viewangles[2]");
2410  SP(KICKANGLES, "kick_angles");
2411  SP(WEAPONINDEX, "gunindex");
2412  SP(WEAPONFRAME, "gunframe");
2413  SE(GUNOFFSET, "gunoffset");
2414  SE(GUNANGLES, "gunangles");
2415  SP(BLEND, "blend");
2416  SP(FOV, "fov");
2417  SP(RDFLAGS, "rdflags");
2418  SE(STATS, "stats");
2419 #undef SP
2420 #undef SE
2421 }
2422 
2423 void MSG_ShowDeltaUsercmdBits_Enhanced(int bits)
2424 {
2425  if (!bits) {
2426  SHOWBITS("<none>");
2427  return;
2428  }
2429 
2430 #define S(b,s) if(bits&CM_##b) SHOWBITS(s)
2431  S(ANGLE1, "angle1");
2432  S(ANGLE2, "angle2");
2433  S(ANGLE3, "angle3");
2434  S(FORWARD, "forward");
2435  S(SIDE, "side");
2436  S(UP, "up");
2437  S(BUTTONS, "buttons");
2438  S(IMPULSE, "msec");
2439 #undef S
2440 }
2441 
2442 #endif // USE_CLIENT
2443 
2444 #if USE_CLIENT || USE_MVD_CLIENT
2445 
2446 void MSG_ShowDeltaEntityBits(int bits)
2447 {
2448 #define S(b,s) if(bits&U_##b) SHOWBITS(s)
2449  S(MODEL, "modelindex");
2450  S(MODEL2, "modelindex2");
2451  S(MODEL3, "modelindex3");
2452  S(MODEL4, "modelindex4");
2453 
2454  if (bits & U_FRAME8)
2455  SHOWBITS("frame8");
2456  if (bits & U_FRAME16)
2457  SHOWBITS("frame16");
2458 
2459  if ((bits & (U_SKIN8 | U_SKIN16)) == (U_SKIN8 | U_SKIN16))
2460  SHOWBITS("skinnum32");
2461  else if (bits & U_SKIN8)
2462  SHOWBITS("skinnum8");
2463  else if (bits & U_SKIN16)
2464  SHOWBITS("skinnum16");
2465 
2466  if ((bits & (U_EFFECTS8 | U_EFFECTS16)) == (U_EFFECTS8 | U_EFFECTS16))
2467  SHOWBITS("effects32");
2468  else if (bits & U_EFFECTS8)
2469  SHOWBITS("effects8");
2470  else if (bits & U_EFFECTS16)
2471  SHOWBITS("effects16");
2472 
2473  if ((bits & (U_RENDERFX8 | U_RENDERFX16)) == (U_RENDERFX8 | U_RENDERFX16))
2474  SHOWBITS("renderfx32");
2475  else if (bits & U_RENDERFX8)
2476  SHOWBITS("renderfx8");
2477  else if (bits & U_RENDERFX16)
2478  SHOWBITS("renderfx16");
2479 
2480  S(ORIGIN1, "origin[0]");
2481  S(ORIGIN2, "origin[1]");
2482  S(ORIGIN3, "origin[2]");
2483  S(ANGLE1, "angles[0]");
2484  S(ANGLE2, "angles[1]");
2485  S(ANGLE3, "angles[2]");
2486  S(OLDORIGIN, "old_origin");
2487  S(SOUND, "sound");
2488  S(EVENT, "event");
2489  S(SOLID, "solid");
2490 #undef S
2491 }
2492 
2493 void MSG_ShowDeltaPlayerstateBits_Packet(int flags)
2494 {
2495 #define S(b,s) if(flags&PPS_##b) SHOWBITS(s)
2496  S(M_TYPE, "pmove.pm_type");
2497  S(M_ORIGIN, "pmove.origin[0,1]");
2498  S(M_ORIGIN2, "pmove.origin[2]");
2499  S(VIEWOFFSET, "viewoffset");
2500  S(VIEWANGLES, "viewangles[0,1]");
2501  S(VIEWANGLE2, "viewangles[2]");
2502  S(KICKANGLES, "kick_angles");
2503  S(WEAPONINDEX, "gunindex");
2504  S(WEAPONFRAME, "gunframe");
2505  S(GUNOFFSET, "gunoffset");
2506  S(GUNANGLES, "gunangles");
2507  S(BLEND, "blend");
2508  S(FOV, "fov");
2509  S(RDFLAGS, "rdflags");
2510  S(STATS, "stats");
2511 #undef S
2512 }
2513 
2514 const char *MSG_ServerCommandString(int cmd)
2515 {
2516  switch (cmd) {
2517  case -1: return "END OF MESSAGE";
2518  default: return "UNKNOWN COMMAND";
2519 #define S(x) case svc_##x: return "svc_" #x;
2520  S(bad)
2521  S(muzzleflash)
2522  S(muzzleflash2)
2523  S(temp_entity)
2524  S(layout)
2525  S(inventory)
2526  S(nop)
2527  S(disconnect)
2528  S(reconnect)
2529  S(sound)
2530  S(print)
2531  S(stufftext)
2532  S(serverdata)
2533  S(configstring)
2534  S(spawnbaseline)
2535  S(centerprint)
2536  S(download)
2537  S(playerinfo)
2538  S(packetentities)
2539  S(deltapacketentities)
2540  S(frame)
2541  S(zpacket)
2542  S(zdownload)
2543  S(gamestate)
2544 #undef S
2545  }
2546 }
2547 
2548 #endif // USE_CLIENT || USE_MVD_CLIENT
2549 
2550 #endif // _DEBUG
2551 
nullEntityState
const entity_packed_t nullEntityState
Definition: msg.c:40
MSG_WriteCoord
static void MSG_WriteCoord(float f)
Definition: msg.c:188
msg_read
sizebuf_t msg_read
Definition: msg.c:37
msg_read_buffer
byte msg_read_buffer[MAX_MSGLEN]
Definition: msg.c:38
BYTE2ANGLE
#define BYTE2ANGLE(x)
Definition: msg.c:212
nullPlayerState
const player_packed_t nullPlayerState
Definition: msg.c:41
MSG_BeginReading
void MSG_BeginReading(void)
Definition: msg.c:1437
ANGLE2BYTE
#define ANGLE2BYTE(x)
Definition: msg.c:211
MSG_ReadDeltaUsercmd_Enhanced
void MSG_ReadDeltaUsercmd_Enhanced(const usercmd_t *from, usercmd_t *to, int version)
Definition: msg.c:1773
MSG_ReadWord
int MSG_ReadWord(void)
Definition: msg.c:1503
MSG_ReadDeltaUsercmd
void MSG_ReadDeltaUsercmd(const usercmd_t *from, usercmd_t *to)
Definition: msg.c:1607
SHORT2COORD
#define SHORT2COORD(x)
Definition: msg.c:186
layout
layout(set=GOD_RAYS_DESC_SET_IDX, binding=0) uniform sampler2DArray TEX_SHADOW_MAP
MSG_ReadPos
void MSG_ReadPos(vec3_t pos)
Definition: msg.c:1580
MSG_WriteByte
void MSG_WriteByte(int c)
Definition: msg.c:107
MSG_ReadChar
int MSG_ReadChar(void)
Definition: msg.c:1461
msg_write_buffer
byte msg_write_buffer[MAX_MSGLEN]
Definition: msg.c:35
nullUserCmd
const usercmd_t nullUserCmd
Definition: msg.c:42
MSG_ReadBits
int MSG_ReadBits(int bits)
Definition: msg.c:1718
MSG_WritePos
void MSG_WritePos(const vec3_t pos)
Definition: msg.c:198
SZ_GetSpace
void * SZ_GetSpace(sizebuf_t *buf, size_t len)
Definition: sizebuf.c:48
Com_Error
void Com_Error(error_type_t type, const char *fmt,...)
Definition: g_main.c:258
MSG_PackEntity
void MSG_PackEntity(entity_packed_t *out, const entity_state_t *in, qboolean short_angles)
Definition: msg.c:468
MSG_WriteDeltaPlayerstate_Enhanced
int MSG_WriteDeltaPlayerstate_Enhanced(const player_packed_t *from, player_packed_t *to, msgPsFlags_t flags)
Definition: msg.c:975
MSG_PackPlayer
void MSG_PackPlayer(player_packed_t *out, const player_state_t *in)
Definition: msg.c:763
MSG_ReadData
byte * MSG_ReadData(size_t len)
Definition: msg.c:1443
S
#define S(name)
Definition: g_save.c:42
msg_write
sizebuf_t msg_write
Definition: msg.c:34
DirToByte
int DirToByte(const vec3_t dir)
Definition: math.c:245
MSG_ReadLong
int MSG_ReadLong(void)
Definition: msg.c:1517
MSG_WriteDeltaEntity
void MSG_WriteDeltaEntity(const entity_packed_t *from, const entity_packed_t *to, msgEsFlags_t flags)
Definition: msg.c:505
MSG_WriteShort
void MSG_WriteShort(int c)
Definition: msg.c:125
COORD2SHORT
#define COORD2SHORT(x)
Definition: msg.c:185
MSG_Init
void MSG_Init(void)
Definition: msg.c:57
SZ_TagInit
void SZ_TagInit(sizebuf_t *buf, void *data, size_t size, uint32_t tag)
Definition: sizebuf.c:23
MSG_ReadCoord
static float MSG_ReadCoord(void)
Definition: msg.c:1575
MSG_WriteString
void MSG_WriteString(const char *string)
Definition: msg.c:160
MSG_ReadDeltaUsercmd_Hacked
void MSG_ReadDeltaUsercmd_Hacked(const usercmd_t *from, usercmd_t *to)
Definition: msg.c:1649
c
statCounters_t c
Definition: main.c:30
MSG_ReadAngle
static float MSG_ReadAngle(void)
Definition: msg.c:1587
MSG_WriteLong
void MSG_WriteLong(int c)
Definition: msg.c:144
MSG_ReadString
size_t MSG_ReadString(char *dest, size_t size)
Definition: msg.c:1531
MSG_WriteChar
void MSG_WriteChar(int c)
Definition: msg.c:89
MSG_ReadStringLine
size_t MSG_ReadStringLine(char *dest, size_t size)
Definition: msg.c:1553
MSG_ReadByte
int MSG_ReadByte(void)
Definition: msg.c:1475
MSG_ReadDir
void MSG_ReadDir(vec3_t dir)
Definition: msg.c:1597
bytedirs
const vec3_t bytedirs[NUMVERTEXNORMALS]
Definition: math.c:80
MSG_ReadAngle16
static float MSG_ReadAngle16(void)
Definition: msg.c:1592
MSG_WriteDir
void MSG_WriteDir(const vec3_t dir)
Definition: msg.c:460
MSG_WriteAngle
void MSG_WriteAngle(float f)
Definition: msg.c:214
MSG_BeginWriting
void MSG_BeginWriting(void)
Definition: msg.c:77
MSG_WriteDeltaPlayerstate_Default
void MSG_WriteDeltaPlayerstate_Default(const player_packed_t *from, const player_packed_t *to)
Definition: msg.c:795
MSG_ReadShort
int MSG_ReadShort(void)
Definition: msg.c:1489