Merge branch 'fix-cultist-attacks': Cultist claws on the hit events, flames from the jaw

This commit is contained in:
kanym 2026-10-29 13:22:45 +01:00
commit 162f71c39d
4 changed files with 137 additions and 18 deletions

View file

@ -323,7 +323,8 @@ namespace RacesOfValheim
GameObject fireVfx = ZNetScene.instance.GetPrefab("fx_fenring_flames");
if (fireVfx == null) return null;
GameObject instance = Object.Instantiate(fireVfx, player.transform.position + Vector3.up * 1.5f + player.transform.forward, player.transform.rotation, player.transform);
GameObject instance = Object.Instantiate(fireVfx, MouthPoint(player), player.transform.rotation, player.transform);
instance.AddComponent<CultistFlameFollow>().Player = player;
// Aumentiamo la durata delle particelle del 20%
foreach (ParticleSystem ps in instance.GetComponentsInChildren<ParticleSystem>())
@ -334,6 +335,41 @@ namespace RacesOfValheim
return instance;
}
// Bone names the breath leaves from, mouth first. Matched by name anywhere under the form.
private static readonly string[] MouthBones = { "Jaw", "Head" };
/// <summary>
/// Where the Cultist's breath starts: just ahead of the form's jaw (or head), so it leaves the
/// mouth at whatever size the form stands. It used to be a fixed 1.5 m above the player's
/// feet, chest height on a Cultist, which is the "fire starts too low" report. Falls back on
/// that fixed point if the form has neither bone.
/// </summary>
public static Vector3 MouthPoint(Player player) => MouthPoint(player, FindMouth(player));
public static Vector3 MouthPoint(Player player, Transform bone)
{
Transform body = player.transform;
return bone != null
? bone.position + body.forward * 0.3f
: body.position + Vector3.up * 1.5f + body.forward;
}
public static Transform FindMouth(Player player)
{
foreach (string wanted in MouthBones)
{
foreach (Transform child in player.transform)
{
if (child.name == "Visual" || !child.gameObject.activeInHierarchy) continue;
foreach (Transform t in child.GetComponentsInChildren<Transform>())
{
if (string.Equals(t.name, wanted, System.StringComparison.OrdinalIgnoreCase)) return t;
}
}
}
return null;
}
public static void PublishFire(bool on)
{
Player player = Player.m_localPlayer;
@ -1034,25 +1070,30 @@ namespace RacesOfValheim
cultistAnim.SetFloat("sideway_speed", 0f);
// Per il Cultista usiamo esplicitamente il trigger dell'artigliata
int baseline = FenrirForms.AttackEvents;
float started = Time.time;
cultistAnim.SetTrigger("attack_claw");
FenrirAttackSync.Publish("attack_claw", "sfx_fenring_claw_start");
yield return new WaitForSeconds(0.4f);
// The blow lands on the clip's own Hit event (ClawAttackCultist: 0.64 s of 1.33 s), and
// the lock holds until the clip is over. A fixed 0.4 s hit and a 0.5 s lock used to land
// the damage before the claw swung and let the next click start a new one mid-swing.
yield return null;
float length = ClipLength(1.33f);
yield return WaitForHit(baseline, started);
PlaySFX("sfx_fenring_claw_start");
ExecuteClawDamage(FenrirConfig.CultistClawDamage.Value);
float remainingTimeToIdle = 0.5f - 0.4f;
if (remainingTimeToIdle > 0)
{
yield return new WaitForSeconds(remainingTimeToIdle);
}
yield return WaitUntilClipEnds(length, started);
cultistAnim.ResetTrigger("attack_claw");
isAttacking = false;
}
// Each swipe of the double claw deals this share of Double Claw Damage. Two swipes give
// 120% of what the old single hit dealt: a little more, for a slower swing that can be cut short.
private const float DoubleClawHitShare = 0.6f;
private IEnumerator DoubleClawRoutine()
{
isAttacking = true;
@ -1061,24 +1102,63 @@ namespace RacesOfValheim
cultistAnim.SetFloat("forward_speed", 0f);
cultistAnim.SetFloat("sideway_speed", 0f);
int baseline = FenrirForms.AttackEvents;
float started = Time.time;
cultistAnim.SetTrigger("attack_claw_double");
FenrirAttackSync.Publish("attack_claw_double", "sfx_fenring_claw_start");
// L'animazione double claw è più lunga, facciamo il danno un po' dopo
yield return new WaitForSeconds(0.6f);
// DoubleClawAttackCultist carries two Hit events (0.64 s and 1.21 s of 1.83 s), one per
// swipe. It used to deal one hit at a fixed 0.6 s, so the second swipe did nothing.
yield return null;
float length = ClipLength(1.83f);
float damage = FenrirConfig.CultistDoubleClawDamage.Value * DoubleClawHitShare;
yield return WaitForHit(baseline, started);
PlaySFX("sfx_fenring_claw_start");
ExecuteClawDamage(damage);
ExecuteClawDamage(FenrirConfig.CultistDoubleClawDamage.Value);
// Attendiamo che l'animazione finisca (locking del giocatore)
// L'animazione doppia del cultista dura circa 1.5 - 2 secondi
yield return new WaitForSeconds(1.2f);
yield return WaitForHit(baseline + 1, started, HitEventTimeout + 0.6f);
PlaySFX("sfx_fenring_claw_start");
ExecuteClawDamage(damage);
yield return WaitUntilClipEnds(length, started);
cultistAnim.ResetTrigger("attack_claw_double");
isAttacking = false;
}
// Longest a swipe waits for its Hit event before landing anyway, measured from the trigger:
// a clip that never sends one must not leave the Cultist swinging at nothing.
private const float HitEventTimeout = 1.5f;
/// <summary>Waits until the clip has sent more than <paramref name="seen"/> hit events since
/// the trigger, or until the timeout.</summary>
private IEnumerator WaitForHit(int seen, float started, float timeout = HitEventTimeout)
{
while (FenrirForms.AttackEvents <= seen && Time.time - started < timeout)
{
yield return null;
}
}
/// <summary>The attack clip's length, read a frame after its trigger, when the transition
/// into it has begun. <paramref name="fallback"/> is the clip's length in the game files.</summary>
private float ClipLength(float fallback)
{
AnimatorClipInfo[] clips = cultistAnim.IsInTransition(0)
? cultistAnim.GetNextAnimatorClipInfo(0)
: cultistAnim.GetCurrentAnimatorClipInfo(0);
if (clips != null && clips.Length > 0 && clips[0].clip != null) return clips[0].clip.length;
return fallback;
}
/// <summary>Holds the lock until the clip has played out, clamped so a wrong reading can
/// neither cut a swing short nor freeze the form.</summary>
private IEnumerator WaitUntilClipEnds(float length, float started)
{
float remaining = Mathf.Clamp(length, 0.6f, 3f) - (Time.time - started);
if (remaining > 0f) yield return new WaitForSeconds(remaining);
}
private void ExecuteClawDamage(float baseDamage)
{
if (Player.m_localPlayer == null) return;
@ -1621,4 +1701,31 @@ namespace RacesOfValheim
}
}
/// <summary>
/// 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.
/// </summary>
internal class CultistFlameFollow : MonoBehaviour
{
public Player Player;
private Transform mouth;
private float nextLook;
private void LateUpdate()
{
if (Player == null) return;
// The form can be rebuilt under the same flames; look again now and then if lost.
if (mouth == null && Time.time >= nextLook)
{
nextLook = Time.time + 0.5f;
mouth = FenrirAttackSync.FindMouth(Player);
}
transform.SetPositionAndRotation(FenrirAttackSync.MouthPoint(Player, mouth), Player.transform.rotation);
}
}
}

View file

@ -154,7 +154,7 @@ namespace RacesOfValheim
CultistClawDamage = config.Bind(cultist, "Claw Damage", 85f,
new ConfigDescription("Base damage of the single claw (Left Mouse), before the Unarmed skill bonus.", new AcceptableValueRange<float>(0f, 500f)));
CultistDoubleClawDamage = config.Bind(cultist, "Double Claw Damage", 140f,
new ConfigDescription("Base damage of the double claw (Middle Mouse), before the Unarmed skill bonus.", new AcceptableValueRange<float>(0f, 500f)));
new ConfigDescription("Base damage of the double claw (Middle Mouse), before the Unarmed skill bonus. Each of its two swipes deals 60% of it.", new AcceptableValueRange<float>(0f, 500f)));
CultistAttackRadius = config.Bind(cultist, "Claw Attack Radius", 2.5f,
new ConfigDescription("Hit-sweep radius for both claw attacks.", new AcceptableValueRange<float>(1f, 10f)));
CultistPushForce = config.Bind(cultist, "Claw Push Force", 30f,

View file

@ -18,6 +18,16 @@ namespace RacesOfValheim
/// form's own routines, and on every revert.</summary>
public static bool LocalAttacking { get; set; }
/// <summary>
/// How many hit events the local form's clips have fired: the creature's own "Hit" event,
/// which CharacterAnimEvent hands to the player's OnAttackTrigger, and BlockPlayerAttackTriggerPatch
/// counts here instead of letting it through. A swing waits for this to move, so its damage
/// lands on the clip's own blow (TrolTrueFormModel.AttackEvents, the same idea).
/// </summary>
public static int AttackEvents { get; private set; }
public static void NotifyAttackTrigger() => AttackEvents++;
public static bool LocalInForm(Character character)
{
if (character == null || character != Player.m_localPlayer) return false;

View file

@ -56,7 +56,9 @@ namespace RacesOfValheim
{
public static bool Prefix(Humanoid __instance)
{
return !FenrirForms.LocalInForm(__instance);
if (!FenrirForms.LocalInForm(__instance)) return true;
FenrirForms.NotifyAttackTrigger();
return false;
}
}