Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No Clone Method fixes. #6571

Merged
merged 3 commits into from
Apr 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 30 additions & 31 deletions MekHQ/src/mekhq/campaign/universe/RATGeneratorConnector.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import megamek.client.ratgenerator.FactionRecord;
import megamek.client.ratgenerator.MissionRole;
import megamek.client.ratgenerator.ModelRecord;
import megamek.client.ratgenerator.Parameters;
import megamek.client.ratgenerator.RATGenerator;
import megamek.client.ratgenerator.UnitTable;
import megamek.common.EntityMovementMode;
Expand All @@ -47,8 +48,7 @@
import megamek.logging.MMLogger;

/**
* Provides access to RATGenerator through the AbstractUnitGenerator and thus
* the IUnitGenerator interface.
* Provides access to RATGenerator through the AbstractUnitGenerator and thus the IUnitGenerator interface.
*
* @author Neoancient
*/
Expand All @@ -70,9 +70,8 @@ public RATGeneratorConnector(final int year) {
}

private @Nullable UnitTable findTable(final String faction, final int unitType, final int weightClass,
final int year, final int quality,
final Collection<EntityMovementMode> movementModes,
final Collection<MissionRole> missionRoles) {
final int year, final int quality, final Collection<EntityMovementMode> movementModes,
final Collection<MissionRole> missionRoles) {
final FactionRecord factionRecord = Factions.getInstance().getFactionRecordOrFallback(faction);
if (factionRecord == null) {
return null;
Expand All @@ -82,17 +81,25 @@ public RATGeneratorConnector(final int year) {
if (weightClass >= 0) {
weightClasses.add(weightClass);
}
return UnitTable.findTable(factionRecord, unitType, year, rating, weightClasses, ModelRecord.NETWORK_NONE,
movementModes, missionRoles, 2, factionRecord);
return UnitTable.findTable(factionRecord,
unitType,
year,
rating,
weightClasses,
ModelRecord.NETWORK_NONE,
movementModes,
missionRoles,
2,
factionRecord);
}

/**
* Helper function that extracts the string-based unit rating from the given
* int-based unit-rating
* for the given faction.
* Helper function that extracts the string-based unit rating from the given int-based unit-rating for the given
* faction.
*
* @param factionRecord Faction record
* @param quality Unit quality number
*
* @return Unit quality string
*/
public static String getFactionSpecificRating(final FactionRecord factionRecord, final int quality) {
Expand All @@ -116,29 +123,23 @@ public boolean isSupportedUnitType(final int unitType) {

@Override
public @Nullable MekSummary generate(final String faction, final int unitType, final int weightClass,
final int year, final int quality,
final Collection<EntityMovementMode> movementModes,
final Collection<MissionRole> missionRoles,
@Nullable Predicate<MekSummary> filter) {
final int year, final int quality, final Collection<EntityMovementMode> movementModes,
final Collection<MissionRole> missionRoles, @Nullable Predicate<MekSummary> filter) {
final UnitTable table = findTable(faction, unitType, weightClass, year, quality, movementModes, missionRoles);
return (table == null) ? null : table.generateUnit((filter == null) ? null : filter::test);
}

@Override
public List<MekSummary> generate(final int count, final String faction, final int unitType, final int weightClass,
final int year, final int quality,
final Collection<EntityMovementMode> movementModes,
final Collection<MissionRole> missionRoles,
@Nullable Predicate<MekSummary> filter) {
final int year, final int quality, final Collection<EntityMovementMode> movementModes,
final Collection<MissionRole> missionRoles, @Nullable Predicate<MekSummary> filter) {
final UnitTable table = findTable(faction, unitType, weightClass, year, quality, movementModes, missionRoles);
return (table == null) ? new ArrayList<>() : table.generateUnits(count, (filter == null) ? null : filter::test);
}

/**
* Generates a list of mek summaries from a RAT determined by the given faction,
* quality and other parameters.
* We force a fallback to try to ensure that something is generated if the
* parents have any possible units to generate,
* Generates a list of mek summaries from a RAT determined by the given faction, quality and other parameters. We
* force a fallback to try to ensure that something is generated if the parents have any possible units to generate,
* as that is the normally expected behaviour for MekHQ OpFor generation.
*
* @param count How many units to generate
Expand All @@ -148,15 +149,13 @@ public List<MekSummary> generate(final int count, final String faction, final in
public List<MekSummary> generate(final int count, final UnitGeneratorParameters parameters) {
final UnitTable table = findOpForTable(parameters);
return table.generateUnits(count,
(parameters.getFilter() == null) ? null : ms -> parameters.getFilter().test(ms));
(parameters.getFilter() == null) ? null : ms -> parameters.getFilter().test(ms));
}

/**
* Generates a single mek summary from a RAT determined by the given faction,
* quality and other parameters.
* We force a fallback to try to ensure that something is generated if the
* parents have any possible units to generate,
* as that is the normally expected behaviour for MekHQ OpFor generation.
* Generates a single mek summary from a RAT determined by the given faction, quality and other parameters. We force
* a fallback to try to ensure that something is generated if the parents have any possible units to generate, as
* that is the normally expected behaviour for MekHQ OpFor generation.
*
* @param parameters RATGenerator parameters
*/
Expand All @@ -167,15 +166,15 @@ public List<MekSummary> generate(final int count, final UnitGeneratorParameters
}

/**
* This finds a unit table for OpFor generation. It falls back using the parent
* faction to try to ensure there are
* This finds a unit table for OpFor generation. It falls back using the parent faction to try to ensure there are
* units in the unit table, so an OpFor is generated.
*
* @param unitParameters the base parameters to find the table using.
*
* @return the unit table to use in generating OpFor mek summaries
*/
private UnitTable findOpForTable(final UnitGeneratorParameters unitParameters) {
final UnitTable.Parameters parameters = unitParameters.getRATGeneratorParameters();
final Parameters parameters = unitParameters.getRATGeneratorParameters();
UnitTable table = UnitTable.findTable(parameters);
if (!table.hasUnits()) {
// Do Parent Factions Fallbacks to try to ensure units can be generated, at a
Expand Down
71 changes: 38 additions & 33 deletions MekHQ/src/mekhq/campaign/universe/UnitGeneratorParameters.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,16 @@
import megamek.client.ratgenerator.FactionRecord;
import megamek.client.ratgenerator.MissionRole;
import megamek.client.ratgenerator.ModelRecord;
import megamek.client.ratgenerator.UnitTable.Parameters;
import megamek.client.ratgenerator.Parameters;
import megamek.common.EntityMovementMode;
import megamek.common.MekSummary;
import mekhq.campaign.mission.AtBDynamicScenarioFactory;

/**
* Data structure that contains parameters relevant to unit generation via the
* IUnitGenerator interface
* and is capable of translating itself to
* megamek.client.ratgenerator.parameters
* Data structure that contains parameters relevant to unit generation via the IUnitGenerator interface and is capable
* of translating itself to megamek.client.ratgenerator.parameters
*
* @author NickAragua
*
*/
public class UnitGeneratorParameters {
private String faction;
Expand All @@ -69,32 +66,31 @@ public UnitGeneratorParameters() {
*/
@Override
public UnitGeneratorParameters clone() {
UnitGeneratorParameters newParams = new UnitGeneratorParameters();

newParams.setFaction(faction);
newParams.setUnitType(unitType);
newParams.setWeightClass(weightClass);
newParams.setYear(year);
newParams.setQuality(quality);
newParams.setFilter(filter);

Collection<EntityMovementMode> newModes = new ArrayList<>();
for (EntityMovementMode movementMode : movementModes) {
newModes.add(movementMode);
}

newParams.setMovementModes(newModes);

for (MissionRole missionRole : missionRoles) {
newParams.addMissionRole(missionRole);
try {
UnitGeneratorParameters unitGeneratorParameters = (UnitGeneratorParameters) super.clone();
unitGeneratorParameters.setFaction(faction);
unitGeneratorParameters.setUnitType(unitType);
unitGeneratorParameters.setWeightClass(weightClass);
unitGeneratorParameters.setYear(year);
unitGeneratorParameters.setQuality(quality);
unitGeneratorParameters.setFilter(filter);

Collection<EntityMovementMode> newModes = new ArrayList<>(movementModes);

unitGeneratorParameters.setMovementModes(newModes);

for (MissionRole missionRole : missionRoles) {
unitGeneratorParameters.addMissionRole(missionRole);
}

return unitGeneratorParameters;
} catch (CloneNotSupportedException e) {
return null;
}

return newParams;
}

/**
* Translate the contents of this data structure into a
* megamek.client.ratgenerator.Parameters object
* Translate the contents of this data structure into a megamek.client.ratgenerator.Parameters object
*
* @return
*/
Expand All @@ -107,11 +103,16 @@ public Parameters getRATGeneratorParameters() {
weightClasses.add(getWeightClass());
}

Parameters params = new Parameters(fRec, getUnitType(), getYear(), rating, weightClasses,
ModelRecord.NETWORK_NONE,
getMovementModes(), getMissionRoles(), 2, fRec);

return params;
return new Parameters(fRec,
getUnitType(),
getYear(),
rating,
weightClasses,
ModelRecord.NETWORK_NONE,
getMovementModes(),
getMissionRoles(),
2,
fRec);
}

public String getFaction() {
Expand Down Expand Up @@ -174,6 +175,10 @@ public void setMissionRoles(Collection<MissionRole> missionRoles) {
this.missionRoles = missionRoles;
}

/**
* @deprecated no indicated uses.
*/
@Deprecated(since = "0.50.05", forRemoval = true)
public void clearMissionRoles() {
missionRoles.clear();
}
Expand Down
Loading
Loading