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

121 lines
4.0 KiB
C#
Raw Normal View History

using HarmonyLib;
using QSB.Patches;
2020-12-22 09:13:08 +00:00
using QSB.Player;
using QSB.Utility;
2020-12-22 09:13:08 +00:00
using System.Linq;
using System.Reflection;
using UnityEngine;
namespace QSB.QuantumSync.Patches
2020-12-22 09:13:08 +00:00
{
[HarmonyPatch]
2020-12-22 09:13:08 +00:00
public class QuantumVisibilityPatches : QSBPatch
{
public override QSBPatchTypes Type => QSBPatchTypes.OnClientConnect;
[HarmonyPostfix]
[HarmonyPatch(typeof(Shape), nameof(Shape.OnEnable))]
2021-07-07 09:02:23 +01:00
public static void Shape_OnEnable(Shape __instance)
=> __instance.RaiseEvent("OnShapeActivated", __instance);
[HarmonyPostfix]
[HarmonyPatch(typeof(Shape), nameof(Shape.OnDisable))]
2021-02-28 15:29:09 +00:00
public static void Shape_OnDisable(Shape __instance)
=> __instance.RaiseEvent("OnShapeDeactivated", __instance);
2021-02-09 17:18:01 +00:00
2020-12-22 09:13:08 +00:00
// ShapeVisibilityTracker patches
[HarmonyPrefix]
[HarmonyPatch(typeof(ShapeVisibilityTracker), nameof(ShapeVisibilityTracker.IsVisibleUsingCameraFrustum))]
2021-06-18 21:54:32 +01:00
public static bool ShapeVisibilityTracker_IsVisibleUsingCameraFrustum(ShapeVisibilityTracker __instance, ref bool __result)
2020-12-22 09:13:08 +00:00
{
2021-10-12 15:32:47 +01:00
__result = QuantumManager.IsVisibleUsingCameraFrustum(__instance, false).Item1;
2020-12-22 09:13:08 +00:00
return false;
}
[HarmonyPrefix]
[HarmonyPatch(typeof(ShapeVisibilityTracker), nameof(ShapeVisibilityTracker.IsVisible))]
2021-06-18 21:54:32 +01:00
public static bool ShapeVisibilityTracker_IsVisible(ShapeVisibilityTracker __instance, ref bool __result)
2020-12-22 09:13:08 +00:00
{
__result = QuantumManager.IsVisible(__instance, false);
2020-12-22 09:13:08 +00:00
return false;
}
2021-02-16 12:13:27 +00:00
// RendererVisibilityTracker patches - probably not needed as i don't think RendererVisibilityTracker is ever used?
2020-12-22 09:13:08 +00:00
[HarmonyPrefix]
[HarmonyPatch(typeof(RendererVisibilityTracker), nameof(RendererVisibilityTracker.IsVisibleUsingCameraFrustum))]
2021-06-18 21:54:32 +01:00
public static bool RendererVisibilityTracker_IsVisibleUsingCameraFrustum(RendererVisibilityTracker __instance, ref bool __result, Renderer ____renderer, bool ____checkFrustumOcclusion)
2020-12-22 09:13:08 +00:00
{
2021-02-22 10:51:51 +00:00
__result = QSBPlayerManager.GetPlayersWithCameras()
.Any(x => GeometryUtility.TestPlanesAABB(x.Camera.GetFrustumPlanes(), ____renderer.bounds))
&& (!____checkFrustumOcclusion || QSBPlayerManager.GetPlayersWithCameras()
2020-12-22 09:13:08 +00:00
.Any(x => !(bool)__instance.GetType()
.GetMethod("IsOccludedFromPosition", BindingFlags.NonPublic | BindingFlags.Instance)
2021-02-22 10:51:51 +00:00
.Invoke(__instance, new object[] { x.Camera.transform.position })));
2020-12-22 09:13:08 +00:00
return false;
}
// VisibilityObject
[HarmonyPrefix]
[HarmonyPatch(typeof(VisibilityObject), nameof(VisibilityObject.CheckIllumination))]
2021-06-18 21:54:32 +01:00
public static bool VisibilityObject_CheckIllumination(VisibilityObject __instance, ref bool __result, bool ____checkIllumination, Vector3 ____localIlluminationOffset, float ____illuminationRadius, Light[] ____lightSources)
{
if (!____checkIllumination)
{
__result = true;
return false;
}
2021-06-18 22:38:32 +01:00
var point = __instance.transform.TransformPoint(____localIlluminationOffset);
var tupleFlashlights = QSBPlayerManager.GetPlayerFlashlights();
2021-10-12 15:32:47 +01:00
var localFlashlight = tupleFlashlights.Item1;
var playerFlashlights = tupleFlashlights.Item2;
// local player flashlight
if (localFlashlight.CheckIlluminationAtPoint(point, ____illuminationRadius))
{
__result = true;
return false;
}
// all other player flashlights
if (playerFlashlights.Any(x => x.CheckIlluminationAtPoint(point, ____illuminationRadius)))
{
__result = true;
return false;
}
2021-08-22 16:40:50 +01:00
// BUG : Implement checking for other probes!
if (Locator.GetProbe() != null && Locator.GetProbe().IsLaunched() && Locator.GetProbe().CheckIlluminationAtPoint(point, ____illuminationRadius))
{
__result = true;
return false;
}
2021-08-22 16:40:50 +01:00
// BUG : Implement checking for other player's thrusters!
if (Locator.GetThrusterLightTracker().CheckIlluminationAtPoint(point, ____illuminationRadius))
{
__result = true;
return false;
}
if (____lightSources != null)
{
foreach (var light in ____lightSources)
{
if (light.intensity > 0f && light.range > 0f)
{
__result = true;
return false;
}
}
}
2021-06-18 22:38:32 +01:00
__result = false;
return false;
}
2020-12-22 09:13:08 +00:00
}
}