@@ -1846,6 +1846,58 @@ void CWeaponBox::Kill()
1846
1846
UTIL_Remove (this );
1847
1847
}
1848
1848
1849
+ bool CWeaponBox::GiveAmmoToPlayer (CBasePlayer *pPlayer, CBasePlayerWeapon *pWeapon, int iCurrentAmmo, const char *pszAmmo, int iMaxAmmo, CBasePlayerItem **pGivenItem)
1850
+ {
1851
+ if (iCurrentAmmo >= iMaxAmmo)
1852
+ return false ; // can't pickup more, these ammo are full in backpack
1853
+
1854
+ // If already have a weapon in backpack, just refill ammo for it
1855
+ if (iCurrentAmmo > 0 )
1856
+ {
1857
+ int iAmmoIndex = GetAmmoIndex (pszAmmo);
1858
+ if (iAmmoIndex > 0 )
1859
+ {
1860
+ // how many gren ammo can pick up?
1861
+ int iAmmoPickup = min (m_rgAmmo[iAmmoIndex], iMaxAmmo - iCurrentAmmo);
1862
+ if (iAmmoPickup > 0 )
1863
+ {
1864
+ if (!FStringNull (m_rgiszAmmo[iAmmoIndex]) &&
1865
+ pPlayer->GiveAmmo (iAmmoPickup, STRING (m_rgiszAmmo[iAmmoIndex]), iMaxAmmo) != -1 )
1866
+ {
1867
+ m_rgAmmo[iAmmoIndex] -= iAmmoPickup;
1868
+
1869
+ if (m_rgAmmo[iAmmoIndex] < 0 )
1870
+ m_rgAmmo[iAmmoIndex] = 0 ;
1871
+
1872
+ EMIT_SOUND (pPlayer->edict (), CHAN_ITEM, " items/9mmclip1.wav" , VOL_NORM, ATTN_NORM);
1873
+ }
1874
+ }
1875
+
1876
+ // ammo exhausted, remove this weapon
1877
+ if (m_rgAmmo[iAmmoIndex] <= 0 )
1878
+ {
1879
+ pWeapon->Kill ();
1880
+
1881
+ // unlink this weapon from the box
1882
+ return true ;
1883
+ }
1884
+
1885
+ // ammo has not been exhausted yet, keep this weapon in weaponbox
1886
+ return false ;
1887
+ }
1888
+ }
1889
+
1890
+ // If no weapon in backpack, then issue weapon
1891
+ if (pPlayer->AddPlayerItem (pWeapon))
1892
+ {
1893
+ pWeapon->AttachToPlayer (pPlayer);
1894
+ if (pGivenItem) *pGivenItem = pWeapon;
1895
+ }
1896
+
1897
+ // unlink this weapon from the box
1898
+ return true ;
1899
+ }
1900
+
1849
1901
// Try to add my contents to the toucher if the toucher is a player.
1850
1902
void CWeaponBox::Touch (CBaseEntity *pOther)
1851
1903
{
@@ -1991,38 +2043,17 @@ void CWeaponBox::Touch(CBaseEntity *pOther)
1991
2043
int playerGrenades = pPlayer->m_rgAmmo [pGrenade->m_iPrimaryAmmoType ];
1992
2044
1993
2045
#ifdef REGAMEDLL_FIXES
1994
- // sorry for hardcode :(
1995
- const int boxAmmoSlot = 1 ;
2046
+ CBasePlayerItem *pNext = m_rgpPlayerItems[i]->m_pNext ;
1996
2047
1997
- if (playerGrenades < pGrenade->iMaxAmmo1 ())
2048
+ // Pickup grenade item or refill ammo
2049
+ if (GiveAmmoToPlayer (pPlayer, pGrenade,
2050
+ playerGrenades, pGrenade->pszAmmo1 (), pGrenade->iMaxAmmo1 (), &givenItem))
1998
2051
{
1999
- if (m_rgAmmo[boxAmmoSlot] > 1 && playerGrenades > 0 )
2000
- {
2001
- if (!FStringNull (m_rgiszAmmo[boxAmmoSlot])
2002
- && pPlayer->GiveAmmo (1 , STRING (m_rgiszAmmo[boxAmmoSlot]), pGrenade->iMaxAmmo1 ()) != -1 )
2003
- {
2004
- m_rgAmmo[boxAmmoSlot]--;
2005
-
2006
- EMIT_SOUND (pPlayer->edict (), CHAN_ITEM, " items/9mmclip1.wav" , VOL_NORM, ATTN_NORM);
2007
- }
2008
- }
2009
- else
2010
- {
2011
- auto pNext = m_rgpPlayerItems[i]->m_pNext ;
2012
-
2013
- if (pPlayer->AddPlayerItem (pItem))
2014
- {
2015
- pItem->AttachToPlayer (pPlayer);
2016
- givenItem = pItem;
2017
- }
2018
-
2019
- // unlink this weapon from the box
2020
- m_rgpPlayerItems[i] = pItem = pNext;
2021
- continue ;
2022
- }
2052
+ // unlink this weapon from the box
2053
+ m_rgpPlayerItems[i] = pItem = pNext;
2054
+ continue ;
2023
2055
}
2024
2056
#else
2025
-
2026
2057
int maxGrenades = 0 ;
2027
2058
const char *grenadeName = nullptr ;
2028
2059
@@ -2096,13 +2127,18 @@ void CWeaponBox::Touch(CBaseEntity *pOther)
2096
2127
{
2097
2128
if (!FStringNull (m_rgiszAmmo[n]))
2098
2129
{
2099
- // there's some ammo of this type.
2100
- #ifndef REGAMEDLL_ADD
2101
- pPlayer->GiveAmmo (m_rgAmmo[n], (char *)STRING (m_rgiszAmmo[n]), MaxAmmoCarry (m_rgiszAmmo[n]));
2130
+ // there's some ammo of this type
2131
+ if (m_rgAmmo[n] > 0 )
2132
+ {
2133
+ #ifdef REGAMEDLL_ADD
2134
+ int iMaxAmmo = m_rgAmmo[n];
2102
2135
#else
2103
- pPlayer-> GiveAmmo (m_rgAmmo[n], STRING (m_rgiszAmmo[n]), m_rgAmmo [n]);
2136
+ int iMaxAmmo = MaxAmmoCarry (m_rgiszAmmo[n]);
2104
2137
#endif
2105
2138
2139
+ pPlayer->GiveAmmo (m_rgAmmo[n], STRING (m_rgiszAmmo[n]), iMaxAmmo);
2140
+ }
2141
+
2106
2142
// now empty the ammo from the weaponbox since we just gave it to the player
2107
2143
m_rgiszAmmo[n] = iStringNull;
2108
2144
m_rgAmmo[n] = 0 ;
@@ -2242,6 +2278,24 @@ int CWeaponBox::GiveAmmo(int iCount, char *szName, int iMax, int *pIndex)
2242
2278
return i;
2243
2279
}
2244
2280
2281
+ int CWeaponBox::GetAmmoIndex (const char *psz) const
2282
+ {
2283
+ if (!psz)
2284
+ return -1 ;
2285
+
2286
+ int i;
2287
+ for (i = 1 ; i < MAX_AMMO_SLOTS; i++)
2288
+ {
2289
+ if (FStringNull (m_rgiszAmmo[i]))
2290
+ continue ;
2291
+
2292
+ if (!Q_stricmp (STRING (m_rgiszAmmo[i]), psz))
2293
+ return i;
2294
+ }
2295
+
2296
+ return -1 ;
2297
+ }
2298
+
2245
2299
// Is a weapon of this type already packed in this box?
2246
2300
BOOL CWeaponBox::HasWeapon (CBasePlayerItem *pCheckItem)
2247
2301
{
0 commit comments