Skip to content

Commit

Permalink
Refactor Lance to StrategicFormation.
Browse files Browse the repository at this point in the history
Updated all references from `Lance` to `StrategicFormation` in multiple classes for clarity and better alignment with game terminology. This includes method names, variables, and comments to reflect the changes more accurately.
  • Loading branch information
IllianiCBT committed Nov 25, 2024
1 parent 6975c49 commit 33ce1ac
Show file tree
Hide file tree
Showing 24 changed files with 453 additions and 441 deletions.
54 changes: 18 additions & 36 deletions MekHQ/src/mekhq/AtBGameThread.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,47 +18,29 @@
*/
package mekhq;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.UUID;

import javax.swing.JOptionPane;

import io.sentry.Sentry;
import megamek.client.AbstractClient;
import megamek.client.Client;
import megamek.client.bot.BotClient;
import megamek.client.bot.princess.Princess;
import megamek.client.generator.RandomCallsignGenerator;
import megamek.client.ui.swing.ClientGUI;
import megamek.common.Entity;
import megamek.common.IAero;
import megamek.common.Infantry;
import megamek.common.MapSettings;
import megamek.common.Minefield;
import megamek.common.UnitType;
import megamek.common.*;
import megamek.common.planetaryconditions.PlanetaryConditions;
import megamek.logging.MMLogger;
import mekhq.campaign.force.Force;
import mekhq.campaign.force.Lance;
import mekhq.campaign.mission.AtBContract;
import mekhq.campaign.mission.AtBDynamicScenario;
import mekhq.campaign.mission.AtBScenario;
import mekhq.campaign.mission.BotForce;
import mekhq.campaign.mission.Scenario;
import mekhq.campaign.force.StrategicFormation;
import mekhq.campaign.mission.*;
import mekhq.campaign.personnel.Person;
import mekhq.campaign.unit.Unit;

import javax.swing.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.*;

/**
* Enhanced version of GameThread which imports settings and non-player units
* into the MM game
Expand Down Expand Up @@ -277,8 +259,8 @@ public void run() {
// Set scenario type-specific delay
deploymentRound = Math.max(entity.getDeployRound(), scenario.getDeploymentDelay() - speed);
// Lances deployed in scout roles always deploy units in 6-walking speed turns
if (scenario.getLanceRole().isScouting() && (scenario.getLance(campaign) != null)
&& (scenario.getLance(campaign).getForceId() == scenario.getLanceForceId())
if (scenario.getLanceRole().isScouting() && (scenario.getStrategicFormation(campaign) != null)
&& (scenario.getStrategicFormation(campaign).getForceId() == scenario.getStrategicFormationId())
&& !useDropship) {
deploymentRound = Math.max(deploymentRound, 6 - speed);
}
Expand Down Expand Up @@ -346,7 +328,7 @@ public void run() {
}
deploymentRound = Math.max(entity.getDeployRound(), scenario.getDeploymentDelay() - speed);
if (!useDropship && scenario.getLanceRole().isScouting()
&& (scenario.getLance(campaign).getForceId() == scenario.getLanceForceId())) {
&& (scenario.getStrategicFormation(campaign).getForceId() == scenario.getStrategicFormationId())) {
deploymentRound = Math.max(deploymentRound, 6 - speed);
}
}
Expand Down Expand Up @@ -537,12 +519,12 @@ protected List<Entity> setupBotEntities(BotClient botClient, BotForce botForce,
int lanceSize;

if (botForce.getTeam() == 2) {
lanceSize = Lance.getStdLanceSize(contract.getEnemy());
lanceSize = StrategicFormation.getStdLanceSize(contract.getEnemy());
} else {
lanceSize = Lance.getStdLanceSize(contract.getEmployerFaction());
lanceSize = StrategicFormation.getStdLanceSize(contract.getEmployerFaction());
}

Comparator<Entity> comp = Comparator.comparing(((Entity e) -> e.getEntityMajorTypeName(e.getEntityType())));
Comparator<Entity> comp = Comparator.comparing(((Entity e) -> Entity.getEntityMajorTypeName(e.getEntityType())));
comp = comp.thenComparing(((Entity e) -> e.getRunMP()), Comparator.reverseOrder());
comp = comp.thenComparing(((Entity e) -> e.getRole().toString()));
entitiesSorted.sort(comp);
Expand All @@ -553,13 +535,13 @@ protected List<Entity> setupBotEntities(BotClient botClient, BotForce botForce,
}

if ((i != 0)
&& !lastType.equals(entity.getEntityMajorTypeName(entity.getEntityType()))) {
&& !lastType.equals(Entity.getEntityMajorTypeName(entity.getEntityType()))) {
forceIdLance++;
lanceName = RCG.generate();
i = forceIdLance * lanceSize;
}

lastType = entity.getEntityMajorTypeName(entity.getEntityType());
lastType = Entity.getEntityMajorTypeName(entity.getEntityType());
entity.setOwner(botClient.getLocalPlayer());
String fName = String.format(forceName, lanceName, forceIdLance);
entity.setForceString(fName);
Expand Down
36 changes: 18 additions & 18 deletions MekHQ/src/mekhq/campaign/Campaign.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
import mekhq.campaign.finances.*;
import mekhq.campaign.finances.enums.TransactionType;
import mekhq.campaign.force.Force;
import mekhq.campaign.force.Lance;
import mekhq.campaign.force.StrategicFormation;
import mekhq.campaign.icons.StandardForceIcon;
import mekhq.campaign.icons.UnitIcon;
import mekhq.campaign.log.HistoricalLogEntry;
Expand Down 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, Lance> lances; // AtB
private final Hashtable<Integer, StrategicFormation> lances; // AtB

private Faction faction;
private int techFactionCode;
Expand Down Expand Up @@ -448,15 +448,15 @@ public List<Force> getAllForces() {
return new ArrayList<>(forceIds.values());
}

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

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

public ArrayList<Lance> getLanceList() {
public ArrayList<StrategicFormation> getStrategicFormationList() {
return lances.values().stream()
.filter(l -> forceIds.containsKey(l.getForceId()))
.collect(Collectors.toCollection(ArrayList::new));
Expand Down Expand Up @@ -905,7 +905,7 @@ public void addForce(Force force, Force superForce) {

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

Expand Down Expand Up @@ -1007,7 +1007,7 @@ public void addUnitToForce(@Nullable Unit u, int id) {
}

if ((null == lances.get(id)) && (null != force)) {
lances.put(id, new Lance(force.getId(), this));
lances.put(id, new StrategicFormation(force.getId(), this));
}
}
}
Expand All @@ -1018,7 +1018,7 @@ public void addUnitToForce(@Nullable Unit u, int id) {

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

final AtBLanceRole requiredLanceRole = contract.getContractType().getRequiredLanceRole();
for (Lance l : lances.values()) {
for (StrategicFormation l : lances.values()) {
if (!l.getRole().isUnassigned() && (l.getMissionId() == contract.getId())) {
total++;
if (l.getRole() == requiredLanceRole) {
Expand Down Expand Up @@ -3717,7 +3717,7 @@ && getLocation().getJumpPath().getLastSystem().getId().equals(contract.getSystem
// If there is a standard battle set for today, deploy the lance.
for (final AtBScenario s : contract.getCurrentAtBScenarios()) {
if ((s.getDate() != null) && s.getDate().equals(getLocalDate())) {
int forceId = s.getLanceForceId();
int forceId = s.getStrategicFormationId();
if ((lances.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
Expand Down Expand Up @@ -3776,11 +3776,11 @@ private void processNewDayATB() {
if (getLocalDate().getDayOfWeek() == DayOfWeek.MONDAY) {
processShipSearch();

// Training Experience - Award to eligible training lances on active contracts
getLances().values().stream()
.filter(lance -> lance.getRole().isTraining()
&& (lance.getContract(this) != null) && lance.isEligible(this)
&& lance.getContract(this).isActiveOn(getLocalDate(), true))
// Training Experience - Award to eligible training Strategic Formations on active contracts
getStrategicFormations().values().stream()
.filter(strategicFormation -> strategicFormation.getRole().isTraining()
&& (strategicFormation.getContract(this) != null) && strategicFormation.isEligible(this)
&& strategicFormation.getContract(this).isActiveOn(getLocalDate(), true))
.forEach(this::awardTrainingXP);
}

Expand Down Expand Up @@ -4841,9 +4841,9 @@ public void removePerson(final @Nullable Person person, final boolean log) {
* commanding officer and
* the minimum experience level of the unit's members.
*
* @param l The {@link Lance} to calculate XP to award for training.
* @param l The {@link StrategicFormation} to calculate XP to award for training.
*/
private void awardTrainingXP(final Lance l) {
private void awardTrainingXP(final StrategicFormation l) {
for (UUID trainerId : forceIds.get(l.getForceId()).getAllUnits(true)) {
Unit trainerUnit = getHangar().getUnit(trainerId);

Expand Down Expand Up @@ -5597,7 +5597,7 @@ public void writeToXML(final PrintWriter pw) {

if (!lances.isEmpty()) {
MHQXMLUtility.writeSimpleXMLOpenTag(pw, indent++, "lances");
for (Lance l : lances.values()) {
for (StrategicFormation l : lances.values()) {
if (forceIds.containsKey(l.getForceId())) {
l.writeToXML(pw, indent);
}
Expand Down
45 changes: 45 additions & 0 deletions MekHQ/src/mekhq/campaign/force/Force.java
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,28 @@ public boolean isDeployed() {
return parentForce;
}

/**
* This method generates a list of all parent forces for the current force object in the
* hierarchy. It repeatedly fetches the parent force of the current force and adds it to a list
* until no more parent forces can be found (i.e., until the top of the force hierarchy is reached).
*
* @return A list of {@link Force} objects representing all the parent forces of the current
* force object in the hierarchy. The list will be empty if there are no parent forces.
*/
public List<Force> getAllParents() {
List<Force> parentForces = new ArrayList<>();

Force parentForce = getParentForce();

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

parentForce = parentForce.getParentForce();
}

return parentForces;
}

public void setParentForce(final @Nullable Force parent) {
this.parentForce = parent;
}
Expand All @@ -229,6 +251,29 @@ public Vector<Force> getSubForces() {
return subForces;
}

/**
* Returns a list of all of this forces' descendant forces.
* This includes direct child forces and their descendents recursively.
* <p>
* This method works by first adding all direct child forces to the list, and
* then recursively adding their descendants by calling this method on each child
* force.
*
* @return A list of {@link Force} objects representing all descendant forces.
* If there are no descendant forces, this method will return an empty list.
*/
public List<Force> getAllSubForces() {
List<Force> allSubForces = new ArrayList<>();

for (Force subForce : subForces) {
allSubForces.add(subForce);

allSubForces.addAll(subForce.getAllSubForces());
}

return allSubForces;
}

public boolean isAncestorOf(Force otherForce) {
Force pForce = otherForce.getParentForce();
while (pForce != null) {
Expand Down
Loading

0 comments on commit 33ce1ac

Please sign in to comment.