From 3100b507fd5c0013a9345e25060a9e9d69125b73 Mon Sep 17 00:00:00 2001 From: IllianiCBT Date: Sun, 24 Nov 2024 18:40:08 -0600 Subject: [PATCH] Added Better Handling for Negative Skill Levels Inserted bounds checks using `max` to prevent negative values in experience calculations. Understandably, the rules in CamOps do not account for characters with -14 skill target numbers. --- .../AverageExperienceRating.java | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/MekHQ/src/mekhq/campaign/rating/CamOpsReputation/AverageExperienceRating.java b/MekHQ/src/mekhq/campaign/rating/CamOpsReputation/AverageExperienceRating.java index a74ed5a1e6..74e3312904 100644 --- a/MekHQ/src/mekhq/campaign/rating/CamOpsReputation/AverageExperienceRating.java +++ b/MekHQ/src/mekhq/campaign/rating/CamOpsReputation/AverageExperienceRating.java @@ -19,11 +19,7 @@ package mekhq.campaign.rating.CamOpsReputation; import megamek.codeUtilities.MathUtility; -import megamek.common.Crew; -import megamek.common.Entity; -import megamek.common.Infantry; -import megamek.common.Jumpship; -import megamek.common.ProtoMek; +import megamek.common.*; import megamek.common.enums.SkillLevel; import megamek.logging.MMLogger; import mekhq.campaign.Campaign; @@ -31,6 +27,8 @@ import mekhq.campaign.personnel.SkillType; import mekhq.campaign.unit.Unit; +import static java.lang.Math.max; + public class AverageExperienceRating { private static final MMLogger logger = MMLogger.create(AverageExperienceRating.class); @@ -157,7 +155,7 @@ private static int calculateAverageExperienceRating(Campaign campaign, boolean l } else if (entity instanceof ProtoMek) { // ProtoMek entities only use gunnery for calculation if (person.hasSkill(SkillType.S_GUN_PROTO)) { - totalExperience += person.getSkill(SkillType.S_GUN_PROTO).getFinalSkillValue(); + totalExperience += max(0, person.getSkill(SkillType.S_GUN_PROTO).getFinalSkillValue()); } personnelCount++; @@ -209,8 +207,8 @@ private static int calculateAverageExperienceRating(Campaign campaign, boolean l */ private static double calculateInfantryExperience(Infantry infantry, Crew crew) { // Average of gunnery and antiMek skill - int gunnery = crew.getGunnery(); - int antiMek = infantry.getAntiMekSkill(); + int gunnery = max(0, crew.getGunnery()); + int antiMek = max(0, infantry.getAntiMekSkill()); return (double) (gunnery + antiMek) / 2; } @@ -231,13 +229,13 @@ private static double calculateRegularExperience(Person person, Entity entity, U if (unit.isDriver(person)) { skillType = SkillType.getDrivingSkillFor(entity); - skillValue += person.getSkill(skillType).getFinalSkillValue(); + skillValue += max(0, person.getSkill(skillType).getFinalSkillValue()); skillCount++; } if (unit.isGunner(person)) { skillType = SkillType.getGunnerySkillFor(entity); - skillValue += person.getSkill(skillType).getFinalSkillValue(); + skillValue += max(0, person.getSkill(skillType).getFinalSkillValue()); skillCount++; }