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: stops bays from loading BA and protomek ammunition #6220

Merged
merged 2 commits into from
Nov 29, 2024
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
48 changes: 28 additions & 20 deletions megamek/src/megamek/client/ui/swing/BayMunitionsChoicePanel.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* MegaMek - Copyright (C) 2017 - The MegaMek Team
* MegaMek - Copyright (C) 2024 - The MegaMek Team
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
Expand All @@ -13,7 +13,6 @@
*/
package megamek.client.ui.swing;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
Expand All @@ -23,7 +22,6 @@
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

import javax.swing.JLabel;
import javax.swing.JPanel;
Expand All @@ -47,6 +45,9 @@
import megamek.common.options.OptionsConstants;
import megamek.logging.MMLogger;

import static megamek.common.AmmoType.F_BATTLEARMOR;
import static megamek.common.AmmoType.F_PROTOMEK;

/**
* @author Neoancient
*/
Expand Down Expand Up @@ -142,9 +143,6 @@ public void apply() {
}

class AmmoRowPanel extends JPanel implements ChangeListener {
/**
*
*/
private static final long serialVersionUID = 7251618728823971065L;

private final JLabel lblTonnage = new JLabel();
Expand All @@ -167,16 +165,17 @@ class AmmoRowPanel extends JPanel implements ChangeListener {
this.ammoMounts = new ArrayList<>(ammoMounts);
this.spinners = new ArrayList<>();

final Optional<WeaponType> wtype = bay.getBayWeapons().stream()
final Optional<WeaponType> weaponType = bay.getBayWeapons().stream()
.map(Mounted::getType).findAny();

// set the bay's tech base to that of any weapon in the bay
// an assumption is made here that bays don't mix clan-only and IS-only tech
// base
this.techBase = wtype.map(EquipmentType::getTechBase).orElse(WeaponType.TECH_BASE_ALL);
this.techBase = weaponType.map(EquipmentType::getTechBase).orElse(WeaponType.TECH_BASE_ALL);

munitions = AmmoType.getMunitionsFor(at).stream()
.filter(this::includeMunition).collect(Collectors.toList());
.filter(this::includeMunition)
.toList();
tonnage = ammoMounts.stream().mapToDouble(Mounted::getSize).sum();
Map<String, Integer> starting = new HashMap<>();
ammoMounts.forEach(m -> starting.merge(m.getType().getInternalName(), m.getBaseShotsLeft(), Integer::sum));
Expand All @@ -203,7 +202,7 @@ class AmmoRowPanel extends JPanel implements ChangeListener {
gbc.insets = new Insets(0, 5, 0, 5);
gbc.gridwidth = 5;
add(new JLabel("(" + entity.getLocationAbbr(bay.getLocation()) + ") "
+ (wtype.isPresent() ? wtype.get().getName() : "?")), gbc);
+ (weaponType.isPresent() ? weaponType.get().getName() : "?")), gbc);
gbc.gridx = 5;
gbc.gridwidth = 1;
gbc.weightx = 1.0;
Expand All @@ -226,33 +225,42 @@ class AmmoRowPanel extends JPanel implements ChangeListener {
recalcMaxValues();
}

private boolean includeMunition(AmmoType atype) {
if (!atype
/**
* Assert if a specific ammo type should be included in the list of munitions for the ship.
* @param ammoType the type of munition to be asserted
* @return true means the munition should be included.
*/
private boolean includeMunition(AmmoType ammoType) {
if (!ammoType
.canAeroUse(game.getOptions().booleanOption(OptionsConstants.ADVAERORULES_AERO_ARTILLERY_MUNITIONS))
|| (atype.getAmmoType() != at)
|| (atype.getRackSize() != rackSize)
|| ((atype.getTechBase() != techBase)
&& (atype.getTechBase() != AmmoType.TECH_BASE_ALL)
|| (ammoType.getAmmoType() != at)
|| (ammoType.getRackSize() != rackSize)
|| ((ammoType.getTechBase() != techBase)
&& (ammoType.getTechBase() != AmmoType.TECH_BASE_ALL)
&& (techBase != AmmoType.TECH_BASE_ALL))
|| !atype.isLegal(game.getOptions().intOption(OptionsConstants.ALLOWED_YEAR),
|| !ammoType.isLegal(game.getOptions().intOption(OptionsConstants.ALLOWED_YEAR),
SimpleTechLevel.getGameTechLevel(game),
techBase == AmmoType.TECH_BASE_CLAN,
techBase == AmmoType.TECH_BASE_ALL,
game.getOptions().booleanOption(OptionsConstants.ALLOWED_SHOW_EXTINCT))) {
return false;
}
if (atype.hasFlag(AmmoType.F_NUCLEAR)
if (ammoType.hasFlag(AmmoType.F_NUCLEAR)
&& !game.getOptions().booleanOption(
OptionsConstants.ADVAERORULES_AT2_NUKES)) {
return false;
}
if (atype.getMunitionType().contains(AmmoType.Munitions.M_ARTEMIS_CAPABLE)) {
if (ammoType.getMunitionType().contains(AmmoType.Munitions.M_ARTEMIS_CAPABLE)) {
return entity.hasWorkingMisc(MiscType.F_ARTEMIS)
|| entity.hasWorkingMisc(MiscType.F_ARTEMIS_PROTO);
}
if (atype.getMunitionType().contains(AmmoType.Munitions.M_ARTEMIS_V_CAPABLE)) {
if (ammoType.getMunitionType().contains(AmmoType.Munitions.M_ARTEMIS_V_CAPABLE)) {
return entity.hasWorkingMisc(MiscType.F_ARTEMIS_V);
}
// A Bay should not load BA nor Protomek exclusive ammo/weapons
if (ammoType.hasFlag(F_BATTLEARMOR) || ammoType.hasFlag(F_PROTOMEK)) {
return false;
}
Comment on lines +260 to +263
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This is the only code newly added.

return true;
}

Expand Down
15 changes: 7 additions & 8 deletions megamek/src/megamek/client/ui/swing/EquipChoicePanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -323,8 +323,7 @@ public void applyChoices() {
boolean condEjectFuel = chCondEjectFuel.isSelected();
boolean condEjectSIDest = chCondEjectSIDest.isSelected();

if (entity instanceof Mek) {
Mek mek = (Mek) entity;
if (entity instanceof Mek mek) {
mek.setAutoEject(!autoEject);
mek.setCondEjectAmmo(condEjectAmmo);
mek.setCondEjectEngine(condEjectEngine);
Expand All @@ -349,8 +348,8 @@ public void applyChoices() {
}

// update munitions selections
for (final Object newVar2 : m_vMunitions) {
((MunitionChoicePanel) newVar2).applyChoice();
for (final MunitionChoicePanel munitions : m_vMunitions) {
munitions.applyChoice();
}
if (panMunitions instanceof BayMunitionsChoicePanel) {
((BayMunitionsChoicePanel) panMunitions).apply();
Expand All @@ -365,12 +364,12 @@ public void applyChoices() {
}

// update MG rapid fire settings
for (final Object newVar1 : m_vMGs) {
((RapidfireMGPanel) newVar1).applyChoice();
for (final RapidfireMGPanel rapidfireMGPanel : m_vMGs) {
rapidfireMGPanel.applyChoice();
}
// update mines setting
for (final Object newVar : m_vMines) {
((MineChoicePanel) newVar).applyChoice();
for (final MineChoicePanel mineChoicePanel : m_vMines) {
mineChoicePanel.applyChoice();
}
// update bomb setting
if (null != m_bombs) {
Expand Down