Skip to content

Commit

Permalink
refactor: Use stream composition for firing field guns
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Saklad5 committed Aug 24, 2024
1 parent e2bacbf commit c3b03f6
Showing 1 changed file with 17 additions and 15 deletions.
32 changes: 17 additions & 15 deletions megamek/src/megamek/common/actions/WeaponAttackAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -2168,21 +2168,23 @@ private static String toHitIsImpossible(Game game, Entity ae, int attackerId, Ta
return Messages.getString("WeaponAttackAction.CantMoveAndFieldGun");
}
// check for mixing infantry and field gun attacks
for (Enumeration<EntityAction> i = game.getActions(); i.hasMoreElements();) {
EntityAction ea = i.nextElement();
if (!(ea instanceof WeaponAttackAction)) {
continue;
}
final WeaponAttackAction prevAttack = (WeaponAttackAction) ea;
if (prevAttack.getEntityId() == attackerId) {
Mounted prevWeapon = ae.getEquipment(prevAttack.getWeaponId());
if ((prevWeapon.getType().hasFlag(WeaponType.F_INFANTRY)
&& (weapon.getLocation() == Infantry.LOC_FIELD_GUNS))
|| (weapon.getType().hasFlag(WeaponType.F_INFANTRY)
&& (prevWeapon.getLocation() == Infantry.LOC_FIELD_GUNS))) {
return Messages.getString("WeaponAttackAction.FieldGunOrSAOnly");
}
}
if (game.getActionsVector().stream()
.filter(WeaponAttackAction.class::isInstance)
.map(WeaponAttackAction.class::cast)
.filter(prevAttack -> prevAttack.getEntityId() == attackerId)
.map(prevAttack -> ae.getEquipment(prevAttack.getWeaponId()))
.anyMatch(prevWeapon ->
(
prevWeapon.getType().hasFlag(WeaponType.F_INFANTRY)
&& weapon.getLocation() == Infantry.LOC_FIELD_GUNS
)
|| (
prevWeapon.getLocation() == Infantry.LOC_FIELD_GUNS
&& weapon.getType().hasFlag(WeaponType.F_INFANTRY)
)
)
) {
return Messages.getString("WeaponAttackAction.FieldGunOrSAOnly");
}
}

Expand Down

0 comments on commit c3b03f6

Please sign in to comment.