Skip to content

Commit 633dd4a

Browse files
authored
[Minor] Adjust Gatling weapon selection (#2133)
the current gatling weapon selection logic will only check if the target is vulnerable to odd weapon, and simply switch to secondary if it doesn't. This will create issue when both odd and even weapons are not supposed to attack the target. This pull request make it follow the process that other weapon selection is using, which is checking if even weapon is available first and then odd weapon also fixed & regulated a few things in weapon selection
1 parent 72fbde4 commit 633dd4a

File tree

3 files changed

+47
-42
lines changed

3 files changed

+47
-42
lines changed

src/Ext/Techno/Hooks.Firing.cpp

Lines changed: 45 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#include "Body.h"
1+
#include "Body.h"
22

33
#include <Ext/Anim/Body.h>
44
#include <Ext/Building/Body.h>
@@ -199,59 +199,68 @@ DEFINE_HOOK(0x6F3432, TechnoClass_WhatWeaponShouldIUse_Gattling, 0xA)
199199
auto const pTargetTechno = abstract_cast<TechnoClass*>(pTarget);
200200
const int oddWeaponIndex = 2 * pThis->CurrentGattlingStage;
201201
const int evenWeaponIndex = oddWeaponIndex + 1;
202-
int chosenWeaponIndex = oddWeaponIndex;
203202
const int eligibleWeaponIndex = TechnoExt::PickWeaponIndex(pThis, pTargetTechno, pTarget, oddWeaponIndex, evenWeaponIndex, true);
204203

205204
if (eligibleWeaponIndex != -1)
206205
{
207-
chosenWeaponIndex = eligibleWeaponIndex;
206+
R->EAX(eligibleWeaponIndex);
207+
return UseWeaponIndex;
208208
}
209-
else if (pTargetTechno)
209+
210+
int chosenWeaponIndex = oddWeaponIndex;
211+
212+
if (pTargetTechno)
210213
{
211214
auto const pTargetExt = TechnoExt::ExtMap.Find(pTargetTechno);
212-
auto const pWeaponOdd = pThis->GetWeapon(oddWeaponIndex)->WeaponType;
213215
auto const pWeaponEven = pThis->GetWeapon(evenWeaponIndex)->WeaponType;
214-
bool skipRemainingChecks = false;
216+
auto const pShield = pTargetExt->Shield.get();
217+
auto const armor = pTargetTechno->GetTechnoType()->Armor;
218+
const bool inAir = pTargetTechno->IsInAir();
219+
const bool isUnderground = pTargetTechno->InWhichLayer() == Layer::Underground;
215220

216-
if (const auto pShield = pTargetExt->Shield.get())
217-
{
218-
if (pShield->IsActive() && !pShield->CanBeTargeted(pWeaponOdd))
221+
auto isWeaponValid = [&](WeaponTypeClass* pWeapon)
219222
{
220-
chosenWeaponIndex = evenWeaponIndex;
221-
skipRemainingChecks = true;
222-
}
223+
if (inAir && !pWeapon->Projectile->AA)
224+
return false;
225+
if (isUnderground && !BulletTypeExt::ExtMap.Find(pWeapon->Projectile)->AU)
226+
return false;
227+
if (pShield && pShield->IsActive() && !pShield->CanBeTargeted(pWeapon))
228+
return false;
229+
if (GeneralUtils::GetWarheadVersusArmor(pWeapon->Warhead, armor) == 0.0)
230+
return false;
231+
return true;
232+
};
233+
234+
// check even weapon first
235+
236+
if (!isWeaponValid(pWeaponEven))
237+
{
238+
R->EAX(chosenWeaponIndex);
239+
return UseWeaponIndex;
223240
}
224241

225-
if (!skipRemainingChecks)
242+
// handle naval targeting
243+
244+
if (!pTargetTechno->OnBridge && !inAir)
226245
{
227-
if (GeneralUtils::GetWarheadVersusArmor(pWeaponOdd->Warhead, pTargetTechno->GetTechnoType()->Armor) == 0.0)
228-
{
229-
chosenWeaponIndex = evenWeaponIndex;
230-
}
231-
else if (pTargetTechno->InWhichLayer() == Layer::Underground)
246+
auto const landType = pTargetTechno->GetCell()->LandType;
247+
248+
if (landType == LandType::Water || landType == LandType::Beach)
232249
{
233-
if (BulletTypeExt::ExtMap.Find(pWeaponEven->Projectile)->AU && !BulletTypeExt::ExtMap.Find(pWeaponOdd->Projectile)->AU)
250+
if (pThis->SelectNavalTargeting(pTargetTechno) == 1)
234251
chosenWeaponIndex = evenWeaponIndex;
235-
}
236-
else
237-
{
238-
auto const landType = pTargetTechno->GetCell()->LandType;
239-
const bool isOnWater = (landType == LandType::Water || landType == LandType::Beach) && !pTargetTechno->IsInAir();
240252

241-
if (!pTargetTechno->OnBridge && isOnWater && pThis->SelectNavalTargeting(pTargetTechno) == 2)
242-
{
243-
chosenWeaponIndex = evenWeaponIndex;
244-
}
245-
else if (pTargetTechno->IsInAir() && !pWeaponOdd->Projectile->AA && pWeaponEven->Projectile->AA)
246-
{
247-
chosenWeaponIndex = evenWeaponIndex;
248-
}
249-
else if (pThis->GetTechnoType()->LandTargeting == LandTargetingType::Land_Secondary)
250-
{
251-
chosenWeaponIndex = evenWeaponIndex;
252-
}
253+
R->EAX(chosenWeaponIndex);
254+
return UseWeaponIndex;
253255
}
254256
}
257+
258+
// check odd weapon
259+
260+
auto const pWeaponOdd = pThis->GetWeapon(oddWeaponIndex)->WeaponType;
261+
262+
if (!isWeaponValid(pWeaponOdd) || pThis->GetTechnoType()->LandTargeting == LandTargetingType::Land_Secondary)
263+
chosenWeaponIndex = evenWeaponIndex;
255264
}
256265

257266
R->EAX(chosenWeaponIndex);

src/Ext/Techno/Hooks.WeaponRange.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ DEFINE_HOOK(0x6FC3A1, TechnoClass_CanFire_InBunkerRangeCheck, 0x5)
196196
GET(TechnoClass*, pThis, EBP);
197197
GET(WeaponTypeClass*, pWeapon, EDI);
198198

199-
if (pThis->WhatAmI() == AbstractType::Unit && WeaponTypeExt::GetRangeWithModifiers(pWeapon, pThis) < 384.0)
199+
if (pThis->WhatAmI() == AbstractType::Unit && WeaponTypeExt::GetRangeWithModifiers(pWeapon, pThis) < 384)
200200
return CannotFire;
201201

202202
return ContinueChecks;

src/Ext/TechnoType/Body.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -158,9 +158,6 @@ int TechnoTypeExt::ExtData::SelectMultiWeapon(TechnoClass* const pThis, Abstract
158158

159159
if (const auto pTargetTechno = abstract_cast<TechnoClass*, true>(pTarget))
160160
{
161-
if (pTargetTechno->Health <= 0 || !pTargetTechno->IsAlive)
162-
return 0;
163-
164161
bool getNavalTargeting = false;
165162

166163
auto checkSecondary = [&](int weaponIndex) -> bool
@@ -217,9 +214,8 @@ int TechnoTypeExt::ExtData::SelectMultiWeapon(TechnoClass* const pThis, Abstract
217214
};
218215

219216
const LandType landType = pTargetTechno->GetCell()->LandType;
220-
const bool targetOnWater = landType == LandType::Water || landType == LandType::Beach;
221217

222-
if (!pTargetTechno->OnBridge && targetOnWater)
218+
if (!pTargetTechno->OnBridge && (landType == LandType::Water || landType == LandType::Beach) && !pTargetTechno->IsInAir())
223219
{
224220
const int result = pThis->SelectNavalTargeting(pTargetTechno);
225221

0 commit comments

Comments
 (0)