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

225 lines
6.9 KiB
C#
Raw Normal View History

2021-01-18 12:33:07 +00:00
using OWML.Common;
using QSB.Events;
2020-12-22 21:39:53 +00:00
using QSB.Patches;
2021-05-19 21:43:29 +01:00
using QSB.Player;
2021-01-12 19:24:07 +00:00
using QSB.Utility;
2021-05-19 21:43:29 +01: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
{
2021-01-18 12:33:07 +00:00
public class ServerQuantumPatches : QSBPatch
2020-12-22 21:39:53 +00:00
{
public override QSBPatchTypes Type => QSBPatchTypes.OnServerClientConnect;
2021-02-18 10:34:35 +00:00
public override void DoPatches()
2021-06-18 21:54:32 +01:00
=> Prefix(nameof(QuantumMoon_ChangeQuantumState));
2021-01-02 12:54:41 +00:00
2021-06-18 21:54:32 +01:00
public static bool QuantumMoon_ChangeQuantumState(
2021-01-02 12:54:41 +00:00
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
)
{
if (!QSBCore.WorldObjectsReady)
{
return false;
}
2021-05-19 21:43:29 +01:00
var isVisibleOutput = QuantumManager.IsVisibleUsingCameraFrustum((ShapeVisibilityTracker)____visibilityTracker, skipInstantVisibilityCheck);
//var moonVisible = isVisibleOutput.First;
var moonVisiblePlayers = isVisibleOutput.Second;
var inMoonPlayers = QSBPlayerManager.PlayerList.Where(x => x.IsInMoon);
if (inMoonPlayers == null)
{
DebugLog.ToConsole($"Warning - inMoonPlayers is null.", MessageType.Warning);
return false;
}
2021-05-19 21:43:29 +01:00
var inShrinePlayers = QSBPlayerManager.PlayerList.Where(x => x.IsInShrine);
if (inShrinePlayers == null)
{
DebugLog.ToConsole($"Warning - inShrinePlayers is null.", MessageType.Warning);
return false;
}
2021-05-19 21:43:29 +01:00
//var outMoonPlayers = QSBPlayerManager.PlayerList.Where(x => !x.IsInMoon);
2021-05-19 21:43:29 +01:00
var outShrinePlayers = QSBPlayerManager.PlayerList.Where(x => !x.IsInShrine);
if (outShrinePlayers == null)
{
DebugLog.ToConsole($"Warning - outShrinePlayers is null.", MessageType.Warning);
return false;
}
if (QuantumManager.Shrine == null)
{
DebugLog.ToConsole($"Warning - QuantumManager.Shrine is null.", MessageType.Warning);
return false;
}
2021-06-18 22:38:32 +01:00
2021-05-19 21:43:29 +01:00
var shrineLit = QuantumManager.Shrine.IsPlayerInDarkness();
// If any of the players in the moon are not in the shrine
if (inMoonPlayers.Any(x => !x.IsInShrine))
{
__result = false;
return false;
}
// If any of the players outside the shrine can see the moon
if (outShrinePlayers.Any(moonVisiblePlayers.Contains))
2021-01-05 15:56:14 +00:00
{
2021-01-18 12:33:07 +00:00
__result = false;
return false;
2021-01-05 15:56:14 +00:00
}
2021-05-19 21:43:29 +01:00
// If there are players in the shrine and the shrine is not lit
2021-06-19 11:26:05 +01:00
if (inShrinePlayers.Count() != 0 && !shrineLit)
2021-05-19 21:43:29 +01:00
{
__result = false;
return false;
}
2021-01-02 12:54:41 +00:00
var flag = false;
if (____isPlayerInside && ____hasSunCollapsed)
{
__result = false;
return false;
}
2021-06-18 22:38:32 +01:00
2021-01-02 12:54:41 +00:00
if (Time.time - ____playerWarpTime < 1f)
{
__result = false;
return false;
}
2021-06-18 22:38:32 +01:00
2021-01-02 12:54:41 +00:00
if (____stateIndex == 5 && ____isPlayerInside && !__instance.IsPlayerEntangled())
{
__result = false;
return false;
}
2021-06-18 22:38:32 +01:00
2021-01-02 12:54:41 +00:00
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;
}
}
2021-06-18 22:38:32 +01:00
2021-01-02 12:54:41 +00:00
if (orbitIndex == -1)
{
DebugLog.ToConsole($"Error - QM failed to find orbit for state {stateIndex}", MessageType.Error);
2021-01-02 12:54:41 +00:00
}
2021-06-18 22:38:32 +01:00
2021-01-02 12:54:41 +00:00
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();
}
2021-06-18 22:38:32 +01:00
2021-01-02 12:54:41 +00:00
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-06-18 22:38:32 +01:00
2021-05-19 21:43:29 +01:00
if (__instance.IsPlayerEntangled() || !QuantumManager.IsVisibleUsingCameraFrustum((ShapeVisibilityTracker)____visibilityTracker, skipInstantVisibilityCheck).First)
2021-01-02 12:54:41 +00:00
{
____moonBody.transform.position = position;
if (!Physics.autoSyncTransforms)
{
Physics.SyncTransforms();
}
2021-06-18 22:38:32 +01:00
2021-01-02 12:54:41 +00:00
____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-06-18 22:38:32 +01:00
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-06-18 22:38:32 +01:00
QSBEventManager.FireEvent(EventNames.QSBMoonStateChange, stateIndex, onUnitSphere, orbitAngle);
2021-01-02 12:54:41 +00:00
break;
}
2021-06-18 22:38:32 +01:00
2021-01-02 12:54:41 +00:00
____visibilityTracker.transform.localPosition = Vector3.zero;
}
else
{
DebugLog.ToConsole("Warning - Quantum moon orbit position occupied! Aborting collapse.", MessageType.Warning);
2021-01-02 12:54:41 +00:00
}
}
2021-06-18 22:38:32 +01:00
2021-01-02 12:54:41 +00:00
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);
}
2021-06-18 22:38:32 +01:00
2021-01-02 12:54:41 +00:00
____referenceFrameVolume.gameObject.SetActive(____stateIndex != 5);
____moonBody.SetIsTargetable(____stateIndex != 5);
for (var l = 0; l < ____deactivateAtEye.Length; l++)
{
____deactivateAtEye[l].SetActive(____stateIndex != 5);
}
2021-06-18 22:38:32 +01:00
2021-02-14 20:39:40 +00:00
GlobalMessenger<OWRigidbody>.FireEvent("QuantumMoonChangeState", ____moonBody);
2021-01-02 12:54:41 +00:00
__result = true;
return false;
}
2021-06-18 22:38:32 +01:00
2021-01-02 12:54:41 +00:00
__result = false;
return false;
}
2020-12-22 21:39:53 +00:00
}
}