msg.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758
  1. /*
  2. ===========================================================================
  3. Copyright (C) 1999-2005 Id Software, Inc.
  4. This file is part of Quake III Arena source code.
  5. Quake III Arena source code is free software; you can redistribute it
  6. and/or modify it under the terms of the GNU General Public License as
  7. published by the Free Software Foundation; either version 2 of the License,
  8. or (at your option) any later version.
  9. Quake III Arena source code is distributed in the hope that it will be
  10. useful, 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. You should have received a copy of the GNU General Public License
  14. along with Foobar; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  16. ===========================================================================
  17. */
  18. #include "../game/q_shared.h"
  19. #include "qcommon.h"
  20. static huffman_t msgHuff;
  21. static qboolean msgInit = qfalse;
  22. int pcount[256];
  23. /*
  24. ==============================================================================
  25. MESSAGE IO FUNCTIONS
  26. Handles byte ordering and avoids alignment errors
  27. ==============================================================================
  28. */
  29. int oldsize = 0;
  30. void MSG_initHuffman();
  31. void MSG_Init( msg_t *buf, byte *data, int length ) {
  32. if (!msgInit) {
  33. MSG_initHuffman();
  34. }
  35. Com_Memset (buf, 0, sizeof(*buf));
  36. buf->data = data;
  37. buf->maxsize = length;
  38. }
  39. void MSG_InitOOB( msg_t *buf, byte *data, int length ) {
  40. if (!msgInit) {
  41. MSG_initHuffman();
  42. }
  43. Com_Memset (buf, 0, sizeof(*buf));
  44. buf->data = data;
  45. buf->maxsize = length;
  46. buf->oob = qtrue;
  47. }
  48. void MSG_Clear( msg_t *buf ) {
  49. buf->cursize = 0;
  50. buf->overflowed = qfalse;
  51. buf->bit = 0; //<- in bits
  52. }
  53. void MSG_Bitstream( msg_t *buf ) {
  54. buf->oob = qfalse;
  55. }
  56. void MSG_BeginReading( msg_t *msg ) {
  57. msg->readcount = 0;
  58. msg->bit = 0;
  59. msg->oob = qfalse;
  60. }
  61. void MSG_BeginReadingOOB( msg_t *msg ) {
  62. msg->readcount = 0;
  63. msg->bit = 0;
  64. msg->oob = qtrue;
  65. }
  66. void MSG_Copy(msg_t *buf, byte *data, int length, msg_t *src)
  67. {
  68. if (length<src->cursize) {
  69. Com_Error( ERR_DROP, "MSG_Copy: can't copy into a smaller msg_t buffer");
  70. }
  71. Com_Memcpy(buf, src, sizeof(msg_t));
  72. buf->data = data;
  73. Com_Memcpy(buf->data, src->data, src->cursize);
  74. }
  75. /*
  76. =============================================================================
  77. bit functions
  78. =============================================================================
  79. */
  80. int overflows;
  81. // negative bit values include signs
  82. void MSG_WriteBits( msg_t *msg, int value, int bits ) {
  83. int i;
  84. // FILE* fp;
  85. oldsize += bits;
  86. // this isn't an exact overflow check, but close enough
  87. if ( msg->maxsize - msg->cursize < 4 ) {
  88. msg->overflowed = qtrue;
  89. return;
  90. }
  91. if ( bits == 0 || bits < -31 || bits > 32 ) {
  92. Com_Error( ERR_DROP, "MSG_WriteBits: bad bits %i", bits );
  93. }
  94. // check for overflows
  95. if ( bits != 32 ) {
  96. if ( bits > 0 ) {
  97. if ( value > ( ( 1 << bits ) - 1 ) || value < 0 ) {
  98. overflows++;
  99. }
  100. } else {
  101. int r;
  102. r = 1 << (bits-1);
  103. if ( value > r - 1 || value < -r ) {
  104. overflows++;
  105. }
  106. }
  107. }
  108. if ( bits < 0 ) {
  109. bits = -bits;
  110. }
  111. if (msg->oob) {
  112. if (bits==8) {
  113. msg->data[msg->cursize] = value;
  114. msg->cursize += 1;
  115. msg->bit += 8;
  116. } else if (bits==16) {
  117. unsigned short *sp = (unsigned short *)&msg->data[msg->cursize];
  118. *sp = LittleShort(value);
  119. msg->cursize += 2;
  120. msg->bit += 16;
  121. } else if (bits==32) {
  122. unsigned int *ip = (unsigned int *)&msg->data[msg->cursize];
  123. *ip = LittleLong(value);
  124. msg->cursize += 4;
  125. msg->bit += 8;
  126. } else {
  127. Com_Error(ERR_DROP, "can't read %d bits\n", bits);
  128. }
  129. } else {
  130. // fp = fopen("c:\\netchan.bin", "a");
  131. value &= (0xffffffff>>(32-bits));
  132. if (bits&7) {
  133. int nbits;
  134. nbits = bits&7;
  135. for(i=0;i<nbits;i++) {
  136. Huff_putBit((value&1), msg->data, &msg->bit);
  137. value = (value>>1);
  138. }
  139. bits = bits - nbits;
  140. }
  141. if (bits) {
  142. for(i=0;i<bits;i+=8) {
  143. // fwrite(bp, 1, 1, fp);
  144. Huff_offsetTransmit (&msgHuff.compressor, (value&0xff), msg->data, &msg->bit);
  145. value = (value>>8);
  146. }
  147. }
  148. msg->cursize = (msg->bit>>3)+1;
  149. // fclose(fp);
  150. }
  151. }
  152. int MSG_ReadBits( msg_t *msg, int bits ) {
  153. int value;
  154. int get;
  155. qboolean sgn;
  156. int i, nbits;
  157. // FILE* fp;
  158. value = 0;
  159. if ( bits < 0 ) {
  160. bits = -bits;
  161. sgn = qtrue;
  162. } else {
  163. sgn = qfalse;
  164. }
  165. if (msg->oob) {
  166. if (bits==8) {
  167. value = msg->data[msg->readcount];
  168. msg->readcount += 1;
  169. msg->bit += 8;
  170. } else if (bits==16) {
  171. unsigned short *sp = (unsigned short *)&msg->data[msg->readcount];
  172. value = LittleShort(*sp);
  173. msg->readcount += 2;
  174. msg->bit += 16;
  175. } else if (bits==32) {
  176. unsigned int *ip = (unsigned int *)&msg->data[msg->readcount];
  177. value = LittleLong(*ip);
  178. msg->readcount += 4;
  179. msg->bit += 32;
  180. } else {
  181. Com_Error(ERR_DROP, "can't read %d bits\n", bits);
  182. }
  183. } else {
  184. nbits = 0;
  185. if (bits&7) {
  186. nbits = bits&7;
  187. for(i=0;i<nbits;i++) {
  188. value |= (Huff_getBit(msg->data, &msg->bit)<<i);
  189. }
  190. bits = bits - nbits;
  191. }
  192. if (bits) {
  193. // fp = fopen("c:\\netchan.bin", "a");
  194. for(i=0;i<bits;i+=8) {
  195. Huff_offsetReceive (msgHuff.decompressor.tree, &get, msg->data, &msg->bit);
  196. // fwrite(&get, 1, 1, fp);
  197. value |= (get<<(i+nbits));
  198. }
  199. // fclose(fp);
  200. }
  201. msg->readcount = (msg->bit>>3)+1;
  202. }
  203. if ( sgn ) {
  204. if ( value & ( 1 << ( bits - 1 ) ) ) {
  205. value |= -1 ^ ( ( 1 << bits ) - 1 );
  206. }
  207. }
  208. return value;
  209. }
  210. //================================================================================
  211. //
  212. // writing functions
  213. //
  214. void MSG_WriteChar( msg_t *sb, int c ) {
  215. #ifdef PARANOID
  216. if (c < -128 || c > 127)
  217. Com_Error (ERR_FATAL, "MSG_WriteChar: range error");
  218. #endif
  219. MSG_WriteBits( sb, c, 8 );
  220. }
  221. void MSG_WriteByte( msg_t *sb, int c ) {
  222. #ifdef PARANOID
  223. if (c < 0 || c > 255)
  224. Com_Error (ERR_FATAL, "MSG_WriteByte: range error");
  225. #endif
  226. MSG_WriteBits( sb, c, 8 );
  227. }
  228. void MSG_WriteData( msg_t *buf, const void *data, int length ) {
  229. int i;
  230. for(i=0;i<length;i++) {
  231. MSG_WriteByte(buf, ((byte *)data)[i]);
  232. }
  233. }
  234. void MSG_WriteShort( msg_t *sb, int c ) {
  235. #ifdef PARANOID
  236. if (c < ((short)0x8000) || c > (short)0x7fff)
  237. Com_Error (ERR_FATAL, "MSG_WriteShort: range error");
  238. #endif
  239. MSG_WriteBits( sb, c, 16 );
  240. }
  241. void MSG_WriteLong( msg_t *sb, int c ) {
  242. MSG_WriteBits( sb, c, 32 );
  243. }
  244. void MSG_WriteFloat( msg_t *sb, float f ) {
  245. union {
  246. float f;
  247. int l;
  248. } dat;
  249. dat.f = f;
  250. MSG_WriteBits( sb, dat.l, 32 );
  251. }
  252. void MSG_WriteString( msg_t *sb, const char *s ) {
  253. if ( !s ) {
  254. MSG_WriteData (sb, "", 1);
  255. } else {
  256. int l,i;
  257. char string[MAX_STRING_CHARS];
  258. l = strlen( s );
  259. if ( l >= MAX_STRING_CHARS ) {
  260. Com_Printf( "MSG_WriteString: MAX_STRING_CHARS" );
  261. MSG_WriteData (sb, "", 1);
  262. return;
  263. }
  264. Q_strncpyz( string, s, sizeof( string ) );
  265. // get rid of 0xff chars, because old clients don't like them
  266. for ( i = 0 ; i < l ; i++ ) {
  267. if ( ((byte *)string)[i] > 127 ) {
  268. string[i] = '.';
  269. }
  270. }
  271. MSG_WriteData (sb, string, l+1);
  272. }
  273. }
  274. void MSG_WriteBigString( msg_t *sb, const char *s ) {
  275. if ( !s ) {
  276. MSG_WriteData (sb, "", 1);
  277. } else {
  278. int l,i;
  279. char string[BIG_INFO_STRING];
  280. l = strlen( s );
  281. if ( l >= BIG_INFO_STRING ) {
  282. Com_Printf( "MSG_WriteString: BIG_INFO_STRING" );
  283. MSG_WriteData (sb, "", 1);
  284. return;
  285. }
  286. Q_strncpyz( string, s, sizeof( string ) );
  287. // get rid of 0xff chars, because old clients don't like them
  288. for ( i = 0 ; i < l ; i++ ) {
  289. if ( ((byte *)string)[i] > 127 ) {
  290. string[i] = '.';
  291. }
  292. }
  293. MSG_WriteData (sb, string, l+1);
  294. }
  295. }
  296. void MSG_WriteAngle( msg_t *sb, float f ) {
  297. MSG_WriteByte (sb, (int)(f*256/360) & 255);
  298. }
  299. void MSG_WriteAngle16( msg_t *sb, float f ) {
  300. MSG_WriteShort (sb, ANGLE2SHORT(f));
  301. }
  302. //============================================================
  303. //
  304. // reading functions
  305. //
  306. // returns -1 if no more characters are available
  307. int MSG_ReadChar (msg_t *msg ) {
  308. int c;
  309. c = (signed char)MSG_ReadBits( msg, 8 );
  310. if ( msg->readcount > msg->cursize ) {
  311. c = -1;
  312. }
  313. return c;
  314. }
  315. int MSG_ReadByte( msg_t *msg ) {
  316. int c;
  317. c = (unsigned char)MSG_ReadBits( msg, 8 );
  318. if ( msg->readcount > msg->cursize ) {
  319. c = -1;
  320. }
  321. return c;
  322. }
  323. int MSG_ReadShort( msg_t *msg ) {
  324. int c;
  325. c = (short)MSG_ReadBits( msg, 16 );
  326. if ( msg->readcount > msg->cursize ) {
  327. c = -1;
  328. }
  329. return c;
  330. }
  331. int MSG_ReadLong( msg_t *msg ) {
  332. int c;
  333. c = MSG_ReadBits( msg, 32 );
  334. if ( msg->readcount > msg->cursize ) {
  335. c = -1;
  336. }
  337. return c;
  338. }
  339. float MSG_ReadFloat( msg_t *msg ) {
  340. union {
  341. byte b[4];
  342. float f;
  343. int l;
  344. } dat;
  345. dat.l = MSG_ReadBits( msg, 32 );
  346. if ( msg->readcount > msg->cursize ) {
  347. dat.f = -1;
  348. }
  349. return dat.f;
  350. }
  351. char *MSG_ReadString( msg_t *msg ) {
  352. static char string[MAX_STRING_CHARS];
  353. int l,c;
  354. l = 0;
  355. do {
  356. c = MSG_ReadByte(msg); // use ReadByte so -1 is out of bounds
  357. if ( c == -1 || c == 0 ) {
  358. break;
  359. }
  360. // translate all fmt spec to avoid crash bugs
  361. if ( c == '%' ) {
  362. c = '.';
  363. }
  364. // don't allow higher ascii values
  365. if ( c > 127 ) {
  366. c = '.';
  367. }
  368. string[l] = c;
  369. l++;
  370. } while (l < sizeof(string)-1);
  371. string[l] = 0;
  372. return string;
  373. }
  374. char *MSG_ReadBigString( msg_t *msg ) {
  375. static char string[BIG_INFO_STRING];
  376. int l,c;
  377. l = 0;
  378. do {
  379. c = MSG_ReadByte(msg); // use ReadByte so -1 is out of bounds
  380. if ( c == -1 || c == 0 ) {
  381. break;
  382. }
  383. // translate all fmt spec to avoid crash bugs
  384. if ( c == '%' ) {
  385. c = '.';
  386. }
  387. string[l] = c;
  388. l++;
  389. } while (l < sizeof(string)-1);
  390. string[l] = 0;
  391. return string;
  392. }
  393. char *MSG_ReadStringLine( msg_t *msg ) {
  394. static char string[MAX_STRING_CHARS];
  395. int l,c;
  396. l = 0;
  397. do {
  398. c = MSG_ReadByte(msg); // use ReadByte so -1 is out of bounds
  399. if (c == -1 || c == 0 || c == '\n') {
  400. break;
  401. }
  402. // translate all fmt spec to avoid crash bugs
  403. if ( c == '%' ) {
  404. c = '.';
  405. }
  406. string[l] = c;
  407. l++;
  408. } while (l < sizeof(string)-1);
  409. string[l] = 0;
  410. return string;
  411. }
  412. float MSG_ReadAngle16( msg_t *msg ) {
  413. return SHORT2ANGLE(MSG_ReadShort(msg));
  414. }
  415. void MSG_ReadData( msg_t *msg, void *data, int len ) {
  416. int i;
  417. for (i=0 ; i<len ; i++) {
  418. ((byte *)data)[i] = MSG_ReadByte (msg);
  419. }
  420. }
  421. /*
  422. =============================================================================
  423. delta functions
  424. =============================================================================
  425. */
  426. extern cvar_t *cl_shownet;
  427. #define LOG(x) if( cl_shownet->integer == 4 ) { Com_Printf("%s ", x ); };
  428. void MSG_WriteDelta( msg_t *msg, int oldV, int newV, int bits ) {
  429. if ( oldV == newV ) {
  430. MSG_WriteBits( msg, 0, 1 );
  431. return;
  432. }
  433. MSG_WriteBits( msg, 1, 1 );
  434. MSG_WriteBits( msg, newV, bits );
  435. }
  436. int MSG_ReadDelta( msg_t *msg, int oldV, int bits ) {
  437. if ( MSG_ReadBits( msg, 1 ) ) {
  438. return MSG_ReadBits( msg, bits );
  439. }
  440. return oldV;
  441. }
  442. void MSG_WriteDeltaFloat( msg_t *msg, float oldV, float newV ) {
  443. if ( oldV == newV ) {
  444. MSG_WriteBits( msg, 0, 1 );
  445. return;
  446. }
  447. MSG_WriteBits( msg, 1, 1 );
  448. MSG_WriteBits( msg, *(int *)&newV, 32 );
  449. }
  450. float MSG_ReadDeltaFloat( msg_t *msg, float oldV ) {
  451. if ( MSG_ReadBits( msg, 1 ) ) {
  452. float newV;
  453. *(int *)&newV = MSG_ReadBits( msg, 32 );
  454. return newV;
  455. }
  456. return oldV;
  457. }
  458. /*
  459. =============================================================================
  460. delta functions with keys
  461. =============================================================================
  462. */
  463. int kbitmask[32] = {
  464. 0x00000001, 0x00000003, 0x00000007, 0x0000000F,
  465. 0x0000001F, 0x0000003F, 0x0000007F, 0x000000FF,
  466. 0x000001FF, 0x000003FF, 0x000007FF, 0x00000FFF,
  467. 0x00001FFF, 0x00003FFF, 0x00007FFF, 0x0000FFFF,
  468. 0x0001FFFF, 0x0003FFFF, 0x0007FFFF, 0x000FFFFF,
  469. 0x001FFFFf, 0x003FFFFF, 0x007FFFFF, 0x00FFFFFF,
  470. 0x01FFFFFF, 0x03FFFFFF, 0x07FFFFFF, 0x0FFFFFFF,
  471. 0x1FFFFFFF, 0x3FFFFFFF, 0x7FFFFFFF, 0xFFFFFFFF,
  472. };
  473. void MSG_WriteDeltaKey( msg_t *msg, int key, int oldV, int newV, int bits ) {
  474. if ( oldV == newV ) {
  475. MSG_WriteBits( msg, 0, 1 );
  476. return;
  477. }
  478. MSG_WriteBits( msg, 1, 1 );
  479. MSG_WriteBits( msg, newV ^ key, bits );
  480. }
  481. int MSG_ReadDeltaKey( msg_t *msg, int key, int oldV, int bits ) {
  482. if ( MSG_ReadBits( msg, 1 ) ) {
  483. return MSG_ReadBits( msg, bits ) ^ (key & kbitmask[bits]);
  484. }
  485. return oldV;
  486. }
  487. void MSG_WriteDeltaKeyFloat( msg_t *msg, int key, float oldV, float newV ) {
  488. if ( oldV == newV ) {
  489. MSG_WriteBits( msg, 0, 1 );
  490. return;
  491. }
  492. MSG_WriteBits( msg, 1, 1 );
  493. MSG_WriteBits( msg, (*(int *)&newV) ^ key, 32 );
  494. }
  495. float MSG_ReadDeltaKeyFloat( msg_t *msg, int key, float oldV ) {
  496. if ( MSG_ReadBits( msg, 1 ) ) {
  497. float newV;
  498. *(int *)&newV = MSG_ReadBits( msg, 32 ) ^ key;
  499. return newV;
  500. }
  501. return oldV;
  502. }
  503. /*
  504. ============================================================================
  505. usercmd_t communication
  506. ============================================================================
  507. */
  508. // ms is allways sent, the others are optional
  509. #define CM_ANGLE1 (1<<0)
  510. #define CM_ANGLE2 (1<<1)
  511. #define CM_ANGLE3 (1<<2)
  512. #define CM_FORWARD (1<<3)
  513. #define CM_SIDE (1<<4)
  514. #define CM_UP (1<<5)
  515. #define CM_BUTTONS (1<<6)
  516. #define CM_WEAPON (1<<7)
  517. /*
  518. =====================
  519. MSG_WriteDeltaUsercmd
  520. =====================
  521. */
  522. void MSG_WriteDeltaUsercmd( msg_t *msg, usercmd_t *from, usercmd_t *to ) {
  523. if ( to->serverTime - from->serverTime < 256 ) {
  524. MSG_WriteBits( msg, 1, 1 );
  525. MSG_WriteBits( msg, to->serverTime - from->serverTime, 8 );
  526. } else {
  527. MSG_WriteBits( msg, 0, 1 );
  528. MSG_WriteBits( msg, to->serverTime, 32 );
  529. }
  530. MSG_WriteDelta( msg, from->angles[0], to->angles[0], 16 );
  531. MSG_WriteDelta( msg, from->angles[1], to->angles[1], 16 );
  532. MSG_WriteDelta( msg, from->angles[2], to->angles[2], 16 );
  533. MSG_WriteDelta( msg, from->forwardmove, to->forwardmove, 8 );
  534. MSG_WriteDelta( msg, from->rightmove, to->rightmove, 8 );
  535. MSG_WriteDelta( msg, from->upmove, to->upmove, 8 );
  536. MSG_WriteDelta( msg, from->buttons, to->buttons, 16 );
  537. MSG_WriteDelta( msg, from->weapon, to->weapon, 8 );
  538. }
  539. /*
  540. =====================
  541. MSG_ReadDeltaUsercmd
  542. =====================
  543. */
  544. void MSG_ReadDeltaUsercmd( msg_t *msg, usercmd_t *from, usercmd_t *to ) {
  545. if ( MSG_ReadBits( msg, 1 ) ) {
  546. to->serverTime = from->serverTime + MSG_ReadBits( msg, 8 );
  547. } else {
  548. to->serverTime = MSG_ReadBits( msg, 32 );
  549. }
  550. to->angles[0] = MSG_ReadDelta( msg, from->angles[0], 16);
  551. to->angles[1] = MSG_ReadDelta( msg, from->angles[1], 16);
  552. to->angles[2] = MSG_ReadDelta( msg, from->angles[2], 16);
  553. to->forwardmove = MSG_ReadDelta( msg, from->forwardmove, 8);
  554. to->rightmove = MSG_ReadDelta( msg, from->rightmove, 8);
  555. to->upmove = MSG_ReadDelta( msg, from->upmove, 8);
  556. to->buttons = MSG_ReadDelta( msg, from->buttons, 16);
  557. to->weapon = MSG_ReadDelta( msg, from->weapon, 8);
  558. }
  559. /*
  560. =====================
  561. MSG_WriteDeltaUsercmd
  562. =====================
  563. */
  564. void MSG_WriteDeltaUsercmdKey( msg_t *msg, int key, usercmd_t *from, usercmd_t *to ) {
  565. if ( to->serverTime - from->serverTime < 256 ) {
  566. MSG_WriteBits( msg, 1, 1 );
  567. MSG_WriteBits( msg, to->serverTime - from->serverTime, 8 );
  568. } else {
  569. MSG_WriteBits( msg, 0, 1 );
  570. MSG_WriteBits( msg, to->serverTime, 32 );
  571. }
  572. if (from->angles[0] == to->angles[0] &&
  573. from->angles[1] == to->angles[1] &&
  574. from->angles[2] == to->angles[2] &&
  575. from->forwardmove == to->forwardmove &&
  576. from->rightmove == to->rightmove &&
  577. from->upmove == to->upmove &&
  578. from->buttons == to->buttons &&
  579. from->weapon == to->weapon) {
  580. MSG_WriteBits( msg, 0, 1 ); // no change
  581. oldsize += 7;
  582. return;
  583. }
  584. key ^= to->serverTime;
  585. MSG_WriteBits( msg, 1, 1 );
  586. MSG_WriteDeltaKey( msg, key, from->angles[0], to->angles[0], 16 );
  587. MSG_WriteDeltaKey( msg, key, from->angles[1], to->angles[1], 16 );
  588. MSG_WriteDeltaKey( msg, key, from->angles[2], to->angles[2], 16 );
  589. MSG_WriteDeltaKey( msg, key, from->forwardmove, to->forwardmove, 8 );
  590. MSG_WriteDeltaKey( msg, key, from->rightmove, to->rightmove, 8 );
  591. MSG_WriteDeltaKey( msg, key, from->upmove, to->upmove, 8 );
  592. MSG_WriteDeltaKey( msg, key, from->buttons, to->buttons, 16 );
  593. MSG_WriteDeltaKey( msg, key, from->weapon, to->weapon, 8 );
  594. }
  595. /*
  596. =====================
  597. MSG_ReadDeltaUsercmd
  598. =====================
  599. */
  600. void MSG_ReadDeltaUsercmdKey( msg_t *msg, int key, usercmd_t *from, usercmd_t *to ) {
  601. if ( MSG_ReadBits( msg, 1 ) ) {
  602. to->serverTime = from->serverTime + MSG_ReadBits( msg, 8 );
  603. } else {
  604. to->serverTime = MSG_ReadBits( msg, 32 );
  605. }
  606. if ( MSG_ReadBits( msg, 1 ) ) {
  607. key ^= to->serverTime;
  608. to->angles[0] = MSG_ReadDeltaKey( msg, key, from->angles[0], 16);
  609. to->angles[1] = MSG_ReadDeltaKey( msg, key, from->angles[1], 16);
  610. to->angles[2] = MSG_ReadDeltaKey( msg, key, from->angles[2], 16);
  611. to->forwardmove = MSG_ReadDeltaKey( msg, key, from->forwardmove, 8);
  612. to->rightmove = MSG_ReadDeltaKey( msg, key, from->rightmove, 8);
  613. to->upmove = MSG_ReadDeltaKey( msg, key, from->upmove, 8);
  614. to->buttons = MSG_ReadDeltaKey( msg, key, from->buttons, 16);
  615. to->weapon = MSG_ReadDeltaKey( msg, key, from->weapon, 8);
  616. } else {
  617. to->angles[0] = from->angles[0];
  618. to->angles[1] = from->angles[1];
  619. to->angles[2] = from->angles[2];
  620. to->forwardmove = from->forwardmove;
  621. to->rightmove = from->rightmove;
  622. to->upmove = from->upmove;
  623. to->buttons = from->buttons;
  624. to->weapon = from->weapon;
  625. }
  626. }
  627. /*
  628. =============================================================================
  629. entityState_t communication
  630. =============================================================================
  631. */
  632. /*
  633. =================
  634. MSG_ReportChangeVectors_f
  635. Prints out a table from the current statistics for copying to code
  636. =================
  637. */
  638. void MSG_ReportChangeVectors_f( void ) {
  639. int i;
  640. for(i=0;i<256;i++) {
  641. if (pcount[i]) {
  642. Com_Printf("%d used %d\n", i, pcount[i]);
  643. }
  644. }
  645. }
  646. typedef struct {
  647. char *name;
  648. int offset;
  649. int bits; // 0 = float
  650. } netField_t;
  651. // using the stringizing operator to save typing...
  652. #define NETF(x) #x,(int)&((entityState_t*)0)->x
  653. netField_t entityStateFields[] =
  654. {
  655. { NETF(pos.trTime), 32 },
  656. { NETF(pos.trBase[0]), 0 },
  657. { NETF(pos.trBase[1]), 0 },
  658. { NETF(pos.trDelta[0]), 0 },
  659. { NETF(pos.trDelta[1]), 0 },
  660. { NETF(pos.trBase[2]), 0 },
  661. { NETF(apos.trBase[1]), 0 },
  662. { NETF(pos.trDelta[2]), 0 },
  663. { NETF(apos.trBase[0]), 0 },
  664. { NETF(event), 10 },
  665. { NETF(angles2[1]), 0 },
  666. { NETF(eType), 8 },
  667. { NETF(torsoAnim), 8 },
  668. { NETF(eventParm), 8 },
  669. { NETF(legsAnim), 8 },
  670. { NETF(groundEntityNum), GENTITYNUM_BITS },
  671. { NETF(pos.trType), 8 },
  672. { NETF(eFlags), 19 },
  673. { NETF(otherEntityNum), GENTITYNUM_BITS },
  674. { NETF(weapon), 8 },
  675. { NETF(clientNum), 8 },
  676. { NETF(angles[1]), 0 },
  677. { NETF(pos.trDuration), 32 },
  678. { NETF(apos.trType), 8 },
  679. { NETF(origin[0]), 0 },
  680. { NETF(origin[1]), 0 },
  681. { NETF(origin[2]), 0 },
  682. { NETF(solid), 24 },
  683. { NETF(powerups), 16 },
  684. { NETF(modelindex), 8 },
  685. { NETF(otherEntityNum2), GENTITYNUM_BITS },
  686. { NETF(loopSound), 8 },
  687. { NETF(generic1), 8 },
  688. { NETF(origin2[2]), 0 },
  689. { NETF(origin2[0]), 0 },
  690. { NETF(origin2[1]), 0 },
  691. { NETF(modelindex2), 8 },
  692. { NETF(angles[0]), 0 },
  693. { NETF(time), 32 },
  694. { NETF(apos.trTime), 32 },
  695. { NETF(apos.trDuration), 32 },
  696. { NETF(apos.trBase[2]), 0 },
  697. { NETF(apos.trDelta[0]), 0 },
  698. { NETF(apos.trDelta[1]), 0 },
  699. { NETF(apos.trDelta[2]), 0 },
  700. { NETF(time2), 32 },
  701. { NETF(angles[2]), 0 },
  702. { NETF(angles2[0]), 0 },
  703. { NETF(angles2[2]), 0 },
  704. { NETF(constantLight), 32 },
  705. { NETF(frame), 16 }
  706. };
  707. // if (int)f == f and (int)f + ( 1<<(FLOAT_INT_BITS-1) ) < ( 1 << FLOAT_INT_BITS )
  708. // the float will be sent with FLOAT_INT_BITS, otherwise all 32 bits will be sent
  709. #define FLOAT_INT_BITS 13
  710. #define FLOAT_INT_BIAS (1<<(FLOAT_INT_BITS-1))
  711. /*
  712. ==================
  713. MSG_WriteDeltaEntity
  714. Writes part of a packetentities message, including the entity number.
  715. Can delta from either a baseline or a previous packet_entity
  716. If to is NULL, a remove entity update will be sent
  717. If force is not set, then nothing at all will be generated if the entity is
  718. identical, under the assumption that the in-order delta code will catch it.
  719. ==================
  720. */
  721. void MSG_WriteDeltaEntity( msg_t *msg, struct entityState_s *from, struct entityState_s *to,
  722. qboolean force ) {
  723. int i, lc;
  724. int numFields;
  725. netField_t *field;
  726. int trunc;
  727. float fullFloat;
  728. int *fromF, *toF;
  729. numFields = sizeof(entityStateFields)/sizeof(entityStateFields[0]);
  730. // all fields should be 32 bits to avoid any compiler packing issues
  731. // the "number" field is not part of the field list
  732. // if this assert fails, someone added a field to the entityState_t
  733. // struct without updating the message fields
  734. assert( numFields + 1 == sizeof( *from )/4 );
  735. // a NULL to is a delta remove message
  736. if ( to == NULL ) {
  737. if ( from == NULL ) {
  738. return;
  739. }
  740. MSG_WriteBits( msg, from->number, GENTITYNUM_BITS );
  741. MSG_WriteBits( msg, 1, 1 );
  742. return;
  743. }
  744. if ( to->number < 0 || to->number >= MAX_GENTITIES ) {
  745. Com_Error (ERR_FATAL, "MSG_WriteDeltaEntity: Bad entity number: %i", to->number );
  746. }
  747. lc = 0;
  748. // build the change vector as bytes so it is endien independent
  749. for ( i = 0, field = entityStateFields ; i < numFields ; i++, field++ ) {
  750. fromF = (int *)( (byte *)from + field->offset );
  751. toF = (int *)( (byte *)to + field->offset );
  752. if ( *fromF != *toF ) {
  753. lc = i+1;
  754. }
  755. }
  756. if ( lc == 0 ) {
  757. // nothing at all changed
  758. if ( !force ) {
  759. return; // nothing at all
  760. }
  761. // write two bits for no change
  762. MSG_WriteBits( msg, to->number, GENTITYNUM_BITS );
  763. MSG_WriteBits( msg, 0, 1 ); // not removed
  764. MSG_WriteBits( msg, 0, 1 ); // no delta
  765. return;
  766. }
  767. MSG_WriteBits( msg, to->number, GENTITYNUM_BITS );
  768. MSG_WriteBits( msg, 0, 1 ); // not removed
  769. MSG_WriteBits( msg, 1, 1 ); // we have a delta
  770. MSG_WriteByte( msg, lc ); // # of changes
  771. oldsize += numFields;
  772. for ( i = 0, field = entityStateFields ; i < lc ; i++, field++ ) {
  773. fromF = (int *)( (byte *)from + field->offset );
  774. toF = (int *)( (byte *)to + field->offset );
  775. if ( *fromF == *toF ) {
  776. MSG_WriteBits( msg, 0, 1 ); // no change
  777. continue;
  778. }
  779. MSG_WriteBits( msg, 1, 1 ); // changed
  780. if ( field->bits == 0 ) {
  781. // float
  782. fullFloat = *(float *)toF;
  783. trunc = (int)fullFloat;
  784. if (fullFloat == 0.0f) {
  785. MSG_WriteBits( msg, 0, 1 );
  786. oldsize += FLOAT_INT_BITS;
  787. } else {
  788. MSG_WriteBits( msg, 1, 1 );
  789. if ( trunc == fullFloat && trunc + FLOAT_INT_BIAS >= 0 &&
  790. trunc + FLOAT_INT_BIAS < ( 1 << FLOAT_INT_BITS ) ) {
  791. // send as small integer
  792. MSG_WriteBits( msg, 0, 1 );
  793. MSG_WriteBits( msg, trunc + FLOAT_INT_BIAS, FLOAT_INT_BITS );
  794. } else {
  795. // send as full floating point value
  796. MSG_WriteBits( msg, 1, 1 );
  797. MSG_WriteBits( msg, *toF, 32 );
  798. }
  799. }
  800. } else {
  801. if (*toF == 0) {
  802. MSG_WriteBits( msg, 0, 1 );
  803. } else {
  804. MSG_WriteBits( msg, 1, 1 );
  805. // integer
  806. MSG_WriteBits( msg, *toF, field->bits );
  807. }
  808. }
  809. }
  810. }
  811. /*
  812. ==================
  813. MSG_ReadDeltaEntity
  814. The entity number has already been read from the message, which
  815. is how the from state is identified.
  816. If the delta removes the entity, entityState_t->number will be set to MAX_GENTITIES-1
  817. Can go from either a baseline or a previous packet_entity
  818. ==================
  819. */
  820. extern cvar_t *cl_shownet;
  821. void MSG_ReadDeltaEntity( msg_t *msg, entityState_t *from, entityState_t *to,
  822. int number) {
  823. int i, lc;
  824. int numFields;
  825. netField_t *field;
  826. int *fromF, *toF;
  827. int print;
  828. int trunc;
  829. int startBit, endBit;
  830. if ( number < 0 || number >= MAX_GENTITIES) {
  831. Com_Error( ERR_DROP, "Bad delta entity number: %i", number );
  832. }
  833. if ( msg->bit == 0 ) {
  834. startBit = msg->readcount * 8 - GENTITYNUM_BITS;
  835. } else {
  836. startBit = ( msg->readcount - 1 ) * 8 + msg->bit - GENTITYNUM_BITS;
  837. }
  838. // check for a remove
  839. if ( MSG_ReadBits( msg, 1 ) == 1 ) {
  840. Com_Memset( to, 0, sizeof( *to ) );
  841. to->number = MAX_GENTITIES - 1;
  842. if ( cl_shownet->integer >= 2 || cl_shownet->integer == -1 ) {
  843. Com_Printf( "%3i: #%-3i remove\n", msg->readcount, number );
  844. }
  845. return;
  846. }
  847. // check for no delta
  848. if ( MSG_ReadBits( msg, 1 ) == 0 ) {
  849. *to = *from;
  850. to->number = number;
  851. return;
  852. }
  853. numFields = sizeof(entityStateFields)/sizeof(entityStateFields[0]);
  854. lc = MSG_ReadByte(msg);
  855. // shownet 2/3 will interleave with other printed info, -1 will
  856. // just print the delta records`
  857. if ( cl_shownet->integer >= 2 || cl_shownet->integer == -1 ) {
  858. print = 1;
  859. Com_Printf( "%3i: #%-3i ", msg->readcount, to->number );
  860. } else {
  861. print = 0;
  862. }
  863. to->number = number;
  864. for ( i = 0, field = entityStateFields ; i < lc ; i++, field++ ) {
  865. fromF = (int *)( (byte *)from + field->offset );
  866. toF = (int *)( (byte *)to + field->offset );
  867. if ( ! MSG_ReadBits( msg, 1 ) ) {
  868. // no change
  869. *toF = *fromF;
  870. } else {
  871. if ( field->bits == 0 ) {
  872. // float
  873. if ( MSG_ReadBits( msg, 1 ) == 0 ) {
  874. *(float *)toF = 0.0f;
  875. } else {
  876. if ( MSG_ReadBits( msg, 1 ) == 0 ) {
  877. // integral float
  878. trunc = MSG_ReadBits( msg, FLOAT_INT_BITS );
  879. // bias to allow equal parts positive and negative
  880. trunc -= FLOAT_INT_BIAS;
  881. *(float *)toF = trunc;
  882. if ( print ) {
  883. Com_Printf( "%s:%i ", field->name, trunc );
  884. }
  885. } else {
  886. // full floating point value
  887. *toF = MSG_ReadBits( msg, 32 );
  888. if ( print ) {
  889. Com_Printf( "%s:%f ", field->name, *(float *)toF );
  890. }
  891. }
  892. }
  893. } else {
  894. if ( MSG_ReadBits( msg, 1 ) == 0 ) {
  895. *toF = 0;
  896. } else {
  897. // integer
  898. *toF = MSG_ReadBits( msg, field->bits );
  899. if ( print ) {
  900. Com_Printf( "%s:%i ", field->name, *toF );
  901. }
  902. }
  903. }
  904. // pcount[i]++;
  905. }
  906. }
  907. for ( i = lc, field = &entityStateFields[lc] ; i < numFields ; i++, field++ ) {
  908. fromF = (int *)( (byte *)from + field->offset );
  909. toF = (int *)( (byte *)to + field->offset );
  910. // no change
  911. *toF = *fromF;
  912. }
  913. if ( print ) {
  914. if ( msg->bit == 0 ) {
  915. endBit = msg->readcount * 8 - GENTITYNUM_BITS;
  916. } else {
  917. endBit = ( msg->readcount - 1 ) * 8 + msg->bit - GENTITYNUM_BITS;
  918. }
  919. Com_Printf( " (%i bits)\n", endBit - startBit );
  920. }
  921. }
  922. /*
  923. ============================================================================
  924. plyer_state_t communication
  925. ============================================================================
  926. */
  927. // using the stringizing operator to save typing...
  928. #define PSF(x) #x,(int)&((playerState_t*)0)->x
  929. netField_t playerStateFields[] =
  930. {
  931. { PSF(commandTime), 32 },
  932. { PSF(origin[0]), 0 },
  933. { PSF(origin[1]), 0 },
  934. { PSF(bobCycle), 8 },
  935. { PSF(velocity[0]), 0 },
  936. { PSF(velocity[1]), 0 },
  937. { PSF(viewangles[1]), 0 },
  938. { PSF(viewangles[0]), 0 },
  939. { PSF(weaponTime), -16 },
  940. { PSF(origin[2]), 0 },
  941. { PSF(velocity[2]), 0 },
  942. { PSF(legsTimer), 8 },
  943. { PSF(pm_time), -16 },
  944. { PSF(eventSequence), 16 },
  945. { PSF(torsoAnim), 8 },
  946. { PSF(movementDir), 4 },
  947. { PSF(events[0]), 8 },
  948. { PSF(legsAnim), 8 },
  949. { PSF(events[1]), 8 },
  950. { PSF(pm_flags), 16 },
  951. { PSF(groundEntityNum), GENTITYNUM_BITS },
  952. { PSF(weaponstate), 4 },
  953. { PSF(eFlags), 16 },
  954. { PSF(externalEvent), 10 },
  955. { PSF(gravity), 16 },
  956. { PSF(speed), 16 },
  957. { PSF(delta_angles[1]), 16 },
  958. { PSF(externalEventParm), 8 },
  959. { PSF(viewheight), -8 },
  960. { PSF(damageEvent), 8 },
  961. { PSF(damageYaw), 8 },
  962. { PSF(damagePitch), 8 },
  963. { PSF(damageCount), 8 },
  964. { PSF(generic1), 8 },
  965. { PSF(pm_type), 8 },
  966. { PSF(delta_angles[0]), 16 },
  967. { PSF(delta_angles[2]), 16 },
  968. { PSF(torsoTimer), 12 },
  969. { PSF(eventParms[0]), 8 },
  970. { PSF(eventParms[1]), 8 },
  971. { PSF(clientNum), 8 },
  972. { PSF(weapon), 5 },
  973. { PSF(viewangles[2]), 0 },
  974. { PSF(grapplePoint[0]), 0 },
  975. { PSF(grapplePoint[1]), 0 },
  976. { PSF(grapplePoint[2]), 0 },
  977. { PSF(jumppad_ent), 10 },
  978. { PSF(loopSound), 16 }
  979. };
  980. /*
  981. =============
  982. MSG_WriteDeltaPlayerstate
  983. =============
  984. */
  985. void MSG_WriteDeltaPlayerstate( msg_t *msg, struct playerState_s *from, struct playerState_s *to ) {
  986. int i;
  987. playerState_t dummy;
  988. int statsbits;
  989. int persistantbits;
  990. int ammobits;
  991. int powerupbits;
  992. int numFields;
  993. int c;
  994. netField_t *field;
  995. int *fromF, *toF;
  996. float fullFloat;
  997. int trunc, lc;
  998. if (!from) {
  999. from = &dummy;
  1000. Com_Memset (&dummy, 0, sizeof(dummy));
  1001. }
  1002. c = msg->cursize;
  1003. numFields = sizeof( playerStateFields ) / sizeof( playerStateFields[0] );
  1004. lc = 0;
  1005. for ( i = 0, field = playerStateFields ; i < numFields ; i++, field++ ) {
  1006. fromF = (int *)( (byte *)from + field->offset );
  1007. toF = (int *)( (byte *)to + field->offset );
  1008. if ( *fromF != *toF ) {
  1009. lc = i+1;
  1010. }
  1011. }
  1012. MSG_WriteByte( msg, lc ); // # of changes
  1013. oldsize += numFields - lc;
  1014. for ( i = 0, field = playerStateFields ; i < lc ; i++, field++ ) {
  1015. fromF = (int *)( (byte *)from + field->offset );
  1016. toF = (int *)( (byte *)to + field->offset );
  1017. if ( *fromF == *toF ) {
  1018. MSG_WriteBits( msg, 0, 1 ); // no change
  1019. continue;
  1020. }
  1021. MSG_WriteBits( msg, 1, 1 ); // changed
  1022. // pcount[i]++;
  1023. if ( field->bits == 0 ) {
  1024. // float
  1025. fullFloat = *(float *)toF;
  1026. trunc = (int)fullFloat;
  1027. if ( trunc == fullFloat && trunc + FLOAT_INT_BIAS >= 0 &&
  1028. trunc + FLOAT_INT_BIAS < ( 1 << FLOAT_INT_BITS ) ) {
  1029. // send as small integer
  1030. MSG_WriteBits( msg, 0, 1 );
  1031. MSG_WriteBits( msg, trunc + FLOAT_INT_BIAS, FLOAT_INT_BITS );
  1032. } else {
  1033. // send as full floating point value
  1034. MSG_WriteBits( msg, 1, 1 );
  1035. MSG_WriteBits( msg, *toF, 32 );
  1036. }
  1037. } else {
  1038. // integer
  1039. MSG_WriteBits( msg, *toF, field->bits );
  1040. }
  1041. }
  1042. c = msg->cursize - c;
  1043. //
  1044. // send the arrays
  1045. //
  1046. statsbits = 0;
  1047. for (i=0 ; i<16 ; i++) {
  1048. if (to->stats[i] != from->stats[i]) {
  1049. statsbits |= 1<<i;
  1050. }
  1051. }
  1052. persistantbits = 0;
  1053. for (i=0 ; i<16 ; i++) {
  1054. if (to->persistant[i] != from->persistant[i]) {
  1055. persistantbits |= 1<<i;
  1056. }
  1057. }
  1058. ammobits = 0;
  1059. for (i=0 ; i<16 ; i++) {
  1060. if (to->ammo[i] != from->ammo[i]) {
  1061. ammobits |= 1<<i;
  1062. }
  1063. }
  1064. powerupbits = 0;
  1065. for (i=0 ; i<16 ; i++) {
  1066. if (to->powerups[i] != from->powerups[i]) {
  1067. powerupbits |= 1<<i;
  1068. }
  1069. }
  1070. if (!statsbits && !persistantbits && !ammobits && !powerupbits) {
  1071. MSG_WriteBits( msg, 0, 1 ); // no change
  1072. oldsize += 4;
  1073. return;
  1074. }
  1075. MSG_WriteBits( msg, 1, 1 ); // changed
  1076. if ( statsbits ) {
  1077. MSG_WriteBits( msg, 1, 1 ); // changed
  1078. MSG_WriteShort( msg, statsbits );
  1079. for (i=0 ; i<16 ; i++)
  1080. if (statsbits & (1<<i) )
  1081. MSG_WriteShort (msg, to->stats[i]);
  1082. } else {
  1083. MSG_WriteBits( msg, 0, 1 ); // no change
  1084. }
  1085. if ( persistantbits ) {
  1086. MSG_WriteBits( msg, 1, 1 ); // changed
  1087. MSG_WriteShort( msg, persistantbits );
  1088. for (i=0 ; i<16 ; i++)
  1089. if (persistantbits & (1<<i) )
  1090. MSG_WriteShort (msg, to->persistant[i]);
  1091. } else {
  1092. MSG_WriteBits( msg, 0, 1 ); // no change
  1093. }
  1094. if ( ammobits ) {
  1095. MSG_WriteBits( msg, 1, 1 ); // changed
  1096. MSG_WriteShort( msg, ammobits );
  1097. for (i=0 ; i<16 ; i++)
  1098. if (ammobits & (1<<i) )
  1099. MSG_WriteShort (msg, to->ammo[i]);
  1100. } else {
  1101. MSG_WriteBits( msg, 0, 1 ); // no change
  1102. }
  1103. if ( powerupbits ) {
  1104. MSG_WriteBits( msg, 1, 1 ); // changed
  1105. MSG_WriteShort( msg, powerupbits );
  1106. for (i=0 ; i<16 ; i++)
  1107. if (powerupbits & (1<<i) )
  1108. MSG_WriteLong( msg, to->powerups[i] );
  1109. } else {
  1110. MSG_WriteBits( msg, 0, 1 ); // no change
  1111. }
  1112. }
  1113. /*
  1114. ===================
  1115. MSG_ReadDeltaPlayerstate
  1116. ===================
  1117. */
  1118. void MSG_ReadDeltaPlayerstate (msg_t *msg, playerState_t *from, playerState_t *to ) {
  1119. int i, lc;
  1120. int bits;
  1121. netField_t *field;
  1122. int numFields;
  1123. int startBit, endBit;
  1124. int print;
  1125. int *fromF, *toF;
  1126. int trunc;
  1127. playerState_t dummy;
  1128. if ( !from ) {
  1129. from = &dummy;
  1130. Com_Memset( &dummy, 0, sizeof( dummy ) );
  1131. }
  1132. *to = *from;
  1133. if ( msg->bit == 0 ) {
  1134. startBit = msg->readcount * 8 - GENTITYNUM_BITS;
  1135. } else {
  1136. startBit = ( msg->readcount - 1 ) * 8 + msg->bit - GENTITYNUM_BITS;
  1137. }
  1138. // shownet 2/3 will interleave with other printed info, -2 will
  1139. // just print the delta records
  1140. if ( cl_shownet->integer >= 2 || cl_shownet->integer == -2 ) {
  1141. print = 1;
  1142. Com_Printf( "%3i: playerstate ", msg->readcount );
  1143. } else {
  1144. print = 0;
  1145. }
  1146. numFields = sizeof( playerStateFields ) / sizeof( playerStateFields[0] );
  1147. lc = MSG_ReadByte(msg);
  1148. for ( i = 0, field = playerStateFields ; i < lc ; i++, field++ ) {
  1149. fromF = (int *)( (byte *)from + field->offset );
  1150. toF = (int *)( (byte *)to + field->offset );
  1151. if ( ! MSG_ReadBits( msg, 1 ) ) {
  1152. // no change
  1153. *toF = *fromF;
  1154. } else {
  1155. if ( field->bits == 0 ) {
  1156. // float
  1157. if ( MSG_ReadBits( msg, 1 ) == 0 ) {
  1158. // integral float
  1159. trunc = MSG_ReadBits( msg, FLOAT_INT_BITS );
  1160. // bias to allow equal parts positive and negative
  1161. trunc -= FLOAT_INT_BIAS;
  1162. *(float *)toF = trunc;
  1163. if ( print ) {
  1164. Com_Printf( "%s:%i ", field->name, trunc );
  1165. }
  1166. } else {
  1167. // full floating point value
  1168. *toF = MSG_ReadBits( msg, 32 );
  1169. if ( print ) {
  1170. Com_Printf( "%s:%f ", field->name, *(float *)toF );
  1171. }
  1172. }
  1173. } else {
  1174. // integer
  1175. *toF = MSG_ReadBits( msg, field->bits );
  1176. if ( print ) {
  1177. Com_Printf( "%s:%i ", field->name, *toF );
  1178. }
  1179. }
  1180. }
  1181. }
  1182. for ( i=lc,field = &playerStateFields[lc];i<numFields; i++, field++) {
  1183. fromF = (int *)( (byte *)from + field->offset );
  1184. toF = (int *)( (byte *)to + field->offset );
  1185. // no change
  1186. *toF = *fromF;
  1187. }
  1188. // read the arrays
  1189. if (MSG_ReadBits( msg, 1 ) ) {
  1190. // parse stats
  1191. if ( MSG_ReadBits( msg, 1 ) ) {
  1192. LOG("PS_STATS");
  1193. bits = MSG_ReadShort (msg);
  1194. for (i=0 ; i<16 ; i++) {
  1195. if (bits & (1<<i) ) {
  1196. to->stats[i] = MSG_ReadShort(msg);
  1197. }
  1198. }
  1199. }
  1200. // parse persistant stats
  1201. if ( MSG_ReadBits( msg, 1 ) ) {
  1202. LOG("PS_PERSISTANT");
  1203. bits = MSG_ReadShort (msg);
  1204. for (i=0 ; i<16 ; i++) {
  1205. if (bits & (1<<i) ) {
  1206. to->persistant[i] = MSG_ReadShort(msg);
  1207. }
  1208. }
  1209. }
  1210. // parse ammo
  1211. if ( MSG_ReadBits( msg, 1 ) ) {
  1212. LOG("PS_AMMO");
  1213. bits = MSG_ReadShort (msg);
  1214. for (i=0 ; i<16 ; i++) {
  1215. if (bits & (1<<i) ) {
  1216. to->ammo[i] = MSG_ReadShort(msg);
  1217. }
  1218. }
  1219. }
  1220. // parse powerups
  1221. if ( MSG_ReadBits( msg, 1 ) ) {
  1222. LOG("PS_POWERUPS");
  1223. bits = MSG_ReadShort (msg);
  1224. for (i=0 ; i<16 ; i++) {
  1225. if (bits & (1<<i) ) {
  1226. to->powerups[i] = MSG_ReadLong(msg);
  1227. }
  1228. }
  1229. }
  1230. }
  1231. if ( print ) {
  1232. if ( msg->bit == 0 ) {
  1233. endBit = msg->readcount * 8 - GENTITYNUM_BITS;
  1234. } else {
  1235. endBit = ( msg->readcount - 1 ) * 8 + msg->bit - GENTITYNUM_BITS;
  1236. }
  1237. Com_Printf( " (%i bits)\n", endBit - startBit );
  1238. }
  1239. }
  1240. int msg_hData[256] = {
  1241. 250315, // 0
  1242. 41193, // 1
  1243. 6292, // 2
  1244. 7106, // 3
  1245. 3730, // 4
  1246. 3750, // 5
  1247. 6110, // 6
  1248. 23283, // 7
  1249. 33317, // 8
  1250. 6950, // 9
  1251. 7838, // 10
  1252. 9714, // 11
  1253. 9257, // 12
  1254. 17259, // 13
  1255. 3949, // 14
  1256. 1778, // 15
  1257. 8288, // 16
  1258. 1604, // 17
  1259. 1590, // 18
  1260. 1663, // 19
  1261. 1100, // 20
  1262. 1213, // 21
  1263. 1238, // 22
  1264. 1134, // 23
  1265. 1749, // 24
  1266. 1059, // 25
  1267. 1246, // 26
  1268. 1149, // 27
  1269. 1273, // 28
  1270. 4486, // 29
  1271. 2805, // 30
  1272. 3472, // 31
  1273. 21819, // 32
  1274. 1159, // 33
  1275. 1670, // 34
  1276. 1066, // 35
  1277. 1043, // 36
  1278. 1012, // 37
  1279. 1053, // 38
  1280. 1070, // 39
  1281. 1726, // 40
  1282. 888, // 41
  1283. 1180, // 42
  1284. 850, // 43
  1285. 960, // 44
  1286. 780, // 45
  1287. 1752, // 46
  1288. 3296, // 47
  1289. 10630, // 48
  1290. 4514, // 49
  1291. 5881, // 50
  1292. 2685, // 51
  1293. 4650, // 52
  1294. 3837, // 53
  1295. 2093, // 54
  1296. 1867, // 55
  1297. 2584, // 56
  1298. 1949, // 57
  1299. 1972, // 58
  1300. 940, // 59
  1301. 1134, // 60
  1302. 1788, // 61
  1303. 1670, // 62
  1304. 1206, // 63
  1305. 5719, // 64
  1306. 6128, // 65
  1307. 7222, // 66
  1308. 6654, // 67
  1309. 3710, // 68
  1310. 3795, // 69
  1311. 1492, // 70
  1312. 1524, // 71
  1313. 2215, // 72
  1314. 1140, // 73
  1315. 1355, // 74
  1316. 971, // 75
  1317. 2180, // 76
  1318. 1248, // 77
  1319. 1328, // 78
  1320. 1195, // 79
  1321. 1770, // 80
  1322. 1078, // 81
  1323. 1264, // 82
  1324. 1266, // 83
  1325. 1168, // 84
  1326. 965, // 85
  1327. 1155, // 86
  1328. 1186, // 87
  1329. 1347, // 88
  1330. 1228, // 89
  1331. 1529, // 90
  1332. 1600, // 91
  1333. 2617, // 92
  1334. 2048, // 93
  1335. 2546, // 94
  1336. 3275, // 95
  1337. 2410, // 96
  1338. 3585, // 97
  1339. 2504, // 98
  1340. 2800, // 99
  1341. 2675, // 100
  1342. 6146, // 101
  1343. 3663, // 102
  1344. 2840, // 103
  1345. 14253, // 104
  1346. 3164, // 105
  1347. 2221, // 106
  1348. 1687, // 107
  1349. 3208, // 108
  1350. 2739, // 109
  1351. 3512, // 110
  1352. 4796, // 111
  1353. 4091, // 112
  1354. 3515, // 113
  1355. 5288, // 114
  1356. 4016, // 115
  1357. 7937, // 116
  1358. 6031, // 117
  1359. 5360, // 118
  1360. 3924, // 119
  1361. 4892, // 120
  1362. 3743, // 121
  1363. 4566, // 122
  1364. 4807, // 123
  1365. 5852, // 124
  1366. 6400, // 125
  1367. 6225, // 126
  1368. 8291, // 127
  1369. 23243, // 128
  1370. 7838, // 129
  1371. 7073, // 130
  1372. 8935, // 131
  1373. 5437, // 132
  1374. 4483, // 133
  1375. 3641, // 134
  1376. 5256, // 135
  1377. 5312, // 136
  1378. 5328, // 137
  1379. 5370, // 138
  1380. 3492, // 139
  1381. 2458, // 140
  1382. 1694, // 141
  1383. 1821, // 142
  1384. 2121, // 143
  1385. 1916, // 144
  1386. 1149, // 145
  1387. 1516, // 146
  1388. 1367, // 147
  1389. 1236, // 148
  1390. 1029, // 149
  1391. 1258, // 150
  1392. 1104, // 151
  1393. 1245, // 152
  1394. 1006, // 153
  1395. 1149, // 154
  1396. 1025, // 155
  1397. 1241, // 156
  1398. 952, // 157
  1399. 1287, // 158
  1400. 997, // 159
  1401. 1713, // 160
  1402. 1009, // 161
  1403. 1187, // 162
  1404. 879, // 163
  1405. 1099, // 164
  1406. 929, // 165
  1407. 1078, // 166
  1408. 951, // 167
  1409. 1656, // 168
  1410. 930, // 169
  1411. 1153, // 170
  1412. 1030, // 171
  1413. 1262, // 172
  1414. 1062, // 173
  1415. 1214, // 174
  1416. 1060, // 175
  1417. 1621, // 176
  1418. 930, // 177
  1419. 1106, // 178
  1420. 912, // 179
  1421. 1034, // 180
  1422. 892, // 181
  1423. 1158, // 182
  1424. 990, // 183
  1425. 1175, // 184
  1426. 850, // 185
  1427. 1121, // 186
  1428. 903, // 187
  1429. 1087, // 188
  1430. 920, // 189
  1431. 1144, // 190
  1432. 1056, // 191
  1433. 3462, // 192
  1434. 2240, // 193
  1435. 4397, // 194
  1436. 12136, // 195
  1437. 7758, // 196
  1438. 1345, // 197
  1439. 1307, // 198
  1440. 3278, // 199
  1441. 1950, // 200
  1442. 886, // 201
  1443. 1023, // 202
  1444. 1112, // 203
  1445. 1077, // 204
  1446. 1042, // 205
  1447. 1061, // 206
  1448. 1071, // 207
  1449. 1484, // 208
  1450. 1001, // 209
  1451. 1096, // 210
  1452. 915, // 211
  1453. 1052, // 212
  1454. 995, // 213
  1455. 1070, // 214
  1456. 876, // 215
  1457. 1111, // 216
  1458. 851, // 217
  1459. 1059, // 218
  1460. 805, // 219
  1461. 1112, // 220
  1462. 923, // 221
  1463. 1103, // 222
  1464. 817, // 223
  1465. 1899, // 224
  1466. 1872, // 225
  1467. 976, // 226
  1468. 841, // 227
  1469. 1127, // 228
  1470. 956, // 229
  1471. 1159, // 230
  1472. 950, // 231
  1473. 7791, // 232
  1474. 954, // 233
  1475. 1289, // 234
  1476. 933, // 235
  1477. 1127, // 236
  1478. 3207, // 237
  1479. 1020, // 238
  1480. 927, // 239
  1481. 1355, // 240
  1482. 768, // 241
  1483. 1040, // 242
  1484. 745, // 243
  1485. 952, // 244
  1486. 805, // 245
  1487. 1073, // 246
  1488. 740, // 247
  1489. 1013, // 248
  1490. 805, // 249
  1491. 1008, // 250
  1492. 796, // 251
  1493. 996, // 252
  1494. 1057, // 253
  1495. 11457, // 254
  1496. 13504, // 255
  1497. };
  1498. void MSG_initHuffman() {
  1499. int i,j;
  1500. msgInit = qtrue;
  1501. Huff_Init(&msgHuff);
  1502. for(i=0;i<256;i++) {
  1503. for (j=0;j<msg_hData[i];j++) {
  1504. Huff_addRef(&msgHuff.compressor, (byte)i); // Do update
  1505. Huff_addRef(&msgHuff.decompressor, (byte)i); // Do update
  1506. }
  1507. }
  1508. }
  1509. /*
  1510. void MSG_NUinitHuffman() {
  1511. byte *data;
  1512. int size, i, ch;
  1513. int array[256];
  1514. msgInit = qtrue;
  1515. Huff_Init(&msgHuff);
  1516. // load it in
  1517. size = FS_ReadFile( "netchan/netchan.bin", (void **)&data );
  1518. for(i=0;i<256;i++) {
  1519. array[i] = 0;
  1520. }
  1521. for(i=0;i<size;i++) {
  1522. ch = data[i];
  1523. Huff_addRef(&msgHuff.compressor, ch); // Do update
  1524. Huff_addRef(&msgHuff.decompressor, ch); // Do update
  1525. array[ch]++;
  1526. }
  1527. Com_Printf("msg_hData {\n");
  1528. for(i=0;i<256;i++) {
  1529. if (array[i] == 0) {
  1530. Huff_addRef(&msgHuff.compressor, i); // Do update
  1531. Huff_addRef(&msgHuff.decompressor, i); // Do update
  1532. }
  1533. Com_Printf("%d, // %d\n", array[i], i);
  1534. }
  1535. Com_Printf("};\n");
  1536. FS_FreeFile( data );
  1537. Cbuf_AddText( "condump dump.txt\n" );
  1538. }
  1539. */
  1540. //===========================================================================