From 691ccc211b666b76dfa2d7af8665d56defc7fe14 Mon Sep 17 00:00:00 2001 From: kanym Date: Tue, 20 Oct 2026 03:17:08 +0200 Subject: [PATCH] fix(visuals): a resized player's weapon hits as far as it is drawn Attack.DoMeleeAttack and DoAreaAttack measure range and ray width from the unscaled root, so a Legacy True Form Trol at 2.5 still hit at a human's 2 m while the drawn mace reached five. Both now grow by the Visual's world scale (never below 1), the factor HeldItemScaleFix sizes the weapon by. Height stays at the feet so a flat swing does not pass over small enemies. F8 toggles an in-game readout of the stretched numbers and what a cast straight ahead passes through. --- JotunnModStub/RaceMeleeReach.cs | 129 ++++++++++++++++++++++++++++++++ 1 file changed, 129 insertions(+) create mode 100644 JotunnModStub/RaceMeleeReach.cs diff --git a/JotunnModStub/RaceMeleeReach.cs b/JotunnModStub/RaceMeleeReach.cs new file mode 100644 index 0000000..4b2d66b --- /dev/null +++ b/JotunnModStub/RaceMeleeReach.cs @@ -0,0 +1,129 @@ +using System.Collections.Generic; +using HarmonyLib; +using UnityEngine; + +namespace RacesOfValheim +{ + /// + /// Makes a resized player's weapon hit as far as the weapon is drawn. + /// + /// Vanilla's melee check (Attack.DoMeleeAttack) fans sphere casts out from the player's ROOT, + /// m_attackHeight above the feet and m_attackRange long, and none of those numbers know the + /// model was resized. Races resize the "Visual" child, never the root (see RaceEyeHeight), so a + /// Legacy True Form Trol at 2.5 swung a mace whose head landed five metres out while the hit + /// still stopped at a human's 2 m. Testers saw it as "hits the first few, then 50/50": the + /// targets close in were inside the human reach, the rest only stood under the drawn weapon. + /// The area attack (sledges, DoAreaAttack) has the same blind spot: its sphere sits a human's + /// range ahead with a human's radius. + /// + /// Range and ray width grow by the model's size, the world scale of the Visual - the same factor + /// HeldItemScaleFix sizes the weapon by, so the hit follows the weapon through tiers, True Form + /// and the grow animation. Never below 1: a Furling or a Dvergar keeps the vanilla reach, for + /// the same reason RaceEyeHeight never shrinks the interact range. + /// + /// The height is left alone on purpose. A horizontal swing is one flat fan at that height, and + /// raised to 2.5 m it passed clean over a Greydwarf's head. Kept at the feet, the wider sphere + /// still covers from the ground to above a human's head, and a troll stands up into it anyway. + /// + /// The troll-model True Form does not swing weapons at all (TrolTrueFormBehavior does its own + /// hits), and a player inside a borrowed form keeps the Visual at 1, so both read as 1 here. + /// Fields are put back in a finalizer: the Attack is a clone per swing, but a throw halfway + /// through must not leave a stretched one behind for the next hit of a chain. + /// + [HarmonyPatch] + internal static class RaceMeleeReach + { + private struct Saved + { + public bool Active; + public float Range; + public float RayWidth; + public float RayWidthCharExtra; + } + + private static IEnumerable TargetMethods() + { + yield return AccessTools.Method(typeof(Attack), "DoMeleeAttack"); + yield return AccessTools.Method(typeof(Attack), "DoAreaAttack"); + } + + private static void Prefix(Attack __instance, Humanoid ___m_character, out Saved __state) + { + __state = default; + if (!(___m_character is Player player)) return; + + float scale = ModelScale(player); + if (scale <= 1.001f) return; + + __state = new Saved + { + Active = true, + Range = __instance.m_attackRange, + RayWidth = __instance.m_attackRayWidth, + RayWidthCharExtra = __instance.m_attackRayWidthCharExtra + }; + __instance.m_attackRange *= scale; + __instance.m_attackRayWidth *= scale; + __instance.m_attackRayWidthCharExtra *= scale; + + if (DebugOn && player == Player.m_localPlayer) Report(__instance, player, scale, __state); + } + + private static void Finalizer(Attack __instance, Saved __state) + { + if (!__state.Active) return; + __instance.m_attackRange = __state.Range; + __instance.m_attackRayWidth = __state.RayWidth; + __instance.m_attackRayWidthCharExtra = __state.RayWidthCharExtra; + } + + private static float ModelScale(Player player) + { + GameObject visual = Traverse.Create(player).Field("m_visual").GetValue(); + if (visual == null) return 1f; + return Mathf.Max(1f, visual.transform.lossyScale.y); + } + + // ---- F8 debug: what the stretched swing reaches, read in game ---- + + internal static bool DebugOn; + + /// One sphere cast straight ahead with the stretched numbers, listing every + /// character it passes through and how far out, next to the vanilla range it replaced. + private static void Report(Attack attack, Player player, float scale, Saved before) + { + Vector3 origin = player.transform.position + Vector3.up * attack.m_attackHeight; + Vector3 dir = player.transform.forward; + float width = Mathf.Max(0.05f, attack.m_attackRayWidth); + RaycastHit[] hits = Physics.SphereCastAll(origin, width, dir, Mathf.Max(0f, attack.m_attackRange - width), + LayerMask.GetMask("character", "character_net", "character_ghost", "hitbox", "character_noenv"), + QueryTriggerInteraction.Ignore); + + var names = new List(); + foreach (RaycastHit hit in hits) + { + GameObject go = Projectile.FindHitObject(hit.collider); + if (go == null || go == player.gameObject) continue; + names.Add($"{go.name.Replace("(Clone)", "")} {hit.distance:0.0}m"); + } + + string line = $"[RoV reach] x{scale:0.00} {attack.m_attackType} range {before.Range:0.0}->{attack.m_attackRange:0.0} " + + $"width {before.RayWidth:0.00}->{attack.m_attackRayWidth:0.00} height {attack.m_attackHeight:0.0} | " + + (names.Count > 0 ? string.Join(", ", names) : "no character ahead"); + Debug.Log(line); + player.Message(MessageHud.MessageType.TopLeft, line); + } + } + + /// F8 turns the reach readout on and off. + [HarmonyPatch(typeof(Player), "Update")] + internal static class RaceMeleeReachDebugKey + { + private static void Postfix(Player __instance) + { + if (__instance != Player.m_localPlayer || !Input.GetKeyDown(KeyCode.F8)) return; + RaceMeleeReach.DebugOn = !RaceMeleeReach.DebugOn; + __instance.Message(MessageHud.MessageType.TopLeft, $"[RoV reach] debug {(RaceMeleeReach.DebugOn ? "on" : "off")}"); + } + } +}