From 1101fc0d60fd78047d8b2845d9ce7a54747291d3 Mon Sep 17 00:00:00 2001 From: Mister_Nebula <41904486+misternebula@users.noreply.github.com> Date: Sat, 2 Jan 2021 12:54:41 +0000 Subject: [PATCH] add quantum moon patch --- .../ServerQuantumStateChangePatches.cs | 135 +++++++++++++++++- 1 file changed, 134 insertions(+), 1 deletion(-) diff --git a/QSB/QuantumSync/Patches/ServerQuantumStateChangePatches.cs b/QSB/QuantumSync/Patches/ServerQuantumStateChangePatches.cs index c895cd9c..d6b4185f 100644 --- a/QSB/QuantumSync/Patches/ServerQuantumStateChangePatches.cs +++ b/QSB/QuantumSync/Patches/ServerQuantumStateChangePatches.cs @@ -1,10 +1,13 @@ -using QSB.Events; +using OWML.Utils; +using QSB.Events; using QSB.Patches; using QSB.QuantumSync.WorldObjects; +using QSB.Utility; using QSB.WorldSync; using System; using System.Collections.Generic; using System.Linq; +using System.Reflection; using UnityEngine; namespace QSB.QuantumSync.Patches @@ -18,6 +21,7 @@ namespace QSB.QuantumSync.Patches QSBCore.Helper.HarmonyHelper.AddPostfix("MoveToSocket", typeof(ServerQuantumStateChangePatches), nameof(Socketed_MoveToSocket)); QSBCore.Helper.HarmonyHelper.AddPostfix("SetVisible", typeof(ServerQuantumStateChangePatches), nameof(QuantumState_SetVisible)); QSBCore.Helper.HarmonyHelper.AddPrefix("ChangeQuantumState", typeof(ServerQuantumStateChangePatches), nameof(Shuffle_ChangeQuantumState)); + QSBCore.Helper.HarmonyHelper.AddPrefix("ChangeQuantumState", typeof(ServerQuantumStateChangePatches), nameof(Moon_ChangeQuantumState)); } public static void Socketed_MoveToSocket(SocketedQuantumObject __instance, QuantumSocket socket) @@ -75,5 +79,134 @@ namespace QSB.QuantumSync.Patches __result = true; return false; } + + public static bool Moon_ChangeQuantumState( + QuantumMoon __instance, + ref bool __result, + bool skipInstantVisibilityCheck, + bool ____isPlayerInside, + bool ____hasSunCollapsed, + float ____playerWarpTime, + ref int ____stateIndex, + ref int ____collapseToIndex, + QuantumOrbit[] ____orbits, + float ____sphereCheckRadius, + VisibilityTracker ____visibilityTracker, + OWRigidbody ____moonBody, + ConstantForceDetector ____constantForceDetector, + ref bool ____useInitialMotion, + ref int ____lastStateIndex, + ref int[] ____stateSkipCounts, + AudioSignal ____quantumSignal, + ReferenceFrameVolume ____referenceFrameVolume, + GameObject[] ____deactivateAtEye + ) + { + var flag = false; + if (____isPlayerInside && ____hasSunCollapsed) + { + __result = false; + return false; + } + if (Time.time - ____playerWarpTime < 1f) + { + __result = false; + return false; + } + if (____stateIndex == 5 && ____isPlayerInside && !__instance.IsPlayerEntangled()) + { + __result = false; + return false; + } + for (var i = 0; i < 10; i++) + { + var stateIndex = (____collapseToIndex == -1) ? (int)__instance.GetType().GetMethod("GetRandomStateIndex", BindingFlags.NonPublic | BindingFlags.Instance).Invoke(__instance, null) : ____collapseToIndex; + var orbitIndex = -1; + for (var j = 0; j < ____orbits.Length; j++) + { + if (____orbits[j].GetStateIndex() == stateIndex) + { + orbitIndex = j; + break; + } + } + if (orbitIndex == -1) + { + Debug.LogError("QUANTUM MOON FAILED TO FIND ORBIT FOR STATE " + stateIndex); + } + var orbitRadius = (orbitIndex == -1) ? 10000f : ____orbits[orbitIndex].GetOrbitRadius(); + var owRigidbody = (orbitIndex == -1) ? Locator.GetAstroObject(AstroObject.Name.Sun).GetOWRigidbody() : ____orbits[orbitIndex].GetAttachedOWRigidbody(); + var onUnitSphere = UnityEngine.Random.onUnitSphere; + if (stateIndex == 5) + { + onUnitSphere.y = 0f; + onUnitSphere.Normalize(); + } + var position = (onUnitSphere * orbitRadius) + owRigidbody.GetWorldCenterOfMass(); + if (!Physics.CheckSphere(position, ____sphereCheckRadius, OWLayerMask.physicalMask) || ____collapseToIndex != -1) + { + ____visibilityTracker.transform.position = position; + if (!Physics.autoSyncTransforms) + { + Physics.SyncTransforms(); + } + if (skipInstantVisibilityCheck || __instance.IsPlayerEntangled() || !(bool)__instance.GetType().GetMethod("CheckVisibilityInstantly", BindingFlags.NonPublic | BindingFlags.Instance).Invoke(__instance, null)) + { + ____moonBody.transform.position = position; + if (!Physics.autoSyncTransforms) + { + Physics.SyncTransforms(); + } + ____visibilityTracker.transform.localPosition = Vector3.zero; + ____constantForceDetector.AddConstantVolume(owRigidbody.GetAttachedGravityVolume(), true, true); + var velocity = owRigidbody.GetVelocity(); + if (____useInitialMotion) + { + var initialMotion = owRigidbody.GetComponent(); + velocity = (initialMotion == null) ? Vector3.zero : initialMotion.GetInitVelocity(); + ____useInitialMotion = false; + } + ____moonBody.SetVelocity(OWPhysics.CalculateOrbitVelocity(owRigidbody, ____moonBody, UnityEngine.Random.Range(0, 360)) + velocity); + ____lastStateIndex = ____stateIndex; + ____stateIndex = stateIndex; + ____collapseToIndex = -1; + flag = true; + for (var k = 0; k < ____stateSkipCounts.Length; k++) + { + ____stateSkipCounts[k] = (k != ____stateIndex) ? (____stateSkipCounts[k] + 1) : 0; + } + break; + } + ____visibilityTracker.transform.localPosition = Vector3.zero; + } + else + { + Debug.LogError("Quantum moon orbit position occupied! Aborting collapse."); + } + } + if (flag) + { + if (____isPlayerInside) + { + __instance.GetType().GetMethod("SetSurfaceState", BindingFlags.NonPublic | BindingFlags.Instance).Invoke(__instance, new object[] { ____stateIndex }); + } + else + { + __instance.GetType().GetMethod("SetSurfaceState", BindingFlags.NonPublic | BindingFlags.Instance).Invoke(__instance, new object[] { ____stateIndex }); + ____quantumSignal.SetSignalActivation(____stateIndex != 5, 2f); + } + ____referenceFrameVolume.gameObject.SetActive(____stateIndex != 5); + ____moonBody.SetIsTargetable(____stateIndex != 5); + for (var l = 0; l < ____deactivateAtEye.Length; l++) + { + ____deactivateAtEye[l].SetActive(____stateIndex != 5); + } + GlobalMessenger.FireEvent("QuantumMoonChangeState", ____moonBody); + __result = true; + return false; + } + __result = false; + return false; + } } } \ No newline at end of file