From 6b5ab2287d8e74ae5a52b442a80a91a587ed7832 Mon Sep 17 00:00:00 2001 From: Nabil Benhammou Date: Tue, 16 Jan 2024 17:37:07 +0100 Subject: [PATCH] feat: added toggable weapon holster with command player input --- cl_dll/input.cpp | 11 +++++++++++ common/in_buttons.h | 3 ++- dlls/player.cpp | 1 + dlls/silencedglock.cpp | 10 ++++++++++ dlls/silencedglock.h | 2 +- dlls/weapons.cpp | 1 + dlls/weapons.h | 1 + dlls/weapons_shared.cpp | 22 +++++++++++++++++++--- 8 files changed, 46 insertions(+), 5 deletions(-) diff --git a/cl_dll/input.cpp b/cl_dll/input.cpp index 3c16a84d..668db739 100644 --- a/cl_dll/input.cpp +++ b/cl_dll/input.cpp @@ -108,6 +108,7 @@ kbutton_t in_alt1; kbutton_t in_score; kbutton_t in_break; kbutton_t in_graph; // Display the netgraph +kbutton_t in_holster; typedef struct kblist_s { @@ -476,6 +477,8 @@ void IN_DuckDown() void IN_DuckUp() { KeyUp(&in_duck); } void IN_ReloadDown() { KeyDown(&in_reload); } void IN_ReloadUp() { KeyUp(&in_reload); } +void IN_HolsterUp() { KeyUp(&in_holster); } +void IN_HolsterDown(){ KeyDown(&in_holster); } void IN_Alt1Down() { KeyDown(&in_alt1); } void IN_Alt1Up() { KeyUp(&in_alt1); } void IN_GraphDown() { KeyDown(&in_graph); } @@ -845,6 +848,11 @@ int CL_ButtonBits(bool bResetState) bits |= IN_RELOAD; } + if ((in_holster.state & 3) != 0) + { + bits |= IN_HOLSTER; + } + if ((in_alt1.state & 3) != 0) { bits |= IN_ALT1; @@ -877,6 +885,7 @@ int CL_ButtonBits(bool bResetState) in_reload.state &= ~2; in_alt1.state &= ~2; in_score.state &= ~2; + in_holster.state &= ~2; } return bits; @@ -968,6 +977,8 @@ void InitInput() gEngfuncs.pfnAddCommand("-graph", IN_GraphUp); gEngfuncs.pfnAddCommand("+break", IN_BreakDown); gEngfuncs.pfnAddCommand("-break", IN_BreakUp); + gEngfuncs.pfnAddCommand("+holster", IN_HolsterDown); + gEngfuncs.pfnAddCommand("-holster", IN_HolsterUp); lookstrafe = gEngfuncs.pfnRegisterVariable("lookstrafe", "0", FCVAR_ARCHIVE); lookspring = gEngfuncs.pfnRegisterVariable("lookspring", "0", FCVAR_ARCHIVE); diff --git a/common/in_buttons.h b/common/in_buttons.h index 2d79d234..ed339efd 100644 --- a/common/in_buttons.h +++ b/common/in_buttons.h @@ -30,4 +30,5 @@ #define IN_RUN (1 << 12) #define IN_RELOAD (1 << 13) #define IN_ALT1 (1 << 14) -#define IN_SCORE (1 << 15) // Used by client.dll for when scoreboard is held down +#define IN_HOLSTER (1 << 15) +#define IN_SCORE (1 << 16) // Used by client.dll for when scoreboard is held down diff --git a/dlls/player.cpp b/dlls/player.cpp index 036dc952..4c3a3ae3 100644 --- a/dlls/player.cpp +++ b/dlls/player.cpp @@ -3102,6 +3102,7 @@ void CBasePlayer::SelectItem(const char* pstr) m_pActiveItem->m_ForceSendAnimations = true; m_pActiveItem->Deploy(); m_pActiveItem->m_ForceSendAnimations = false; + m_pActiveItem->GetWeaponPtr()->m_holstered = false; m_pActiveItem->UpdateItemInfo(); } } diff --git a/dlls/silencedglock.cpp b/dlls/silencedglock.cpp index b73ba371..580eec5e 100644 --- a/dlls/silencedglock.cpp +++ b/dlls/silencedglock.cpp @@ -47,6 +47,7 @@ bool CSilencedGlock::GetItemInfo(ItemInfo* p) bool CSilencedGlock::Deploy() { // pev->body = 1; + m_holstered = false; return DefaultDeploy("models/v_silencedglock.mdl", "models/p_silencedglock.mdl", GLOCK_DRAW, "onehanded"); } void CSilencedGlock::SecondaryAttack() @@ -173,3 +174,12 @@ void CSilencedGlock::WeaponIdle() SendWeaponAnim(iAnim); } } + +void CSilencedGlock::Holster() +{ + ALERT(at_console, "SIlenced glock holster func called \n"); + m_holstered = true; + SendWeaponAnim(GLOCK_HOLSTER); +} + + diff --git a/dlls/silencedglock.h b/dlls/silencedglock.h index cfe252ad..0bb6f62e 100644 --- a/dlls/silencedglock.h +++ b/dlls/silencedglock.h @@ -19,6 +19,7 @@ class CSilencedGlock: public CBasePlayerWeapon bool Deploy() override; void Reload() override; void WeaponIdle() override; + void Holster() override; bool UseDecrement() override { @@ -35,5 +36,4 @@ class CSilencedGlock: public CBasePlayerWeapon unsigned short m_usFireGlock1; unsigned short m_usFireGlock2; - }; diff --git a/dlls/weapons.cpp b/dlls/weapons.cpp index 9fd076da..ab536d8d 100644 --- a/dlls/weapons.cpp +++ b/dlls/weapons.cpp @@ -865,6 +865,7 @@ void CBasePlayerWeapon::Holster() m_fInReload = false; // cancel any reload in progress. m_pPlayer->pev->viewmodel = 0; m_pPlayer->pev->weaponmodel = 0; + m_holstered = true; } void CBasePlayerAmmo::Spawn() diff --git a/dlls/weapons.h b/dlls/weapons.h index 6f18c7ff..4955426f 100644 --- a/dlls/weapons.h +++ b/dlls/weapons.h @@ -360,6 +360,7 @@ class CBasePlayerWeapon : public CBasePlayerItem int m_iClientClip; // the last version of m_iClip sent to hud dll int m_iClientWeaponState; // the last version of the weapon state sent to hud dll (is current weapon, is on target) bool m_fInReload; // Are we in the middle of a reload; + bool m_holstered; int m_iDefaultAmmo; // how much ammo you get when you pick up this weapon as placed by a level designer. diff --git a/dlls/weapons_shared.cpp b/dlls/weapons_shared.cpp index 97d7065c..6a173431 100644 --- a/dlls/weapons_shared.cpp +++ b/dlls/weapons_shared.cpp @@ -138,6 +138,23 @@ void CBasePlayerWeapon::ItemPostFrame() m_flLastFireTime = 0.0f; } + if((m_pPlayer->m_afButtonPressed & IN_HOLSTER) != 0 && !m_fInReload) + { + ALERT(at_console, "Holster key pressed\n"); + if(m_holstered) + { + m_holstered = false; + Deploy(); + } + else + Holster(); + return; + } + + if(m_pPlayer->pev->button & (IN_ATTACK2 | IN_ATTACK) && m_holstered) { + return; + } + if ((m_pPlayer->pev->button & IN_ATTACK2) != 0 && CanAttack(m_flNextSecondaryAttack, gpGlobals->time, UseDecrement())) { if (pszAmmo2() && 0 == m_pPlayer->m_rgAmmo[SecondaryAmmoIndex()]) @@ -164,7 +181,7 @@ void CBasePlayerWeapon::ItemPostFrame() // reload when reload is pressed, or if no buttons are down and weapon is empty. Reload(); } - else if ((m_pPlayer->pev->button & (IN_ATTACK | IN_ATTACK2)) == 0) + else if ((m_pPlayer->pev->button & (IN_ATTACK | IN_ATTACK2)) == 0 && !m_holstered) { // no fire buttons down @@ -190,13 +207,12 @@ void CBasePlayerWeapon::ItemPostFrame() return; } } - WeaponIdle(); return; } // catch all - if (ShouldWeaponIdle()) + if (ShouldWeaponIdle() && !m_holstered) { WeaponIdle(); }