Skip to content

Commit

Permalink
Merge pull request #4758 from Sleet01/Fix_MHQ_4755_npe_when_opfor_has…
Browse files Browse the repository at this point in the history
…_no_faction_code_MHQ_part

MHQ side of fix for MHQ 4755: NPE when opfor has no faction code
  • Loading branch information
Sleet01 authored Aug 31, 2024
2 parents d6d435c + e117cff commit b0bc1cc
Showing 1 changed file with 30 additions and 3 deletions.
33 changes: 30 additions & 3 deletions MekHQ/src/mekhq/gui/BriefingTab.java
Original file line number Diff line number Diff line change
Expand Up @@ -927,8 +927,10 @@ private void autoconfigureBotMunitions(AtBScenario scenario, List<Unit> chosen)
Game cGame = getCampaign().getGame();
boolean groundMap = scenario.getBoardType() == AtBScenario.T_GROUND;
boolean spaceMap = scenario.getBoardType() == AtBScenario.T_SPACE;
ArrayList<Entity> alliedEntities = new ArrayList<>();

ArrayList<String> allyFactionCodes = new ArrayList<>();
ArrayList<String> opforFactionCodes = new ArrayList<>();
String opforFactionCode = "IS";
String allyFaction = "IS";
int opforQuality = RATING_5;
Expand All @@ -938,12 +940,15 @@ private void autoconfigureBotMunitions(AtBScenario scenario, List<Unit> chosen)
// This had better be an AtB contract...
final Mission mission = comboMission.getSelectedItem();
if (mission instanceof AtBContract atbc) {
opforFactionCode = atbc.getEnemyCode();
opforFactionCode = (atbc.getEnemyCode().isBlank()) ? opforFactionCode : atbc.getEnemyCode();
opforQuality = atbc.getEnemyQuality();
allyFactionCodes.add(atbc.getEmployerCode());
allyFaction = atbc.getEmployerName(allowedYear);
} else {
allyFactionCodes.add(allyFaction);
}
Faction opforFaction = Factions.getInstance().getFaction(opforFactionCode);
opforFactionCodes.add(opforFactionCode);
boolean isPirate = opforFaction.isRebelOrPirate();

// Collect player units to use as configuration fodder
Expand All @@ -958,6 +963,7 @@ private void autoconfigureBotMunitions(AtBScenario scenario, List<Unit> chosen)
if (botForce.getName().contains(allyFaction)) {
// Stuff with our employer's name should be with us.
playerEntities.addAll(botForce.getFixedEntityList());
alliedEntities.addAll(botForce.getFixedEntityList());
} else {
int botTeam = botForce.getTeam();
if (!botTeamMappings.containsKey(botTeam)) {
Expand All @@ -967,10 +973,11 @@ private void autoconfigureBotMunitions(AtBScenario scenario, List<Unit> chosen)
}
}

// Configure generated units with appropriate munitions (for BV calcs)
TeamLoadoutGenerator tlg = new TeamLoadoutGenerator(cGame);

// Reconfigure each group separately so they only consider their own capabilities
for (ArrayList<Entity> entityList: botTeamMappings.values()) {
// Configure generated units with appropriate munitions (for BV calcs)
TeamLoadoutGenerator tlg = new TeamLoadoutGenerator(cGame);
// bin fill ratio will be adjusted by the loadout generator based on piracy and quality
ReconfigurationParameters rp = TeamLoadoutGenerator.generateParameters(
cGame,
Expand All @@ -988,6 +995,26 @@ private void autoconfigureBotMunitions(AtBScenario scenario, List<Unit> chosen)
MunitionTree mt = TeamLoadoutGenerator.generateMunitionTree(rp, entityList, "");
tlg.reconfigureEntities(entityList, opforFactionCode, mt, rp);
}

// Finally, reconfigure all allies (but not player entities) as one organization
ArrayList<Entity> allEnemyEntities = new ArrayList<>();
botTeamMappings.values().stream().forEach(x -> allEnemyEntities.addAll(x));
ReconfigurationParameters rp = TeamLoadoutGenerator.generateParameters(
cGame,
cGame.getOptions(),
alliedEntities,
allyFactionCodes.get(0),
allEnemyEntities,
opforFactionCodes,
opforQuality,
(getCampaign().getFaction().isPirate()) ? TeamLoadoutGenerator.UNSET_FILL_RATIO : 1.0f
);
rp.isPirate = getCampaign().getFaction().isPirate();
rp.groundMap = groundMap;
rp.spaceEnvironment = spaceMap;
MunitionTree mt = TeamLoadoutGenerator.generateMunitionTree(rp, alliedEntities, "");
tlg.reconfigureEntities(alliedEntities, allyFactionCodes.get(0), mt, rp);

}

private void joinScenario() {
Expand Down

0 comments on commit b0bc1cc

Please sign in to comment.