RacesOfValheim/JotunnModStub/Aswid/BatFormBehavior.cs
kanym 6f439f37c3 feat: Free Bat Flight, the old bat flight as a choice
The old bat flight is back as Free Flight: vanilla debug fly, up to 50 m/s, no stamina and no weight limit, Jump rises and Left Ctrl sinks. Each player picks it with Aswid - Bat Form / Free Bat Flight (local, never synced). The synced Bat Flight Rule (PlayerChoice, AlwaysStaminaFlight, AlwaysFreeFlight) lets the host or server decide for everyone. The mode is read when the form goes up, so a change counts from the next transform.

Debug fly makes vanilla treat the player as absent: no monster sees, hears or wakes for them, and hits are dropped. The six patches removed in v1.6.1 are back in AswidBatForm, for bats only and with no switch, so a free bat is fast but not invisible or immortal. Turning flight off now clears both flags, so death and reset end either flight. The compendium shows the Free Flight line when it is the active mode.

Asked for by MrBiffty, who builds tall with the bat.
2026-10-29 19:33:30 +01:00

169 lines
7.1 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.
///
/// FREE FLIGHT. A player who picks it (or a server that forces it, AswidConfig.UsesFreeBatFlight)
/// gets the old flight back: vanilla's debug fly, both halves (the field for the local physics,
/// s_debugFly in the ZDO for everyone else), and no DirectedFlight. The mode is read once, when
/// the form goes up. AswidBatForm keeps monsters seeing and hurting a free-flying bat.
/// </summary>
public class BatFormBehavior : MonoBehaviour, IDirectedFlyer
{
private static readonly System.Reflection.FieldInfo FlyingField =
AccessTools.Field(typeof(Character), "m_flying");
private static readonly System.Reflection.FieldInfo DebugFlyField =
AccessTools.Field(typeof(Player), "m_debugFly");
private Animator anim;
private AswidProgression.FormRules formRules;
private float startTime;
private bool warnedTooHeavy;
private bool expired;
private bool free;
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;
free = AswidConfig.UsesFreeBatFlight();
if (!free) DirectedFlight.Register(this);
Hold();
}
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.
Hold();
// Free Flight ignores weight, so only Stamina Flight warns about it.
if (!free)
{
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);
}
private void Hold()
{
if (free) SetDebugFly(true);
else SetFlying(true);
}
/// <summary>
/// The local half of Stamina Flight. The remote half is the Aswid_IsBatForm ZDO flag
/// AswidRace already writes, so there is nothing to replicate here. Turning it off also
/// turns debug fly off, so every exit path ends both flights whichever one was up.
/// </summary>
public static void SetFlying(bool flying)
{
Player player = Player.m_localPlayer;
if (player == null) return;
if (FlyingField != null) FlyingField.SetValue(player, flying);
if (!flying) SetDebugFly(false);
}
/// <summary>
/// Both halves of debug fly, for Free Flight. The ZDO write every frame is not network
/// spam: ZDO.Set returns early when the value is unchanged.
/// </summary>
private static void SetDebugFly(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);
}
}
}