diff --git a/JotunnModStub/FormStagger.cs b/JotunnModStub/FormStagger.cs new file mode 100644 index 0000000..4543ef6 --- /dev/null +++ b/JotunnModStub/FormStagger.cs @@ -0,0 +1,80 @@ +using System.Collections.Generic; +using HarmonyLib; +using UnityEngine; + +namespace RacesOfValheim +{ + /// + /// A staggered player in a form plays the form's own stagger. + /// + /// Vanilla staggers a character with SetTrigger("stagger") on its own Animator (Character.RPC_Stagger), + /// and that Animator belongs to the hidden human. The form's Animator never heard of it, so the + /// True Form troll stood idle while the player could not move. A tester saw it. + /// + /// Every creature worn as a form has the vanilla stagger ready to play (read offline with + /// scripts/dump_controller.py, bundle c4210710): a "stagger" trigger and an AnyState -> Stagger + /// transition in troll_animator, fenring_animator, fenring_cultist_animator, wolf_animator, + /// ulv_animator, SeekerBrute_animator (the Sverm Ravager) and bat_animator (the Aswid bat). + /// gd_king_animator (the Muspell) and hatchling_animator (the Skadi drake) have none, and are + /// left alone by the parameter check. + /// + /// The cue is Character.IsStaggering(), which reads the player's Animator state. The player's + /// triggers are replicated, so this holds for onlookers' copies of the form too, with nothing + /// published of its own. Fired once, as the stagger starts. + /// + internal static class FormStagger + { + private const string Trigger = "stagger"; + + private static readonly AccessTools.FieldRef AnimatorRef = + AccessTools.FieldRefAccess("m_animator"); + + private static readonly HashSet staggering = new HashSet(); + private static readonly List animators = new List(); + + public static void Tick(Player player) + { + int id = player.GetInstanceID(); + if (!player.IsStaggering()) + { + staggering.Remove(id); + return; + } + if (!staggering.Add(id)) return; + + Animator own = AnimatorRef(player); + RuntimeAnimatorController ownController = own != null ? own.runtimeAnimatorController : null; + + player.GetComponentsInChildren(false, animators); + foreach (Animator anim in animators) + { + // The human's own Animator staggers already, and so does anything driven by a copy of + // its controller (a VRM model). + if (anim == own || anim.runtimeAnimatorController == null) continue; + if (anim.runtimeAnimatorController == ownController) continue; + if (!anim.isActiveAndEnabled || !HasTrigger(anim)) continue; + + anim.SetTrigger(Trigger); + } + animators.Clear(); + } + + private static bool HasTrigger(Animator anim) + { + foreach (AnimatorControllerParameter p in anim.parameters) + { + if (p.type == AnimatorControllerParameterType.Trigger && p.name == Trigger) return true; + } + return false; + } + + [HarmonyPatch(typeof(Player), "LateUpdate")] + public static class Player_LateUpdate_FormStagger_Patch + { + public static void Postfix(Player __instance) + { + FormStagger.Tick(__instance); + } + } + } +}