Skip to content

Commit

Permalink
Client: Input: Sanitize sensitivity
Browse files Browse the repository at this point in the history
  • Loading branch information
tmp64 committed Aug 24, 2024
1 parent 1280ee9 commit 5880d65
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/game/client/hud_msg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ void ClearEventList(void);
#endif

extern ConVar zoom_sensitivity_ratio;
extern cvar_t *sensitivity;
extern float g_lastFOV;

cvar_t *cl_lw = nullptr;

void CAM_ToFirstPerson(void);
float IN_GetMouseSensitivity();

/// USER-DEFINED SERVER MESSAGE HANDLERS

Expand Down Expand Up @@ -177,7 +177,7 @@ int CHud::MsgFunc_SetFOV(const char *pszName, int iSize, void *pbuf)
else
{
// set a new sensitivity that is proportional to the change from the FOV default
m_flMouseSensitivity = sensitivity->value * ((float)newfov / (float)def_fov) * zoom_sensitivity_ratio.GetFloat();
m_flMouseSensitivity = IN_GetMouseSensitivity() * ((float)newfov / (float)def_fov) * zoom_sensitivity_ratio.GetFloat();
}

// Update crosshair after zoom change
Expand Down
4 changes: 2 additions & 2 deletions src/game/client/hud_redraw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ int grgLogoFrame[MAX_LOGO_FRAMES] = {
};

float HUD_GetFOV(void);
float IN_GetMouseSensitivity();

extern cvar_t *sensitivity;
extern ConVar zoom_sensitivity_ratio;

ConVar hud_colortext("hud_colortext", "1", FCVAR_BHL_ARCHIVE);
Expand Down Expand Up @@ -82,7 +82,7 @@ void CHud::Think(void)
else
{
// set a new sensitivity that is proportional to the change from the FOV default
m_flMouseSensitivity = sensitivity->value * ((float)newfov / (float)default_fov.GetInt()) * zoom_sensitivity_ratio.GetFloat();
m_flMouseSensitivity = IN_GetMouseSensitivity() * ((float)newfov / (float)default_fov.GetInt()) * zoom_sensitivity_ratio.GetFloat();
}

// think about default fov
Expand Down
24 changes: 23 additions & 1 deletion src/game/client/inputw32.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -635,6 +635,28 @@ void IN_GetMousePos(int *mx, int *my)
gEngfuncs.GetMousePosition(mx, my);
}

/*
===========
IN_GetMouseSensitivity
Get mouse sensitivity with sanitization
===========
*/
float IN_GetMouseSensitivity()
{
// Absurdly high sensitivity values can cause the game to hang, so clamp
if (sensitivity->value > 10000.0)
{
gEngfuncs.Cvar_SetValue(sensitivity->name, 10000.0);
}
else if (sensitivity->value < 0.01)
{
gEngfuncs.Cvar_SetValue(sensitivity->name, 0.01);
}

return sensitivity->value;
}

/*
===========
IN_ResetMouse
Expand Down Expand Up @@ -714,7 +736,7 @@ void IN_ScaleMouse(float *x, float *y)
float my = *y;

// This is the default sensitivity
float mouse_senstivity = (gHUD.GetSensitivity() != 0) ? gHUD.GetSensitivity() : sensitivity->value;
float mouse_senstivity = (gHUD.GetSensitivity() != 0) ? gHUD.GetSensitivity() : IN_GetMouseSensitivity();

// Using special accleration values
if (m_customaccel->value != 0)
Expand Down

0 comments on commit 5880d65

Please sign in to comment.