Skip to content

Commit

Permalink
Reset button, animation cancel, world check, VR offset fix
Browse files Browse the repository at this point in the history
  • Loading branch information
SDraw committed Apr 8, 2023
1 parent 8ffc876 commit 8bee0d2
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 18 deletions.
22 changes: 21 additions & 1 deletion ml_prm/Main.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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());
}
Expand Down Expand Up @@ -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);
}
}

}
}
3 changes: 2 additions & 1 deletion ml_prm/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
31 changes: 27 additions & 4 deletions ml_prm/RagdollController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -196,6 +198,12 @@ internal void OnStartCalibration()
SwitchRagdoll();
}

internal void OnWorldSpawn()
{
if(m_enabled && m_avatarReady)
SwitchRagdoll(true);
}

// IK updates
void OnIKPreUpdate()
{
Expand Down Expand Up @@ -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)
{
Expand All @@ -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;
}
}
}

Expand Down
79 changes: 68 additions & 11 deletions ml_prm/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -34,6 +44,8 @@ public enum ModSetting
static MelonLoader.MelonPreferences_Category ms_category = null;
static List<MelonLoader.MelonPreferences_Entry> ms_entries = null;

static List<object> ms_uiElements = new List<object>();

internal static void Init()
{
ms_category = MelonLoader.MelonPreferences.CreateCategory("PRM");
Expand Down Expand Up @@ -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);
};
}
}
}
11 changes: 10 additions & 1 deletion ml_prm/Utils.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
}

0 comments on commit 8bee0d2

Please sign in to comment.