css_enhanced_waf/game/shared/usercmd.cpp

382 lines
7.6 KiB
C++

//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
// $NoKeywords: $
//=============================================================================//
#include "cbase.h"
#include "usercmd.h"
#include "bitbuf.h"
#include "checksum_md5.h"
#include "const.h"
#include "utlvector.h"
#include "shareddefs.h"
// memdbgon must be the last include file in a .cpp file!!!
#ifdef CLIENT_DLL
#include "c_baseplayer.h"
#include "cliententitylist.h"
#else
#include "player.h"
#endif
#include "tier0/memdbgon.h"
// TF2 specific, need enough space for OBJ_LAST items from tf_shareddefs.h
#define WEAPON_SUBTYPE_BITS 6
//-----------------------------------------------------------------------------
// Purpose: Write a delta compressed user command.
// Input : *buf -
// *to -
// *from -
// Output : static
//-----------------------------------------------------------------------------
void WriteUsercmd( bf_write *buf, const CUserCmd *to, const 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->WriteFloat( to->viewangles[ 0 ] );
}
else
{
buf->WriteOneBit( 0 );
}
if ( to->viewangles[ 1 ] != from->viewangles[ 1 ] )
{
buf->WriteOneBit( 1 );
buf->WriteFloat( to->viewangles[ 1 ] );
}
else
{
buf->WriteOneBit( 0 );
}
if ( to->viewangles[ 2 ] != from->viewangles[ 2 ] )
{
buf->WriteOneBit( 1 );
buf->WriteFloat( to->viewangles[ 2 ] );
}
else
{
buf->WriteOneBit( 0 );
}
if ( to->forwardmove != from->forwardmove )
{
buf->WriteOneBit( 1 );
buf->WriteFloat( to->forwardmove );
}
else
{
buf->WriteOneBit( 0 );
}
if ( to->sidemove != from->sidemove )
{
buf->WriteOneBit( 1 );
buf->WriteFloat( to->sidemove );
}
else
{
buf->WriteOneBit( 0 );
}
if ( to->upmove != from->upmove )
{
buf->WriteOneBit( 1 );
buf->WriteFloat( to->upmove );
}
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 );
}
#ifdef CLIENT_DLL
int highestEntityIndex = 0;
if (cl_entitylist)
highestEntityIndex = cl_entitylist->GetHighestEntityIndex();
#else
static constexpr auto highestEntityIndex = MAX_EDICTS-1;
#endif
// Write entity count
buf->WriteUBitLong(highestEntityIndex, 11);
// Write finally simulation data with entity index
for (unsigned int i = 0; i <= highestEntityIndex; i++)
{
if (from->simulationdata[i].m_flInterpolatedSimulationTime
!= to->simulationdata[i].m_flInterpolatedSimulationTime)
{
buf->WriteOneBit(1);
buf->WriteBitFloat(to->simulationdata[i].m_flInterpolatedSimulationTime);
}
else
{
buf->WriteOneBit(0);
}
if (from->simulationdata[i].m_flSimulationTime != to->simulationdata[i].m_flSimulationTime)
{
buf->WriteOneBit(1);
buf->WriteBitFloat(to->simulationdata[i].m_flSimulationTime);
}
else
{
buf->WriteOneBit(0);
}
}
if (to->debug_hitboxes != from->debug_hitboxes)
{
buf->WriteOneBit(1);
buf->WriteUBitLong(to->debug_hitboxes, 2);
}
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 );
}
}
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->ReadFloat();
}
if ( buf->ReadOneBit() )
{
move->viewangles[1] = buf->ReadFloat();
}
if ( buf->ReadOneBit() )
{
move->viewangles[2] = buf->ReadFloat();
}
// Moved value validation and clamping to CBasePlayer::ProcessUsercmds()
// Read movement
if ( buf->ReadOneBit() )
{
move->forwardmove = buf->ReadFloat();
}
if ( buf->ReadOneBit() )
{
move->sidemove = buf->ReadFloat();
}
if ( buf->ReadOneBit() )
{
move->upmove = buf->ReadFloat();
}
// 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();
}
const auto highestEntityIndex = buf->ReadUBitLong(11);
for (unsigned int i = 0; i <= highestEntityIndex;i++)
{
if (buf->ReadOneBit())
{
move->simulationdata[i].m_flInterpolatedSimulationTime = buf->ReadBitFloat();
}
if (buf->ReadOneBit())
{
move->simulationdata[i].m_flSimulationTime = buf->ReadBitFloat();
}
}
if ( buf->ReadOneBit() )
{
move->debug_hitboxes = (CUserCmd::debug_hitboxes_t)buf->ReadUBitLong(2);
}
#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
}