From 2df8264c42279f941d0aae6ef31f27271ccd6009 Mon Sep 17 00:00:00 2001 From: kanym Date: Sat, 17 Oct 2026 19:06:08 +0200 Subject: [PATCH 1/6] debug: log the Ulv's sleep state while the player sits (temporary) --- JotunnModStub/Fenrir/FenrirFormShared.cs | 29 +++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/JotunnModStub/Fenrir/FenrirFormShared.cs b/JotunnModStub/Fenrir/FenrirFormShared.cs index f412620..e1a8b72 100644 --- a/JotunnModStub/Fenrir/FenrirFormShared.cs +++ b/JotunnModStub/Fenrir/FenrirFormShared.cs @@ -87,7 +87,9 @@ namespace RacesOfValheim (hasSleeping ? "sitting puts it to sleep." : "sitting keeps it idle.")); } } - if (hasSleeping) form.SetBool("sleeping", IsSitting(player)); + bool sitting = IsSitting(player); + if (hasSleeping) form.SetBool("sleeping", sitting); + DebugSleep(form, player, sitting); return grounded; } @@ -98,6 +100,31 @@ namespace RacesOfValheim /// never saw it and the Ulv stayed on its feet. Player.StartEmote writes the emote's name on the /// ZDO, which every client reads, so this is right for an onlooker's copy as well. /// + // TEMPORARY DEBUG for the Ulv that will not sleep. Remove with the fix. + private bool lastSitting; + private float nextSleepLog; + private void DebugSleep(Animator form, Player player, bool sitting) + { + if (player == null || player != Player.m_localPlayer) return; + ZNetView nview = player.GetComponent(); + string emote = nview != null && nview.IsValid() ? nview.GetZDO().GetString(ZDOVars.s_emote, "") : "?"; + if (sitting != lastSitting) + { + lastSitting = sitting; + Jotunn.Logger.LogInfo($"[SleepDebug] sitting={sitting} IsSitting={player.IsSitting()} emote='{emote}' hasSleeping={hasSleeping}"); + } + if (!sitting && emote != "sit") return; + if (Time.time < nextSleepLog) return; + nextSleepLog = Time.time + 1f; + string clip = "?"; + var clips = form.GetCurrentAnimatorClipInfo(0); + if (clips.Length > 0 && clips[0].clip != null) clip = clips[0].clip.name; + bool sleepingNow = false; + try { sleepingNow = form.GetBool("sleeping"); } catch {} + Jotunn.Logger.LogInfo($"[SleepDebug] sitting={sitting} emote='{emote}' sleepingBool={sleepingNow} clip='{clip}' " + + $"inTransition={form.IsInTransition(0)} enabled={form.enabled} speed={form.speed}"); + } + private static bool IsSitting(Player player) { if (player == null) return false; From 4cb6da801bf669e49a2002fb848b50fff33b0dd9 Mon Sep 17 00:00:00 2001 From: kanym Date: Sat, 17 Oct 2026 19:09:38 +0200 Subject: [PATCH 2/6] Revert "debug: log the Ulv's sleep state while the player sits (temporary)" This reverts commit 2df8264c42279f941d0aae6ef31f27271ccd6009. --- JotunnModStub/Fenrir/FenrirFormShared.cs | 29 +----------------------- 1 file changed, 1 insertion(+), 28 deletions(-) diff --git a/JotunnModStub/Fenrir/FenrirFormShared.cs b/JotunnModStub/Fenrir/FenrirFormShared.cs index e1a8b72..f412620 100644 --- a/JotunnModStub/Fenrir/FenrirFormShared.cs +++ b/JotunnModStub/Fenrir/FenrirFormShared.cs @@ -87,9 +87,7 @@ namespace RacesOfValheim (hasSleeping ? "sitting puts it to sleep." : "sitting keeps it idle.")); } } - bool sitting = IsSitting(player); - if (hasSleeping) form.SetBool("sleeping", sitting); - DebugSleep(form, player, sitting); + if (hasSleeping) form.SetBool("sleeping", IsSitting(player)); return grounded; } @@ -100,31 +98,6 @@ namespace RacesOfValheim /// never saw it and the Ulv stayed on its feet. Player.StartEmote writes the emote's name on the /// ZDO, which every client reads, so this is right for an onlooker's copy as well. /// - // TEMPORARY DEBUG for the Ulv that will not sleep. Remove with the fix. - private bool lastSitting; - private float nextSleepLog; - private void DebugSleep(Animator form, Player player, bool sitting) - { - if (player == null || player != Player.m_localPlayer) return; - ZNetView nview = player.GetComponent(); - string emote = nview != null && nview.IsValid() ? nview.GetZDO().GetString(ZDOVars.s_emote, "") : "?"; - if (sitting != lastSitting) - { - lastSitting = sitting; - Jotunn.Logger.LogInfo($"[SleepDebug] sitting={sitting} IsSitting={player.IsSitting()} emote='{emote}' hasSleeping={hasSleeping}"); - } - if (!sitting && emote != "sit") return; - if (Time.time < nextSleepLog) return; - nextSleepLog = Time.time + 1f; - string clip = "?"; - var clips = form.GetCurrentAnimatorClipInfo(0); - if (clips.Length > 0 && clips[0].clip != null) clip = clips[0].clip.name; - bool sleepingNow = false; - try { sleepingNow = form.GetBool("sleeping"); } catch {} - Jotunn.Logger.LogInfo($"[SleepDebug] sitting={sitting} emote='{emote}' sleepingBool={sleepingNow} clip='{clip}' " + - $"inTransition={form.IsInTransition(0)} enabled={form.enabled} speed={form.speed}"); - } - private static bool IsSitting(Player player) { if (player == null) return false; From 9cf3f5386c7e413ca38690cbe1591e0bfc90f702 Mon Sep 17 00:00:00 2001 From: kanym Date: Sat, 17 Oct 2026 19:09:50 +0200 Subject: [PATCH 3/6] fix(fenrir): the Ulv sleeps when the player sits Sitting now zeroes the form's speed. The seated player's Animator keeps a forward_speed above zero, which kept the Ulv in its Walk clip on the spot and out of reach of its Sleep state, which leaves from idle. The debug log showed it: sitting, sleeping=true, clip 'Walk'. --- JotunnModStub/Fenrir/FenrirFormShared.cs | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/JotunnModStub/Fenrir/FenrirFormShared.cs b/JotunnModStub/Fenrir/FenrirFormShared.cs index f412620..3922be6 100644 --- a/JotunnModStub/Fenrir/FenrirFormShared.cs +++ b/JotunnModStub/Fenrir/FenrirFormShared.cs @@ -66,8 +66,15 @@ namespace RacesOfValheim // sneaking in a form slid across the ground in the idle pose. Crouched, the player moves // at sneaking speed, and the creature's controller picks its walk for a speed that low: // the sneak a player asked for, from animations the creature already has. - try { form.SetFloat("forward_speed", grounded ? playerAnim.GetFloat("forward_speed") : 0f); } catch {} - try { form.SetFloat("sideway_speed", grounded ? playerAnim.GetFloat("sideway_speed") : 0f); } catch {} + // + // Sitting reads as still too. The seated player's own Animator keeps a forward_speed that + // is not zero, and passed through it kept the Ulv in its Walk clip - on the spot, and out + // of reach of its Sleep state, which only leaves from idle. Seen in the log: sitting, + // sleeping=true, clip 'Walk'. + bool sitting = IsSitting(player); + bool still = !grounded || sitting; + try { form.SetFloat("forward_speed", still ? 0f : playerAnim.GetFloat("forward_speed")); } catch {} + try { form.SetFloat("sideway_speed", still ? 0f : playerAnim.GetFloat("sideway_speed")); } catch {} // Sitting puts the creature to sleep, on the "sleeping" bool its own MonsterAI uses - the // Ulv curls up. IsSitting reads the animator's state, which is replicated, so it is right @@ -87,7 +94,7 @@ namespace RacesOfValheim (hasSleeping ? "sitting puts it to sleep." : "sitting keeps it idle.")); } } - if (hasSleeping) form.SetBool("sleeping", IsSitting(player)); + if (hasSleeping) form.SetBool("sleeping", sitting); return grounded; } From fb354c2387dc760e71058197388906d88e4ceb66 Mon Sep 17 00:00:00 2001 From: kanym Date: Sat, 17 Oct 2026 19:13:47 +0200 Subject: [PATCH 4/6] fix(fenrir): enter the Ulv's Sleep state by hand when the player sits A vanilla Ulv is born asleep: its controller only knows the way out of Sleep, so setting the bool from Idle did nothing. The state is now cross-faded into when the player sits; standing drops the bool and the controller's own 'Sleep to Idle' plays. The log names the state found. --- JotunnModStub/Fenrir/FenrirFormShared.cs | 34 ++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/JotunnModStub/Fenrir/FenrirFormShared.cs b/JotunnModStub/Fenrir/FenrirFormShared.cs index 3922be6..5ee2233 100644 --- a/JotunnModStub/Fenrir/FenrirFormShared.cs +++ b/JotunnModStub/Fenrir/FenrirFormShared.cs @@ -48,6 +48,9 @@ namespace RacesOfValheim // drives the creature's own sleep through it, and not every creature has one. private Animator sleepChecked; private bool hasSleeping; + private int sleepState; + private bool wasSitting; + private static readonly string[] SleepStateNames = { "Sleep", "sleep", "Sleeping", "sleeping" }; private static readonly System.Collections.Generic.HashSet sleepLogged = new System.Collections.Generic.HashSet(); /// Copies the movement parameters, and says whether the form counts as grounded. @@ -87,14 +90,41 @@ namespace RacesOfValheim { if (p.type == AnimatorControllerParameterType.Bool && p.name == "sleeping") { hasSleeping = true; break; } } + + // The state's name is not readable at runtime, only testable: the clip is 'Sleep', and + // the state is most likely named after it. + sleepState = 0; + string found = "none"; + if (hasSleeping) + { + foreach (string candidate in SleepStateNames) + { + int hash = Animator.StringToHash(candidate); + if (!form.HasState(0, hash)) continue; + sleepState = hash; + found = candidate; + break; + } + } string controller = form.runtimeAnimatorController != null ? form.runtimeAnimatorController.name : form.name; if (sleepLogged.Add(controller)) { Jotunn.Logger.LogInfo($"[FenrirForm] '{controller}' {(hasSleeping ? "has" : "has no")} sleeping animation; " + - (hasSleeping ? "sitting puts it to sleep." : "sitting keeps it idle.")); + (hasSleeping ? $"sitting puts it to sleep (state '{found}')." : "sitting keeps it idle.")); } } - if (hasSleeping) form.SetBool("sleeping", sitting); + if (hasSleeping) + { + form.SetBool("sleeping", sitting); + + // The bool alone never put the Ulv to sleep. A vanilla Ulv is BORN asleep: its + // controller enters Sleep from the start and only knows the way out (Sleep, then + // 'Sleep to Idle' once the bool drops). Nothing leads from Idle back into Sleep, so + // the state is entered by hand when the player sits; standing up drops the bool and + // the controller's own wake-up plays. + if (sitting && !wasSitting && sleepState != 0) form.CrossFadeInFixedTime(sleepState, 0.4f, 0); + } + wasSitting = sitting; return grounded; } From e83c3c5d74757a951343d36d0a4bbce6e224f44e Mon Sep 17 00:00:00 2001 From: kanym Date: Sat, 17 Oct 2026 19:22:24 +0200 Subject: [PATCH 5/6] fix(fenrir): enter the Ulv's sleep by its real state path scripts/dump_controller.py reads an AnimatorController's states and transitions offline from Valheim's bundles with UnityPy. On ulv_animator it shows the entry state "check sleeping" picks Sleep only at start-up and Movement has no way back into it, so the bool alone could never work. The cross-fade now targets "Base Layer.Sleep". --- JotunnModStub/Fenrir/FenrirFormShared.cs | 9 +- scripts/dump_controller.py | 105 +++++++++++++++++++++++ 2 files changed, 111 insertions(+), 3 deletions(-) create mode 100644 scripts/dump_controller.py diff --git a/JotunnModStub/Fenrir/FenrirFormShared.cs b/JotunnModStub/Fenrir/FenrirFormShared.cs index 5ee2233..a6eb382 100644 --- a/JotunnModStub/Fenrir/FenrirFormShared.cs +++ b/JotunnModStub/Fenrir/FenrirFormShared.cs @@ -50,7 +50,10 @@ namespace RacesOfValheim private bool hasSleeping; private int sleepState; private bool wasSitting; - private static readonly string[] SleepStateNames = { "Sleep", "sleep", "Sleeping", "sleeping" }; + // Read off ulv_animator with scripts/dump_controller.py: the entry state "check sleeping" goes + // to Sleep only at start-up, and Movement has no way back into it. "Base Layer.Sleep" is the + // real path; the short name stays as a fallback for another creature built the same way. + private static readonly string[] SleepStateNames = { "Base Layer.Sleep", "Sleep" }; private static readonly System.Collections.Generic.HashSet sleepLogged = new System.Collections.Generic.HashSet(); /// Copies the movement parameters, and says whether the form counts as grounded. @@ -91,8 +94,8 @@ namespace RacesOfValheim if (p.type == AnimatorControllerParameterType.Bool && p.name == "sleeping") { hasSleeping = true; break; } } - // The state's name is not readable at runtime, only testable: the clip is 'Sleep', and - // the state is most likely named after it. + // The state's name is not readable at runtime, only testable - but it is readable offline, + // see SleepStateNames. sleepState = 0; string found = "none"; if (hasSleeping) diff --git a/scripts/dump_controller.py b/scripts/dump_controller.py new file mode 100644 index 0000000..07b9274 --- /dev/null +++ b/scripts/dump_controller.py @@ -0,0 +1,105 @@ +r"""Dump a Unity AnimatorController's parameters, states and transitions from Valheim's own files. + +Reads the game's asset files offline, so animator state and parameter names are read, not guessed. + +Setup, once (UnityPy into a folder of your choice, not into the repo): + python -m pip install --target C:\path o\pylib UnityPy + +Usage (Windows paths: a Git Bash "/c/..." path does not resolve for a Windows Python): + python -I scripts/dump_controller.py C:\path o\pylib " alheim_Data" ulv_animator wolf_animator + +Pass valheim_Data to search everything (slow, a few minutes), or a single bundle file to read +only that one. Creature animators live in valheim_Data\StreamingAssets\SoftRef\Bundles; the +Fenrir forms' four (wolf, ulv, fenring, fenring_cultist) are all in bundle c4210710 as of 1.0.20. +A controller's name is the one dumpanim prints ("controller 'ulv_animator'"). +""" +import os, sys, json + +sys.path.insert(0, sys.argv[1]) +import UnityPy # noqa: E402 + +data_dir = sys.argv[2] +wanted = set(sys.argv[3:]) + +candidates = [data_dir] if os.path.isfile(data_dir) else [] +for root, _, files in ([] if os.path.isfile(data_dir) else os.walk(data_dir)): + for f in files: + p = os.path.join(root, f) + if f.endswith(('.resS', '.resource', '.manifest', '.json', '.dll', '.csv', '.txt', '.info', '.config')): + continue + if os.path.getsize(p) < 1024: + continue + candidates.append(p) + +found = {} +for path in candidates: + try: + env = UnityPy.load(path) + except Exception: + continue + for obj in env.objects: + if obj.type.name != 'AnimatorController': + continue + try: + name = obj.peek_name() + except Exception: + name = None + if name is None: + try: + name = obj.read().m_Name + except Exception: + continue + if name not in wanted or name in found: + continue + tree = obj.read_typetree() + found[name] = (path, tree) + if len(found) == len(wanted): + break + +for name, (path, tree) in found.items(): + print(f'=== {name} ({os.path.basename(path)})') + tos = {int(k): v for k, v in tree.get('m_TOS', [])} + print('m_TOS (hash -> name):') + for h, v in sorted(tos.items(), key=lambda kv: kv[1]): + print(f' {h:>12} {v}') + + ctrl = tree.get('m_Controller', {}) + params = ctrl.get('m_Values', {}).get('data', {}).get('m_ValueArray', []) + print('parameters:') + for p in params: + print(f" {tos.get(p.get('m_ID'), p.get('m_ID'))} type={p.get('m_Type')}") + + for sm_i, sm in enumerate(ctrl.get('m_StateMachineArray', [])): + sm = sm.get('data', sm) + states = sm.get('m_StateConstantArray', []) + print(f'state machine {sm_i}: {len(states)} states, default index {sm.get("m_DefaultState")}') + any_tr = sm.get('m_AnyStateTransitionConstantArray', []) + + def describe(tr): + tr = tr.get('data', tr) + conds = [] + for c in tr.get('m_ConditionConstantArray', []): + c = c.get('data', c) + conds.append(f"{tos.get(c.get('m_EventID'), c.get('m_EventID'))} mode={c.get('m_ConditionMode')} th={c.get('m_EventThreshold')}") + dest = tr.get('m_DestinationState') + return dest, conds + + names = [] + for st in states: + st = st.get('data', st) + names.append(tos.get(st.get('m_NameID'), st.get('m_NameID'))) + for i, st in enumerate(states): + st = st.get('data', st) + print(f' [{i}] state "{names[i]}" fullPath="{tos.get(st.get("m_FullPathID"), st.get("m_FullPathID"))}" tag="{tos.get(st.get("m_TagID"), st.get("m_TagID"))}"') + for tr in st.get('m_TransitionConstantArray', []): + dest, conds = describe(tr) + dname = names[dest] if isinstance(dest, int) and 0 <= dest < len(names) else dest + print(f' -> {dname} when {conds}') + for tr in any_tr: + dest, conds = describe(tr) + dname = names[dest] if isinstance(dest, int) and 0 <= dest < len(names) else dest + print(f' AnyState -> {dname} when {conds}') + +missing = wanted - set(found) +if missing: + print('not found:', ', '.join(sorted(missing))) From 5c0f092c89e19d5e83899eaeff4612edf706c212 Mon Sep 17 00:00:00 2001 From: kanym Date: Sat, 17 Oct 2026 19:28:42 +0200 Subject: [PATCH 6/6] fix(fenrir): a form on a chair settles in front of the seat On a chair the player sits on the seat, and the form stood inside the chair at seat height. The form's root now goes 0.5 m in front of the seat, on whatever is under that point, skipping the seat's own colliders. The seat is found by a short raycast under the player, so it works for onlookers too, who have no attach point. Back to zero on standing up; the sit key is untouched. --- JotunnModStub/Fenrir/FenrirFormShared.cs | 66 ++++++++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git a/JotunnModStub/Fenrir/FenrirFormShared.cs b/JotunnModStub/Fenrir/FenrirFormShared.cs index a6eb382..3d33ddf 100644 --- a/JotunnModStub/Fenrir/FenrirFormShared.cs +++ b/JotunnModStub/Fenrir/FenrirFormShared.cs @@ -129,9 +129,75 @@ namespace RacesOfValheim } wasSitting = sitting; + PlaceOnChair(form, player); + return grounded; } + // How far in front of the seat a creature on a chair settles, in metres. + private const float ChairForward = 0.5f; + private static readonly int ChairGroundMask = LayerMask.GetMask("Default", "static_solid", "terrain", "piece", "vehicle"); + private static readonly RaycastHit[] chairHits = new RaycastHit[16]; + private Transform placedRoot; + + /// + /// On a chair the player sits on the seat itself, and a creature there stood inside the chair, + /// at seat height. It goes half a metre in front of the seat instead, standing on whatever is + /// under that point. Moves the form's own root (the child of the player), never the player. + /// The sit key is left alone: the player is on the ground already. + /// + private void PlaceOnChair(Animator form, Player player) + { + Transform root = RootUnder(form.transform, player != null ? player.transform : null); + if (root == null) return; + + bool onChair = player.IsSitting(); + if (!onChair) + { + if (placedRoot == root && root.localPosition != Vector3.zero) root.localPosition = Vector3.zero; + return; + } + placedRoot = root; + + Transform body = player.transform; + Piece seat = SeatUnder(body.position); + Vector3 target = body.position + body.forward * ChairForward; + + float groundY = target.y; + int count = Physics.RaycastNonAlloc(target + Vector3.up * 1.5f, Vector3.down, chairHits, 4f, ChairGroundMask); + float best = float.MaxValue; + for (int i = 0; i < count; i++) + { + Collider c = chairHits[i].collider; + if (c == null || c.transform.IsChildOf(body)) continue; + if (seat != null && c.GetComponentInParent() == seat) continue; + if (chairHits[i].distance < best) { best = chairHits[i].distance; groundY = chairHits[i].point.y; } + } + + root.localPosition = body.InverseTransformPoint(new Vector3(target.x, groundY, target.z)); + } + + /// The chair or bench the player sits on: the first piece straight under them. + private static Piece SeatUnder(Vector3 position) + { + int count = Physics.RaycastNonAlloc(position + Vector3.up * 0.3f, Vector3.down, chairHits, 1.5f, ChairGroundMask); + Piece seat = null; + float best = float.MaxValue; + for (int i = 0; i < count; i++) + { + Piece piece = chairHits[i].collider != null ? chairHits[i].collider.GetComponentInParent() : null; + if (piece != null && chairHits[i].distance < best) { best = chairHits[i].distance; seat = piece; } + } + return seat; + } + + private static Transform RootUnder(Transform t, Transform player) + { + if (player == null) return null; + while (t != null && t.parent != player) t = t.parent; + return t; + } + /// /// The sit key, or a chair. The sit key is an EMOTE, not the "sitting" animator tag that /// Humanoid.IsSitting asks about - that tag belongs to chairs and benches - so IsSitting alone