Skip to content

Commit

Permalink
Don't apply damage to peds without a game entity
Browse files Browse the repository at this point in the history
Also remove the assertion in ApplyPedDamageFromGame
  • Loading branch information
botder committed Sep 6, 2019
1 parent a53e021 commit 632130e
Showing 1 changed file with 10 additions and 21 deletions.
31 changes: 10 additions & 21 deletions Client/mods/deathmatch/logic/CClientGame.cpp
Expand Up @@ -4273,13 +4273,20 @@ bool CClientGame::DamageHandler(CPed* pDamagePed, CEventDamage* pEvent)
CPools* pPools = g_pGame->GetPools();

// Grab the damaged ped
CClientPed* pDamagedPed = NULL;
CClientPed* pDamagedPed = nullptr;

if (pDamagePed)
{
SClientEntity<CPedSA>* pPedClientEntity = pPools->GetPed((DWORD*)pDamagePed->GetInterface());
SClientEntity<CPedSA>* pPedClientEntity = pPools->GetPed(reinterpret_cast<DWORD*>(pDamagePed->GetInterface()));

if (pPedClientEntity)
{
pDamagedPed = reinterpret_cast<CClientPed*>(pPedClientEntity->pClientEntity);
// NOTE(botder): Don't use the damaged ped if the associated game entity doesn't exist to avoid a crash
// in the function ApplyPedDamageFromGame
if (pPedClientEntity->pClientEntity && pPedClientEntity->pClientEntity->GetGameEntity() != nullptr)
{
pDamagedPed = reinterpret_cast<CClientPed*>(pPedClientEntity->pClientEntity);
}
}
}

Expand Down Expand Up @@ -4415,24 +4422,6 @@ bool CClientGame::DamageHandler(CPed* pDamagePed, CEventDamage* pEvent)
bool CClientGame::ApplyPedDamageFromGame(eWeaponType weaponUsed, float fDamage, uchar hitZone, CClientPed* pDamagedPed, CClientEntity* pInflictingEntity,
CEventDamage* pEvent)
{
if (pDamagedPed->GetGamePlayer() == nullptr)
{
// Shouldn't happen, but it does anyway. Log some information about the damaged ped and the ped pool; and then crash optionally
auto entityType = static_cast<int>(pDamagedPed->GetType());
bool isInVehicle = pDamagedPed->IsInVehicle();
bool isDead = pDamagedPed->IsDead();
bool isLocalPlayer = pDamagedPed->IsLocalPlayer();
unsigned long pedCount = g_pGame->GetPools()->GetPedCount();

WriteDebugEvent(
SString("CClientGame::ApplyPedDamageFromGame: GetGamePlayer() == nullptr (type: %d, inVehicle: %d, isDead: %d, isLocalPlayer: %d, pedCount: %lu)",
entityType, isInVehicle, isDead, isLocalPlayer, pedCount));

// Crash on purpose to gather crash information for this offset and not somewhere else
assert(false);
return false;
}

float fPreviousHealth = pDamagedPed->m_fHealth;
float fCurrentHealth = pDamagedPed->GetGamePlayer()->GetHealth();
float fPreviousArmor = pDamagedPed->m_fArmor;
Expand Down

0 comments on commit 632130e

Please sign in to comment.