From a4d6fee2dc14d1fea5b8ebf54bd9955b6fd49269 Mon Sep 17 00:00:00 2001 From: Jeremy Saklad Date: Sun, 18 Aug 2024 12:04:52 -0500 Subject: [PATCH] refactor: Clean up code for weapon-linked components The new implementation iterates over weapons lazily, and combines the code for both bay and non-bay weapons. --- .../common/actions/WeaponAttackAction.java | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/megamek/src/megamek/common/actions/WeaponAttackAction.java b/megamek/src/megamek/common/actions/WeaponAttackAction.java index 3c21d30a349..392fdbeac39 100644 --- a/megamek/src/megamek/common/actions/WeaponAttackAction.java +++ b/megamek/src/megamek/common/actions/WeaponAttackAction.java @@ -456,24 +456,19 @@ private static ToHitData toHitCalc(Game game, int attackerId, Targetable target, && (munition.contains(AmmoType.Munitions.M_FOLLOW_THE_LEADER) && !ComputeECM.isAffectedByECM(ae, ae.getPosition(), target.getPosition())); - Mounted mLinker = weapon.getLinkedBy(); + Mounted mLinker; - boolean bApollo = ((mLinker != null) && (mLinker.getType() instanceof MiscType) && !mLinker.isDestroyed() - && !mLinker.isMissing() && !mLinker.isBreached() && mLinker.getType().hasFlag(MiscType.F_APOLLO)) - && (atype != null) && (atype.getAmmoType() == AmmoType.T_MRM); + boolean bApollo; - boolean bArtemisV = ((mLinker != null) && (mLinker.getType() instanceof MiscType) && !mLinker.isDestroyed() - && !mLinker.isMissing() && !mLinker.isBreached() && mLinker.getType().hasFlag(MiscType.F_ARTEMIS_V) - && !isECMAffected && !bMekTankStealthActive && (atype != null) - && (munition.contains(AmmoType.Munitions.M_ARTEMIS_V_CAPABLE))); + boolean bArtemisV; if (ae.usesWeaponBays()) { - for (WeaponMounted bayW : weapon.getBayWeapons()) { + (ae.usesWeaponBays() ? weapon.streamBayWeapons() : Stream.of(weapon)).forEach (bayW -> Mounted bayWAmmo = bayW.getLinked(); if (bayWAmmo == null) { //At present, all weapons below using mLinker use ammo, so this won't be a problem - continue; + return; } AmmoType bAmmo = (AmmoType) bayWAmmo.getType(); @@ -496,7 +491,7 @@ private static ToHitData toHitCalc(Game game, int attackerId, Targetable target, && !mLinker.isMissing() && !mLinker.isBreached() && mLinker.getType().hasFlag(MiscType.F_ARTEMIS_V) && !isECMAffected && !bMekTankStealthActive && (atype != null) && (bAmmo != null) && (bAmmo.getMunitionType().contains(AmmoType.Munitions.M_ARTEMIS_V_CAPABLE))); - } + ) } boolean inSameBuilding = Compute.isInSameBuilding(game, ae, te);