Skip to content

Commit b21fe79

Browse files
committed
refactor: Use stream composition for air-to-ground target modifiers
1 parent 891a17d commit b21fe79

File tree

1 file changed

+10
-12
lines changed

1 file changed

+10
-12
lines changed

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

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3676,18 +3676,16 @@ private static ToHitData compileAeroAttackerToHitMods(Game game, Entity ae, Targ
36763676
}
36773677
// units making air to ground attacks are easier to hit by air-to-air
36783678
// attacks
3679-
if (Compute.isAirToAir(ae, target)) {
3680-
for (Enumeration<EntityAction> i = game.getActions(); i.hasMoreElements();) {
3681-
EntityAction ea = i.nextElement();
3682-
if (!(ea instanceof WeaponAttackAction)) {
3683-
continue;
3684-
}
3685-
WeaponAttackAction prevAttack = (WeaponAttackAction) ea;
3686-
if ((te != null && prevAttack.getEntityId() == te.getId()) && prevAttack.isAirToGround(game)) {
3687-
toHit.addModifier(-3, Messages.getString("WeaponAttackAction.TeGroundAttack"));
3688-
break;
3689-
}
3690-
}
3679+
3680+
// The ID of the entity being targeted.
3681+
final Optional<Integer> targetEntityId = Optional.ofNullable(te).map(Entity::getId);
3682+
3683+
if (Compute.isAirToAir(ae, target) && targetEntityId.isPresent() && game.getActionsVector().stream()
3684+
.filter(WeaponAttackAction.class::isInstance)
3685+
.map(WeaponAttackAction.class::cast)
3686+
.anyMatch(prevAttack -> prevAttack.getEntityId() == targetEntityId.orElseThrow() && prevAttack.isAirToGround(game))
3687+
) {
3688+
toHit.addModifier(-3, Messages.getString("WeaponAttackAction.TeGroundAttack"));
36913689
}
36923690
// grounded aero
36933691
if (!ae.isAirborne() && !ae.isSpaceborne()) {

0 commit comments

Comments
 (0)