Skip to content

Commit

Permalink
Refactored entity substitution logic in scenario generation
Browse files Browse the repository at this point in the history
Reorganized the entity substitution process to enhance code clarity and streamline entity generation based on the scenario and unit type. Removed redundant substitution attempts and ensured that fallback mechanisms are clearly defined for each scenario type, improving the maintainability of the code.
  • Loading branch information
IllianiCBT committed Dec 6, 2024
1 parent 0d490d6 commit f56ec2f
Showing 1 changed file with 32 additions and 45 deletions.
77 changes: 32 additions & 45 deletions MekHQ/src/mekhq/campaign/mission/AtBDynamicScenarioFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -3119,14 +3119,6 @@ private static List<Entity> generateLance(String faction, SkillLevel skill, int
continue;
}

entity = substituteEntity(faction, skill, quality, weights, rolesByType,
campaign, unitTypes, unitIndex, unitTypes);

if (entity != null) {
generatedEntities.add(entity);
continue;
}

String role = null;
Integer type = unitTypes.get(unitIndex);
if (type != null) {
Expand All @@ -3142,6 +3134,14 @@ private static List<Entity> generateLance(String faction, SkillLevel skill, int
EntityWeightClass.getClassName(AtBConfiguration.decodeWeightStr(weights, unitIndex)),
role != null ? "roles (" + role + ')' : ""));

entity = substituteEntity(faction, skill, quality, weights, rolesByType,
campaign, unitTypes, unitIndex, unitTypes);

if (entity != null) {
generatedEntities.add(entity);
continue;
}

// fallback unitType list container
List<Integer> fallbackUnitType = unitTypes;

Expand All @@ -3151,24 +3151,27 @@ private static List<Entity> generateLance(String faction, SkillLevel skill, int
entity = substituteEntity(faction, skill, quality, weights, rolesByType,
campaign, unitTypes, unitIndex, fallbackUnitType);
} else if (scenario.getBoardType() == T_GROUND) {
if (allowsTanks) {
if (unitTypes.get(unitIndex) != TANK) {
logger.info("Switching unit type to Tank");
fallbackUnitType = List.of(TANK);
if (allowsTanks && unitTypes.get(unitIndex) != TANK) {
logger.info("Switching unit type to Tank");
fallbackUnitType = List.of(TANK);

entity = substituteEntity(faction, skill, quality, weights, rolesByType,
campaign, unitTypes, unitIndex, fallbackUnitType);
}
entity = substituteEntity(faction, skill, quality, weights, rolesByType,
campaign, unitTypes, unitIndex, fallbackUnitType);
}

// Abandon attempts to generate
if (entity == null) {
entity = substituteEntity(faction, skill, quality, weights, null,
campaign, unitTypes, unitIndex, fallbackUnitType);
}
} else {
logger.info("Unable to generate Tank due to scenario limitations. Aborting" +
" attempt.");
continue;
if (unitTypes.get(unitIndex) != MEK) {
logger.info("Switching unit type to Mek");
fallbackUnitType = List.of(MEK);

entity = substituteEntity(faction, skill, quality, weights, rolesByType,
campaign, unitTypes, unitIndex, fallbackUnitType);
}

// Abandon attempts to generate by role
if (entity == null) {
logger.info("Removing role requirements.");
entity = substituteEntity(faction, skill, quality, weights, null,
campaign, unitTypes, unitIndex, unitTypes);
}
} else {
if (unitTypes.get(unitIndex) != AEROSPACEFIGHTER) {
Expand All @@ -3179,8 +3182,9 @@ private static List<Entity> generateLance(String faction, SkillLevel skill, int
campaign, unitTypes, unitIndex, fallbackUnitType);
}

// Abandon attempts to generate
// Abandon attempts to generate by role
if (entity == null) {
logger.info("Removing role requirements.");
entity = substituteEntity(faction, skill, quality, weights, null,
campaign, unitTypes, unitIndex, fallbackUnitType);
}
Expand Down Expand Up @@ -3234,27 +3238,10 @@ private static List<Entity> generateLance(String faction, SkillLevel skill, int
if (entity != null) {
logger.info("Substitution successful.");
return entity;
} else {
logger.info("Unable to substitute entity.");
return null;
}

logger.info("Substitution unsuccessful. Relaxing role requirements.");

entity = getNewEntity(faction, skill, quality, fallbackUnitType, weights, rolesByType,
campaign, unitIndex);

if (entity != null) {
return entity;
}

logger.info("That helped, cycling weights (final attempt).");
entity = attemptSubstitutionViaWeight(faction, skill, quality, weights,
null, campaign, unitTypes, unitIndex);

if (entity != null) {
return entity;
}

logger.info("Unable to substitute entity.");
return null;
}

/**
Expand Down

0 comments on commit f56ec2f

Please sign in to comment.