Skip to content

Commit 0e61b69

Browse files
authored
Merge pull request #6435 from IllianiCBT/forceCounter
Corrected Briefing Room Combat Team Counter
2 parents 60e47e6 + 5fff01e commit 0e61b69

File tree

2 files changed

+89
-74
lines changed

2 files changed

+89
-74
lines changed

MekHQ/src/mekhq/campaign/mission/enums/CombatRole.java

+36-19
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@
2727
*/
2828
package mekhq.campaign.mission.enums;
2929

30+
import java.util.ResourceBundle;
31+
3032
import megamek.logging.MMLogger;
3133
import mekhq.MekHQ;
3234

33-
import java.util.ResourceBundle;
34-
3535
public enum CombatRole {
3636
// region Enum Declarations
3737
MANEUVER("CombatRole.MANEUVER.text", "CombatRole.MANEUVER.toolTipText"),
@@ -50,7 +50,7 @@ public enum CombatRole {
5050
// region Constructors
5151
CombatRole(final String name, final String toolTipText) {
5252
final ResourceBundle resources = ResourceBundle.getBundle("mekhq.resources.Mission",
53-
MekHQ.getMHQOptions().getLocale());
53+
MekHQ.getMHQOptions().getLocale());
5454
this.name = resources.getString(name);
5555
this.toolTipText = resources.getString(toolTipText);
5656
}
@@ -86,36 +86,54 @@ public boolean isAuxiliary() {
8686
public boolean isReserve() {
8787
return this == RESERVE;
8888
}
89+
90+
/**
91+
* Determines if this role is categorized as a combat role.
92+
*
93+
* <p>A role is considered a combat role if it matches one of the following predefined roles:</p>
94+
* <ul>
95+
* <li>{@code FRONTLINE}</li>
96+
* <li>{@code MANEUVER}</li>
97+
* <li>{@code PATROL}</li>
98+
* </ul>
99+
*
100+
* @return {@code true} if this role is one of the combat roles; {@code false} otherwise.
101+
*/
102+
public boolean isCombatRole() {
103+
return this == FRONTLINE || this == MANEUVER || this == PATROL;
104+
}
89105
// endregion Boolean Comparison Methods
90106

91107
// region File I/O
108+
92109
/**
93110
* Parses a {@link String} into a {@link CombatRole} enum value.
94111
* <p>
95-
* This method first attempts to interpret the input string as an integer and then maps it
96-
* to the corresponding {@link CombatRole} based on its ordinal index. If that fails, it
97-
* attempts to match the string to the name of a {@link CombatRole} using {@code Enum.valueOf(String)}.
98-
* If both parsing approaches fail, it logs an error and returns the default value {@code IN_RESERVE}.
112+
* This method first attempts to interpret the input string as an integer and then maps it to the corresponding
113+
* {@link CombatRole} based on its ordinal index. If that fails, it attempts to match the string to the name of a
114+
* {@link CombatRole} using {@code Enum.valueOf(String)}. If both parsing approaches fail, it logs an error and
115+
* returns the default value {@code IN_RESERVE}.
99116
* </p>
100117
*
101-
* @param text the string to be parsed into a {@link CombatRole}.
102-
* The string can represent either:
118+
* @param text the string to be parsed into a {@link CombatRole}. The string can represent either:
103119
* <ul>
104120
* <li>An integer corresponding to the ordinal index of a {@link CombatRole}.</li>
105121
* <li>The name of a {@link CombatRole}.</li>
106122
* </ul>
107-
* @return the corresponding {@link CombatRole} if the input is valid;
108-
* otherwise, returns {@code IN_RESERVE}.
123+
*
124+
* @return the corresponding {@link CombatRole} if the input is valid; otherwise, returns {@code IN_RESERVE}.
109125
*/
110126
public static CombatRole parseFromString(final String text) {
111127
try {
112128
int value = Integer.parseInt(text);
113129
return values()[value];
114-
} catch (Exception ignored) {}
130+
} catch (Exception ignored) {
131+
}
115132

116133
try {
117134
return valueOf(text);
118-
} catch (Exception ignored) {}
135+
} catch (Exception ignored) {
136+
}
119137

120138
// <50.02 compatibility handler
121139
return switch (text) {
@@ -125,19 +143,18 @@ public static CombatRole parseFromString(final String text) {
125143
case "IN_RESERVE", "UNASSIGNED" -> RESERVE;
126144
default -> {
127145
MMLogger.create(CombatRole.class)
128-
.warn(String.format("Unable to parse %s into an CombatRole. Returning RESERVE.",
129-
text));
146+
.warn(String.format("Unable to parse %s into an CombatRole. Returning RESERVE.", text));
130147

131148
yield RESERVE;
132149
}
133150

134151
};
135152

136153
// To be uncommented once the above compatibility handler is removed.
137-
// MMLogger.create(CombatRole.class)
138-
// .error("Unable to parse " + text + " into an CombatRole. Returning RESERVE.");
139-
//
140-
// return RESERVE;
154+
// MMLogger.create(CombatRole.class)
155+
// .error("Unable to parse " + text + " into an CombatRole. Returning RESERVE.");
156+
//
157+
// return RESERVE;
141158
}
142159
// endregion File I/O
143160

MekHQ/src/mekhq/gui/view/LanceAssignmentView.java

+53-55
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,24 @@
2828
*/
2929
package mekhq.gui.view;
3030

31+
import static megamek.client.ui.WrapLayout.wordWrap;
32+
import static mekhq.campaign.mission.enums.CombatRole.FRONTLINE;
33+
import static mekhq.campaign.mission.enums.CombatRole.MANEUVER;
34+
import static mekhq.campaign.mission.enums.CombatRole.PATROL;
35+
import static mekhq.campaign.mission.enums.CombatRole.TRAINING;
36+
37+
import java.awt.Component;
38+
import java.awt.Dimension;
39+
import java.util.ArrayList;
40+
import java.util.Comparator;
41+
import java.util.List;
42+
import javax.swing.*;
43+
import javax.swing.RowSorter.SortKey;
44+
import javax.swing.event.TableModelEvent;
45+
import javax.swing.event.TableModelListener;
46+
import javax.swing.table.TableColumn;
47+
import javax.swing.table.TableRowSorter;
48+
3149
import megamek.client.ui.models.XTableColumnModel;
3250
import megamek.common.util.sorter.NaturalOrderComparator;
3351
import mekhq.MekHQ;
@@ -40,27 +58,9 @@
4058
import mekhq.gui.model.DataTableModel;
4159
import mekhq.gui.utilities.MekHqTableCellRenderer;
4260

43-
import javax.swing.*;
44-
import javax.swing.RowSorter.SortKey;
45-
import javax.swing.event.TableModelEvent;
46-
import javax.swing.event.TableModelListener;
47-
import javax.swing.table.TableColumn;
48-
import javax.swing.table.TableRowSorter;
49-
import java.awt.*;
50-
import java.util.ArrayList;
51-
import java.util.Comparator;
52-
import java.util.List;
53-
54-
import static megamek.client.ui.WrapLayout.wordWrap;
55-
import static mekhq.campaign.mission.enums.CombatRole.FRONTLINE;
56-
import static mekhq.campaign.mission.enums.CombatRole.MANEUVER;
57-
import static mekhq.campaign.mission.enums.CombatRole.PATROL;
58-
import static mekhq.campaign.mission.enums.CombatRole.TRAINING;
59-
6061
/**
61-
* Against the Bot
62-
* Shows how many lances are required to be deployed on active contracts and
63-
* in what roles and allows the player to assign units to those roles.
62+
* Against the Bot Shows how many lances are required to be deployed on active contracts and in what roles and allows
63+
* the player to assign units to those roles.
6464
*
6565
* @author Neoancient
6666
*/
@@ -83,8 +83,7 @@ private void initComponents() {
8383
cbContract = new JComboBox<>();
8484
cbContract.setRenderer(new DefaultListCellRenderer() {
8585
@Override
86-
public Component getListCellRendererComponent(JList<?> list, Object value, int index,
87-
boolean isSelected, boolean cellHasFocus) {
86+
public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
8887
return new JLabel((null == value) ? "None" : ((AtBContract) value).getName());
8988
}
9089
});
@@ -93,9 +92,7 @@ public Component getListCellRendererComponent(JList<?> list, Object value, int i
9392
cbRole.setName("cbRole");
9493
cbRole.setRenderer(new DefaultListCellRenderer() {
9594
@Override
96-
public Component getListCellRendererComponent(final JList<?> list, final Object value,
97-
final int index, final boolean isSelected,
98-
final boolean cellHasFocus) {
95+
public Component getListCellRendererComponent(final JList<?> list, final Object value, final int index, final boolean isSelected, final boolean cellHasFocus) {
9996
super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
10097
if (value instanceof CombatRole) {
10198
list.setToolTipText(wordWrap(((CombatRole) value).getToolTipText()));
@@ -117,12 +114,10 @@ public Component getListCellRendererComponent(final JList<?> list, final Object
117114
column.setPreferredWidth(rlModel.getColumnWidth(i));
118115
column.setCellRenderer(new MekHqTableCellRenderer() {
119116
@Override
120-
public Component getTableCellRendererComponent(JTable table, Object value,
121-
boolean isSelected, boolean hasFocus,
122-
int row, int column) {
117+
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
123118
super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
124-
setHorizontalAlignment(((RequiredLancesTableModel) table.getModel()).
125-
getAlignment(table.convertColumnIndexToModel(column)));
119+
setHorizontalAlignment(((RequiredLancesTableModel) table.getModel()).getAlignment(table.convertColumnIndexToModel(
120+
column)));
126121
if (table.convertColumnIndexToModel(column) > RequiredLancesTableModel.COL_CONTRACT) {
127122
if (((String) value).indexOf('/') >= 0) {
128123
setForeground(MekHQ.getMHQOptions().getBelowContractMinimumForeground());
@@ -132,7 +127,7 @@ public Component getTableCellRendererComponent(JTable table, Object value,
132127
}
133128
});
134129
}
135-
TableRowSorter<RequiredLancesTableModel>sorter = new TableRowSorter<>(rlModel);
130+
TableRowSorter<RequiredLancesTableModel> sorter = new TableRowSorter<>(rlModel);
136131
tblRequiredLances.setRowSorter(sorter);
137132

138133
tblRequiredLances.setIntercellSpacing(new Dimension(0, 0));
@@ -148,9 +143,7 @@ public Component getTableCellRendererComponent(JTable table, Object value,
148143
column.setPreferredWidth(rlModel.getColumnWidth(i));
149144
column.setCellRenderer(new MekHqTableCellRenderer() {
150145
@Override
151-
public Component getTableCellRendererComponent(JTable table, Object value,
152-
boolean isSelected, boolean hasFocus,
153-
int row, int column) {
146+
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
154147
switch (column) {
155148
case LanceAssignmentTableModel.COL_FORCE:
156149
if (null != value) {
@@ -169,7 +162,7 @@ public Component getTableCellRendererComponent(JTable table, Object value,
169162
break;
170163
default:
171164
super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
172-
}
165+
}
173166
return this;
174167
}
175168
});
@@ -191,13 +184,13 @@ public boolean include(Entry<? extends LanceAssignmentTableModel, ? extends Inte
191184
}
192185
};
193186
final NaturalOrderComparator noc = new NaturalOrderComparator();
194-
TableRowSorter<LanceAssignmentTableModel>laSorter = new TableRowSorter<>(laModel);
187+
TableRowSorter<LanceAssignmentTableModel> laSorter = new TableRowSorter<>(laModel);
195188
laSorter.setRowFilter(laFilter);
196189
laSorter.setComparator(LanceAssignmentTableModel.COL_FORCE, forceComparator);
197-
laSorter.setComparator(LanceAssignmentTableModel.COL_CONTRACT, (c1, c2) ->
198-
noc.compare(((AtBContract) c1).getName(), ((AtBContract) c2).getName()));
199-
laSorter.setComparator(LanceAssignmentTableModel.COL_ROLE, (r1, r2) ->
200-
noc.compare(r1.toString(), r2.toString()));
190+
laSorter.setComparator(LanceAssignmentTableModel.COL_CONTRACT,
191+
(c1, c2) -> noc.compare(((AtBContract) c1).getName(), ((AtBContract) c2).getName()));
192+
laSorter.setComparator(LanceAssignmentTableModel.COL_ROLE,
193+
(r1, r2) -> noc.compare(r1.toString(), r2.toString()));
201194
List<SortKey> sortKeys = new ArrayList<>();
202195
sortKeys.add(new SortKey(LanceAssignmentTableModel.COL_FORCE, SortOrder.ASCENDING));
203196
sorter.setSortKeys(sortKeys);
@@ -214,12 +207,12 @@ public boolean include(Entry<? extends LanceAssignmentTableModel, ? extends Inte
214207
add(panRequiredLances);
215208

216209
int cmdrStrategy = 0;
217-
if ((campaign.getFlaggedCommander() != null)
218-
&& (campaign.getFlaggedCommander().getSkill(SkillType.S_STRATEGY) != null)) {
210+
if ((campaign.getFlaggedCommander() != null) &&
211+
(campaign.getFlaggedCommander().getSkill(SkillType.S_STRATEGY) != null)) {
219212
cmdrStrategy = campaign.getFlaggedCommander().getSkill(SkillType.S_STRATEGY).getLevel();
220213
}
221214
int maxDeployedLances = campaign.getCampaignOptions().getBaseStrategyDeployment() +
222-
campaign.getCampaignOptions().getAdditionalStrategyDeployment() * cmdrStrategy;
215+
campaign.getCampaignOptions().getAdditionalStrategyDeployment() * cmdrStrategy;
223216
add(new JLabel("Maximum Deployed Forces: " + maxDeployedLances));
224217

225218
panAssignments = new JPanel();
@@ -241,8 +234,8 @@ public void refresh() {
241234
}
242235
AtBContract defaultContract = activeContracts.isEmpty() ? null : activeContracts.get(0);
243236
for (CombatTeam combatTeam : campaign.getCombatTeamsTable().values()) {
244-
if ((combatTeam.getContract(campaign) == null)
245-
|| !combatTeam.getContract(campaign).isActiveOn(campaign.getLocalDate(), true)) {
237+
if ((combatTeam.getContract(campaign) == null) ||
238+
!combatTeam.getContract(campaign).isActiveOn(campaign.getLocalDate(), true)) {
246239
combatTeam.setContract(defaultContract);
247240
}
248241
}
@@ -307,8 +300,8 @@ class RequiredLancesTableModel extends DataTableModel {
307300
public RequiredLancesTableModel(final Campaign campaign) {
308301
this.campaign = campaign;
309302
data = new ArrayList<AtBContract>();
310-
columnNames = new String[]{"Contract", "Total", MANEUVER.toString(), FRONTLINE.toString(),
311-
PATROL.toString(), TRAINING.toString()};
303+
columnNames = new String[] { "Contract", "Total", MANEUVER.toString(), FRONTLINE.toString(), PATROL.toString(),
304+
TRAINING.toString() };
312305
}
313306

314307
@Override
@@ -363,9 +356,13 @@ public Object getValueAt(int row, int column) {
363356
if (column == COL_TOTAL) {
364357
int t = 0;
365358
for (CombatTeam combatTeam : campaign.getAllCombatTeams()) {
366-
if (data.get(row).equals(combatTeam.getContract(campaign))
367-
&& (combatTeam.getRole() != CombatRole.RESERVE)
368-
&& combatTeam.isEligible(campaign)) {
359+
AtBContract assignedContract = combatTeam.getContract(campaign);
360+
boolean isCadreDuty = assignedContract.getContractType().isCadreDuty();
361+
CombatRole role = combatTeam.getRole();
362+
boolean isRoleSuitable = (isCadreDuty && role.isTraining()) || role.isCombatRole();
363+
boolean isDeploymentEligible = combatTeam.isEligible(campaign);
364+
365+
if ((data.get(row).equals(assignedContract)) && isRoleSuitable && isDeploymentEligible) {
369366
t++;
370367
}
371368
}
@@ -376,9 +373,10 @@ public Object getValueAt(int row, int column) {
376373
} else if (contract.getContractType().getRequiredCombatRole().ordinal() == column - 2) {
377374
int t = 0;
378375
for (CombatTeam combatTeam : campaign.getAllCombatTeams()) {
379-
if (data.get(row).equals(combatTeam.getContract(campaign))
380-
&& (combatTeam.getRole() == combatTeam.getContract(campaign).getContractType().getRequiredCombatRole())
381-
&& combatTeam.isEligible(campaign)) {
376+
if (data.get(row).equals(combatTeam.getContract(campaign)) &&
377+
(combatTeam.getRole() ==
378+
combatTeam.getContract(campaign).getContractType().getRequiredCombatRole()) &&
379+
combatTeam.isEligible(campaign)) {
382380
t++;
383381
}
384382
}
@@ -405,7 +403,7 @@ class LanceAssignmentTableModel extends DataTableModel {
405403
public LanceAssignmentTableModel(Campaign campaign) {
406404
this.campaign = campaign;
407405
data = new ArrayList<>();
408-
columnNames = new String[]{"Force", "Weight Class", "Mission", "Role"};
406+
columnNames = new String[] { "Force", "Weight Class", "Mission", "Role" };
409407
}
410408

411409
@Override
@@ -447,7 +445,7 @@ public CombatTeam getRow(int row) {
447445

448446
@Override
449447
public Object getValueAt(int row, int column) {
450-
final String[] WEIGHT_CODES = {"Ultra-Light", "Light", "Medium", "Heavy", "Assault", "Super Heavy"};
448+
final String[] WEIGHT_CODES = { "Ultra-Light", "Light", "Medium", "Heavy", "Assault", "Super Heavy" };
451449

452450
if (row >= getRowCount()) {
453451
return "";

0 commit comments

Comments
 (0)