From 725d527980c2b9f7143a112ca9707fc6620a68d9 Mon Sep 17 00:00:00 2001 From: psikomonkie <189469115+psikomonkie@users.noreply.github.com> Date: Sun, 8 Dec 2024 18:23:49 -0500 Subject: [PATCH 1/6] Issue 6247:NPE with TacOps Energy Weapons when Changing Firing Mode --- megamek/src/megamek/common/Compute.java | 2 +- .../common/equipment/WeaponMounted.java | 2 +- .../weapons/PulseLaserWeaponHandler.java | 2 +- .../common/weapons/lasers/LaserWeapon.java | 38 +++++++++++++------ 4 files changed, 30 insertions(+), 14 deletions(-) diff --git a/megamek/src/megamek/common/Compute.java b/megamek/src/megamek/common/Compute.java index 03f1fef53a2..a7ad5fd44af 100644 --- a/megamek/src/megamek/common/Compute.java +++ b/megamek/src/megamek/common/Compute.java @@ -7248,7 +7248,7 @@ public static boolean allowAimedShotWith(WeaponMounted weapon, AimingMode aiming case TARGETING_COMPUTER: if (!wtype.hasFlag(WeaponType.F_DIRECT_FIRE) || wtype.hasFlag(WeaponType.F_PULSE) - || weapon.curMode().getName().equals("Pulse") + || weapon.curMode().getName().startsWith("Pulse") || (wtype instanceof HAGWeapon)) { return false; } diff --git a/megamek/src/megamek/common/equipment/WeaponMounted.java b/megamek/src/megamek/common/equipment/WeaponMounted.java index 390b0e9aaf3..5b5102d9bca 100644 --- a/megamek/src/megamek/common/equipment/WeaponMounted.java +++ b/megamek/src/megamek/common/equipment/WeaponMounted.java @@ -131,7 +131,7 @@ && getLinkedBy().getType().hasFlag( heat++; } } - if (curMode().equals("Pulse")) { + if (curMode().getName().startsWith("Pulse")) { heat += 2; } diff --git a/megamek/src/megamek/common/weapons/PulseLaserWeaponHandler.java b/megamek/src/megamek/common/weapons/PulseLaserWeaponHandler.java index 07ce462ee3b..3cd02182a6d 100644 --- a/megamek/src/megamek/common/weapons/PulseLaserWeaponHandler.java +++ b/megamek/src/megamek/common/weapons/PulseLaserWeaponHandler.java @@ -42,7 +42,7 @@ protected boolean doChecks(Vector vPhaseReport) { WeaponMounted laser = waa.getEntity(game).getWeapon(waa.getWeaponId()); - if ((roll.getIntValue() == 2) && laser.curMode().equals("Pulse")) { + if ((roll.getIntValue() == 2) && laser.curMode().getName().startsWith("Pulse")) { vPhaseReport.addAll(gameManager.explodeEquipment(laser.getEntity(), laser.getLocation(), laser.getLinkedBy())); } return false; diff --git a/megamek/src/megamek/common/weapons/lasers/LaserWeapon.java b/megamek/src/megamek/common/weapons/lasers/LaserWeapon.java index b58bfd66a4f..62ed00703c7 100644 --- a/megamek/src/megamek/common/weapons/lasers/LaserWeapon.java +++ b/megamek/src/megamek/common/weapons/lasers/LaserWeapon.java @@ -13,11 +13,7 @@ */ package megamek.common.weapons.lasers; -import megamek.common.AmmoType; -import megamek.common.Game; -import megamek.common.MiscType; -import megamek.common.Mounted; -import megamek.common.ToHitData; +import megamek.common.*; import megamek.common.actions.WeaponAttackAction; import megamek.common.annotations.Nullable; import megamek.common.equipment.MiscMounted; @@ -29,6 +25,8 @@ import megamek.common.weapons.PulseLaserWeaponHandler; import megamek.server.totalwarfare.TWGameManager; +import java.util.Collections; + /** * @author Andrew Hunter * @since Sep 2, 2004 @@ -49,8 +47,25 @@ public void adaptToGameOptions(GameOptions gOp) { super.adaptToGameOptions(gOp); if (!(this instanceof PulseLaserWeapon)) { - addMode(""); - addMode("Pulse"); + if (!(gOp.booleanOption(OptionsConstants.ADVCOMBAT_TACOPS_ENERGY_WEAPONS))) { + for (EquipmentMode mode : Collections.list(getModes())){ + if(mode.getName().startsWith("Pulse Damage ")){ + removeMode(mode.getName()); + } + } + addMode(""); + addMode("Pulse"); + } + else{ + removeMode(""); + removeMode("Pulse"); + for (EquipmentMode mode : Collections.list(getModes())){ + if(mode.getName().startsWith("Damage ")){ + addMode("Pulse " + mode.getName()); + } + } + } + } } @@ -60,10 +75,11 @@ public int getModesCount(Mounted mounted) { if ((linkedBy instanceof MiscMounted) && !linkedBy.isInoperable() && ((MiscMounted) linkedBy).getType().hasFlag(MiscType.F_RISC_LASER_PULSE_MODULE)) { - return 2; - } else { - return 0; + return super.getModesCount(); + } else if(this instanceof PulseLaserWeapon) { + return super.getModesCount(); } + return super.getModesCount() / 2; } @Override @@ -71,7 +87,7 @@ public int getToHitModifier(@Nullable Mounted mounted) { if ((mounted == null) || !(mounted.getLinkedBy() instanceof MiscMounted) || !mounted.getLinkedBy().getType().hasFlag(MiscType.F_RISC_LASER_PULSE_MODULE)) { return super.getToHitModifier(mounted); } - return mounted.curMode().getName().equals("Pulse") ? -2 : 0; + return mounted.curMode().getName().startsWith("Pulse") ? -2 : 0; } @Override From 3f10043ef4871bc39d566f91a9167d19d686bd74 Mon Sep 17 00:00:00 2001 From: psikomonkie <189469115+psikomonkie@users.noreply.github.com> Date: Sun, 8 Dec 2024 20:11:29 -0500 Subject: [PATCH 2/6] Issue 6247:NPE with TacOps Energy Weapons when Changing Firing Mode --- megamek/src/megamek/common/Compute.java | 7 +++---- .../common/weapons/lasers/EnergyWeapon.java | 2 ++ .../common/weapons/lasers/LaserWeapon.java | 20 +++++++------------ 3 files changed, 12 insertions(+), 17 deletions(-) diff --git a/megamek/src/megamek/common/Compute.java b/megamek/src/megamek/common/Compute.java index a7ad5fd44af..c34b95b85cc 100644 --- a/megamek/src/megamek/common/Compute.java +++ b/megamek/src/megamek/common/Compute.java @@ -6910,13 +6910,12 @@ public static int dialDownDamage(Mounted weapon, WeaponType wtype, int range) return toReturn; } - String damage = weapon.curMode().getName(); + String damage = weapon.curMode().getName().toLowerCase(); // Vehicle flamers have damage and heat modes so lets make sure this is // an actual dial down Damage. - if ((damage.trim().toLowerCase().indexOf("damage") == 0) - && (damage.trim().length() > 6)) { - toReturn = Integer.parseInt(damage.substring(6).trim()); + if ((damage.trim().length() > 6) && damage.contains("damage")) { + toReturn = Integer.parseInt(damage.substring(damage.indexOf("damage")+6).trim()); } return Math.min(wtype.getDamage(range), toReturn); diff --git a/megamek/src/megamek/common/weapons/lasers/EnergyWeapon.java b/megamek/src/megamek/common/weapons/lasers/EnergyWeapon.java index df4052d447c..f0977908dc5 100644 --- a/megamek/src/megamek/common/weapons/lasers/EnergyWeapon.java +++ b/megamek/src/megamek/common/weapons/lasers/EnergyWeapon.java @@ -55,10 +55,12 @@ public void adaptToGameOptions(GameOptions gOp) { for (; dmg >= 0; dmg--) { addMode("Damage " + dmg); } + removeMode(""); } else { int dmg = (damage == WeaponType.DAMAGE_VARIABLE) ? damageShort : damage; for (; dmg >= 0; dmg--) { removeMode("Damage " + dmg); + removeMode("Pulse Damage" + dmg); } } } diff --git a/megamek/src/megamek/common/weapons/lasers/LaserWeapon.java b/megamek/src/megamek/common/weapons/lasers/LaserWeapon.java index 62ed00703c7..d6dd1ca8c48 100644 --- a/megamek/src/megamek/common/weapons/lasers/LaserWeapon.java +++ b/megamek/src/megamek/common/weapons/lasers/LaserWeapon.java @@ -47,25 +47,17 @@ public void adaptToGameOptions(GameOptions gOp) { super.adaptToGameOptions(gOp); if (!(this instanceof PulseLaserWeapon)) { - if (!(gOp.booleanOption(OptionsConstants.ADVCOMBAT_TACOPS_ENERGY_WEAPONS))) { - for (EquipmentMode mode : Collections.list(getModes())){ - if(mode.getName().startsWith("Pulse Damage ")){ - removeMode(mode.getName()); - } - } + if (modes.isEmpty()) { addMode(""); addMode("Pulse"); } - else{ - removeMode(""); - removeMode("Pulse"); - for (EquipmentMode mode : Collections.list(getModes())){ - if(mode.getName().startsWith("Damage ")){ + else { + for (var mode : Collections.list(getModes())) { + if(!mode.getName().contains("Pulse")) { addMode("Pulse " + mode.getName()); } } } - } } @@ -79,7 +71,9 @@ public int getModesCount(Mounted mounted) { } else if(this instanceof PulseLaserWeapon) { return super.getModesCount(); } - return super.getModesCount() / 2; + + //Only works if laser pulse module's "Pulse" modes are added last. + return (int) modes.stream().filter(mode -> !mode.getName().startsWith("Pulse")).count(); } @Override From bc7319a58b9fe72ce72e52619b0feb0b76fdbac1 Mon Sep 17 00:00:00 2001 From: psikomonkie <189469115+psikomonkie@users.noreply.github.com> Date: Sun, 8 Dec 2024 20:16:57 -0500 Subject: [PATCH 3/6] Issue 6247:NPE with TacOps Energy Weapons when Changing Firing Mode --- megamek/src/megamek/common/weapons/lasers/EnergyWeapon.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/megamek/src/megamek/common/weapons/lasers/EnergyWeapon.java b/megamek/src/megamek/common/weapons/lasers/EnergyWeapon.java index f0977908dc5..b92ac56073b 100644 --- a/megamek/src/megamek/common/weapons/lasers/EnergyWeapon.java +++ b/megamek/src/megamek/common/weapons/lasers/EnergyWeapon.java @@ -56,11 +56,12 @@ public void adaptToGameOptions(GameOptions gOp) { addMode("Damage " + dmg); } removeMode(""); + removeMode("Pulse"); } else { int dmg = (damage == WeaponType.DAMAGE_VARIABLE) ? damageShort : damage; for (; dmg >= 0; dmg--) { removeMode("Damage " + dmg); - removeMode("Pulse Damage" + dmg); + removeMode("Pulse Damage " + dmg); } } } From 1df760f9a60e430f0970593112cf45e34249c8a2 Mon Sep 17 00:00:00 2001 From: psikomonkie <189469115+psikomonkie@users.noreply.github.com> Date: Sun, 8 Dec 2024 20:29:07 -0500 Subject: [PATCH 4/6] Issue 6247:NPE with TacOps Energy Weapons when Changing Firing Mode --- megamek/src/megamek/common/weapons/lasers/LaserWeapon.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/megamek/src/megamek/common/weapons/lasers/LaserWeapon.java b/megamek/src/megamek/common/weapons/lasers/LaserWeapon.java index d6dd1ca8c48..a3898cf46b5 100644 --- a/megamek/src/megamek/common/weapons/lasers/LaserWeapon.java +++ b/megamek/src/megamek/common/weapons/lasers/LaserWeapon.java @@ -47,13 +47,13 @@ public void adaptToGameOptions(GameOptions gOp) { super.adaptToGameOptions(gOp); if (!(this instanceof PulseLaserWeapon)) { - if (modes.isEmpty()) { + if (!hasModes()) { addMode(""); addMode("Pulse"); } else { for (var mode : Collections.list(getModes())) { - if(!mode.getName().contains("Pulse")) { + if(!mode.getName().contains("Pulse") && !mode.getName().isEmpty()) { addMode("Pulse " + mode.getName()); } } From c550b395f5b79f5baf5cf39a3f59c0a7caedb1c2 Mon Sep 17 00:00:00 2001 From: psikomonkie <189469115+psikomonkie@users.noreply.github.com> Date: Sun, 8 Dec 2024 20:30:52 -0500 Subject: [PATCH 5/6] Issue 6247:NPE with TacOps Energy Weapons when Changing Firing Mode --- megamek/src/megamek/common/Compute.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/megamek/src/megamek/common/Compute.java b/megamek/src/megamek/common/Compute.java index c34b95b85cc..00548e05787 100644 --- a/megamek/src/megamek/common/Compute.java +++ b/megamek/src/megamek/common/Compute.java @@ -6915,7 +6915,7 @@ public static int dialDownDamage(Mounted weapon, WeaponType wtype, int range) // Vehicle flamers have damage and heat modes so lets make sure this is // an actual dial down Damage. if ((damage.trim().length() > 6) && damage.contains("damage")) { - toReturn = Integer.parseInt(damage.substring(damage.indexOf("damage")+6).trim()); + toReturn = Integer.parseInt(damage.substring(damage.indexOf("damage") + 6).trim()); } return Math.min(wtype.getDamage(range), toReturn); From 5fb42e0e91057db9b8e73f406ad53e365d58ad5d Mon Sep 17 00:00:00 2001 From: psikomonkie <189469115+psikomonkie@users.noreply.github.com> Date: Sun, 8 Dec 2024 21:16:07 -0500 Subject: [PATCH 6/6] Issue 6247:NPE with TacOps Energy Weapons when Changing Firing Mode --- megamek/src/megamek/common/Compute.java | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/megamek/src/megamek/common/Compute.java b/megamek/src/megamek/common/Compute.java index c95a0f74f92..da5e4a7dd39 100644 --- a/megamek/src/megamek/common/Compute.java +++ b/megamek/src/megamek/common/Compute.java @@ -6919,7 +6919,12 @@ public static int dialDownDamage(Mounted weapon, WeaponType wtype, int range) // Vehicle flamers have damage and heat modes so lets make sure this is // an actual dial down Damage. if ((damage.trim().length() > 6) && damage.contains("damage")) { - toReturn = Integer.parseInt(damage.substring(damage.indexOf("damage") + 6).trim()); + try { + toReturn = Integer.parseInt(damage.substring(damage.indexOf("damage") + 6).trim()); + } + catch (NumberFormatException e) { + logger.warn("Failed to get dialed down damage. " + e.getMessage()); + } } return Math.min(wtype.getDamage(range), toReturn);