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

Added Unit Quality Parameter to addNewUnit Method and Related Tests #4150

Merged
merged 4 commits into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
20 changes: 16 additions & 4 deletions MekHQ/src/mekhq/campaign/Campaign.java
Original file line number Diff line number Diff line change
Expand Up @@ -672,7 +672,7 @@ public void purchaseShipSearchResult() {
: calculatePartTransitTime(Compute.d6(2) - 2);

getFinances().debit(TransactionType.UNIT_PURCHASE, getLocalDate(), cost, "Purchased " + en.getShortName());
addNewUnit(en, true, transitDays);
addNewUnit(en, true, transitDays, 3);
if (!getCampaignOptions().isInstantUnitMarketDelivery()) {
addReport("<font color='green'>Unit will be delivered in " + transitDays + " days.</font>");
}
Expand Down Expand Up @@ -1155,11 +1155,21 @@ public void addTestUnit(TestUnit tu) {
}

/**
* Add a new unit to the campaign.
* Add a new unit to the campaign and set its quality.
* Quality 3 (D) is suitable for most uses.
*
* @param en An <code>Entity</code> object that the new unit will be wrapped around
* @param en An <code>Entity</code> object that the new unit will be wrapped around
* @param allowNewPilots A boolean indicating whether to add new pilots for the unit
* @param days The number of days for the new unit to arrive
* @param quality The quality of the new unit (0-5)
* @return The newly added unit
* @throws IllegalArgumentException If the quality is not within the valid range (0-5)
*/
public Unit addNewUnit(Entity en, boolean allowNewPilots, int days) {
public Unit addNewUnit(Entity en, boolean allowNewPilots, int days, int quality) {
if ((quality < 0) || (quality > 5)) {
throw new IllegalArgumentException("Invalid quality in mekhq/campaign/Campaign.java/addNewUnit: " + quality);
}

Unit unit = new Unit(en, this);
getHangar().addUnit(unit);

Expand Down Expand Up @@ -1192,6 +1202,8 @@ public Unit addNewUnit(Entity en, boolean allowNewPilots, int days) {
}
unit.resetPilotAndEntity();

unit.setQuality(quality);

// Assign an entity ID to our new unit
if (Entity.NONE == en.getId()) {
en.setId(game.getNextEntityId());
Expand Down
6 changes: 3 additions & 3 deletions MekHQ/src/mekhq/campaign/Quartermaster.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public enum PartAcquisitionResult {
PlanetSpecificFailure,
Success
}

private final Campaign campaign;

/**
Expand Down Expand Up @@ -470,13 +470,13 @@ public boolean buyUnit(Entity en, int days) {
Money cost = new Unit(en, getCampaign()).getBuyCost();
if (getCampaign().getFinances().debit(TransactionType.UNIT_PURCHASE, getCampaign().getLocalDate(),
cost, "Purchased " + en.getShortName())) {
getCampaign().addNewUnit(en, false, days);
getCampaign().addNewUnit(en, false, days, 3);
IllianiCBT marked this conversation as resolved.
Show resolved Hide resolved
return true;
} else {
return false;
}
} else {
getCampaign().addNewUnit(en, false, days);
getCampaign().addNewUnit(en, false, days, 3);
return true;
}
}
Expand Down
4 changes: 2 additions & 2 deletions MekHQ/src/mekhq/campaign/mission/AtBContract.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import megamek.common.icons.Camouflage;
import megamek.common.loaders.EntityLoadingException;
import mekhq.MekHQ;
import mekhq.utilities.MHQXMLUtility;
import mekhq.campaign.Campaign;
import mekhq.campaign.event.MissionChangedEvent;
import mekhq.campaign.finances.Money;
Expand All @@ -46,6 +45,7 @@
import mekhq.campaign.universe.Faction;
import mekhq.campaign.universe.Factions;
import mekhq.campaign.universe.RandomFactionGenerator;
import mekhq.utilities.MHQXMLUtility;
import org.apache.logging.log4j.LogManager;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
Expand Down Expand Up @@ -526,7 +526,7 @@ public void doBonusRoll(Campaign c) {
}

if (null != en) {
c.addNewUnit(en, false, 0);
c.addNewUnit(en, false, 0, 3);
} else {
c.addReport("<html><font color='red'>Could not load unit</font></html>");
}
Expand Down
4 changes: 2 additions & 2 deletions MekHQ/src/mekhq/campaign/mission/Loot.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@
import megamek.common.MechSummary;
import megamek.common.MechSummaryCache;
import megamek.common.loaders.EntityLoadingException;
import mekhq.utilities.MHQXMLUtility;
import mekhq.campaign.Campaign;
import mekhq.campaign.finances.Money;
import mekhq.campaign.finances.enums.TransactionType;
import mekhq.campaign.parts.Part;
import mekhq.utilities.MHQXMLUtility;
import org.apache.logging.log4j.LogManager;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
Expand Down Expand Up @@ -148,7 +148,7 @@ public void get(Campaign campaign, Scenario s) {
}

for (Entity e : units) {
campaign.addNewUnit(e, false, 0);
campaign.addNewUnit(e, false, 0, 3);
}

for (Part p : parts) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ protected void execute() {
return;
}
Entity en = mechFileParser.getEntity();
getCampaign().addNewUnit(en, false, 0);
getCampaign().addNewUnit(en, false, 0, 3);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
import mekhq.campaign.personnel.generator.AbstractPersonnelGenerator;
import mekhq.campaign.personnel.ranks.Rank;
import mekhq.campaign.unit.Unit;
import mekhq.campaign.universe.*;
import mekhq.campaign.universe.Faction;
import mekhq.campaign.universe.companyGeneration.AtBRandomMechParameters;
import mekhq.campaign.universe.companyGeneration.CompanyGenerationOptions;
import mekhq.campaign.universe.companyGeneration.CompanyGenerationPersonTracker;
Expand Down Expand Up @@ -947,7 +947,7 @@ private List<Unit> createUnits(final Campaign campaign,
continue;
}

final Unit unit = campaign.addNewUnit(tracker.getEntity(), false, 0);
final Unit unit = campaign.addNewUnit(tracker.getEntity(), false, 0, 3);
unit.addPilotOrSoldier(tracker.getPerson());
if (getOptions().isGenerateUnitsAsAttached()) {
tracker.getPerson().setOriginalUnit(unit);
Expand Down Expand Up @@ -1287,7 +1287,7 @@ public List<Entity> generateMothballedEntities(final Campaign campaign,
private List<Unit> createMothballedSpareUnits(final Campaign campaign,
final List<Entity> mothballedEntities) {
final List<Unit> mothballedUnits = mothballedEntities.stream()
.map(entity -> campaign.addNewUnit(entity, false, 0))
.map(entity -> campaign.addNewUnit(entity, false, 0, 3))
.collect(Collectors.toList());
mothballedUnits.forEach(Unit::completeMothball);
return mothballedUnits;
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 @@ -1893,7 +1893,7 @@ protected void loadListFile(final boolean allowNewPilots) {
if (unitFile != null) {
try {
for (Entity entity : new MULParser(unitFile, getCampaign().getGameOptions()).getEntities()) {
getCampaign().addNewUnit(entity, allowNewPilots, 0);
getCampaign().addNewUnit(entity, allowNewPilots, 0, 3);
}
} catch (Exception e) {
LogManager.getLogger().error("", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ private void addOneItem(final IAcquisitionWork acquisition) {
if (equipment instanceof Part) {
gui.getCampaign().getQuartermaster().addPart((Part) equipment, 0);
} else if (equipment instanceof Entity) {
gui.getCampaign().addNewUnit((Entity) equipment, false, 0);
gui.getCampaign().addNewUnit((Entity) equipment, false, 0, 3);
} else {
LogManager.getLogger().error("Attempted to add unknown equipment of " + acquisition.getAcquisitionName());
return;
Expand Down
2 changes: 1 addition & 1 deletion MekHQ/src/mekhq/gui/dialog/GMToolsDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -1362,7 +1362,7 @@ private void addRATRolledUnit() {
}

if (getLastRolledUnit() != null) {
final Unit unit = getGUI().getCampaign().addNewUnit(getLastRolledUnit(), false, 0);
final Unit unit = getGUI().getCampaign().addNewUnit(getLastRolledUnit(), false, 0, 3);
if ((getPerson() != null) && (getPerson().getUnit() == null)) {
unit.addPilotOrSoldier(getPerson());
getPerson().setOriginalUnit(unit);
Expand Down
2 changes: 1 addition & 1 deletion MekHQ/src/mekhq/gui/dialog/MekHQUnitSelectorDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ protected JPanel createButtonsPanel() {
protected void select(boolean isGM) {
if (getSelectedEntity() != null) {
if (isGM) {
campaign.addNewUnit(selectedUnit.getEntity(), false, 0);
campaign.addNewUnit(selectedUnit.getEntity(), false, 0, 3);
} else {
campaign.getShoppingList().addShoppingItem(selectedUnit, 1, campaign);
}
Expand Down
8 changes: 2 additions & 6 deletions MekHQ/src/mekhq/gui/dialog/PersonnelMarketDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,7 @@
import megamek.client.ui.preferences.*;
import megamek.client.ui.swing.MechViewPanel;
import megamek.codeUtilities.StringUtility;
import megamek.common.Aero;
import megamek.common.Compute;
import megamek.common.Entity;
import megamek.common.Mech;
import megamek.common.Tank;
import megamek.common.*;
import mekhq.MekHQ;
import mekhq.campaign.Campaign;
import mekhq.campaign.finances.Money;
Expand Down Expand Up @@ -378,7 +374,7 @@ private void addUnit(Entity en, boolean pay) {
unitCost, "Purchased " + en.getShortName())) {
return;
}
Unit unit = campaign.addNewUnit(en, false, 0);
Unit unit = campaign.addNewUnit(en, false, 0, 3);
if (unit == null) {
// No such unit matching the entity.
return;
Expand Down
4 changes: 2 additions & 2 deletions MekHQ/src/mekhq/gui/panes/UnitMarketPane.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/
package mekhq.gui.panes;

import megamek.client.ui.models.XTableColumnModel;
import megamek.client.ui.panels.EntityImagePanel;
import megamek.client.ui.panes.EntityViewPane;
import megamek.client.ui.preferences.*;
Expand All @@ -33,7 +34,6 @@
import mekhq.campaign.market.unitMarket.UnitMarketOffer;
import mekhq.gui.baseComponents.AbstractMHQSplitPane;
import mekhq.gui.model.UnitMarketTableModel;
import megamek.client.ui.models.XTableColumnModel;
import mekhq.gui.sorter.WeightClassSorter;
import org.apache.logging.log4j.LogManager;

Expand Down Expand Up @@ -455,7 +455,7 @@ public void addSelectedOffers() {
private void finalizeEntityAcquisition(final List<UnitMarketOffer> offers,
final boolean instantDelivery) {
for (final UnitMarketOffer offer : offers) {
getCampaign().addNewUnit(offer.getEntity(), false, instantDelivery ? 0 : offer.getTransitDuration());
getCampaign().addNewUnit(offer.getEntity(), false, instantDelivery ? 0 : offer.getTransitDuration(), 3);
if (!instantDelivery) {
getCampaign().addReport(String.format(resources.getString("UnitMarketPane.UnitDeliveryLength.report"),
offer.getTransitDuration()));
Expand Down
13 changes: 6 additions & 7 deletions MekHQ/unittests/mekhq/campaign/QuartermasterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import mekhq.campaign.finances.Money;
import mekhq.campaign.finances.enums.TransactionType;
import mekhq.campaign.parts.*;
import mekhq.campaign.parts.equipment.EquipmentPart;
import mekhq.campaign.unit.TestUnit;
import mekhq.campaign.unit.Unit;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -249,7 +248,7 @@ public void buyUnitAddsUnconditionallyIfNotPayingForUnits() {
assertTrue(quartermaster.buyUnit(mockEntity, transitDays));

// ...and the new unit should be added to the campaign.
verify(mockCampaign, times(1)).addNewUnit(eq(mockEntity), eq(false), eq(transitDays));
verify(mockCampaign, times(1)).addNewUnit(eq(mockEntity), eq(false), eq(transitDays), eq(3));
}

@Test
Expand All @@ -274,7 +273,7 @@ public void buyUnitReturnsFalseIfOutOfCash() {
assertFalse(quartermaster.buyUnit(mockEntity, 0));

// ...and the new unit should NOT be added to the campaign.
verify(mockCampaign, times(0)).addNewUnit(eq(mockEntity), eq(false), eq(0));
verify(mockCampaign, times(0)).addNewUnit(eq(mockEntity), eq(false), eq(0), eq(3));
}

@Test
Expand Down Expand Up @@ -302,7 +301,7 @@ public void buyUnitBuysAUnitIfWeCanAffordIt() {
assertTrue(quartermaster.buyUnit(mockEntity, 0));

// ...and the new unit should be added to the campaign...
verify(mockCampaign, times(1)).addNewUnit(eq(mockEntity), eq(false), eq(0));
verify(mockCampaign, times(1)).addNewUnit(eq(mockEntity), eq(false), eq(0), eq(3));

// ...and it should cost the right amount.
assertEquals(Money.of(cost), captor.getValue());
Expand Down Expand Up @@ -333,7 +332,7 @@ public void buyUnitBuysInfantryUsingAlternateCost() {
assertTrue(quartermaster.buyUnit(mockEntity, 0));

// ...and the new infantry should be added to the campaign...
verify(mockCampaign, times(1)).addNewUnit(eq(mockEntity), eq(false), eq(0));
verify(mockCampaign, times(1)).addNewUnit(eq(mockEntity), eq(false), eq(0), eq(3));

// ...and it should cost the right amount.
assertEquals(Money.of(cost), captor.getValue());
Expand Down Expand Up @@ -369,7 +368,7 @@ public void buyUnitAppliesClanCostMultiplier() {
assertTrue(quartermaster.buyUnit(mockEntity, 0));

// ...and the new unit should be added to the campaign...
verify(mockCampaign, times(1)).addNewUnit(eq(mockEntity), eq(false), eq(0));
verify(mockCampaign, times(1)).addNewUnit(eq(mockEntity), eq(false), eq(0), eq(3));

// ...and it should cost the right amount.
assertEquals(Money.of(clanMultiplier * cost), captor.getValue());
Expand Down Expand Up @@ -405,7 +404,7 @@ public void buyUnitAppliesClanCostMultiplierToInfantryAlso() {
assertTrue(quartermaster.buyUnit(mockEntity, 0));

// ...and the new clan infantry should be added to the campaign...
verify(mockCampaign, times(1)).addNewUnit(eq(mockEntity), eq(false), eq(0));
verify(mockCampaign, times(1)).addNewUnit(eq(mockEntity), eq(false), eq(0), eq(3));

// ...and it should cost the right amount.
assertEquals(Money.of(clanMultiplier * cost), captor.getValue());
Expand Down
2 changes: 1 addition & 1 deletion MekHQ/unittests/mekhq/campaign/unit/UnitTestUtilities.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
public final class UnitTestUtilities {

public static Unit addAndGetUnit(Campaign campaign, Entity entity) {
campaign.addNewUnit(entity, false, 0);
campaign.addNewUnit(entity, false, 0, 3);
for (Unit unit : campaign.getHangar().getUnits()) {
return unit;
}
Expand Down
Loading