From dde30ed9a2e981d9250babe779dd23d1bb02a9da Mon Sep 17 00:00:00 2001 From: Simon Date: Fri, 18 Oct 2024 18:48:17 +0200 Subject: [PATCH] prevent getPosition() on INarcPods; use BrushOffAttackAction --- .../client/ui/swing/TargetChoiceDialog.java | 19 +++++++++++++++---- .../common/actions/WeaponAttackAction.java | 5 ++++- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/megamek/src/megamek/client/ui/swing/TargetChoiceDialog.java b/megamek/src/megamek/client/ui/swing/TargetChoiceDialog.java index 35be8bc9a87..16abb7394d3 100644 --- a/megamek/src/megamek/client/ui/swing/TargetChoiceDialog.java +++ b/megamek/src/megamek/client/ui/swing/TargetChoiceDialog.java @@ -17,6 +17,7 @@ import megamek.client.ui.swing.tooltip.UnitToolTip; import megamek.client.ui.swing.util.UIUtil; import megamek.common.*; +import megamek.common.actions.BrushOffAttackAction; import megamek.common.actions.WeaponAttackAction; import megamek.common.annotations.Nullable; @@ -90,15 +91,25 @@ protected String infoText(Targetable target) { String result = ""; if (firingEntity != null) { - ToHitData thd = WeaponAttackAction.toHit(clientGUI.getClient().getGame(), firingEntity.getId(), target); - thd.setLocation(target.getPosition()); - thd.setRange(firingEntity.getPosition().distance(target.getPosition())); + ToHitData thd; + if (target instanceof INarcPod) { + // must not call getPosition() on INarcPods! Check both arms if necessary + thd = BrushOffAttackAction.toHit(clientGUI.getClient().getGame(), + firingEntity.getId(), target, BrushOffAttackAction.RIGHT); + if (thd.getValue() == TargetRoll.IMPOSSIBLE) { + thd = BrushOffAttackAction.toHit(clientGUI.getClient().getGame(), + firingEntity.getId(), target, BrushOffAttackAction.LEFT); + } + } else { + thd = WeaponAttackAction.toHit(clientGUI.getClient().getGame(), firingEntity.getId(), target); + thd.setLocation(target.getPosition()); + thd.setRange(firingEntity.getPosition().distance(target.getPosition())); + } if (thd.needsRoll()) { int mod = thd.getValue(); result += "
Target To Hit Mod: " + (mod < 0 ? "" : "+") + mod + ""; } else { result += "
" + thd.getValueAsString() + " To Hit: " + thd.getDesc(); - } } diff --git a/megamek/src/megamek/common/actions/WeaponAttackAction.java b/megamek/src/megamek/common/actions/WeaponAttackAction.java index e447ef9229b..e249bbec0d4 100644 --- a/megamek/src/megamek/common/actions/WeaponAttackAction.java +++ b/megamek/src/megamek/common/actions/WeaponAttackAction.java @@ -855,7 +855,10 @@ public static ToHitData toHit(Game game, int attackerId, Targetable target) { int aElev = ae.getElevation(); int tElev = target.getElevation(); int targEl; - if (te == null) { + if (target instanceof INarcPod) { + // brush off attached INarcs on oneself; use left arm as a placeholder here as no choice has been made + return BrushOffAttackAction.toHit(game, attackerId, target, BrushOffAttackAction.LEFT); + } else if (te == null) { targEl = game.getBoard().getHex(target.getPosition()).floor(); } else { targEl = te.relHeight();