Skip to content

Commit 891a17d

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

File tree

1 file changed

+21
-13
lines changed

1 file changed

+21
-13
lines changed

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

+21-13
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import java.io.Serializable;
4242
import java.util.*;
4343
import java.util.stream.Stream;
44+
import java.util.stream.IntStream;
4445
import java.util.stream.Collectors;
4546

4647
/**
@@ -2437,21 +2438,28 @@ private static String toHitIsImpossible(Game game, Entity ae, int attackerId, Ta
24372438
}
24382439

24392440
// 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)) {
2441+
if (ae instanceof Protomech) {
2442+
// A lazy stream that evaluates to the locations of weapons already fired by the attacker.
2443+
IntStream firedWeaponLocations = game.getActionsVector().stream()
2444+
.filter(ea -> ea.getEntityId() == attackerId)
2445+
.filter(WeaponAttackAction.class::isInstance)
2446+
.map(WeaponAttackAction.class::cast)
2447+
.map(otherWAA -> ae.getEquipment(otherWAA.getWeaponId()))
2448+
.mapToInt(Mounted::getLocation);
2449+
2450+
switch (weapon.getLocation()) {
2451+
case Protomech.LOC_MAINGUN:
2452+
if (firedWeaponLocations.anyMatch(otherWeaponLocation ->
2453+
otherWeaponLocation == Protomech.LOC_LARM || otherWeaponLocation == Protomech.LOC_RARM
2454+
)) {
2455+
return Messages.getString("WeaponAttackAction.CantFireArmsAndMainGun");
2456+
}
2457+
break;
2458+
case Protomech.LOC_LARM:
2459+
case Protomech.LOC_RARM:
2460+
if (firedWeaponLocations.anyMatch(otherWeaponLocation -> otherWeaponLocation == Protomech.LOC_MAINGUN)) {
24522461
return Messages.getString("WeaponAttackAction.CantFireArmsAndMainGun");
24532462
}
2454-
}
24552463
}
24562464
}
24572465

0 commit comments

Comments
 (0)