diff --git a/JotunnModStub/Aswid/AswidRace.cs b/JotunnModStub/Aswid/AswidRace.cs index e8bb4c7..b95daff 100644 --- a/JotunnModStub/Aswid/AswidRace.cs +++ b/JotunnModStub/Aswid/AswidRace.cs @@ -463,7 +463,7 @@ namespace RacesOfValheim if (EnvMan.instance != null && EnvMan.IsDay() && !isTransformingSequence && Player.m_localPlayer.GetSEMan().HaveStatusEffect("SE_MistForm".GetStableHashCode())) { - StartCoroutine(RevertMistFormRoutine()); + RunFormRoutine(RevertMistFormRoutine()); } // Same rule for a night-only bat: a form the sun forbids cannot be kept through the @@ -474,7 +474,7 @@ namespace RacesOfValheim EnvMan.instance != null && EnvMan.IsDay()) { Player.m_localPlayer.Message(MessageHud.MessageType.Center, ModText.Get("rov_msg_aswid_morning_sun_drives_bat_back")); - StartCoroutine(RevertToHumanRoutine()); + RunFormRoutine(RevertToHumanRoutine()); } } @@ -565,7 +565,7 @@ namespace RacesOfValheim if (IsTransformed) { - StartCoroutine(RevertToHumanRoutine()); + RunFormRoutine(RevertToHumanRoutine()); return; } @@ -580,18 +580,73 @@ namespace RacesOfValheim return; } - StartCoroutine(TransformIntoBatRoutine(rules)); + RunFormRoutine(TransformIntoBatRoutine(rules)); + } + + // The one bat or mist routine in flight, if any. Kept so ResetRace can stop it: a + // TransformIntoBatRoutine left running through a reset finishes its 1.1s gesture and + // turns a freshly reset Human into a bat. + private Coroutine formRoutine; + + private void RunFormRoutine(System.Collections.IEnumerator routine) + { + formRoutine = StartCoroutine(routine); + } + + /// + /// Drops bat and mist form on the spot for ResetRace: no puff, no tween, nothing left + /// running. ForceRevert is no use there - it skips a form that is still mid-gesture, and + /// its coroutine would still be shrinking the body back after the reset has finished. + /// + /// True if there was a form to drop. + public bool HardResetForReset() + { + Player player = Player.m_localPlayer; + if (player == null) return false; + + if (formRoutine != null) + { + StopCoroutine(formRoutine); + formRoutine = null; + } + + bool hadMist = mistTransformation != null || player.GetSEMan().HaveStatusEffect("SE_MistForm".GetStableHashCode()); + bool hadBat = IsTransformed || isTransformingSequence; + + // Also clears isTransformingSequence and puts the transform back to scale 1, which + // covers a reset caught halfway through the bat's shrink. + HardResetBatForm(); + + if (hadMist) + { + if (mistTransformation != null) + { + Destroy(mistTransformation); + mistTransformation = null; + } + + Transform playerVisual = player.transform.Find("Visual"); + PlayerVisualHider.Show(playerVisual, "aswid-reset-mist"); + if (playerVisual != null) playerVisual.localScale = Vector3.one; + + player.GetSEMan().RemoveStatusEffect("SE_MistForm".GetStableHashCode(), true); + ZNetView nview = player.GetComponent(); + if (nview != null && nview.IsValid()) nview.GetZDO().Set("Aswid_IsMistForm", false); + } + + isTransformingSequence = false; + return hadBat || hadMist; } public void ForceRevert() { if (IsTransformed && !isTransformingSequence) { - StartCoroutine(RevertToHumanRoutine()); + RunFormRoutine(RevertToHumanRoutine()); } if (Player.m_localPlayer != null && Player.m_localPlayer.GetSEMan().HaveStatusEffect("SE_MistForm".GetStableHashCode()) && !isTransformingSequence) { - StartCoroutine(RevertMistFormRoutine()); + RunFormRoutine(RevertMistFormRoutine()); } } @@ -745,11 +800,11 @@ namespace RacesOfValheim if (Player.m_localPlayer.GetSEMan().HaveStatusEffect("SE_MistForm".GetStableHashCode())) { - StartCoroutine(RevertMistFormRoutine()); + RunFormRoutine(RevertMistFormRoutine()); } else { - StartCoroutine(TransformIntoMistRoutine()); + RunFormRoutine(TransformIntoMistRoutine()); } } diff --git a/JotunnModStub/Fenrir/FenrirRace.cs b/JotunnModStub/Fenrir/FenrirRace.cs index c1d7664..ee5cd5a 100644 --- a/JotunnModStub/Fenrir/FenrirRace.cs +++ b/JotunnModStub/Fenrir/FenrirRace.cs @@ -185,7 +185,7 @@ namespace RacesOfValheim { if (isTransformed) { - StartCoroutine(RevertToHumanRoutine()); + RunFormRoutine(RevertToHumanRoutine()); return; } @@ -213,7 +213,7 @@ namespace RacesOfValheim { } } - StartCoroutine(TransformIntoWolfRoutine(rules)); + RunFormRoutine(TransformIntoWolfRoutine(rules)); } /// @@ -256,11 +256,63 @@ namespace RacesOfValheim { return "$rov_msg_fenrir_formname_fenrir"; } + // The one transform or revert routine in flight, if any, so ResetRace can stop it. A + // revert left to finish on its own lands its cooldown after the reset has already swept + // the status effects, and the next Fenrir starts locked out. + private Coroutine formRoutine; + + private void RunFormRoutine(IEnumerator routine) + { + formRoutine = StartCoroutine(routine); + } + + /// + /// Drops whatever form is up, immediately, for ResetRace: no poof, no message, and no + /// cooldown - ExecuteReversion hands one out, which is right for a form running out and + /// wrong for a player leaving the race. + /// + /// True if there was a form to drop. + public bool HardResetForReset() + { + Player player = Player.m_localPlayer; + if (player == null) return false; + + if (formRoutine != null) + { + StopCoroutine(formRoutine); + formRoutine = null; + } + + bool hadForm = isTransformed || isTransformingSequence || currentTransformation != null; + if (hadForm) + { + if (currentTransformation != null) Destroy(currentTransformation); + currentTransformation = null; + DestroyAllForms(); + + ZNetView nview = player.GetComponent(); + if (nview != null && nview.IsValid()) nview.GetZDO().Set("RacesOfValheim_FenrirForm", ""); + + PlayerVisualHider.Show(player.transform.Find("Visual"), "fenrir-reset"); + + player.GetSEMan().RemoveStatusEffect("SE_FenrirForm".GetStableHashCode()); + player.GetSEMan().RemoveStatusEffect("SE_CultistForm".GetStableHashCode()); + player.GetSEMan().RemoveStatusEffect("SE_WolfForm".GetStableHashCode()); + } + + isTransformed = false; + isTransformingSequence = false; + activeRules = null; + revertMessage = null; + sunSicknessTimer = 0f; + return hadForm; + } + public void ForceRevert() { if (isTransformed && !isTransformingSequence) { - StartCoroutine(RevertToHumanRoutine()); + RunFormRoutine(RevertToHumanRoutine()); } } diff --git a/JotunnModStub/RacesOfValheim.cs b/JotunnModStub/RacesOfValheim.cs index 6e826c6..e6f8289 100644 --- a/JotunnModStub/RacesOfValheim.cs +++ b/JotunnModStub/RacesOfValheim.cs @@ -4,6 +4,7 @@ using HarmonyLib; using Jotunn.Configs; using Jotunn.Entities; using Jotunn.Managers; +using Jotunn.Utils; using UnityEngine; using UnityEngine.UI; // Added for UI @@ -99,6 +100,20 @@ namespace RacesOfValheim Player player = Player.m_localPlayer; if (player == null) return; + // What the reset actually took off, printed at the end so it can be read in the console. + var removed = new System.Collections.Generic.List(); + + // Forms first, before any status effect goes. Taking a form's effect away without + // undoing the form leaves the body hidden (bat, mist, drake, wolf, Muspell, Ravager) + // or oversized (True Form), and several races only ever look at their form from an + // Update that returns early for anyone who is not of that race any more. + if (AswidRace.Instance != null && AswidRace.Instance.HardResetForReset()) removed.Add("form: Aswid bat/mist"); + if (FenrirRace.Instance != null && FenrirRace.Instance.HardResetForReset()) removed.Add("form: Fenrir"); + if (SkadiRace.Instance != null && SkadiRace.Instance.HardResetForReset()) removed.Add("form: Skadi drake"); + if (TrolRace.Instance != null && TrolRace.Instance.HardResetForReset()) removed.Add("form: Trol True Form"); + if (TrentaMuspell.HardResetForReset(player)) removed.Add("form: Trenta Muspell"); + if (SvermRavagerForm.HardResetForReset(player)) removed.Add("form: Sverm Ravager"); + RemovePermanentRaceEquipment(player); player.m_customData.Remove("Race"); @@ -120,21 +135,23 @@ namespace RacesOfValheim } } - player.GetSEMan().RemoveStatusEffect("SE_FenrirCurse".GetStableHashCode()); - player.GetSEMan().RemoveStatusEffect("SE_AswidStats".GetStableHashCode()); - player.GetSEMan().RemoveStatusEffect("SE_SkeletonStats".GetStableHashCode()); - player.GetSEMan().RemoveStatusEffect("SE_SkadiStats".GetStableHashCode()); - player.GetSEMan().RemoveStatusEffect("SE_SkadiCooldown".GetStableHashCode()); - player.GetSEMan().RemoveStatusEffect(DvergarConfig.StatsSE.GetStableHashCode()); - player.GetSEMan().RemoveStatusEffect(DvergarConfig.OverchargeSE.GetStableHashCode()); - player.GetSEMan().RemoveStatusEffect(DvergarConfig.OverchargeCdSE.GetStableHashCode()); - player.GetSEMan().RemoveStatusEffect(DvergarConfig.RunicPingCdSE.GetStableHashCode()); - player.GetSEMan().RemoveStatusEffect(TrentaConfig.StatsSE.GetStableHashCode()); - player.GetSEMan().RemoveStatusEffect(TrentaConfig.PurpleRainCdSE.GetStableHashCode()); - player.GetSEMan().RemoveStatusEffect(TrentaConfig.PortalCdSE.GetStableHashCode()); - player.GetSEMan().RemoveStatusEffect(SvermConfig.StatsSE.GetStableHashCode()); - player.GetSEMan().RemoveStatusEffect(SvermConfig.RavagerSE.GetStableHashCode()); - player.GetSEMan().RemoveStatusEffect(SvermConfig.RavagerCdSE.GetStableHashCode()); + // Every status effect this mod registers, rather than a list kept by hand: the hand + // list was missing the True Form and both of its cooldowns, Bat and Mist Form, the + // Skeleton's resurrect cooldown and most of Skadi's, and the next effect anyone adds + // would have been missing too. The forms were dropped above, so taking their effects + // away here no longer leaves a body hidden or blown up. + SEMan seman = player.GetSEMan(); + foreach (CustomStatusEffect custom in ModRegistry.GetStatusEffects(PluginGUID)) + { + StatusEffect se = custom?.StatusEffect; + if (se == null) continue; + + int hash = se.name.GetStableHashCode(); + if (!seman.HaveStatusEffect(hash)) continue; + + seman.RemoveStatusEffect(hash, true); + removed.Add(se.name); + } // COLD, which is not one of ours at all - and that is exactly why it has to go. // @@ -161,9 +178,21 @@ namespace RacesOfValheim // Only after the ZDO above already says "Human" - see the class doc comment. RaceItemCleanup.PurgeForeignRaceItems(player); + // Normal size. The Trol, the Skeleton past Fader and the self-sizing races all scale + // either the player or its Visual, and each puts its own size back only from code that + // may not run again now the race has changed. The next race sets its own size anyway. + player.transform.localScale = Vector3.one; + GameObject visual = Traverse.Create(player).Field("m_visual").GetValue(); + if (visual != null) visual.transform.localScale = Vector3.one; + + string summary = removed.Count == 0 + ? "[ResetRace] Nothing race-specific was left on the player." + : "[ResetRace] Removed: " + string.Join(", ", removed); + Jotunn.Logger.LogInfo(summary); + if (Console.instance != null) Console.instance.Print(summary); + if (Instance != null) { - if (FenrirRace.Instance != null) FenrirRace.Instance.ForceRevert(); Instance.ShowRaceSelectionMenu(); } } diff --git a/JotunnModStub/Skadi/SkadiRace.cs b/JotunnModStub/Skadi/SkadiRace.cs index 573685d..27ea13e 100644 --- a/JotunnModStub/Skadi/SkadiRace.cs +++ b/JotunnModStub/Skadi/SkadiRace.cs @@ -247,7 +247,7 @@ namespace RacesOfValheim { formExpireTime = 0f; Player.m_localPlayer.Message(MessageHud.MessageType.Center, ModText.Get("rov_msg_skadi_dragon_form_fades")); - StartCoroutine(RevertToHumanRoutine()); + RunFormRoutine(RevertToHumanRoutine()); return; } @@ -687,7 +687,7 @@ namespace RacesOfValheim if (isTransformed) { - StartCoroutine(RevertToHumanRoutine()); + RunFormRoutine(RevertToHumanRoutine()); return; } @@ -707,7 +707,7 @@ namespace RacesOfValheim } activeRules = rules; - StartCoroutine(TransformRoutine(rules)); + RunFormRoutine(TransformRoutine(rules)); } private Vector3 GetFormScale(SkadiProgression.FormType form) @@ -914,6 +914,57 @@ namespace RacesOfValheim if (se != null) se.m_ttl = seconds; } + // The one transform or revert routine in flight, if any, so ResetRace can stop it. + private Coroutine formRoutine; + + private void RunFormRoutine(System.Collections.IEnumerator routine) + { + formRoutine = StartCoroutine(routine); + } + + /// + /// Drops the drake form on the spot for ResetRace. Update bails out on its first line for + /// anyone who is not a Skadi, so once the race changes nothing in this class ever looks at + /// the form again - a Skadi reset in drake form stayed a drake for good. No message and no + /// cooldown here, unlike ExecuteReversion. + /// + /// True if there was a form to drop. + public bool HardResetForReset() + { + Player player = Player.m_localPlayer; + if (player == null) return false; + + if (formRoutine != null) + { + StopCoroutine(formRoutine); + formRoutine = null; + } + + bool hadForm = isTransformed || isTransformingSequence || currentTransformation != null; + if (hadForm) + { + if (currentTransformation != null) Destroy(currentTransformation); + currentTransformation = null; + foreach (var f in player.GetComponentsInChildren(true)) Destroy(f.gameObject); + + ZNetView nview = player.GetComponent(); + if (nview != null && nview.IsValid()) + { + nview.GetZDO().Set("Skadi_Form", ""); + nview.GetZDO().Set("RacesOfValheim_SkadiForm", ""); + } + + PlayerVisualHider.Show(player.transform.Find("Visual"), "skadi-reset"); + player.GetSEMan().RemoveStatusEffect("SE_SkadiForm".GetStableHashCode(), true); + } + + isTransformed = false; + isTransformingSequence = false; + formExpireTime = 0f; + activeRules = null; + return hadForm; + } + private void ExecuteReversion() { Transform playerVisual = Player.m_localPlayer.transform.Find("Visual"); diff --git a/JotunnModStub/Sverm/SvermRavagerForm.cs b/JotunnModStub/Sverm/SvermRavagerForm.cs index b0e6497..f41b7f0 100644 --- a/JotunnModStub/Sverm/SvermRavagerForm.cs +++ b/JotunnModStub/Sverm/SvermRavagerForm.cs @@ -177,7 +177,7 @@ namespace RacesOfValheim if (SvermRace.Instance != null) { - SvermRace.Instance.StartCoroutine(EnterRoutine(player)); + formRoutine = SvermRace.Instance.StartCoroutine(EnterRoutine(player)); } else { @@ -342,12 +342,42 @@ namespace RacesOfValheim } } + // The enter or exit routine in flight, if any, so ResetRace can stop it. An ExitRoutine + // left running finishes with Exit(spendCooldown: true), which puts the cooldown back on + // after the reset has swept it. + private static Coroutine formRoutine; + + /// + /// Drops Ravager Strain on the spot for ResetRace, and forgets its cooldown: nextAllowedAt + /// is a static the status effect sweep never touches, so without this a player who left + /// the Sverm and came back stayed locked out of the form with no timer in the HUD. + /// + /// True if there was a form to drop. + public static bool HardResetForReset(Player player) + { + if (player == null) return false; + + if (formRoutine != null && SvermRace.Instance != null) + { + SvermRace.Instance.StopCoroutine(formRoutine); + } + formRoutine = null; + + bool hadForm = IsActive || transforming || exiting || model != null; + if (IsActive || model != null) Exit(player, spendCooldown: false); + + transforming = false; + exiting = false; + nextAllowedAt = 0f; + return hadForm; + } + /// Kicks off the graceful exit (shrink, then Exit) if there is a host to run it on. private static void RunExit(Player player, bool spendCooldown, string message) { if (SvermRace.Instance != null) { - SvermRace.Instance.StartCoroutine(ExitRoutine(player, spendCooldown, message)); + formRoutine = SvermRace.Instance.StartCoroutine(ExitRoutine(player, spendCooldown, message)); } else { diff --git a/JotunnModStub/Trenta/TrentaMuspell.cs b/JotunnModStub/Trenta/TrentaMuspell.cs index 4499faf..d3af9ef 100644 --- a/JotunnModStub/Trenta/TrentaMuspell.cs +++ b/JotunnModStub/Trenta/TrentaMuspell.cs @@ -53,11 +53,11 @@ namespace RacesOfValheim if (IsTransformed) { - TrentaRace.Instance.StartCoroutine(RevertRoutine(player)); + formRoutine = TrentaRace.Instance.StartCoroutine(RevertRoutine(player)); } else { - TrentaRace.Instance.StartCoroutine(TransformRoutine(player)); + formRoutine = TrentaRace.Instance.StartCoroutine(TransformRoutine(player)); } } @@ -68,10 +68,51 @@ namespace RacesOfValheim { if (IsTransformed && !isTransformingSequence) { - TrentaRace.Instance.StartCoroutine(RevertRoutine(player)); + formRoutine = TrentaRace.Instance.StartCoroutine(RevertRoutine(player)); } } + // The one transform or revert routine in flight, if any, so ResetRace can stop it. + private static Coroutine formRoutine; + + /// + /// Drops the Muspell on the spot for ResetRace. ForceRevert would get there too, from + /// TrentaRace.Update, but a frame and a half-second tween later - after ResetRace has + /// already reopened the menu - and not at all if the reset lands mid-gesture. + /// + /// True if there was a form to drop. + public static bool HardResetForReset(Player player) + { + if (player == null) return false; + + if (formRoutine != null && TrentaRace.Instance != null) + { + TrentaRace.Instance.StopCoroutine(formRoutine); + } + formRoutine = null; + + bool hadForm = IsTransformed || isTransformingSequence || currentTransformation != null; + if (hadForm) + { + if (currentTransformation != null) Object.Destroy(currentTransformation); + currentTransformation = null; + + ZNetView nview = player.GetComponent(); + if (nview != null && nview.IsValid()) nview.GetZDO().Set("RacesOfValheim_MuspellForm", false); + + Transform playerVisual = player.transform.Find("Visual"); + if (playerVisual != null) + { + playerVisual.localScale = Vector3.one; + playerVisual.gameObject.SetActive(true); + } + } + + IsTransformed = false; + isTransformingSequence = false; + return hadForm; + } + /// /// Called by Purple Rain instead of its usual staff_summon trigger whenever Muspell is /// active - The Elder's own root-summon gesture, played on the transformation clone's diff --git a/JotunnModStub/Trol/TrolRace.cs b/JotunnModStub/Trol/TrolRace.cs index e1894ec..e2d26c4 100644 --- a/JotunnModStub/Trol/TrolRace.cs +++ b/JotunnModStub/Trol/TrolRace.cs @@ -206,11 +206,52 @@ namespace RacesOfValheim bool isTrueFormActive = Player.m_localPlayer.GetSEMan().HaveStatusEffect("SE_TrolTrueForm".GetStableHashCode()); if (wasTrueFormActive && !isTrueFormActive && !isTransforming) { - StartCoroutine(RevertTrueFormRoutine()); + RunFormRoutine(RevertTrueFormRoutine()); } wasTrueFormActive = isTrueFormActive; } + // The one grow or shrink routine in flight, if any, so ResetRace can stop it. + private Coroutine formRoutine; + + private void RunFormRoutine(System.Collections.IEnumerator routine) + { + formRoutine = StartCoroutine(routine); + } + + /// + /// Drops True Form on the spot for ResetRace. Update returns on its first line for anyone + /// who is not a Trol, so its auto-revert never saw the form end. wasTrueFormActive is the + /// part that would bite later: left true, the next time this player picks Trol the first + /// Update reads it as a form that just ran out and hands out a True Form cooldown. + /// + /// True if there was a form to drop. + public bool HardResetForReset() + { + Player player = Player.m_localPlayer; + if (player == null) return false; + + if (formRoutine != null) + { + StopCoroutine(formRoutine); + formRoutine = null; + } + + int trueFormHash = "SE_TrolTrueForm".GetStableHashCode(); + bool hadForm = isTransforming || wasTrueFormActive || player.GetSEMan().HaveStatusEffect(trueFormHash); + if (hadForm) + { + player.GetSEMan().RemoveStatusEffect(trueFormHash, true); + + ZNetView nview = player.GetComponent(); + if (nview != null && nview.IsValid()) nview.GetZDO().Set(Trol_PlayerUpdate_Patch.TrueFormKey, false); + } + + isTransforming = false; + wasTrueFormActive = false; + return hadForm; + } + private void TryTrueForm() { if (BossProgressionManager.GetCurrentProgressionState() < 5) return; // Requires Moder @@ -239,7 +280,7 @@ namespace RacesOfValheim if (!isTransforming) { - StartCoroutine(TransformToTrueFormRoutine()); + RunFormRoutine(TransformToTrueFormRoutine()); } }