diff --git a/JotunnModStub/Dvergar/DvergarConfig.cs b/JotunnModStub/Dvergar/DvergarConfig.cs index c4e1080..e891051 100644 --- a/JotunnModStub/Dvergar/DvergarConfig.cs +++ b/JotunnModStub/Dvergar/DvergarConfig.cs @@ -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. diff --git a/JotunnModStub/Dvergar/DvergarPatches.cs b/JotunnModStub/Dvergar/DvergarPatches.cs index 6953737..94ae69e 100644 --- a/JotunnModStub/Dvergar/DvergarPatches.cs +++ b/JotunnModStub/Dvergar/DvergarPatches.cs @@ -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()); diff --git a/JotunnModStub/Dvergar/DvergarRace.cs b/JotunnModStub/Dvergar/DvergarRace.cs index def4d70..9b55b9b 100644 --- a/JotunnModStub/Dvergar/DvergarRace.cs +++ b/JotunnModStub/Dvergar/DvergarRace.cs @@ -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(); + if (visEq != null) Traverse.Create(visEq).Method("UpdateColors").GetValue(); + } + DvergarAbilities.Tick(player, state); } } diff --git a/JotunnModStub/Dvergar/DvergarVisuals.cs b/JotunnModStub/Dvergar/DvergarVisuals.cs index a1dc77f..6015658 100644 --- a/JotunnModStub/Dvergar/DvergarVisuals.cs +++ b/JotunnModStub/Dvergar/DvergarVisuals.cs @@ -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; + + /// + /// How many stars the Dvergar's colouring has earned: one at Bonemass, two at Fader. + /// + public static int GetStarTier(int state) + { + if (state >= DvergarConfig.TwoStarState) return 2; + if (state >= DvergarConfig.OneStarState) return 1; + return 0; + } + + /// + /// 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. + /// + 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); + } + + /// + /// 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. + /// + private static int GetProgressionState(Player player) + { + ZNetView nview = player != null ? player.GetComponent() : 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(DvergarConfig.DvergerPrefabName); + LevelEffects effects = prefab != null ? prefab.GetComponentInChildren(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; + } + + /// + /// 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. + /// + 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 ─────────────────────────────────────────────────────────── /// diff --git a/JotunnModStub/Skeleton/SkeletonPatches.cs b/JotunnModStub/Skeleton/SkeletonPatches.cs index 61a92a5..40d9043 100644 --- a/JotunnModStub/Skeleton/SkeletonPatches.cs +++ b/JotunnModStub/Skeleton/SkeletonPatches.cs @@ -244,19 +244,22 @@ namespace RacesOfValheim if (!__instance.m_isPlayer) return; Player p = __instance.GetComponent(); if (p == null) return; - ZNetView nview = p.GetComponent(); - 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}"; diff --git a/JotunnModStub/Skeleton/SkeletonVisuals.cs b/JotunnModStub/Skeleton/SkeletonVisuals.cs index 75f9e7e..cc46787 100644 --- a/JotunnModStub/Skeleton/SkeletonVisuals.cs +++ b/JotunnModStub/Skeleton/SkeletonVisuals.cs @@ -16,6 +16,55 @@ namespace RacesOfValheim /// public static class SkeletonVisuals { + public const string RaceId = "Skeleton"; + + /// + /// 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. + /// + public static string GetPlayerRace(Player player) + { + if (player == null) return "Human"; + + ZNetView nview = player.GetComponent(); + 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; + + /// + /// 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. + /// + public static int GetProgressionState(Player player) + { + ZNetView nview = player != null ? player.GetComponent() : 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 =