From 5971cf3a659487244d98c67b5f34912976ff19e2 Mon Sep 17 00:00:00 2001 From: Roman Chistokhodov Date: Tue, 2 Jul 2024 02:47:44 +0300 Subject: [PATCH] Redo how the min health threshold is applied (again) --- dlls/cbase.cpp | 26 ++++++++++++++++++++++++-- dlls/cbase.h | 3 +++ dlls/combat.cpp | 3 +-- dlls/func_break.cpp | 4 ++-- dlls/triggers.cpp | 18 ++---------------- 5 files changed, 32 insertions(+), 22 deletions(-) diff --git a/dlls/cbase.cpp b/dlls/cbase.cpp index 7c1266214b..95790d760f 100644 --- a/dlls/cbase.cpp +++ b/dlls/cbase.cpp @@ -574,6 +574,28 @@ int CBaseEntity::TakeHealth(CBaseEntity *pHealer, float flHealth, int bitsDamage // inflict damage on this entity. bitsDamageType indicates type of damage inflicted, ie: DMG_CRUSH +void CBaseEntity::ApplyDamageToHealth(float flDamage) +{ + const float healthBeforeDamage = pev->health; + + // do the damage + pev->health -= flDamage; + + if (m_healthMinThreshold > 0 && pev->health < m_healthMinThreshold) + { + if (IsPlayer()) + { + pev->health = Q_max((int)m_healthMinThreshold, 1); + } + else + { + pev->health = Q_max(m_healthMinThreshold, 1.0f); + pev->health = Q_min(healthBeforeDamage, pev->health); + } + m_healthMinThreshold = 0.0f; + } +} + int CBaseEntity::TakeDamage( entvars_t *pevInflictor, entvars_t *pevAttacker, float flDamage, int bitsDamageType ) { Vector vecTemp; @@ -612,8 +634,8 @@ int CBaseEntity::TakeDamage( entvars_t *pevInflictor, entvars_t *pevAttacker, fl pev->velocity = pev->velocity + vecDir * flForce; } - // do the damage - pev->health -= flDamage; + ApplyDamageToHealth(flDamage); + if( pev->health <= 0 ) { Killed( pevInflictor, pevAttacker, GIB_NORMAL ); diff --git a/dlls/cbase.h b/dlls/cbase.h index 052f890ead..7bf6e67191 100644 --- a/dlls/cbase.h +++ b/dlls/cbase.h @@ -424,6 +424,9 @@ class CBaseEntity virtual void SendMessages(CBaseEntity* pClient) {} virtual bool HandleDoorBlockage(CBaseEntity* pDoor) { return false; } + + void ApplyDamageToHealth(float flDamage); + float m_healthMinThreshold; }; // Ugly technique to override base member functions diff --git a/dlls/combat.cpp b/dlls/combat.cpp index 1fd1d9ce47..c89730211a 100644 --- a/dlls/combat.cpp +++ b/dlls/combat.cpp @@ -1111,8 +1111,7 @@ int CBaseMonster::TakeDamage( entvars_t *pevInflictor, entvars_t *pevAttacker, f AddScoreForDamage(pevAttacker, this, flTake); - // do the damage - pev->health -= flTake; + ApplyDamageToHealth(flTake); // HACKHACK Don't kill monsters in a script. Let them break their scripts first if( m_MonsterState == MONSTERSTATE_SCRIPT ) diff --git a/dlls/func_break.cpp b/dlls/func_break.cpp index 2b91fc3b87..e60e6583d8 100644 --- a/dlls/func_break.cpp +++ b/dlls/func_break.cpp @@ -639,8 +639,8 @@ int CBreakable::TakeDamage( entvars_t *pevInflictor, entvars_t *pevAttacker, flo // this global is still used for glass and other non-monster killables, along with decals. g_vecAttackDir = vecTemp.Normalize(); - // do the damage - pev->health -= flDamage; + ApplyDamageToHealth(flDamage); + if( pev->health <= 0 ) { if (FBitSet(bitsDamageType, DMG_NONLETHAL)) diff --git a/dlls/triggers.cpp b/dlls/triggers.cpp index ba7178a08f..327ea721a8 100644 --- a/dlls/triggers.cpp +++ b/dlls/triggers.cpp @@ -1551,19 +1551,12 @@ void CTriggerHurt::DoDamage(CBaseEntity *pTarget, float fldmg) { int damageType = DamageType(); - const float healthBeforeDmg = pTarget->pev->health; const float minHealthThreshold = pev->dmg_save; - if (minHealthThreshold > 0) { - damageType |= DMG_NONLETHAL; + pTarget->m_healthMinThreshold = minHealthThreshold; } pTarget->TakeDamage( pev, pev, fldmg, damageType ); - - if (minHealthThreshold > 0 && pTarget->pev->health < minHealthThreshold) - { - pTarget->pev->health = Q_min(minHealthThreshold, healthBeforeDmg); - } } } @@ -5390,11 +5383,9 @@ void CTriggerHurtRemote::DoDamage(CBaseEntity* pTarget) float fldmg = pev->dmg; int damageType = DamageType(); - const float healthBeforeDmg = pTarget->pev->health; const float minHealthThreshold = pev->dmg_save; - if (minHealthThreshold > 0) { - damageType |= DMG_NONLETHAL; + pTarget->m_healthMinThreshold = minHealthThreshold; } entvars_t* pevAttacker = pActivator != 0 ? pActivator->pev : pev; @@ -5414,11 +5405,6 @@ void CTriggerHurtRemote::DoDamage(CBaseEntity* pTarget) { pTarget->TakeDamage(pTarget->pev, pevAttacker, fldmg, damageType); } - - if (minHealthThreshold > 0 && pTarget->pev->health < minHealthThreshold) - { - pTarget->pev->health = Q_min(minHealthThreshold, healthBeforeDmg); - } } else {