RacesOfValheim/JotunnModStub/WarVulture/WarVulturePatches.cs
2026-08-09 21:54:56 +02:00

193 lines
8 KiB
C#

using System.Reflection;
using HarmonyLib;
using UnityEngine;
namespace RacesOfValheim
{
/// <summary>
/// The one patch that makes a seated player able to fly.
///
/// Player.SetControls opens with: if the player is attached and presses any movement key, and
/// there is no doodad controller, call AttachStop. That is vanilla's "stand up from the chair",
/// read straight out of the assembly - and against a rider who drives themselves it fires on
/// the first tap of W.
///
/// Giving the game a doodad controller instead is not the way out: SetDoodadControlls zeroes
/// moveDir, run and autoRun unconditionally afterwards, so the rider would stay attached and
/// be unable to move. Both doors are shut, which is why this one is a patch.
///
/// So m_attached is lifted for the duration of SetControls and put straight back. Nothing else
/// in that method reads it (checked instruction by instruction), and everything else in the
/// game still sees an attached player - including ZSyncAnimation, which is what keeps the
/// sitting pose on everyone else's screen.
///
/// AND THE SECOND HALF, added after the first flight. Lifting m_attached hands the rider back
/// every action a standing player has, and three of them have no business happening in a
/// saddle: jump, dodge and crouch each played their own animation for half a second before the
/// sitting pose snapped back, which is what a rider trying to gain height actually saw.
///
/// They are suppressed here, at the source, and it costs nothing that was wanted: vanilla's
/// UpdateDebugFly does NOT read these arguments. It reads ZInput directly - GetButton("Jump")
/// to climb and KeyCode.LeftControl to descend - so taking them out of SetControls removes the
/// jump and keeps the climb. Same for the click: while riding, the dismount is read straight
/// off ZInput in WarVultureMount, so the attack is suppressed here rather than left to fight
/// the saddle pose for an animation it was never going to win.
///
/// It costs one boolean read per frame when nobody is riding.
/// </summary>
[HarmonyPatch(typeof(Player), "SetControls")]
public static class WarVultureControlsPatch
{
public static void Prefix(Player __instance, ref bool ___m_attached,
ref bool attack, ref bool attackHold,
ref bool secondaryAttack, ref bool secondaryAttackHold,
ref bool jump, ref bool crouch, ref bool dodge,
out bool __state)
{
__state = false;
if (!WarVultureMount.IsRiding(__instance)) return;
attack = false;
attackHold = false;
secondaryAttack = false;
secondaryAttackHold = false;
jump = false;
crouch = false;
dodge = false;
if (!___m_attached) return;
___m_attached = false;
__state = true;
}
public static void Postfix(ref bool ___m_attached, bool __state)
{
if (__state) ___m_attached = true;
}
}
/// <summary>
/// The speed of the ride, and the bird's wind.
///
/// Character.UpdateDebugFly has just written this physics step's velocity when this runs, so
/// this is the last word on it. The mount scales what vanilla computed rather than replacing
/// it - see WarVultureMount.ApplyFlight for why that keeps the flight feeling smooth.
///
/// m_currentVel, m_body and m_run are all private on Character and arrive through Harmony's
/// field injection, which is verified when the patch is applied: a Valheim rename becomes a
/// startup error instead of a mount that silently stops working.
/// </summary>
[HarmonyPatch(typeof(Character), "UpdateDebugFly")]
public static class WarVultureFlightPatch
{
private static readonly FieldInfo DebugFlySpeedField = AccessTools.Field(typeof(Character), "m_debugFlySpeed");
private static bool warnedAboutSpeedField;
/// <summary>
/// Vanilla's own debug fly speed, read live rather than cached: the game exposes it as a
/// static that can be retuned, and a stale copy here would quietly scale the whole ride.
/// </summary>
public static float DebugFlySpeed
{
get
{
if (DebugFlySpeedField != null) return System.Convert.ToSingle(DebugFlySpeedField.GetValue(null));
if (!warnedAboutSpeedField)
{
warnedAboutSpeedField = true;
Jotunn.Logger.LogWarning("[WarVulture] Character.m_debugFlySpeed could not be found, so the " +
"mount's speeds are being scaled against an assumed 10 m/s. If the " +
"bird flies wrong, this line is why.");
}
return 10f;
}
}
public static void Postfix(Character __instance, ref Vector3 ___m_currentVel, Rigidbody ___m_body, bool ___m_run)
{
WarVultureMount mount = WarVultureMount.Active;
if (mount == null) return;
Player player = __instance as Player;
if (player == null || !WarVultureMount.IsRiding(player)) return;
mount.ApplyFlight(___m_run, ref ___m_currentVel, ___m_body);
}
}
/// <summary>
/// Gives every player a watcher, so a rider on someone else's screen gets a bird under them.
///
/// A second Player.Awake postfix alongside the one in ZdoVisualSync rather than an edit to it:
/// that file belongs to the shared race visuals, this belongs to the vulture, and Harmony is
/// perfectly happy to hold both.
/// </summary>
[HarmonyPatch(typeof(Player), "Awake")]
public static class WarVulturePlayerAwakePatch
{
public static void Postfix(Player __instance)
{
if (__instance.GetComponent<WarVultureRemoteVisual>() == null)
{
__instance.gameObject.AddComponent<WarVultureRemoteVisual>();
}
}
}
/// <summary>
/// The bird as everyone ELSE sees it.
///
/// The rider's own client draws its bird from WarVultureMount; this draws one for every other
/// player who has the flag set on their ZDO. It never touches flight, attachment or input -
/// remote players are moved by their own owner and the pose replicates itself through
/// ZSyncAnimation, so there is nothing here but a model that appears and disappears.
/// </summary>
public class WarVultureRemoteVisual : MonoBehaviour
{
private const string HolderName = "WarVultureRemoteModel";
private Player player;
private WarVultureRig rig;
private void Awake()
{
player = GetComponent<Player>();
}
private void Update()
{
if (player == null) return;
// The local rider is drawn by their own mount; two birds is one too many.
if (player == Player.m_localPlayer) { Hide(); return; }
ZNetView nview = player.GetComponent<ZNetView>();
if (nview == null || !nview.IsValid()) { Hide(); return; }
bool mounted = nview.GetZDO().GetBool(WarVultureMount.MountedKey, false) && !player.IsDead();
if (mounted && rig == null) rig = WarVultureVisual.Spawn(player.transform, HolderName);
else if (!mounted && rig != null) Hide();
if (rig != null)
{
Vector3 velocity = player.GetVelocity();
rig.Drive(new Vector3(velocity.x, 0f, velocity.z).magnitude);
}
}
private void Hide()
{
if (rig == null) return;
Destroy(rig.gameObject);
rig = null;
}
private void OnDestroy()
{
Hide();
}
}
}