diff --git a/JotunnModStub/Skadi/DrakeFormBehavior.cs b/JotunnModStub/Skadi/DrakeFormBehavior.cs index 53a4122..75a6a6e 100644 --- a/JotunnModStub/Skadi/DrakeFormBehavior.cs +++ b/JotunnModStub/Skadi/DrakeFormBehavior.cs @@ -6,11 +6,21 @@ namespace RacesOfValheim { public class DrakeFormBehavior : MonoBehaviour { + // Single active instance, read by SkadiDirectedFlightPatch to feed a + // look-direction-based m_moveDir into Character.UpdateFlying every frame. + public static DrakeFormBehavior Active { get; private set; } + private Animator anim; private bool isFlying = false; private bool isLanding = false; private float lastAttackTime = 0f; - + + public bool IsFlying => isFlying; + + // Time.time of the last powered/grounded moment. SkadiDirectedFlightPatch holds the form + // level for a short window past this before letting it start sinking. + public float LastPoweredTime; + private SkadiProgression.FormRules formRules; public void Initialize(SkadiProgression.FormRules rules) @@ -18,15 +28,37 @@ namespace RacesOfValheim this.formRules = rules; } + // formRules is a snapshot taken when the form was entered, so a progression change while + // still transformed (defeating a boss, or the increaseprogression debug command) wouldn't + // grant the attack until the player reverted and transformed again. Re-read the rules for + // the form we're currently in so the unlock applies as soon as the tier is reached. + private bool CanAttackNow() + { + if (formRules == null) return false; + + int state = BossProgressionManager.GetCurrentProgressionState(); + SkadiProgression.FormRules live = null; + + switch (formRules.Form) + { + case SkadiProgression.FormType.Drake: live = SkadiProgression.GetGKeyRules(state); break; + case SkadiProgression.FormType.Moder: live = SkadiProgression.GetHKeyRules(state); break; + case SkadiProgression.FormType.SkeletonDragon: live = SkadiProgression.GetJKeyRules(state); break; + } + + return live != null && live.Form == formRules.Form && live.CanAttackInForm; + } + private const float ATTACK_COST = 20f; private const float ATTACK_COOLDOWN = 3f; private void Start() { + Active = this; anim = GetComponentInChildren(); if (Player.m_localPlayer != null) { - HarmonyLib.Traverse.Create(Player.m_localPlayer).Field("m_debugFly").SetValue(false); + HarmonyLib.Traverse.Create(Player.m_localPlayer).Field("m_flying").SetValue(false); } } @@ -40,20 +72,22 @@ namespace RacesOfValheim if (!isFlying && !isLanding) { isFlying = true; - HarmonyLib.Traverse.Create(Player.m_localPlayer).Field("m_debugFly").SetValue(true); + LastPoweredTime = Time.time; + HarmonyLib.Traverse.Create(Player.m_localPlayer).Field("m_flying").SetValue(true); } else if (isFlying && !isLanding) { isLanding = true; isFlying = false; - HarmonyLib.Traverse.Create(Player.m_localPlayer).Field("m_debugFly").SetValue(false); + HarmonyLib.Traverse.Create(Player.m_localPlayer).Field("m_flying").SetValue(false); } else if (isLanding) { // Cancel landing, resume flight isLanding = false; isFlying = true; - HarmonyLib.Traverse.Create(Player.m_localPlayer).Field("m_debugFly").SetValue(true); + LastPoweredTime = Time.time; + HarmonyLib.Traverse.Create(Player.m_localPlayer).Field("m_flying").SetValue(true); } } @@ -88,7 +122,7 @@ namespace RacesOfValheim } // Handle Attack - if (formRules != null && formRules.CanAttackInForm) + if (CanAttackNow()) { if (Input.GetMouseButtonDown(0)) { @@ -345,9 +379,10 @@ namespace RacesOfValheim private void OnDestroy() { + if (Active == this) Active = null; if (Player.m_localPlayer != null) { - HarmonyLib.Traverse.Create(Player.m_localPlayer).Field("m_debugFly").SetValue(false); + HarmonyLib.Traverse.Create(Player.m_localPlayer).Field("m_flying").SetValue(false); } } } diff --git a/JotunnModStub/Skadi/SkadiPatches.cs b/JotunnModStub/Skadi/SkadiPatches.cs index e79121c..7675048 100644 --- a/JotunnModStub/Skadi/SkadiPatches.cs +++ b/JotunnModStub/Skadi/SkadiPatches.cs @@ -200,6 +200,79 @@ namespace RacesOfValheim + // DrakeFormBehavior flips the player into Character's native flying-creature movement + // (m_flying = true) instead of the old debug-fly noclip, which lets us feed it a full 3D + // move direction. Character.UpdateFlying normally reads whatever m_moveDir the vanilla input + // pipeline last wrote (ground-plane WASD relative to camera yaw only); this patch overwrites + // it every frame with the camera's actual look direction (pitch included) so the player always + // glides toward wherever the mouse is aiming, with W adding a speed boost (m_run) on top of the + // base glide speed instead of being required just to move. + [HarmonyPatch(typeof(Character), "UpdateFlying")] + public static class SkadiDirectedFlightPatch + { + private const float TURN_SPEED = 120f; + private const float GLIDE_SPEED = 5f; + private const float BOOST_SPEED = 12f; + private const float HOVER_SECONDS = 2f; + // Tangent of the unpowered descent angle: 0.4 sinks at roughly 22 degrees below level. + private const float DESCENT_SLOPE = 0.4f; + + public static void Prefix(Character __instance) + { + if (__instance != Player.m_localPlayer) return; + + DrakeFormBehavior form = DrakeFormBehavior.Active; + if (form == null || !form.IsFlying) return; + + var traverse = Traverse.Create(__instance); + Vector3 lookDir = Player.m_localPlayer.GetLookDir(); + bool boost = Input.GetKey(KeyCode.W); + bool onGround = __instance.IsOnGround(); + + traverse.Field("m_run").SetValue(boost); + traverse.Field("m_flyFastSpeed").SetValue(BOOST_SPEED); + + // UpdateFlying rotates the body toward m_moveDir on its own, but at m_flyTurnSpeed, + // which sits at Character's 12 deg/s default because the Player prefab was never set + // up as a flying creature - a 90 degree turn would take over seven seconds, so the + // model reads as not turning at all. Give it a speed that suits a flying mount. + traverse.Field("m_flyTurnSpeed").SetValue(TURN_SPEED); + + // Powered flight and ground contact both refresh the hover window. + if (boost || onGround) form.LastPoweredTime = Time.time; + + Vector3 moveDir = lookDir; + float coastSpeed; + + if (onGround || Time.time - form.LastPoweredTime < HOVER_SECONDS) + { + // Holding station: on the ground this keeps a touchdown from sliding the player + // forward forever, and in the air it gives the beat of level flight before the + // form starts to sink. m_moveDir stays full length so the body still turns to + // face the camera, and zeroing the speed (rather than the rigidbody velocity) + // avoids fighting the AddForce UpdateFlying queues, which caused visible jitter. + coastSpeed = 0f; + } + else + { + // Unpowered flight is a glide, not a hover with a joystick: drift along the + // heading the camera faces while losing height, so aiming at the sky without + // holding W no longer climbs. + Vector3 heading = new Vector3(lookDir.x, 0f, lookDir.z); + if (heading.sqrMagnitude < 0.0001f) + { + heading = __instance.transform.forward; + heading.y = 0f; + } + moveDir = (heading.normalized + Vector3.down * DESCENT_SLOPE).normalized; + coastSpeed = GLIDE_SPEED; + } + + traverse.Field("m_moveDir").SetValue(moveDir); + traverse.Field("m_flySlowSpeed").SetValue(coastSpeed); + } + } + [HarmonyPatch(typeof(Humanoid), "StartAttack")] public static class SkadiAttackPatch { diff --git a/JotunnModStub/Skadi/SkadiRace.cs b/JotunnModStub/Skadi/SkadiRace.cs index 4d5d5bd..071dac5 100644 --- a/JotunnModStub/Skadi/SkadiRace.cs +++ b/JotunnModStub/Skadi/SkadiRace.cs @@ -11,6 +11,7 @@ namespace RacesOfValheim public static SkadiRace Instance; public static SE_SkadiStats skadiCurseSE; public static SE_FormCooldown skadiCdSE; + public static SE_SkadiForm skadiFormSE; private float auraTimer = 0f; private float lastSpellTime = 0f; @@ -18,6 +19,8 @@ namespace RacesOfValheim private bool isTransformed = false; private bool isTransformingSequence = false; private SkadiProgression.FormRules activeRules = null; + // Time.time at which a timed form drops; 0 means the current form has no time limit. + private float formExpireTime = 0f; private float lastAuraTime = 0f; private GameObject frostAuraVFX = null; @@ -47,18 +50,33 @@ namespace RacesOfValheim ItemManager.Instance.AddStatusEffect(skadiStats); skadiCurseSE = skadiStats.StatusEffect as SE_SkadiStats; + // Timed forms and their cooldown are Moder's, so they carry her trophy rather than the + // drake one used for the passive blessing. + Sprite dragonIcon = GetTrophyIcon("TrophyDragonQueen") ?? GetTrophyIcon("TrophyHatchling"); + skadiCdSE = ScriptableObject.CreateInstance(); skadiCdSE.name = "SE_SkadiCooldown"; skadiCdSE.m_name = "Dragon Form Cooldown"; - if (drakeTrophy != null) - { - ItemDrop trophyDrop = drakeTrophy.GetComponent(); - if (trophyDrop != null && trophyDrop.m_itemData.m_shared.m_icons.Length > 0) - { - skadiCdSE.m_icon = trophyDrop.m_itemData.m_shared.m_icons[0]; - } - } + skadiCdSE.m_icon = dragonIcon; ItemManager.Instance.AddStatusEffect(new CustomStatusEffect(skadiCdSE, fixReference: false)); + + skadiFormSE = ScriptableObject.CreateInstance(); + skadiFormSE.name = "SE_SkadiForm"; + skadiFormSE.m_name = "Dragon Form"; + skadiFormSE.m_tooltip = "Your dragon shape holds for a while longer."; + skadiFormSE.m_icon = dragonIcon; + ItemManager.Instance.AddStatusEffect(new CustomStatusEffect(skadiFormSE, fixReference: false)); + } + + private static Sprite GetTrophyIcon(string prefabName) + { + GameObject trophy = PrefabManager.Cache.GetPrefab(prefabName)?.gameObject; + ItemDrop drop = trophy != null ? trophy.GetComponent() : null; + if (drop != null && drop.m_itemData.m_shared.m_icons.Length > 0) + { + return drop.m_itemData.m_shared.m_icons[0]; + } + return null; } @@ -139,6 +157,16 @@ namespace RacesOfValheim if (isTransformingSequence) return; + // Forms with a DurationMinutes budget drop on their own once it runs out, which is + // also what starts their cooldown - without this the timed tiers never expired. + if (isTransformed && formExpireTime > 0f && Time.time >= formExpireTime) + { + formExpireTime = 0f; + Player.m_localPlayer.Message(MessageHud.MessageType.Center, "Your dragon form fades."); + StartCoroutine(RevertToHumanRoutine()); + return; + } + int state = BossProgressionManager.GetCurrentProgressionState(); if (Input.GetKeyDown(KeyCode.G)) { TryTransformWithRules(SkadiProgression.GetGKeyRules(state)); return; } if (Input.GetKeyDown(KeyCode.H)) { TryTransformWithRules(SkadiProgression.GetHKeyRules(state)); return; } @@ -589,10 +617,33 @@ namespace RacesOfValheim else if (prefabName == "Fader") Player.m_localPlayer.Message(MessageHud.MessageType.Center, "You are Fader!"); isTransformed = true; + StartFormTimer(rules); } } } + // Drives both the expiry check in Update and the HUD icon: the status effect exists purely + // so Valheim draws Moder's trophy with its own countdown next to the minimap. + private void StartFormTimer(SkadiProgression.FormRules rules) + { + int hash = "SE_SkadiForm".GetStableHashCode(); + SEMan seman = Player.m_localPlayer.GetSEMan(); + seman.RemoveStatusEffect(hash, true); + + if (rules.DurationMinutes <= 0f) + { + formExpireTime = 0f; + return; + } + + float seconds = rules.DurationMinutes * 60f; + formExpireTime = Time.time + seconds; + + seman.AddStatusEffect(hash, true); + StatusEffect se = seman.GetStatusEffect(hash); + if (se != null) se.m_ttl = seconds; + } + private void ExecuteReversion() { Transform playerVisual = Player.m_localPlayer.transform.Find("Visual"); @@ -615,6 +666,8 @@ namespace RacesOfValheim } isTransformed = false; + formExpireTime = 0f; + Player.m_localPlayer.GetSEMan().RemoveStatusEffect("SE_SkadiForm".GetStableHashCode(), true); Player.m_localPlayer.Message(MessageHud.MessageType.Center, "You returned to human form."); if (activeRules != null && activeRules.CooldownMinutes > 0) diff --git a/JotunnModStub/Skadi/SkadiStatusEffects.cs b/JotunnModStub/Skadi/SkadiStatusEffects.cs index 536e8de..b6517af 100644 --- a/JotunnModStub/Skadi/SkadiStatusEffects.cs +++ b/JotunnModStub/Skadi/SkadiStatusEffects.cs @@ -4,6 +4,12 @@ using UnityEngine; namespace RacesOfValheim { + // Timed dragon forms. m_ttl is set when the form is entered, which is all Valheim needs to + // render the countdown: StatusEffect.GetIconText() prints the remaining time by itself. + public class SE_SkadiForm : StatusEffect + { + } + public class SE_SkadiStats : SE_Stats { public override void Setup(Character character) diff --git a/README.md b/README.md index 3132ba0..813b42e 100644 --- a/README.md +++ b/README.md @@ -81,13 +81,18 @@ This mod currently introduces 6 custom races: ### Skadi Progression - **Base (No Bosses)**: Immune to Cold, Bonus Frost Damage, 50% Frost Resistance. -- **Eikthyr**: +20% Eitr Regen, Freezing to Cold, Cold Spell. -- **The Elder**: Drake Form (Cannot attack). -- **Bonemass**: Drake Form (Can attack). -- **Moder**: Moder Form (1 min duration, 10 min cooldown). -- **Yagluth**: Improved Cold Spell. Moder Form (1 min duration, 5 min cooldown). -- **The Queen**: Moder Form (Unlimited duration, no cooldown). -- **Fader**: Skeleton Dragon Form. +- **Eikthyr**: +20% Eitr Regen, Freezing to Cold, Cold Spell (Middle Mouse). +- **The Elder**: Drake Form ('G' Key) (Cannot attack). +- **Bonemass**: Drake Form ('G' Key) (Can attack). +- **Moder**: Moder Form ('H' Key) (1 min duration, 10 min cooldown). +- **Yagluth**: Improved Cold Spell. Moder Form ('H' Key) (1 min duration, 5 min cooldown). +- **The Queen**: Moder Form ('H' Key) (Unlimited duration, no cooldown). +- **Fader**: Skeleton Dragon Form ('J' Key). + +While transformed, Right Mouse toggles flight. You glide toward wherever you are aiming; hold W +to power forward and climb. Let go and the form holds level for a couple of seconds before +settling into a gentle descent. Timed forms show their remaining seconds, and their cooldown, +as a Moder trophy icon beside the minimap. ### Skeleton Progression - **Base (No Bosses)**: +50 Max Health. Undead Diet (Cannot eat mortal food, no stamina bar).