Skip to content

Commit

Permalink
Merge pull request #5742 from IllianiCBT/clearUnits
Browse files Browse the repository at this point in the history
Updated 'Clear Units' Button to Correctly Handle StratCon Scenarios
  • Loading branch information
HammerGS authored Jan 15, 2025
2 parents 3650136 + bab4c1f commit 1aa1816
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 5 deletions.
5 changes: 3 additions & 2 deletions MekHQ/resources/mekhq/resources/CampaignGUI.properties
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,9 @@ btnPrintRS.toolTipText=Print record sheets for all currently assigned units.
btnPrintRS.text=Print Sheets
btnGetMul.toolTipText=Get a MUL file of all assigned units that can be loaded into MegaMek
btnGetMul.text=Export MUL File
btnClearAssignedUnits.toolTipText=Clear all assigned units for this scenario
btnClearAssignedUnits.text=Clear Units
btnClearAssignedUnits.toolTipText=Clear all assigned units for this scenario. Restricted to GM Mode\
\ if it is a scenario assigned to the Area of Operations.
btnClearAssignedUnits.text=Reset Deployment
btnResolveScenario.toolTipText=Bring up a wizard that will guide you through the process of resolving this scenario either by MUL files from a MegaMek game or by manually editing for tabletop games.
btnResolveScenario.text=Resolve Manually
btnAutoResolveScenario.toolTipText=<html>Start a game of MegaMek with all the assigned units played by bots.<br>At the game's conclusion, you will be presented with a series of dialogs for resolving the scenario.</html>
Expand Down
50 changes: 50 additions & 0 deletions MekHQ/src/mekhq/campaign/mission/AtBScenario.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@
import mekhq.campaign.rating.IUnitRating;
import mekhq.campaign.stratcon.StratconBiomeManifest;
import mekhq.campaign.stratcon.StratconBiomeManifest.MapTypeList;
import mekhq.campaign.stratcon.StratconCampaignState;
import mekhq.campaign.stratcon.StratconScenario;
import mekhq.campaign.stratcon.StratconTrackState;
import mekhq.campaign.unit.Unit;
import mekhq.campaign.universe.*;
import mekhq.utilities.MHQXMLUtility;
Expand Down Expand Up @@ -2116,4 +2119,51 @@ public String getDeploymentInstructions() {
public boolean canStartScenario(Campaign c) {
return c.getLocalDate().equals(getDate()) && super.canStartScenario(c);
}

/**
* Retrieves the {@link StratconScenario} associated with the current mission ID.
*
* <p>The method first retrieves the {@link AtBContract} from the given campaign.
* If the contract, its {@link StratconCampaignState}, or any required track data
* is unavailable, the method returns {@code null}. It iterates through all
* {@link StratconTrackState} objects in the campaign state and their associated scenarios.
* If a {@link StratconScenario} contains a non-null {@link AtBDynamicScenario} whose
* mission ID matches the current mission ID, it is returned.
*
* @param campaign the {@link Campaign} instance being queried for the scenario
* @return the matching {@link StratconScenario} if found, or {@code null} if no match
* is found or any required data is missing
* @throws NullPointerException if {@code campaign} is {@code null}
*/
public @Nullable StratconScenario getStratconScenario(Campaign campaign) {
// Get contract
AtBContract contract = getContract(campaign);
if (contract == null) {
return null;
}

// Fetch campaign state
StratconCampaignState campaignState = contract.getStratconCampaignState();
if (campaignState == null) {
return null;
}

// Find associated StratCon Scenario, if any
for (StratconTrackState track : campaignState.getTracks()) {
Collection<StratconScenario> trackScenarios = track.getScenarios().values();

for (StratconScenario scenario : trackScenarios) {
AtBDynamicScenario backingScenario = scenario.getBackingScenario();
if (backingScenario == null) {
continue;
}

if (backingScenario.getMissionId() == getMissionId()) {
return scenario;
}
}
}

return null;
}
}
29 changes: 26 additions & 3 deletions MekHQ/src/mekhq/gui/BriefingTab.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import mekhq.campaign.personnel.autoAwards.AutoAwardsController;
import mekhq.campaign.personnel.enums.PersonnelRole;
import mekhq.campaign.personnel.enums.PersonnelStatus;
import mekhq.campaign.stratcon.StratconScenario;
import mekhq.campaign.unit.Unit;
import mekhq.campaign.universe.Faction;
import mekhq.campaign.universe.Factions;
Expand Down Expand Up @@ -676,13 +677,27 @@ private void addScenario() {
}

private void clearAssignedUnits() {
if (0 == JOptionPane.showConfirmDialog(null, "Do you really want to remove all units from this scenario?",
if (0 == JOptionPane.showConfirmDialog(null,
"Do you really want to remove all units from this scenario?",
"Clear Units?", JOptionPane.YES_NO_OPTION)) {
int row = scenarioTable.getSelectedRow();
Scenario scenario = scenarioModel.getScenario(scenarioTable.convertRowIndexToModel(row));
if (null == scenario) {

if (scenario == null) {
return;
}

// This handles StratCon undeployment
if (scenario instanceof AtBScenario) {
StratconScenario stratConScenario = ((AtBScenario) scenario).getStratconScenario(getCampaign());

if (stratConScenario != null) {
stratConScenario.resetScenario(getCampaign());
return;
}
}

// This handles Legacy AtB undeployment
scenario.clearAllForcesAndPersonnel(getCampaign());
}
}
Expand Down Expand Up @@ -1282,11 +1297,19 @@ public void refreshScenarioView() {
SwingUtilities.invokeLater(() -> scrollScenarioView.getVerticalScrollBar().setValue(0));

final boolean canStartGame = scenario.canStartScenario(getCampaign());

btnStartGame.setEnabled(canStartGame);
btnJoinGame.setEnabled(canStartGame);
btnLoadGame.setEnabled(canStartGame);
btnGetMul.setEnabled(canStartGame);
btnClearAssignedUnits.setEnabled(canStartGame);

final boolean hasTrack = scenario.getHasTrack();
if (hasTrack) {
btnClearAssignedUnits.setEnabled(canStartGame && getCampaign().isGM());
} else {
btnClearAssignedUnits.setEnabled(canStartGame);
}

btnResolveScenario.setEnabled(canStartGame);
if (scenario instanceof AtBScenario) {
btnAutoResolveScenario.setEnabled(canStartGame);
Expand Down

0 comments on commit 1aa1816

Please sign in to comment.