From f9db5be09be0842d133ba426c535820c7e4b9a43 Mon Sep 17 00:00:00 2001 From: Aaro Varis Date: Sun, 9 Mar 2025 23:15:51 +0200 Subject: [PATCH] zone patches --- BoneSync/Patching/ZonePatches.cs | 136 +++++++++++++++++++++++++++++++ 1 file changed, 136 insertions(+) diff --git a/BoneSync/Patching/ZonePatches.cs b/BoneSync/Patching/ZonePatches.cs index 015680a..aec916f 100644 --- a/BoneSync/Patching/ZonePatches.cs +++ b/BoneSync/Patching/ZonePatches.cs @@ -8,9 +8,84 @@ using HarmonyLib; using MelonLoader; using StressLevelZero.Pool; using StressLevelZero.Zones; +using UnityEngine; namespace BoneSync.Patching { + + public static class ZoneTrackingUtilities + { + public static Dictionary zoneCount = new Dictionary(); + public static Dictionary triggerCount = new Dictionary(); + + public static void Increment(SceneZone zone) + { + if (!zoneCount.ContainsKey(zone)) + zoneCount.Add(zone, 0); + + zoneCount[zone]++; + } + + public static void Decrement(SceneZone zone) + { + if (!zoneCount.ContainsKey(zone)) + zoneCount.Add(zone, 0); + + zoneCount[zone]--; + zoneCount[zone] = Mathf.Clamp(zoneCount[zone], 0, int.MaxValue); + } + + public static bool CanEnter(SceneZone zone) + { + if (!zoneCount.ContainsKey(zone)) + return false; + + return zoneCount[zone] <= 1; + } + + public static bool CanExit(SceneZone zone) + { + if (!zoneCount.ContainsKey(zone)) + return false; + + return zoneCount[zone] <= 0; + } + + public static void Increment(PlayerTrigger trigger) + { + if (!triggerCount.ContainsKey(trigger)) + triggerCount.Add(trigger, 0); + + triggerCount[trigger]++; + } + + public static void Decrement(PlayerTrigger trigger) + { + if (!triggerCount.ContainsKey(trigger)) + triggerCount.Add(trigger, 0); + + triggerCount[trigger]--; + triggerCount[trigger] = Mathf.Clamp(triggerCount[trigger], 0, int.MaxValue); + } + + public static bool CanEnter(PlayerTrigger trigger) + { + if (!triggerCount.ContainsKey(trigger)) + return false; + + return triggerCount[trigger] <= 1; + } + + public static bool CanExit(PlayerTrigger trigger) + { + if (!triggerCount.ContainsKey(trigger)) + return false; + + return triggerCount[trigger] <= 0; + } + } + + [HarmonyPatch(typeof(ZoneSpawner))] public static class ZoneSpawnerPatch { @@ -50,4 +125,65 @@ namespace BoneSync.Patching return false; } } + + // SceneZone and PlayerTrigger patches are based on the ones from entanglement mod + [HarmonyPatch(typeof(SceneZone))] + public static class SceneZonePatch + { + [HarmonyPatch(nameof(SceneZone.OnTriggerEnter)), HarmonyPrefix] + public static bool EnterPrefix(SceneZone __instance, Collider other) + { + if (other.CompareTag("Player")) + { + ZoneTrackingUtilities.Increment(__instance); + bool canEnter = ZoneTrackingUtilities.CanEnter(__instance); + return canEnter; + } + + return true; + } + + [HarmonyPatch(nameof(SceneZone.OnTriggerExit)), HarmonyPrefix] + public static bool ExitPrefix(SceneZone __instance, Collider other) + { + if (other.CompareTag("Player")) + { + ZoneTrackingUtilities.Decrement(__instance); + bool canExit = ZoneTrackingUtilities.CanExit(__instance); + return canExit; + } + + return true; + } + } + + [HarmonyPatch(typeof(PlayerTrigger))] + public static class PlayerTriggerPatch + { + [HarmonyPatch(nameof(PlayerTrigger.OnTriggerEnter)), HarmonyPrefix] + public static bool EnterPrefix(PlayerTrigger __instance, Collider other) + { + if (other.CompareTag("Player")) + { + ZoneTrackingUtilities.Increment(__instance); + bool canEnter = ZoneTrackingUtilities.CanEnter(__instance); + return canEnter; + } + + return true; + } + + [HarmonyPatch(nameof(PlayerTrigger.OnTriggerExit)), HarmonyPrefix] + public static bool ExitPrefix(PlayerTrigger __instance, Collider other) + { + if (other.CompareTag("Player")) + { + ZoneTrackingUtilities.Decrement(__instance); + bool canExit = ZoneTrackingUtilities.CanExit(__instance); + return canExit; + } + + return true; + } + } }