RacesOfValheim/JotunnModStub/Fenrir/FenrirFormShared.cs
kanym b4b6fe3758 fix(fenrir): hidden human no longer attacks or blocks under a form
The attack block patched Character.StartAttack, which Humanoid overrides
without calling the base, so it never ran; and it covered the Fenring only.
Every form swing also fired the hidden human's attack: stamina spent,
regen held at zero while it played, and forward_speed dropped to 0. Patch
Humanoid.StartAttack for all four forms, and clear m_blocking after
SetControls so the pad's heavy attack stops raising the guard.

Forms treat the player as airborne only after 0.25 s off the ground, so
bumps no longer flicker run to idle. Melee reach ignores the target's own
colliders and accepts a clear line from the eyes or the attack point, with
an opt-in hit log. Sunrise names the key that still works by day.
2026-09-28 22:19:37 +02:00

129 lines
6.2 KiB
C#

using UnityEngine;
namespace RacesOfValheim
{
/// <summary>What every Fenrir form shares: whether the local player wears one at all.</summary>
internal static class FenrirForms
{
private static readonly int FenringHash = "SE_FenrirForm".GetStableHashCode();
private static readonly int WolfHash = "SE_WolfForm".GetStableHashCode();
private static readonly int CultistHash = "SE_CultistForm".GetStableHashCode();
/// <summary>
/// Any of the four forms. The Wolf and the Ulv share SE_WolfForm, the Fenring has
/// SE_FenrirForm and the Cultist SE_CultistForm. Checking only SE_FenrirForm, as the attack
/// block used to, left three of the four forms out.
/// </summary>
public static bool LocalInForm(Character character)
{
if (character == null || character != Player.m_localPlayer) return false;
SEMan seman = character.GetSEMan();
if (seman == null) return false;
return seman.HaveStatusEffect(FenringHash) || seman.HaveStatusEffect(WolfHash) || seman.HaveStatusEffect(CultistHash);
}
}
/// <summary>
/// The movement half of a form's Animator, copied from the hidden player's. Shared by the four
/// local behaviours and RemoteFenrirAnimator so both views of the same wolf agree.
///
/// WHY THE GRACE PERIOD. Valheim writes onGround = IsOnGround() every frame, and running over
/// uneven ground that drops to false for a frame or two at every bump. The player's own
/// controller rides that out on its transitions. The forms used to read it raw and zero the
/// speed the same frame, so the creature dropped from run to idle and back about once a second
/// on any hillside - the flicker a player reported in all four forms.
/// </summary>
internal class FormLocomotion
{
// Long enough to cover a bump, short enough that a real jump still reads as one.
private const float AirborneGrace = 0.25f;
private float airborneFor;
/// <summary>Copies the movement parameters, and says whether the form counts as grounded.</summary>
public bool Mirror(Animator form, Animator player, bool crouching)
{
bool touching = player.GetBool("onGround");
airborneFor = touching ? 0f : airborneFor + Time.deltaTime;
bool grounded = airborneFor <= AirborneGrace;
// try/catch as the behaviours always did it: a controller missing one of these must not
// throw once per frame.
try { form.SetBool("onGround", grounded); } catch {}
try { form.SetBool("inWater", player.GetBool("inWater")); } catch {}
// Airborne and crouching both read as standing still, as before.
bool still = !grounded || crouching;
try { form.SetFloat("forward_speed", still ? 0f : player.GetFloat("forward_speed")); } catch {}
try { form.SetFloat("sideway_speed", still ? 0f : player.GetFloat("sideway_speed")); } catch {}
return grounded;
}
}
/// <summary>
/// Whether a melee swing of a form can reach what it found, shared by the four claw and bite
/// attacks.
///
/// The old test was one Linecast from the eyes to the centre of the collider, against terrain,
/// pieces and the Default layer. Two ways it threw hits away: the line clipped the ground on
/// any slope or against anything lower than the eyes, and against a tree, a rock or a nest the
/// line hit the target's own collider - those live on exactly the layers it tested - so they
/// could never be damaged at all. Now the target's own colliders do not count as cover, and a
/// swing lands if either the eyes or the point of the attack have a clear line to it.
/// </summary>
internal static class FormMelee
{
private static readonly int CoverMask = LayerMask.GetMask("Default", "static_solid", "terrain", "piece");
private static readonly RaycastHit[] rayHits = new RaycastHit[16];
public static bool CanReach(Vector3 eye, Vector3 attackPoint, Collider hit, IDestructible target, out string blockedBy)
{
Bounds bounds = hit.bounds;
if (Clear(eye, bounds.center, target, out blockedBy)) return true;
return Clear(attackPoint, bounds.ClosestPoint(attackPoint), target, out _);
}
private static bool Clear(Vector3 from, Vector3 to, IDestructible target, out string blockedBy)
{
blockedBy = null;
Vector3 delta = to - from;
float distance = delta.magnitude;
if (distance < 0.01f) return true;
int count = Physics.RaycastNonAlloc(from, delta / distance, rayHits, distance, CoverMask);
for (int i = 0; i < count; i++)
{
Collider cover = rayHits[i].collider;
if (cover == null) continue;
if (ReferenceEquals(cover.GetComponentInParent<IDestructible>(), target)) continue;
blockedBy = cover.name;
return false;
}
return true;
}
/// <summary>
/// One line per rejected candidate while "Debug: Log Form Hits" is on, so the next "my bite
/// did nothing" report comes with the reason in the log.
/// </summary>
public static void LogMiss(string form, Collider hit, string reason)
{
if (!FenrirConfig.DebugFormHits.Value) return;
Jotunn.Logger.LogInfo($"[FenrirForm] {form} swing skipped '{hit.name}' (layer {LayerMask.LayerToName(hit.gameObject.layer)}): {reason}.");
}
public static void LogHit(string form, IDestructible target, float damage)
{
if (!FenrirConfig.DebugFormHits.Value) return;
string name = target is MonoBehaviour behaviour && behaviour != null ? behaviour.name : target.ToString();
Jotunn.Logger.LogInfo($"[FenrirForm] {form} swing hit '{name}' for {damage:0}.");
}
public static void LogSwing(string form, int candidates)
{
if (!FenrirConfig.DebugFormHits.Value) return;
Jotunn.Logger.LogInfo($"[FenrirForm] {form} swing: {candidates} collider(s) in reach.");
}
}
}