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

Fix #3767: NPE while scouting if AtB tries to generate enemy with SPAs not found in MM #3994

Merged
merged 2 commits into from
Apr 15, 2024
Merged
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
25 changes: 18 additions & 7 deletions MekHQ/src/mekhq/campaign/mission/CrewSkillUpgrader.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import megamek.common.options.OptionsConstants;
import mekhq.campaign.personnel.SpecialAbility;
import mekhq.campaign.personnel.enums.PersonnelRole;
import org.apache.logging.log4j.LogManager;

import java.util.*;

Expand All @@ -47,7 +48,7 @@ public class CrewSkillUpgrader {
*/
public CrewSkillUpgrader(int upgradeIntensity) {
this.upgradeIntensity = upgradeIntensity;

specialAbilitiesByUnitType = new HashMap<>();

for (SpecialAbility spa : SpecialAbility.getWeightedSpecialAbilities()) {
Expand Down Expand Up @@ -90,7 +91,7 @@ public void upgradeCrew(Entity entity) {
double skillAvg = (entity.getCrew().getGunnery() + entity.getCrew().getPiloting()) / 2.0;
double xpCap = 0;
int spaCap = 0;

// elite
if (skillAvg < 3) {
xpCap = maxAbilityXPCost;
Expand Down Expand Up @@ -165,14 +166,24 @@ private int addSingleSPA(Entity entity, double xpCap) {
spaValue = pickRandomWeapon(entity, true);
break;
default:
if ((entity.getCrew() == null) ||
(entity.getCrew().getOptions() == null) ||
(entity.getCrew().getOptions(spa.getName()) == null)) {
// If there's no crew, stop looking for SPAs
if (entity.getCrew() == null) {
return 0;
// If we can't access the option, try a different one
} else if ((entity.getCrew().getOptions() == null) ||
(entity.getCrew().getOptions(spa.getName()) == null) ||
(!entity.getCrew().getOptions(spa.getName()).hasMoreElements())) {
continue;
}

entity.getCrew().getOptions().getOption(spa.getName()).setValue(true);
return spa.getCost();
// If the option has a name but isn't defined, try another one
try {
entity.getCrew().getOptions().getOption(spa.getName()).setValue(true);
return spa.getCost();
} catch (NullPointerException e) {
LogManager.getLogger().warn("Attempted to assign SPA '" + spa.getName() + "' but SPA not found.");
continue;
}
}

// Assign the SPA, unless it was unable to pick a random weapon/specialization for whatever reason
Expand Down
Loading