Skip to content

Commit a4d6fee

Browse files
committed
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.
1 parent a3bbba8 commit a4d6fee

File tree

1 file changed

+6
-11
lines changed

1 file changed

+6
-11
lines changed

megamek/src/megamek/common/actions/WeaponAttackAction.java

+6-11
Original file line numberDiff line numberDiff line change
@@ -456,24 +456,19 @@ private static ToHitData toHitCalc(Game game, int attackerId, Targetable target,
456456
&& (munition.contains(AmmoType.Munitions.M_FOLLOW_THE_LEADER)
457457
&& !ComputeECM.isAffectedByECM(ae, ae.getPosition(), target.getPosition()));
458458

459-
Mounted mLinker = weapon.getLinkedBy();
459+
Mounted mLinker;
460460

461-
boolean bApollo = ((mLinker != null) && (mLinker.getType() instanceof MiscType) && !mLinker.isDestroyed()
462-
&& !mLinker.isMissing() && !mLinker.isBreached() && mLinker.getType().hasFlag(MiscType.F_APOLLO))
463-
&& (atype != null) && (atype.getAmmoType() == AmmoType.T_MRM);
461+
boolean bApollo;
464462

465-
boolean bArtemisV = ((mLinker != null) && (mLinker.getType() instanceof MiscType) && !mLinker.isDestroyed()
466-
&& !mLinker.isMissing() && !mLinker.isBreached() && mLinker.getType().hasFlag(MiscType.F_ARTEMIS_V)
467-
&& !isECMAffected && !bMekTankStealthActive && (atype != null)
468-
&& (munition.contains(AmmoType.Munitions.M_ARTEMIS_V_CAPABLE)));
463+
boolean bArtemisV;
469464

470465
if (ae.usesWeaponBays()) {
471-
for (WeaponMounted bayW : weapon.getBayWeapons()) {
466+
(ae.usesWeaponBays() ? weapon.streamBayWeapons() : Stream.of(weapon)).forEach (bayW ->
472467
Mounted<?> bayWAmmo = bayW.getLinked();
473468

474469
if (bayWAmmo == null) {
475470
//At present, all weapons below using mLinker use ammo, so this won't be a problem
476-
continue;
471+
return;
477472
}
478473
AmmoType bAmmo = (AmmoType) bayWAmmo.getType();
479474

@@ -496,7 +491,7 @@ private static ToHitData toHitCalc(Game game, int attackerId, Targetable target,
496491
&& !mLinker.isMissing() && !mLinker.isBreached() && mLinker.getType().hasFlag(MiscType.F_ARTEMIS_V)
497492
&& !isECMAffected && !bMekTankStealthActive && (atype != null)
498493
&& (bAmmo != null) && (bAmmo.getMunitionType().contains(AmmoType.Munitions.M_ARTEMIS_V_CAPABLE)));
499-
}
494+
)
500495
}
501496

502497
boolean inSameBuilding = Compute.isInSameBuilding(game, ae, te);

0 commit comments

Comments
 (0)