Skip to content

Commit 332a3fd

Browse files
committed
refactor: Use stream composition for firing ProtoMech main/arm guns
1 parent d16dce5 commit 332a3fd

File tree

1 file changed

+20
-13
lines changed

1 file changed

+20
-13
lines changed

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

+20-13
Original file line numberDiff line numberDiff line change
@@ -2437,21 +2437,28 @@ private static String toHitIsImpossible(Game game, Entity ae, int attackerId, Ta
24372437
}
24382438

24392439
// Protomechs cannot fire arm weapons and main gun in the same turn
2440-
if ((ae instanceof Protomech)
2441-
&& ((weapon.getLocation() == Protomech.LOC_MAINGUN)
2442-
|| (weapon.getLocation() == Protomech.LOC_RARM)
2443-
|| (weapon.getLocation() == Protomech.LOC_LARM))) {
2444-
final boolean firingMainGun = weapon.getLocation() == Protomech.LOC_MAINGUN;
2445-
for (EntityAction ea : game.getActionsVector()) {
2446-
if ((ea.getEntityId() == attackerId) && (ea instanceof WeaponAttackAction)) {
2447-
WeaponAttackAction otherWAA = (WeaponAttackAction) ea;
2448-
final Mounted otherWeapon = ae.getEquipment(otherWAA.getWeaponId());
2449-
if ((firingMainGun && ((otherWeapon.getLocation() == Protomech.LOC_RARM)
2450-
|| (otherWeapon.getLocation() == Protomech.LOC_LARM)))
2451-
|| !firingMainGun && (otherWeapon.getLocation() == Protomech.LOC_MAINGUN)) {
2440+
if (ae instanceof Protomech) {
2441+
// A lazy stream that evaluates to the locations of weapons already fired by the attacker.
2442+
IntStream firedWeaponLocations = game.getActionsVector().stream()
2443+
.filter(ea -> ea.getEntityId() == attackerId)
2444+
.filter(WeaponAttackAction.class::isInstance)
2445+
.map(WeaponAttackAction.class::cast)
2446+
.map(otherWAA -> ae.getEquipment(otherWAA.getWeaponId()))
2447+
.mapToInt(Mounted::getLocation);
2448+
2449+
switch (weapon.getLocation()) {
2450+
case Protomech.LOC_MAINGUN:
2451+
if (firedWeaponLocations.anyMatch(otherWeaponLocation ->
2452+
otherWeaponLocation == Protomech.LOC_LARM || otherWeaponLocation == Protomech.LOC_RARM
2453+
)) {
2454+
return Messages.getString("WeaponAttackAction.CantFireArmsAndMainGun");
2455+
}
2456+
break;
2457+
case Protomech.LOC_LARM:
2458+
case Protomech.LOC_RARM:
2459+
if (firedWeaponLocations.anyMatch(otherWeaponLocation -> otherWeaponLocation == Protomech.LOC_MAINGUN)) {
24522460
return Messages.getString("WeaponAttackAction.CantFireArmsAndMainGun");
24532461
}
2454-
}
24552462
}
24562463
}
24572464

0 commit comments

Comments
 (0)