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; }