User:Cvoxalury/Show ammo pickup icons without having the weapon

From Valve Developer Community
Jump to: navigation, search

The item pickup history relies on the player having the respective weapon in their inventory in order to determine which specific icon to display. As such, if the player hasn't obtained the weapon yet, the ammo pickups for it won't show up on the HUD.

This can be changed by storing a list of icons for each ammo type elsewhere, and picking them from there if the player doesn't have the respective weapon.

Code

Editing history_resource.cpp

In 🖿game\client\history_resource.cpp.cpp, add the following code:

  • At the top, include the ammodef.h header - needed in order to access the ammo names:
#include "ammodef.h"
  • In CHudHistoryResource::Paint(), inside of the switch ( m_PickupHistory[i].type ), add the highlighted code:
	case HISTSLOT_AMMO:
	{
		// Get the weapon we belong to
#ifndef HL2MP
		const FileWeaponInfo_t *pWpnInfo = gWR.GetWeaponFromAmmo( m_PickupHistory[i].iId );
		if (!pWpnInfo)
		{
			itemAmmoIcon = NULL;
			itemIcon = gHUD.GetIcon(GetAmmoDef()->GetAmmoOfIndex(m_PickupHistory[i].iId)->pName);
			iAmount = m_PickupHistory[i].iCount;
		}
		else
		{
			if ( pWpnInfo && ( pWpnInfo->iMaxClip1 >= 0 || pWpnInfo->iMaxClip2 >= 0 ) )
			{
				// The weapon will be the main icon, and the ammo the smaller
				itemIcon = pWpnInfo->iconSmall;
				itemAmmoIcon = gWR.GetAmmoIconFromWeapon( m_PickupHistory[i].iId );
			}
			else
#endif // HL2MP
			{
				itemIcon = gWR.GetAmmoIconFromWeapon( m_PickupHistory[i].iId );
				itemAmmoIcon = NULL;
			}
		}
#ifdef CSTRIKE_DLL
		// show grenades as the weapon icon
		if ( pWpnInfo && pWpnInfo->iFlags & ITEM_FLAG_EXHAUSTIBLE )	
		{
			itemIcon = pWpnInfo->iconActive;
			itemAmmoIcon = NULL;
			bHalfHeight = false;
		}
#endif

		iAmount = m_PickupHistory[i].iCount;
	}
	break;
	case HISTSLOT_AMMODENIED:
	{
		const FileWeaponInfo_t *pWpnInfo = gWR.GetWeaponFromAmmo(m_PickupHistory[i].iId);
		if (!pWpnInfo)
		{
			itemIcon = gHUD.GetIcon(GetAmmoDef()->GetAmmoOfIndex(m_PickupHistory[i].iId)->pName);
		}
		else
		itemIcon = gWR.GetAmmoIconFromWeapon( m_PickupHistory[i].iId );
		iAmount = 0;
		bUseAmmoFullMsg = true;
		// display as red
		clr = gHUD.m_clrCaution;	
		clr[3] = MIN( scale, 255 );
	}
	break;

The gHUD.GetIcon function looks up icons by name from a script file, hud_textures.txt. That's where the new list of icons will be stored.

Editing hud_textures.txt

Edit 🖿<your mod name>\scripts\hud_textures.txt, and create entries for each ammo type.

The name of each entry should be the name of the ammo, and the data block can be copied from the weapon scripts.

For example, for Pistol ammo, look up 🖿scripts\weapon_pistol.txt:

The name of the ammo - "primary_ammo" - is "Pistol"; the graphics (font-based) is in the "ammo" block within TextureData. So, the entry should say,

"Pistol"
{
	"font"		"WeaponIconsSmall"
	"character"	"p"
}

Append this block at the end of the hud_textures.txt file, before the closing bracket. Do so for every other ammo type used in the weapons.

In case of secondary ammo (weapon_smg1's grenades, weapon_ar2's altfires), see "secondary_ammo" for names and "ammo2" for graphics data.

If you don't make an entry for a particular ammo type, the ammo icon for it simply won't be drawn. It is advisable to make a test map with all the ammo pickups and without weapons, and check if the HUD changes work as expected.

Notes

The file 🖿<your mod name>\scripts\mod_textures.txt can be used instead of hud_textures.txt, as gHUD.GetIcon pulls data from both of them.

You can put your ammo entries in there, if you prefer to organize your files this way.