Skip to content

Commit 4d90a5f

Browse files
committed
CWeaponBox::Touch: Fix grenade pickup (Resolves #923, Closes #931)
1 parent f4c4e89 commit 4d90a5f

File tree

2 files changed

+90
-33
lines changed

2 files changed

+90
-33
lines changed

regamedll/dlls/weapons.cpp

+86-32
Original file line numberDiff line numberDiff line change
@@ -1846,6 +1846,58 @@ void CWeaponBox::Kill()
18461846
UTIL_Remove(this);
18471847
}
18481848

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+
18491901
// Try to add my contents to the toucher if the toucher is a player.
18501902
void CWeaponBox::Touch(CBaseEntity *pOther)
18511903
{
@@ -1991,38 +2043,17 @@ void CWeaponBox::Touch(CBaseEntity *pOther)
19912043
int playerGrenades = pPlayer->m_rgAmmo[pGrenade->m_iPrimaryAmmoType];
19922044

19932045
#ifdef REGAMEDLL_FIXES
1994-
// sorry for hardcode :(
1995-
const int boxAmmoSlot = 1;
2046+
CBasePlayerItem *pNext = m_rgpPlayerItems[i]->m_pNext;
19962047

1997-
if (playerGrenades < pGrenade->iMaxAmmo1())
2048+
// Pickup grenade item or refill ammo
2049+
if (GiveAmmoToPlayer(pPlayer, pGrenade,
2050+
playerGrenades, pGrenade->pszAmmo1(), pGrenade->iMaxAmmo1(), &givenItem))
19982051
{
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;
20232055
}
20242056
#else
2025-
20262057
int maxGrenades = 0;
20272058
const char *grenadeName = nullptr;
20282059

@@ -2096,13 +2127,18 @@ void CWeaponBox::Touch(CBaseEntity *pOther)
20962127
{
20972128
if (!FStringNull(m_rgiszAmmo[n]))
20982129
{
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];
21022135
#else
2103-
pPlayer->GiveAmmo(m_rgAmmo[n], STRING(m_rgiszAmmo[n]), m_rgAmmo[n]);
2136+
int iMaxAmmo = MaxAmmoCarry(m_rgiszAmmo[n]);
21042137
#endif
21052138

2139+
pPlayer->GiveAmmo(m_rgAmmo[n], STRING(m_rgiszAmmo[n]), iMaxAmmo);
2140+
}
2141+
21062142
// now empty the ammo from the weaponbox since we just gave it to the player
21072143
m_rgiszAmmo[n] = iStringNull;
21082144
m_rgAmmo[n] = 0;
@@ -2242,6 +2278,24 @@ int CWeaponBox::GiveAmmo(int iCount, char *szName, int iMax, int *pIndex)
22422278
return i;
22432279
}
22442280

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+
22452299
// Is a weapon of this type already packed in this box?
22462300
BOOL CWeaponBox::HasWeapon(CBasePlayerItem *pCheckItem)
22472301
{

regamedll/dlls/weapons.h

+4-1
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,9 @@ class CWeaponBox: public CBaseEntity
477477
public:
478478
BOOL IsEmpty();
479479
int GiveAmmo(int iCount, char *szName, int iMax, int *pIndex = nullptr);
480+
int GetAmmoIndex(const char *psz) const;
481+
bool GiveAmmoToPlayer(CBasePlayer *pPlayer, CBasePlayerWeapon *pWeapon,
482+
int iCurrentAmmo, const char *pszAmmo, int iMaxAmmo, CBasePlayerItem **pGivenItem = NULL);
480483

481484
void EXPORT Kill();
482485
void EXPORT BombThink();
@@ -1269,7 +1272,7 @@ inline float CKnife::KnifeSwingDamage(bool fast) const { return fast ? m_flSwing
12691272
inline float CKnife::KnifeStabDistance() const { return m_flStabDistance; }
12701273
inline float CKnife::KnifeSwingDistance() const { return m_flSwingDistance; }
12711274
inline float CKnife::KnifeBackStabMultiplier() const { return m_flBackStabMultiplier; }
1272-
#else
1275+
#else
12731276
inline float CKnife::KnifeStabDamage() const { return KNIFE_STAB_DAMAGE; }
12741277
inline float CKnife::KnifeSwingDamage(bool fast) const { return fast ? KNIFE_SWING_DAMAGE_FAST : KNIFE_SWING_DAMAGE; }
12751278
inline float CKnife::KnifeStabDistance() const { return KNIFE_STAB_DISTANCE; }

0 commit comments

Comments
 (0)