Phobos icon indicating copy to clipboard operation
Phobos copied to clipboard

Remove hardcoded AA/Gattling weapon selection restrictions

Open Starkku opened this issue 3 years ago • 6 comments

  • If both Primary and Secondary weapons can fire at air targets (projectile has AA=true), Primary can now be picked instead of always forcing Secondary. Also applies to IsGattling=true, with odd-numbered and even-numbered WeaponX slots instead of Primary and Secondary, respectively.
  • IsGattling=true can now fall back to ´secondary´ weapon slot (even-numbered WeaponX slots) if 'primary' one (odd-numbered) cannot fire at current target (armor type, CanTarget(Houses), shield etc).

This allows for things like AA-only units with two different weapons and units using gattling logic with two sets of weapons that can both fire at ground targets.

As a bonus threw these fixes in:

  • Fixed LandTargeting=1 not preventing from targeting TerrainTypes (trees etc.) on land.
  • Fixed NavalTargeting=6 not preventing from targeting empty water cells or TerrainTypes (trees etc.) on water.
  • Fixed NavalTargeting=7 and/or LandTargeting=2 resulting in still targeting TerrainTypes (trees etc.) on land with Primary weapon.

Starkku avatar Sep 07 '22 19:09 Starkku

Nightly build for this pull request:

github-actions[bot] avatar Sep 07 '22 19:09 github-actions[bot]

Fixed the outstanding issues from feedback and simplified some of the code.

Starkku avatar Sep 08 '22 09:09 Starkku

I think if gattling can select weapon by versus.... Just a suggestion.

DEFINE_HOOK(0x6F3436, TechnoClass_SelectGattlingWeapon, 0x6)
{
	GET(TechnoClass*, pThis, ESI);
	GET(AbstractClass*, pTarget, EBP);

	enum { Odd = 0x6F346A, Even = 0x6F345C, Origin = 0 };
	int gattlingStage = pThis->CurrentGattlingStage;
	auto pTypeExt = TechnoTypeExt::ExtMap.Find(pThis->GetTechnoType());

	if (pTypeExt->Gattling_SelectWeaponByVersus)
	{
		WeaponTypeClass* pWeaponOdd = pThis->GetWeapon(gattlingStage << 1)->WeaponType;
		WeaponTypeClass* pWeaponEven = pThis->GetWeapon(gattlingStage << 1 | 1)->WeaponType;

		if (auto pTargetTechno = abstract_cast<TechnoClass*>(pTarget))
		{
			R->ESI(gattlingStage);

			auto pTargetExt = TechnoExt::ExtMap.Find(pTargetTechno);
			if (pTargetTechno->IsInAir())
			{
				if (!pWeaponEven->Projectile->AA)
					return Odd;

				if (GeneralUtils::GetWarheadVersusArmor(pWeaponEven->Warhead, pTargetExt->GetArmorIdx(pWeaponEven)) != 0.0)
					return Even;

				if (GeneralUtils::GetWarheadVersusArmor(pWeaponOdd->Warhead, pTargetExt->GetArmorIdx(pWeaponOdd)) == 0.0)
					return Even;

				return Odd;
			}
			else
			{
				if (!pWeaponOdd->Projectile->AG)
					return Even;

				if (GeneralUtils::GetWarheadVersusArmor(pWeaponOdd->Warhead, pTargetExt->GetArmorIdx(pWeaponOdd)) != 0.0)
					return Odd;

				if (GeneralUtils::GetWarheadVersusArmor(pWeaponEven->Warhead, pTargetExt->GetArmorIdx(pWeaponEven)) == 0.0)
					return Odd;

				return Even;
			}
		}
		else
		{
			if (auto pTargetObject = abstract_cast<ObjectClass*>(pTarget))
			{
				ObjectTypeClass* pTargetType = pTargetObject->GetType();

				if (GeneralUtils::GetWarheadVersusArmor(pWeaponOdd->Warhead, pTargetType->Armor) != 0.0)
					return Odd;

				if (GeneralUtils::GetWarheadVersusArmor(pWeaponEven->Warhead, pTargetType->Armor) == 0.0)
					return Odd;

				return Even;
			}
			else
			{
				return Odd;
			}
		}
	}

	return Origin;
}

ststl-s avatar Sep 09 '22 05:09 ststl-s

Did some minor code tweaks and added Land/NavalTargeting support for IsGattling weapons in same manner as regular weapons.

I think if gattling can select weapon by versus.... Just a suggestion.

Current code already behaves largely the same as that, and not counting for Phobos-required checks and the Naval/LandTargeting things I just added, is also less verbose. Given that the gattling weapon fallbacks were implemented with consistency with regular weapons in mind, the main differences are that the AG=false fallback from odd to even and armor type checks for non-techno targets do not exist. And the point was to make it work without separate toggle anyways, which I believe it currently does just fine.

Starkku avatar Sep 11 '22 20:09 Starkku

I found that if you ordered a boomber submarine (who has LandTargeting = 2) to attack a tree on land, it will try to use torpedo (primary weapon). I think it's incorrect, it should use missile(secondary weapon).

NetsuNegi avatar Oct 01 '22 03:10 NetsuNegi

Fixed the vanilla bug of LandTargeting=2 and/or NavalTargeting=7 not changing weapon used against TerrainTypes on land to Secondary.

Starkku avatar Oct 01 '22 10:10 Starkku