Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WeaponBox: Fix hardcoded maxammo #446

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions regamedll/dlls/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1288,7 +1288,13 @@ void PackPlayerItem(CBasePlayer *pPlayer, CBasePlayerItem *pItem, bool packAmmo)
// pack the ammo
if (packAmmo)
{

#ifdef REGAMEDLL_ADD
pWeaponBox->PackAmmoEx(MAKE_STRING(pItem->pszAmmo1()), pPlayer->m_rgAmmo[pItem->PrimaryAmmoIndex()], pItem->iMaxAmmo1());
#else
pWeaponBox->PackAmmo(MAKE_STRING(pItem->pszAmmo1()), pPlayer->m_rgAmmo[pItem->PrimaryAmmoIndex()]);
#endif

}

pWeaponBox->SetModel(modelName);
Expand Down Expand Up @@ -1344,7 +1350,12 @@ void PackPlayerNade(CBasePlayer *pPlayer, CBasePlayerItem *pItem, bool packAmmo)
// pack the ammo
if (packAmmo)
{

#ifdef REGAMEDLL_ADD
pWeaponBox->PackAmmoEx(MAKE_STRING(pItem->pszAmmo1()), pPlayer->m_rgAmmo[pItem->PrimaryAmmoIndex()], pItem->iMaxAmmo1());
#else
pWeaponBox->PackAmmo(MAKE_STRING(pItem->pszAmmo1()), pPlayer->m_rgAmmo[pItem->PrimaryAmmoIndex()]);
#endif
}

pWeaponBox->SetModel(modelName);
Expand Down Expand Up @@ -7789,12 +7800,19 @@ CBaseEntity *EXT_FUNC CBasePlayer::__API_HOOK(DropPlayerItem)(const char *pszIte
int iAmmoIndex = GetAmmoIndex(pWeapon->pszAmmo1());
if (iAmmoIndex != -1)
{

#ifdef REGAMEDLL_ADD
pWeaponBox->PackAmmoEx(MAKE_STRING(pWeapon->pszAmmo1()), m_rgAmmo[iAmmoIndex], pWeapon->iMaxAmmo1());
#else // REGAMEDLL_ADD

#ifdef REGAMEDLL_FIXES
// why not pack the ammo more than one?
pWeaponBox->PackAmmo(MAKE_STRING(pWeapon->pszAmmo1()), m_rgAmmo[iAmmoIndex]);
#else
#else // REGAMEDLL_FIXES
pWeaponBox->PackAmmo(MAKE_STRING(pWeapon->pszAmmo1()), m_rgAmmo[iAmmoIndex] > 0);
#endif
#endif // REGAMEDLL_FIXES

#endif // REGAMEDLL_ADD
m_rgAmmo[iAmmoIndex] = 0;
}
}
Expand Down
54 changes: 53 additions & 1 deletion regamedll/dlls/weapons.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1931,8 +1931,11 @@ void CWeaponBox::Touch(CBaseEntity *pOther)
if (!FStringNull(m_rgiszAmmo[n]))
{
// there's some ammo of this type.
#ifdef REGAMEDLL_ADD
pPlayer->GiveAmmo(m_rgAmmo[n], STRING(m_rgiszAmmo[n]), m_rgAmmo[n]);
#else
pPlayer->GiveAmmo(m_rgAmmo[n], (char *)STRING(m_rgiszAmmo[n]), MaxAmmoCarry(m_rgiszAmmo[n]));

#endif
// now empty the ammo from the weaponbox since we just gave it to the player
m_rgiszAmmo[n] = iStringNull;
m_rgAmmo[n] = 0;
Expand Down Expand Up @@ -2005,6 +2008,55 @@ BOOL CWeaponBox::PackWeapon(CBasePlayerItem *pWeapon)
return TRUE;
}

#ifdef REGAMEDLL_ADD
int CWeaponBox::PackAmmoEx(string_t iszName, int iCount, int iMaxCarry)
{
if (!iszName)
{
// ALERT(at_console, "[%s] NULL string!\n", __FUNCTION__);
return -1;
}

// Duplicate -- int CBasePlayer::GetAmmoIndex
auto GetAmmoIndex = [&](const char *pszAmmoName)->int
{
for (auto& ammo : ammoIndex)
{
if (!Q_stricmp(ammo.name, pszAmmoName)) {
return ammo.type;
}
}

return -1;
};

int iAmmoIndex = GetAmmoIndex(iszName);

if (iAmmoIndex < 0 || iAmmoIndex >= MAX_AMMO_SLOTS)
{
// ALERT(at_console, "[%s] out of named ammo slots!\n", __FUNCTION__);
return -1;
}

if (iMaxCarry == -1)
iMaxCarry = MaxAmmoCarry(iszName);

int iAdd = Q_min(iCount, iMaxCarry - m_rgAmmo[iAmmoIndex]);

if (iAdd < 1)
return iAmmoIndex;

if (FStringNull(m_rgiszAmmo[iAmmoIndex]))
{
m_rgiszAmmo[iAmmoIndex] = iszName;
}

m_rgAmmo[iAmmoIndex] += iAdd;

return iAmmoIndex;
}
#endif

BOOL CWeaponBox::PackAmmo(string_t iszName, int iCount)
{
if (!iszName)
Expand Down
4 changes: 4 additions & 0 deletions regamedll/dlls/weapons.h
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,10 @@ class CWeaponBox: public CBaseEntity
BOOL PackWeapon(CBasePlayerItem *pWeapon);
BOOL PackAmmo(string_t iszName, int iCount);

#ifdef REGAMEDLL_ADD
int PackAmmoEx(string_t iszName, int iCount, int iMaxCarry = -1);
#endif

#ifdef REGAMEDLL_API
void SetModel_OrigFunc(const char *pszModelName);
#endif
Expand Down