Skip to content

Commit

Permalink
refactor: Use stream composition for firing ProtoMech main/arm guns
Browse files Browse the repository at this point in the history
  • Loading branch information
Saklad5 committed Aug 24, 2024
1 parent d16dce5 commit 332a3fd
Showing 1 changed file with 20 additions and 13 deletions.
33 changes: 20 additions & 13 deletions megamek/src/megamek/common/actions/WeaponAttackAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -2437,21 +2437,28 @@ private static String toHitIsImpossible(Game game, Entity ae, int attackerId, Ta
}

// Protomechs cannot fire arm weapons and main gun in the same turn
if ((ae instanceof Protomech)
&& ((weapon.getLocation() == Protomech.LOC_MAINGUN)
|| (weapon.getLocation() == Protomech.LOC_RARM)
|| (weapon.getLocation() == Protomech.LOC_LARM))) {
final boolean firingMainGun = weapon.getLocation() == Protomech.LOC_MAINGUN;
for (EntityAction ea : game.getActionsVector()) {
if ((ea.getEntityId() == attackerId) && (ea instanceof WeaponAttackAction)) {
WeaponAttackAction otherWAA = (WeaponAttackAction) ea;
final Mounted otherWeapon = ae.getEquipment(otherWAA.getWeaponId());
if ((firingMainGun && ((otherWeapon.getLocation() == Protomech.LOC_RARM)
|| (otherWeapon.getLocation() == Protomech.LOC_LARM)))
|| !firingMainGun && (otherWeapon.getLocation() == Protomech.LOC_MAINGUN)) {
if (ae instanceof Protomech) {
// A lazy stream that evaluates to the locations of weapons already fired by the attacker.
IntStream firedWeaponLocations = game.getActionsVector().stream()
.filter(ea -> ea.getEntityId() == attackerId)
.filter(WeaponAttackAction.class::isInstance)
.map(WeaponAttackAction.class::cast)
.map(otherWAA -> ae.getEquipment(otherWAA.getWeaponId()))
.mapToInt(Mounted::getLocation);

switch (weapon.getLocation()) {
case Protomech.LOC_MAINGUN:
if (firedWeaponLocations.anyMatch(otherWeaponLocation ->
otherWeaponLocation == Protomech.LOC_LARM || otherWeaponLocation == Protomech.LOC_RARM
)) {
return Messages.getString("WeaponAttackAction.CantFireArmsAndMainGun");
}
break;
case Protomech.LOC_LARM:
case Protomech.LOC_RARM:
if (firedWeaponLocations.anyMatch(otherWeaponLocation -> otherWeaponLocation == Protomech.LOC_MAINGUN)) {
return Messages.getString("WeaponAttackAction.CantFireArmsAndMainGun");
}
}
}
}

Expand Down

0 comments on commit 332a3fd

Please sign in to comment.