Skip to content

Commit

Permalink
Low ammo warning
Browse files Browse the repository at this point in the history
  • Loading branch information
sabianroberts committed Nov 17, 2024
1 parent 4884a10 commit 76474a0
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 6 deletions.
1 change: 1 addition & 0 deletions cl_dll/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
64 changes: 63 additions & 1 deletion cl_dll/ammo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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 )
{
Expand Down Expand Up @@ -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.
//
Expand Down Expand Up @@ -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 );

Expand Down Expand Up @@ -986,6 +1046,8 @@ int CHudAmmo::Draw(float flTime)
SPR_DrawAdditive(0, x, y - iOffset, &m_pWeapon->rcAmmo2);
}
}

Warning();
return 1;
}

Expand Down
38 changes: 38 additions & 0 deletions cl_dll/cl_weapons.h
Original file line number Diff line number Diff line change
@@ -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
30 changes: 25 additions & 5 deletions cl_dll/health.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "cl_util.h"
#include "parsemsg.h"
#include <string.h>
#include <cmath>


DECLARE_MESSAGE(m_Health, Health )
Expand Down Expand Up @@ -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)
Expand All @@ -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 );
Expand Down Expand Up @@ -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);
}

Expand Down
1 change: 1 addition & 0 deletions cl_dll/health.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
5 changes: 5 additions & 0 deletions cl_dll/hud.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 76474a0

Please sign in to comment.