Skip to content

Commit 7934631

Browse files
committed
Fix f4439a1 optimization's typo in logical judgment
1 parent f4439a1 commit 7934631

File tree

2 files changed

+78
-49
lines changed

2 files changed

+78
-49
lines changed

src/Ext/Techno/Hooks.Firing.cpp

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,12 @@ DEFINE_HOOK(0x6F33CD, TechnoClass_WhatWeaponShouldIUse_ForceFire, 0x6)
5151
auto const pWeaponSecondary = pThis->GetWeapon(1)->WeaponType;
5252
auto const pPrimaryExt = WeaponTypeExt::ExtMap.Find(pWeaponPrimary);
5353

54-
if (pWeaponSecondary && !pPrimaryExt->SkipWeaponPicking && (!EnumFunctions::IsCellEligible(pCell, pPrimaryExt->CanTarget, true, true)
55-
|| !pPrimaryExt->IsHealthRatioEligible(pThis) || (pPrimaryExt->AttachEffect_CheckOnFirer && !pPrimaryExt->HasRequiredAttachedEffects(pThis, pThis))))
54+
if (pWeaponSecondary
55+
&& !pPrimaryExt->SkipWeaponPicking
56+
&& (!EnumFunctions::IsCellEligible(pCell, pPrimaryExt->CanTarget, true, true)
57+
|| !pPrimaryExt->IsHealthRatioEligible(pThis)
58+
|| (pPrimaryExt->AttachEffect_CheckOnFirer
59+
&& !pPrimaryExt->HasRequiredAttachedEffects(pThis, pThis))))
5660
{
5761
R->EAX(1);
5862
return ReturnWeaponIndex;
@@ -257,52 +261,48 @@ DEFINE_HOOK(0x6FC339, TechnoClass_CanFire, 0x6)
257261
}
258262

259263
// AAOnly doesn't need to be checked if LandTargeting=1.
260-
if (pThis->GetTechnoType()->LandTargeting != LandTargetingType::Land_Not_OK && pWeapon->Projectile->AA && pTarget && !pTarget->IsInAir())
264+
if (pThis->GetTechnoType()->LandTargeting != LandTargetingType::Land_Not_OK && pWeapon->Projectile->AA
265+
&& pTarget && !pTarget->IsInAir() && BulletTypeExt::ExtMap.Find(pWeapon->Projectile)->AAOnly)
261266
{
262-
const auto pBulletTypeExt = BulletTypeExt::ExtMap.Find(pWeapon->Projectile);
263-
264-
if (pBulletTypeExt->AAOnly)
265-
return CannotFire;
267+
return CannotFire;
266268
}
267269

268270
const auto pTechno = abstract_cast<TechnoClass*>(pTarget);
269271
CellClass* pTargetCell = nullptr;
270272

271273
if (pTarget)
272274
{
273-
if (const auto pCell = abstract_cast<CellClass*>(pTarget))
274-
{
275-
pTargetCell = pCell;
276-
}
277-
else if (const auto pObject = abstract_cast<ObjectClass*>(pTarget))
275+
if (const auto pObject = abstract_cast<ObjectClass*, true>(pTarget))
278276
{
279277
// Ignore target cell for technos that are in air.
280278
if ((pTechno && !pTechno->IsInAir()) || pObject != pTechno)
281279
pTargetCell = pObject->GetCell();
282280
}
281+
else if (const auto pCell = abstract_cast<CellClass*, true>(pTarget))
282+
{
283+
pTargetCell = pCell;
284+
}
283285
}
284286

285287
const auto pWeaponExt = WeaponTypeExt::ExtMap.Find(pWeapon);
286288

287-
if (!pWeaponExt->SkipWeaponPicking && pTargetCell)
289+
if (!pWeaponExt->SkipWeaponPicking && pTargetCell
290+
&& !EnumFunctions::IsCellEligible(pTargetCell, pWeaponExt->CanTarget, true, true))
288291
{
289-
if (!EnumFunctions::IsCellEligible(pTargetCell, pWeaponExt->CanTarget, true, true))
290-
return CannotFire;
292+
return CannotFire;
291293
}
292294

293295
if (pTechno)
294296
{
295297
if (!pWeaponExt->SkipWeaponPicking)
296298
{
297-
if (!EnumFunctions::IsTechnoEligible(pTechno, pWeaponExt->CanTarget) ||
298-
!EnumFunctions::CanTargetHouse(pWeaponExt->CanTargetHouses, pThis->Owner, pTechno->Owner) ||
299-
!pWeaponExt->IsHealthRatioEligible(pTechno))
299+
if (!EnumFunctions::IsTechnoEligible(pTechno, pWeaponExt->CanTarget)
300+
|| !EnumFunctions::CanTargetHouse(pWeaponExt->CanTargetHouses, pThis->Owner, pTechno->Owner)
301+
|| !pWeaponExt->IsHealthRatioEligible(pTechno)
302+
|| !pWeaponExt->HasRequiredAttachedEffects(pTechno, pThis))
300303
{
301304
return CannotFire;
302305
}
303-
304-
if (!pWeaponExt->HasRequiredAttachedEffects(pTechno, pThis))
305-
return CannotFire;
306306
}
307307

308308
if (pWH->Airstrike)
@@ -369,8 +369,8 @@ DEFINE_HOOK(0x6FC689, TechnoClass_CanFire_LandNavalTarget, 0x6)
369369
{
370370
const auto landType = pCell->LandType;
371371

372-
if (pType->NavalTargeting == NavalTargetingType::Naval_None &&
373-
(landType == LandType::Water || landType == LandType::Beach))
372+
if (pType->NavalTargeting == NavalTargetingType::Naval_None
373+
&& (landType == LandType::Water || landType == LandType::Beach))
374374
{
375375
return DisallowFiring;
376376
}
@@ -379,13 +379,13 @@ DEFINE_HOOK(0x6FC689, TechnoClass_CanFire_LandNavalTarget, 0x6)
379379
{
380380
const auto landType = pTerrain->GetCell()->LandType;
381381

382-
if (pType->LandTargeting == LandTargetingType::Land_Not_OK &&
383-
landType != LandType::Water && landType != LandType::Beach)
382+
if (pType->LandTargeting == LandTargetingType::Land_Not_OK
383+
&& landType != LandType::Water && landType != LandType::Beach)
384384
{
385385
return DisallowFiring;
386386
}
387-
else if (pType->NavalTargeting == NavalTargetingType::Naval_None &&
388-
(landType == LandType::Water || landType == LandType::Beach))
387+
else if (pType->NavalTargeting == NavalTargetingType::Naval_None
388+
&& (landType == LandType::Water || landType == LandType::Beach))
389389
{
390390
return DisallowFiring;
391391
}

src/Ext/Techno/WeaponHelpers.cpp

Lines changed: 51 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -26,46 +26,75 @@ int TechnoExt::PickWeaponIndex(TechnoClass* pThis, TechnoClass* pTargetTechno, A
2626
CellClass* pTargetCell = nullptr;
2727

2828
// Ignore target cell for airborne target technos.
29-
if (!pTargetTechno || !pTargetTechno->IsInAir())
29+
if (pTarget && (!pTargetTechno || !pTargetTechno->IsInAir()))
3030
{
31-
if (auto const pCell = abstract_cast<CellClass*>(pTarget))
32-
pTargetCell = pCell;
33-
else if (auto const pObject = abstract_cast<ObjectClass*>(pTarget))
31+
if (auto const pObject = abstract_cast<ObjectClass*, true>(pTarget))
3432
pTargetCell = pObject->GetCell();
33+
else if (auto const pCell = abstract_cast<CellClass*, true>(pTarget))
34+
pTargetCell = pCell;
3535
}
3636

37-
if (!pSecondExt->SkipWeaponPicking && (pTargetCell && !EnumFunctions::IsCellEligible(pTargetCell, pSecondExt->CanTarget, true, true)) ||
38-
(pTargetTechno && (!EnumFunctions::IsTechnoEligible(pTargetTechno, pSecondExt->CanTarget) ||
39-
!EnumFunctions::CanTargetHouse(pSecondExt->CanTargetHouses, pThis->Owner, pTargetTechno->Owner) ||
40-
!pSecondExt->IsHealthRatioEligible(pTargetTechno) ||
41-
!pSecondExt->HasRequiredAttachedEffects(pTargetTechno, pThis))))
37+
if (!pSecondExt->SkipWeaponPicking)
4238
{
43-
return weaponIndexOne;
39+
if (pTargetCell && !EnumFunctions::IsCellEligible(pTargetCell, pSecondExt->CanTarget, true, true))
40+
return weaponIndexOne;
41+
42+
if (pTargetTechno)
43+
{
44+
if (!EnumFunctions::IsTechnoEligible(pTargetTechno, pSecondExt->CanTarget)
45+
|| !EnumFunctions::CanTargetHouse(pSecondExt->CanTargetHouses, pThis->Owner, pTargetTechno->Owner)
46+
|| !pSecondExt->IsHealthRatioEligible(pTargetTechno)
47+
|| !pSecondExt->HasRequiredAttachedEffects(pTargetTechno, pThis))
48+
{
49+
return weaponIndexOne;
50+
}
51+
}
4452
}
4553

4654
const bool secondIsAA = pTargetTechno && pTargetTechno->IsInAir() && pWeaponTwo->Projectile->AA;
4755
const bool skipPrimaryPicking = pFirstExt->SkipWeaponPicking;
4856
const bool firstAllowedAE = !skipPrimaryPicking && pFirstExt->HasRequiredAttachedEffects(pTargetTechno, pThis);
4957

50-
if (!allowFallback && (!allowAAFallback || !secondIsAA) && !TechnoExt::CanFireNoAmmoWeapon(pThis, 1) && firstAllowedAE)
58+
if (!allowFallback
59+
&& (!allowAAFallback || !secondIsAA)
60+
&& !TechnoExt::CanFireNoAmmoWeapon(pThis, 1)
61+
&& firstAllowedAE)
62+
{
5163
return weaponIndexOne;
64+
}
5265

53-
if (!skipPrimaryPicking && (pTargetCell && !EnumFunctions::IsCellEligible(pTargetCell, pFirstExt->CanTarget, true, true)) ||
54-
(pTargetTechno && (!EnumFunctions::IsTechnoEligible(pTargetTechno, pFirstExt->CanTarget) ||
55-
!EnumFunctions::CanTargetHouse(pFirstExt->CanTargetHouses, pThis->Owner, pTargetTechno->Owner) ||
56-
!pFirstExt->IsHealthRatioEligible(pTargetTechno) || !firstAllowedAE)))
66+
if (!skipPrimaryPicking)
5767
{
58-
return weaponIndexTwo;
59-
}
68+
if (pTargetCell && !EnumFunctions::IsCellEligible(pTargetCell, pFirstExt->CanTarget, true, true))
69+
return weaponIndexTwo;
6070

61-
auto const pType = pThis->GetTechnoType();
71+
if (pTargetTechno)
72+
{
73+
if (!EnumFunctions::IsTechnoEligible(pTargetTechno, pFirstExt->CanTarget)
74+
|| !EnumFunctions::CanTargetHouse(pFirstExt->CanTargetHouses, pThis->Owner, pTargetTechno->Owner)
75+
|| !pFirstExt->IsHealthRatioEligible(pTargetTechno)
76+
|| !firstAllowedAE)
77+
{
78+
return weaponIndexTwo;
79+
}
80+
}
81+
}
6282

6383
// Handle special case with NavalTargeting / LandTargeting.
64-
if (!pTargetTechno && (pType->NavalTargeting == NavalTargetingType::Naval_Primary ||
65-
pType->LandTargeting == LandTargetingType::Land_Secondary) &&
66-
pTargetCell->LandType != LandType::Water && pTargetCell->LandType != LandType::Beach)
84+
if (!pTargetTechno && pTargetCell)
6785
{
68-
return weaponIndexTwo;
86+
auto const pType = pThis->GetTechnoType();
87+
88+
if (pType->NavalTargeting == NavalTargetingType::Naval_Primary
89+
|| pType->LandTargeting == LandTargetingType::Land_Secondary)
90+
{
91+
auto const landType = pTargetCell->LandType;
92+
93+
if (landType != LandType::Water && landType != LandType::Beach)
94+
{
95+
return weaponIndexTwo;
96+
}
97+
}
6998
}
7099

71100
return -1;

0 commit comments

Comments
 (0)