diff --git a/JotunnModStub/Dvergur/DvergurConfig.cs b/JotunnModStub/Dvergur/DvergurConfig.cs index 36d9146..dac8e68 100644 --- a/JotunnModStub/Dvergur/DvergurConfig.cs +++ b/JotunnModStub/Dvergur/DvergurConfig.cs @@ -35,21 +35,27 @@ namespace RacesOfValheim // is set to zero, and would bake the old value into any assembly built against this one. public static readonly float VisualScale = 0.70f; // -30% versus a human + // ─── Skin ──────────────────────────────────────────────────────────── + // Tint applied over the human skin texture, the same multiplicative m_skinColor the + // Furling and Trol use. Ashen grey with a blue cast, to read as one of the deep folk. + public static readonly Vector3 SkinColor = new Vector3(0.72f, 0.74f, 0.82f); + // ─── Cosmetic hood ─────────────────────────────────────────────────── // The helmet slot itself stays free — whatever the player equips keeps all of its stats, - // only the mesh is swapped. Tried in order; the first prefab that resolves wins. - public static readonly string[] HoodPrefabCandidates = - { - "HelmetDverger", // Dverger circlet item - "helmet_dverger", // its attach visual, if the item prefab is unavailable - "Dverger" // last resort: lift the headgear renderer off the creature - }; + // only the mesh is swapped. + // + // The hood is lifted straight off the Dvergr NPC rather than off the Dverger Circlet item: + // the circlet is a metal band, not the hood the race is meant to wear. + public const string DvergerPrefabName = "Dverger"; - // Valheim rigs are authored at scale 100, hence the very small numbers. Tuned against - // the head bone, same approach as the Furling trophy head. - public static readonly Vector3 HoodLocalPosition = new Vector3(0f, 0.0012f, 0f); - public static readonly Vector3 HoodLocalRotation = new Vector3(-90f, 0f, 90f); - public static readonly float HoodLocalScale = 0.0075f; + // Matched against renderer object names inside that prefab, lowercased, by Contains. + public static readonly string[] HoodRendererNames = { "hat", "hood", "helmet", "cap" }; + + // Applied on top of whatever the rig positions the hood at. Identity is correct when the + // bone remap succeeds; these exist so a stubborn mesh can be nudged without a code change. + public static readonly Vector3 HoodOffsetPosition = Vector3.zero; + public static readonly Vector3 HoodOffsetRotation = Vector3.zero; + public static readonly float HoodExtraScale = 1f; // ─── Health malus ──────────────────────────────────────────────────── diff --git a/JotunnModStub/Dvergur/DvergurPatches.cs b/JotunnModStub/Dvergur/DvergurPatches.cs index bfe4d2e..250aae1 100644 --- a/JotunnModStub/Dvergur/DvergurPatches.cs +++ b/JotunnModStub/Dvergur/DvergurPatches.cs @@ -14,17 +14,25 @@ namespace RacesOfValheim // ─── Health malus ──────────────────────────────────────────────────── /// - /// The Dvergur is a small frame, and carries less health for it. The target is resolved - /// with AccessTools rather than named against a type, because GetMaxHealth may be declared - /// on Character or overridden on Player depending on the game version - AccessTools walks - /// up to whichever one actually runs, and the local-player guard makes either correct. + /// The Dvergur is a small frame, and carries less health for it. + /// + /// The malus is taken off the food total rather than off Character.GetMaxHealth, which is + /// what the first attempt did and what produced the bug where the bar shrank but the + /// number stayed put and the border flashed. Patching the getter makes it lie to the rest + /// of the game: Character.SetMaxHealth is the only thing that clamps current health down + /// to the new maximum, and it never saw a lower number, so m_health stayed at 58. Worse, + /// Player.SetMaxHealth flashes the bar whenever the new maximum is above GetMaxHealth() - + /// against a getter that always read low, every food tick looked like a health *increase* + /// and re-triggered the flash. + /// + /// GetTotalFoodValue is upstream of both: UpdateFood feeds its result straight into + /// SetMaxHealth, so Valheim does its own clamping and its own flash logic on an honest + /// number and bar, value and animation all agree. /// - [HarmonyPatch] - public static class Dvergur_GetMaxHealth_Patch + [HarmonyPatch(typeof(Player), "GetTotalFoodValue")] + public static class Dvergur_GetTotalFoodValue_Patch { - public static MethodBase TargetMethod() => AccessTools.Method(typeof(Player), "GetMaxHealth"); - - public static void Postfix(Character __instance, ref float __result) + public static void Postfix(Player __instance, ref float hp) { if (__instance == null || __instance != Player.m_localPlayer) return; if (!DvergurRace.IsDvergur()) return; @@ -32,7 +40,26 @@ namespace RacesOfValheim float malus = DvergurProgression.Current.HealthMalus; if (malus <= 0f) return; - __result = Mathf.Max(DvergurConfig.MinMaxHealth, __result - malus); + hp = Mathf.Max(DvergurConfig.MinMaxHealth, hp - malus); + } + } + + // ─── Scale ─────────────────────────────────────────────────────────── + + /// + /// Sizing runs off Player.Update, which is where the Trol does it and the only place it + /// holds: Trol's own postfix forces every non-Trol player's visual back to scale 1 on the + /// same frame, so a scale applied from anywhere else was simply overwritten. Being a + /// postfix on the same method puts the Dvergur on equal footing, and RaceScale keeps the + /// two from undoing each other. + /// + [HarmonyPatch(typeof(Player), "Update")] + public static class Dvergur_PlayerUpdate_Scale_Patch + { + public static void Postfix(Player __instance) + { + if (__instance == null) return; + DvergurVisuals.ApplyScale(__instance, DvergurVisuals.IsDvergurPlayer(__instance)); } } @@ -290,7 +317,7 @@ namespace RacesOfValheim Player player = __instance.GetComponent(); if (player == null) return; - DvergurVisuals.Apply(player); + DvergurVisuals.ApplyHood(player); } } } diff --git a/JotunnModStub/Dvergur/DvergurRace.cs b/JotunnModStub/Dvergur/DvergurRace.cs index 319d58c..daa83c0 100644 --- a/JotunnModStub/Dvergur/DvergurRace.cs +++ b/JotunnModStub/Dvergur/DvergurRace.cs @@ -1,3 +1,4 @@ +using HarmonyLib; using UnityEngine; namespace RacesOfValheim @@ -6,14 +7,18 @@ namespace RacesOfValheim /// Driver for the Dvergur: a short, stocky smith who trades a slice of health for an affinity /// with hammer, bow, axe and crossbow, a knack for mining and the ear of Haldor and Hildir. /// - /// Only the per-frame upkeep lives here. The perks themselves are Harmony patches in - /// DvergurPatches, the two eitr abilities are in DvergurAbilities, and every number they use - /// comes from DvergurConfig by way of DvergurProgression. + /// Only the per-frame upkeep lives here. Body scale is a Player.Update postfix in + /// DvergurPatches, since that is the one place it survives; the perks are the other patches in + /// that file; the two eitr abilities are in DvergurAbilities; and every number they use comes + /// from DvergurConfig by way of DvergurProgression. /// public class DvergurRace : MonoBehaviour { public static DvergurRace Instance; + // NaN so that the very first frame always counts as a change and pushes the malus through. + private float lastAppliedMalus = float.NaN; + private void Awake() { Instance = this; @@ -36,15 +41,22 @@ namespace RacesOfValheim Player player = Player.m_localPlayer; if (player == null) return; - if (!IsDvergur()) + bool isDvergur = IsDvergur(); + + DvergurVisuals.ApplyHood(player); + + // Max health is recomputed by Player.UpdateFood, which runs on its own schedule. Left + // to itself the malus would only take effect at the next food tick, so a change of + // race or of tier is pushed through immediately - this is also what makes the health + // bar and the number agree the instant a player becomes (or stops being) a Dvergur. + float desiredMalus = isDvergur ? DvergurProgression.Current.HealthMalus : 0f; + if (!Mathf.Approximately(desiredMalus, lastAppliedMalus)) { - DvergurVisuals.Apply(player); - return; + lastAppliedMalus = desiredMalus; + Traverse.Create(player).Method("UpdateFood", 0f, true).GetValue(); } - DvergurVisuals.Apply(player); - - if (player.IsDead()) return; + if (!isDvergur || player.IsDead()) return; int state = BossProgressionManager.GetCurrentProgressionState(); diff --git a/JotunnModStub/Dvergur/DvergurVisuals.cs b/JotunnModStub/Dvergur/DvergurVisuals.cs index fc93a24..1742ae7 100644 --- a/JotunnModStub/Dvergur/DvergurVisuals.cs +++ b/JotunnModStub/Dvergur/DvergurVisuals.cs @@ -5,22 +5,27 @@ using UnityEngine; namespace RacesOfValheim { /// - /// The Dvergur's looks: a short body and a hood that is always worn. + /// The Dvergur's looks: a short body, dverger skin, and a hood that is always worn. /// /// The hood is a pure cosmetic override, not an equipment lock. The helmet slot stays free and - /// whatever the player equips keeps every one of its stats - only its mesh is swapped out, the - /// way the Skeleton hides helmets outright, so armour, resistances and set bonuses all still - /// count. Everything here runs for local and remote players alike, which is why it takes a - /// Player rather than reading Player.m_localPlayer. + /// whatever the player equips keeps every one of its stats - only its mesh is swapped out, so + /// armour, resistances and set bonuses all still count. + /// + /// Everything here runs for local and remote players alike, which is why it takes a Player + /// rather than reading Player.m_localPlayer. /// public static class DvergurVisuals { private const string HoodName = "DvergurHood"; + // The hood only has to be looked up once per session; the search walks a whole creature + // prefab and would otherwise run again for every player that comes into view. + private static GameObject cachedHoodSource; + private static bool hoodSourceResolved; + /// /// Reads the race off the ZDO so remote players render correctly, falling back to - /// m_customData for the main-menu preview which has no ZNetView. Same shape as - /// FurlingPatches.GetPlayerRace. + /// m_customData for the main-menu preview which has no ZNetView. /// public static string GetPlayerRace(Player player) { @@ -41,22 +46,84 @@ namespace RacesOfValheim public static bool IsDvergurPlayer(Player player) => GetPlayerRace(player) == DvergurConfig.RaceId; + // ─── Scale ─────────────────────────────────────────────────────────── + /// - /// Brings a player's body in line with their race. Safe to call every frame: each step - /// checks the current value first and does nothing when it already matches. + /// Sizes a player, driven from a Player.Update postfix exactly the way the Trol is - which + /// is the arrangement that actually holds. Doing it from the plugin's own MonoBehaviour + /// left the scale in a fight it could not win: Trol's Player.Update postfix resets every + /// non-Trol player's visual to 1 on the same frame, and whichever ran last won. /// - public static void Apply(Player player) + public static void ApplyScale(Player player, bool isDvergur) { if (player == null) return; - bool isDvergur = IsDvergurPlayer(player) && !player.IsDead(); + // The "Visual" child, never the root: the root carries the collider and the camera + // pivot, and shrinking those drops the player through the ground they stand on. + GameObject visual = Traverse.Create(player).Field("m_visual").GetValue(); + if (visual == null) return; - ApplyScale(player, isDvergur); + if (isDvergur) + { + float target = DvergurConfig.VisualScale; + if (Mathf.Abs(visual.transform.localScale.x - target) > 0.001f) + { + visual.transform.localScale = new Vector3(target, target, target); + } + } + else if (Mathf.Abs(visual.transform.localScale.x - DvergurConfig.VisualScale) < 0.001f) + { + // Only ever undo our own scale - another race may legitimately be sizing this player. + visual.transform.localScale = Vector3.one; + } + } + + // ─── Skin ──────────────────────────────────────────────────────────── + + /// + /// Tints the local player's skin, saving the original first so ResetRace can put it back. + /// Same save-then-tint dance the Furling, Trol and Draugh selections do. + /// + public static void ApplySkinColor(Player player) + { + if (player == null) return; VisEquipment visEq = player.GetComponent(); if (visEq == null) return; - if (isDvergur) + if (!player.m_customData.ContainsKey("OriginalSkinColor_X")) + { + Vector3 current = Traverse.Create(visEq).Field("m_skinColor").GetValue(); + player.m_customData["OriginalSkinColor_X"] = current.x.ToString(); + player.m_customData["OriginalSkinColor_Y"] = current.y.ToString(); + player.m_customData["OriginalSkinColor_Z"] = current.z.ToString(); + } + + Traverse.Create(visEq).Method("SetSkinColor", DvergurConfig.SkinColor).GetValue(); + RefreshColors(visEq); + } + + public static void RefreshColors(VisEquipment visEq) + { + if (visEq == null) return; + visEq.GetType().GetMethod("UpdateColors", + System.Reflection.BindingFlags.Instance | + System.Reflection.BindingFlags.NonPublic | + System.Reflection.BindingFlags.Public)?.Invoke(visEq, null); + } + + // ─── Hood ──────────────────────────────────────────────────────────── + + /// + /// Keeps the hood and the hidden helmet in step. Safe to call every frame: each step + /// checks the current value first and does nothing when it already matches. + /// + public static void ApplyHood(Player player) + { + VisEquipment visEq = player != null ? player.GetComponent() : null; + if (visEq == null) return; + + if (IsDvergurPlayer(player) && !player.IsDead()) { EnsureHood(visEq); HideEquippedHelmet(visEq); @@ -67,27 +134,6 @@ namespace RacesOfValheim } } - private static void ApplyScale(Player player, bool isDvergur) - { - // Scaling the "Visual" child rather than the root, as Furling does: the root carries - // the collider and the camera pivot, and shrinking those makes the player fall through - // the world geometry they were standing on. - GameObject visual = Traverse.Create(player).Field("m_visual").GetValue(); - if (visual == null) return; - - Vector3 target = isDvergur ? Vector3.one * DvergurConfig.VisualScale : Vector3.one; - - if (isDvergur) - { - if (visual.transform.localScale != target) visual.transform.localScale = target; - } - else if (visual.transform.localScale == Vector3.one * DvergurConfig.VisualScale) - { - // Only undo our own scale - another race may legitimately be scaling this player. - visual.transform.localScale = Vector3.one; - } - } - /// /// Blanks the mesh of whatever helmet is equipped. Scaled to zero rather than deactivated /// because Valheim re-enables the instance whenever it refreshes visuals, and this is the @@ -106,10 +152,10 @@ namespace RacesOfValheim { if (visEq.m_bodyModel == null || visEq.m_bodyModel.bones == null) return; - Transform headBone = FindBoneByName(visEq.m_bodyModel.bones, "Head"); - if (headBone == null) return; + Transform parent = visEq.m_bodyModel.transform.parent; + if (parent == null) return; - Transform existing = headBone.Find(HoodName); + Transform existing = parent.Find(HoodName); if (existing != null) { if (!existing.gameObject.activeSelf) existing.gameObject.SetActive(true); @@ -117,45 +163,57 @@ namespace RacesOfValheim } GameObject source = ResolveHoodSource(); - if (source == null) - { - // Logged once per player rather than per frame: the early return above means we - // only reach here while the hood is genuinely missing. - Jotunn.Logger.LogWarning( - $"[Dvergur] No hood prefab found among [{string.Join(", ", DvergurConfig.HoodPrefabCandidates)}]; " + - "the helmet slot stays cosmetically empty."); - return; - } + if (source == null) return; // already logged once by the resolver bool wasActive = source.activeSelf; source.SetActive(false); - GameObject hood = Object.Instantiate(source, headBone, false); + GameObject hood = Object.Instantiate(source, parent, false); source.SetActive(wasActive); hood.name = HoodName; - - // Strip anything that would make a decorative mesh behave like a world object - - // hover prompts, pickup triggers, physics. Same cleanup the Furling head does. - foreach (MonoBehaviour comp in hood.GetComponentsInChildren(true)) - { - Object.DestroyImmediate(comp); - } - foreach (Rigidbody body in hood.GetComponentsInChildren(true)) - { - Object.DestroyImmediate(body); - } - foreach (Collider col in hood.GetComponentsInChildren(true)) - { - Object.DestroyImmediate(col); - } - + StripBehaviours(hood); hood.SetActive(true); - hood.transform.localPosition = DvergurConfig.HoodLocalPosition; - hood.transform.localRotation = Quaternion.Euler(DvergurConfig.HoodLocalRotation); - hood.transform.localScale = Vector3.one * DvergurConfig.HoodLocalScale; - // Without the creature shader the hood ignores the camera's dither fade and blocks - // the view in first person; its own textures are kept. + // The hood is skinned to the Dvergr rig. Instantiated as-is its bones still point at + // the source prefab's transforms, so it renders at the world origin or not at all - + // which is exactly why the first attempt was invisible. Rebinding it to the player's + // own bones by name is what the Skeleton does for whole bodies, and it means the rig + // positions the hood for us instead of us guessing offsets. + Transform[] playerBones = visEq.m_bodyModel.bones; + bool rebound = false; + + foreach (SkinnedMeshRenderer smr in hood.GetComponentsInChildren(true)) + { + Transform[] bones = smr.bones; + Transform[] remapped = new Transform[bones.Length]; + for (int i = 0; i < bones.Length; i++) + { + Transform match = bones[i] != null ? FindBoneByName(playerBones, bones[i].name) : null; + remapped[i] = match != null ? match : playerBones[0]; + } + smr.bones = remapped; + + if (smr.rootBone != null) + { + smr.rootBone = FindBoneByName(playerBones, smr.rootBone.name) ?? playerBones[0]; + } + rebound = true; + } + + if (!rebound) + { + // A rigid hood rather than a skinned one: it has no bones to rebind, so it is + // simply hung off the head the way the Furling's head is. + Transform headBone = FindBoneByName(playerBones, "Head"); + if (headBone != null) hood.transform.SetParent(headBone, false); + } + + hood.transform.localPosition = DvergurConfig.HoodOffsetPosition; + hood.transform.localRotation = Quaternion.Euler(DvergurConfig.HoodOffsetRotation); + hood.transform.localScale = Vector3.one * DvergurConfig.HoodExtraScale; + + // Without the creature shader the hood ignores the camera's dither fade and fills the + // screen in first person; its own textures are kept. Shader dither = Shader.Find("Custom/Creature"); if (dither != null) { @@ -166,40 +224,80 @@ namespace RacesOfValheim } } + private static void StripBehaviours(GameObject obj) + { + // Anything that would make a decorative mesh behave like a world object - hover + // prompts, pickup triggers, AI, physics. + foreach (MonoBehaviour comp in obj.GetComponentsInChildren(true)) + { + Object.DestroyImmediate(comp); + } + foreach (Rigidbody body in obj.GetComponentsInChildren(true)) + { + Object.DestroyImmediate(body); + } + foreach (Collider col in obj.GetComponentsInChildren(true)) + { + Object.DestroyImmediate(col); + } + } + /// - /// First candidate prefab that resolves wins. A creature prefab is handled specially: the - /// Dvergr's headgear is a child renderer of the creature, not a standalone prefab, so the - /// piece is lifted out rather than the whole NPC being instantiated on the player's head. + /// Digs the headgear out of the Dvergr NPC prefab. Resolved once and remembered, including + /// the failure, so a missing prefab cannot turn into a per-frame log. /// private static GameObject ResolveHoodSource() { - foreach (string candidate in DvergurConfig.HoodPrefabCandidates) + if (hoodSourceResolved) return cachedHoodSource; + hoodSourceResolved = true; + + GameObject dverger = PrefabManager.Cache.GetPrefab(DvergurConfig.DvergerPrefabName); + if (dverger == null) { - GameObject prefab = PrefabManager.Cache.GetPrefab(candidate); - if (prefab == null) continue; + Jotunn.Logger.LogWarning($"[Dvergur] Prefab '{DvergurConfig.DvergerPrefabName}' not found; no hood."); + return null; + } - if (prefab.GetComponent() == null) return prefab; - - foreach (Renderer renderer in prefab.GetComponentsInChildren(true)) + foreach (Renderer renderer in dverger.GetComponentsInChildren(true)) + { + string name = renderer.gameObject.name.ToLower(); + foreach (string keyword in DvergurConfig.HoodRendererNames) { - string name = renderer.gameObject.name.ToLower(); - if (name.Contains("helmet") || name.Contains("hat") || name.Contains("hood") || name.Contains("cap")) - { - return renderer.gameObject; - } + if (!name.Contains(keyword)) continue; + + Jotunn.Logger.LogInfo($"[Dvergur] Hood resolved to '{renderer.gameObject.name}' ({renderer.GetType().Name})."); + cachedHoodSource = renderer.gameObject; + return cachedHoodSource; } } + + // Nothing matched, so say exactly what was on offer - that turns a silent invisible + // hood into a one-line fix in DvergurConfig.HoodRendererNames. + System.Text.StringBuilder sb = new System.Text.StringBuilder(); + foreach (Renderer renderer in dverger.GetComponentsInChildren(true)) + { + sb.Append(renderer.gameObject.name).Append(" (").Append(renderer.GetType().Name).Append("), "); + } + Jotunn.Logger.LogWarning( + $"[Dvergur] No renderer in '{DvergurConfig.DvergerPrefabName}' matched " + + $"[{string.Join(", ", DvergurConfig.HoodRendererNames)}]. Available: {sb}"); return null; } private static void RemoveHood(VisEquipment visEq) { - if (visEq.m_bodyModel == null || visEq.m_bodyModel.bones == null) return; + if (visEq.m_bodyModel == null) return; - Transform headBone = FindBoneByName(visEq.m_bodyModel.bones, "Head"); - if (headBone == null) return; + Transform parent = visEq.m_bodyModel.transform.parent; + Transform hood = parent != null ? parent.Find(HoodName) : null; + + // Fall back to the head bone for the rigid-hood case, which parents itself there. + if (hood == null && visEq.m_bodyModel.bones != null) + { + Transform headBone = FindBoneByName(visEq.m_bodyModel.bones, "Head"); + if (headBone != null) hood = headBone.Find(HoodName); + } - Transform hood = headBone.Find(HoodName); if (hood == null) return; // never was a Dvergur - leave the helmet alone // Destroy is deferred to the end of the frame, so the object is renamed first: without @@ -216,6 +314,8 @@ namespace RacesOfValheim private static Transform FindBoneByName(Transform[] bones, string targetBoneName) { + if (bones == null || string.IsNullOrEmpty(targetBoneName)) return null; + foreach (Transform bone in bones) { if (bone != null && bone.name == targetBoneName) return bone; @@ -224,7 +324,9 @@ namespace RacesOfValheim string target = targetBoneName.ToLower(); foreach (Transform bone in bones) { - if (bone != null && bone.name.ToLower().Contains(target)) return bone; + if (bone == null) continue; + string name = bone.name.ToLower(); + if (name == target || name.Contains(target) || target.Contains(name)) return bone; } return null; } diff --git a/JotunnModStub/RaceScale.cs b/JotunnModStub/RaceScale.cs new file mode 100644 index 0000000..7c8f5fe --- /dev/null +++ b/JotunnModStub/RaceScale.cs @@ -0,0 +1,35 @@ +namespace RacesOfValheim +{ + /// + /// Which races size their own body. + /// + /// Several races postfix Player.Update and set the player's "Visual" scale from there. Each of + /// them also wants to undo that scale once the player is no longer of their race, and the + /// straightforward way to write that - "if this isn't my race, force the scale back to 1" - + /// makes every such race stamp on every other one, with the winner decided by whichever + /// postfix Harmony happened to run last. + /// + /// That is what left the Dvergur at full height: Trol's reset ran on the same frame and put it + /// straight back to 1. Asking here first means a race only ever resets a player that nobody + /// else is sizing. + /// + public static class RaceScale + { + private static readonly string[] SelfSizingRaces = + { + "Trol", + "Furling", + DvergurConfig.RaceId + }; + + public static bool ManagesOwnScale(string race) + { + if (string.IsNullOrEmpty(race)) return false; + foreach (string self in SelfSizingRaces) + { + if (race == self) return true; + } + return false; + } + } +} diff --git a/JotunnModStub/RacesOfValheim.cs b/JotunnModStub/RacesOfValheim.cs index 9b02236..ceb30bc 100644 --- a/JotunnModStub/RacesOfValheim.cs +++ b/JotunnModStub/RacesOfValheim.cs @@ -38,6 +38,7 @@ namespace RacesOfValheim CommandManager.Instance.AddConsoleCommand(new CheckProgressionCommand()); CommandManager.Instance.AddConsoleCommand(new IncreaseProgressionCommand()); CommandManager.Instance.AddConsoleCommand(new ResetProgressionCommand()); + CommandManager.Instance.AddConsoleCommand(new DumpDvergerCommand()); } /// @@ -592,8 +593,9 @@ namespace RacesOfValheim Player.m_localPlayer.GetSEMan().AddStatusEffect(DvergurConfig.StatsSE.GetStableHashCode(), true); - // The Dvergur keeps its own skin - only the silhouette and the hood change, so - // UpdateVisuals is enough and UpdateColors has nothing to do here. + // Saves the original skin first, so ResetRace can hand it back. + DvergurVisuals.ApplySkinColor(Player.m_localPlayer); + VisEquipment visEq = Player.m_localPlayer.GetComponent(); if (visEq != null) { @@ -690,6 +692,13 @@ namespace RacesOfValheim if (visEq != null) visEq.GetType().GetMethod("UpdateColors", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Public)?.Invoke(visEq, null); } + // Re-tinted rather than merely refreshed: the skin colour is the one piece of the + // Dvergur that is not rebuilt from the race id on its own. + if (__instance.m_customData.ContainsKey("Race") && __instance.m_customData["Race"] == DvergurConfig.RaceId) + { + DvergurVisuals.ApplySkinColor(__instance); + } + // Check for Race Selection if (!__instance.m_customData.ContainsKey("Race")) { @@ -768,6 +777,82 @@ namespace RacesOfValheim } } + /// + /// Dumps the Dvergr NPC prefab: its transform tree, every renderer on it, and its per-star + /// LevelEffects colours. Prefab contents live in the asset bundles and cannot be read outside + /// a running game, so this is how the hood mesh and the star tints get identified. + /// + public class DumpDvergerCommand : ConsoleCommand + { + public override string Name => "dumpdverger"; + public override string Help => "[DEBUG] Logs the Dverger prefab hierarchy, renderers and star colours"; + + public override void Run(string[] args) + { + string prefabName = args.Length > 0 ? args[0] : DvergurConfig.DvergerPrefabName; + GameObject prefab = Jotunn.Managers.PrefabManager.Cache.GetPrefab(prefabName); + if (prefab == null) + { + Console.instance.Print($"Prefab '{prefabName}' not found."); + return; + } + + Jotunn.Logger.LogInfo($"=== {prefabName} HIERARCHY ==="); + DumpTransform(prefab.transform, ""); + + Jotunn.Logger.LogInfo($"=== {prefabName} RENDERERS ==="); + foreach (Renderer renderer in prefab.GetComponentsInChildren(true)) + { + Jotunn.Logger.LogInfo($" {renderer.gameObject.name} ({renderer.GetType().Name}) path={GetPath(renderer.transform)}"); + } + + LevelEffects levelEffects = prefab.GetComponentInChildren(true); + if (levelEffects != null && levelEffects.m_levelSetups != null) + { + Jotunn.Logger.LogInfo($"=== {prefabName} LEVEL SETUPS (index 0 = 1 star) ==="); + for (int i = 0; i < levelEffects.m_levelSetups.Count; i++) + { + var setup = levelEffects.m_levelSetups[i]; + if (setup == null) continue; + Jotunn.Logger.LogInfo( + $" [{i}] scale={setup.m_scale} hue={setup.m_hue} sat={setup.m_saturation} val={setup.m_value} " + + $"emissive={setup.m_setEmissiveColor}:{setup.m_emissiveColor}"); + } + } + else + { + Jotunn.Logger.LogInfo($"{prefabName} has no LevelEffects."); + } + + Console.instance.Print($"Dumped '{prefabName}' to the BepInEx log."); + } + + private static void DumpTransform(Transform t, string indent) + { + if (t == null) return; + + System.Text.StringBuilder components = new System.Text.StringBuilder(); + foreach (Component c in t.GetComponents()) + { + if (c != null) components.Append(c.GetType().Name).Append(" "); + } + + Jotunn.Logger.LogInfo($"{indent}{t.name} [{components}]"); + for (int i = 0; i < t.childCount; i++) DumpTransform(t.GetChild(i), indent + " "); + } + + private static string GetPath(Transform t) + { + string path = t.name; + while (t.parent != null) + { + t = t.parent; + path = t.name + "/" + path; + } + return path; + } + } + public class ResetProgressionCommand : ConsoleCommand { public override string Name => "resetprogression"; diff --git a/JotunnModStub/Trol/TrolPatches.cs b/JotunnModStub/Trol/TrolPatches.cs index 4492d82..a049085 100644 --- a/JotunnModStub/Trol/TrolPatches.cs +++ b/JotunnModStub/Trol/TrolPatches.cs @@ -176,8 +176,11 @@ namespace RacesOfValheim } } } - else if (Mathf.Abs(visual.transform.localScale.x - 1f) > 0.001f) + else if (!RaceScale.ManagesOwnScale(race) && Mathf.Abs(visual.transform.localScale.x - 1f) > 0.001f) { + // Races that size themselves are left alone. Without this guard the reset fires on + // every frame for a Furling or a Dvergur too, undoing their scale as fast as they + // set it, because both are applied from a postfix on this same method. visual.transform.localScale = Vector3.one; } } diff --git a/JotunnModStub/ZdoVisualSync.cs b/JotunnModStub/ZdoVisualSync.cs index a3b1a38..77b684b 100644 --- a/JotunnModStub/ZdoVisualSync.cs +++ b/JotunnModStub/ZdoVisualSync.cs @@ -83,9 +83,9 @@ namespace RacesOfValheim } else if (race == DvergurConfig.RaceId) { - // The hood itself is rebuilt by the VisEquipment patch; this keeps the shrunken - // silhouette and the hidden helmet asserted between those rebuilds. - DvergurVisuals.Apply(m_player); + // Scale is handled for every player by the Player.Update postfix; this only keeps + // the hood and the hidden helmet asserted between equipment refreshes. + DvergurVisuals.ApplyHood(m_player); } else {