Skip to content

Commit

Permalink
refactor: Use stream composition for BA Narc targeting
Browse files Browse the repository at this point in the history
  • Loading branch information
Saklad5 committed Aug 21, 2024
1 parent 88b5b8d commit f45da84
Showing 1 changed file with 10 additions and 14 deletions.
24 changes: 10 additions & 14 deletions megamek/src/megamek/common/actions/WeaponAttackAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -1881,20 +1881,16 @@ private static String toHitIsImpossible(Game game, Entity ae, int attackerId, Ta
// BA compact narc: we have one weapon for each trooper, but you
// can fire only at one target at a time
if (wtype.getName().equals("Compact Narc")) {
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().getName().equals("Compact Narc")) {
if (prevAttack.getTargetId() != target.getId()) {
return Messages.getString("WeaponAttackAction.OneTargetForCNarc");
}
}
}
if (game.getActionsVector().stream()
.filter(WeaponAttackAction.class::isInstance)
.map(WeaponAttackAction.class::cast)
.anyMatch(prevAttack -> prevAttack.getEntityId() == attackerId
&& ae.getEquipment(prevAttack.getWeaponId())
.getType().getName().equals("Compact Narc")
&& prevAttack.getTargetId() != target.getId()
)
) {
return Messages.getString("WeaponAttackAction.OneTargetForCNarc");
}
}

Expand Down

0 comments on commit f45da84

Please sign in to comment.