fix: a stagger cancels the form's attack

A form swing waits for its clip's hit event and lands on a timeout if none comes,
so a stagger that cut the clip short still dealt the blow. Attacks are marked as
they start (TrySpendStamina, Ravager AttackRoutine) and their hits are dropped if
a stagger began since. No attack or Cultist breath starts while staggered.
This commit is contained in:
kanym 2026-10-29 13:53:04 +01:00
commit beffd0bee6
5 changed files with 28 additions and 7 deletions

View file

@ -726,7 +726,7 @@ namespace RacesOfValheim
private void ExecuteClawDamage(float baseDamage)
{
if (Player.m_localPlayer == null) return;
if (Player.m_localPlayer == null || FormStagger.AttackInterrupted) return;
Vector3 playerPos = Player.m_localPlayer.transform.position;
Vector3 attackPoint = playerPos + Player.m_localPlayer.transform.forward * 1.5f + Vector3.up * 1f;
@ -842,7 +842,7 @@ namespace RacesOfValheim
bool canAct = RaceInput.Allowed;
// Fire attack on Right Click
if (canAct && RaceInput.AttackHeld(1))
if (canAct && RaceInput.AttackHeld(1) && !Player.m_localPlayer.IsStaggering())
{
if (Player.m_localPlayer.GetEitr() > 0)
{
@ -1161,7 +1161,7 @@ namespace RacesOfValheim
private void ExecuteClawDamage(float baseDamage)
{
if (Player.m_localPlayer == null) return;
if (Player.m_localPlayer == null || FormStagger.AttackInterrupted) return;
Vector3 playerPos = Player.m_localPlayer.transform.position;
Vector3 attackPoint = playerPos + Player.m_localPlayer.transform.forward * 1.5f + Vector3.up * 1f;
@ -1375,7 +1375,7 @@ namespace RacesOfValheim
private void ExecuteBiteDamage(float baseDamage)
{
if (Player.m_localPlayer == null) return;
if (Player.m_localPlayer == null || FormStagger.AttackInterrupted) return;
Vector3 playerPos = Player.m_localPlayer.transform.position;
// Il lupo è basso, abbassiamo l'altezza dell'attacco
@ -1584,7 +1584,7 @@ namespace RacesOfValheim
private void ExecuteBiteDamage(float baseDamage)
{
if (Player.m_localPlayer == null) return;
if (Player.m_localPlayer == null || FormStagger.AttackInterrupted) return;
Vector3 playerPos = Player.m_localPlayer.transform.position;
Vector3 attackPoint = playerPos + Player.m_localPlayer.transform.forward * 1.5f + Vector3.up * 0.5f;

View file

@ -373,10 +373,14 @@ namespace RacesOfValheim
/// the equipment modifier, status effects (meads, race passives), then up to a third off with
/// the attack's skill. Without enough, the attack does not start and the stamina bar flashes,
/// as it does for a weapon. A cost of 0 is free and always goes through.
/// A staggered player cannot attack at all, and an attack that may start is marked for
/// FormStagger, so a stagger before its hit cancels it.
/// </summary>
/// <returns>True if the attack may start; the stamina is already spent.</returns>
public static bool TrySpendStamina(Player player, float baseCost, Skills.SkillType skill)
{
if (player != null && player.IsStaggering()) return false;
FormStagger.MarkAttack();
if (player == null || baseCost <= 0f) return true;
float cost = baseCost * (1f + player.GetEquipmentAttackStaminaModifier());

View file

@ -21,6 +21,11 @@ namespace RacesOfValheim
/// 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.
///
/// A stagger also cancels the local form's attack, as it cancels a weapon's in vanilla. The
/// form's swing waits for its clip's hit event and lands on a timeout if none comes, so a
/// stagger that cut the clip short still dealt the blow 1.5 s later. MarkAttack is called as an
/// attack starts, and AttackInterrupted tells the hit whether a stagger began since.
/// </summary>
internal static class FormStagger
{
@ -32,6 +37,15 @@ namespace RacesOfValheim
private static readonly HashSet<int> staggering = new HashSet<int>();
private static readonly List<Animator> animators = new List<Animator>();
// Staggers of the local player so far, and the count when the current attack started.
private static int localStaggers;
private static int attackMark;
public static void MarkAttack() => attackMark = localStaggers;
/// <summary>True if the local player has been staggered since the last MarkAttack.</summary>
public static bool AttackInterrupted => localStaggers != attackMark;
public static void Tick(Player player)
{
int id = player.GetInstanceID();
@ -41,6 +55,7 @@ namespace RacesOfValheim
return;
}
if (!staggering.Add(id)) return;
if (player == Player.m_localPlayer) localStaggers++;
Animator own = AnimatorRef(player);
RuntimeAnimatorController ownController = own != null ? own.runtimeAnimatorController : null;

View file

@ -744,7 +744,7 @@ namespace RacesOfValheim
wasGrounded = grounded;
if (!RaceInput.Allowed || !grounded) return;
if (!RaceInput.Allowed || !grounded || player.IsStaggering()) return;
// Right click rams, left click bites, middle click slams. All three read the device
// directly (mouse, or its gamepad counterpart - see RaceInput.AttackDown) rather than
@ -782,6 +782,7 @@ namespace RacesOfValheim
float radius, bool slam, bool lunge)
{
isAttacking = true;
FormStagger.MarkAttack();
bool played = StartAttackState(profile);
if (rootMotion != null) rootMotion.Active = lunge;
@ -791,6 +792,7 @@ namespace RacesOfValheim
yield return FollowAttackState(stateName, played, 0f, () =>
{
if (FormStagger.AttackInterrupted) return;
if (slam) Slam(profile, radius, leftY, rightY, dropSamples);
else ExecuteDamage(profile, radius);
}, () =>

View file

@ -211,7 +211,7 @@ namespace RacesOfValheim
yield return null;
}
if (player != null && !player.IsDead())
if (player != null && !player.IsDead() && !FormStagger.AttackInterrupted)
{
float scale = transform.localScale.x;
int landed;