8000 Improvement: #6867 Added Weapons as a separate autologistics category by psikomonkie · Pull Request #6943 · MegaMek/mekhq · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Improvement: #6867 Added Weapons as a separate autologistics category #6943

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

Merged
merged 8 commits into from
May 10, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,9 @@ lblAutoLogisticsJumpJets.tooltip=autoLogistics counts the number of jump jets in
lblAutoLogisticsEngines.text=Engine Restock Percent
lblAutoLogisticsEngines.tooltip=autoLogistics counts the number of engines in use and then\
\ automatically orders replacement spares equal to the percentage set in this option.
lblAutoLogisticsWeapons.text=Weapon Restock Percent
lblAutoLogisticsWeapons.tooltip=autoLogistics counts the number of weapons in use and then\
\ automatically orders replacement spares equal to the percentage set in this option.
lblAutoLogisticsOther.text=Other Restock Percent
lblAutoLogisticsOther.tooltip=autoLogistics counts each part in use, that is not covered by one of\
\ the other options, and automatically orders replacement spares equal to the percentage set in\
Expand Down
4 changes: 4 additions & 0 deletions MekHQ/src/mekhq/campaign/Campaign.java
Original file line number Diff line number Diff line change
Expand Up @@ -2800,6 +2800,10 @@ private int getDefaultStockPercent(Part part) {
return campaignOptions.getAutoLogisticsJumpJets();
} else if (part instanceof EnginePart) {
return campaignOptions.getAutoLogisticsEngines();
} else if (part instanceof EquipmentPart equipmentPart) {
if (equipmentPart.getType() instanceof WeaponType) {
return campaignOptions.getAutoLogisticsWeapons();
}
}

return campaignOptions.getAutoLogisticsOther();
Expand Down
33 changes: 23 additions & 10 deletions MekHQ/src/mekhq/campaign/CampaignOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ public static String getTechLevelName(final int techLevel) {
private int autoLogisticsActuators;
private int autoLogisticsJumpJets;
private int autoLogisticsEngines;
private int autoLogisticsWeapons;
private int autoLogisticsOther;

// Delivery
Expand Down Expand Up @@ -711,6 +712,7 @@ public CampaignOptions() {
autoLogisticsActuators = 100;
autoLogisticsJumpJets = 50;
autoLogisticsEngines = 0;
autoLogisticsWeapons = 50;
autoLogisticsOther = 0;

// Delivery
Expand Down Expand Up @@ -4394,6 +4396,14 @@ public void setAutoLogisticsEngines(int autoLogisticsEngines) {
this.autoLogisticsEngines = autoLogisticsEngines;
}

public int getAutoLogisticsWeapons() {
return autoLogisticsWeapons;
}

public void setAutoLogisticsWeapons(int autoLogisticsWeapons) {
this.autoLogisticsWeapons = autoLogisticsWeapons;
}

public int getAutoLogisticsOther() {
return autoLogisticsOther;
}
Expand Down Expand Up @@ -4950,6 +4960,7 @@ public void writeToXml(final PrintWriter pw, int indent) {
MHQXMLUtility.writeSimpleXMLTag(pw, indent, "autoLogisticsActuators", autoLogisticsActuators);
MHQXMLUtility.writeSimpleXMLTag(pw, indent, "autoLogisticsJumpJets", autoLogisticsJumpJets);
MHQXMLUtility.writeSimpleXMLTag(pw, indent, "autoLogisticsEngines", autoLogisticsEngines);
MHQXMLUtility.writeSimpleXMLTag(pw, indent, "autoLogisticsWeapons", autoLogisticsWeapons);
MHQXMLUtility.writeSimpleXMLTag(pw, indent, "autoLogisticsOther", autoLogisticsOther);

// region Personnel Tab
Expand Down Expand Up @@ -5689,25 +5700,27 @@ public static CampaignOptions generateCampaignOptionsFromXml(Node wn, Version ve

// autoLogistics
} else if (nodeName.equalsIgnoreCase("autoLogisticsHeatSink")) {
retVal.autoLogisticsHeatSink = Integer.parseInt(wn2.getTextContent().trim());
retVal.autoLogisticsHeatSink = MathUtility.parseInt(wn2.getTextContent().trim());
} else if (nodeName.equalsIgnoreCase("autoLogisticsMekHead")) {
retVal.autoLogisticsMekHead = Integer.parseInt(wn2.getTextContent().trim());
retVal.autoLogisticsMekHead = MathUtility.parseInt(wn2.getTextContent().trim());
} else if (nodeName.equalsIgnoreCase("autoLogisticsMekLocation")) {
retVal.autoLogisticsMekLocation = Integer.parseInt(wn2.getTextContent().trim());
retVal.autoLogisticsMekLocation = MathUtility.parseInt(wn2.getTextContent().trim());
} else if (nodeName.equalsIgnoreCase("autoLogisticsNonRepairableLocation")) {
retVal.autoLogisticsNonRepairableLocation = Integer.parseInt(wn2.getTextContent().trim());
retVal.autoLogisticsNonRepairableLocation = MathUtility.parseInt(wn2.getTextContent().trim());
} else if (nodeName.equalsIgnoreCase("autoLogisticsArmor")) {
retVal.autoLogisticsArmor = Integer.parseInt(wn2.getTextContent().trim());
retVal.autoLogisticsArmor = MathUtility.parseInt(wn2.getTextContent().trim());
} else if (nodeName.equalsIgnoreCase("autoLogisticsAmmunition")) {
retVal.autoLogisticsAmmunition = Integer.parseInt(wn2.getTextContent().trim());
retVal.autoLogisticsAmmunition = MathUtility.parseInt(wn2.getTextContent().trim());
} else if (nodeName.equalsIgnoreCase("autoLogisticsActuators")) {
retVal.autoLogisticsActuators = Integer.parseInt(wn2.getTextContent().trim());
retVal.autoLogisticsActuators = MathUtility.parseInt(wn2.getTextContent().trim());
} else if (nodeName.equalsIgnoreCase("autoLogisticsJumpJets")) {
retVal.autoLogisticsJumpJets = Integer.parseInt(wn2.getTextContent().trim());
retVal.autoLogisticsJumpJets = MathUtility.parseInt(wn2.getTextContent().trim());
} else if (nodeName.equalsIgnoreCase("autoLogisticsEngines")) {
retVal.autoLogisticsEngines = Integer.parseInt(wn2.getTextContent().trim());
retVal.autoLogisticsEngines = MathUtility.parseInt(wn2.getTextContent().trim());
} else if (nodeName.equalsIgnoreCase("autoLogisticsWeapons")) {
retVal.autoLogisticsWeapons = MathUtility.parseInt(wn2.getTextContent().trim());
} else if (nodeName.equalsIgnoreCase("autoLogisticsOther")) {
retVal.autoLogisticsOther = Integer.parseInt(wn2.getTextContent().trim());
retVal.autoLogisticsOther = MathUtility.parseInt(wn2.getTextContent().trim());

// region Personnel Tab
// region General Personnel
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ public class EquipmentAndSuppliesTab {
private JPanel pnlAutoLogistics;
private JLabel lblAutoLogisticsHeatSink;
private JSpinner spnAutoLogisticsHeatSink;
private JLabel lblAutoLogisticsWeapons;
private JSpinner spnAutoLogisticsWeapons;
private JLabel lblAutoLogisticsMekHead;
private JSpinner spnAutoLogisticsMekHead;
private JLabel lblAutoLogisticsMekLocation;
Expand Down Expand Up @@ -297,6 +299,8 @@ private void initializeAutoLogisticsTab() {
pnlAutoLogistics = new JPanel();
lblAutoLogisticsHeatSink = new JLabel();
spnAutoLogisticsHeatSink = new JSpinner();
lblAutoLogisticsWeapons = new JLabel();
spnAutoLogisticsWeapons = new JSpinner();
lblAutoLogisticsMekHead = new JLabel();
spnAutoLogisticsMekHead = new JSpinner();
lblAutoLogisticsMekLocation = new JLabel();
Expand Down Expand Up @@ -486,6 +490,9 @@ private JPanel createAutoLogisticsPanel() {
lblAutoLogisticsHeatSink = new CampaignOptionsLabel("AutoLogisticsHeatSink");
spnAutoLogisticsHeatSink = new CampaignOptionsSpinner("AutoLogisticsHeatSink",
250, 0, 10000, 1);
lblAutoLogisticsWeapons = new CampaignOptionsLabel("AutoLogisticsWeapons");
spnAutoLogisticsWeapons = new CampaignOptionsSpinner("AutoLogisticsWeapons",
50, 0, 10000, 1);

lblAutoLogisticsActuators = new CampaignOptionsLabel("AutoLogisticsActuators");
spnAutoLogisticsActuators = new CampaignOptionsSpinner("AutoLogisticsActuators",
Expand All @@ -511,65 +518,27 @@ private JPanel createAutoLogisticsPanel() {
layout.gridy = 0;
layout.gridx = 0;
layout.gridwidth = 1;
panel.add(lblAutoLogisticsMekHead, layout);
layout.gridx++;
panel.add(spnAutoLogisticsMekHead, layout);

layout.gridx = 0;
layout.gridy++;
panel.add(lblAutoLogisticsMekLocation, layout);
layout.gridx++;
panel.add(spnAutoLogisticsMekLocation, layout);

layout.gridx = 0;
layout.gridy++;
panel.add(lblAutoLogisticsNonRepairableLocation, layout);
layout.gridx++;
panel.add(spnAutoLogisticsNonRepairableLocation, layout);

layout.gridx = 0;
layout.gridy++;
panel.add(lblAutoLogisticsHeatSink, layout);
layout.gridx++;
panel.add(spnAutoLogisticsHeatSink, layout);

layout.gridx = 0;
layout.gridy++;
panel.add(lblAutoLogisticsArmor, layout);
layout.gridx++;
panel.add(spnAutoLogisticsArmor, layout);

layout.gridx = 0;
layout.gridy++;
panel.add(lblAutoLogisticsAmmunition, layout);
layout.gridx++;
panel.add(spnAutoLogisticsAmmunition, layout);
addSpinnerToPanel(panel, layout, lblAutoLogisticsMekHead, spnAutoLogisticsMekHead);
addSpinnerToPanel(panel, layout, lblAutoLogisticsMekLocation, spnAutoLogisticsMekLocation);
addSpinnerToPanel(panel, layout, lblAutoLogisticsNonRepairableLocation, spnAutoLogisticsNonRepairableLocation);
addSpinnerToPanel(panel, layout, lblAutoLogisticsHeatSink, spnAutoLogisticsHeatSink);
addSpinnerToPanel(panel, layout, lblAutoLogisticsArmor, spnAutoLogisticsArmor);
addSpinnerToPanel(panel, layout, lblAutoLogisticsAmmunition, spnAutoLogisticsAmmunition);
addSpinnerToPanel(panel, layout, lblAutoLogisticsActuators, spnAutoLogisticsActuators);
addSpinnerToPanel(panel, layout, lblAutoLogisticsJumpJets, spnAutoLogisticsJumpJets);
addSpinnerToPanel(panel, layout, lblAutoLogisticsEngines, spnAutoLogisticsEngines);
addSpinnerToPanel(panel, layout, lblAutoLogisticsWeapons, spnAutoLogisticsWeapons);
addSpinnerToPanel(panel, layout, lblAutoLogisticsOther, spnAutoLogisticsOther);

layout.gridx = 0;
layout.gridy++;
panel.add(lblAutoLogisticsActuators, layout);
layout.gridx++;
panel.add(spnAutoLogisticsActuators, layout);

layout.gridx = 0;
layout.gridy++;
panel.add(lblAutoLogisticsJumpJets, layout);
layout.gridx++;
panel.add(spnAutoLogisticsJumpJets, layout);
return panel;
}

layout.gridx = 0;
private void addSpinnerToPanel(JPanel panel, GridBagConstraints layout, JLabel label, JSpinner spinner) {
layout.gridy++;
panel.add(lblAutoLogisticsEngines, layout);
panel.add(label, layout);
layout.gridx++;
panel.add(spnAutoLogisticsEngines, layout);

panel.add(spinner, layout);
layout.gridx = 0;
layout.gridy++;
panel.add(lblAutoLogisticsOther, layout);
layout.gridx++;
panel.add(spnAutoLogisticsOther, layout);

return panel;
}

/**
Expand Down Expand Up @@ -1113,6 +1082,7 @@ public void applyCampaignOptionsToCampaign(@Nullable CampaignOptions presetCampa
options.setAutoLogisticsJumpJets((int) spnAutoLogisticsJumpJets.getValue());
options.setAutoLogisticsEngines((int) spnAutoLogisticsEngines.getValue());
options.setAutoLogisticsHeatSink((int) spnAutoLogisticsHeatSink.getValue());
options.setAutoLogisticsWeapons((int) spnAutoLogisticsWeapons.getValue());
options.setAutoLogisticsOther((int) spnAutoLogisticsOther.getValue());

// Delivery
Expand Down Expand Up @@ -1189,6 +1159,7 @@ public void loadValuesFromCampaignOptions(@Nullable CampaignOptions presetCampai
spnAutoLogisticsJumpJets.setValue(options.getAutoLogisticsJumpJets());
spnAutoLogisticsEngines.setValue(options.getAutoLogisticsEngines());
spnAutoLogisticsHeatSink.setValue(options.getAutoLogisticsHeatSink());
spnAutoLogisticsWeapons.setValue(options.getAutoLogisticsWeapons());
spnAutoLogisticsOther.setValue(options.getAutoLogisticsOther());

// Delivery
Expand Down
50 changes: 50 additions & 0 deletions MekHQ/unittests/mekhq/campaign/CampaignTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,15 @@
import java.util.stream.Stream;

import megamek.common.Dropship;
import megamek.common.Entity;
import megamek.common.EquipmentType;
import megamek.common.Mek;
import megamek.common.WeaponType;
import megamek.common.enums.SkillLevel;
import mekhq.campaign.enums.CampaignTransportType;
import mekhq.campaign.parts.*;
import mekhq.campaign.parts.equipment.AmmoBin;
import mekhq.campaign.parts.equipment.EquipmentPart;
import mekhq.campaign.parts.equipment.HeatSink;
import mekhq.campaign.parts.equipment.JumpJet;
import mekhq.campaign.personnel.Person;
Expand Down Expand Up @@ -410,6 +413,21 @@ public void testGetSetStockPercentEngines() {
assertEquals(SECOND_DESIRED_STOCK, secondStockLevel);
}

@Test
public void testGetSetStockPercentWeapons() {
// Act
campaign.getCampaignOptions().setAutoLogisticsWeapons(FIRST_DESIRED_STOCK);
int firstStockLevel = campaign.getCampaignOptions().getAutoLogisticsWeapons();

// Let's change the stock level to something else so we can make sure it properly changes
campaign.getCampaignOptions().setAutoLogisticsWeapons(SECOND_DESIRED_STOCK);
int secondStockLevel = campaign.getCampaignOptions().getAutoLogisticsWeapons();

// Assert
assertEquals(FIRST_DESIRED_STOCK, firstStockLevel);
assertEquals(SECOND_DESIRED_STOCK, secondStockLevel);
}

@Test
public void testGetSetStockPercentOther() {
// Act
Expand Down Expand Up @@ -479,6 +497,7 @@ void beforeEach() {
when(mockCampaignOptions.getAutoLogisticsActuators()).thenReturn(INCORRECT_STOCK_LEVEL);
when(mockCampaignOptions.getAutoLogisticsJumpJets()).thenReturn(INCORRECT_STOCK_LEVEL);
when(mockCampaignOptions.getAutoLogisticsEngines()).thenReturn(INCORRECT_STOCK_LEVEL);
when(mockCampaignOptions.getAutoLogisticsWeapons()).thenReturn(INCORRECT_STOCK_LEVEL);
when(mockCampaignOptions.getAutoLogisticsOther()).thenReturn(INCORRECT_STOCK_LEVEL);
}

Expand Down Expand Up @@ -813,6 +832,37 @@ public void testGetDefaultStockPercentEngines() {
assertEquals(1, afterChangeAllPercents.stream().filter(i -> i == DESIRED_STOCK_LEVEL).toArray().length);
}

@Test
public void testGetDefaultStockPercentWeapons() {
// Arrange
WeaponType mockWeaponType = mock(WeaponType.class);
part = new EquipmentPart(1, mockWeaponType, Entity.LOC_NONE, 1.0, false, campaign);

// Act
try {
initialStockPercent = (int) method.invoke(campaign, part);
initialAllPercents = getAllDefaultStockPercents();

// Let's change it and make sure that it uses the new value
when(mockCampaignOptions.getAutoLogisticsWeapons()).thenReturn(DESIRED_STOCK_LEVEL); //TODO
Copy link
Preview
Copilot AI May 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Consider removing or resolving the TODO comment once the intended behavior for weapon autologistics is finalized.

Suggested change
when(mockCampaignOptions.getAutoLogisticsWeapons()).thenReturn(DESIRED_STOCK_LEVEL); //TODO
// Mock the campaign options to return the desired stock level for weapon autologistics
when(mockCampaignOptions.getAutoLogisticsWeapons()).thenReturn(DESIRED_STOCK_LEVEL);

Copilot uses AI. Check for mistakes.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ha, it's nice to see Copilot bullying someone else for a change! :P

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No that was actually something I missed. The //TODO tag itself just needed removed. It was originally commented out during the development process. I left the //TODO flag so I'd uncomment it once it's implemented it. Just forgot to remove the todo :clown:


desiredStockPercent = (int) method.invoke(campaign, part);
afterChangeAllPercents = getAllDefaultStockPercents();
} catch (IllegalAccessException | InvocationTargetException e) {
throw new RuntimeException(e);
}

// Assert
assertEquals(INCORRECT_STOCK_LEVEL, initialStockPercent);
assertEquals(DESIRED_STOCK_LEVEL, desiredStockPercent);

// None of the initial defaults should contain the desired stock percent
assertFalse(initialAllPercents.contains(desiredStockPercent));

// Only one of these should be the desired stock percent
assertEquals(1, afterChangeAllPercents.stream().filter(i -> i == DESIRED_STOCK_LEVEL).toArray().length);
}

@ParameterizedTest
@MethodSource(value = "otherUnhandledDefaultStockPercentParts")
public void testGetDefaultStockPercentOtherUnhandled(Part otherPart) {
Expand Down
Loading
0