1
0
mirror of https://github.com/alliedmodders/hl2sdk.git synced 2024-12-23 01:59:43 +08:00
hl2sdk/game_shared/usercmd.cpp
2008-09-15 01:33:59 -05:00

304 lines
6.0 KiB
C++

//========= Copyright © 1996-2005, Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
// $NoKeywords: $
//=============================================================================//
#include "cbase.h"
#include "usercmd.h"
#include "bitbuf.h"
#include "checksum_md5.h"
// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"
#define WEAPON_SUBTYPE_BITS 6
//-----------------------------------------------------------------------------
// Purpose: Write a delta compressed user command.
// Input : *buf -
// *to -
// *from -
// Output : static
//-----------------------------------------------------------------------------
void WriteUsercmd( bf_write *buf, CUserCmd *to, CUserCmd *from )
{
if ( to->command_number != ( from->command_number + 1 ) )
{
buf->WriteOneBit( 1 );
buf->WriteUBitLong( to->command_number, 32 );
}
else
{
buf->WriteOneBit( 0 );
}
if ( to->tick_count != ( from->tick_count + 1 ) )
{
buf->WriteOneBit( 1 );
buf->WriteUBitLong( to->tick_count, 32 );
}
else
{
buf->WriteOneBit( 0 );
}
if ( to->viewangles[ 0 ] != from->viewangles[ 0 ] )
{
buf->WriteOneBit( 1 );
buf->WriteBitAngle( to->viewangles[ 0 ], 16 );
}
else
{
buf->WriteOneBit( 0 );
}
if ( to->viewangles[ 1 ] != from->viewangles[ 1 ] )
{
buf->WriteOneBit( 1 );
buf->WriteBitAngle( to->viewangles[ 1 ], 16 );
}
else
{
buf->WriteOneBit( 0 );
}
if ( to->viewangles[ 2 ] != from->viewangles[ 2 ] )
{
buf->WriteOneBit( 1 );
buf->WriteBitAngle( to->viewangles[ 2 ], 8 );
}
else
{
buf->WriteOneBit( 0 );
}
if ( to->forwardmove != from->forwardmove )
{
buf->WriteOneBit( 1 );
buf->WriteSBitLong( static_cast<int>(to->forwardmove), 16 );
}
else
{
buf->WriteOneBit( 0 );
}
if ( to->sidemove != from->sidemove )
{
buf->WriteOneBit( 1 );
buf->WriteSBitLong( static_cast<int>(to->sidemove), 16 );
}
else
{
buf->WriteOneBit( 0 );
}
if ( to->upmove != from->upmove )
{
buf->WriteOneBit( 1 );
buf->WriteSBitLong( static_cast<int>(to->upmove), 16 );
}
else
{
buf->WriteOneBit( 0 );
}
if ( to->buttons != from->buttons )
{
buf->WriteOneBit( 1 );
buf->WriteUBitLong( to->buttons, 32 );
}
else
{
buf->WriteOneBit( 0 );
}
if ( to->impulse != from->impulse )
{
buf->WriteOneBit( 1 );
buf->WriteUBitLong( to->impulse, 8 );
}
else
{
buf->WriteOneBit( 0 );
}
if ( to->weaponselect != from->weaponselect )
{
buf->WriteOneBit( 1 );
buf->WriteUBitLong( to->weaponselect, MAX_EDICT_BITS );
if ( to->weaponsubtype != from->weaponsubtype )
{
buf->WriteOneBit( 1 );
buf->WriteUBitLong( to->weaponsubtype, WEAPON_SUBTYPE_BITS );
}
else
{
buf->WriteOneBit( 0 );
}
}
else
{
buf->WriteOneBit( 0 );
}
// TODO: Can probably get away with fewer bits.
if ( to->mousedx != from->mousedx )
{
buf->WriteOneBit( 1 );
buf->WriteShort( to->mousedx );
}
else
{
buf->WriteOneBit( 0 );
}
if ( to->mousedy != from->mousedy )
{
buf->WriteOneBit( 1 );
buf->WriteShort( to->mousedy );
}
else
{
buf->WriteOneBit( 0 );
}
#if defined( HL2_CLIENT_DLL )
if ( to->entitygroundcontact.Count() != 0 )
{
buf->WriteOneBit( 1 );
buf->WriteShort( to->entitygroundcontact.Count() );
int i;
for (i = 0; i < to->entitygroundcontact.Count(); i++)
{
buf->WriteUBitLong( to->entitygroundcontact[i].entindex, MAX_EDICT_BITS );
buf->WriteBitCoord( to->entitygroundcontact[i].minheight );
buf->WriteBitCoord( to->entitygroundcontact[i].maxheight );
}
to->entitygroundcontact.RemoveAll();
}
else
{
buf->WriteOneBit( 0 );
}
#endif
}
//-----------------------------------------------------------------------------
// Purpose: Read in a delta compressed usercommand.
// Input : *buf -
// *move -
// *from -
// Output : static void ReadUsercmd
//-----------------------------------------------------------------------------
void ReadUsercmd( bf_read *buf, CUserCmd *move, CUserCmd *from )
{
// Assume no change
*move = *from;
if ( buf->ReadOneBit() )
{
move->command_number = buf->ReadUBitLong( 32 );
}
else
{
// Assume steady increment
move->command_number = from->command_number + 1;
}
if ( buf->ReadOneBit() )
{
move->tick_count = buf->ReadUBitLong( 32 );
}
else
{
// Assume steady increment
move->tick_count = from->tick_count + 1;
}
// Read direction
if ( buf->ReadOneBit() )
{
move->viewangles[0] = buf->ReadBitAngle( 16 );
}
if ( buf->ReadOneBit() )
{
move->viewangles[1] = buf->ReadBitAngle( 16 );
}
if ( buf->ReadOneBit() )
{
move->viewangles[2] = buf->ReadBitAngle( 8 );
}
// Read movement
if ( buf->ReadOneBit() )
{
move->forwardmove = buf->ReadSBitLong( 16 );
}
if ( buf->ReadOneBit() )
{
move->sidemove = buf->ReadSBitLong( 16 );
}
if ( buf->ReadOneBit() )
{
move->upmove = buf->ReadSBitLong( 16 );
}
// read buttons
if ( buf->ReadOneBit() )
{
move->buttons = buf->ReadUBitLong( 32 );
}
if ( buf->ReadOneBit() )
{
move->impulse = buf->ReadUBitLong( 8 );
}
if ( buf->ReadOneBit() )
{
move->weaponselect = buf->ReadUBitLong( MAX_EDICT_BITS );
if ( buf->ReadOneBit() )
{
move->weaponsubtype = buf->ReadUBitLong( WEAPON_SUBTYPE_BITS );
}
}
move->random_seed = MD5_PseudoRandom( move->command_number ) & 0x7fffffff;
if ( buf->ReadOneBit() )
{
move->mousedx = buf->ReadShort();
}
if ( buf->ReadOneBit() )
{
move->mousedy = buf->ReadShort();
}
#if defined( HL2_DLL )
if ( buf->ReadOneBit() )
{
move->entitygroundcontact.SetCount( buf->ReadShort() );
int i;
for (i = 0; i < move->entitygroundcontact.Count(); i++)
{
move->entitygroundcontact[i].entindex = buf->ReadUBitLong( MAX_EDICT_BITS );
move->entitygroundcontact[i].minheight = buf->ReadBitCoord( );
move->entitygroundcontact[i].maxheight = buf->ReadBitCoord( );
}
}
#endif
}