132 lines
6.3 KiB
C#
132 lines
6.3 KiB
C#
using System.Collections.Generic;
|
|
using HarmonyLib;
|
|
using UnityEngine;
|
|
|
|
namespace RacesOfValheim
|
|
{
|
|
/// <summary>
|
|
/// 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.
|
|
/// </summary>
|
|
[HarmonyPatch]
|
|
internal static class RaceMeleeReach
|
|
{
|
|
private struct Saved
|
|
{
|
|
public bool Active;
|
|
public float Range;
|
|
public float RayWidth;
|
|
public float RayWidthCharExtra;
|
|
}
|
|
|
|
private static IEnumerable<System.Reflection.MethodBase> 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<GameObject>();
|
|
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;
|
|
|
|
/// <summary>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.</summary>
|
|
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<string>();
|
|
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);
|
|
}
|
|
}
|
|
|
|
/// <summary>The debug key (DebugConfig, F8 by default) turns the reach readout on and off.</summary>
|
|
[HarmonyPatch(typeof(Player), "Update")]
|
|
internal static class RaceMeleeReachDebugKey
|
|
{
|
|
private static void Postfix(Player __instance)
|
|
{
|
|
if (__instance != Player.m_localPlayer || !DebugConfig.EnableDebugKey.Value) return;
|
|
if (!DebugConfig.DebugKey.Value.IsDownIgnoringHeldKeys()) return;
|
|
RaceMeleeReach.DebugOn = !RaceMeleeReach.DebugOn;
|
|
__instance.Message(MessageHud.MessageType.TopLeft, $"[RoV reach] debug {(RaceMeleeReach.DebugOn ? "on" : "off")}");
|
|
AlfarMiner.DebugReport(__instance);
|
|
AlfarDroneAI.DebugReport(__instance);
|
|
}
|
|
}
|
|
}
|