From 12ed9477aaab7c9d99d8e02d79b1a4694cc3ac96 Mon Sep 17 00:00:00 2001 From: Jeremy Saklad Date: Sat, 24 Aug 2024 16:22:20 -0500 Subject: [PATCH] refactor: Use stream composition for sensor shadows --- .../common/actions/WeaponAttackAction.java | 35 +++++++++++-------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/megamek/src/megamek/common/actions/WeaponAttackAction.java b/megamek/src/megamek/common/actions/WeaponAttackAction.java index 5b94efd80ca..d71f1a4d9a3 100644 --- a/megamek/src/megamek/common/actions/WeaponAttackAction.java +++ b/megamek/src/megamek/common/actions/WeaponAttackAction.java @@ -4210,7 +4210,9 @@ else if ((atype != null) // Aerospace target modifiers if (te != null && te.isAero() && te.isAirborne()) { - IAero a = (IAero) te; + // Finalized for concurrency reasons + final Entity targetEntity = te; + IAero a = (IAero) targetEntity; // is the target at zero velocity if ((a.getCurrentVelocity() == 0) && !(a.isSpheroid() && !game.getBoard().inSpace())) { @@ -4234,19 +4236,24 @@ else if ((atype != null) // Target hidden in the sensor shadow of a larger spacecraft if (game.getOptions().booleanOption(OptionsConstants.ADVAERORULES_STRATOPS_SENSOR_SHADOW) && game.getBoard().inSpace()) { - for (Entity en : Compute.getAdjacentEntitiesAlongAttack(ae.getPosition(), target.getPosition(), game)) { - if (!en.isEnemyOf(te) && en.isLargeCraft() - && ((en.getWeight() - te.getWeight()) >= -STRATOPS_SENSOR_SHADOW_WEIGHT_DIFF)) { - toHit.addModifier(+1, Messages.getString("WeaponAttackAction.SensorShadow")); - break; - } - } - for (Entity en : game.getEntitiesVector(target.getPosition())) { - if (!en.isEnemyOf(te) && en.isLargeCraft() && !en.equals((Entity) a) - && ((en.getWeight() - te.getWeight()) >= -STRATOPS_SENSOR_SHADOW_WEIGHT_DIFF)) { - toHit.addModifier(+1, Messages.getString("WeaponAttackAction.SensorShadow")); - break; - } + if (Compute.getAdjacentEntitiesAlongAttack(ae.getPosition(), target.getPosition(), game).stream() + .anyMatch(en -> + !en.isEnemyOf(targetEntity) + && en.isLargeCraft() + && en.getWeight() - targetEntity.getWeight() >= -STRATOPS_SENSOR_SHADOW_WEIGHT_DIFF + ) + ) { + toHit.addModifier(+1, Messages.getString("WeaponAttackAction.SensorShadow")); + } + if (game.getEntitiesVector(target.getPosition()).stream() + .anyMatch(en -> + !en.isEnemyOf(targetEntity) + && en.isLargeCraft() + && !en.equals(targetEntity) + && en.getWeight() - targetEntity.getWeight() >= -STRATOPS_SENSOR_SHADOW_WEIGHT_DIFF + ) + ) { + toHit.addModifier(+1, Messages.getString("WeaponAttackAction.SensorShadow")); } } }