diff --git a/JotunnModStub/Fenrir/FenrirBehaviors.cs b/JotunnModStub/Fenrir/FenrirBehaviors.cs index 40901e4..8a4871b 100644 --- a/JotunnModStub/Fenrir/FenrirBehaviors.cs +++ b/JotunnModStub/Fenrir/FenrirBehaviors.cs @@ -429,6 +429,8 @@ namespace RacesOfValheim private ZNetView nview; private string lastAttack; private GameObject flames; + private bool breathing; + private float breathStarted; private readonly FormLocomotion locomotion = new FormLocomotion(); // The flames hang off the player, not this model, so they outlive it unless taken down. @@ -469,18 +471,31 @@ namespace RacesOfValheim bool fire = nview.GetZDO().GetBool(FenrirAttackSync.FireKey, false); - // The breath itself: up while the flag is, gone when it drops. Only the cultist ever - // sets the flag, so no other form can grow flames here. - if (fire && flames == null) + // The flag is up for the whole hold, wind-up included, as on the owner's side; the + // flames come at the same point of the same clip (CultistBreathClip). Only the cultist + // ever sets the flag, so no other form can grow flames here. + if (fire && !breathing) { - flames = FenrirAttackSync.SpawnFlames(player); + breathing = true; + breathStarted = Time.time; // The roar that opens the breath, once per start, the same cue the owner hears. // "sfx_cultist_attack_fire_start" was asked for here and there is no sfx_cultist_* // prefab in the game at all; the fenring's own fire-claw snarl is the real one. FenrirCreatureSound.PlayFireRoar(player.transform.position); } - else if (!fire && flames != null) { Destroy(flames); flames = null; } + if (fire) + { + CultistBreathClip.Hold(formAnim); + if (flames == null && CultistBreathClip.FlamesDue(formAnim, breathStarted)) + flames = FenrirAttackSync.SpawnFlames(player); + } + else if (breathing) + { + breathing = false; + CultistBreathClip.Release(formAnim, player); + if (flames != null) { Destroy(flames); flames = null; } + } foreach (AnimatorControllerParameter p in formAnim.parameters) { @@ -841,29 +856,22 @@ namespace RacesOfValheim // still falls through to StopFire() instead of leaving the flames running. bool canAct = RaceInput.Allowed; - // Fire attack on Right Click - if (canAct && RaceInput.AttackHeld(1) && !Player.m_localPlayer.IsStaggering()) + // Fire attack on Right Click, held. + bool wantFire = canAct && RaceInput.AttackHeld(1) && !Player.m_localPlayer.IsStaggering(); + if (wantFire && Player.m_localPlayer.GetEitr() <= 0f) { - if (Player.m_localPlayer.GetEitr() > 0) - { - if (!isShootingFire) - { - StartFire(); - } - else - { - UpdateFire(); - } - } - else - { - // Held with an empty pool. Until now this did nothing whatsoever: no - // flames, no sound, no reason given - and the form's own tier is Yagluth - // while eitr is a Mistlands thing, so a Cultist meets this before it ever - // meets the fire. - SayNoEitr(); - if (isShootingFire) StopFire(); - } + // Held with an empty pool. Until now this did nothing whatsoever: no + // flames, no sound, no reason given - and the form's own tier is Yagluth + // while eitr is a Mistlands thing, so a Cultist meets this before it ever + // meets the fire. + SayNoEitr(); + wantFire = false; + } + + if (wantFire) + { + if (!isShootingFire) StartFire(); + else UpdateFire(); } else if (isShootingFire) { @@ -943,31 +951,50 @@ namespace RacesOfValheim ModText.Get("rov_msg_fenrir_only_breath_comes_out_fire")); } + // The breath runs on the flame clip (CultistBreathClip): pressed, the clip starts; the flames, + // the eitr drain and the damage begin at its own Hit point; released, the clip is cut. + private bool flaming; + private float fireStarted; + + /// True while the local Cultist holds its breath attack, wind-up included. Read by + /// Cultist_BreathFacesCamera_Patch. + public static bool LocalBreathing { get; private set; } + private void StartFire() { if (Player.m_localPlayer == null || isShootingFire) return; isShootingFire = true; + LocalBreathing = true; + flaming = false; + fireStarted = Time.time; + fireTimer = 0f; FormAnimGuard.LeaveHowl(cultistAnim); cultistAnim.SetFloat("forward_speed", 0f); cultistAnim.SetFloat("sideway_speed", 0f); - cultistAnim.SetBool("attack_fire", true); FenrirAttackSync.PublishFire(true); - + // Same cue the remote mirror plays - see the fire branch in the sync behaviour above. FenrirCreatureSound.PlayFireRoar(Player.m_localPlayer.transform.position); cultistAnim.SetTrigger("attack_flames"); FenrirAttackSync.Publish("attack_flames"); - - if (fireEffectInstance == null) fireEffectInstance = FenrirAttackSync.SpawnFlames(Player.m_localPlayer); } private void UpdateFire() { - Player.m_localPlayer.UseEitr(FenrirConfig.CultistFireEitrDrainPerSecond.Value * Time.deltaTime); cultistAnim.SetFloat("forward_speed", 0f); cultistAnim.SetFloat("sideway_speed", 0f); - + CultistBreathClip.Hold(cultistAnim); + + if (!flaming) + { + if (!CultistBreathClip.FlamesDue(cultistAnim, fireStarted)) return; + flaming = true; + if (fireEffectInstance == null) fireEffectInstance = FenrirAttackSync.SpawnFlames(Player.m_localPlayer); + } + + Player.m_localPlayer.UseEitr(FenrirConfig.CultistFireEitrDrainPerSecond.Value * Time.deltaTime); + fireTimer += Time.deltaTime; if (fireTimer >= 0.2f) // Damage tick every 0.2s { @@ -980,14 +1007,18 @@ namespace RacesOfValheim private void OnDestroy() { if (isShootingFire) FenrirAttackSync.PublishFire(false); + LocalBreathing = false; } private void StopFire() { isShootingFire = false; + LocalBreathing = false; + flaming = false; cultistAnim.ResetTrigger("attack_flames"); FenrirAttackSync.PublishFire(false); - + CultistBreathClip.Release(cultistAnim, Player.m_localPlayer); + if (fireEffectInstance != null) { Destroy(fireEffectInstance); @@ -1702,29 +1733,149 @@ namespace RacesOfValheim } /// - /// Keeps the Cultist's breath at its mouth: on the Jaw bone (fenring_cultist rig: - /// .../Neck/Head/Jaw), facing where the player faces. Placed every LateUpdate, after the form's - /// Animator has moved the head, rather than parented to the bone, so the flames point where the - /// player aims and not wherever the jaw happens to tilt. The same on every client's copy. + /// The Cultist's breath, timed by its own clip. fenring_cultist_animator's attack_flames state + /// plays "Flame Spell Attack" (read offline with UnityPy, bundle c4210710): 3.5 s, the hands + /// light at 1.094 s (TrailOn), the vanilla flames come at 1.245 s (Hit) and are taken down at + /// 3.193 s. The state leaves for Movement only when the clip ends, so a short click used to + /// leave the Cultist standing with its arms up for the rest of the 3.5 s, breathing nothing. + /// + /// Now the flames wait for the clip's Hit point, the arms are held in the casting pose for as + /// long as the button is, and letting go blends straight back to Movement. Shared by the + /// owner's Cultist and every onlooker's copy, so both play the same breath. + /// + internal static class CultistBreathClip + { + private static readonly int State = Animator.StringToHash("Base Layer.attack_flames"); + private const string MovementState = "Base Layer.Movement"; + + // The clip's Hit event, where the vanilla Cultist's flames come out. + private const float FlamesAt = 1.245f; + + // Inside the flaming stretch (1.245 to 3.193 s): the Animator stops here while held. + private const float PinAt = 2.6f; + + private const float Fade = 0.25f; + + /// Seconds into the flame clip, or -1 while the Animator is in another state. + private static float ClipTime(Animator anim) + { + AnimatorStateInfo info = anim.IsInTransition(0) ? anim.GetNextAnimatorStateInfo(0) : anim.GetCurrentAnimatorStateInfo(0); + return info.fullPathHash == State ? info.normalizedTime * info.length : -1f; + } + + /// True once the clip has reached its Hit point. If the clip never started (another + /// state took the Animator), the same delay counted from the press stands in for it. + public static bool FlamesDue(Animator anim, float started) + { + float t = ClipTime(anim); + return t >= FlamesAt || (t < 0f && Time.time - started >= FlamesAt + 0.2f); + } + + /// Holds the arms in the casting pose: the Animator stops once the clip reaches PinAt. + public static void Hold(Animator anim) + { + if (!anim.IsInTransition(0) && ClipTime(anim) >= PinAt) anim.speed = 0f; + } + + /// Lets the Animator run again and blends out of the clip, unless a stagger has + /// taken over (FormStagger has fired the form's own). + public static void Release(Animator anim, Player player) + { + if (anim == null) return; + anim.speed = 1f; + if (ClipTime(anim) >= 0f && (player == null || !player.IsStaggering())) + anim.CrossFadeInFixedTime(MovementState, Fade, 0); + } + } + + /// + /// Keeps the Cultist's breath in its hands, where the vanilla Cultist casts it from. fx_fenring_flames + /// has two emitters, "flames_world left" and "flames_world right", set 1.2 to 1.6 m ahead of + /// its own origin; spawned at the creature, the fire began that far in front of it. Each + /// emitter goes on its hand bone (fenring_cultist rig: .../LeftForeArm/LeftHand and the right + /// one), pointing where the player faces, so the two streams leave the palms and run forward. + /// The embers and the light sit between the hands. Placed every LateUpdate, after the form's + /// Animator has moved the arms. The same on every client's copy. + /// + /// A form without hand bones keeps the whole effect at its mouth, as before. /// internal class CultistFlameFollow : MonoBehaviour { + // Ahead of the palm, so the stream does not start inside the hand. + private const float AheadOfHand = 0.15f; + + // The light, further ahead along the stream. + private const float LightAhead = 1.5f; + public Player Player; - private Transform mouth; + private Transform mouth, leftHand, rightHand; + private Transform leftFlames, rightFlames, embers, light; + private bool partsFound; private float nextLook; private void LateUpdate() { if (Player == null) return; + if (!partsFound) + { + partsFound = true; + leftFlames = transform.Find("flames_world left"); + rightFlames = transform.Find("flames_world right"); + embers = transform.Find("FireEmbers"); + light = transform.Find("Point light"); + } + // The form can be rebuilt under the same flames; look again now and then if lost. - if (mouth == null && Time.time >= nextLook) + if ((leftHand == null || rightHand == null || mouth == null) && Time.time >= nextLook) { nextLook = Time.time + 0.5f; + leftHand = FindBone(Player, "LeftHand"); + rightHand = FindBone(Player, "RightHand"); mouth = FenrirAttackSync.FindMouth(Player); } - transform.SetPositionAndRotation(FenrirAttackSync.MouthPoint(Player, mouth), Player.transform.rotation); + Quaternion facing = Player.transform.rotation; + transform.SetPositionAndRotation(FenrirAttackSync.MouthPoint(Player, mouth), facing); + if (leftHand == null || rightHand == null || leftFlames == null || rightFlames == null) return; + + Vector3 forward = Player.transform.forward; + Vector3 left = leftHand.position + forward * AheadOfHand; + Vector3 right = rightHand.position + forward * AheadOfHand; + leftFlames.SetPositionAndRotation(left, facing); + rightFlames.SetPositionAndRotation(right, facing); + + Vector3 between = (left + right) * 0.5f; + if (embers != null) embers.SetPositionAndRotation(between, facing); + if (light != null) light.position = between + forward * LightAhead; + } + + private static Transform FindBone(Player player, string name) + { + foreach (Transform child in player.transform) + { + if (child.name == "Visual" || !child.gameObject.activeInHierarchy) continue; + foreach (Transform t in child.GetComponentsInChildren()) + { + if (t.name == name) return t; + } + } + return null; + } + } + + /// + /// Turns the Cultist toward the camera while it breathes fire, as vanilla turns a player who + /// blocks or draws a bow: Player.AlwaysRotateCamera answers true for those, and Character then + /// turns the body to the look direction at its own turn speed. The breath is not a vanilla + /// attack, so the body stayed facing wherever the movement keys had left it. + /// + [HarmonyPatch(typeof(Player), "AlwaysRotateCamera")] + public static class Cultist_BreathFacesCamera_Patch + { + public static void Postfix(Player __instance, ref bool __result) + { + if (!__result && __instance == Player.m_localPlayer && CultistFormBehavior.LocalBreathing) __result = true; } }