quantum-space-buddies/QSB/QuantumSync/Patches/ServerQuantumStateChangePatches.cs

233 lines
8.4 KiB
C#
Raw Normal View History

2021-01-03 09:52:34 +00:00
using QSB.Events;
2020-12-22 21:39:53 +00:00
using QSB.Patches;
2021-01-03 12:38:02 +00:00
using QSB.Player;
using QSB.QuantumSync.WorldObjects;
2021-01-03 12:38:02 +00:00
using QSB.Utility;
2020-12-24 10:06:17 +00:00
using QSB.WorldSync;
using System;
2021-01-01 10:17:35 +00:00
using System.Collections.Generic;
2020-12-24 10:06:17 +00:00
using System.Linq;
2021-01-02 12:54:41 +00:00
using System.Reflection;
2020-12-22 21:39:53 +00:00
using UnityEngine;
namespace QSB.QuantumSync.Patches
2020-12-22 21:39:53 +00:00
{
public class ServerQuantumStateChangePatches : QSBPatch
{
public override QSBPatchTypes Type => QSBPatchTypes.OnServerClientConnect;
2020-12-24 10:06:17 +00:00
public override void DoPatches()
{
QSBCore.Helper.HarmonyHelper.AddPostfix<SocketedQuantumObject>("MoveToSocket", typeof(ServerQuantumStateChangePatches), nameof(Socketed_MoveToSocket));
QSBCore.Helper.HarmonyHelper.AddPostfix<QuantumState>("SetVisible", typeof(ServerQuantumStateChangePatches), nameof(QuantumState_SetVisible));
2021-01-01 10:17:35 +00:00
QSBCore.Helper.HarmonyHelper.AddPrefix<QuantumShuffleObject>("ChangeQuantumState", typeof(ServerQuantumStateChangePatches), nameof(Shuffle_ChangeQuantumState));
2021-01-02 12:54:41 +00:00
QSBCore.Helper.HarmonyHelper.AddPrefix<QuantumMoon>("ChangeQuantumState", typeof(ServerQuantumStateChangePatches), nameof(Moon_ChangeQuantumState));
2020-12-24 10:06:17 +00:00
}
2020-12-22 21:39:53 +00:00
public static void Socketed_MoveToSocket(SocketedQuantumObject __instance, QuantumSocket socket)
{
var objId = QuantumManager.Instance.GetId(__instance);
var socketId = QuantumManager.Instance.GetId(socket);
2020-12-24 10:06:17 +00:00
GlobalMessenger<int, int, Quaternion>
2021-01-01 10:17:35 +00:00
.FireEvent(
EventNames.QSBSocketStateChange,
2020-12-24 10:06:17 +00:00
objId,
socketId,
__instance.transform.localRotation);
}
public static void QuantumState_SetVisible(QuantumState __instance, bool visible)
{
if (!visible)
{
return;
}
2020-12-24 10:06:17 +00:00
var allMultiStates = QSBWorldSync.GetWorldObjects<QSBMultiStateQuantumObject>();
var owner = allMultiStates.First(x => x.QuantumStates.Contains(__instance));
GlobalMessenger<int, int>
2021-01-01 10:17:35 +00:00
.FireEvent(
EventNames.QSBMultiStateChange,
2020-12-24 10:06:17 +00:00
QuantumManager.Instance.GetId(owner.AttachedObject),
Array.IndexOf(owner.QuantumStates, __instance));
2020-12-22 21:39:53 +00:00
}
2021-01-01 10:17:35 +00:00
public static bool Shuffle_ChangeQuantumState(
QuantumShuffleObject __instance,
ref List<int> ____indexList,
ref Vector3[] ____localPositions,
ref Transform[] ____shuffledObjects,
ref bool __result)
{
____indexList.Clear();
____indexList = Enumerable.Range(0, ____localPositions.Length).ToList();
for (var i = 0; i < ____indexList.Count; ++i)
{
var random = UnityEngine.Random.Range(i, ____indexList.Count);
var temp = ____indexList[i];
____indexList[i] = ____indexList[random];
____indexList[random] = temp;
}
for (var j = 0; j < ____shuffledObjects.Length; j++)
{
____shuffledObjects[j].localPosition = ____localPositions[____indexList[j]];
}
GlobalMessenger<int, int[]>
.FireEvent(
EventNames.QSBQuantumShuffle,
QuantumManager.Instance.GetId(__instance),
____indexList.ToArray());
__result = true;
return false;
}
2021-01-02 12:54:41 +00:00
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();
}
2021-01-03 12:38:02 +00:00
if (__instance.IsPlayerEntangled() || !IsVisibleUsingCameraFrustum((ShapeVisibilityTracker)____visibilityTracker, skipInstantVisibilityCheck))
2021-01-02 12:54:41 +00:00
{
____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<InitialMotion>();
velocity = (initialMotion == null) ? Vector3.zero : initialMotion.GetInitVelocity();
____useInitialMotion = false;
}
2021-01-03 09:52:34 +00:00
var orbitAngle = UnityEngine.Random.Range(0, 360);
____moonBody.SetVelocity(OWPhysics.CalculateOrbitVelocity(owRigidbody, ____moonBody, orbitAngle) + velocity);
2021-01-02 12:54:41 +00:00
____lastStateIndex = ____stateIndex;
____stateIndex = stateIndex;
____collapseToIndex = -1;
flag = true;
for (var k = 0; k < ____stateSkipCounts.Length; k++)
{
____stateSkipCounts[k] = (k != ____stateIndex) ? (____stateSkipCounts[k] + 1) : 0;
}
2021-01-03 09:52:34 +00:00
GlobalMessenger<int, Vector3, int>.FireEvent(EventNames.QSBMoonStateChange, stateIndex, onUnitSphere, orbitAngle);
2021-01-02 12:54:41 +00:00
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
{
2021-01-03 09:52:34 +00:00
__instance.GetType().GetMethod("SetSurfaceState", BindingFlags.NonPublic | BindingFlags.Instance).Invoke(__instance, new object[] { -1 });
2021-01-02 12:54:41 +00:00
____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<OWRigidbody>.FireEvent("QuantumMoonChangeState", ____moonBody);
__result = true;
return false;
}
__result = false;
return false;
}
2021-01-03 12:38:02 +00:00
private static bool IsVisibleUsingCameraFrustum(ShapeVisibilityTracker tracker, bool skipVisibilityCheck)
{
DebugLog.DebugWrite($"Skip vis check? {skipVisibilityCheck}");
foreach (var camera in QSBPlayerManager.GetPlayerCameras(!skipVisibilityCheck))
{
var visible = (bool)tracker.GetType()
.GetMethod("IsInFrustum", BindingFlags.NonPublic | BindingFlags.Instance)
.Invoke(tracker, new object[] { camera.GetFrustumPlanes() });
DebugLog.DebugWrite($"{camera.name}, Visible : {visible}");
}
var returnValue = tracker.gameObject.activeInHierarchy
&& QSBPlayerManager.GetPlayerCameras(!skipVisibilityCheck)
.Any(x => (bool)tracker.GetType()
.GetMethod("IsInFrustum", BindingFlags.NonPublic | BindingFlags.Instance)
.Invoke(tracker, new object[] { x.GetFrustumPlanes() }));
DebugLog.DebugWrite($"Is Moon Visible? {returnValue}");
return returnValue;
}
2020-12-22 21:39:53 +00:00
}
}