Skip to content

Commit

Permalink
refactor: Use stream composition to check for usable weapon bays
Browse files Browse the repository at this point in the history
  • Loading branch information
Saklad5 committed Aug 18, 2024
1 parent 29643a5 commit 8132fd6
Showing 1 changed file with 8 additions and 18 deletions.
26 changes: 8 additions & 18 deletions megamek/src/megamek/common/actions/WeaponAttackAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -1403,27 +1403,17 @@ private static String toHitIsImpossible(Game game, Entity ae, int attackerId, Ta

// limit large craft to zero net heat and to heat by arc
final int heatCapacity = ae.getHeatCapacity();
if (ae.usesWeaponBays() && (weapon != null) && !weapon.getBayWeapons().isEmpty()) {
if (ae.usesWeaponBays() && (weapon != null)) {
int totalHeat = 0;

// first check to see if there are any usable weapons
boolean usable = false;
for (WeaponMounted m : weapon.getBayWeapons()) {
WeaponType bayWType = m.getType();
boolean bayWUsesAmmo = (bayWType.getAmmoType() != AmmoType.T_NA);
if (m.canFire()) {
if (bayWUsesAmmo) {
if ((m.getLinked() != null) && (m.getLinked().getUsableShotsLeft() > 0)) {
usable = true;
break;
}
} else {
usable = true;
break;
}
}
}
if (!usable) {
if (!streamBayWeapons()
.filter(WeaponMounted::canFire)
.anyMatch(m ->
m.getType().getAmmoType() == AmmoType.T_NA
|| Optional.ofNullable(m.getLinked()).map(a -> a.getUsableShotsLeft() > 0).orElse(false)
)
) {
return Messages.getString("WeaponAttackAction.BayNotReady");
}

Expand Down

0 comments on commit 8132fd6

Please sign in to comment.