Skip to content

Commit 720a88e

Browse files
committed
refactor: Use stream composition for firing field guns
It may be possible to optimize this further if it can be assumed that all CI attacks either have the infantry flag or are field guns, but I've settled for replicating the previous logic for now.
1 parent c59a753 commit 720a88e

File tree

1 file changed

+17
-15
lines changed

1 file changed

+17
-15
lines changed

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

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2168,21 +2168,23 @@ private static String toHitIsImpossible(Game game, Entity ae, int attackerId, Ta
21682168
return Messages.getString("WeaponAttackAction.CantMoveAndFieldGun");
21692169
}
21702170
// check for mixing infantry and field gun attacks
2171-
for (Enumeration<EntityAction> i = game.getActions(); i.hasMoreElements();) {
2172-
EntityAction ea = i.nextElement();
2173-
if (!(ea instanceof WeaponAttackAction)) {
2174-
continue;
2175-
}
2176-
final WeaponAttackAction prevAttack = (WeaponAttackAction) ea;
2177-
if (prevAttack.getEntityId() == attackerId) {
2178-
Mounted prevWeapon = ae.getEquipment(prevAttack.getWeaponId());
2179-
if ((prevWeapon.getType().hasFlag(WeaponType.F_INFANTRY)
2180-
&& (weapon.getLocation() == Infantry.LOC_FIELD_GUNS))
2181-
|| (weapon.getType().hasFlag(WeaponType.F_INFANTRY)
2182-
&& (prevWeapon.getLocation() == Infantry.LOC_FIELD_GUNS))) {
2183-
return Messages.getString("WeaponAttackAction.FieldGunOrSAOnly");
2184-
}
2185-
}
2171+
if (game.getActionsVector().stream()
2172+
.filter(WeaponAttackAction.class::isInstance)
2173+
.map(WeaponAttackAction.class::cast)
2174+
.filter(prevAttack -> prevAttack.getEntityId() == attackerId)
2175+
.map(prevAttack -> ae.getEquipment(prevAttack.getWeaponId()))
2176+
.anyMatch(prevWeapon ->
2177+
(
2178+
prevWeapon.getType().hasFlag(WeaponType.F_INFANTRY)
2179+
&& weapon.getLocation() == Infantry.LOC_FIELD_GUNS
2180+
)
2181+
|| (
2182+
prevWeapon.getLocation() == Infantry.LOC_FIELD_GUNS
2183+
&& weapon.getType().hasFlag(WeaponType.F_INFANTRY)
2184+
)
2185+
)
2186+
) {
2187+
return Messages.getString("WeaponAttackAction.FieldGunOrSAOnly");
21862188
}
21872189
}
21882190

0 commit comments

Comments
 (0)