Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

306 lines
5.7 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #include "cbase.h"
  8. #include "usercmd.h"
  9. #include "bitbuf.h"
  10. #include "checksum_md5.h"
  11. // memdbgon must be the last include file in a .cpp file!!!
  12. #include "tier0/memdbgon.h"
  13. // TF2 specific, need enough space for OBJ_LAST items from tf_shareddefs.h
  14. #define WEAPON_SUBTYPE_BITS 6
  15. //-----------------------------------------------------------------------------
  16. // Purpose: Write a delta compressed user command.
  17. // Input : *buf -
  18. // *to -
  19. // *from -
  20. // Output : static
  21. //-----------------------------------------------------------------------------
  22. void WriteUsercmd( bf_write *buf, const CUserCmd *to, const CUserCmd *from )
  23. {
  24. if ( to->command_number != ( from->command_number + 1 ) )
  25. {
  26. buf->WriteOneBit( 1 );
  27. buf->WriteUBitLong( to->command_number, 32 );
  28. }
  29. else
  30. {
  31. buf->WriteOneBit( 0 );
  32. }
  33. if ( to->tick_count != ( from->tick_count + 1 ) )
  34. {
  35. buf->WriteOneBit( 1 );
  36. buf->WriteUBitLong( to->tick_count, 32 );
  37. }
  38. else
  39. {
  40. buf->WriteOneBit( 0 );
  41. }
  42. if ( to->viewangles[ 0 ] != from->viewangles[ 0 ] )
  43. {
  44. buf->WriteOneBit( 1 );
  45. buf->WriteFloat( to->viewangles[ 0 ] );
  46. }
  47. else
  48. {
  49. buf->WriteOneBit( 0 );
  50. }
  51. if ( to->viewangles[ 1 ] != from->viewangles[ 1 ] )
  52. {
  53. buf->WriteOneBit( 1 );
  54. buf->WriteFloat( to->viewangles[ 1 ] );
  55. }
  56. else
  57. {
  58. buf->WriteOneBit( 0 );
  59. }
  60. if ( to->viewangles[ 2 ] != from->viewangles[ 2 ] )
  61. {
  62. buf->WriteOneBit( 1 );
  63. buf->WriteFloat( to->viewangles[ 2 ] );
  64. }
  65. else
  66. {
  67. buf->WriteOneBit( 0 );
  68. }
  69. if ( to->forwardmove != from->forwardmove )
  70. {
  71. buf->WriteOneBit( 1 );
  72. buf->WriteFloat( to->forwardmove );
  73. }
  74. else
  75. {
  76. buf->WriteOneBit( 0 );
  77. }
  78. if ( to->sidemove != from->sidemove )
  79. {
  80. buf->WriteOneBit( 1 );
  81. buf->WriteFloat( to->sidemove );
  82. }
  83. else
  84. {
  85. buf->WriteOneBit( 0 );
  86. }
  87. if ( to->upmove != from->upmove )
  88. {
  89. buf->WriteOneBit( 1 );
  90. buf->WriteFloat( to->upmove );
  91. }
  92. else
  93. {
  94. buf->WriteOneBit( 0 );
  95. }
  96. if ( to->buttons != from->buttons )
  97. {
  98. buf->WriteOneBit( 1 );
  99. buf->WriteUBitLong( to->buttons, 32 );
  100. }
  101. else
  102. {
  103. buf->WriteOneBit( 0 );
  104. }
  105. if ( to->impulse != from->impulse )
  106. {
  107. buf->WriteOneBit( 1 );
  108. buf->WriteUBitLong( to->impulse, 8 );
  109. }
  110. else
  111. {
  112. buf->WriteOneBit( 0 );
  113. }
  114. if ( to->weaponselect != from->weaponselect )
  115. {
  116. buf->WriteOneBit( 1 );
  117. buf->WriteUBitLong( to->weaponselect, MAX_EDICT_BITS );
  118. if ( to->weaponsubtype != from->weaponsubtype )
  119. {
  120. buf->WriteOneBit( 1 );
  121. buf->WriteUBitLong( to->weaponsubtype, WEAPON_SUBTYPE_BITS );
  122. }
  123. else
  124. {
  125. buf->WriteOneBit( 0 );
  126. }
  127. }
  128. else
  129. {
  130. buf->WriteOneBit( 0 );
  131. }
  132. // TODO: Can probably get away with fewer bits.
  133. if ( to->mousedx != from->mousedx )
  134. {
  135. buf->WriteOneBit( 1 );
  136. buf->WriteShort( to->mousedx );
  137. }
  138. else
  139. {
  140. buf->WriteOneBit( 0 );
  141. }
  142. if ( to->mousedy != from->mousedy )
  143. {
  144. buf->WriteOneBit( 1 );
  145. buf->WriteShort( to->mousedy );
  146. }
  147. else
  148. {
  149. buf->WriteOneBit( 0 );
  150. }
  151. #if defined( HL2_CLIENT_DLL )
  152. if ( to->entitygroundcontact.Count() != 0 )
  153. {
  154. buf->WriteOneBit( 1 );
  155. buf->WriteShort( to->entitygroundcontact.Count() );
  156. int i;
  157. for (i = 0; i < to->entitygroundcontact.Count(); i++)
  158. {
  159. buf->WriteUBitLong( to->entitygroundcontact[i].entindex, MAX_EDICT_BITS );
  160. buf->WriteBitCoord( to->entitygroundcontact[i].minheight );
  161. buf->WriteBitCoord( to->entitygroundcontact[i].maxheight );
  162. }
  163. }
  164. else
  165. {
  166. buf->WriteOneBit( 0 );
  167. }
  168. #endif
  169. }
  170. //-----------------------------------------------------------------------------
  171. // Purpose: Read in a delta compressed usercommand.
  172. // Input : *buf -
  173. // *move -
  174. // *from -
  175. // Output : static void ReadUsercmd
  176. //-----------------------------------------------------------------------------
  177. void ReadUsercmd( bf_read *buf, CUserCmd *move, CUserCmd *from )
  178. {
  179. // Assume no change
  180. *move = *from;
  181. if ( buf->ReadOneBit() )
  182. {
  183. move->command_number = buf->ReadUBitLong( 32 );
  184. }
  185. else
  186. {
  187. // Assume steady increment
  188. move->command_number = from->command_number + 1;
  189. }
  190. if ( buf->ReadOneBit() )
  191. {
  192. move->tick_count = buf->ReadUBitLong( 32 );
  193. }
  194. else
  195. {
  196. // Assume steady increment
  197. move->tick_count = from->tick_count + 1;
  198. }
  199. // Read direction
  200. if ( buf->ReadOneBit() )
  201. {
  202. move->viewangles[0] = buf->ReadFloat();
  203. }
  204. if ( buf->ReadOneBit() )
  205. {
  206. move->viewangles[1] = buf->ReadFloat();
  207. }
  208. if ( buf->ReadOneBit() )
  209. {
  210. move->viewangles[2] = buf->ReadFloat();
  211. }
  212. // Moved value validation and clamping to CBasePlayer::ProcessUsercmds()
  213. // Read movement
  214. if ( buf->ReadOneBit() )
  215. {
  216. move->forwardmove = buf->ReadFloat();
  217. }
  218. if ( buf->ReadOneBit() )
  219. {
  220. move->sidemove = buf->ReadFloat();
  221. }
  222. if ( buf->ReadOneBit() )
  223. {
  224. move->upmove = buf->ReadFloat();
  225. }
  226. // read buttons
  227. if ( buf->ReadOneBit() )
  228. {
  229. move->buttons = buf->ReadUBitLong( 32 );
  230. }
  231. if ( buf->ReadOneBit() )
  232. {
  233. move->impulse = buf->ReadUBitLong( 8 );
  234. }
  235. if ( buf->ReadOneBit() )
  236. {
  237. move->weaponselect = buf->ReadUBitLong( MAX_EDICT_BITS );
  238. if ( buf->ReadOneBit() )
  239. {
  240. move->weaponsubtype = buf->ReadUBitLong( WEAPON_SUBTYPE_BITS );
  241. }
  242. }
  243. move->random_seed = MD5_PseudoRandom( move->command_number ) & 0x7fffffff;
  244. if ( buf->ReadOneBit() )
  245. {
  246. move->mousedx = buf->ReadShort();
  247. }
  248. if ( buf->ReadOneBit() )
  249. {
  250. move->mousedy = buf->ReadShort();
  251. }
  252. #if defined( HL2_DLL )
  253. if ( buf->ReadOneBit() )
  254. {
  255. move->entitygroundcontact.SetCount( buf->ReadShort() );
  256. int i;
  257. for (i = 0; i < move->entitygroundcontact.Count(); i++)
  258. {
  259. move->entitygroundcontact[i].entindex = buf->ReadUBitLong( MAX_EDICT_BITS );
  260. move->entitygroundcontact[i].minheight = buf->ReadBitCoord( );
  261. move->entitygroundcontact[i].maxheight = buf->ReadBitCoord( );
  262. }
  263. }
  264. #endif
  265. }