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 e3ead6d commit 262c4ca
Showing 1 changed file with 27 additions and 34 deletions.
61 changes: 27 additions & 34 deletions megamek/src/megamek/common/actions/WeaponAttackAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -456,48 +456,41 @@ 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()) {
Mounted<?> bayWAmmo = bayW.getLinked();
(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;
}
AmmoType bAmmo = (AmmoType) bayWAmmo.getType();
if (bayWAmmo == null) {
//At present, all weapons below using mLinker use ammo, so this won't be a problem
return;
}
AmmoType bAmmo = (AmmoType) bayWAmmo.getType();

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

//If the artillery bay is firing cruise missiles, they have some special rules
//It is possible to combine cruise missiles and other artillery in a bay, so
//set this to true if any of the weapons are cruise missile launchers.
if (bayW.getType().hasFlag(WeaponType.F_CRUISE_MISSILE)) {
isCruiseMissile = true;
}
//If the artillery bay is firing cruise missiles, they have some special rules
//It is possible to combine cruise missiles and other artillery in a bay, so
//set this to true if any of the weapons are cruise missile launchers.
if (bayW.getType().hasFlag(WeaponType.F_CRUISE_MISSILE)) {
isCruiseMissile = true;
}

mLinker = bayW.getLinkedBy();
bApollo = ((mLinker != null) && (mLinker.getType() instanceof MiscType) && !mLinker.isDestroyed()
&& !mLinker.isMissing() && !mLinker.isBreached() && mLinker.getType().hasFlag(MiscType.F_APOLLO))
&& (bAmmo != null) && (bAmmo.getAmmoType() == AmmoType.T_MRM);
mLinker = bayW.getLinkedBy();
bApollo = ((mLinker != null) && (mLinker.getType() instanceof MiscType) && !mLinker.isDestroyed()
&& !mLinker.isMissing() && !mLinker.isBreached() && mLinker.getType().hasFlag(MiscType.F_APOLLO))
&& (bAmmo != null) && (bAmmo.getAmmoType() == AmmoType.T_MRM);

bArtemisV = ((mLinker != null) && (mLinker.getType() instanceof MiscType) && !mLinker.isDestroyed()
&& !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)));
}
}
bArtemisV = ((mLinker != null) && (mLinker.getType() instanceof MiscType) && !mLinker.isDestroyed()
&& !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.

Check notice

Code scanning / CodeQL

Unread local variable Note

Variable 'AmmoType bAmmo' is never read.

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

Expand Down

0 comments on commit 262c4ca

Please sign in to comment.