RacesOfValheim/JotunnModStub/Fenrir/FenrirPatches.cs
kanym ffb45d1c1c fix(fenrir): Cultist claws land on the clip's own hits
The single claw dealt its damage at a fixed 0.4 s, before the swing in
ClawAttackCultist (Hit event at 0.64 s of 1.33 s), and released the lock
at 0.5 s, so clicking again started a new claw mid-swing. The double
claw dealt one hit at 0.6 s although DoubleClawAttackCultist swipes
twice (Hit events at 0.64 s and 1.21 s).

BlockPlayerAttackTriggerPatch now counts the form's Hit events
(FenrirForms.AttackEvents) instead of only swallowing them. Both claws
wait for their events, with a timeout, and hold the lock until the clip
ends. Each double-claw swipe deals 60% of Double Claw Damage, 120% in
all for a slower swing that can be cut short.
2026-10-29 02:23:27 +01:00

157 lines
6.7 KiB
C#

using System.Collections;
using BepInEx;
using HarmonyLib;
using Jotunn.Configs;
using Jotunn.Entities;
using Jotunn.Managers;
using UnityEngine;
using UnityEngine.UI; // Added for UI
namespace RacesOfValheim
{
/// <summary>
/// The hidden human does not attack while a form does.
///
/// This patched Character.StartAttack, and it never ran once: Humanoid overrides StartAttack
/// without calling the base, and a player is a Humanoid. Skadi, the Aswid and the Sverm all
/// patch Humanoid, which is why their forms were clean and the Fenrir's were not. So every
/// claw and bite also fired the invisible human's own attack on the same button, and that
/// attack is what a player saw as three separate bugs:
/// - it spent stamina on every swing, and while its animation ran Player.UpdateStats held
/// stamina regen at zero (InAttack), so an empty bar did not come back mid-fight;
/// - a human mid-attack stands still, so the forward_speed the form copies dropped to 0 and
/// the creature fell back to idle in the middle of a fight;
/// - it did so with whatever weapon was in hand.
/// It also only ever looked at SE_FenrirForm, so even working it would have covered the Fenring
/// alone and left the Wolf, the Ulv and the Cultist out.
/// </summary>
[HarmonyPatch(typeof(Humanoid), nameof(Humanoid.StartAttack))]
public static class BlockPlayerLogicAttackPatch
{
public static bool Prefix(Humanoid __instance)
{
return !FenrirForms.LocalInForm(__instance);
}
}
/// <summary>
/// The rest of the human's attack loop, shut while a form is up. StartAttack is the only door an
/// attack starts through and it is blocked above, but a crossbow also lives in the loop around it:
/// PlayerAttackInput reloads it and holds its draw, and the form's own Animator carries
/// CharacterAnimEvent, whose OnAttackTrigger event resolves to the PLAYER (its Character is the first
/// one up the hierarchy) and fires whatever attack the human still has in hand. A tester's crossbow
/// shot on all three form attacks, reloaded for free, like a semi-automatic.
/// </summary>
[HarmonyPatch(typeof(Player), "PlayerAttackInput")]
public static class BlockPlayerAttackInputPatch
{
public static bool Prefix(Player __instance)
{
return !FenrirForms.LocalInForm(__instance);
}
}
[HarmonyPatch(typeof(Humanoid), nameof(Humanoid.OnAttackTrigger))]
public static class BlockPlayerAttackTriggerPatch
{
public static bool Prefix(Humanoid __instance)
{
if (!FenrirForms.LocalInForm(__instance)) return true;
FenrirForms.NotifyAttackTrigger();
return false;
}
}
// --- PATCH 2: BLOCCO ANIMAZIONI ED EFFETTI SONORI NATIVI ---
// Every form now, not the Fenring alone: see BlockPlayerLogicAttackPatch.
[HarmonyPatch(typeof(ZSyncAnimation), nameof(ZSyncAnimation.SetTrigger))]
public static class BlockPlayerAnimationsPatch
{
public static bool Prefix(ZSyncAnimation __instance, string name)
{
if (Player.m_localPlayer != null && __instance.gameObject == Player.m_localPlayer.gameObject)
{
if (FenrirForms.LocalInForm(Player.m_localPlayer))
{
string triggerName = name.ToLower();
if (triggerName.Contains("attack") || triggerName.Contains("swing") || triggerName.Contains("punch"))
{
return false;
}
}
}
return true;
}
}
/// <summary>
/// The hidden human does not block while a form is up either. The forms put their heavy attack
/// on Right Mouse, and on a gamepad RaceInput maps that to JoyBlock, so every heavy bite also
/// raised the human's guard: it turned to face the camera and strafed (the form copies that
/// movement) and regenerated stamina at 80%. A wolf has no shield to raise.
///
/// Cleared after SetControls rather than by answering IsBlocking with false: Player also reads
/// the raw m_blocking flag for the strafe, and the block-and-jump dodge is worked out inside
/// SetControls before this runs, so the dodge still works.
/// </summary>
[HarmonyPatch(typeof(Player), nameof(Player.SetControls))]
public static class BlockPlayerGuardPatch
{
private static readonly AccessTools.FieldRef<Character, bool> Blocking =
AccessTools.FieldRefAccess<Character, bool>("m_blocking");
public static void Postfix(Player __instance)
{
if (FenrirForms.LocalInForm(__instance)) Blocking(__instance) = false;
}
}
[HarmonyPatch]
public static class FenrirDamagePatch
{
[HarmonyTargetMethod]
public static System.Reflection.MethodBase TargetMethod()
{
return System.Linq.Enumerable.FirstOrDefault(
HarmonyLib.AccessTools.GetDeclaredMethods(typeof(ItemDrop.ItemData)),
m => m.Name == "GetDamage" && m.ReturnType == typeof(HitData.DamageTypes)
);
}
public static void Postfix(ItemDrop.ItemData __instance, ref HitData.DamageTypes __result)
{
if (Player.m_localPlayer != null &&
Player.m_localPlayer.GetSEMan().HaveStatusEffect("SE_FenrirCurse".GetStableHashCode()) &&
Player.m_localPlayer.GetInventory().ContainsItem(__instance))
{
int state = BossProgressionManager.GetCurrentProgressionState();
if (state > 0)
{
float bonus = FenrirProgression.GetDamageBonus(state);
if (__result.m_slash > 0) __result.m_slash += bonus;
if (__result.m_blunt > 0) __result.m_blunt += bonus;
if (__result.m_pierce > 0) __result.m_pierce += bonus;
if (__result.m_chop > 0) __result.m_chop += bonus;
if (__result.m_pickaxe > 0) __result.m_pickaxe += bonus;
}
}
}
}
[HarmonyPatch(typeof(Player), nameof(Player.GetBodyArmor))]
public static class FenrirArmorPatch
{
public static void Postfix(Player __instance, ref float __result)
{
if (__instance != null && __instance == Player.m_localPlayer)
{
if (__instance.GetSEMan().HaveStatusEffect("SE_FenrirCurse".GetStableHashCode()))
{
int state = BossProgressionManager.GetCurrentProgressionState();
__result += FenrirProgression.GetArmorBonus(state);
}
}
}
}
}