Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix 3880 cannot refit clan mechs due to renames #3899

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions MekHQ/src/mekhq/Utilities.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import megamek.common.*;
import megamek.common.annotations.Nullable;
import megamek.common.enums.Gender;
import megamek.common.loaders.EntityLoadingException;
import megamek.common.options.IOption;
import megamek.common.options.OptionsConstants;
import mekhq.campaign.Campaign;
Expand Down Expand Up @@ -1256,4 +1257,32 @@ public static int selectBestBayFor(Entity cargo, Entity transport) {
// Shouldn't happen
return -1;
}

/**
* Testable function to get the original unit based on information from a new unit
* @param newE new Entity we want to read information from
* @return MechSummary that most closely represents the original of the new Entity
* @throws EntityLoadingException
*/
public static MechSummary retrieveOriginalUnit(Entity newE) throws EntityLoadingException {
MechSummaryCache cacheInstance = MechSummaryCache.getInstance();
cacheInstance.loadMechData();

// I need to change the new entity to the one from the mtf file now, so that equipment numbers will match
MechSummary summary = cacheInstance.getMech(newE.getFullChassis() + " " + newE.getModel());

if (null == summary) {
// Attempt to deal with new naming convention directly
summary = cacheInstance.getMech(
newE.getChassis() + " (" + newE.getClanChassisName() + ") " + newE.getModel());
}

// If we got this far with no summary loaded, give up
if (null == summary) {
throw new EntityLoadingException(String.format("Could not load %s %s from the mech cache",
newE.getChassis(), newE.getModel()));
}

return summary;
}
}
11 changes: 2 additions & 9 deletions MekHQ/src/mekhq/campaign/parts/Refit.java
Original file line number Diff line number Diff line change
Expand Up @@ -1569,15 +1569,7 @@ public void saveCustomization() throws EntityLoadingException {
getCampaign().addCustom(newEntity.getChassis() + " " + newEntity.getModel());

try {
MechSummaryCache.getInstance().loadMechData();

// I need to change the new entity to the one from the mtf file now, so that equipment numbers will match

MechSummary summary = MechSummaryCache.getInstance().getMech(newEntity.getChassis() + " " + newEntity.getModel());
if (null == summary) {
throw new EntityLoadingException(String.format("Could not load %s %s from the mech cache",
newEntity.getChassis(), newEntity.getModel()));
}
MechSummary summary = Utilities.retrieveOriginalUnit(newEntity);

newEntity = new MechFileParser(summary.getSourceFile(), summary.getEntryName()).getEntity();
LogManager.getLogger().info(String.format("Saved %s %s to %s",
Expand All @@ -1602,6 +1594,7 @@ public void saveCustomization() throws EntityLoadingException {
}
}


private int getTimeMultiplier() {
int mult;
switch (refitClass) {
Expand Down
2 changes: 1 addition & 1 deletion MekHQ/src/mekhq/gui/CampaignGUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -1581,7 +1581,7 @@ public void refitUnit(Refit r, boolean selectModelName) {
}
}

String s = (String) JOptionPane.showInputDialog(frame,
String s = (techList.isEmpty()) ? null : (String) JOptionPane.showInputDialog(frame,
"Which tech should work on the refit?", "Select Tech",
JOptionPane.PLAIN_MESSAGE, null, techList.toArray(), techList.get(0));

Expand Down
11 changes: 8 additions & 3 deletions MekHQ/src/mekhq/gui/dialog/ChooseRefitDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -259,10 +259,15 @@ private void refitTableValueChanged() {

private void populateRefits() {
List<Refit> refits = new ArrayList<>();
for (String model : Utilities.getAllVariants(unit.getEntity(), campaign)) {
MechSummary summary = MechSummaryCache.getInstance().getMech(unit.getEntity().getChassis() + " " + model);
Entity e = unit.getEntity();
for (String model : Utilities.getAllVariants(e, campaign)) {
MechSummary summary = MechSummaryCache.getInstance().getMech(e.getFullChassis() + " " + model);
if (null == summary) {
continue;
// Attempt to deal with new naming scheme directly
summary = MechSummaryCache.getInstance().getMech(e.getChassis() + " (" + e.getClanChassisName() + ") " + model);
if (null == summary) {
continue;
}
}
try {
Entity refitEn = new MechFileParser(summary.getSourceFile(), summary.getEntryName()).getEntity();
Expand Down
81 changes: 81 additions & 0 deletions MekHQ/unittests/mekhq/campaign/parts/RefitTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import megamek.common.loaders.EntityLoadingException;
import megamek.common.options.GameOptions;
import megamek.common.options.OptionsConstants;
import mekhq.campaign.parts.equipment.MissingAmmoBin;
import mekhq.utilities.MHQXMLUtility;
import mekhq.campaign.*;
import mekhq.campaign.finances.Money;
Expand All @@ -33,6 +34,7 @@
import mekhq.campaign.personnel.Person;
import mekhq.campaign.unit.Unit;
import mekhq.campaign.unit.UnitTestUtilities;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -794,4 +796,83 @@ public void heavyTrackedApcMgToStandard() throws EntityLoadingException, IOExcep
String report = refit.succeed();
assertNotNull(report);
}

@Test
public void testMasakariAtoMasakariB() {
// Create the original entity backing the unit
Entity oldEntity = UnitTestUtilities.getMasakariWarhawkA();
Player mockPlayer = mock(Player.class);
when(mockPlayer.getName()).thenReturn("Test Player");
oldEntity.setOwner(mockPlayer);

// Create the entity we're going to refit to
Entity newEntity = UnitTestUtilities.getMasakariWarhawkB();

// Create the unit which will be refit
Unit oldUnit = new Unit(oldEntity, mockCampaign);
oldUnit.setId(UUID.randomUUID());
oldUnit.initializeParts(false);

// Create the Refit
Refit refit = new Refit(oldUnit, newEntity, false, false);
assertEquals(mockCampaign, refit.getCampaign());

// Omni reconfiguration
assertEquals(Refit.CLASS_OMNI, refit.getRefitClass());

// Time?
// Omni reconfig = 120 minutes here
assertEquals(120.0, refit.getActualTime(), 0.1);

// Cost?
assertEquals(Money.of(316000).multipliedBy(1.1),
refit.getCost());

// We're removing 1 Large Laser and using existing armor in 10 locations
List<Part> removedParts = refit.getOldUnitParts();
assertEquals(9, removedParts.size());
assertEquals(2, removedParts.stream()
.filter(p -> (p instanceof EquipmentPart) && p.getName().equals("ER Large Laser"))
.count());
assertEquals(0, removedParts.stream().filter(p -> (p instanceof Armor)).count());

// All of the new parts should be from the old unit
List<Part> newParts = refit.getNewUnitParts();
assertTrue(newParts.stream().allMatch(p -> p.getUnit().equals(oldUnit)));

// We need to buy:
// - 1 clan gauss rifle with 2 ammo bins,
// - 3 ER medium lasers,
// - 1 ER small laser,
// - 2 SRM-6 launchers sharing 5 Narc-capable ammo bins,
// - 1 NARC launcher with 1 ton of pods
List<Part> shoppingCart = refit.getShoppingList();
assertEquals(1, shoppingCart.stream()
.filter(p -> (p instanceof MissingEquipmentPart) && p.getName().equals("Gauss Rifle"))
.count());
assertEquals(2, shoppingCart.stream()
.filter(p -> (p instanceof AmmoBin) && p.getName().equals("Gauss Rifle Ammo [Clan] Bin"))
.count());
assertEquals(3, shoppingCart.stream()
.filter(p -> (p instanceof MissingEquipmentPart) && p.getName().equals("ER Medium Laser"))
.count());
assertEquals(1, shoppingCart.stream()
.filter(p -> (p instanceof MissingEquipmentPart) && p.getName().equals("ER Small Laser"))
.count());
assertEquals(2, shoppingCart.stream()
.filter(p -> (p instanceof MissingEquipmentPart) && p.getName().equals("SRM 6"))
.count());
assertEquals(5, shoppingCart.stream()
.filter(p -> (p instanceof AmmoBin) && p.getName().equals("SRM 6 (Clan) Narc-capable Ammo Bin"))
.count());
assertEquals(1, shoppingCart.stream()
.filter(p -> (p instanceof MissingEquipmentPart) && p.getName().equals("Narc"))
.count());
assertEquals(1, shoppingCart.stream()
.filter(p -> (p instanceof AmmoBin) && p.getName().equals("Narc Pods Bin"))
.count());

// We should have 0 points of standard armor on order
assertNull(refit.getNewArmorSupplies());
}
}
Loading
Loading