From b94733646ecd6cc143293d4e33199dbae02a330c Mon Sep 17 00:00:00 2001 From: Weaver Date: Sun, 20 Oct 2024 13:12:43 -0700 Subject: [PATCH 1/2] Force.setScenarioId now affects its units, redundant code elsewhere removed --- MekHQ/src/mekhq/campaign/Campaign.java | 20 +++---------------- MekHQ/src/mekhq/campaign/force/Force.java | 14 +++++++++---- .../storypoint/ScenarioStoryPoint.java | 8 +------- .../stratcon/StratconRulesManager.java | 2 +- .../campaign/stratcon/StratconScenario.java | 5 +++-- MekHQ/src/mekhq/gui/CampaignGUI.java | 2 +- .../mekhq/gui/adapter/TOEMouseAdapter.java | 8 +------- .../dialog/ForceTemplateAssignmentDialog.java | 2 +- .../gui/stratcon/StratconScenarioWizard.java | 2 +- 9 files changed, 22 insertions(+), 41 deletions(-) diff --git a/MekHQ/src/mekhq/campaign/Campaign.java b/MekHQ/src/mekhq/campaign/Campaign.java index ac638a50d4..eccafcfa45 100644 --- a/MekHQ/src/mekhq/campaign/Campaign.java +++ b/MekHQ/src/mekhq/campaign/Campaign.java @@ -896,7 +896,7 @@ public void addForce(Force force, Force superForce) { int id = lastForceId + 1; force.setId(id); superForce.addSubForce(force, true); - force.setScenarioId(superForce.getScenarioId()); + force.setScenarioId(superForce.getScenarioId(), this); forceIds.put(id, force); lastForceId = id; @@ -917,15 +917,7 @@ public void moveForce(Force force, Force superForce) { } superForce.addSubForce(force, true); - force.setScenarioId(superForce.getScenarioId()); - - for (Object o : force.getAllChildren(this)) { - if (o instanceof Unit) { - ((Unit) o).setScenarioId(superForce.getScenarioId()); - } else if (o instanceof Force) { - ((Force) o).setScenarioId(superForce.getScenarioId()); - } - } + force.setScenarioId(superForce.getScenarioId(), this); // repopulate formation levels across the TO&E Force.populateFormationLevelsFromOrigin(this); @@ -3697,14 +3689,8 @@ && getLocation().getJumpPath().getLastSystem().getId().equals(contract.getSystem } if (!forceUnderRepair) { - forceIds.get(forceId).setScenarioId(s.getId()); + forceIds.get(forceId).setScenarioId(s.getId(), this); s.addForces(forceId); - for (UUID uid : forceIds.get(forceId).getAllUnits(true)) { - Unit u = getHangar().getUnit(uid); - if (u != null) { - u.setScenarioId(s.getId()); - } - } addReport(MessageFormat.format( resources.getString("atbScenarioTodayWithForce.format"), diff --git a/MekHQ/src/mekhq/campaign/force/Force.java b/MekHQ/src/mekhq/campaign/force/Force.java index 008822a834..6fb6ce545a 100644 --- a/MekHQ/src/mekhq/campaign/force/Force.java +++ b/MekHQ/src/mekhq/campaign/force/Force.java @@ -183,10 +183,16 @@ public int getScenarioId() { return scenarioId; } - public void setScenarioId(int i) { - this.scenarioId = i; + public void setScenarioId(int scenarioId, Campaign campaign) { + this.scenarioId = scenarioId; for (Force sub : getSubForces()) { - sub.setScenarioId(i); + sub.setScenarioId(scenarioId, campaign); + } + for (UUID uid : getUnits()) { + Unit unit = campaign.getUnit(uid); + if (null != unit) { + unit.setScenarioId(scenarioId); + } } } @@ -410,7 +416,7 @@ public void clearScenarioIds(Campaign c, boolean killSub) { c.getScenario(getScenarioId()).addUnit(uid); } } - setScenarioId(-1); + setScenarioId(-1,c); } public int getId() { diff --git a/MekHQ/src/mekhq/campaign/storyarc/storypoint/ScenarioStoryPoint.java b/MekHQ/src/mekhq/campaign/storyarc/storypoint/ScenarioStoryPoint.java index 947dcd0066..bf7d6b11f6 100644 --- a/MekHQ/src/mekhq/campaign/storyarc/storypoint/ScenarioStoryPoint.java +++ b/MekHQ/src/mekhq/campaign/storyarc/storypoint/ScenarioStoryPoint.java @@ -95,13 +95,7 @@ public void start() { Force force = getCampaign().getForce(deployedForceId); if (null != force) { scenario.addForces(force.getId()); - force.setScenarioId(scenario.getId()); - for (UUID uid : force.getAllUnits(true)) { - Unit u = getCampaign().getUnit(uid); - if (null != u) { - u.setScenarioId(scenario.getId()); - } - } + force.setScenarioId(scenario.getId(), getCampaign()); } } } diff --git a/MekHQ/src/mekhq/campaign/stratcon/StratconRulesManager.java b/MekHQ/src/mekhq/campaign/stratcon/StratconRulesManager.java index a1b6ff1c53..28dce4c76b 100644 --- a/MekHQ/src/mekhq/campaign/stratcon/StratconRulesManager.java +++ b/MekHQ/src/mekhq/campaign/stratcon/StratconRulesManager.java @@ -723,7 +723,7 @@ public static void commitPrimaryForces(Campaign campaign, StratconScenario scena for (int forceID : scenario.getPlayerTemplateForceIDs()) { Force force = campaign.getForce(forceID); force.clearScenarioIds(campaign, true); - force.setScenarioId(scenario.getBackingScenarioID()); + force.setScenarioId(scenario.getBackingScenarioID(), campaign); } scenario.commitPrimaryForces(); diff --git a/MekHQ/src/mekhq/campaign/stratcon/StratconScenario.java b/MekHQ/src/mekhq/campaign/stratcon/StratconScenario.java index 2ddc5c7ae4..6ae8c6135a 100644 --- a/MekHQ/src/mekhq/campaign/stratcon/StratconScenario.java +++ b/MekHQ/src/mekhq/campaign/stratcon/StratconScenario.java @@ -17,6 +17,7 @@ import jakarta.xml.bind.annotation.adapters.XmlJavaTypeAdapter; import mekhq.MekHQ; import mekhq.adapter.DateAdapter; +import mekhq.campaign.Campaign; import mekhq.campaign.event.DeploymentChangedEvent; import mekhq.campaign.force.Force; import mekhq.campaign.mission.AtBDynamicScenario; @@ -91,10 +92,10 @@ public void addPrimaryForce(int forceID) { * Add a force to the backing scenario, trying to associate it with the given template. * Does some scenario and force house-keeping, fires a deployment changed event. */ - public void addForce(Force force, String templateID) { + public void addForce(Force force, String templateID, Campaign campaign) { if (!getBackingScenario().getForceIDs().contains(force.getId())) { backingScenario.addForce(force.getId(), templateID); - force.setScenarioId(getBackingScenarioID()); + force.setScenarioId(getBackingScenarioID(), campaign); MekHQ.triggerEvent(new DeploymentChangedEvent(force, getBackingScenario())); } } diff --git a/MekHQ/src/mekhq/gui/CampaignGUI.java b/MekHQ/src/mekhq/gui/CampaignGUI.java index 3f4abf8356..4c4febd789 100644 --- a/MekHQ/src/mekhq/gui/CampaignGUI.java +++ b/MekHQ/src/mekhq/gui/CampaignGUI.java @@ -2419,7 +2419,7 @@ public void undeployForce(Force f, boolean killSubs) { continue; } scenario.addForces(sub.getId()); - sub.setScenarioId(scenario.getId()); + sub.setScenarioId(scenario.getId(), getCampaign()); } prevId = parent.getId(); } diff --git a/MekHQ/src/mekhq/gui/adapter/TOEMouseAdapter.java b/MekHQ/src/mekhq/gui/adapter/TOEMouseAdapter.java index a27365809e..fae28bb097 100644 --- a/MekHQ/src/mekhq/gui/adapter/TOEMouseAdapter.java +++ b/MekHQ/src/mekhq/gui/adapter/TOEMouseAdapter.java @@ -361,13 +361,7 @@ public void actionPerformed(ActionEvent action) { force.clearScenarioIds(gui.getCampaign(), true); if (null != scenario) { scenario.addForces(force.getId()); - force.setScenarioId(scenario.getId()); - for (UUID uid : force.getAllUnits(true)) { - Unit u = gui.getCampaign().getUnit(uid); - if (null != u) { - u.setScenarioId(scenario.getId()); - } - } + force.setScenarioId(scenario.getId(), gui.getCampaign()); } MekHQ.triggerEvent(new DeploymentChangedEvent(force, scenario)); } diff --git a/MekHQ/src/mekhq/gui/dialog/ForceTemplateAssignmentDialog.java b/MekHQ/src/mekhq/gui/dialog/ForceTemplateAssignmentDialog.java index b58d2e4fd8..49c4db64e6 100644 --- a/MekHQ/src/mekhq/gui/dialog/ForceTemplateAssignmentDialog.java +++ b/MekHQ/src/mekhq/gui/dialog/ForceTemplateAssignmentDialog.java @@ -208,7 +208,7 @@ private void assignForceToTemplate() { // all this stuff apparently needs to happen when assigning a force to a scenario campaignGUI.undeployForce(force); force.clearScenarioIds(campaignGUI.getCampaign(), true); - force.setScenarioId(currentScenario.getId()); + force.setScenarioId(currentScenario.getId(),campaignGUI.getCampaign()); currentScenario.addForce(forceID, templateList.getSelectedValue().getForceName()); for (UUID uid : force.getAllUnits(true)) { Unit u = campaignGUI.getCampaign().getUnit(uid); diff --git a/MekHQ/src/mekhq/gui/stratcon/StratconScenarioWizard.java b/MekHQ/src/mekhq/gui/stratcon/StratconScenarioWizard.java index b305727c8e..de1869a70b 100644 --- a/MekHQ/src/mekhq/gui/stratcon/StratconScenarioWizard.java +++ b/MekHQ/src/mekhq/gui/stratcon/StratconScenarioWizard.java @@ -470,7 +470,7 @@ private void btnCommitClicked(ActionEvent e) { } } - currentScenario.addForce(force, templateID); + currentScenario.addForce(force, templateID, campaign); } } From d1690eff6d558fad73a49505c82e943eb44d9c41 Mon Sep 17 00:00:00 2001 From: Weaver Date: Sun, 20 Oct 2024 13:17:57 -0700 Subject: [PATCH 2/2] Force - add doc comment --- MekHQ/src/mekhq/campaign/force/Force.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/MekHQ/src/mekhq/campaign/force/Force.java b/MekHQ/src/mekhq/campaign/force/Force.java index 6fb6ce545a..ce602f978f 100644 --- a/MekHQ/src/mekhq/campaign/force/Force.java +++ b/MekHQ/src/mekhq/campaign/force/Force.java @@ -183,6 +183,11 @@ public int getScenarioId() { return scenarioId; } + /** + * Set scenario ID (e.g. deploy to scenario) for a force and all of its subforces and units + * @param scenarioId scenario to deploy to + * @param campaign campaign - required to update units + */ public void setScenarioId(int scenarioId, Campaign campaign) { this.scenarioId = scenarioId; for (Force sub : getSubForces()) {