RacesOfValheim/JotunnModStub/Aswid/BatFormBehavior.cs
kanym 77cdc67e97 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.
2026-10-26 14:26:42 +01:00

134 lines
5.4 KiB
C#

using HarmonyLib;
using UnityEngine;
namespace RacesOfValheim
{
/// <summary>
/// The bat's flight, held on for as long as the worn model exists.
///
/// 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.
///
/// 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, IDirectedFlyer
{
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()
{
Player player = Player.m_localPlayer;
if (player == null || player.IsDead()) return;
// 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);
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.GetVelocity();
vel.y = 0f;
anim.SetFloat("forward_speed", vel.magnitude);
anim.SetBool("flying", true);
}
}
private void OnDestroy()
{
DirectedFlight.Unregister(this);
SetFlying(false);
}
/// <summary>
/// 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>
public static void SetFlying(bool flying)
{
Player player = Player.m_localPlayer;
if (player == null || FlyingField == null) return;
FlyingField.SetValue(player, flying);
}
}
}