-
-
Notifications
You must be signed in to change notification settings - Fork 140
Expand file tree
/
Copy pathHooks.ReceiveDamage.cpp
More file actions
504 lines (392 loc) · 16.6 KB
/
Copy pathHooks.ReceiveDamage.cpp
File metadata and controls
504 lines (392 loc) · 16.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
#include "Body.h"
#include <Ext/House/Body.h>
#include <Ext/Side/Body.h>
#include <Ext/TEvent/Body.h>
#include <Ext/WarheadType/Body.h>
#include <Ext/WeaponType/Body.h>
#include <Utilities/AresHelper.h>
namespace ReceiveDamageTemp
{
bool SkipLowDamageCheck = false;
}
// #issue 88 : shield logic
DEFINE_HOOK(0x701900, TechnoClass_ReceiveDamage_Shield, 0x6)
{
GET(TechnoClass*, pThis, ECX);
LEA_STACK(args_ReceiveDamage*, args, 0x4);
const auto pWHExt = WarheadTypeExt::ExtMap.Find(args->WH);
int& damage = *args->Damage;
// AffectsAbove/BelowPercent & AffectsNeutral can ignore IgnoreDefenses like AffectsAllies/Enmies/Owner
// They should be checked here to cover all cases that directly use ReceiveDamage to deal damage
if (!pWHExt->IsHealthInThreshold(pThis) || !pWHExt->IsVeterancyInThreshold(pThis) || (!pWHExt->AffectsNeutral && pThis->Owner->IsNeutral()))
{
damage = 0;
return 0;
}
const auto pExt = TechnoExt::ExtMap.Find(pThis);
const auto pSourceHouse = args->SourceHouse;
const auto pTargetHouse = pThis->Owner;
// Apply warhead effects
if (damage && !pWHExt->ApplyPerTargetEffectsOnDetonate.Get(RulesExt::Global()->ApplyPerTargetEffectsOnDetonate))
pWHExt->DetonateOnOneUnit(args->SourceHouse, pThis, CoordStruct { 0, 0, 0 }, damage, args->Attacker, nullptr, args->DistanceToEpicenter);
// Calculate Damage Multiplier
if (!args->IgnoreDefenses && damage)
{
double multiplier = 1.0;
if (args->Attacker && args->Attacker->Berzerk)
{
if (!pSourceHouse || !pTargetHouse || !pSourceHouse->IsAlliedWith(pTargetHouse))
multiplier = pWHExt->DamageEnemiesMultiplier_Berzerk.Get(RulesExt::Global()->DamageEnemiesMultiplier_Berzerk.Get(RulesExt::Global()->DamageEnemiesMultiplier));
else if (pSourceHouse != pTargetHouse)
multiplier = pWHExt->DamageAlliesMultiplier_Berzerk.Get(RulesExt::Global()->DamageAlliesMultiplier_Berzerk.Get(!pWHExt->AffectsEnemies ? RulesExt::Global()->DamageAlliesMultiplier_NotAffectsEnemies.Get(RulesExt::Global()->DamageAlliesMultiplier) : RulesExt::Global()->DamageAlliesMultiplier));
else
multiplier = pWHExt->DamageOwnerMultiplier_Berzerk.Get(RulesExt::Global()->DamageOwnerMultiplier_Berzerk.Get(!pWHExt->AffectsEnemies ? RulesExt::Global()->DamageOwnerMultiplier_NotAffectsEnemies.Get(RulesExt::Global()->DamageOwnerMultiplier) : RulesExt::Global()->DamageOwnerMultiplier));
}
else
{
if (!pSourceHouse || !pTargetHouse || !pSourceHouse->IsAlliedWith(pTargetHouse))
multiplier = pWHExt->DamageEnemiesMultiplier.Get(RulesExt::Global()->DamageEnemiesMultiplier);
else if (pSourceHouse != pTargetHouse)
multiplier = pWHExt->DamageAlliesMultiplier.Get(!pWHExt->AffectsEnemies ? RulesExt::Global()->DamageAlliesMultiplier_NotAffectsEnemies.Get(RulesExt::Global()->DamageAlliesMultiplier) : RulesExt::Global()->DamageAlliesMultiplier);
else
multiplier = pWHExt->DamageOwnerMultiplier.Get(!pWHExt->AffectsEnemies ? RulesExt::Global()->DamageOwnerMultiplier_NotAffectsEnemies.Get(RulesExt::Global()->DamageOwnerMultiplier) : RulesExt::Global()->DamageOwnerMultiplier);
}
if (pWHExt->DamageSourceHealthMultiplier && args->Attacker)
multiplier += pWHExt->DamageSourceHealthMultiplier * args->Attacker->GetHealthPercentage();
if (pWHExt->DamageTargetHealthMultiplier)
multiplier += pWHExt->DamageTargetHealthMultiplier * pThis->GetHealthPercentage();
if (multiplier != 1.0)
{
const auto sgnDamage = damage > 0 ? 1 : -1;
const auto calculateDamage = static_cast<int>(damage * multiplier);
damage = calculateDamage ? calculateDamage : sgnDamage;
}
}
// Raise Combat Alert
const auto pHouseExt = HouseExt::ExtMap.Find(pTargetHouse);
if (RulesExt::Global()->CombatAlert && damage > 1)
{
auto raiseCombatAlert = [&]()
{
if (!pTargetHouse->IsControlledByCurrentPlayer() || (RulesExt::Global()->CombatAlert_SuppressIfAllyDamage && pTargetHouse->IsAlliedWith(pSourceHouse)))
return;
if (pHouseExt->CombatAlertTimer.HasTimeLeft() || pWHExt->CombatAlert_Suppress.Get(!pWHExt->Malicious || pWHExt->Nonprovocative))
return;
const auto pTypeExt = pExt->TypeExtData;
const auto pType = pTypeExt->OwnerObject();
if (!pTypeExt->CombatAlert.Get(RulesExt::Global()->CombatAlert_Default.Get(!pType->Insignificant && !pType->Spawned)) || !pThis->IsInPlayfield)
return;
const auto pBuilding = abstract_cast<BuildingClass*, true>(pThis);
if (RulesExt::Global()->CombatAlert_IgnoreBuilding && pBuilding && (pTypeExt->CombatAlert_NotBuilding.isset() ? !pTypeExt->CombatAlert_NotBuilding.Get() : !pBuilding->Type->IsVehicle()))
return;
const auto coordInMap = pThis->GetCoords();
if (RulesExt::Global()->CombatAlert_SuppressIfInScreen)
{
const auto pTactical = TacticalClass::Instance;
const auto coordInScreen = pTactical->CoordsToScreen(coordInMap) - pTactical->TacticalPos;
const auto screenArea = DSurface::Composite->GetRect();
if (screenArea.Width >= coordInScreen.X && screenArea.Height >= coordInScreen.Y && coordInScreen.X >= 0 && coordInScreen.Y >= 0) // check if the unit is in screen
return;
}
pHouseExt->CombatAlertTimer.Start(RulesExt::Global()->CombatAlert_Interval);
RadarEventClass::Create(RadarEventType::Combat, CellClass::Coord2Cell(coordInMap));
int index = -1;
if (!RulesExt::Global()->CombatAlert_MakeAVoice) // No one want to play two sound at a time, I guess?
return;
else if (pTypeExt->CombatAlert_UseFeedbackVoice.Get(RulesExt::Global()->CombatAlert_UseFeedbackVoice) && pType->VoiceFeedback.Count > 0) // Use VoiceFeedback first
VocClass::PlayGlobal(pType->VoiceFeedback.GetItem(0), 0x2000, 1.0f);
else if (pTypeExt->CombatAlert_UseAttackVoice.Get(RulesExt::Global()->CombatAlert_UseAttackVoice) && pType->VoiceAttack.Count > 0) // Use VoiceAttack then
VocClass::PlayGlobal(pType->VoiceAttack.GetItem(0), 0x2000, 1.0f);
else if (pTypeExt->CombatAlert_UseEVA.Get(RulesExt::Global()->CombatAlert_UseEVA)) // Use Eva finally
index = pTypeExt->CombatAlert_EVA.Get(VoxClass::FindIndex((const char*)"EVA_UnitsInCombat"));
if (index != -1)
VoxClass::PlayIndex(index);
};
raiseCombatAlert();
}
// Combat Music
if (const auto pTargetSideExt = SideExt::ExtMap.TryFind(SideClass::Array.GetItemOrDefault(pTargetHouse->SideIndex)))
{
if (pTargetSideExt->CombatMusic_UnderAttack && !pTargetHouse->IsAlliedWith(pSourceHouse))
pHouseExt->MusicChange(pTargetSideExt->CombatMusic_Theme, pTargetSideExt->CombatMusic_Duration);
}
if (const auto pSourceSideExt = SideExt::ExtMap.TryFind(SideClass::Array.GetItemOrDefault(pSourceHouse->SideIndex)))
{
if (!pSourceSideExt->CombatMusic_UnderAttack && !pSourceHouse->IsAlliedWith(pTargetHouse) && !pTargetHouse->IsNeutral())
HouseExt::ExtMap.Find(pSourceHouse)->MusicChange(pSourceSideExt->CombatMusic_Theme, pSourceSideExt->CombatMusic_Duration);
}
// Shield Receive Damage
if (!args->IgnoreDefenses)
{
int nDamageLeft = damage;
if (const auto pShieldData = pExt->Shield.get())
{
if (pShieldData->IsActive())
{
nDamageLeft = pShieldData->ReceiveDamage(args);
if (nDamageLeft >= 0)
{
damage = nDamageLeft;
if (const auto pTag = pThis->AttachedTag)
pTag->RaiseEvent((TriggerEvent)PhobosTriggerEvent::ShieldBroken, pThis, CellStruct::Empty);
}
if (nDamageLeft == 0)
ReceiveDamageTemp::SkipLowDamageCheck = true;
}
else if (!pShieldData->IsAvailable() || pShieldData->GetHP() <= 0)
{
pShieldData->SetRespawnRestartInCombat();
}
}
if ((!pWHExt->CanKill || pExt->AE.Unkillable)
&& pThis->Health > 0 && nDamageLeft != 0
&& pWHExt->CanTargetHouse(pSourceHouse, pThis)
&& MapClass::GetTotalDamage(nDamageLeft, args->WH, pThis->GetTechnoType()->Armor, args->DistanceToEpicenter) >= pThis->Health)
{
// Update remaining damage and check if the target will die and should be avoided
damage = 0;
pThis->Health = 1;
pThis->EstimatedHealth = 1;
ReceiveDamageTemp::SkipLowDamageCheck = true;
}
}
return 0;
}
DEFINE_HOOK(0x7019D8, TechnoClass_ReceiveDamage_SkipLowDamageCheck, 0x5)
{
if (ReceiveDamageTemp::SkipLowDamageCheck)
{
ReceiveDamageTemp::SkipLowDamageCheck = false;
}
else
{
// Restore overridden instructions
GET(int*, nDamage, EBX);
if (*nDamage < 1)
*nDamage = 1;
}
return 0x7019E3;
}
DEFINE_HOOK(0x702819, TechnoClass_ReceiveDamage_Decloak, 0xA)
{
GET(TechnoClass* const, pThis, ESI);
GET_STACK(WarheadTypeClass*, pWarhead, STACK_OFFSET(0xC4, 0xC));
if (auto const pExt = WarheadTypeExt::ExtMap.TryFind(pWarhead))
{
if (pExt->DecloakDamagedTargets)
pThis->Uncloak(false);
}
return 0x702823;
}
DEFINE_HOOK(0x701DFF, TechnoClass_ReceiveDamage_FlyingStrings, 0x7)
{
if (!Phobos::DisplayDamageNumbers)
return 0;
GET(TechnoClass* const, pThis, ESI);
GET(int* const, pDamage, EBX);
if (*pDamage)
GeneralUtils::DisplayDamageNumberString(*pDamage, DamageDisplayType::Regular, pThis->GetRenderCoords(), TechnoExt::ExtMap.Find(pThis)->DamageNumberOffset);
return 0;
}
DEFINE_HOOK(0x702603, TechnoClass_ReceiveDamage_Explodes, 0x6)
{
enum { SkipExploding = 0x702672, SkipKillingPassengers = 0x702669 };
GET(TechnoClass*, pThis, ESI);
const auto pTypeExt = TechnoExt::ExtMap.Find(pThis)->TypeExtData;
if (pThis->WhatAmI() == AbstractType::Building)
{
if (!pTypeExt->Explodes_DuringBuildup && (pThis->CurrentMission == Mission::Construction || pThis->CurrentMission == Mission::Selling))
return SkipExploding;
}
if (!pTypeExt->Explodes_KillPassengers)
return SkipKillingPassengers;
return 0;
}
DEFINE_HOOK(0x702672, TechnoClass_ReceiveDamage_RevengeWeapon, 0x5)
{
GET(TechnoClass*, pThis, ESI);
GET_STACK(TechnoClass*, pSource, STACK_OFFSET(0xC4, 0x10));
GET_STACK(WarheadTypeClass*, pWarhead, STACK_OFFSET(0xC4, 0xC));
TechnoExt::ApplyKillWeapon(pThis, pSource, pWarhead);
if (pSource)
TechnoExt::ApplyRevengeWeapon(pThis, pSource, pWarhead);
return 0;
}
DEFINE_HOOK(0x518434, InfantryClass_ReceiveDamage_SkipDeathAnim, 0x7)
{
enum { SkipDeathAnim = 0x5185F1 };
GET(InfantryClass*, pThis, ESI);
return pThis->Transporter ? SkipDeathAnim : 0;
}
// Issue #237 NotHuman additional animations support
// Author: Otamaa
DEFINE_HOOK(0x518505, InfantryClass_ReceiveDamage_NotHuman, 0x4)
{
enum { SkipPlayAnim = 0x518515, SkipGameCode = 0x5185F1 };
GET(InfantryClass* const, pThis, ESI);
if (pThis->SequenceAnim == Sequence::Paradrop && pThis->IsFallingDown)
{
GameCreate<AnimClass>(RulesClass::Instance->InfantryExplode, pThis->Location);
return SkipGameCode;
}
REF_STACK(args_ReceiveDamage const, receiveDamageArgs, STACK_OFFSET(0xD0, 0x4));
// Die1-Die5 sequences are offset by 10
constexpr auto Die = [](int x) { return x + 10; };
int resultSequence = Die(1);
auto const pTypeExt = TechnoExt::ExtMap.Find(pThis)->TypeExtData;
if (pTypeExt->NotHuman_RandomDeathSequence.Get())
resultSequence = ScenarioClass::Instance->Random.RandomRanged(Die(1), Die(5));
if (receiveDamageArgs.WH)
{
auto const pWarheadExt = WarheadTypeExt::ExtMap.Find(receiveDamageArgs.WH);
const int whSequence = pWarheadExt->NotHuman_DeathSequence.Get();
if (whSequence > 0)
resultSequence = Math::min(Die(whSequence), Die(5));
}
R->ECX(pThis);
pThis->PlayAnim(static_cast<Sequence>(resultSequence), true);
return SkipPlayAnim;
}
DEFINE_HOOK(0x702050, TechnoClass_ReceiveDamage_AttachEffectExpireWeapon, 0x6)
{
GET(TechnoClass*, pThis, ESI);
auto const pExt = TechnoExt::ExtMap.Find(pThis);
std::set<AttachEffectTypeClass*> cumulativeTypes;
std::vector<std::pair<WeaponTypeClass*, TechnoClass*>> expireWeapons;
for (auto const& attachEffect : pExt->AttachedEffects)
{
auto const pType = attachEffect->GetType();
if (pType->ExpireWeapon && (pType->ExpireWeapon_TriggerOn & ExpireWeaponCondition::Death) != ExpireWeaponCondition::None)
{
if (!pType->Cumulative || !pType->ExpireWeapon_CumulativeOnlyOnce || !cumulativeTypes.contains(pType))
{
if (pType->Cumulative && pType->ExpireWeapon_CumulativeOnlyOnce)
cumulativeTypes.insert(pType);
if (pType->ExpireWeapon_UseInvokerAsOwner)
{
if (auto const pInvoker = attachEffect->GetInvoker())
expireWeapons.push_back(std::make_pair(pType->ExpireWeapon, pInvoker));
}
else
{
expireWeapons.push_back(std::make_pair(pType->ExpireWeapon, pThis));
}
}
}
}
auto const coords = pThis->GetCoords();
for (auto const& pair : expireWeapons)
{
auto const pInvoker = pair.second;
WeaponTypeExt::DetonateAt(pair.first, coords, pInvoker, pInvoker->Owner, pThis);
}
return 0;
}
DEFINE_HOOK(0x701E18, TechnoClass_ReceiveDamage_ReflectDamage, 0x7)
{
GET(TechnoClass*, pThis, ESI);
GET(const int*, pDamage, EBX);
GET_STACK(TechnoClass*, pSource, STACK_OFFSET(0xC4, 0x10));
GET_STACK(HouseClass*, pSourceHouse, STACK_OFFSET(0xC4, 0x1C));
GET_STACK(WarheadTypeClass*, pWarhead, STACK_OFFSET(0xC4, 0xC));
if (*pDamage <= 0)
return 0;
auto const pWHExt = WarheadTypeExt::ExtMap.Find(pWarhead);
if (pWHExt->Reflected)
return 0;
auto const pExt = TechnoExt::ExtMap.Find(pThis);
auto& random = ScenarioClass::Instance->Random;
const auto& suppressType = pWHExt->SuppressReflectDamage_Types;
const auto& suppressGroup = pWHExt->SuppressReflectDamage_Groups;
const bool suppress = pWHExt->SuppressReflectDamage;
const bool suppressByType = suppressType.size() > 0;
const bool suppressByGroup = suppressGroup.size() > 0;
if (pExt->AE.ReflectDamage && *pDamage > 0 && (!suppress || suppressByType || suppressByGroup))
{
for (auto const& attachEffect : pExt->AttachedEffects)
{
if (!attachEffect->IsActive())
continue;
auto const pType = attachEffect->GetType();
if (!pType->ReflectDamage)
continue;
if (pType->ReflectDamage_Chance < random.RandomDouble())
continue;
if (suppress)
{
if (suppressByType && suppressType.Contains(pType))
continue;
if (suppressByGroup && pType->HasGroups(suppressGroup, false))
continue;
}
auto const pWH = pType->ReflectDamage_Warhead.Get(RulesClass::Instance->C4Warhead);
int damage = pType->ReflectDamage_Override.Get(static_cast<int>(*pDamage * pType->ReflectDamage_Multiplier));
if (pType->ReflectDamage_UseInvokerAsOwner)
{
auto const pInvoker = attachEffect->GetInvoker();
if (pInvoker && EnumFunctions::CanTargetHouse(pType->ReflectDamage_AffectsHouse, pInvoker->Owner, pSourceHouse))
{
auto const pWHExtRef = WarheadTypeExt::ExtMap.Find(pWH);
pWHExtRef->Reflected = true;
if (pType->ReflectDamage_Warhead_Detonate)
WarheadTypeExt::DetonateAt(pWH, pSource, pInvoker, damage, pInvoker->Owner);
else
pSource->ReceiveDamage(&damage, 0, pWH, pInvoker, false, false, pInvoker->Owner);
pWHExtRef->Reflected = false;
}
}
else if (EnumFunctions::CanTargetHouse(pType->ReflectDamage_AffectsHouse, pThis->Owner, pSourceHouse))
{
auto const pWHExtRef = WarheadTypeExt::ExtMap.Find(pWH);
pWHExtRef->Reflected = true;
if (pType->ReflectDamage_Warhead_Detonate)
WarheadTypeExt::DetonateAt(pWH, pSource, pThis, damage, pThis->Owner);
else
pSource->ReceiveDamage(&damage, 0, pWH, pThis, false, false, pThis->Owner);
pWHExtRef->Reflected = false;
}
}
}
return 0;
}
DEFINE_HOOK(0x5F5480, ObjectClass_ReceiveDamage_FlashDuration, 0x6)
{
enum { SkipGameCode = 0x5F545C };
GET(ObjectClass*, pThis, ESI);
REF_STACK(args_ReceiveDamage const, receiveDamageArgs, STACK_OFFSET(0x24, 0x4));
int nFlashDuration = 7;
if (auto const pWH = receiveDamageArgs.WH)
nFlashDuration = WarheadTypeExt::ExtMap.Find(pWH)->Flash_Duration.Get(nFlashDuration);
if (nFlashDuration > 0)
pThis->Flash(nFlashDuration);
return SkipGameCode;
}
// Remove the YR Psychedelic ally check before our hook incase Phobos is ran without Ares.
DEFINE_JUMP(LJMP, 0x701CE5, 0x701D0B);
// Check houses for Psychedelic at the address Ares returns to after immunity checks.
DEFINE_HOOK(0x701D2E, TechnoClass_ReceiveDamage_AllowBerzerkOnAllies, 0x6)
{
enum { DisallowBerzerk = 0x701D3E };
GET(TechnoClass*, pThis, ESI);
REF_STACK(args_ReceiveDamage const, receiveDamageArgs, STACK_OFFSET(0xC4, 0x4));
if (!RulesExt::Global()->AllowBerzerkOnAllies && pThis->Owner->IsAlliedWith(receiveDamageArgs.SourceHouse))
return DisallowBerzerk;
return 0;
}
DEFINE_HOOK(0x702823, TechnoClass_ReceiveDamage_SkipDamagedParticle, 0x7)
{
enum { SkipParticle = 0x702A25, RemoveParticle = 0x70283C, SpawnParticle = 0x702857 };
GET(TechnoClass*, pThis, ESI);
if (pThis->Transporter)
return SkipParticle;
return pThis->GetHealthPercentage() <= RulesClass::Instance->ConditionYellow ? SpawnParticle : RemoveParticle;
}
DEFINE_HOOK(0x737E6E, UnitClass_ReceiveDamage_SkipExplode, 0xA)
{
enum { ContinueCheck = 0x737E78, SkipExplode = 0x737F74 };
GET(UnitClass*, pThis, ESI);
if (pThis->Transporter)
return SkipExplode;
R->EAX(pThis->GetHeight());
return ContinueCheck;
}