From 51bffe754553edb35c875e03acce5a87c1d9d7c0 Mon Sep 17 00:00:00 2001 From: sleet01 Date: Tue, 31 Oct 2023 19:00:35 -0700 Subject: [PATCH 1/2] Fix for RAC and UAC ammo bingo state hanging game --- .../megamek/common/weapons/RACHandler.java | 69 +++++++++---------- .../common/weapons/UltraWeaponHandler.java | 39 +++++++---- 2 files changed, 55 insertions(+), 53 deletions(-) diff --git a/megamek/src/megamek/common/weapons/RACHandler.java b/megamek/src/megamek/common/weapons/RACHandler.java index dd818151179..8f4d63e61d0 100644 --- a/megamek/src/megamek/common/weapons/RACHandler.java +++ b/megamek/src/megamek/common/weapons/RACHandler.java @@ -43,7 +43,7 @@ public RACHandler(ToHitData t, WeaponAttackAction w, Game g, GameManager m) { /* * (non-Javadoc) - * + * * @see megamek.common.weapons.UltraWeaponHandler#doChecks(java.util.Vector) */ @Override @@ -51,7 +51,7 @@ protected boolean doChecks(Vector vPhaseReport) { if (doAmmoFeedProblemCheck(vPhaseReport)) { return true; } - + if (ae instanceof Infantry) { return false; } @@ -91,7 +91,7 @@ protected boolean doChecks(Vector vPhaseReport) { /* * (non-Javadoc) - * + * * @see megamek.common.weapons.WeaponHandler#useAmmo() */ @Override @@ -99,49 +99,42 @@ protected void useAmmo() { int actualShots; setDone(); checkAmmo(); - if (weapon.curMode().equals(Weapon.MODE_RAC_SIX_SHOT)) { - howManyShots = 6; - } else if (weapon.curMode().equals(Weapon.MODE_RAC_FIVE_SHOT)) { - howManyShots = 5; - } else if (weapon.curMode().equals(Weapon.MODE_RAC_FOUR_SHOT)) { - howManyShots = 4; - } else if (weapon.curMode().equals(Weapon.MODE_RAC_THREE_SHOT)) { - howManyShots = 3; - } else if (weapon.curMode().equals(Weapon.MODE_RAC_TWO_SHOT)) { - howManyShots = 2; - } else if (weapon.curMode().equals(Weapon.MODE_AC_SINGLE)) { - howManyShots = 1; + + switch (weapon.curMode().toString()){ + case Weapon.MODE_RAC_SIX_SHOT: howManyShots = 6; + break; + case Weapon.MODE_RAC_FIVE_SHOT: howManyShots = 5; + break; + case Weapon.MODE_RAC_FOUR_SHOT: howManyShots = 4; + break; + case Weapon.MODE_RAC_THREE_SHOT: howManyShots = 3; + break; + case Weapon.MODE_RAC_TWO_SHOT: howManyShots = 2; + break; + case Weapon.MODE_AC_SINGLE: howManyShots = 1; + break; } + + // Reduce number of allowed shots to number of remaining rounds of ammo if applicable int total = ae.getTotalAmmoOfType(ammo.getType()); - if (total >= 6) { - actualShots = 6; - } else if (total >= 5) { - actualShots = 5; - } else if (total >= 3) { - actualShots = 3; - } else if (total >= 2) { - actualShots = 2; + if (total < 0 ) { + throw new RuntimeException("Invalid total ammo value < 0!"); + } else if (total < 6) { + actualShots = total; } else { - actualShots = 1; + actualShots = 6; } if (actualShots < howManyShots) { howManyShots = actualShots; } + int shotsNeedFiring = howManyShots; - if (ammo.getUsableShotsLeft() == 0) { - ae.loadWeapon(weapon); - ammo = weapon.getLinked(); - // there will be some ammo somewhere, otherwise shot will not have - // been fired. - } - while (shotsNeedFiring > ammo.getUsableShotsLeft()) { - shotsNeedFiring -= ammo.getBaseShotsLeft(); - ammo.setShotsLeft(0); - ae.loadWeapon(weapon); - ammo = weapon.getLinked(); - } - ammo.setShotsLeft(ammo.getBaseShotsLeft() - shotsNeedFiring); + // Try to reload if the linked bin is empty but another exists + attemptToReloadWeapon(); + + // Reduce linked ammo bin; if it runs out, switch to another. + reduceShotsLeft(shotsNeedFiring); } @Override @@ -153,5 +146,5 @@ protected boolean usesClusterTable() { protected int calcnClusterAero(Entity entityTarget) { return 5; } - + } diff --git a/megamek/src/megamek/common/weapons/UltraWeaponHandler.java b/megamek/src/megamek/common/weapons/UltraWeaponHandler.java index 1638d8a839d..840014aebc4 100644 --- a/megamek/src/megamek/common/weapons/UltraWeaponHandler.java +++ b/megamek/src/megamek/common/weapons/UltraWeaponHandler.java @@ -56,40 +56,49 @@ public UltraWeaponHandler(ToHitData t, WeaponAttackAction w, Game g, GameManager /* * (non-Javadoc) - * + * * @see megamek.common.weapons.WeaponHandler#addHeatUseAmmo() */ @Override protected void useAmmo() { setDone(); checkAmmo(); + howManyShots = (weapon.curMode().equals(Weapon.MODE_AC_SINGLE) ? 1 : 2); int total = ae.getTotalAmmoOfType(ammo.getType()); - if ((total > 1) && !weapon.curMode().equals(Weapon.MODE_AC_SINGLE)) { - howManyShots = 2; - } else { + if (total > 1 ) { + // No need to change howManyShots + } else if (total == 1) { howManyShots = 1; + } else { + howManyShots = 0; } + // Handle bins that are empty or will be emptied by this attack + attemptToReloadWeapon(); + reduceShotsLeft(howManyShots); + } + + protected void attemptToReloadWeapon() { + // We _may_ be able to reload from another ammo source, but in case + // a previous attack burned through all the ammo, this attack may be SOL. if (ammo.getUsableShotsLeft() == 0) { ae.loadWeapon(weapon); ammo = weapon.getLinked(); - // there will be some ammo somewhere, otherwise shot will not have been fired. } + } - if (ammo.getUsableShotsLeft() == 1) { + protected void reduceShotsLeft(int shotsNeedFiring) { + while (shotsNeedFiring > ammo.getUsableShotsLeft()) { + shotsNeedFiring -= ammo.getBaseShotsLeft(); ammo.setShotsLeft(0); - ae.loadWeapon(weapon); - ammo = weapon.getLinked(); - // that fired one, do we need to fire another? - ammo.setShotsLeft(ammo.getBaseShotsLeft() - ((howManyShots == 2) ? 1 : 0)); - } else { - ammo.setShotsLeft(ammo.getBaseShotsLeft() - howManyShots); + attemptToReloadWeapon(); } + ammo.setShotsLeft(ammo.getBaseShotsLeft() - shotsNeedFiring); } /* * (non-Javadoc) - * + * * @see megamek.common.weapons.WeaponHandler#calcHits(java.util.Vector) */ @Override @@ -138,7 +147,7 @@ protected int calcHits(Vector vPhaseReport) { /* * (non-Javadoc) - * + * * @see megamek.common.weapons.WeaponHandler#doChecks(java.util.Vector) */ @Override @@ -165,7 +174,7 @@ protected boolean doChecks(Vector vPhaseReport) { /* * (non-Javadoc) - * + * * @see megamek.common.weapons.WeaponHandler#calcDamagePerHit() */ @Override From 6986af6ba39b6abe6a75d294b5d87e2da11832c7 Mon Sep 17 00:00:00 2001 From: Sleet01 Date: Sat, 4 Nov 2023 10:50:59 -0700 Subject: [PATCH 2/2] Update RACHandler.java Fix formatting on switch/case --- megamek/src/megamek/common/weapons/RACHandler.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/megamek/src/megamek/common/weapons/RACHandler.java b/megamek/src/megamek/common/weapons/RACHandler.java index 8f4d63e61d0..39dc819b583 100644 --- a/megamek/src/megamek/common/weapons/RACHandler.java +++ b/megamek/src/megamek/common/weapons/RACHandler.java @@ -100,7 +100,7 @@ protected void useAmmo() { setDone(); checkAmmo(); - switch (weapon.curMode().toString()){ + switch (weapon.curMode().toString()) { case Weapon.MODE_RAC_SIX_SHOT: howManyShots = 6; break; case Weapon.MODE_RAC_FIVE_SHOT: howManyShots = 5;