From a7229e989224261ba4b8dcb1fe39aa7e9139b122 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20M=C3=BCller?= Date: Sun, 2 Jun 2024 21:39:19 +0200 Subject: [PATCH 1/8] Use array of size `NUM_DUMMIES` instead of two variables Reduce duplicate code and improve clarity. Remove unnecessary casts to `void *`. --- src/game/client/components/controls.cpp | 82 +++++++++++-------------- 1 file changed, 35 insertions(+), 47 deletions(-) diff --git a/src/game/client/components/controls.cpp b/src/game/client/components/controls.cpp index ab7e6ae1a..136e7496a 100644 --- a/src/game/client/components/controls.cpp +++ b/src/game/client/components/controls.cpp @@ -2,6 +2,7 @@ /* If you are missing that file, acquire a complete release at teeworlds.com. */ #include +#include #include #include @@ -65,8 +66,7 @@ void CControls::OnPlayerDeath() struct CInputState { CControls *m_pControls; - int *m_pVariable1; - int *m_pVariable2; + int *m_apVariables[NUM_DUMMIES]; }; static void ConKeyInputState(IConsole::IResult *pResult, void *pUserData) @@ -76,10 +76,7 @@ static void ConKeyInputState(IConsole::IResult *pResult, void *pUserData) if(pState->m_pControls->GameClient()->m_GameInfo.m_BugDDRaceInput && pState->m_pControls->GameClient()->m_Snap.m_SpecInfo.m_Active) return; - if(g_Config.m_ClDummy) - *pState->m_pVariable2 = pResult->GetInteger(0); - else - *pState->m_pVariable1 = pResult->GetInteger(0); + *pState->m_apVariables[g_Config.m_ClDummy] = pResult->GetInteger(0); } static void ConKeyInputCounter(IConsole::IResult *pResult, void *pUserData) @@ -89,22 +86,16 @@ static void ConKeyInputCounter(IConsole::IResult *pResult, void *pUserData) if(pState->m_pControls->GameClient()->m_GameInfo.m_BugDDRaceInput && pState->m_pControls->GameClient()->m_Snap.m_SpecInfo.m_Active) return; - int *v; - if(g_Config.m_ClDummy) - v = pState->m_pVariable2; - else - v = pState->m_pVariable1; - - if(((*v) & 1) != pResult->GetInteger(0)) - (*v)++; - *v &= INPUT_STATE_MASK; + int *pVariable = pState->m_apVariables[g_Config.m_ClDummy]; + if(((*pVariable) & 1) != pResult->GetInteger(0)) + (*pVariable)++; + *pVariable &= INPUT_STATE_MASK; } struct CInputSet { CControls *m_pControls; - int *m_pVariable1; - int *m_pVariable2; + int *m_apVariables[NUM_DUMMIES]; int m_Value; }; @@ -113,10 +104,7 @@ static void ConKeyInputSet(IConsole::IResult *pResult, void *pUserData) CInputSet *pSet = (CInputSet *)pUserData; if(pResult->GetInteger(0)) { - if(g_Config.m_ClDummy) - *pSet->m_pVariable2 = pSet->m_Value; - else - *pSet->m_pVariable1 = pSet->m_Value; + *pSet->m_apVariables[g_Config.m_ClDummy] = pSet->m_Value; } } @@ -131,58 +119,58 @@ void CControls::OnConsoleInit() { // game commands { - static CInputState s_State = {this, &m_aInputDirectionLeft[0], &m_aInputDirectionLeft[1]}; - Console()->Register("+left", "", CFGFLAG_CLIENT, ConKeyInputState, (void *)&s_State, "Move left"); + static CInputState s_State = {this, {&m_aInputDirectionLeft[0], &m_aInputDirectionLeft[1]}}; + Console()->Register("+left", "", CFGFLAG_CLIENT, ConKeyInputState, &s_State, "Move left"); } { - static CInputState s_State = {this, &m_aInputDirectionRight[0], &m_aInputDirectionRight[1]}; - Console()->Register("+right", "", CFGFLAG_CLIENT, ConKeyInputState, (void *)&s_State, "Move right"); + static CInputState s_State = {this, {&m_aInputDirectionRight[0], &m_aInputDirectionRight[1]}}; + Console()->Register("+right", "", CFGFLAG_CLIENT, ConKeyInputState, &s_State, "Move right"); } { - static CInputState s_State = {this, &m_aInputData[0].m_Jump, &m_aInputData[1].m_Jump}; - Console()->Register("+jump", "", CFGFLAG_CLIENT, ConKeyInputState, (void *)&s_State, "Jump"); + static CInputState s_State = {this, {&m_aInputData[0].m_Jump, &m_aInputData[1].m_Jump}}; + Console()->Register("+jump", "", CFGFLAG_CLIENT, ConKeyInputState, &s_State, "Jump"); } { - static CInputState s_State = {this, &m_aInputData[0].m_Hook, &m_aInputData[1].m_Hook}; - Console()->Register("+hook", "", CFGFLAG_CLIENT, ConKeyInputState, (void *)&s_State, "Hook"); + static CInputState s_State = {this, {&m_aInputData[0].m_Hook, &m_aInputData[1].m_Hook}}; + Console()->Register("+hook", "", CFGFLAG_CLIENT, ConKeyInputState, &s_State, "Hook"); } { - static CInputState s_State = {this, &m_aInputData[0].m_Fire, &m_aInputData[1].m_Fire}; - Console()->Register("+fire", "", CFGFLAG_CLIENT, ConKeyInputCounter, (void *)&s_State, "Fire"); + static CInputState s_State = {this, {&m_aInputData[0].m_Fire, &m_aInputData[1].m_Fire}}; + Console()->Register("+fire", "", CFGFLAG_CLIENT, ConKeyInputCounter, &s_State, "Fire"); } { - static CInputState s_State = {this, &m_aShowHookColl[0], &m_aShowHookColl[1]}; - Console()->Register("+showhookcoll", "", CFGFLAG_CLIENT, ConKeyInputState, (void *)&s_State, "Show Hook Collision"); + static CInputState s_State = {this, {&m_aShowHookColl[0], &m_aShowHookColl[1]}}; + Console()->Register("+showhookcoll", "", CFGFLAG_CLIENT, ConKeyInputState, &s_State, "Show Hook Collision"); } { - static CInputSet s_Set = {this, &m_aInputData[0].m_WantedWeapon, &m_aInputData[1].m_WantedWeapon, 1}; - Console()->Register("+weapon1", "", CFGFLAG_CLIENT, ConKeyInputSet, (void *)&s_Set, "Switch to hammer"); + static CInputSet s_Set = {this, {&m_aInputData[0].m_WantedWeapon, &m_aInputData[1].m_WantedWeapon}, 1}; + Console()->Register("+weapon1", "", CFGFLAG_CLIENT, ConKeyInputSet, &s_Set, "Switch to hammer"); } { - static CInputSet s_Set = {this, &m_aInputData[0].m_WantedWeapon, &m_aInputData[1].m_WantedWeapon, 2}; - Console()->Register("+weapon2", "", CFGFLAG_CLIENT, ConKeyInputSet, (void *)&s_Set, "Switch to gun"); + static CInputSet s_Set = {this, {&m_aInputData[0].m_WantedWeapon, &m_aInputData[1].m_WantedWeapon}, 2}; + Console()->Register("+weapon2", "", CFGFLAG_CLIENT, ConKeyInputSet, &s_Set, "Switch to gun"); } { - static CInputSet s_Set = {this, &m_aInputData[0].m_WantedWeapon, &m_aInputData[1].m_WantedWeapon, 3}; - Console()->Register("+weapon3", "", CFGFLAG_CLIENT, ConKeyInputSet, (void *)&s_Set, "Switch to shotgun"); + static CInputSet s_Set = {this, {&m_aInputData[0].m_WantedWeapon, &m_aInputData[1].m_WantedWeapon}, 3}; + Console()->Register("+weapon3", "", CFGFLAG_CLIENT, ConKeyInputSet, &s_Set, "Switch to shotgun"); } { - static CInputSet s_Set = {this, &m_aInputData[0].m_WantedWeapon, &m_aInputData[1].m_WantedWeapon, 4}; - Console()->Register("+weapon4", "", CFGFLAG_CLIENT, ConKeyInputSet, (void *)&s_Set, "Switch to grenade"); + static CInputSet s_Set = {this, {&m_aInputData[0].m_WantedWeapon, &m_aInputData[1].m_WantedWeapon}, 4}; + Console()->Register("+weapon4", "", CFGFLAG_CLIENT, ConKeyInputSet, &s_Set, "Switch to grenade"); } { - static CInputSet s_Set = {this, &m_aInputData[0].m_WantedWeapon, &m_aInputData[1].m_WantedWeapon, 5}; - Console()->Register("+weapon5", "", CFGFLAG_CLIENT, ConKeyInputSet, (void *)&s_Set, "Switch to laser"); + static CInputSet s_Set = {this, {&m_aInputData[0].m_WantedWeapon, &m_aInputData[1].m_WantedWeapon}, 5}; + Console()->Register("+weapon5", "", CFGFLAG_CLIENT, ConKeyInputSet, &s_Set, "Switch to laser"); } { - static CInputSet s_Set = {this, &m_aInputData[0].m_NextWeapon, &m_aInputData[1].m_NextWeapon, 0}; - Console()->Register("+nextweapon", "", CFGFLAG_CLIENT, ConKeyInputNextPrevWeapon, (void *)&s_Set, "Switch to next weapon"); + static CInputSet s_Set = {this, {&m_aInputData[0].m_NextWeapon, &m_aInputData[1].m_NextWeapon}, 0}; + Console()->Register("+nextweapon", "", CFGFLAG_CLIENT, ConKeyInputNextPrevWeapon, &s_Set, "Switch to next weapon"); } { - static CInputSet s_Set = {this, &m_aInputData[0].m_PrevWeapon, &m_aInputData[1].m_PrevWeapon, 0}; - Console()->Register("+prevweapon", "", CFGFLAG_CLIENT, ConKeyInputNextPrevWeapon, (void *)&s_Set, "Switch to previous weapon"); + static CInputSet s_Set = {this, {&m_aInputData[0].m_PrevWeapon, &m_aInputData[1].m_PrevWeapon}, 0}; + Console()->Register("+prevweapon", "", CFGFLAG_CLIENT, ConKeyInputNextPrevWeapon, &s_Set, "Switch to previous weapon"); } } From b7579b0f0f1940a88b4bc3b1df32f482b92a3f72 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20M=C3=BCller?= Date: Sun, 2 Jun 2024 21:50:41 +0200 Subject: [PATCH 2/8] Ensure last input send time is reset when disconnecting Reset send time when disconnecting so input sending should always behave consistently like on the first connection. --- src/game/client/components/controls.cpp | 9 +++++---- src/game/client/components/controls.h | 1 + 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/game/client/components/controls.cpp b/src/game/client/components/controls.cpp index 136e7496a..2bfb8b06e 100644 --- a/src/game/client/components/controls.cpp +++ b/src/game/client/components/controls.cpp @@ -35,6 +35,8 @@ void CControls::OnReset() for(int &AmmoCount : m_aAmmoCount) AmmoCount = 0; m_OldMouseX = m_OldMouseY = 0.0f; + + m_LastSendTime = 0; } void CControls::ResetInput(int Dummy) @@ -188,7 +190,6 @@ void CControls::OnMessage(int Msg, void *pRawMsg) int CControls::SnapInput(int *pData) { - static int64_t LastSendTime = 0; bool Send = false; // update player state @@ -224,7 +225,7 @@ int CControls::SnapInput(int *pData) m_aInputData[g_Config.m_ClDummy].m_TargetY = (int)m_aMousePos[g_Config.m_ClDummy].y; // send once a second just to be sure - if(time_get() > LastSendTime + time_freq()) + if(time_get() > m_LastSendTime + time_freq()) Send = true; } else @@ -328,7 +329,7 @@ int CControls::SnapInput(int *pData) Send = true; // send at at least 10hz - if(time_get() > LastSendTime + time_freq() / 25) + if(time_get() > m_LastSendTime + time_freq() / 25) Send = true; if(m_pClient->m_Snap.m_pLocalCharacter && m_pClient->m_Snap.m_pLocalCharacter->m_Weapon == WEAPON_NINJA && (m_aInputData[g_Config.m_ClDummy].m_Direction || m_aInputData[g_Config.m_ClDummy].m_Jump || m_aInputData[g_Config.m_ClDummy].m_Hook)) @@ -341,7 +342,7 @@ int CControls::SnapInput(int *pData) if(!Send) return 0; - LastSendTime = time_get(); + m_LastSendTime = time_get(); mem_copy(pData, &m_aInputData[g_Config.m_ClDummy], sizeof(m_aInputData[0])); return sizeof(m_aInputData[0]); } diff --git a/src/game/client/components/controls.h b/src/game/client/components/controls.h index 67323a12a..f8ce75d71 100644 --- a/src/game/client/components/controls.h +++ b/src/game/client/components/controls.h @@ -23,6 +23,7 @@ public: int m_aAmmoCount[NUM_WEAPONS]; + int64_t m_LastSendTime; CNetObj_PlayerInput m_aInputData[NUM_DUMMIES]; CNetObj_PlayerInput m_aLastData[NUM_DUMMIES]; int m_aInputDirectionLeft[NUM_DUMMIES]; From 178a0dabfa17a9a6b4eca119e9cc6f104b60a1c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20M=C3=BCller?= Date: Sun, 2 Jun 2024 21:53:15 +0200 Subject: [PATCH 3/8] Remove dead code and unused variables --- src/game/client/components/controls.cpp | 10 ---------- src/game/client/components/controls.h | 5 ----- 2 files changed, 15 deletions(-) diff --git a/src/game/client/components/controls.cpp b/src/game/client/components/controls.cpp index 2bfb8b06e..027a67262 100644 --- a/src/game/client/components/controls.cpp +++ b/src/game/client/components/controls.cpp @@ -19,9 +19,6 @@ CControls::CControls() { mem_zero(&m_aLastData, sizeof(m_aLastData)); - m_LastDummy = 0; - m_OtherFire = 0; - mem_zero(m_aMousePos, sizeof(m_aMousePos)); mem_zero(m_aMousePosOnAction, sizeof(m_aMousePosOnAction)); mem_zero(m_aTargetPos, sizeof(m_aTargetPos)); @@ -34,7 +31,6 @@ void CControls::OnReset() for(int &AmmoCount : m_aAmmoCount) AmmoCount = 0; - m_OldMouseX = m_OldMouseY = 0.0f; m_LastSendTime = 0; } @@ -42,7 +38,6 @@ void CControls::OnReset() void CControls::ResetInput(int Dummy) { m_aLastData[Dummy].m_Direction = 0; - //m_aLastData[Dummy].m_Hook = 0; // simulate releasing the fire button if((m_aLastData[Dummy].m_Fire & 1) != 0) m_aLastData[Dummy].m_Fire++; @@ -54,11 +49,6 @@ void CControls::ResetInput(int Dummy) m_aInputDirectionRight[Dummy] = 0; } -void CControls::OnRelease() -{ - //OnReset(); -} - void CControls::OnPlayerDeath() { for(int &AmmoCount : m_aAmmoCount) diff --git a/src/game/client/components/controls.h b/src/game/client/components/controls.h index f8ce75d71..0f792df5e 100644 --- a/src/game/client/components/controls.h +++ b/src/game/client/components/controls.h @@ -18,8 +18,6 @@ public: vec2 m_aMousePos[NUM_DUMMIES]; vec2 m_aMousePosOnAction[NUM_DUMMIES]; vec2 m_aTargetPos[NUM_DUMMIES]; - float m_OldMouseX; - float m_OldMouseY; int m_aAmmoCount[NUM_WEAPONS]; @@ -29,14 +27,11 @@ public: int m_aInputDirectionLeft[NUM_DUMMIES]; int m_aInputDirectionRight[NUM_DUMMIES]; int m_aShowHookColl[NUM_DUMMIES]; - int m_LastDummy; - int m_OtherFire; CControls(); virtual int Sizeof() const override { return sizeof(*this); } virtual void OnReset() override; - virtual void OnRelease() override; virtual void OnRender() override; virtual void OnMessage(int MsgType, void *pRawMsg) override; virtual bool OnCursorMove(float x, float y, IInput::ECursorType CursorType) override; From 2904c4a2e8caf3c38f324a3a266f2c29ee1bced7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20M=C3=BCller?= Date: Sun, 2 Jun 2024 22:07:22 +0200 Subject: [PATCH 4/8] Restructure input send checks without `if`s --- src/game/client/components/controls.cpp | 38 +++++++------------------ 1 file changed, 11 insertions(+), 27 deletions(-) diff --git a/src/game/client/components/controls.cpp b/src/game/client/components/controls.cpp index 027a67262..54fca856c 100644 --- a/src/game/client/components/controls.cpp +++ b/src/game/client/components/controls.cpp @@ -180,8 +180,6 @@ void CControls::OnMessage(int Msg, void *pRawMsg) int CControls::SnapInput(int *pData) { - bool Send = false; - // update player state if(m_pClient->m_Chat.IsActive()) m_aInputData[g_Config.m_ClDummy].m_PlayerFlags = PLAYERFLAG_CHATTING; @@ -196,8 +194,7 @@ int CControls::SnapInput(int *pData) if(m_pClient->m_Controls.m_aShowHookColl[g_Config.m_ClDummy]) m_aInputData[g_Config.m_ClDummy].m_PlayerFlags |= PLAYERFLAG_AIM; - if(m_aLastData[g_Config.m_ClDummy].m_PlayerFlags != m_aInputData[g_Config.m_ClDummy].m_PlayerFlags) - Send = true; + bool Send = m_aLastData[g_Config.m_ClDummy].m_PlayerFlags != m_aInputData[g_Config.m_ClDummy].m_PlayerFlags; m_aLastData[g_Config.m_ClDummy].m_PlayerFlags = m_aInputData[g_Config.m_ClDummy].m_PlayerFlags; @@ -215,8 +212,7 @@ int CControls::SnapInput(int *pData) m_aInputData[g_Config.m_ClDummy].m_TargetY = (int)m_aMousePos[g_Config.m_ClDummy].y; // send once a second just to be sure - if(time_get() > m_LastSendTime + time_freq()) - Send = true; + Send = Send || time_get() > m_LastSendTime + time_freq(); } else { @@ -303,27 +299,15 @@ int CControls::SnapInput(int *pData) } #endif // check if we need to send input - if(m_aInputData[g_Config.m_ClDummy].m_Direction != m_aLastData[g_Config.m_ClDummy].m_Direction) - Send = true; - else if(m_aInputData[g_Config.m_ClDummy].m_Jump != m_aLastData[g_Config.m_ClDummy].m_Jump) - Send = true; - else if(m_aInputData[g_Config.m_ClDummy].m_Fire != m_aLastData[g_Config.m_ClDummy].m_Fire) - Send = true; - else if(m_aInputData[g_Config.m_ClDummy].m_Hook != m_aLastData[g_Config.m_ClDummy].m_Hook) - Send = true; - else if(m_aInputData[g_Config.m_ClDummy].m_WantedWeapon != m_aLastData[g_Config.m_ClDummy].m_WantedWeapon) - Send = true; - else if(m_aInputData[g_Config.m_ClDummy].m_NextWeapon != m_aLastData[g_Config.m_ClDummy].m_NextWeapon) - Send = true; - else if(m_aInputData[g_Config.m_ClDummy].m_PrevWeapon != m_aLastData[g_Config.m_ClDummy].m_PrevWeapon) - Send = true; - - // send at at least 10hz - if(time_get() > m_LastSendTime + time_freq() / 25) - Send = true; - - if(m_pClient->m_Snap.m_pLocalCharacter && m_pClient->m_Snap.m_pLocalCharacter->m_Weapon == WEAPON_NINJA && (m_aInputData[g_Config.m_ClDummy].m_Direction || m_aInputData[g_Config.m_ClDummy].m_Jump || m_aInputData[g_Config.m_ClDummy].m_Hook)) - Send = true; + Send = Send || m_aInputData[g_Config.m_ClDummy].m_Direction != m_aLastData[g_Config.m_ClDummy].m_Direction; + Send = Send || m_aInputData[g_Config.m_ClDummy].m_Jump != m_aLastData[g_Config.m_ClDummy].m_Jump; + Send = Send || m_aInputData[g_Config.m_ClDummy].m_Fire != m_aLastData[g_Config.m_ClDummy].m_Fire; + Send = Send || m_aInputData[g_Config.m_ClDummy].m_Hook != m_aLastData[g_Config.m_ClDummy].m_Hook; + Send = Send || m_aInputData[g_Config.m_ClDummy].m_WantedWeapon != m_aLastData[g_Config.m_ClDummy].m_WantedWeapon; + Send = Send || m_aInputData[g_Config.m_ClDummy].m_NextWeapon != m_aLastData[g_Config.m_ClDummy].m_NextWeapon; + Send = Send || m_aInputData[g_Config.m_ClDummy].m_PrevWeapon != m_aLastData[g_Config.m_ClDummy].m_PrevWeapon; + Send = Send || time_get() > m_LastSendTime + time_freq() / 25; // send at least 10hz + Send = Send || (m_pClient->m_Snap.m_pLocalCharacter && m_pClient->m_Snap.m_pLocalCharacter->m_Weapon == WEAPON_NINJA && (m_aInputData[g_Config.m_ClDummy].m_Direction || m_aInputData[g_Config.m_ClDummy].m_Jump || m_aInputData[g_Config.m_ClDummy].m_Hook)); } // copy and return size From f94053bd66e4816232fe68442a11eded236051ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20M=C3=BCller?= Date: Sun, 2 Jun 2024 22:08:08 +0200 Subject: [PATCH 5/8] Rename variable `w` to `Weapon` --- src/game/client/components/controls.cpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/game/client/components/controls.cpp b/src/game/client/components/controls.cpp index 54fca856c..0aed43552 100644 --- a/src/game/client/components/controls.cpp +++ b/src/game/client/components/controls.cpp @@ -336,16 +336,16 @@ void CControls::OnRender() m_pClient->m_Snap.m_pLocalCharacter->m_Weapon != WEAPON_HAMMER && m_pClient->m_Snap.m_pLocalCharacter->m_Weapon != WEAPON_NINJA) { - int w; - for(w = WEAPON_LASER; w > WEAPON_GUN; w--) + int Weapon; + for(Weapon = WEAPON_LASER; Weapon > WEAPON_GUN; Weapon--) { - if(w == m_pClient->m_Snap.m_pLocalCharacter->m_Weapon) + if(Weapon == m_pClient->m_Snap.m_pLocalCharacter->m_Weapon) continue; - if(m_aAmmoCount[w] > 0) + if(m_aAmmoCount[Weapon] > 0) break; } - if(w != m_pClient->m_Snap.m_pLocalCharacter->m_Weapon) - m_aInputData[g_Config.m_ClDummy].m_WantedWeapon = w + 1; + if(Weapon != m_pClient->m_Snap.m_pLocalCharacter->m_Weapon) + m_aInputData[g_Config.m_ClDummy].m_WantedWeapon = Weapon + 1; } } From 01be442280953968d6d9866fe289ffe2b5c7f4c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20M=C3=BCller?= Date: Sun, 2 Jun 2024 22:09:46 +0200 Subject: [PATCH 6/8] Rename variable `MDistance` to `MouseDistance` --- src/game/client/components/controls.cpp | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/game/client/components/controls.cpp b/src/game/client/components/controls.cpp index 0aed43552..9dcca38ff 100644 --- a/src/game/client/components/controls.cpp +++ b/src/game/client/components/controls.cpp @@ -414,18 +414,18 @@ void CControls::ClampMousePos() float MinDistance = g_Config.m_ClDyncam ? g_Config.m_ClDyncamMinDistance : g_Config.m_ClMouseMinDistance; float MouseMin = MinDistance; - float MDistance = length(m_aMousePos[g_Config.m_ClDummy]); - if(MDistance < 0.001f) + float MouseDistance = length(m_aMousePos[g_Config.m_ClDummy]); + if(MouseDistance < 0.001f) { m_aMousePos[g_Config.m_ClDummy].x = 0.001f; m_aMousePos[g_Config.m_ClDummy].y = 0; - MDistance = 0.001f; + MouseDistance = 0.001f; } - if(MDistance < MouseMin) - m_aMousePos[g_Config.m_ClDummy] = normalize_pre_length(m_aMousePos[g_Config.m_ClDummy], MDistance) * MouseMin; - MDistance = length(m_aMousePos[g_Config.m_ClDummy]); - if(MDistance > MouseMax) - m_aMousePos[g_Config.m_ClDummy] = normalize_pre_length(m_aMousePos[g_Config.m_ClDummy], MDistance) * MouseMax; + if(MouseDistance < MouseMin) + m_aMousePos[g_Config.m_ClDummy] = normalize_pre_length(m_aMousePos[g_Config.m_ClDummy], MouseDistance) * MouseMin; + MouseDistance = length(m_aMousePos[g_Config.m_ClDummy]); + if(MouseDistance > MouseMax) + m_aMousePos[g_Config.m_ClDummy] = normalize_pre_length(m_aMousePos[g_Config.m_ClDummy], MouseDistance) * MouseMax; } } From 1fcb8a54695d6ea37573ddc2e062f5abcd153d10 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20M=C3=BCller?= Date: Sun, 2 Jun 2024 22:12:21 +0200 Subject: [PATCH 7/8] Add `GetMinMouseDistance` function, remove variable `MinDistance` --- src/game/client/components/controls.cpp | 10 +++++++--- src/game/client/components/controls.h | 1 + 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/game/client/components/controls.cpp b/src/game/client/components/controls.cpp index 9dcca38ff..672105b6c 100644 --- a/src/game/client/components/controls.cpp +++ b/src/game/client/components/controls.cpp @@ -410,9 +410,8 @@ void CControls::ClampMousePos() } else { - float MouseMax = GetMaxMouseDistance(); - float MinDistance = g_Config.m_ClDyncam ? g_Config.m_ClDyncamMinDistance : g_Config.m_ClMouseMinDistance; - float MouseMin = MinDistance; + const float MouseMin = GetMinMouseDistance(); + const float MouseMax = GetMaxMouseDistance(); float MouseDistance = length(m_aMousePos[g_Config.m_ClDummy]); if(MouseDistance < 0.001f) @@ -429,6 +428,11 @@ void CControls::ClampMousePos() } } +float CControls::GetMinMouseDistance() const +{ + return g_Config.m_ClDyncam ? g_Config.m_ClDyncamMinDistance : g_Config.m_ClMouseMinDistance; +} + float CControls::GetMaxMouseDistance() const { float CameraMaxDistance = 200.0f; diff --git a/src/game/client/components/controls.h b/src/game/client/components/controls.h index 0f792df5e..ce6bc664d 100644 --- a/src/game/client/components/controls.h +++ b/src/game/client/components/controls.h @@ -12,6 +12,7 @@ class CControls : public CComponent { + float GetMinMouseDistance() const; float GetMaxMouseDistance() const; public: From 4ec5b04a2eba8da2df0188ecd457029bdb00dc3e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20M=C3=BCller?= Date: Sun, 2 Jun 2024 22:19:06 +0200 Subject: [PATCH 8/8] Use `vec2` instead of two separate variables --- src/game/client/components/controls.cpp | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/src/game/client/components/controls.cpp b/src/game/client/components/controls.cpp index 672105b6c..b143b909f 100644 --- a/src/game/client/components/controls.cpp +++ b/src/game/client/components/controls.cpp @@ -219,13 +219,11 @@ int CControls::SnapInput(int *pData) m_aInputData[g_Config.m_ClDummy].m_TargetX = (int)m_aMousePos[g_Config.m_ClDummy].x; m_aInputData[g_Config.m_ClDummy].m_TargetY = (int)m_aMousePos[g_Config.m_ClDummy].y; - if(g_Config.m_ClSubTickAiming && m_aMousePosOnAction[g_Config.m_ClDummy].x != 0 && m_aMousePosOnAction[g_Config.m_ClDummy].y != 0) + if(g_Config.m_ClSubTickAiming && m_aMousePosOnAction[g_Config.m_ClDummy] != vec2(0.0f, 0.0f)) { m_aInputData[g_Config.m_ClDummy].m_TargetX = (int)m_aMousePosOnAction[g_Config.m_ClDummy].x; m_aInputData[g_Config.m_ClDummy].m_TargetY = (int)m_aMousePosOnAction[g_Config.m_ClDummy].y; - - m_aMousePosOnAction[g_Config.m_ClDummy].x = 0; - m_aMousePosOnAction[g_Config.m_ClDummy].y = 0; + m_aMousePosOnAction[g_Config.m_ClDummy] = vec2(0.0f, 0.0f); } if(!m_aInputData[g_Config.m_ClDummy].m_TargetX && !m_aInputData[g_Config.m_ClDummy].m_TargetY) @@ -365,9 +363,9 @@ bool CControls::OnCursorMove(float x, float y, IInput::ECursorType CursorType) if(CursorType == IInput::CURSOR_JOYSTICK && g_Config.m_InpControllerAbsolute && m_pClient->m_Snap.m_pGameInfoObj && !m_pClient->m_Snap.m_SpecInfo.m_Active) { - float AbsX = 0.0f, AbsY = 0.0f; - if(Input()->GetActiveJoystick()->Absolute(&AbsX, &AbsY)) - m_aMousePos[g_Config.m_ClDummy] = vec2(AbsX, AbsY) * GetMaxMouseDistance(); + vec2 AbsoluteDirection; + if(Input()->GetActiveJoystick()->Absolute(&AbsoluteDirection.x, &AbsoluteDirection.y)) + m_aMousePos[g_Config.m_ClDummy] = AbsoluteDirection * GetMaxMouseDistance(); return true; }