diff --git a/JotunnModStub/Fenrir/FenrirFormShared.cs b/JotunnModStub/Fenrir/FenrirFormShared.cs index 259fc08..656f761 100644 --- a/JotunnModStub/Fenrir/FenrirFormShared.cs +++ b/JotunnModStub/Fenrir/FenrirFormShared.cs @@ -413,6 +413,27 @@ namespace RacesOfValheim return true; } + private static readonly System.Collections.Generic.Dictionary weapons = + new System.Collections.Generic.Dictionary(); + + /// + /// A creature's own attack item (dragon_bite, SeekerBrute_ram...), read once and cached, + /// null if the game has none by that name. A hand-built swing skips Attack, and Attack is + /// where vanilla plays the item's m_triggerEffect and m_hitEffect, so a form that wants the + /// creature's real swing and impact sounds plays them from here. + /// + public static ItemDrop.ItemData.SharedData CreatureWeapon(string prefabName) + { + if (string.IsNullOrEmpty(prefabName)) return null; + if (weapons.TryGetValue(prefabName, out ItemDrop.ItemData.SharedData cached)) return cached; + + GameObject prefab = Jotunn.Managers.PrefabManager.Cache.GetPrefab(prefabName); + ItemDrop.ItemData.SharedData shared = prefab != null ? prefab.GetComponent()?.m_itemData?.m_shared : null; + if (shared == null) Jotunn.Logger.LogWarning($"[FormMelee] No attack item '{prefabName}'; that swing stays silent."); + weapons[prefabName] = shared; + return shared; + } + public static void LogHit(string form, IDestructible target, float damage) { if (!FenrirConfig.DebugFormHits.Value) return; diff --git a/JotunnModStub/Skadi/DrakeFormBehavior.cs b/JotunnModStub/Skadi/DrakeFormBehavior.cs index f1335bf..b76674f 100644 --- a/JotunnModStub/Skadi/DrakeFormBehavior.cs +++ b/JotunnModStub/Skadi/DrakeFormBehavior.cs @@ -311,6 +311,29 @@ namespace RacesOfValheim return true; } + /// + /// How many hit events the form's clips have sent: the dragon's own "Hit" event, which + /// CharacterAnimEvent hands to the player's OnAttackTrigger and SkadiBlockAttackTriggerPatch + /// counts here instead of letting it through. FenrirForms.AttackEvents, the same idea. + /// + public static int AttackEvents { get; private set; } + + public static void NotifyAttackTrigger() => AttackEvents++; + + // Longest a melee waits for its Hit event before landing anyway, from the trigger. The + // slowest blow is the Moder's claw at 2.0 s of 3.75 s; a clip that never sends one must + // not leave the swing hitting nothing. + private const float HitEventTimeout = 3f; + + /// + /// Waits for the clip's own blow. A fixed 0.5 s used to land every melee early, read off the + /// game's clips: Moder bite 0.60 s and claws 2.0 s, Fader bite 1.27 s, claws 1.24 s, spin 1.30 s. + /// + private IEnumerator WaitForHit(int baseline, float started) + { + while (AttackEvents == baseline && Time.time - started < HitEventTimeout) yield return null; + } + private IEnumerator PerformAttackCoroutine(int button) { lastAttackTime = Time.time; @@ -409,13 +432,18 @@ namespace RacesOfValheim breathingUntil = Time.time + projDelay + projCount * 0.05f + 0.5f; } + int hitBaseline = AttackEvents; + float triggeredAt = Time.time; SetAttackTrigger(attackTrigger); - yield return new WaitForSeconds(projDelay); - if (isMelee) { - ExecuteMeleeDamage(MODER_MELEE_DAMAGE, MELEE_RADIUS); + yield return WaitForHit(hitBaseline, triggeredAt); + ExecuteMeleeDamage(MODER_MELEE_DAMAGE, MELEE_RADIUS, MeleeWeapon(attackTrigger)); + } + else + { + yield return new WaitForSeconds(projDelay); } if (!isMelee && projName != null) @@ -515,13 +543,18 @@ namespace RacesOfValheim breathingUntil = Time.time + projDelay + projCount * 0.05f + 0.5f; } + int hitBaseline = AttackEvents; + float triggeredAt = Time.time; SetAttackTrigger(attackTrigger); - yield return new WaitForSeconds(projDelay); - if (isMelee) { - ExecuteMeleeDamage(meleeDamage, meleeRadius); + yield return WaitForHit(hitBaseline, triggeredAt); + ExecuteMeleeDamage(meleeDamage, meleeRadius, MeleeWeapon(attackTrigger)); + } + else + { + yield return new WaitForSeconds(projDelay); } if (!isMelee && projName != null) @@ -583,7 +616,28 @@ namespace RacesOfValheim // The dragon forms never go through Humanoid.StartAttack - SkadiAttackPatch blocks it, and // they carry no weapon - so the bite, claw and spin animations landed no damage at all and // simply burned eitr. Sweep for targets the way FenrirBehaviors does for its claws. - private void ExecuteMeleeDamage(float baseDamage, float radius) + private string FormLabel => formRules != null ? formRules.Form.ToString() : "Skadi form"; + + /// + /// The real creature's attack item behind each melee trigger, for its impact sound. The + /// Fader's spin has no item of its own in the game's manifest, so it borrows the claw's. + /// + private string MeleeWeapon(string trigger) + { + bool fader = formRules != null && formRules.Form == SkadiProgression.FormType.SkeletonDragon; + switch (trigger) + { + case "attack_bite": return fader ? "Fader_Bite" : "dragon_bite"; + case "attack_claw_left": return "dragon_claw_left"; + case "attack_claw_right": return "dragon_claw_right"; + case "attack_ClawL": return "Fader_Claw_Left"; + case "attack_ClawR": return "Fader_Claw_Right"; + case "attack_Spin": return "Fader_Claw_Left"; + default: return null; + } + } + + private void ExecuteMeleeDamage(float baseDamage, float radius, string weapon) { if (Player.m_localPlayer == null) return; @@ -598,6 +652,8 @@ namespace RacesOfValheim int layerMask = LayerMask.GetMask("Default", "static_solid", "Default_small", "piece", "piece_nonsolid", "terrain", "character", "character_net", "character_ghost", "hitbox", "character_noenv", "vehicle"); Collider[] hits = Physics.OverlapSphere(attackPoint, radius, layerMask); + FormMelee.LogSwing(FormLabel, hits.Length); + ItemDrop.ItemData.SharedData shared = FormMelee.CreatureWeapon(weapon); System.Collections.Generic.HashSet hitObjects = new System.Collections.Generic.HashSet(); bool hitCreature = false; @@ -613,18 +669,29 @@ namespace RacesOfValheim if (characterTarget != null && characterTarget == Player.m_localPlayer) continue; GameObject targetObj = (target as MonoBehaviour)?.gameObject; - if (targetObj != null) - { - if (hitObjects.Contains(targetObj)) continue; - hitObjects.Add(targetObj); - } + // Marked only once it is hit, further down: marking it here let the first collider + // found (a limb outside the arc) rule out a body squarely in the swing. + if (targetObj != null && hitObjects.Contains(targetObj)) continue; Vector3 targetCenter = hit.bounds.center; Vector3 dirToTarget = (targetCenter - playerPos).normalized; - if (!fullCircle && Vector3.Angle(forward, dirToTarget) >= 70f) continue; + float angle = Vector3.Angle(forward, dirToTarget); + if (!fullCircle && angle >= 70f) + { + FormMelee.LogMiss(FormLabel, hit, $"outside the swing arc ({angle:0} deg)"); + continue; + } + // FormMelee.CanReach, not a bare Linecast. The Linecast tested the piece layer and + // counted the target's own collider as cover, so anything living on that layer + // could never be hit: the T.W.I.G. training dummy is a Humanoid whose only collider + // is on "piece". Wildlife sits on "character" and was never affected. Vector3 eyePos = playerPos + Vector3.up * 1.5f; - if (Physics.Linecast(eyePos, targetCenter, out RaycastHit rayHit, LayerMask.GetMask("Default", "static_solid", "terrain", "piece"))) continue; + if (!FormMelee.CanReach(eyePos, attackPoint, hit, target, out string cover)) + { + FormMelee.LogMiss(FormLabel, hit, "no clear line, blocked by '" + cover + "'"); + continue; + } HitData hitData = new HitData(); // Frost-flavoured maul; SkadiDamagePatch tops this up with the per-tier frost bonus. @@ -640,8 +707,11 @@ namespace RacesOfValheim hitData.m_toolTier = 0; hitData.SetAttacker(Player.m_localPlayer); + if (targetObj != null) hitObjects.Add(targetObj); + FormMelee.LogHit(FormLabel, target, totalDamage); target.Damage(hitData); FormMelee.GrantAdrenaline(target); + shared?.m_hitEffect.Create(targetCenter, Quaternion.identity); if (characterTarget != null) hitCreature = true; } diff --git a/JotunnModStub/Skadi/SkadiPatches.cs b/JotunnModStub/Skadi/SkadiPatches.cs index 94b1c56..b7560c2 100644 --- a/JotunnModStub/Skadi/SkadiPatches.cs +++ b/JotunnModStub/Skadi/SkadiPatches.cs @@ -408,7 +408,11 @@ namespace RacesOfValheim public static bool Prefix(Humanoid __instance) { if (__instance != Player.m_localPlayer) return true; - return __instance.GetComponentInChildren() == null; + if (__instance.GetComponentInChildren() == null) return true; + + // The dragon's own clip calling its blow: the hit cue for the form's melee. + DrakeFormBehavior.NotifyAttackTrigger(); + return false; } } } diff --git a/JotunnModStub/Sverm/SvermConfigManagement.cs b/JotunnModStub/Sverm/SvermConfigManagement.cs index a25aec9..62bda1a 100644 --- a/JotunnModStub/Sverm/SvermConfigManagement.cs +++ b/JotunnModStub/Sverm/SvermConfigManagement.cs @@ -46,6 +46,7 @@ namespace RacesOfValheim public static ConfigEntry RavagerYawDegrees; public static ConfigEntry RavagerAttackOffsetValue; public static ConfigEntry RavagerSlamCraterValue; + public static ConfigEntry RavagerDamageShareValue; // ─── Colony caps ───────────────────────────────────────────────────── // Every ceiling on how many things the colony can have alive at once, in one section, so @@ -231,6 +232,11 @@ namespace RacesOfValheim "The middle-click slam digs into the ground where it lands, like a pickaxe blow. " + "Never inside a ward you have no access to."); + RavagerDamageShareValue = config.Bind(ravager, "Ravager Attack Damage", 1f, + new ConfigDescription("Share of a real Seeker Brute's damage each Ravager attack deals. " + + "Your Unarmed skill then scales it the way it scales a weapon: about 40% at level 0, " + + "about 92% at level 100.", new AcceptableValueRange(0.1f, 2f))); + EmberGlowStrength = config.Bind(ember, "Glow Strength", SvermConfig.EmberGlowStrength, "How hot the chitin burns at Fader. This multiplies an HDR colour and it is applied " + "to the chest, the legs and the helmet at once, so it goes a very long way. Settled " + @@ -364,6 +370,7 @@ namespace RacesOfValheim public static float RavagerYaw => RavagerYawDegrees?.Value ?? 0f; public static float RavagerAttackOffset => RavagerAttackOffsetValue?.Value ?? 1.5f; public static bool RavagerSlamCrater => RavagerSlamCraterValue?.Value ?? true; + public static float RavagerDamageShare => RavagerDamageShareValue?.Value ?? 1f; public static bool Spines => SpinesEnabled?.Value ?? false; public static float SizeSmall => SpineSizeSmall?.Value ?? SvermConfig.SpineSizeSmall; diff --git a/JotunnModStub/Sverm/SvermRavagerAttacks.cs b/JotunnModStub/Sverm/SvermRavagerAttacks.cs index 2bf3567..e084678 100644 --- a/JotunnModStub/Sverm/SvermRavagerAttacks.cs +++ b/JotunnModStub/Sverm/SvermRavagerAttacks.cs @@ -24,6 +24,29 @@ namespace RacesOfValheim { public string Trigger; public HitData.DamageTypes Damage; + + /// The Brute weapon itself, for its swing (m_triggerEffect) and impact + /// (m_hitEffect) sounds. Vanilla plays both inside Attack, which this form never runs, + /// so without them every hit landed silent. Null if the weapon was missing. + public ItemDrop.ItemData.SharedData Shared; + + /// The weapon's own push (m_attackForce), tool tier and swing arc, the way + /// TrolTrueFormModel.Profile carries the troll's. + public float PushForce; + public int ToolTier; + public float Angle; + } + + /// + /// One blow's damage: the Brute's, times Ravager Attack Damage, times the Unarmed skill + /// factor the way a weapon's skill scales it (0.25-0.55 at level 0, 0.85-1 at 100). + /// TrolTrueFormModel.DamageFor does the same with Clubs for the troll. + /// + public static HitData.DamageTypes DamageFor(Player player, Profile profile) + { + HitData.DamageTypes damage = profile.Damage.Clone(); + damage.Modify(SvermConfigManagement.RavagerDamageShare * player.GetRandomSkillFactor(Skills.SkillType.Unarmed)); + return damage; } private static Profile bite; @@ -72,6 +95,10 @@ namespace RacesOfValheim { Trigger = shared.m_attack != null ? (shared.m_attack.m_attackAnimation ?? "") : "", Damage = shared.m_damages, + Shared = shared, + PushForce = shared.m_attackForce, + ToolTier = shared.m_toolTier, + Angle = shared.m_attack != null ? shared.m_attack.m_attackAngle : 90f, }; Jotunn.Logger.LogInfo($"[Sverm] Ravager Strain reads '{profile.Trigger}' off '{sourcePrefabName}' - " + diff --git a/JotunnModStub/Sverm/SvermRavagerForm.cs b/JotunnModStub/Sverm/SvermRavagerForm.cs index 52262b1..44e6599 100644 --- a/JotunnModStub/Sverm/SvermRavagerForm.cs +++ b/JotunnModStub/Sverm/SvermRavagerForm.cs @@ -794,7 +794,7 @@ namespace RacesOfValheim { if (FormStagger.AttackInterrupted) return; if (slam) Slam(profile, radius, leftY, rightY, dropSamples); - else ExecuteDamage(profile, radius); + else ExecuteDamage(profile, radius, false); }, () => { if (leftClaw == null || rightClaw == null) return; @@ -828,7 +828,11 @@ namespace RacesOfValheim { Player player = Player.m_localPlayer; if (player != null) - ApplyDamageInSphere(player.transform.position + Vector3.up, SvermConfig.RavagerTauntRadius, profile.Damage); + { + // No Strike: the roar above is the taunt's sound, the weapon's would double it. + if (ApplyDamageInSphere(player.transform.position + Vector3.up, SvermConfig.RavagerTauntRadius, profile, true)) + FormMelee.RaiseSkill(Skills.SkillType.Unarmed); + } }, null); isAttacking = false; @@ -844,6 +848,11 @@ namespace RacesOfValheim { if (hasForwardSpeed) anim.SetFloat("forward_speed", 0f); + // The Brute weapon's swing sound, which vanilla plays in Attack.Start (the bite's + // snap, the ram's charge, the slam's wind-up). The taunt's weapon has none. + Player player = Player.m_localPlayer; + if (player != null) profile.Shared?.m_startEffect.Create(player.transform.position, player.transform.rotation, player.transform); + if (string.IsNullOrEmpty(profile.Trigger) || !knownTriggers.Contains(profile.Trigger)) return false; anim.SetTrigger(profile.Trigger); @@ -944,14 +953,14 @@ namespace RacesOfValheim Transform claw = StrikingClaw(leftY, rightY, samples); if (claw == null) { - ExecuteDamage(profile, radius); + ExecuteDamage(profile, radius, true); 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); + Strike(claw.position + Vector3.up * 0.5f, radius, profile, true); DigCrater(claw.position); } @@ -985,14 +994,14 @@ namespace RacesOfValheim /// FenrirBehaviors.ExecuteClawDamage, unchanged in shape: a sphere swept against the same /// broad layer mask every melee form in this mod already uses. /// - private void ExecuteDamage(SvermRavagerAttacks.Profile profile, float radius) + private void ExecuteDamage(SvermRavagerAttacks.Profile profile, float radius, bool slam) { Player player = Player.m_localPlayer; if (player == null) return; float reach = SvermConfigManagement.RavagerAttackOffset; Vector3 attackPoint = player.transform.position + player.transform.forward * reach + Vector3.up; - ApplyDamageInSphere(attackPoint, radius, profile.Damage); + Strike(attackPoint, radius, profile, slam); } private const string CraterPrefab = "digg_v3"; @@ -1015,26 +1024,40 @@ namespace RacesOfValheim if (crater != null) Object.Instantiate(crater, ground.point, Quaternion.identity); } + private const string FormLabel = "Ravager"; + + private static readonly int HitMask = LayerMask.GetMask("Default", "static_solid", "Default_small", "piece", + "piece_nonsolid", "terrain", "character", "character_net", "character_ghost", + "hitbox", "character_noenv", "vehicle"); + /// - /// The actual sweep, shared by every attack this form has (the three forward swings and - /// the taunt's own AoE) - only the sphere's centre and radius differ between them. + /// The actual sweep, shared by every attack this form has. TrolTrueFormHit.SphereHit in + /// shape: a forward swing stays inside its arc (the Brute's own, widened to 75 degrees each + /// side the way the troll's is), the slam's claws and the taunt reach all around, and + /// FormMelee.CanReach keeps a blow from landing through a wall. Each target that takes the + /// blow plays the Brute weapon's own impact effect, which vanilla plays inside Attack. /// /// m_skill is Swords, deliberately NOT Unarmed: Sverm_Damage_Patch rewrites any Unarmed /// hit from a Sverm into the Chitin Claws' own tier damage, and that patch fires for THIS /// call too, since it hooks Character.Damage itself rather than caring who dealt the hit. - /// Swords keeps that rewrite from overwriting the Brute's real numbers with the claws'. + /// Swords keeps that rewrite from overwriting the Brute's numbers with the claws'. It is + /// the Brute weapons' own skill too. The damage still scales with Unarmed (DamageFor). /// - private static void ApplyDamageInSphere(Vector3 center, float radius, HitData.DamageTypes damage) + /// True if the blow landed on a creature, so the caller raises the skill. + private static bool ApplyDamageInSphere(Vector3 center, float radius, SvermRavagerAttacks.Profile profile, bool allAround) { Player player = Player.m_localPlayer; - if (player == null) return; + if (player == null) return false; - int layerMask = LayerMask.GetMask("Default", "static_solid", "Default_small", "piece", - "piece_nonsolid", "terrain", "character", "character_net", "character_ghost", - "hitbox", "character_noenv", "vehicle"); + Vector3 pos = player.transform.position; + Vector3 forward = player.transform.forward; + float halfAngle = allAround ? 180f : Mathf.Max(75f, profile.Angle * 0.5f); + HitData.DamageTypes damage = SvermRavagerAttacks.DamageFor(player, profile); - Collider[] hits = Physics.OverlapSphere(center, radius, layerMask); + Collider[] hits = Physics.OverlapSphere(center, radius, HitMask); + FormMelee.LogSwing(FormLabel, hits.Length); var hitObjects = new System.Collections.Generic.HashSet(); + bool hitCreature = false; foreach (Collider hit in hits) { @@ -1042,35 +1065,65 @@ namespace RacesOfValheim if (hit.attachedRigidbody != null && hit.attachedRigidbody.gameObject == player.gameObject) continue; IDestructible target = hit.GetComponentInParent(); - if (target == null) continue; - - if (target is Character characterTarget && characterTarget == player) continue; + if (target == null || (target is Character self && self == player)) continue; + // Marked only once it is hit, further down, so a limb outside the arc does not rule + // out a body squarely in the swing. GameObject targetObj = (target as MonoBehaviour)?.gameObject; - if (targetObj != null) - { - if (hitObjects.Contains(targetObj)) continue; - hitObjects.Add(targetObj); - } + if (targetObj != null && hitObjects.Contains(targetObj)) continue; Vector3 targetCenter = hit.bounds.center; - Vector3 dirToTarget = (targetCenter - player.transform.position).normalized; + Vector3 flat = targetCenter - pos; + flat.y = 0f; + float angle = flat.sqrMagnitude > 0.0001f ? Vector3.Angle(forward, flat) : 0f; + if (angle > halfAngle) + { + FormMelee.LogMiss(FormLabel, hit, $"outside the swing arc ({angle:0} deg)"); + continue; + } + + if (!FormMelee.CanReach(player.GetEyePoint(), center, hit, target, out string cover)) + { + FormMelee.LogMiss(FormLabel, hit, "no clear line, blocked by '" + cover + "'"); + continue; + } HitData hitData = new HitData(); hitData.m_damage = damage; hitData.m_point = targetCenter; - hitData.m_dir = dirToTarget; - hitData.m_pushForce = 40f; + hitData.m_dir = (targetCenter - pos).normalized; + hitData.m_pushForce = profile.PushForce; hitData.m_skill = Skills.SkillType.Swords; hitData.m_dodgeable = true; hitData.m_blockable = true; hitData.m_hitType = HitData.HitType.PlayerHit; - hitData.m_toolTier = 100; + hitData.m_toolTier = (short)profile.ToolTier; hitData.SetAttacker(player); + if (targetObj != null) hitObjects.Add(targetObj); + FormMelee.LogHit(FormLabel, target, damage.GetTotalDamage()); target.Damage(hitData); FormMelee.GrantAdrenaline(target); + profile.Shared?.m_hitEffect.Create(targetCenter, Quaternion.identity); + if (target is Character) hitCreature = true; } + + return hitCreature; + } + + /// + /// One blow of a Ravager attack: the Brute weapon's trigger effect (the slam's landing + /// thud; the bite and the ram have none), the sweep, and the Unarmed experience vanilla + /// pays inside Attack for a swing that reached a creature. The Ravager fights unarmed like + /// the rest of the Sverm, so that is the skill it trains. + /// + private static void Strike(Vector3 center, float radius, SvermRavagerAttacks.Profile profile, bool allAround) + { + Player player = Player.m_localPlayer; + if (player == null) return; + + profile.Shared?.m_triggerEffect.Create(center, player.transform.rotation); + if (ApplyDamageInSphere(center, radius, profile, allAround)) FormMelee.RaiseSkill(Skills.SkillType.Unarmed); } } }