feat: Trol True Form can block and roll

Right click blocks in True Form with a raised-arms guard, rooted in place.
Block + jump rolls at half distance with the troll's jump animation at
double speed. The slam moves to middle click (secondary attack on a pad).
trolpose is a debug command for trying guard and roll states live.
This commit is contained in:
kanym 2026-10-28 13:53:35 +01:00
commit 3cc1bee2c0
4 changed files with 382 additions and 1 deletions

View file

@ -79,6 +79,8 @@ namespace RacesOfValheim
public static bool LocalActive => localModel != null;
public static Animator LocalAnimator => localModel != null ? localModel.GetComponentInChildren<Animator>() : null;
/// <summary>True while the local troll is in the middle of a swing, a slam or a throw.
/// Trol_TrueFormHoldStill_Patch holds the player still for exactly that long.</summary>
public static bool LocalAttacking { get; set; }
@ -434,6 +436,105 @@ namespace RacesOfValheim
nview.GetZDO().Set(AttackTriggerKey, trigger + "#" + attackSeq);
}
// ─── Guard and roll poses ────────────────────────────────────────────
// The troll has no block or roll of its own, so the guard is one frame of another state held
// still. State names are read off troll_animator with scripts/dump_controller.py: Movement,
// attack, jump, punch, throw, swing log V, swing log H, Stagger, Summon, Sleeping, Wakeup,
// In Water. Both are settable live with the trolpose command until the right frame is found.
public const string MovementState = "Movement";
// Picked in game with trolpose: the troll half risen out of its Wakeup, arms up.
public static string GuardState = "Wakeup";
public static float GuardTime = 0.13f;
/// <summary>The state played whole when the player rolls; null plays nothing and the troll
/// slides along with the roll.</summary>
public static string DodgeState = "jump";
/// <summary>How fast the roll state plays: the troll's jump runs longer than a roll, so at 1
/// the troll was still leaping after the roll had ended.</summary>
public static float DodgeSpeed = 2f;
// The local troll's Animator while its roll state plays, null otherwise.
// Trol_TrueFormDodgeSpeed_Patch keeps its speed up through every FixedUpdate.
private static Animator dodgeAnimator;
private static float dodgeStarted;
/// <summary>Set while the owner's troll holds its guard, to "state|time", so every other
/// client's copy holds the same frame. Empty when it is not guarding.</summary>
public const string GuardKey = "RacesOfValheim_TrolTrueFormGuard";
public static bool HasState(Animator anim, string state)
{
return anim != null && !string.IsNullOrEmpty(state) && anim.HasState(0, Animator.StringToHash("Base Layer." + state));
}
public static void PlayDodge(Animator anim)
{
if (!HasState(anim, DodgeState)) return;
anim.speed = DodgeSpeed;
anim.CrossFadeInFixedTime("Base Layer." + DodgeState, 0.1f, 0, 0f);
dodgeAnimator = anim;
dodgeStarted = Time.time;
}
/// <summary>
/// Called every frame: keeps the roll state at DodgeSpeed while it plays, and puts the speed
/// back once the troll has left it. The first moments are skipped, because the cross-fade
/// into the state only starts on the Animator's next update.
/// </summary>
public static void TickDodge()
{
if (dodgeAnimator == null) return;
int hash = Animator.StringToHash("Base Layer." + DodgeState);
bool inState = dodgeAnimator.GetCurrentAnimatorStateInfo(0).fullPathHash == hash ||
(dodgeAnimator.IsInTransition(0) && dodgeAnimator.GetNextAnimatorStateInfo(0).fullPathHash == hash);
if (inState || Time.time - dodgeStarted < 0.15f) dodgeAnimator.speed = DodgeSpeed;
else EndDodge();
}
public static void EndDodge()
{
if (dodgeAnimator != null) dodgeAnimator.speed = 1f;
dodgeAnimator = null;
}
/// <summary>The troll's CharacterAnimEvent sets its Animator back to speed 1 in every
/// FixedUpdate; this sets the roll's speed again right after.</summary>
[HarmonyPatch(typeof(CharacterAnimEvent), nameof(CharacterAnimEvent.CustomFixedUpdate))]
public static class Trol_TrueFormDodgeSpeed_Patch
{
public static void Postfix(CharacterAnimEvent __instance)
{
if (dodgeAnimator != null && __instance.gameObject == dodgeAnimator.gameObject) dodgeAnimator.speed = DodgeSpeed;
}
}
public static void PublishGuard(Player player, bool on)
{
ZNetView nview = player != null ? player.GetComponent<ZNetView>() : null;
if (nview == null || !nview.IsValid()) return;
string value = on ? GuardState + "|" + GuardTime.ToString(System.Globalization.CultureInfo.InvariantCulture) : "";
if (nview.GetZDO().GetString(GuardKey, "") != value) nview.GetZDO().Set(GuardKey, value);
}
/// <summary>The pose another client's troll should hold, read back off the owner's ZDO.</summary>
public static bool ReadGuard(ZDO zdo, out string state, out float time)
{
state = null;
time = 0f;
string value = zdo.GetString(GuardKey, "");
int bar = value.LastIndexOf('|');
if (bar <= 0) return false;
state = value.Substring(0, bar);
return float.TryParse(value.Substring(bar + 1), System.Globalization.NumberStyles.Float,
System.Globalization.CultureInfo.InvariantCulture, out time);
}
/// <summary>
/// The damage a troll attack deals for this player right now: the troll's own numbers, times
/// Troll Attack Damage, times the same random Clubs factor a club would get.
@ -465,6 +566,35 @@ namespace RacesOfValheim
crouch = false;
dodge = false;
}
/// <summary>
/// Rooted while guarding, so the held pose cannot slide across the ground. Cleared after
/// SetControls, not before: the block-and-jump roll takes its direction from the move
/// input inside SetControls, and an input zeroed first sent every roll backwards.
/// </summary>
public static void Postfix(Player __instance)
{
if (__instance != Player.m_localPlayer || !LocalActive || LocalAttacking) return;
if (__instance.IsBlocking() && !__instance.InDodge()) MoveDir(__instance) = Vector3.zero;
}
private static readonly AccessTools.FieldRef<Character, Vector3> MoveDir =
AccessTools.FieldRefAccess<Character, Vector3>("m_moveDir");
}
// Share of a normal roll's distance the troll covers. The roll moves by root motion, and
// under True Form two animators feed it: the hidden human's dodge and the troll's jump.
private const float RollDistance = 0.5f;
/// <summary>A shorter roll for the troll: every bit of root motion that reaches the player
/// during a True Form roll is scaled, whichever of the two clips it came from.</summary>
[HarmonyPatch(typeof(Character), nameof(Character.AddRootMotion))]
public static class Trol_TrueFormRollDistance_Patch
{
public static void Prefix(Character __instance, ref Vector3 vel)
{
if (__instance == Player.m_localPlayer && LocalActive && __instance.InDodge()) vel *= RollDistance;
}
}
[HarmonyPatch(typeof(Humanoid), "StartAttack")]
@ -502,4 +632,86 @@ namespace RacesOfValheim
}
}
}
/// <summary>
/// Holds one troll at one frame of a state, for the True Form guard. It blends in the way it
/// blends out: a cross-fade into the frame first, then the frame pinned once the fade is over.
/// Pinned again every frame it holds: the troll's own CharacterAnimEvent puts the Animator back
/// to speed 1 in every FixedUpdate, so a speed of 0 set once would not stay.
/// </summary>
internal class TrolGuardPose
{
private const float Fade = 0.2f;
// Each state's length, learned the first time it is held: the fade starts that far ahead of
// the frame, so the state lands on it when the fade ends instead of running past it.
private static readonly Dictionary<int, float> lengths = new Dictionary<int, float>();
private int state;
private float target;
private float fadeStarted;
private bool fading;
private float pinned;
public void Hold(Animator anim, string name, float time)
{
if (!TrolTrueFormModel.HasState(anim, name)) return;
int hash = Animator.StringToHash("Base Layer." + name);
time = Mathf.Clamp01(time);
if (hash != state || time != target)
{
state = hash;
target = time;
StartFade(anim);
}
if (fading)
{
anim.speed = 1f;
if (Time.time - fadeStarted < Fade || anim.IsInTransition(0)) return;
AnimatorStateInfo info = anim.GetCurrentAnimatorStateInfo(0);
if (info.fullPathHash == state && info.length > 0f)
{
lengths[state] = info.length;
pinned = info.normalizedTime < 1f ? info.normalizedTime : target;
}
else
{
pinned = target;
}
fading = false;
}
anim.Play(state, 0, pinned);
anim.speed = 0f;
}
private void StartFade(Animator anim)
{
// CrossFade takes its duration as a share of the state being left, so turn the seconds
// into that share.
float from = anim.GetCurrentAnimatorStateInfo(0).length;
float lead = lengths.TryGetValue(state, out float length) && length > 0f ? Fade / length : 0f;
anim.speed = 1f;
anim.CrossFade(state, from > 0f ? Fade / from : Fade, 0, Mathf.Max(0f, target - lead));
fadeStarted = Time.time;
fading = true;
}
public void Release(Animator anim)
{
state = 0;
fading = false;
if (anim == null) return;
anim.speed = 1f;
if (TrolTrueFormModel.HasState(anim, TrolTrueFormModel.MovementState))
{
anim.CrossFadeInFixedTime("Base Layer." + TrolTrueFormModel.MovementState, Fade, 0);
}
}
}
}