diff --git a/MekHQ/src/mekhq/campaign/parts/equipment/AmmoBin.java b/MekHQ/src/mekhq/campaign/parts/equipment/AmmoBin.java index 404f124af98..de2ad0c16f9 100644 --- a/MekHQ/src/mekhq/campaign/parts/equipment/AmmoBin.java +++ b/MekHQ/src/mekhq/campaign/parts/equipment/AmmoBin.java @@ -53,11 +53,13 @@ import megamek.common.equipment.AmmoType; import megamek.common.equipment.EquipmentType; import megamek.common.equipment.Mounted; +import megamek.common.equipment.WeaponMounted; import megamek.common.rolls.TargetRoll; import megamek.common.units.Aero; import megamek.common.units.Jumpship; import megamek.common.units.ProtoMek; import megamek.common.units.SmallCraft; +import megamek.common.weapons.Weapon; import megamek.logging.MMLogger; import mekhq.campaign.Campaign; import mekhq.campaign.finances.Money; @@ -312,6 +314,13 @@ public void loadBin() { mounted.setShotsLeft(shots); } + // Reset fired, mainly for One-Shot weapons + try { + mounted.getLinkedBy().setFired(false); + } catch (Exception ignored) { + LOGGER.error("Unable to reset fired state for mounted {}", mounted); + } + shotsNeeded -= shots; } diff --git a/MekHQ/unittests/mekhq/campaign/parts/equipment/AmmoBinTest.java b/MekHQ/unittests/mekhq/campaign/parts/equipment/AmmoBinTest.java index 59039633d58..a964ac337d8 100644 --- a/MekHQ/unittests/mekhq/campaign/parts/equipment/AmmoBinTest.java +++ b/MekHQ/unittests/mekhq/campaign/parts/equipment/AmmoBinTest.java @@ -58,7 +58,10 @@ import megamek.Version; import megamek.common.equipment.AmmoMounted; import megamek.common.equipment.AmmoType; +import megamek.common.equipment.EquipmentType; import megamek.common.equipment.Mounted; +import megamek.common.equipment.WeaponMounted; +import megamek.common.equipment.WeaponType; import megamek.common.units.Entity; import megamek.common.units.ProtoMek; import mekhq.campaign.Campaign; @@ -1177,6 +1180,60 @@ public void loadFullBinAfterChangingAmmoType() { assertEquals(ammoType.getShots(), quartermaster.getAmmoAvailable(ammoType)); } + @Test + public void loadBinForOneShotResetsFiredState() { + Campaign mockCampaign = mock(Campaign.class); + CampaignOptions mockCampaignOptions = mock(CampaignOptions.class); + when(mockCampaign.getCampaignOptions()).thenReturn(mockCampaignOptions); + Warehouse warehouse = new Warehouse(); + when(mockCampaign.getWarehouse()).thenReturn(warehouse); + Quartermaster quartermaster = new Quartermaster(mockCampaign); + when(mockCampaign.getQuartermaster()).thenReturn(quartermaster); + + AmmoType ammoType = getAmmoType("IS Ammo RL-10"); + + // Create an Ammo Bin with no ammo ... + int shotsNeeded = ammoType.getShots(); + int equipmentNum = 42; + AmmoBin ammoBin = new AmmoBin(0, ammoType, equipmentNum, shotsNeeded, true, false, mockCampaign); + + // ... place the ammo bin on a unit ... + Unit mockUnit = mock(Unit.class); + Entity mockEntity = mock(Entity.class); + when(mockUnit.getEntity()).thenReturn(mockEntity); + AmmoMounted mockMounted = mock(AmmoMounted.class); + when(mockMounted.getType()).thenReturn(ammoType); + when(mockMounted.getBaseShotsLeft()).thenReturn(0); + when(mockEntity.getEquipment(eq(equipmentNum))).thenReturn((Mounted) mockMounted); + ammoBin.setUnit(mockUnit); + + + // ...Set up the linking weapon... + WeaponType launcherType = (WeaponType) EquipmentUtilities.getEquipmentType("RL 10"); + WeaponMounted launcher = new WeaponMounted(mockEntity, launcherType); + launcher.setLinked(mockMounted); + when(mockMounted.getLinkedBy()).thenReturn((Mounted) launcher); + + // ...set the linking weapon as having fired... + launcher.setFired(true); + + // ... and add just enough ammo of the right type to the warehouse ... + quartermaster.addAmmo(ammoType, ammoType.getShots()); + + // ... and try to load it. + ammoBin.loadBin(); + + // The launcher should now no longer be in the "fired" state. + assertFalse(launcher.isFired()); + + // We should have no shots needed ... + assertEquals(0, ammoBin.getShotsNeeded()); + verify(mockMounted, times(1)).setShotsLeft(eq(shotsNeeded)); + + // ... and no more ammo available in the warehouse + assertEquals(0, quartermaster.getAmmoAvailable(ammoType)); + } + @Test public void fixBinWithoutUnitDoesNothing() { Campaign mockCampaign = mock(Campaign.class);