From 76474a06bb7212c8c33e55eb1000dc3043cfb4a9 Mon Sep 17 00:00:00 2001 From: Sabian Roberts <31491602+sabianroberts@users.noreply.github.com> Date: Sun, 17 Nov 2024 02:23:23 +0000 Subject: [PATCH] Low ammo warning --- cl_dll/CMakeLists.txt | 1 + cl_dll/ammo.cpp | 64 ++++++++++++++++++++++++++++++++++++++++++- cl_dll/cl_weapons.h | 38 +++++++++++++++++++++++++ cl_dll/health.cpp | 30 ++++++++++++++++---- cl_dll/health.h | 1 + cl_dll/hud.h | 5 ++++ 6 files changed, 133 insertions(+), 6 deletions(-) create mode 100644 cl_dll/cl_weapons.h diff --git a/cl_dll/CMakeLists.txt b/cl_dll/CMakeLists.txt index 28ce0632..af1e972d 100644 --- a/cl_dll/CMakeLists.txt +++ b/cl_dll/CMakeLists.txt @@ -12,6 +12,7 @@ add_sources( cdll_int.cpp cl_dll.h cl_util.h + cl_weapons.h color_tags.cpp color_tags.h com_weapons.cpp diff --git a/cl_dll/ammo.cpp b/cl_dll/ammo.cpp index 451b1f59..dc01d211 100644 --- a/cl_dll/ammo.cpp +++ b/cl_dll/ammo.cpp @@ -28,6 +28,7 @@ #include "ammohistory.h" #include "vgui_TeamFortressViewport.h" +#include "cl_weapons.h" WEAPON *gpActiveSel; // NULL means off, 1 means just the menu bar, otherwise // this points to the active weapon menu item @@ -265,6 +266,8 @@ DECLARE_COMMAND(m_Ammo, PrevWeapon); int CHudAmmo::Init(void) { + m_iCurrentWeapon = -1; + m_iCurrentClipAmmo = -1; gHUD.AddHudElem(this); HOOK_MESSAGE(CurWeapon); @@ -575,6 +578,8 @@ int CHudAmmo::MsgFunc_CurWeapon(const char *pszName, int iSize, void *pbuf ) int iId = READ_CHAR(); int iClip = READ_CHAR(); + m_iCurrentWeapon = iId; + // detect if we're also on target if ( iState > 1 ) { @@ -635,10 +640,51 @@ int CHudAmmo::MsgFunc_CurWeapon(const char *pszName, int iSize, void *pbuf ) m_fFade = 200.0f; //!!! m_iFlags |= HUD_ACTIVE; + m_iCurrentClipAmmo = iClip; return 1; } +void CHudAmmo::Warning() +{ + static bool ammoWarningPlayed = false; + int ammoCount = m_iCurrentClipAmmo; + + if (m_iCurrentWeapon == WEAPON_CROWBAR || m_iCurrentWeapon == WEAPON_PIPEWRENCH || m_iCurrentWeapon == WEAPON_KNIFE || m_iCurrentWeapon == WEAPON_GRAPPLE || m_iCurrentWeapon == WEAPON_RPG || m_iCurrentWeapon == WEAPON_SATCHEL || m_iCurrentWeapon == WEAPON_SHOCKRIFLE || m_iCurrentWeapon == WEAPON_HORNETGUN) + lowAmmoThreshold = -1; + else if (m_iCurrentWeapon == WEAPON_GLOCK) + lowAmmoThreshold = 3; + else if (m_iCurrentWeapon == WEAPON_EAGLE) + lowAmmoThreshold = 2; + else if (m_iCurrentWeapon == WEAPON_PYTHON) + lowAmmoThreshold = 1; + else if (m_iCurrentWeapon == WEAPON_MP5) + lowAmmoThreshold = 10; + else if (m_iCurrentWeapon == WEAPON_SHOTGUN) + lowAmmoThreshold = 2; // give space for a double shot on warning. + else if (m_iCurrentWeapon == WEAPON_CROSSBOW) + lowAmmoThreshold = 1; + else if (m_iCurrentWeapon == WEAPON_GAUSS || m_iCurrentWeapon == WEAPON_EGON) + lowAmmoThreshold = 40; + else if (m_iCurrentWeapon == WEAPON_DISPLACER) + lowAmmoThreshold = 60; + else if (m_iCurrentWeapon == WEAPON_HANDGRENADE || m_iCurrentWeapon == WEAPON_TRIPMINE || m_iCurrentWeapon == WEAPON_SPORELAUNCHER || m_iCurrentWeapon == WEAPON_SNARK || m_iCurrentWeapon == WEAPON_PENGUIN) + lowAmmoThreshold = 1; // most boobie traps. + else if (m_iCurrentWeapon == WEAPON_M249) + lowAmmoThreshold = 15; + else if (m_iCurrentWeapon == WEAPON_SNIPERRIFLE) + lowAmmoThreshold = 1; + + if (ammoCount > lowAmmoThreshold) + ammoWarningPlayed = false; + else if (!ammoWarningPlayed) + { + PlaySound("common/warning.wav", 1.0); + ammoWarningPlayed = true; + } +} + + // // WeaponList -- Tells the hud about a new weapon type. // @@ -893,7 +939,21 @@ int CHudAmmo::Draw(float flTime) if (m_fFade > 0) m_fFade -= (gHUD.m_flTimeDelta * 20); - UnpackRGB(r,g,b, gHUD.m_iDefaultHUDColor); + UnpackRGB(r, g, b, RGB_DEFAULT); + if (m_iCurrentClipAmmo > lowAmmoThreshold) + { + UnpackRGB(r, g, b, gHUD.m_iDefaultHUDColor); + if (Blinking) + Blinking = false; + } + else + { + Blinking = true; + a = (int)(fabs(sin(flTime * 6)) * 256.0); // flash + r = 250; + g = 0; + b = 0; + } ScaleColors(r, g, b, a ); @@ -986,6 +1046,8 @@ int CHudAmmo::Draw(float flTime) SPR_DrawAdditive(0, x, y - iOffset, &m_pWeapon->rcAmmo2); } } + + Warning(); return 1; } diff --git a/cl_dll/cl_weapons.h b/cl_dll/cl_weapons.h new file mode 100644 index 00000000..e00ebf15 --- /dev/null +++ b/cl_dll/cl_weapons.h @@ -0,0 +1,38 @@ +/**** +* +* Copyright © 2021-2024 The Phoenix Project Software. Some Rights Reserved. +* +* AURA +* +* cl_weapons.h - a shortcut to define the IDs of weapons clientside. +* +* +****/ + +#define WEAPON_NONE 0 +#define WEAPON_CROWBAR 1 +#define WEAPON_GLOCK 2 +#define WEAPON_PYTHON 3 +#define WEAPON_MP5 4 +#define WEAPON_CHAINGUN 5 +#define WEAPON_CROSSBOW 6 +#define WEAPON_SHOTGUN 7 +#define WEAPON_RPG 8 +#define WEAPON_GAUSS 9 +#define WEAPON_EGON 10 +#define WEAPON_HORNETGUN 11 +#define WEAPON_HANDGRENADE 12 +#define WEAPON_TRIPMINE 13 +#define WEAPON_SATCHEL 14 +#define WEAPON_SNARK 15 +#define WEAPON_GRAPPLE 16 +#define WEAPON_EAGLE 17 +#define WEAPON_PIPEWRENCH 18 +#define WEAPON_M249 19 +#define WEAPON_DISPLACER 20 +#define WEAPON_SHOCKRIFLE 22 +#define WEAPON_SPORELAUNCHER 23 +#define WEAPON_SNIPERRIFLE 24 +#define WEAPON_KNIFE 25 +#define WEAPON_PENGUIN 26 +#define WEAPON_ONE 27 diff --git a/cl_dll/health.cpp b/cl_dll/health.cpp index 34b7aaef..8c850327 100644 --- a/cl_dll/health.cpp +++ b/cl_dll/health.cpp @@ -26,6 +26,7 @@ #include "cl_util.h" #include "parsemsg.h" #include +#include DECLARE_MESSAGE(m_Health, Health ) @@ -178,6 +179,18 @@ int CHudHealth::Draw(float flTime) if ( !m_hSprite ) m_hSprite = LoadSprite(PAIN_NAME); + + if (m_iHealth > 25) + { + UnpackRGB(r, g, b, gHUD.m_iDefaultHUDColor); + if (Blinking) + Blinking = false; + } + else + { + Blinking = true; + a = (int)(fabs(sin(gHUD.m_flTime * 10)) * 256.0); // flash + } // Has health changed? Flash the health # if (m_fFade) @@ -196,10 +209,6 @@ int CHudHealth::Draw(float flTime) } else a = MIN_ALPHA; - - // If health is getting low, make it bright red - if (m_iHealth <= 15) - a = 255; GetPainColor( r, g, b ); ScaleColors(r, g, b, a ); @@ -227,7 +236,18 @@ int CHudHealth::Draw(float flTime) int iHeight = gHUD.m_iFontHeight; int iWidth = HealthWidth/10; - UnpackRGB(r, g, b, gHUD.m_iDefaultHUDColor); + if (m_iHealth > 25) + { + UnpackRGB(r, g, b, gHUD.m_iDefaultHUDColor); + if (Blinking) + Blinking = false; + } + else + { + Blinking = true; + a = (int)(fabs(sin(gHUD.m_flTime * 10)) * 256.0); // flash + } + FillRGBA(x, y, iWidth, iHeight, r, g, b, a); } diff --git a/cl_dll/health.h b/cl_dll/health.h index 067db4b4..37ec5352 100644 --- a/cl_dll/health.h +++ b/cl_dll/health.h @@ -111,6 +111,7 @@ class CHudHealth: public CHudBase int m_HUD_dmg_bio; int m_HUD_cross; float m_fAttackFront, m_fAttackRear, m_fAttackLeft, m_fAttackRight; + bool Blinking; void GetPainColor( int &r, int &g, int &b ); float m_fFade; diff --git a/cl_dll/hud.h b/cl_dll/hud.h index ad884712..d379346c 100644 --- a/cl_dll/hud.h +++ b/cl_dll/hud.h @@ -121,10 +121,15 @@ struct HUDLIST { class CHudAmmo: public CHudBase { public: + bool Blinking; + int lowAmmoThreshold; // the amount of ammo at which the warning sound will play. + int m_iCurrentWeapon; + int m_iCurrentClipAmmo; int Init( void ); int VidInit( void ); int Draw(float flTime); void Think(void); + void Warning(void); void Reset(void); int DrawWList(float flTime); int MsgFunc_CurWeapon(const char *pszName, int iSize, void *pbuf);