Skip to content

Commit ceec3db

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 e3ead6d commit ceec3db

File tree

1 file changed

+27
-34
lines changed

1 file changed

+27
-34
lines changed

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

+27-34
Original file line numberDiff line numberDiff line change
@@ -456,48 +456,41 @@ 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

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

474-
if (bayWAmmo == null) {
475-
//At present, all weapons below using mLinker use ammo, so this won't be a problem
476-
continue;
477-
}
478-
AmmoType bAmmo = (AmmoType) bayWAmmo.getType();
468+
if (bayWAmmo == null) {
469+
//At present, all weapons below using mLinker use ammo, so this won't be a problem
470+
return;
471+
}
472+
AmmoType bAmmo = (AmmoType) bayWAmmo.getType();
479473

480-
//If we're using optional rules and firing Arrow Homing missiles from a bay...
481-
isHoming = bAmmo != null && bAmmo.getMunitionType().contains(AmmoType.Munitions.M_HOMING);
474+
//If we're using optional rules and firing Arrow Homing missiles from a bay...
475+
isHoming = bAmmo != null && bAmmo.getMunitionType().contains(AmmoType.Munitions.M_HOMING);
482476

483-
//If the artillery bay is firing cruise missiles, they have some special rules
484-
//It is possible to combine cruise missiles and other artillery in a bay, so
485-
//set this to true if any of the weapons are cruise missile launchers.
486-
if (bayW.getType().hasFlag(WeaponType.F_CRUISE_MISSILE)) {
487-
isCruiseMissile = true;
488-
}
477+
//If the artillery bay is firing cruise missiles, they have some special rules
478+
//It is possible to combine cruise missiles and other artillery in a bay, so
479+
//set this to true if any of the weapons are cruise missile launchers.
480+
if (bayW.getType().hasFlag(WeaponType.F_CRUISE_MISSILE)) {
481+
isCruiseMissile = true;
482+
}
489483

490-
mLinker = bayW.getLinkedBy();
491-
bApollo = ((mLinker != null) && (mLinker.getType() instanceof MiscType) && !mLinker.isDestroyed()
492-
&& !mLinker.isMissing() && !mLinker.isBreached() && mLinker.getType().hasFlag(MiscType.F_APOLLO))
493-
&& (bAmmo != null) && (bAmmo.getAmmoType() == AmmoType.T_MRM);
484+
mLinker = bayW.getLinkedBy();
485+
bApollo = ((mLinker != null) && (mLinker.getType() instanceof MiscType) && !mLinker.isDestroyed()
486+
&& !mLinker.isMissing() && !mLinker.isBreached() && mLinker.getType().hasFlag(MiscType.F_APOLLO))
487+
&& (bAmmo != null) && (bAmmo.getAmmoType() == AmmoType.T_MRM);
494488

495-
bArtemisV = ((mLinker != null) && (mLinker.getType() instanceof MiscType) && !mLinker.isDestroyed()
496-
&& !mLinker.isMissing() && !mLinker.isBreached() && mLinker.getType().hasFlag(MiscType.F_ARTEMIS_V)
497-
&& !isECMAffected && !bMekTankStealthActive && (atype != null)
498-
&& (bAmmo != null) && (bAmmo.getMunitionType().contains(AmmoType.Munitions.M_ARTEMIS_V_CAPABLE)));
499-
}
500-
}
489+
bArtemisV = ((mLinker != null) && (mLinker.getType() instanceof MiscType) && !mLinker.isDestroyed()
490+
&& !mLinker.isMissing() && !mLinker.isBreached() && mLinker.getType().hasFlag(MiscType.F_ARTEMIS_V)
491+
&& !isECMAffected && !bMekTankStealthActive && (atype != null)
492+
&& (bAmmo != null) && (bAmmo.getMunitionType().contains(AmmoType.Munitions.M_ARTEMIS_V_CAPABLE)));
493+
);
501494

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

0 commit comments

Comments
 (0)