Skip to content

Commit

Permalink
Library: speedometer mode: fixed working in demo playback
Browse files Browse the repository at this point in the history
  • Loading branch information
SNMetamorph committed Mar 7, 2023
1 parent 3de1698 commit 5a85a7c
Show file tree
Hide file tree
Showing 10 changed files with 87 additions and 23 deletions.
3 changes: 3 additions & 0 deletions sources/library/displaymode_angletracking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ CModeAngleTracking::CModeAngleTracking()

void CModeAngleTracking::Render2D(float frameTime, int scrWidth, int scrHeight, CStringStack &screenText)
{
if (!g_LocalPlayer.PlayerMoveAvailable())
return;

const float threshold = 0.001f;
const vec3_t &currAngles = g_LocalPlayer.GetAngles();
float pitchVelocity = (currAngles.x - m_vecLastAngles.x) / g_pPlayerMove->frametime;
Expand Down
3 changes: 3 additions & 0 deletions sources/library/displaymode_entityreport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ CModeEntityReport::CModeEntityReport()

void CModeEntityReport::Render2D(float frameTime, int scrWidth, int scrHeight, CStringStack &screenText)
{
if (!g_LocalPlayer.PlayerMoveAvailable())
return;

int debugMode = ConVars::gsm_debug->value;
if (!g_EntityDictionary.IsInitialized())
g_EntityDictionary.Initialize();
Expand Down
3 changes: 3 additions & 0 deletions sources/library/displaymode_facereport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ CModeFaceReport::CModeFaceReport()

void CModeFaceReport::Render2D(float frameTime, int scrWidth, int scrHeight, CStringStack &screenText)
{
if (!g_LocalPlayer.PlayerMoveAvailable())
return;

const float lineLen = 11590.0f;
vec3_t intersectPoint;
vec3_t viewOrigin = g_LocalPlayer.GetViewOrigin();
Expand Down
3 changes: 3 additions & 0 deletions sources/library/displaymode_full.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ CModeFull::CModeFull()

void CModeFull::Render2D(float frameTime, int scrWidth, int scrHeight, CStringStack &screenText)
{
if (!g_LocalPlayer.PlayerMoveAvailable())
return;

float timeDelta = GetSmoothSystemFrametime();
float velocityNum = g_LocalPlayer.GetVelocityHorz();
const vec3_t &origin = g_LocalPlayer.GetOrigin();
Expand Down
3 changes: 3 additions & 0 deletions sources/library/displaymode_measurement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,9 @@ void CModeMeasurement::LoadLineSprite()

void CModeMeasurement::Render2D(float frameTime, int screenWidth, int screenHeight, CStringStack &screenText)
{
if (!g_LocalPlayer.PlayerMoveAvailable())
return;

const vec3_t &originPointA = GetPointOriginA();
const vec3_t &originPointB = GetPointOriginB();
float pointsDistance = GetPointsDistance();
Expand Down
58 changes: 50 additions & 8 deletions sources/library/displaymode_speedometer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ void CModeSpeedometer::Render2D(float frameTime, int scrWidth, int scrHeight, CS
);
}

void CModeSpeedometer::DrawVelocityBar(int centerX, int centerY, float velocity)
void CModeSpeedometer::DrawVelocityBar(int centerX, int centerY, float velocity) const
{
const int barHeight = 15;
const int barMargin = 60;
Expand All @@ -46,14 +46,56 @@ void CModeSpeedometer::DrawVelocityBar(int centerX, int centerY, float velocity)

void CModeSpeedometer::CalculateVelocity(float frameTime)
{
if (g_LocalPlayer.IsSpectate())
{
int targetIndex = g_LocalPlayer.GetSpectateTargetIndex();
if (g_pClientEngfuncs->GetEntityByIndex(targetIndex) != nullptr) {
m_flVelocity = Utils::GetEntityVelocityApprox(targetIndex).Length2D();
}
if (LocalPlayerSpectating()) {
m_flVelocity = GetEntityVelocityApprox(GetSpectatedTargetIndex());
}
else {
m_flVelocity = (g_pPlayerMove->velocity + g_pPlayerMove->basevelocity).Length2D();
m_flVelocity = GetLocalPlayerVelocity();
}
}

float CModeSpeedometer::GetEntityVelocityApprox(int entityIndex) const
{
if (g_pClientEngfuncs->GetEntityByIndex(entityIndex)) {
return Utils::GetEntityVelocityApprox(entityIndex).Length2D();
}
return 0.0f;
}

int CModeSpeedometer::GetSpectatedTargetIndex() const
{
if (g_LocalPlayer.PlayerMoveAvailable()) {
return g_LocalPlayer.GetSpectateTargetIndex();
}
else
{
cl_entity_t *localPlayer = g_pClientEngfuncs->GetLocalPlayer();
return localPlayer->curstate.iuser2;
}
}

bool CModeSpeedometer::LocalPlayerSpectating() const
{
if (g_LocalPlayer.PlayerMoveAvailable()) {
return g_LocalPlayer.IsSpectate();
}
else
{
cl_entity_t *localPlayer = g_pClientEngfuncs->GetLocalPlayer();
int specMode = localPlayer->curstate.iuser1;
int targetIndex = localPlayer->curstate.iuser2;
return specMode != 0 && targetIndex != 0;
}
}

float CModeSpeedometer::GetLocalPlayerVelocity() const
{
if (g_LocalPlayer.PlayerMoveAvailable()) {
return (g_pPlayerMove->velocity + g_pPlayerMove->basevelocity).Length2D();
}
else
{
cl_entity_t *localPlayer = g_pClientEngfuncs->GetLocalPlayer();
return Utils::GetEntityVelocityApprox(localPlayer->index).Length2D();
}
}
6 changes: 5 additions & 1 deletion sources/library/displaymode_speedometer.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ class CModeSpeedometer : public IDisplayMode

private:
void CalculateVelocity(float frameTime);
void DrawVelocityBar(int scrWidth, int scrHeight, float velocity);
float GetEntityVelocityApprox(int entityIndex) const;
int GetSpectatedTargetIndex() const;
bool LocalPlayerSpectating() const;
float GetLocalPlayerVelocity() const;
void DrawVelocityBar(int scrWidth, int scrHeight, float velocity) const;

float m_flVelocity;
float m_flLastUpdateTime;
Expand Down
12 changes: 4 additions & 8 deletions sources/library/hooks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,22 @@ NOINLINE static int __cdecl HookRedraw(float time, int intermission)
{
// call original function
PLH::FnCast(g_hookRedraw.GetTrampolineAddr(), CHooks::pfnRedraw_t())(time, intermission);
if (g_pPlayerMove)
{
bool isIntermission = intermission != 0;
g_Application.DisplayModeRender2D();
}
g_Application.DisplayModeRender2D();
return 1;
}

NOINLINE static void __cdecl HookPlayerMove(playermove_t *pmove, int server)
{
PLH::FnCast(g_hookPlayerMove.GetTrampolineAddr(), CHooks::pfnPlayerMove_t())(pmove, server);
g_LocalPlayer.Setup(pmove);
g_LocalPlayer.UpdatePlayerMove(pmove);
}

NOINLINE static int __cdecl HookKeyEvent(int keyDown, int keyCode, const char *bindName)
{
int returnCode = PLH::FnCast(g_hookKeyEvent.GetTrampolineAddr(), CHooks::pfnKeyEvent_t())(
keyDown, keyCode, bindName
);
return returnCode && g_Application.KeyInput(keyDown, keyCode, bindName);
return (returnCode && g_Application.KeyInput(keyDown, keyCode, bindName)) ? 1 : 0;
}

NOINLINE static void __cdecl HookDrawTriangles()
Expand All @@ -48,7 +44,7 @@ NOINLINE static void __cdecl HookDrawTriangles()
NOINLINE static int __cdecl HookIsThirdPerson()
{
int returnCode = PLH::FnCast(g_hookIsThirdPerson.GetTrampolineAddr(), CHooks::pfnIsThirdPerson_t())();
return returnCode || g_LocalPlayer.IsThirdPersonForced();
return (returnCode || g_LocalPlayer.IsThirdPersonForced()) ? 1 : 0;
}

NOINLINE static void __cdecl HookCameraOffset(float *cameraOffset)
Expand Down
15 changes: 10 additions & 5 deletions sources/library/local_player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ CLocalPlayer &CLocalPlayer::GetInstance()
return instance;
}

void CLocalPlayer::Setup(playermove_t *pmove)
void CLocalPlayer::UpdatePlayerMove(playermove_t *pmove)
{
m_pPlayerMove = pmove;
}
Expand Down Expand Up @@ -83,12 +83,17 @@ float CLocalPlayer::GetThirdPersonCameraDist() const
return maxDist * traceInfo.fraction;
}

bool CLocalPlayer::PlayerMoveAvailable() const
{
// we can't use player move when demo playing
bool demoPlaying = g_pClientEngfuncs->pDemoAPI->IsPlayingback() != 0;
return m_pPlayerMove != nullptr && !demoPlaying;
}

bool CLocalPlayer::IsSpectate() const
{
/*
assume that it's valid only for cs 1.6/hl1
because other mods can use iuser variables for other purposes
*/
// assume that it's valid only for cs 1.6/hl1
// because other mods can use iuser variables for other purposes
int specMode = m_pPlayerMove->iuser1;
int targetIndex = m_pPlayerMove->iuser2;
return specMode != 0 && targetIndex != 0;
Expand Down
4 changes: 3 additions & 1 deletion sources/library/local_player.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class CLocalPlayer
{
public:
static CLocalPlayer &GetInstance();
void Setup(playermove_t *pmove);
void UpdatePlayerMove(playermove_t *pmove);
playermove_t* &GetPlayerMove();

const vec3_t &GetOrigin() const;
Expand All @@ -19,10 +19,12 @@ class CLocalPlayer
vec3_t GetViewOrigin() const;
vec3_t GetViewDirection() const;

bool PlayerMoveAvailable() const;
bool IsSpectate() const;
int GetSpectateTargetIndex() const;
bool IsThirdPersonForced() const;
float GetThirdPersonCameraDist() const;

private:
CLocalPlayer() : m_pPlayerMove(nullptr) {};
~CLocalPlayer() {};
Expand Down

0 comments on commit 5a85a7c

Please sign in to comment.