fix: forms play their own stagger animation

The player's stagger trigger only reached the hidden human's Animator. FormStagger
fires the form's own vanilla stagger as it starts, for every form whose controller
has one (Trol True Form, the Fenrir forms, Ravager, Aswid bat), on every client.
This commit is contained in:
kanym 2026-10-29 13:28:01 +01:00
commit dd92bdf345

View file

@ -0,0 +1,80 @@
using System.Collections.Generic;
using HarmonyLib;
using UnityEngine;
namespace RacesOfValheim
{
/// <summary>
/// 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.
/// </summary>
internal static class FormStagger
{
private const string Trigger = "stagger";
private static readonly AccessTools.FieldRef<Character, Animator> AnimatorRef =
AccessTools.FieldRefAccess<Character, Animator>("m_animator");
private static readonly HashSet<int> staggering = new HashSet<int>();
private static readonly List<Animator> animators = new List<Animator>();
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);
}
}
}
}