Skip to content

Commit

Permalink
Rename lances to strategicFormations and add force override
Browse files Browse the repository at this point in the history
Renamed the `lances` variable to `strategicFormations` throughout the codebase for clarity. Added functionality to override strategic formation assignment and updated relevant logic to consider this override. Enhanced the strategic formation eligibility check and adjusted UI components to reflect these changes.
  • Loading branch information
IllianiCBT committed Nov 25, 2024
1 parent 33ce1ac commit bc13555
Show file tree
Hide file tree
Showing 8 changed files with 199 additions and 90 deletions.
45 changes: 22 additions & 23 deletions MekHQ/src/mekhq/campaign/Campaign.java
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ public class Campaign implements ITechManager {

// hierarchically structured Force object to define TO&E
private Force forces;
private final Hashtable<Integer, StrategicFormation> lances; // AtB
private final Hashtable<Integer, StrategicFormation> strategicFormations; // AtB

private Faction faction;
private int techFactionCode;
Expand Down Expand Up @@ -309,7 +309,7 @@ public Campaign() {
setRankSystemDirect(Ranks.getRankSystemFromCode(Ranks.DEFAULT_SYSTEM_CODE));
forces = new Force(name);
forceIds.put(0, forces);
lances = new Hashtable<>();
strategicFormations = new Hashtable<>();
finances = new Finances();
astechPool = 0;
medicPool = 0;
Expand Down Expand Up @@ -449,15 +449,15 @@ public List<Force> getAllForces() {
}

public void importLance(StrategicFormation l) {
lances.put(l.getForceId(), l);
strategicFormations.put(l.getForceId(), l);
}

public Hashtable<Integer, StrategicFormation> getStrategicFormations() {
return lances;
return strategicFormations;
}

public ArrayList<StrategicFormation> getStrategicFormationList() {
return lances.values().stream()
return strategicFormations.values().stream()
.filter(l -> forceIds.containsKey(l.getForceId()))
.collect(Collectors.toCollection(ArrayList::new));
}
Expand Down Expand Up @@ -904,8 +904,8 @@ public void addForce(Force force, Force superForce) {
lastForceId = id;

if (campaignOptions.isUseAtB() && !force.getUnits().isEmpty()) {
if (null == lances.get(id)) {
lances.put(id, new StrategicFormation(force.getId(), this));
if (null == strategicFormations.get(id)) {
strategicFormations.put(id, new StrategicFormation(force.getId(), this));
}
}

Expand Down Expand Up @@ -1003,22 +1003,21 @@ public void addUnitToForce(@Nullable Unit u, int id) {

if (campaignOptions.isUseAtB()) {
if ((null != prevForce) && prevForce.getUnits().isEmpty()) {
lances.remove(prevForce.getId());
strategicFormations.remove(prevForce.getId());
}

if ((null == lances.get(id)) && (null != force)) {
lances.put(id, new StrategicFormation(force.getId(), this));
if ((null == strategicFormations.get(id)) && (null != force)) {
strategicFormations.put(id, new StrategicFormation(force.getId(), this));
}
}
}

/**
* Adds force and all its subforces to the AtB lance table
*/

private void addAllLances(Force force) {
if (!force.getUnits().isEmpty()) {
lances.put(force.getId(), new StrategicFormation(force.getId(), this));
if (force.isStrategicFormation()) {
strategicFormations.put(force.getId(), new StrategicFormation(force.getId(), this));
}
for (Force f : force.getSubForces()) {
addAllLances(f);
Expand Down Expand Up @@ -3618,7 +3617,7 @@ public int getDeploymentDeficit(AtBContract contract) {
int role = -Math.max(1, contract.getRequiredLances() / 2);

final AtBLanceRole requiredLanceRole = contract.getContractType().getRequiredLanceRole();
for (StrategicFormation l : lances.values()) {
for (StrategicFormation l : strategicFormations.values()) {
if (!l.getRole().isUnassigned() && (l.getMissionId() == contract.getId())) {
total++;
if (l.getRole() == requiredLanceRole) {
Expand Down Expand Up @@ -3718,7 +3717,7 @@ && getLocation().getJumpPath().getLastSystem().getId().equals(contract.getSystem
for (final AtBScenario s : contract.getCurrentAtBScenarios()) {
if ((s.getDate() != null) && s.getDate().equals(getLocalDate())) {
int forceId = s.getStrategicFormationId();
if ((lances.get(forceId) != null) && !forceIds.get(forceId).isDeployed()) {
if ((strategicFormations.get(forceId) != null) && !forceIds.get(forceId).isDeployed()) {
// If any unit in the force is under repair, don't deploy the force
// Merely removing the unit from deployment would break with user expectation
boolean forceUnderRepair = false;
Expand Down Expand Up @@ -4967,7 +4966,7 @@ public void removeForce(Force force) {
}

if (campaignOptions.isUseAtB()) {
lances.remove(fid);
strategicFormations.remove(fid);
}

if (null != force.getParentForce()) {
Expand Down Expand Up @@ -5020,7 +5019,7 @@ public void removeUnitFromForce(Unit u) {
}

if (campaignOptions.isUseAtB() && force.getUnits().isEmpty()) {
lances.remove(force.getId());
strategicFormations.remove(force.getId());
}
}
}
Expand Down Expand Up @@ -5595,14 +5594,14 @@ public void writeToXML(final PrintWriter pw) {
// CAW: implicit DEPENDS-ON to the <missions> node, do not move this above it
contractMarket.writeToXML(pw, indent);

if (!lances.isEmpty()) {
MHQXMLUtility.writeSimpleXMLOpenTag(pw, indent++, "lances");
for (StrategicFormation l : lances.values()) {
if (!strategicFormations.isEmpty()) {
MHQXMLUtility.writeSimpleXMLOpenTag(pw, indent++, "strategicFormations");
for (StrategicFormation l : strategicFormations.values()) {
if (forceIds.containsKey(l.getForceId())) {
l.writeToXML(pw, indent);
}
}
MHQXMLUtility.writeSimpleXMLCloseTag(pw, --indent, "lances");
MHQXMLUtility.writeSimpleXMLCloseTag(pw, --indent, "strategicFormations");
}
MHQXMLUtility.writeSimpleXMLTag(pw, indent, "shipSearchStart", getShipSearchStart());
MHQXMLUtility.writeSimpleXMLTag(pw, indent, "shipSearchType", shipSearchType);
Expand Down Expand Up @@ -6627,8 +6626,8 @@ public TargetRoll getTargetForAcquisition(final IAcquisitionWork acquisition,
}

public @Nullable AtBContract getAttachedAtBContract(Unit unit) {
if (null != unit && null != lances.get(unit.getForceId())) {
return lances.get(unit.getForceId()).getContract(this);
if (null != unit && null != strategicFormations.get(unit.getForceId())) {
return strategicFormations.get(unit.getForceId()).getContract(this);
}
return null;
}
Expand Down
41 changes: 37 additions & 4 deletions MekHQ/src/mekhq/campaign/force/Force.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,17 @@ public class Force {
// pathway to force icon
public static final int FORCE_NONE = -1;

public static final int STRATEGIC_FORMATION_OVERRIDE_NONE = -1;
public static final int STRATEGIC_FORMATION_OVERRIDE_FALSE = 0;
public static final int STRATEGIC_FORMATION_OVERRIDE_TRUE = 1;

private String name;
private StandardForceIcon forceIcon;
private Camouflage camouflage;
private String desc;
private boolean combatForce;
private boolean isStrategicFormation;
private int overrideStrategicFormation;
private FormationLevel formationLevel;
private FormationLevel overrideFormationLevel;
private Force parentForce;
Expand All @@ -94,6 +100,8 @@ public Force(String name) {
setCamouflage(new Camouflage());
setDescription("");
this.combatForce = true;
this.isStrategicFormation = false;
this.overrideStrategicFormation = STRATEGIC_FORMATION_OVERRIDE_NONE;
this.formationLevel = FormationLevel.NONE;
this.overrideFormationLevel = FormationLevel.NONE;
this.parentForce = null;
Expand Down Expand Up @@ -163,6 +171,22 @@ public void setCombatForce(boolean combatForce, boolean setForSubForces) {
}
}

public boolean isStrategicFormation() {
return isStrategicFormation;
}

public void setStrategicFormation(final boolean isStrategicFormation) {
this.isStrategicFormation = isStrategicFormation;
}

public int getOverrideStrategicFormation() {
return overrideStrategicFormation;
}

public void setOverrideStrategicFormation(final int overrideStrategicFormation) {
this.overrideStrategicFormation = overrideStrategicFormation;
}

public FormationLevel getFormationLevel() {
return formationLevel;
}
Expand Down Expand Up @@ -232,12 +256,18 @@ public boolean isDeployed() {
public List<Force> getAllParents() {
List<Force> parentForces = new ArrayList<>();

Force parentForce = getParentForce();
Force parentFormation = parentForce;

while (parentForce.getParentForce() != null) {
parentForces.add(parentForce.getParentForce());
if (parentForce != null) {
parentForces.add(parentForce);
}

while (parentFormation != null) {
parentFormation = parentFormation.getParentForce();

parentForce = parentForce.getParentForce();
if (parentFormation != null) {
parentForces.add(parentFormation);
}
}

return parentForces;
Expand Down Expand Up @@ -653,6 +683,7 @@ public void writeToXML(PrintWriter pw1, int indent) {
MHQXMLUtility.writeSimpleXMLTag(pw1, indent, "desc", desc);
}
MHQXMLUtility.writeSimpleXMLTag(pw1, indent, "combatForce", combatForce);
MHQXMLUtility.writeSimpleXMLTag(pw1, indent, "overrideStrategicFormation", overrideStrategicFormation);
MHQXMLUtility.writeSimpleXMLTag(pw1, indent, "formationLevel", formationLevel.toString());
MHQXMLUtility.writeSimpleXMLTag(pw1, indent, "populateOriginNode", overrideFormationLevel.toString());
MHQXMLUtility.writeSimpleXMLTag(pw1, indent, "scenarioId", scenarioId);
Expand Down Expand Up @@ -700,6 +731,8 @@ public void writeToXML(PrintWriter pw1, int indent) {
retVal.setDescription(wn2.getTextContent().trim());
} else if (wn2.getNodeName().equalsIgnoreCase("combatForce")) {
retVal.setCombatForce(Boolean.parseBoolean(wn2.getTextContent().trim()), false);
} else if (wn2.getNodeName().equalsIgnoreCase("overrideStrategicFormation")) {
retVal.setOverrideStrategicFormation(Integer.parseInt(wn2.getTextContent().trim()));
} else if (wn2.getNodeName().equalsIgnoreCase("formationLevel")) {
retVal.setFormationLevel(FormationLevel.parseFromString(wn2.getTextContent().trim()));
} else if (wn2.getNodeName().equalsIgnoreCase("populateOriginNode")) {
Expand Down
64 changes: 54 additions & 10 deletions MekHQ/src/mekhq/campaign/force/StrategicFormation.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,13 @@

import java.io.PrintWriter;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import static megamek.common.EntityWeightClass.WEIGHT_ULTRA_LIGHT;
import static mekhq.campaign.force.Force.STRATEGIC_FORMATION_OVERRIDE_NONE;
import static mekhq.campaign.force.Force.STRATEGIC_FORMATION_OVERRIDE_TRUE;

/**
* Used by Against the Bot & StratCon to track additional information about each force
* on the TO&amp;E that has at least one unit assigned. Extra info includes whether
Expand Down Expand Up @@ -206,9 +209,30 @@ public int getWeightClass(Campaign campaign) {
*/
double weight = calculateTotalWeight(campaign, forceId);

Force originForce = campaign.getForce(forceId);

if (originForce == null) {
return WEIGHT_ULTRA_LIGHT;
}

List<Force> subForces = originForce.getSubForces();
int subForcesCount = subForces.size();

for (Force childForce : subForces) {
double childForceWeight = calculateTotalWeight(campaign, childForce.getId());

if (childForceWeight > 0) {
weight += childForceWeight;
} else {
subForcesCount--;
}
}

weight = weight / subForcesCount;

weight = weight * 4.0 / getStdLanceSize(campaign.getFaction());
if (weight < 40) {
return EntityWeightClass.WEIGHT_ULTRA_LIGHT;
return WEIGHT_ULTRA_LIGHT;
}
if (weight <= 130) {
return EntityWeightClass.WEIGHT_LIGHT;
Expand All @@ -229,7 +253,12 @@ public boolean isEligible(Campaign campaign) {
// ensure the lance is marked as a combat force
final Force force = campaign.getForce(forceId);

if ((force == null) || !force.isCombatForce()) {
if (force == null) {
return false;
}

if (!force.isCombatForce()) {
force.setStrategicFormation(false);
return false;
}

Expand All @@ -241,12 +270,14 @@ public boolean isEligible(Campaign campaign) {
int size = getSize(campaign);
if (size < getStdLanceSize(campaign.getFaction()) - 1 ||
size > getStdLanceSize(campaign.getFaction()) + 2) {
force.setStrategicFormation(false);
return false;
}
}

if (campaign.getCampaignOptions().isLimitLanceWeight() &&
getWeightClass(campaign) > EntityWeightClass.WEIGHT_ASSAULT) {
force.setStrategicFormation(false);
return false;
}

Expand All @@ -258,6 +289,7 @@ size > getStdLanceSize(campaign.getFaction()) + 2) {

if (entity != null) {
if (entity.getUnitType() >= UnitType.JUMPSHIP) {
force.setStrategicFormation(false);
return false;
}
if ((entity.getEntityType() & ETYPE_GROUND) != 0) {
Expand All @@ -267,19 +299,28 @@ size > getStdLanceSize(campaign.getFaction()) + 2) {
}
}

if (hasGround) {
ArrayList<StrategicFormation> strategicFormations = campaign.getStrategicFormationList();
List<Integer> allLanceForceIds = new ArrayList<>();
int isOverridden = force.getOverrideStrategicFormation();
if (isOverridden != STRATEGIC_FORMATION_OVERRIDE_NONE) {
boolean overrideState = isOverridden == STRATEGIC_FORMATION_OVERRIDE_TRUE;
force.setStrategicFormation(overrideState);

List<Force> associatedForces = force.getAllParents();
associatedForces.addAll(force.getAllSubForces());

for (StrategicFormation strategicFormation : strategicFormations) {
allLanceForceIds.add(strategicFormation.getForceId());
for (Force associatedForce : associatedForces) {
associatedForce.setStrategicFormation(false);
}

return overrideState;
}

if (hasGround) {
// Parent Forces
List<Force> parentForces = force.getAllParents();

for (Force parentForce : parentForces) {
if (allLanceForceIds.contains(parentForce.getId())) {
if (parentForce.isStrategicFormation()) {
force.setStrategicFormation(false);
return false;
}
}
Expand All @@ -288,12 +329,15 @@ size > getStdLanceSize(campaign.getFaction()) + 2) {
List<Force> childForces = force.getAllSubForces();

for (Force childForce : childForces) {
if (allLanceForceIds.contains(childForce.getId())) {
if (childForce.isStrategicFormation()) {
force.setStrategicFormation(false);
return false;
}
}
}

force.setStrategicFormation(hasGround);

return hasGround;
}

Expand Down
Loading

0 comments on commit bc13555

Please sign in to comment.