feat(aswid): bat flies on the Skadi's directed flight

The bat flew on vanilla's debug-fly flag: 50 m/s, free, blind to carry
weight, and invisible to monsters until six patches lied about the flag.
It now flies on m_flying with the drake's controls, moved out of
SkadiPatches into a shared DirectedFlight core that both forms feed
their own numbers into. Powered flight and the sprint cost stamina; out
of stamina the bat glides. Encumbered flight costs 15% more, or is
blocked with 'While Encumbered'. Optional duration and cooldown, off by
default. The aggro/damage patches, the debug-fly speed patch and their
three settings are removed; the drake's behaviour is unchanged.
This commit is contained in:
kanym 2026-10-26 14:26:42 +01:00
commit 77cdc67e97
19 changed files with 534 additions and 516 deletions

View file

@ -1,86 +1,134 @@
using HarmonyLib;
using UnityEngine;
using Jotunn.Managers;
namespace RacesOfValheim
{
/// <summary>
/// The bat's flight, held on for as long as the worn model exists.
///
/// Debug fly has two halves, and this used to write only one. The field Player.m_debugFly is
/// what the local player's own physics reads; ZDOVars.s_debugFly is what every OTHER client
/// reads, because Character.IsDebugFlying consults the ZDO whenever it is not the owner. With
/// only the field written, a bat flew on its own screen and fell, walked and swam on everyone
/// else's - the fault WarVultureMount.SetFlying already names in its own summary. Both halves
/// go through SetFlying below now, so there is one place that can ever get it wrong again.
/// The bat flies on Character.m_flying and is steered by DirectedFlightPatch, the same
/// controls as the Skadi's drake; this class only hands over the bat's own numbers and keeps
/// the flag up. It used to fly on vanilla's debug-fly flag, which ignored carry weight, cost
/// nothing, moved at 50 m/s and made every monster blind to the player - the Discord reports
/// that started the 2026 rebalance.
///
/// Writing the ZDO every frame is not network spam: ZDO.Set(int, bool) bottoms out in
/// ZDOExtraData.Set, which returns false when the value is unchanged, and only a true return
/// bumps the data revision. Read out of the assembly rather than assumed, because "every frame"
/// deserved the check.
/// Other clients learn the bat is airborne from Aswid_IsBatForm in the ZDO, which
/// Aswid_MultiplayerVisuals_Patch mirrors into their copy's m_flying.
/// </summary>
public class BatFormBehavior : MonoBehaviour
public class BatFormBehavior : MonoBehaviour, IDirectedFlyer
{
private static readonly System.Reflection.FieldInfo DebugFlyField =
AccessTools.Field(typeof(Player), "m_debugFly");
private static readonly System.Reflection.FieldInfo FlyingField =
AccessTools.Field(typeof(Character), "m_flying");
private Animator anim;
private AswidProgression.FormRules formRules;
private float startTime;
private bool warnedTooHeavy;
private bool expired;
public void Initialize(AswidProgression.FormRules rules)
{
this.formRules = rules;
}
// ─── IDirectedFlyer ─────────────────────────────────────────────────────
// A bat is never grounded on purpose: it flies for as long as the form lasts.
public bool IsFlying => true;
public float LastPoweredTime { get; set; }
public float TurnSpeed => AswidConfig.BatTurnSpeed.Value;
public float GlideSpeed => AswidConfig.BatGlideSpeed.Value;
public float BoostSpeed => AswidConfig.BatBoostSpeed.Value;
public float SprintSpeed => AswidConfig.BatSprintSpeed.Value;
public float ReverseSpeed => AswidConfig.BatReverseSpeed.Value;
public float LiftoffSpeed => AswidConfig.BatLiftoffSpeed.Value;
public float HoverSeconds => AswidConfig.BatHoverSeconds.Value;
public float PoweredStaminaPerSecond => AswidConfig.BatPoweredStaminaPerSecond.Value * EncumberedCostFactor();
public float SprintStaminaPerSecond => AswidConfig.BatSprintStaminaPerSecond.Value * EncumberedCostFactor();
public bool CanPower => !(IsEncumbered() && AswidConfig.BatEncumbered.Value == AswidConfig.EncumberedMode.Blocked);
private static bool IsEncumbered()
{
return Player.m_localPlayer != null && Player.m_localPlayer.IsEncumbered();
}
private static float EncumberedCostFactor()
{
return IsEncumbered() ? 1f + AswidConfig.BatEncumberedExtraCost.Value : 1f;
}
/// <summary>
/// The form's lifetime in seconds at the current state, or 0 for no limit. The per-state
/// bonus is Dissolution's "scales with boss progression", and only means anything on top
/// of a base limit.
/// </summary>
public static float MaxDuration()
{
float max = AswidConfig.BatMaxDuration.Value;
if (max <= 0f) return 0f;
return max + AswidConfig.BatDurationPerState.Value * BossProgressionManager.GetCurrentProgressionState();
}
private void Start()
{
anim = GetComponentInChildren<Animator>();
startTime = Time.time;
LastPoweredTime = Time.time;
DirectedFlight.Register(this);
SetFlying(true);
}
private void Update()
{
if (Player.m_localPlayer == null || Player.m_localPlayer.IsDead()) return;
Player player = Player.m_localPlayer;
if (player == null || player.IsDead()) return;
// Ensure flight remains on continuously
// Held every frame rather than set once, for the same reason the debug-fly flag was:
// anything that clears it mid-form (a vanilla reset, another mod) would drop the bat
// into walking with the model still on.
SetFlying(true);
// Sync Animator
if (!CanPower && !warnedTooHeavy)
{
warnedTooHeavy = true;
player.Message(MessageHud.MessageType.Center, ModText.Get("rov_msg_aswid_bat_too_heavy_to_beat"));
}
else if (CanPower)
{
warnedTooHeavy = false;
}
float max = MaxDuration();
if (max > 0f && !expired && Time.time - startTime >= max && AswidRace.Instance != null)
{
expired = true;
player.Message(MessageHud.MessageType.Center, ModText.Get("rov_msg_aswid_bat_form_fades"));
AswidRace.Instance.ForceRevert();
}
if (anim != null)
{
Vector3 vel = Player.m_localPlayer.GetVelocity();
Vector3 vel = player.GetVelocity();
vel.y = 0f;
float speed = vel.magnitude;
// Bats don't necessarily have a complex animator like players,
// but if they do have a speed or flying parameter, we set it here.
anim.SetFloat("forward_speed", speed);
anim.SetFloat("forward_speed", vel.magnitude);
anim.SetBool("flying", true);
}
}
private void OnDestroy()
{
DirectedFlight.Unregister(this);
SetFlying(false);
}
/// <summary>
/// Both halves of debug fly, always together. The field write survives a missing or invalid
/// ZNetView on purpose: without it a bat whose networking went away mid-flight would keep
/// flying on foot forever, which is the worse of the two failures.
/// The local half of flight. The remote half is the Aswid_IsBatForm ZDO flag AswidRace
/// already writes, so there is nothing to replicate here.
/// </summary>
private static void SetFlying(bool flying)
public static void SetFlying(bool flying)
{
Player player = Player.m_localPlayer;
if (player == null) return;
if (DebugFlyField != null) DebugFlyField.SetValue(player, flying);
ZNetView nview = player.GetComponent<ZNetView>();
if (nview != null && nview.IsValid() && nview.IsOwner())
{
nview.GetZDO().Set(ZDOVars.s_debugFly, flying);
}
if (player == null || FlyingField == null) return;
FlyingField.SetValue(player, flying);
}
}
}