Skip to content

Commit

Permalink
refactor: Clean up code for weapon-linked components
Browse files Browse the repository at this point in the history
The new implementation iterates over weapons lazily, and combines the
code for both bay and non-bay weapons.
  • Loading branch information
Saklad5 committed Aug 18, 2024
1 parent c073b1e commit 6902929
Showing 1 changed file with 6 additions and 11 deletions.
17 changes: 6 additions & 11 deletions megamek/src/megamek/common/actions/WeaponAttackAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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)));
}
)

Check notice

Code scanning / CodeQL

Unread local variable Note

Variable 'Mounted<> mLinker' is never read.

Check notice

Code scanning / CodeQL

Unread local variable Note

Variable 'boolean bApollo' is never read.

Check notice

Code scanning / CodeQL

Unread local variable Note

Variable 'boolean bArtemisV' is never read.
}

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

0 comments on commit 6902929

Please sign in to comment.