503 lines
15 KiB
C#
Raw Normal View History

using HarmonyLib;
using OWML.Common;
2021-02-21 18:25:25 +00:00
using QSB.Events;
2021-01-18 12:33:07 +00:00
using QSB.Patches;
2021-01-26 23:41:53 +00:00
using QSB.Player;
using QSB.QuantumSync.WorldObjects;
using QSB.Utility;
using QSB.WorldSync;
using System.Collections.Generic;
using System.Linq;
2021-02-16 20:49:10 +00:00
using System.Reflection;
2021-01-18 12:33:07 +00:00
using UnityEngine;
namespace QSB.QuantumSync.Patches
{
[HarmonyPatch]
2021-01-18 12:33:07 +00:00
public class QuantumPatches : QSBPatch
{
public override QSBPatchTypes Type => QSBPatchTypes.OnClientConnect;
[HarmonyPrefix]
[HarmonyPatch(typeof(QuantumObject), nameof(QuantumObject.IsLockedByPlayerContact))]
2021-06-18 21:54:32 +01:00
public static bool QuantumObject_IsLockedByPlayerContact(ref bool __result, QuantumObject __instance)
2021-02-26 14:19:42 +00:00
{
var playersEntangled = QuantumManager.GetEntangledPlayers(__instance);
__result = playersEntangled.Count() != 0 && __instance.IsIlluminated();
return false;
2021-02-18 15:36:11 +00:00
}
[HarmonyPrefix]
[HarmonyPatch(typeof(SocketedQuantumObject), nameof(SocketedQuantumObject.ChangeQuantumState))]
2021-06-18 21:54:32 +01:00
public static bool SocketedQuantumObject_ChangeQuantumState(
2021-02-21 18:25:25 +00:00
SocketedQuantumObject __instance,
ref bool __result,
bool skipInstantVisibilityCheck,
List<QuantumSocket> ____childSockets,
List<QuantumSocket> ____socketList,
ref QuantumSocket ____recentlyObscuredSocket,
QuantumSocket ____occupiedSocket)
{
if (WorldObjectManager.AllObjectsReady)
2021-02-21 18:25:25 +00:00
{
2021-11-01 15:49:00 +00:00
var socketedWorldObject = QSBWorldSync.GetWorldFromUnity<QSBSocketedQuantumObject>(__instance);
if (socketedWorldObject.ControllingPlayer != QSBPlayerManager.LocalPlayerId)
{
return false;
}
2021-02-21 18:25:25 +00:00
}
foreach (var socket in ____childSockets)
{
if (socket.IsOccupied())
{
__result = false;
return false;
}
}
if (____socketList.Count <= 1)
{
DebugLog.ToConsole($"Error - Not enough quantum sockets in list for {__instance.name}!", MessageType.Error);
__result = false;
return false;
}
var list = new List<QuantumSocket>();
foreach (var socket in ____socketList)
{
if (!socket.IsOccupied() && socket.IsActive())
{
list.Add(socket);
}
}
if (list.Count == 0)
{
__result = false;
return false;
}
if (____recentlyObscuredSocket != null)
{
__instance.GetType().GetMethod("MoveToSocket", BindingFlags.NonPublic | BindingFlags.Instance).Invoke(__instance, new object[] { ____recentlyObscuredSocket });
____recentlyObscuredSocket = null;
__result = true;
return false;
}
var occupiedSocket = ____occupiedSocket;
for (var i = 0; i < 20; i++)
{
var index = UnityEngine.Random.Range(0, list.Count);
__instance.GetType().GetMethod("MoveToSocket", BindingFlags.NonPublic | BindingFlags.Instance).Invoke(__instance, new object[] { list[index] });
if (skipInstantVisibilityCheck)
{
__result = true;
return false;
}
2021-06-18 22:38:32 +01:00
2021-02-21 18:25:25 +00:00
bool socketNotSuitable;
var isSocketIlluminated = (bool)__instance.GetType().GetMethod("CheckIllumination", BindingFlags.NonPublic | BindingFlags.Instance).Invoke(__instance, null);
var playersEntangled = QuantumManager.GetEntangledPlayers(__instance);
if (playersEntangled.Count() != 0)
{
// socket not suitable if illuminated
socketNotSuitable = isSocketIlluminated;
}
else
{
var checkVisInstant = (bool)__instance.GetType().GetMethod("CheckVisibilityInstantly", BindingFlags.NonPublic | BindingFlags.Instance).Invoke(__instance, null);
if (isSocketIlluminated)
{
// socket not suitable if object is visible
socketNotSuitable = checkVisInstant;
}
else
{
// socket not suitable if player is inside object
socketNotSuitable = playersEntangled.Any(x => __instance.CheckPointInside(x.CameraBody.transform.position));
}
}
if (!socketNotSuitable)
{
__result = true;
return false;
}
2021-06-18 22:38:32 +01:00
2021-02-21 18:25:25 +00:00
list.RemoveAt(index);
if (list.Count == 0)
{
break;
}
}
2021-06-18 22:38:32 +01:00
2021-02-21 18:25:25 +00:00
__instance.GetType().GetMethod("MoveToSocket", BindingFlags.NonPublic | BindingFlags.Instance).Invoke(__instance, new object[] { occupiedSocket });
__result = false;
return false;
}
2021-01-26 23:41:53 +00:00
[HarmonyPostfix]
[HarmonyPatch(typeof(SocketedQuantumObject), nameof(SocketedQuantumObject.MoveToSocket))]
2021-06-18 21:54:32 +01:00
public static void SocketedQuantumObject_MoveToSocket(SocketedQuantumObject __instance, QuantumSocket socket)
2021-01-26 23:41:53 +00:00
{
if (!WorldObjectManager.AllObjectsReady)
{
return;
}
2021-02-26 17:04:16 +00:00
if (socket == null)
{
DebugLog.ToConsole($"Error - Trying to move {__instance.name} to a null socket!", MessageType.Error);
return;
}
2021-06-18 22:38:32 +01:00
2021-11-01 15:49:00 +00:00
var objectWorldObject = QSBWorldSync.GetWorldFromUnity<QSBSocketedQuantumObject>(__instance);
var socketWorldObject = QSBWorldSync.GetWorldFromUnity<QSBQuantumSocket>(socket);
2021-02-26 21:55:39 +00:00
2021-02-18 10:34:35 +00:00
if (objectWorldObject == null)
2021-01-26 23:41:53 +00:00
{
2021-02-18 10:34:35 +00:00
DebugLog.ToConsole($"Worldobject is null for {__instance.name}!");
2021-01-26 23:41:53 +00:00
return;
}
2021-02-18 10:34:35 +00:00
if (objectWorldObject.ControllingPlayer != QSBPlayerManager.LocalPlayerId)
2021-01-26 23:41:53 +00:00
{
return;
}
QSBEventManager.FireEvent(
2021-01-26 23:41:53 +00:00
EventNames.QSBSocketStateChange,
2021-02-18 10:34:35 +00:00
objectWorldObject.ObjectId,
socketWorldObject.ObjectId,
2021-01-26 23:41:53 +00:00
__instance.transform.localRotation);
}
[HarmonyPrefix]
[HarmonyPatch(typeof(QuantumShuffleObject), nameof(QuantumShuffleObject.ChangeQuantumState))]
2021-06-18 21:54:32 +01:00
public static bool QuantumShuffleObject_ChangeQuantumState(
2021-01-26 23:41:53 +00:00
QuantumShuffleObject __instance,
ref List<int> ____indexList,
ref Vector3[] ____localPositions,
ref Transform[] ____shuffledObjects,
ref bool __result)
{
QSBQuantumShuffleObject shuffleWorldObject = default;
if (WorldObjectManager.AllObjectsReady)
2021-01-26 23:41:53 +00:00
{
2021-11-01 15:49:00 +00:00
shuffleWorldObject = QSBWorldSync.GetWorldFromUnity<QSBQuantumShuffleObject>(__instance);
if (shuffleWorldObject.ControllingPlayer != QSBPlayerManager.LocalPlayerId)
{
return false;
}
2021-01-26 23:41:53 +00:00
}
____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;
}
2021-01-26 23:41:53 +00:00
for (var j = 0; j < ____shuffledObjects.Length; j++)
{
____shuffledObjects[j].localPosition = ____localPositions[____indexList[j]];
}
if (WorldObjectManager.AllObjectsReady)
{
QSBEventManager.FireEvent(
2021-01-26 23:41:53 +00:00
EventNames.QSBQuantumShuffle,
2021-02-18 10:34:35 +00:00
shuffleWorldObject.ObjectId,
2021-01-26 23:41:53 +00:00
____indexList.ToArray());
__result = true;
}
2021-01-26 23:41:53 +00:00
return false;
}
[HarmonyPrefix]
[HarmonyPatch(typeof(MultiStateQuantumObject), nameof(MultiStateQuantumObject.Start))]
2021-06-18 21:54:32 +01:00
public static bool MultiStateQuantumObject_Start(MultiStateQuantumObject __instance, Sector ____sector, bool ____collapseOnStart)
2021-03-18 13:35:11 +00:00
{
if (!WorldObjectManager.AllObjectsReady)
{
return true;
}
2021-11-01 15:49:00 +00:00
var qsbObj = QSBWorldSync.GetWorldFromUnity<QSBMultiStateQuantumObject>(__instance);
2021-03-18 13:35:11 +00:00
if (qsbObj.ControllingPlayer == 0)
{
return true;
}
foreach (var state in qsbObj.QuantumStates)
{
if (!state.IsMeantToBeEnabled)
{
state.SetVisible(false);
}
}
if (____sector == null)
{
__instance.CheckEnabled();
2021-03-18 13:35:11 +00:00
}
2021-06-18 22:38:32 +01:00
2021-03-18 13:35:11 +00:00
if (____collapseOnStart)
{
__instance.Collapse(true);
2021-03-18 13:35:11 +00:00
}
return false;
}
[HarmonyPrefix]
[HarmonyPatch(typeof(MultiStateQuantumObject), nameof(MultiStateQuantumObject.ChangeQuantumState))]
2021-06-18 21:54:32 +01:00
public static bool MultiStateQuantumObject_ChangeQuantumState(MultiStateQuantumObject __instance)
2021-01-26 23:41:53 +00:00
{
if (!WorldObjectManager.AllObjectsReady)
{
return true;
}
2021-11-01 15:49:00 +00:00
var qsbObj = QSBWorldSync.GetWorldFromUnity<QSBMultiStateQuantumObject>(__instance);
2021-03-18 13:35:11 +00:00
if (qsbObj.ControllingPlayer == 0 && qsbObj.CurrentState == -1)
2021-03-17 17:04:57 +00:00
{
return true;
}
2021-06-18 22:38:32 +01:00
2021-03-18 13:35:11 +00:00
var isInControl = qsbObj.ControllingPlayer == QSBPlayerManager.LocalPlayerId;
2021-01-26 23:41:53 +00:00
return isInControl;
}
[HarmonyPostfix]
[HarmonyPatch(typeof(QuantumState), nameof(QuantumState.SetVisible))]
2021-01-26 23:41:53 +00:00
public static void QuantumState_SetVisible(QuantumState __instance, bool visible)
{
if (!WorldObjectManager.AllObjectsReady)
{
return;
}
2021-01-26 23:41:53 +00:00
if (!visible)
{
return;
}
2021-06-18 22:38:32 +01:00
2021-01-26 23:41:53 +00:00
var allMultiStates = QSBWorldSync.GetWorldObjects<QSBMultiStateQuantumObject>();
2021-11-01 15:49:00 +00:00
var stateObject = QSBWorldSync.GetWorldFromUnity<QSBQuantumState>(__instance);
var owner = allMultiStates.FirstOrDefault(x => x.QuantumStates.Contains(stateObject));
if (owner == default)
{
2021-10-24 10:47:25 +01:00
DebugLog.ToConsole($"Error - Could not find QSBMultiStateQuantumObject for state {__instance.name}", MessageType.Error);
return;
}
2021-01-26 23:41:53 +00:00
if (owner.ControllingPlayer != QSBPlayerManager.LocalPlayerId)
{
return;
}
2021-06-18 22:38:32 +01:00
2021-03-18 13:35:11 +00:00
var stateIndex = owner.QuantumStates.IndexOf(stateObject);
QSBEventManager.FireEvent(
2021-01-26 23:41:53 +00:00
EventNames.QSBMultiStateChange,
2021-02-18 10:34:35 +00:00
owner.ObjectId,
2021-03-17 17:04:57 +00:00
stateIndex);
2021-01-26 23:41:53 +00:00
}
[HarmonyPrefix]
[HarmonyPatch(typeof(QuantumShrine), nameof(QuantumShrine.IsPlayerInDarkness))]
2021-06-18 21:54:32 +01:00
public static bool QuantumShrine_IsPlayerInDarkness(ref bool __result, Light[] ____lamps, float ____fadeFraction, bool ____isProbeInside, NomaiGateway ____gate)
2021-01-26 23:41:53 +00:00
{
foreach (var lamp in ____lamps)
{
if (lamp.intensity > 0f)
{
__result = false;
return false;
}
}
var playersInMoon = QSBPlayerManager.PlayerList.Where(x => x.IsInMoon);
2021-03-25 20:56:26 +00:00
if (playersInMoon.Any(player => !player.IsInShrine))
2021-01-26 23:41:53 +00:00
{
__result = false;
return false;
}
2021-03-25 20:56:26 +00:00
if (playersInMoon.Any(player => player.FlashLight != null && player.FlashLight.FlashlightOn))
{
__result = false;
return false;
}
if (playersInMoon.Count() == 0)
{
__result = false;
return false;
}
2021-03-25 22:01:10 +00:00
if (QSBPlayerManager.LocalPlayer != null
&& QSBPlayerManager.LocalPlayer.IsInShrine
2021-03-25 20:56:26 +00:00
&& PlayerState.IsFlashlightOn())
{
__result = false;
return false;
}
2021-08-22 16:40:50 +01:00
// BUG : make this *really* check for all players - check other probes and other jetpacks!
2021-01-26 23:41:53 +00:00
__result = ____gate.GetOpenFraction() == 0f
&& !____isProbeInside
&& Locator.GetThrusterLightTracker().GetLightRange() <= 0f;
return false;
}
[HarmonyPrefix]
[HarmonyPatch(typeof(QuantumShrine), nameof(QuantumShrine.ChangeQuantumState))]
2021-06-18 21:54:32 +01:00
public static bool QuantumShrine_ChangeQuantumState(QuantumShrine __instance)
2021-01-26 23:41:53 +00:00
{
2021-11-01 15:49:00 +00:00
var shrineWorldObject = QSBWorldSync.GetWorldFromUnity<QSBSocketedQuantumObject>(__instance);
2021-02-18 10:34:35 +00:00
var isInControl = shrineWorldObject.ControllingPlayer == QSBPlayerManager.LocalPlayerId;
2021-01-26 23:41:53 +00:00
return isInControl;
}
[HarmonyPrefix]
[HarmonyPatch(typeof(QuantumShrine), nameof(QuantumShrine.OnEntry))]
2021-06-18 21:54:32 +01:00
public static bool QuantumShrine_OnEntry(
2021-01-18 12:33:07 +00:00
GameObject hitObj,
ref bool ____isPlayerInside,
ref bool ____fading,
OWLightController ____exteriorLightController,
ref bool ____isProbeInside)
{
if (hitObj.CompareTag("PlayerDetector"))
{
____isPlayerInside = true;
____fading = true;
____exteriorLightController.FadeTo(0f, 1f);
2021-02-10 19:59:28 +00:00
QSBEventManager.FireEvent(EventNames.QSBEnterShrine);
2021-01-18 12:33:07 +00:00
}
else if (hitObj.CompareTag("ProbeDetector"))
{
____isProbeInside = true;
}
2021-06-18 22:38:32 +01:00
2021-01-18 12:33:07 +00:00
return false;
}
[HarmonyPrefix]
[HarmonyPatch(typeof(QuantumShrine), nameof(QuantumShrine.OnExit))]
2021-06-18 21:54:32 +01:00
public static bool QuantumShrine_OnExit(
2021-01-18 12:33:07 +00:00
GameObject hitObj,
ref bool ____isPlayerInside,
ref bool ____fading,
OWLightController ____exteriorLightController,
ref bool ____isProbeInside)
{
if (hitObj.CompareTag("PlayerDetector"))
{
____isPlayerInside = false;
____fading = true;
____exteriorLightController.FadeTo(1f, 1f);
2021-02-10 19:59:28 +00:00
QSBEventManager.FireEvent(EventNames.QSBExitShrine);
2021-01-18 12:33:07 +00:00
}
else if (hitObj.CompareTag("ProbeDetector"))
{
____isProbeInside = false;
}
2021-06-18 22:38:32 +01:00
2021-01-18 12:33:07 +00:00
return false;
}
2021-02-16 20:49:10 +00:00
[HarmonyPrefix]
[HarmonyPatch(typeof(QuantumMoon), nameof(QuantumMoon.CheckPlayerFogProximity))]
2021-06-18 21:54:32 +01:00
public static bool QuantumMoon_CheckPlayerFogProximity(
2021-02-16 20:49:10 +00:00
QuantumMoon __instance,
int ____stateIndex,
float ____eyeStateFogOffset,
ref bool ____isPlayerInside,
float ____fogRadius,
float ____fogThickness,
float ____fogRolloffDistance,
string ____revealFactID,
OWRigidbody ____moonBody,
bool ____hasSunCollapsed,
Transform ____vortexReturnPivot,
OWAudioSource ____vortexAudio,
ref int ____collapseToIndex,
VisibilityTracker ____visibilityTracker,
QuantumFogEffectBubbleController ____playerFogBubble,
QuantumFogEffectBubbleController ____shipLandingCamFogBubble)
{
var playerDistance = Vector3.Distance(__instance.transform.position, Locator.GetPlayerCamera().transform.position);
var fogOffset = (____stateIndex != 5) ? 0f : ____eyeStateFogOffset;
var distanceFromFog = playerDistance - (____fogRadius + fogOffset);
var fogAlpha = 0f;
if (!____isPlayerInside)
{
fogAlpha = Mathf.InverseLerp(____fogThickness + ____fogRolloffDistance, ____fogThickness, distanceFromFog);
if (distanceFromFog < 0f)
{
2021-10-12 15:32:47 +01:00
if ((bool)__instance.GetType().GetMethod("IsLockedByProbeSnapshot", BindingFlags.NonPublic | BindingFlags.Instance).Invoke(__instance, null) || QuantumManager.IsVisibleUsingCameraFrustum((ShapeVisibilityTracker)____visibilityTracker, true).Item1)
2021-02-16 20:49:10 +00:00
{
____isPlayerInside = true;
__instance.GetType().GetMethod("SetSurfaceState", BindingFlags.NonPublic | BindingFlags.Instance).Invoke(__instance, new object[] { ____stateIndex });
Locator.GetShipLogManager().RevealFact(____revealFactID, true, true);
QSBEventManager.FireEvent("PlayerEnterQuantumMoon");
}
else
{
__instance.GetType().GetMethod("Collapse", BindingFlags.NonPublic | BindingFlags.Instance).Invoke(__instance, new object[] { true });
}
}
}
else if (____isPlayerInside)
{
fogAlpha = Mathf.InverseLerp(-____fogThickness - ____fogRolloffDistance, -____fogThickness, distanceFromFog);
if (distanceFromFog >= 0f)
{
if (____stateIndex != 5)
{
____isPlayerInside = false;
2021-10-12 15:32:47 +01:00
if (!(bool)__instance.GetType().GetMethod("IsLockedByProbeSnapshot", BindingFlags.NonPublic | BindingFlags.Instance).Invoke(__instance, null) && !QuantumManager.IsVisibleUsingCameraFrustum((ShapeVisibilityTracker)____visibilityTracker, true).Item1)
2021-02-16 20:49:10 +00:00
{
__instance.GetType().GetMethod("Collapse", BindingFlags.NonPublic | BindingFlags.Instance).Invoke(__instance, new object[] { true });
}
2021-06-18 22:38:32 +01:00
2021-02-16 20:49:10 +00:00
__instance.GetType().GetMethod("SetSurfaceState", BindingFlags.NonPublic | BindingFlags.Instance).Invoke(__instance, new object[] { -1 });
QSBEventManager.FireEvent("PlayerExitQuantumMoon");
}
else
{
var vector = Locator.GetPlayerTransform().position - __instance.transform.position;
Locator.GetPlayerBody().SetVelocity(____moonBody.GetPointVelocity(Locator.GetPlayerTransform().position) - (vector.normalized * 5f));
var d = 80f;
Locator.GetPlayerBody().SetPosition(__instance.transform.position + (____vortexReturnPivot.up * d));
if (!Physics.autoSyncTransforms)
{
Physics.SyncTransforms();
}
2021-06-18 22:38:32 +01:00
2021-02-16 20:49:10 +00:00
var component = Locator.GetPlayerCamera().GetComponent<PlayerCameraController>();
component.SetDegreesY(component.GetMinDegreesY());
____vortexAudio.SetLocalVolume(0f);
____collapseToIndex = 1;
__instance.GetType().GetMethod("Collapse", BindingFlags.NonPublic | BindingFlags.Instance).Invoke(__instance, new object[] { true });
}
}
}
2021-06-18 22:38:32 +01:00
2021-02-16 20:49:10 +00:00
____playerFogBubble.SetFogAlpha(fogAlpha);
____shipLandingCamFogBubble.SetFogAlpha(fogAlpha);
return false;
}
2021-01-18 12:33:07 +00:00
}
}