diff --git a/JotunnModStub/SkillLossFix.cs b/JotunnModStub/SkillLossFix.cs new file mode 100644 index 0000000..0c44eae --- /dev/null +++ b/JotunnModStub/SkillLossFix.cs @@ -0,0 +1,61 @@ +using System.Reflection; +using HarmonyLib; + +namespace RacesOfValheim +{ + /// + /// Honours the world's "no skill loss" setting for every skill, without exception. + /// + /// Death runs Skills.OnDeath, which is only ever: + /// + /// LowerAllSkills(m_DeathLowerFactor * Game.m_skillReductionRate) + /// + /// and the world modifier works by driving m_skillReductionRate to zero. LowerAllSkills then + /// walks every skill and applies `m_level -= m_level * factor`, which at a factor of zero + /// takes nothing - but it also does two things unconditionally, whatever the factor: + /// + /// * it wipes m_accumulator on every skill, throwing away all progress toward the next + /// level, and + /// * it prints "$msg_skills_lowered". + /// + /// So on a no-skill-loss world a death still silently costs the progress banked toward the + /// next level in every skill and still announces a loss. On a skill sitting just short of the + /// next level that reads exactly like losing a level - which is how this surfaced, on Blood + /// Magic, the skill the Aswid gains fastest and so the one most likely to be near a boundary. + /// + /// Blocking the call outright when the factor is zero is what "no skill loss" should mean. + /// The guard is on the factor rather than on the race, so it holds for every skill and every + /// race, and a world with a partial death penalty is left completely alone. + /// + [HarmonyPatch(typeof(Skills), "LowerAllSkills")] + public static class Skills_LowerAllSkills_NoLossPatch + { + // Not public on Game, hence the reflection; resolved once rather than per death. + private static readonly FieldInfo SkillReductionRateField = + AccessTools.Field(typeof(Game), "m_skillReductionRate"); + + public static bool Prefix(Skills __instance, float factor) + { + Player player = Traverse.Create(__instance).Field("m_player").Value; + if (player == null || player != Player.m_localPlayer) return true; + + // Logged either way: a death that lowers nothing and a death that lowers something + // look identical from in-game, and this is the only place the difference is visible. + float worldRate = ReadWorldRate(); + Jotunn.Logger.LogInfo( + $"[SkillLoss] LowerAllSkills factor={factor} worldSkillReductionRate={worldRate} " + + $"bloodMagic={player.GetSkillLevel(Skills.SkillType.BloodMagic):0.##} " + + $"-> {(factor <= 0f ? "blocked" : "allowed")}"); + + return factor > 0f; + } + + private static float ReadWorldRate() + { + // Fail loud rather than silently: a rename in a game update should show up in the log + // as an obviously wrong number, not as a patch that quietly stops matching. + if (SkillReductionRateField == null) return float.NaN; + return (float)SkillReductionRateField.GetValue(null); + } + } +}