r5sdk/r5dev/game/server/player.cpp

195 lines
6.0 KiB
C++
Raw Normal View History

//======== Copyright (c) Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
//===========================================================================//
#include "core/stdafx.h"
#include "common/protocol.h"
#include "game/shared/shareddefs.h"
#include "game/shared/usercmd.h"
#include "game/server/movehelper_server.h"
#include "gameinterface.h"
#include "player.h"
#include "engine/server/server.h"
//------------------------------------------------------------------------------
// Purpose: executes a null command for this player
//------------------------------------------------------------------------------
void CPlayer::RunNullCommand(void)
{
CUserCmd cmd;
2023-02-12 15:06:08 +01:00
float flOldFrameTime = (*g_pGlobals)->m_flFrameTime;
float flOldCurTime = (*g_pGlobals)->m_flCurTime;
pl.fixangle = FIXANGLE_NONE;
EyeAngles(&cmd.viewangles);
2023-02-12 15:06:08 +01:00
SetTimeBase((*g_pGlobals)->m_flCurTime);
MoveHelperServer()->SetHost(this);
PlayerRunCommand(&cmd, MoveHelperServer());
SetLastUserCommand(&cmd);
2023-02-12 15:06:08 +01:00
(*g_pGlobals)->m_flFrameTime = flOldFrameTime;
(*g_pGlobals)->m_flCurTime = flOldCurTime;
2023-01-19 21:45:15 +01:00
MoveHelperServer()->SetHost(NULL);
}
//------------------------------------------------------------------------------
// Purpose: gets the eye angles of this player
// Input : *pAngles -
// Output : QAngle*
//------------------------------------------------------------------------------
QAngle* CPlayer::EyeAngles(QAngle* pAngles)
{
return v_CPlayer__EyeAngles(this, pAngles);
}
//------------------------------------------------------------------------------
// Purpose: sets the time base for this player
// Input : flTimeBase -
//------------------------------------------------------------------------------
inline void CPlayer::SetTimeBase(float flTimeBase)
{
float flTime = float(TIME_TO_TICKS(flTimeBase));
if (flTime < 0.0f)
flTime = 0.0f;
SetLastUCmdSimulationRemainderTime(flTime);
2023-02-12 15:06:08 +01:00
float flSomeTime = flTimeBase - m_lastUCmdSimulationRemainderTime * (*g_pGlobals)->m_flTickInterval;
if (flSomeTime >= 0.0)
{
flTime = flSomeTime;
}
SetTotalExtraClientCmdTimeAttempted(flTime);
}
//------------------------------------------------------------------------------
// Purpose: sets the last user cmd simulation remainder time
// Input : flRemainderTime -
//------------------------------------------------------------------------------
void CPlayer::SetLastUCmdSimulationRemainderTime(float flRemainderTime)
{
if (m_lastUCmdSimulationRemainderTime != flRemainderTime)
{
edict_t nEdict = NetworkProp()->GetEdict();
if (nEdict != FL_EDICT_INVALID)
{
2023-02-12 15:06:08 +01:00
_InterlockedOr16((SHORT*)(*g_pGlobals)->m_pEdicts + nEdict + 32, 0x200u);
}
m_lastUCmdSimulationRemainderTime = flRemainderTime;
}
}
//------------------------------------------------------------------------------
// Purpose: sets the total extra client cmd time attempted
// Input : flAttemptedTime -
//------------------------------------------------------------------------------
void CPlayer::SetTotalExtraClientCmdTimeAttempted(float flAttemptedTime)
{
if (m_totalExtraClientCmdTimeAttempted != flAttemptedTime)
{
edict_t nEdict = NetworkProp()->GetEdict();
if (nEdict != FL_EDICT_INVALID)
{
2023-02-12 15:06:08 +01:00
_InterlockedOr16((SHORT*)(*g_pGlobals)->m_pEdicts + nEdict + 32, 0x200u);
}
m_totalExtraClientCmdTimeAttempted = flAttemptedTime;
}
}
//------------------------------------------------------------------------------
// Purpose: processes user cmd's for this player
// Input : *cmds -
// numCmds -
// totalCmds -
// droppedPackets -
// paused -
//------------------------------------------------------------------------------
void CPlayer::ProcessUserCmds(CUserCmd* cmds, int numCmds, int totalCmds,
int droppedPackets, bool paused)
{
if (totalCmds <= 0)
return;
const CClient* client = g_pServer->GetClient(GetEdict() - 1);
const CNetChan* chan = client->GetNetChan();
CUserCmd* lastCmd = &m_Commands[MAX_QUEUED_COMMANDS_PROCESS];
const float latencyAmount = chan->GetLatency(FLOW_OUTGOING);
const float maxCommandDelta = sv_maxunlag_delta->GetFloat();
const float localCurTime = (*g_pGlobals)->m_flCurTime;
const float lastCommandTime = m_LastCmd.command_time;
for (int i = totalCmds - 1; i >= 0; i--)
{
CUserCmd* cmd = &cmds[i];
const float commandTime = cmd->command_time;
const float commandDelta = fabs(commandTime - localCurTime) - latencyAmount;
if (/*commandTime < lastCommandTime ||*/ commandDelta > maxCommandDelta)
{
cmd->command_time = localCurTime - latencyAmount;
if (IsDebug())
{
Warning(eDLL_T::SERVER, "%s: Command delta of %f exceeded maximum of %f; commandTime=%f, lastCommandTime=%f, localCurTime=%f, latencyAmount=%f\n",
__FUNCTION__, commandDelta, maxCommandDelta, commandTime, lastCommandTime, localCurTime, latencyAmount);
}
}
const int commandNumber = cmd->command_number;
if (commandNumber > m_latestCommandQueued)
{
m_latestCommandQueued = commandNumber;
const int lastCommandNumber = lastCmd->command_number;
if (lastCommandNumber == MAX_QUEUED_COMMANDS_PROCESS)
return;
CUserCmd* queuedCmd = &m_Commands[lastCommandNumber];
queuedCmd->Copy(cmd);
if (++lastCmd->command_number > player_userCmdsQueueWarning->GetInt())
{
const float curTime = float(Plat_FloatTime());
if ((curTime - m_lastCommandCountWarnTime) > 0.5f)
m_lastCommandCountWarnTime = curTime;
}
}
}
lastCmd->tick_count += droppedPackets;
m_bGamePaused = paused;
}
//------------------------------------------------------------------------------
// Purpose: runs user command for this player
// Input : *pUserCmd -
// *pMover -
//------------------------------------------------------------------------------
void CPlayer::PlayerRunCommand(CUserCmd* pUserCmd, IMoveHelper* pMover)
{
v_CPlayer__PlayerRunCommand(this, pUserCmd, pMover);
}
//------------------------------------------------------------------------------
// Purpose: stores off a user command
// Input : *pUserCmd -
//------------------------------------------------------------------------------
void CPlayer::SetLastUserCommand(CUserCmd* pUserCmd)
{
m_LastCmd.Copy(pUserCmd);
}