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.
This commit is contained in:
kanym 2026-08-04 00:22:30 +02:00
commit 11fd68757a

View file

@ -0,0 +1,61 @@
using System.Reflection;
using HarmonyLib;
namespace RacesOfValheim
{
/// <summary>
/// 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.
/// </summary>
[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<Player>("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);
}
}
}