diff --git a/ml_prm/Main.cs b/ml_prm/Main.cs index 1734a54..06479de 100644 --- a/ml_prm/Main.cs +++ b/ml_prm/Main.cs @@ -1,4 +1,5 @@ -using ABI_RC.Core.InteractionSystem; +using ABI_RC.Core; +using ABI_RC.Core.InteractionSystem; using ABI_RC.Core.Player; using ABI_RC.Systems.IK.SubSystems; using System; @@ -39,6 +40,11 @@ public override void OnInitializeMelon() new HarmonyLib.HarmonyMethod(typeof(PlayerRagdollMod).GetMethod(nameof(OnStartCalibration_Prefix), BindingFlags.Static | BindingFlags.NonPublic)), null ); + HarmonyInstance.Patch( + typeof(RootLogic).GetMethod(nameof(RootLogic.SpawnOnWorldInstance)), + new HarmonyLib.HarmonyMethod(typeof(PlayerRagdollMod).GetMethod(nameof(OnWorldSpawn_Prefix), BindingFlags.Static | BindingFlags.NonPublic)), + null + ); MelonLoader.MelonCoroutines.Start(WaitForLocalPlayer()); } @@ -115,5 +121,19 @@ void OnStartCalibration() } } + static void OnWorldSpawn_Prefix() => ms_instance?.OnWorldSpawn(); + void OnWorldSpawn() + { + try + { + if(m_localController != null) + m_localController.OnWorldSpawn(); + } + catch(Exception e) + { + MelonLoader.MelonLogger.Error(e); + } + } + } } diff --git a/ml_prm/README.md b/ml_prm/README.md index a8b53b8..3bb96a7 100644 --- a/ml_prm/README.md +++ b/ml_prm/README.md @@ -17,8 +17,9 @@ Optional mod's settings with [BTKUILib](https://github.com/BTK-Development/BTKUI * **Velocity multiplier:** velocity force multiplier based on player's movement direction; `2.0` by default. * **Movement drag:** movement resistance; `1.0` by default. * **Angular movement drag:** angular movement resistance; `0.5` by default. +* **Reset settings:** resets mod settings to default. # Notes * Incompatible with `Follow hips on IK override` option in AvatarMotionTweaker. -* Even if locally ragdoll state is activated in the middle of playing emote, remote players still see whole emote animation. * Not suggested to activate fly mode with enabled ragdoll state. +* Can't be activated in worlds that don't allow flying and spawnables. diff --git a/ml_prm/RagdollController.cs b/ml_prm/RagdollController.cs index 86fbba2..6c7d2ab 100644 --- a/ml_prm/RagdollController.cs +++ b/ml_prm/RagdollController.cs @@ -11,6 +11,8 @@ namespace ml_prm { class RagdollController : MonoBehaviour { + static readonly Vector4 ms_pointVector = new Vector4(0f, 0f, 0f, 1f); + VRIK m_vrIK = null; float m_vrIkWeight = 1f; @@ -196,6 +198,12 @@ internal void OnStartCalibration() SwitchRagdoll(); } + internal void OnWorldSpawn() + { + if(m_enabled && m_avatarReady) + SwitchRagdoll(true); + } + // IK updates void OnIKPreUpdate() { @@ -238,13 +246,15 @@ void OnGravityChange(bool p_state) } // Arbitrary - public void SwitchRagdoll() + void SwitchRagdoll() => SwitchRagdoll(false); + public void SwitchRagdoll(bool p_force = false) { - if(m_avatarReady && (MovementSystem.Instance.lastSeat == null) && !BodySystem.isCalibrating) + if(m_avatarReady && (MovementSystem.Instance.lastSeat == null) && !BodySystem.isCalibrating && (Utils.IsWorldSafe() || p_force)) { m_enabled = !m_enabled; MovementSystem.Instance.SetImmobilized(m_enabled); + PlayerSetup.Instance.animatorManager.SetAnimatorParameterTrigger("CancelEmote"); if(m_enabled) { @@ -268,8 +278,21 @@ public void SwitchRagdoll() if(!Settings.RestorePosition && (m_puppetReferences.hips != null)) { - Vector3 l_pos = m_puppetReferences.hips.position; - PlayerSetup.Instance.transform.position = l_pos; + if(Utils.IsInVR()) + { + Matrix4x4 l_playerMatrix = PlayerSetup.Instance.transform.GetMatrix(); + Matrix4x4 l_avatarMatrix = PlayerSetup.Instance._avatar.transform.GetMatrix(); + Matrix4x4 l_avatarToPlayer = l_avatarMatrix.inverse * l_playerMatrix; + + Vector3 l_pos = m_puppetReferences.hips.position; + Vector3 l_offset = l_avatarToPlayer * ms_pointVector; + PlayerSetup.Instance.transform.position = (l_pos + l_offset); + } + else + { + Vector3 l_pos = m_puppetReferences.hips.position; + PlayerSetup.Instance.transform.position = l_pos; + } } } diff --git a/ml_prm/Settings.cs b/ml_prm/Settings.cs index 438d6c9..d538eb3 100644 --- a/ml_prm/Settings.cs +++ b/ml_prm/Settings.cs @@ -16,6 +16,16 @@ public enum ModSetting Gravity } + enum UiElementIndex + { + Hotkey = 0, + RestorePosition, + Gravity, + VelocityMultiplier, + MovementDrag, + AngularDrag + } + public static bool Hotkey { get; private set; } = true; public static float VelocityMultiplier { get; private set; } = 2f; public static bool RestorePosition { get; private set; } = false; @@ -34,6 +44,8 @@ public enum ModSetting static MelonLoader.MelonPreferences_Category ms_category = null; static List ms_entries = null; + static List ms_uiElements = new List(); + internal static void Init() { ms_category = MelonLoader.MelonPreferences.CreateCategory("PRM"); @@ -71,42 +83,87 @@ static void CreateBtkUi() { SwitchChange?.Invoke(); }; - l_categoryMod.AddToggle("Use hotkey", "Switch ragdoll mode with 'R' key", Hotkey).OnValueUpdated += (state) => + + ms_uiElements.Add(l_categoryMod.AddToggle("Use hotkey", "Switch ragdoll mode with 'R' key", Hotkey)); + (ms_uiElements[(int)UiElementIndex.Hotkey] as BTKUILib.UIObjects.Components.ToggleButton).OnValueUpdated += (state) => { Hotkey = state; ms_entries[(int)ModSetting.Hotkey].BoxedValue = state; - HotkeyChange?.Invoke(Hotkey); + HotkeyChange?.Invoke(state); }; - l_categoryMod.AddToggle("Restore position", "Bring avatar back where ragdoll state was activated", RestorePosition).OnValueUpdated += (state) => + + ms_uiElements.Add(l_categoryMod.AddToggle("Restore position", "Bring avatar back where ragdoll state was activated", RestorePosition)); + (ms_uiElements[(int)UiElementIndex.RestorePosition] as BTKUILib.UIObjects.Components.ToggleButton).OnValueUpdated += (state) => { RestorePosition = state; ms_entries[(int)ModSetting.RestorePosition].BoxedValue = state; - RestorePositionChange?.Invoke(RestorePosition); + RestorePositionChange?.Invoke(state); }; - l_categoryMod.AddToggle("Use gravity", "Apply gravity to ragdoll", Gravity).OnValueUpdated += (state) => + + ms_uiElements.Add(l_categoryMod.AddToggle("Use gravity", "Apply gravity to ragdoll", Gravity)); + (ms_uiElements[(int)UiElementIndex.Gravity] as BTKUILib.UIObjects.Components.ToggleButton).OnValueUpdated += (state) => { Gravity = state; ms_entries[(int)ModSetting.Gravity].BoxedValue = state; - GravityChange?.Invoke(Gravity); + GravityChange?.Invoke(state); }; - l_page.AddSlider("Velocity multiplier", "Velocity multiplier upon entering ragdoll state", VelocityMultiplier, 1f, 50f).OnValueUpdated += (value) => + + ms_uiElements.Add(l_page.AddSlider("Velocity multiplier", "Velocity multiplier upon entering ragdoll state", VelocityMultiplier, 1f, 50f)); + (ms_uiElements[(int)UiElementIndex.VelocityMultiplier] as BTKUILib.UIObjects.Components.SliderFloat).OnValueUpdated += (value) => { VelocityMultiplier = value; ms_entries[(int)ModSetting.VelocityMultiplier].BoxedValue = value; - VelocityMultiplierChange?.Invoke(VelocityMultiplier); + VelocityMultiplierChange?.Invoke(value); }; - l_page.AddSlider("Movement drag", "Movement resistance", MovementDrag, 0f, 100f).OnValueUpdated += (value) => + + ms_uiElements.Add(l_page.AddSlider("Movement drag", "Movement resistance", MovementDrag, 0f, 100f)); + (ms_uiElements[(int)UiElementIndex.MovementDrag] as BTKUILib.UIObjects.Components.SliderFloat).OnValueUpdated += (value) => { MovementDrag = value; ms_entries[(int)ModSetting.MovementDrag].BoxedValue = value; - MovementDragChange?.Invoke(MovementDrag); + MovementDragChange?.Invoke(value); }; - l_page.AddSlider("Angular movement drag", "Rotation movement resistance", AngularDrag, 0.5f, 50f).OnValueUpdated += (value) => + + ms_uiElements.Add(l_page.AddSlider("Angular movement drag", "Rotation movement resistance", AngularDrag, 0.5f, 50f)); + (ms_uiElements[(int)UiElementIndex.AngularDrag] as BTKUILib.UIObjects.Components.SliderFloat).OnValueUpdated += (value) => { AngularDrag = value; ms_entries[(int)ModSetting.AngularDrag].BoxedValue = value; AngularDragChange?.Invoke(AngularDrag); }; + + l_categoryMod.AddButton("Reset settings", "", "Reset mod settings to default").OnPress += () => + { + Hotkey = true; + (ms_uiElements[(int)UiElementIndex.Hotkey] as BTKUILib.UIObjects.Components.ToggleButton).ToggleValue = true; + ms_entries[(int)ModSetting.Hotkey].BoxedValue = true; + HotkeyChange?.Invoke(true); + + RestorePosition = false; + ms_entries[(int)ModSetting.RestorePosition].BoxedValue = false; + (ms_uiElements[(int)UiElementIndex.RestorePosition] as BTKUILib.UIObjects.Components.ToggleButton).ToggleValue = false; + RestorePositionChange?.Invoke(false); + + Gravity = true; + ms_entries[(int)ModSetting.Gravity].BoxedValue = true; + (ms_uiElements[(int)UiElementIndex.Gravity] as BTKUILib.UIObjects.Components.ToggleButton).ToggleValue = true; + GravityChange?.Invoke(true); + + VelocityMultiplier = 2f; + ms_entries[(int)ModSetting.VelocityMultiplier].BoxedValue = 2f; + (ms_uiElements[(int)UiElementIndex.VelocityMultiplier] as BTKUILib.UIObjects.Components.SliderFloat).SetSliderValue(2f); + VelocityMultiplierChange?.Invoke(2f); + + MovementDrag = 1f; + ms_entries[(int)ModSetting.MovementDrag].BoxedValue = 1f; + (ms_uiElements[(int)UiElementIndex.MovementDrag] as BTKUILib.UIObjects.Components.SliderFloat).SetSliderValue(1f); + MovementDragChange?.Invoke(1f); + + AngularDrag = 0.5f; + ms_entries[(int)ModSetting.MovementDrag].BoxedValue = 0.5f; + (ms_uiElements[(int)UiElementIndex.AngularDrag] as BTKUILib.UIObjects.Components.SliderFloat).SetSliderValue(0.5f); + AngularDragChange?.Invoke(0.5f); + }; } } } diff --git a/ml_prm/Utils.cs b/ml_prm/Utils.cs index ccc657f..7b8d73d 100644 --- a/ml_prm/Utils.cs +++ b/ml_prm/Utils.cs @@ -1,13 +1,22 @@ -using UnityEngine; +using ABI.CCK.Components; +using UnityEngine; namespace ml_prm { static class Utils { + public static bool IsInVR() => ((ABI_RC.Core.Savior.CheckVR.Instance != null) && ABI_RC.Core.Savior.CheckVR.Instance.hasVrDeviceLoaded); + public static bool IsWorldSafe() => ((CVRWorld.Instance != null) && CVRWorld.Instance.allowFlying && CVRWorld.Instance.allowSpawnables); + public static void CopyGlobal(this Transform p_source, Transform p_target) { p_target.position = p_source.position; p_target.rotation = p_source.rotation; } + + public static Matrix4x4 GetMatrix(this Transform p_transform, bool p_pos = true, bool p_rot = true, bool p_scl = false) + { + return Matrix4x4.TRS(p_pos ? p_transform.position : Vector3.zero, p_rot ? p_transform.rotation : Quaternion.identity, p_scl ? p_transform.localScale : Vector3.one); + } } }