Skip to content

Commit 8132fd6

Browse files
committed
refactor: Use stream composition to check for usable weapon bays
1 parent 29643a5 commit 8132fd6

File tree

1 file changed

+8
-18
lines changed

1 file changed

+8
-18
lines changed

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

+8-18
Original file line numberDiff line numberDiff line change
@@ -1403,27 +1403,17 @@ private static String toHitIsImpossible(Game game, Entity ae, int attackerId, Ta
14031403

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

14091409
// first check to see if there are any usable weapons
1410-
boolean usable = false;
1411-
for (WeaponMounted m : weapon.getBayWeapons()) {
1412-
WeaponType bayWType = m.getType();
1413-
boolean bayWUsesAmmo = (bayWType.getAmmoType() != AmmoType.T_NA);
1414-
if (m.canFire()) {
1415-
if (bayWUsesAmmo) {
1416-
if ((m.getLinked() != null) && (m.getLinked().getUsableShotsLeft() > 0)) {
1417-
usable = true;
1418-
break;
1419-
}
1420-
} else {
1421-
usable = true;
1422-
break;
1423-
}
1424-
}
1425-
}
1426-
if (!usable) {
1410+
if (!streamBayWeapons()
1411+
.filter(WeaponMounted::canFire)
1412+
.anyMatch(m ->
1413+
m.getType().getAmmoType() == AmmoType.T_NA
1414+
|| Optional.ofNullable(m.getLinked()).map(a -> a.getUsableShotsLeft() > 0).orElse(false)
1415+
)
1416+
) {
14271417
return Messages.getString("WeaponAttackAction.BayNotReady");
14281418
}
14291419

0 commit comments

Comments
 (0)