add-dvergar-color-and-skeleton-menu #14

Merged
kanym merged 30 commits from add-dvergar-color-and-skeleton-menu into main 2026-08-05 22:08:39 +02:00
6 changed files with 189 additions and 7 deletions
Showing only changes of commit eabc1c54bf - Show all commits

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.
kanym 2026-08-05 19:27:34 +02:00

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>

View file

@ -244,19 +244,22 @@ namespace RacesOfValheim
if (!__instance.m_isPlayer) return;
Player p = __instance.GetComponent<Player>();
if (p == null) return;
ZNetView nview = p.GetComponent<ZNetView>();
if (nview == null || !nview.IsValid()) return;
string race = nview.GetZDO().GetString("RacesOfValheim_Race", "Human");
if (race == "Skeleton")
// Race and tier both come through SkeletonVisuals, which falls back to
// m_customData when there is no ZNetView. That is the whole of what was keeping
// the race off the main-menu character preview: the preview player carries its
// saved custom data but has no network view, so an unconditional ZDO read gave up
// before it could find out it was looking at a Skeleton.
string race = SkeletonVisuals.GetPlayerRace(p);
if (race == SkeletonVisuals.RaceId)
{
if (__instance.m_bodyModel != null)
{
// Ensure the base model is hidden
__instance.m_bodyModel.enabled = false;
int state = nview.GetZDO().GetInt("RacesOfValheim_SkeletonState", 0);
int state = SkeletonVisuals.GetProgressionState(p);
SkeletonProgression.SkeletonRules rules = SkeletonProgression.GetRules(state);
string targetBodyName = $"SkeletonBody_{rules.ModelPrefab}";

View file

@ -16,6 +16,55 @@ namespace RacesOfValheim
/// </summary>
public static class SkeletonVisuals
{
public const string RaceId = "Skeleton";
/// <summary>
/// Reads the race off the ZDO, falling back to m_customData.
///
/// The fallback is what makes the race show up on the main-menu character preview.
/// FejdStartup.SetupCharacterPreview instantiates a Player and calls
/// PlayerProfile.LoadPlayerData on it, which restores m_customData - so the preview does
/// know it is a Skeleton. What it has no business having is a ZNetView, and reading the
/// race from the ZDO alone meant the body swap gave up before it ever looked.
/// </summary>
public static string GetPlayerRace(Player player)
{
if (player == null) return "Human";
ZNetView nview = player.GetComponent<ZNetView>();
if (nview != null && nview.IsValid())
{
string race = nview.GetZDO().GetString("RacesOfValheim_Race", "");
if (!string.IsNullOrEmpty(race)) return race;
}
if (player.m_customData != null && player.m_customData.TryGetValue("Race", out string custom))
{
return custom;
}
return "Human";
}
public static bool IsSkeletonPlayer(Player player) => GetPlayerRace(player) == RaceId;
/// <summary>
/// The tier a given player's body should be built for. The ZDO carries it so remote
/// players render at their own standing; without one - the menu preview - fall back to
/// whatever this client can work out, which in the menu is the base tier, since there is
/// no world loaded to have killed anything.
/// </summary>
public static int GetProgressionState(Player player)
{
ZNetView nview = player != null ? player.GetComponent<ZNetView>() : null;
if (nview != null && nview.IsValid())
{
return nview.GetZDO().GetInt("RacesOfValheim_SkeletonState", 0);
}
return BossProgressionManager.GetCurrentProgressionState();
}
// Fire sources in preference order. The torch is an item every world has; the ground
// torch and the fire pit are the building pieces to fall back on.
private static readonly string[] FlameSourcePrefabs =