Skip to content

Commit

Permalink
refactor: Use stream composition for BA AP attacks
Browse files Browse the repository at this point in the history
  • Loading branch information
Saklad5 committed Aug 21, 2024
1 parent 3ce1501 commit 88b5b8d
Showing 1 changed file with 8 additions and 13 deletions.
21 changes: 8 additions & 13 deletions megamek/src/megamek/common/actions/WeaponAttackAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -1866,20 +1866,15 @@ private static String toHitIsImpossible(Game game, Entity ae, int attackerId, Ta
if ((ae instanceof BattleArmor) && wtype.hasFlag(WeaponType.F_INFANTRY)) {
final int weapId = ae.getEquipmentNum(weapon);
// See if this unit has made a previous AP attack
for (Enumeration<EntityAction> i = game.getActions(); i.hasMoreElements();) {
Object o = i.nextElement();
if (!(o instanceof WeaponAttackAction)) {
continue;
}
WeaponAttackAction prevAttack = (WeaponAttackAction) o;
if (game.getActionsVector().stream()
.filter(WeaponAttackAction.class::isInstance)
.map(WeaponAttackAction.class::cast)
// Is this an attack from this entity
if (prevAttack.getEntityId() == ae.getId()) {
Mounted prevWeapon = ae.getEquipment(prevAttack.getWeaponId());
WeaponType prevWtype = (WeaponType) prevWeapon.getType();
if (prevWtype.hasFlag(WeaponType.F_INFANTRY) && (prevAttack.getWeaponId() != weapId)) {
return Messages.getString("WeaponAttackAction.OnlyOneBAAPAttack");
}
}
.filter(prevAttack -> prevAttack.getEntityId() == ae.getId())
.map(WeaponAttackAction::getWeaponId)
.anyMatch(prevAttackId -> prevAttackId != weapId && ae.getEquipment(prevAttackId).getType().hasFlag(WeaponType.F_INFANTRY))
) {
return Messages.getString("WeaponAttackAction.OnlyOneBAAPAttack");
}
}

Expand Down

0 comments on commit 88b5b8d

Please sign in to comment.