diff --git a/JotunnModStub/Sverm/SvermRavagerForm.cs b/JotunnModStub/Sverm/SvermRavagerForm.cs index 68caf14..01ad562 100644 --- a/JotunnModStub/Sverm/SvermRavagerForm.cs +++ b/JotunnModStub/Sverm/SvermRavagerForm.cs @@ -585,8 +585,19 @@ namespace RacesOfValheim // present, so PlayAttack does not pass a name to SetTrigger the controller cannot use. private readonly System.Collections.Generic.HashSet knownTriggers = new System.Collections.Generic.HashSet(); + // The two front claws (bone names read from the SeekerBrute prefab with UnityPy). The slam + // brings one down, then the other; each Hit lands under whichever claw just came down. + private Transform leftClaw; + private Transform rightClaw; + private void Start() { + foreach (Transform bone in GetComponentsInChildren(true)) + { + if (bone.name == "l.frontfoot") leftClaw = bone; + else if (bone.name == "r.frontfoot") rightClaw = bone; + } + anim = GetComponentInChildren(); if (anim == null || anim.runtimeAnimatorController == null) return; @@ -685,15 +696,15 @@ namespace RacesOfValheim // be the only reliable option. if (RaceInput.AttackDown(1)) { - StartCoroutine(AttackRoutine(SvermRavagerAttacks.Ram, 2f, 0.3f, 0.7f, false)); + StartCoroutine(AttackRoutine(SvermRavagerAttacks.Ram, "Attack Ram", 2f, false)); } else if (RaceInput.AttackDown(0)) { - StartCoroutine(AttackRoutine(SvermRavagerAttacks.Bite, 2f, 0.3f, 0.6f, false)); + StartCoroutine(AttackRoutine(SvermRavagerAttacks.Bite, "Attack Bite", 2f, false)); } else if (RaceInput.AttackDown(2)) { - StartCoroutine(AttackRoutine(SvermRavagerAttacks.Slam, 1.2f, 0.4f, 1.0f, true)); + StartCoroutine(AttackRoutine(SvermRavagerAttacks.Slam, "Attack Slam", 1.2f, true)); } else if ((SvermConfigManagement.RavagerKey.Value.IsDown() || RaceInput.ConsumePress(SvermConfigManagement.RavagerKey.Value)) && Time.time >= nextTauntAt) @@ -704,31 +715,33 @@ namespace RacesOfValheim } /// - /// One swing: play the clip, wait for the moment it should land, hit whatever is there, - /// then hold isAttacking until the swing is fully spent. FenrirFormBehavior.ClawRoutine in - /// every particular except the damage itself, which here is the Brute's own real numbers - /// (SvermRavagerAttacks) instead of one tuned float. + /// One swing, timed by the clip itself instead of by fixed delays. Each "Hit" animation + /// event in the clip is a hit here, at the same point of the clip (the bite and the ram + /// have one, the slam has two, one per claw). The player is held still until the + /// animator starts blending out of the attack state, which is when the real Brute can + /// move again. /// - private System.Collections.IEnumerator AttackRoutine(SvermRavagerAttacks.Profile profile, float radius, - float hitDelay, float lockDuration, bool digs) + private System.Collections.IEnumerator AttackRoutine(SvermRavagerAttacks.Profile profile, string stateName, + float radius, bool slam) { isAttacking = true; + bool played = StartAttackState(profile); - if (hasForwardSpeed) anim.SetFloat("forward_speed", 0f); + int dropSamples = 0; + float[] leftY = new float[ClawHistory]; + float[] rightY = new float[ClawHistory]; - if (!string.IsNullOrEmpty(profile.Trigger) && knownTriggers.Contains(profile.Trigger)) + yield return FollowAttackState(stateName, played, 0f, () => { - anim.SetTrigger(profile.Trigger); - SvermRavagerForm.PublishAttackTrigger(Player.m_localPlayer, profile.Trigger); - } - - yield return new WaitForSeconds(hitDelay); - - ExecuteDamage(profile, radius); - if (digs) DigCrater(); - - float remaining = lockDuration - hitDelay; - if (remaining > 0f) yield return new WaitForSeconds(remaining); + if (slam) Slam(profile, radius, leftY, rightY, dropSamples); + else ExecuteDamage(profile, radius); + }, () => + { + if (leftClaw == null || rightClaw == null) return; + leftY[dropSamples % ClawHistory] = leftClaw.position.y; + rightY[dropSamples % ClawHistory] = rightClaw.position.y; + dropSamples++; + }); isAttacking = false; } @@ -738,37 +751,159 @@ namespace RacesOfValheim /// swing - the roar reaches out in every direction, so the sphere is centred on the /// player instead of offset ahead of them the way AttackRoutine's three do. Deliberately /// silent: no message, no status effect, nothing in a corner of the screen - just the - /// animation, the sound if one resolves, and the hit. + /// animation, the sound if one resolves, and the hit. The clip has no Hit event, so the + /// roar lands at TauntHitPoint of the clip. /// private System.Collections.IEnumerator TauntRoutine() { isAttacking = true; SvermRavagerAttacks.Profile profile = SvermRavagerAttacks.Taunt; - - if (hasForwardSpeed) anim.SetFloat("forward_speed", 0f); - - if (!string.IsNullOrEmpty(profile.Trigger) && knownTriggers.Contains(profile.Trigger)) - { - anim.SetTrigger(profile.Trigger); - SvermRavagerForm.PublishAttackTrigger(Player.m_localPlayer, profile.Trigger); - } + bool played = StartAttackState(profile); PlaySound(SvermConfig.RavagerTauntSoundEffects); - yield return new WaitForSeconds(0.3f); - - Player player = Player.m_localPlayer; - if (player != null) + yield return FollowAttackState("Taunt", played, TauntHitPoint, () => { - ApplyDamageInSphere(player.transform.position + Vector3.up, SvermConfig.RavagerTauntRadius, profile.Damage); - } - - yield return new WaitForSeconds(0.4f); + Player player = Player.m_localPlayer; + if (player != null) + ApplyDamageInSphere(player.transform.position + Vector3.up, SvermConfig.RavagerTauntRadius, profile.Damage); + }, null); isAttacking = false; } + // 0.3 s into the 2.67 s taunt clip, where the old fixed delay put the roar's hit. + private const float TauntHitPoint = 0.11f; + + // Frames of claw height kept to tell which claw just came down (about 0.2 s at 60 fps). + private const int ClawHistory = 12; + + private bool StartAttackState(SvermRavagerAttacks.Profile profile) + { + if (hasForwardSpeed) anim.SetFloat("forward_speed", 0f); + + if (string.IsNullOrEmpty(profile.Trigger) || !knownTriggers.Contains(profile.Trigger)) return false; + + anim.SetTrigger(profile.Trigger); + SvermRavagerForm.PublishAttackTrigger(Player.m_localPlayer, profile.Trigger); + return true; + } + + /// + /// Waits out one attack state frame by frame. Calls onHit at every Hit event of the + /// state's clip (or once at fallbackHit, a 0-1 point of the clip, when the clip has + /// none), and returns once the animator starts leaving the state. If the state never + /// starts (trigger missing), it hits once and returns, so the attack still works. + /// + private System.Collections.IEnumerator FollowAttackState(string stateName, bool played, float fallbackHit, + System.Action onHit, System.Action eachFrame) + { + float startedAt = Time.time; + bool entered = false; + float[] hits = null; + int nextHit = 0; + + while (played && Time.time - startedAt < 8f) + { + eachFrame?.Invoke(); + + AnimatorStateInfo state; + AnimatorClipInfo[] clips; + bool leaving; + if (!TryGetAttackState(stateName, out state, out clips, out leaving)) + { + if (entered || Time.time - startedAt > 0.5f) break; + yield return null; + continue; + } + + entered = true; + if (hits == null) hits = ReadHitPoints(clips, fallbackHit); + + while (nextHit < hits.Length && state.normalizedTime >= hits[nextHit]) + { + onHit(); + nextHit++; + } + + if (leaving) break; + yield return null; + } + + if (!entered) onHit(); + } + + private bool TryGetAttackState(string stateName, out AnimatorStateInfo state, out AnimatorClipInfo[] clips, out bool leaving) + { + state = anim.GetCurrentAnimatorStateInfo(0); + if (state.IsName(stateName)) + { + clips = anim.GetCurrentAnimatorClipInfo(0); + leaving = anim.IsInTransition(0); + return true; + } + + leaving = false; + if (anim.IsInTransition(0)) + { + state = anim.GetNextAnimatorStateInfo(0); + if (state.IsName(stateName)) + { + clips = anim.GetNextAnimatorClipInfo(0); + return true; + } + } + + clips = null; + return false; + } + + private static float[] ReadHitPoints(AnimatorClipInfo[] clips, float fallbackHit) + { + var points = new System.Collections.Generic.List(); + if (clips != null && clips.Length > 0 && clips[0].clip != null && clips[0].clip.length > 0f) + { + AnimationClip clip = clips[0].clip; + foreach (AnimationEvent ev in clip.events) + if (ev.functionName == "Hit") points.Add(ev.time / clip.length); + } + + if (points.Count == 0) points.Add(fallbackHit); + points.Sort(); + return points.ToArray(); + } + + /// + /// One claw of the slam landing: the claw that dropped the most over the last few frames + /// is the one that just struck. Damage and crater go under it, not at a fixed point ahead. + /// + private void Slam(SvermRavagerAttacks.Profile profile, float radius, float[] leftY, float[] rightY, int samples) + { + Transform claw = StrikingClaw(leftY, rightY, samples); + if (claw == null) + { + ExecuteDamage(profile, radius); + Player player = Player.m_localPlayer; + if (player != null) + DigCrater(player.transform.position + player.transform.forward * SvermConfigManagement.RavagerAttackOffset); + return; + } + + ApplyDamageInSphere(claw.position + Vector3.up * 0.5f, radius, profile.Damage); + DigCrater(claw.position); + } + + private Transform StrikingClaw(float[] leftY, float[] rightY, int samples) + { + if (leftClaw == null || rightClaw == null || samples == 0) return null; + + int oldest = samples < ClawHistory ? 0 : samples % ClawHistory; + float leftDrop = leftY[oldest] - leftClaw.position.y; + float rightDrop = rightY[oldest] - rightClaw.position.y; + return leftDrop >= rightDrop ? leftClaw : rightClaw; + } + /// Tries each candidate in order and plays the first that resolves, same as SvermRavagerForm.PlayEffect. private static void PlaySound(string[] candidates) { @@ -807,12 +942,10 @@ namespace RacesOfValheim /// a pickaxe's dig on bare terrain only. TerrainOp does not check wards on its own, so the /// ward check is done here. /// - private static void DigCrater() + private static void DigCrater(Vector3 impact) { - Player player = Player.m_localPlayer; - if (player == null || !SvermConfigManagement.RavagerSlamCrater) return; + if (Player.m_localPlayer == null || !SvermConfigManagement.RavagerSlamCrater) return; - Vector3 impact = player.transform.position + player.transform.forward * SvermConfigManagement.RavagerAttackOffset; if (!Physics.Raycast(impact + Vector3.up * 3f, Vector3.down, out RaycastHit ground, 8f, GroundMask)) return; if (ground.collider == null || ground.collider.GetComponent() == null) return; if (!PrivateArea.CheckAccess(ground.point, 0f, false)) return;