Skip to content

Commit f00c9a1

Browse files
committed
Optimize TechnoClass_ReceiveDamage and fix incorrect logic
- Fix the issue that when the damage is reduced to non lethal, `CanKill=no` will directly set the health to 1.
1 parent 03e42a1 commit f00c9a1

File tree

1 file changed

+19
-31
lines changed

1 file changed

+19
-31
lines changed

src/Ext/Techno/Hooks.ReceiveDamage.cpp

Lines changed: 19 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,10 @@ DEFINE_HOOK(0x701900, TechnoClass_ReceiveDamage_Shield, 0x6)
2121

2222
const auto pRules = RulesExt::Global();
2323
const auto pExt = TechnoExt::ExtMap.Find(pThis);
24-
const auto pTypeExt = pExt->TypeExtData;
25-
const auto pType = pTypeExt->OwnerObject();
2624
const auto pWHExt = WarheadTypeExt::ExtMap.Find(args->WH);
2725

2826
const auto pSourceHouse = args->SourceHouse;
2927
const auto pTargetHouse = pThis->Owner;
30-
const bool unkillable = !pWHExt->CanKill || pExt->AE.Unkillable;
31-
int nDamageLeft = *args->Damage;
3228

3329
// Calculate Damage Multiplier
3430
if (!args->IgnoreDefenses && *args->Damage)
@@ -62,7 +58,11 @@ DEFINE_HOOK(0x701900, TechnoClass_ReceiveDamage_Shield, 0x6)
6258

6359
if (pHouseExt->CombatAlertTimer.HasTimeLeft() || pWHExt->CombatAlert_Suppress.Get(!pWHExt->Malicious || pWHExt->Nonprovocative))
6460
return;
65-
else if (!pTypeExt->CombatAlert.Get(pRules->CombatAlert_Default.Get(!pType->Insignificant && !pType->Spawned)) || !pThis->IsInPlayfield)
61+
62+
const auto pTypeExt = pExt->TypeExtData;
63+
const auto pType = pTypeExt->OwnerObject();
64+
65+
if (!pTypeExt->CombatAlert.Get(pRules->CombatAlert_Default.Get(!pType->Insignificant && !pType->Spawned)) || !pThis->IsInPlayfield)
6666
return;
6767

6868
const auto pBuilding = abstract_cast<BuildingClass*>(pThis);
@@ -104,42 +104,30 @@ DEFINE_HOOK(0x701900, TechnoClass_ReceiveDamage_Shield, 0x6)
104104
// Shield Receive Damage
105105
if (!args->IgnoreDefenses)
106106
{
107+
int nDamageLeft = *args->Damage;
108+
107109
if (const auto pShieldData = pExt->Shield.get())
108110
{
109-
if (!pShieldData->IsActive())
111+
if (pShieldData->IsActive())
110112
{
111-
int nDamageTotal = MapClass::GetTotalDamage(nDamageLeft, args->WH, pThis->GetTechnoType()->Armor, 0);
113+
nDamageLeft = pShieldData->ReceiveDamage(args);
112114

113-
// Check if the warhead can not kill targets
114-
if (pThis->Health > 0 && unkillable && nDamageTotal >= pThis->Health)
115+
if (nDamageLeft >= 0)
115116
{
116-
*args->Damage = 0;
117-
pThis->Health = 1;
118-
pThis->EstimatedHealth = 1;
119-
ReceiveDamageTemp::SkipLowDamageCheck = true;
120-
}
121-
122-
return 0;
123-
}
117+
*args->Damage = nDamageLeft;
124118

125-
nDamageLeft = pShieldData->ReceiveDamage(args);
126-
127-
if (nDamageLeft >= 0)
128-
{
129-
*args->Damage = nDamageLeft;
119+
if (auto pTag = pThis->AttachedTag)
120+
pTag->RaiseEvent((TriggerEvent)PhobosTriggerEvent::ShieldBroken, pThis, CellStruct::Empty);
121+
}
130122

131-
if (auto pTag = pThis->AttachedTag)
132-
pTag->RaiseEvent((TriggerEvent)PhobosTriggerEvent::ShieldBroken, pThis, CellStruct::Empty);
123+
if (nDamageLeft == 0)
124+
ReceiveDamageTemp::SkipLowDamageCheck = true;
133125
}
134-
135-
if (nDamageLeft == 0)
136-
ReceiveDamageTemp::SkipLowDamageCheck = true;
137126
}
138127

139-
// Update remaining damage and check if the target will die and should be avoided
140-
int nDamageTotal = MapClass::GetTotalDamage(nDamageLeft, args->WH, pThis->GetTechnoType()->Armor, 0);
141-
142-
if (pThis->Health > 0 && unkillable && nDamageTotal >= pThis->Health)
128+
if (pThis->Health > 0 && (!pWHExt->CanKill || pExt->AE.Unkillable) // Check if the warhead can not kill targets
129+
// Update remaining damage and check if the target will die and should be avoided
130+
&& MapClass::GetTotalDamage(nDamageLeft, args->WH, pThis->GetTechnoType()->Armor, 0) >= pThis->Health)
143131
{
144132
*args->Damage = 0;
145133
pThis->Health = 1;

0 commit comments

Comments
 (0)