feat(skeleton): show the race on the main-menu character preview

The body swap read the race off the ZDO and gave up when there was none, which
is exactly the situation on the menu preview - FejdStartup instantiates a Player
and calls PlayerProfile.LoadPlayerData on it, so the preview knows its own custom
data and therefore its race, but it has no business owning a network view. It now
falls back to m_customData the way every other race in the mod already does, so a
Skeleton character looks like one before you press Start.

The tier falls back the same way. In the menu there is no world loaded to have
killed anything, so the preview is the base skeleton whatever the character has
done elsewhere.

feat(dvergar): age into a starred dverger's colours

One star at Bonemass, two at Fader.

Nothing is picked by eye. A starred creature in Valheim is not a second model: it
is the same one recoloured by LevelEffects, whose LevelSetup list holds a
hue/saturation/value offset per star. Those offsets are read off the Dverger
prefab at runtime and applied to the race's own slate blue-grey, so the tiers land
on the tints the game itself uses and keep following them if the prefab changes.

The tier comes from the ZDO key DvergarRace already publishes, so a remote Dvergar
is coloured at their own standing rather than the observer's - which matters as
soon as anyone runs personal progression.

Crossing a tier pushes a colour refresh through immediately, since the tint is
only written from the UpdateColors postfix and Valheim runs that on its own
schedule. Same treatment the health malus already gets a few lines above.

The hood is deliberately untouched.
This commit is contained in:
kanym 2026-08-05 19:27:34 +02:00
commit eabc1c54bf
6 changed files with 189 additions and 7 deletions

View file

@ -51,6 +51,17 @@ namespace RacesOfValheim
// Dvergr are white-haired. The hood hides most of it, but the beard shows.
public static readonly Color HairColor = new Color(0.85f, 0.87f, 0.92f, 1f);
// ─── Star colours ────────────────────────────────────────────────────
// A starred dverger is not a different model: Valheim recolours the same one with a
// LevelEffects component, whose LevelSetup list carries a hue/saturation/value offset per
// star and an optional emissive colour for the eyes. Setup 0 is one star, setup 1 is two.
//
// Nothing here hardcodes a colour. The offsets are read off the creature prefab at
// runtime, so the race ages into exactly the tints the game itself uses - and follows
// them if a patch ever changes them.
public const int OneStarState = 4; // Bonemass
public const int TwoStarState = 8; // Fader
// ─── Cosmetic hood ───────────────────────────────────────────────────
// The helmet slot itself stays free — whatever the player equips keeps all of its stats,
// only the mesh is swapped.

View file

@ -343,7 +343,10 @@ namespace RacesOfValheim
if (__instance.m_bodyModel != null && __instance.m_bodyModel.material != null)
{
__instance.m_bodyModel.material.SetColor("_SkinColor", DvergarConfig.SkinColor);
// Not the flat config colour any more: it carries the star shift a dverger of
// this standing would wear, so the race visibly ages at Bonemass and again at
// Fader.
__instance.m_bodyModel.material.SetColor("_SkinColor", DvergarVisuals.GetSkinColor(player));
}
TintRenderers(Traverse.Create(__instance).Field("m_hairItemInstance").GetValue<GameObject>());

View file

@ -18,6 +18,7 @@ namespace RacesOfValheim
// NaN so that the very first frame always counts as a change and pushes the malus through.
private float lastAppliedMalus = float.NaN;
private int lastAppliedStarTier = -1;
private void Awake()
{
@ -101,6 +102,18 @@ namespace RacesOfValheim
nview.GetZDO().Set(DvergarConfig.StateZdoKey, state);
}
// The skin tint is only written from the UpdateColors postfix, which Valheim runs on
// its own schedule - so crossing into a new star tier would otherwise not show until
// something else happened to refresh the visuals. Push one through, the same way the
// health malus above pushes a food update.
int starTier = DvergarVisuals.GetStarTier(state);
if (starTier != lastAppliedStarTier)
{
lastAppliedStarTier = starTier;
VisEquipment visEq = player.GetComponent<VisEquipment>();
if (visEq != null) Traverse.Create(visEq).Method("UpdateColors").GetValue();
}
DvergarAbilities.Tick(player, state);
}
}

View file

@ -47,6 +47,109 @@ namespace RacesOfValheim
public static bool IsDvergarPlayer(Player player) => GetPlayerRace(player) == DvergarConfig.RaceId;
// ─── Star colours ────────────────────────────────────────────────────
// Read once per session, failure included.
private static LevelEffects.LevelSetup[] cachedStarSetups;
private static bool starSetupsResolved;
/// <summary>
/// How many stars the Dvergar's colouring has earned: one at Bonemass, two at Fader.
/// </summary>
public static int GetStarTier(int state)
{
if (state >= DvergarConfig.TwoStarState) return 2;
if (state >= DvergarConfig.OneStarState) return 1;
return 0;
}
/// <summary>
/// The skin tint for a player at their current standing: the race's own slate blue-grey,
/// shifted by the hue/saturation/value offsets a real dverger of that star count wears.
///
/// Valheim does not give a starred creature a different model or a different colour - it
/// gives the same material an HSV offset through LevelEffects. Reading those offsets off
/// the creature prefab rather than picking two colours by eye means the tiers land on the
/// exact tints the game uses, and keeps following them if the prefab ever changes.
/// </summary>
public static Color GetSkinColor(Player player)
{
LevelEffects.LevelSetup setup = GetStarSetup(GetStarTier(GetProgressionState(player)));
if (setup == null) return DvergarConfig.SkinColor;
return ShiftHsv(DvergarConfig.SkinColor, setup.m_hue, setup.m_saturation, setup.m_value);
}
/// <summary>
/// The tier this particular Dvergar should be coloured for. DvergarRace publishes it to
/// the ZDO every frame, so a remote player is shown at their own standing rather than the
/// observer's - which matters the moment anyone runs personal progression. Falls back to
/// this client's own reckoning when there is no ZDO, which is the main-menu preview.
/// </summary>
private static int GetProgressionState(Player player)
{
ZNetView nview = player != null ? player.GetComponent<ZNetView>() : null;
if (nview != null && nview.IsValid())
{
int published = nview.GetZDO().GetInt(DvergarConfig.StateZdoKey, 0);
if (published > 0) return published;
}
return BossProgressionManager.GetCurrentProgressionState();
}
private static LevelEffects.LevelSetup GetStarSetup(int tier)
{
if (tier <= 0) return null;
LevelEffects.LevelSetup[] setups = ResolveStarSetups();
if (setups == null || setups.Length == 0) return null;
// m_levelSetups[0] is one star, [1] is two - the game indexes it as level - 2.
int index = Mathf.Min(tier - 1, setups.Length - 1);
return setups[index];
}
private static LevelEffects.LevelSetup[] ResolveStarSetups()
{
if (starSetupsResolved) return cachedStarSetups;
starSetupsResolved = true;
GameObject prefab = PrefabManager.Cache.GetPrefab<GameObject>(DvergarConfig.DvergerPrefabName);
LevelEffects effects = prefab != null ? prefab.GetComponentInChildren<LevelEffects>(true) : null;
if (effects == null || effects.m_levelSetups == null || effects.m_levelSetups.Count == 0)
{
Jotunn.Logger.LogWarning(
$"[Dvergar] No LevelEffects setups on '{DvergarConfig.DvergerPrefabName}'; " +
"star colouring stays at the base tint.");
return null;
}
cachedStarSetups = effects.m_levelSetups.ToArray();
Jotunn.Logger.LogInfo(
$"[Dvergar] Star colours resolved: {cachedStarSetups.Length} setups from " +
$"'{DvergarConfig.DvergerPrefabName}'.");
return cachedStarSetups;
}
/// <summary>
/// Applies an HSV offset the way the creature shader does - hue wraps, the other two
/// clamp - so the result is the same shift a starred dverger gets, on our colour.
/// </summary>
private static Color ShiftHsv(Color color, float hue, float saturation, float value)
{
Color.RGBToHSV(color, out float h, out float s, out float v);
h = Mathf.Repeat(h + hue, 1f);
s = Mathf.Clamp01(s + saturation);
v = Mathf.Clamp01(v + value);
Color shifted = Color.HSVToRGB(h, s, v);
shifted.a = color.a;
return shifted;
}
// ─── Scale ───────────────────────────────────────────────────────────
/// <summary>