From 11fd68757a37a5c3176fcd88d386dd0516367095 Mon Sep 17 00:00:00 2001 From: kanym Date: Tue, 4 Aug 2026 00:22:30 +0200 Subject: [PATCH] fix(skills): honour "no skill loss" for every skill, not just the level Death runs Skills.OnDeath, which is only LowerAllSkills(m_DeathLowerFactor * Game.m_skillReductionRate) and the world modifier works by driving that rate to zero. LowerAllSkills then takes nothing off m_level, as intended - but it also wipes m_accumulator on every skill and prints "$msg_skills_lowered" unconditionally, whatever the factor. So a death on a no-skill-loss world still quietly threw away all the progress banked toward the next level and still announced a loss. On a skill sitting just short of the next level that is indistinguishable from losing one, which is how this surfaced on Blood Magic - the skill the Aswid gains fastest, and so the one most often near a boundary. The guard is on the factor rather than on any race, so it holds for every skill and every race, and a world with a partial death penalty is left alone. It logs the factor and the world rate on each death, since a death that lowers nothing and a death that lowers something are otherwise identical from in-game. --- JotunnModStub/SkillLossFix.cs | 61 +++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 JotunnModStub/SkillLossFix.cs 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); + } + } +}