diff --git a/ShareSuite/MoneySharingHooks.cs b/ShareSuite/MoneySharingHooks.cs index c1b153e..71377c6 100644 --- a/ShareSuite/MoneySharingHooks.cs +++ b/ShareSuite/MoneySharingHooks.cs @@ -1,9 +1,14 @@ -using System; -using System.Linq; +using EntityStates.AffixVoid; using EntityStates.GoldGat; using MonoMod.Cil; using R2API.Utils; using RoR2; +using System; +using System.Collections; +using System.Collections.Generic; +using System.ComponentModel.Design; +using System.Linq; +using System.Reflection; using UnityEngine; using UnityEngine.Networking; @@ -28,6 +33,7 @@ internal static void UnHook() On.RoR2.Networking.NetworkManagerSystem.OnClientConnect -= GoldGatConnect; On.RoR2.Networking.NetworkManagerSystem.OnClientDisconnect -= GoldGatDisconnect; On.RoR2.HealthComponent.TakeDamage -= RollOfPenniesDamageHook; + On.RoR2.GoldSiphonNearbyBodyController.DrainGold -= SiphonGold; IL.EntityStates.GoldGat.GoldGatFire.FireBullet -= RemoveGoldGatMoneyLine; } @@ -46,6 +52,7 @@ internal static void Hook() On.RoR2.Networking.NetworkManagerSystem.OnClientConnect += GoldGatConnect; On.RoR2.Networking.NetworkManagerSystem.OnClientDisconnect += GoldGatDisconnect; On.RoR2.HealthComponent.TakeDamage += RollOfPenniesDamageHook; + On.RoR2.GoldSiphonNearbyBodyController.DrainGold += SiphonGold; if (ShareSuite.MoneyIsShared.Value && GeneralHooks.IsMultiplayer()) IL.EntityStates.GoldGat.GoldGatFire.FireBullet += RemoveGoldGatMoneyLine; @@ -106,6 +113,85 @@ public static bool SharedMoneyEnabled() return ShareSuite.MoneyIsShared.Value; } + private static void SiphonGold(On.RoR2.GoldSiphonNearbyBodyController.orig_DrainGold orig, + GoldSiphonNearbyBodyController self) + { + if (!GeneralHooks.IsMultiplayer() || !ShareSuite.MoneyIsShared.Value) + { + orig(self); + return; + } + + uint currentDrain = self.GetFieldValue("appliedDrain"); + + //var bindFlags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic + // | BindingFlags.Static | BindingFlags.GetField; + //var amountOfPlayerField = typeof(GoldSiphonNearbyBodyController).GetField("tetheredPlayers", bindFlags); + //int amountOfPlayers = (int) amountOfPlayerField.GetValue(self); +#if DEBUG + Debug.Log("-------"); + Debug.Log("Siphoning gold"); + Debug.Log($"Amount of Player: {self.GetFieldValue("tetheredPlayers")}"); + Debug.Log($"tierTracker: {self.GetFieldValue("tierTracker")}"); + Debug.Log($"goldDrainValue: {self.GetFieldValue("goldDrainValue")}"); + Debug.Log($"appliedDrain: {currentDrain}"); +#endif + SphereSearch sphereSearch = self.GetFieldValue("sphereSearch"); + List candidates = new List(); + List hurtBoxes = new List(); + TeamMask mask = default(TeamMask); + mask.AddTeam(TeamIndex.Player); + sphereSearch.mask = LayerIndex.entityPrecise.mask; + sphereSearch.origin = self.gameObject.transform.position; + sphereSearch.radius = self.GetFieldValue("parentShrineReference").radius; + sphereSearch.queryTriggerInteraction = QueryTriggerInteraction.UseGlobal; + sphereSearch.RefreshCandidates(); + sphereSearch.FilterCandidatesByHurtBoxTeam(mask); + sphereSearch.OrderCandidatesByDistance(); + sphereSearch.FilterCandidatesByDistinctHurtBoxEntities(); + sphereSearch.GetHurtBoxes(hurtBoxes); + sphereSearch.GetCandidates(candidates); + sphereSearch.ClearCandidates(); + int players = 0; + if (hurtBoxes.Count > 0) + { + foreach (HurtBox obj in hurtBoxes) + { + if (obj == null) + { + Debug.Log("hurtBox: null hurtbox"); + } + else { + if (obj.healthComponent.body.isPlayerControlled) players++; +#if DEBUG + Debug.Log($"Players in range: {players}"); +#endif + } + } + } +#if DEBUG + Debug.Log($"currentDrain {currentDrain}"); + Debug.Log($"SharedMoneyValue {SharedMoneyValue}"); +#endif + if (currentDrain == 4294967295) { + Debug.Log("We're at the stupid value again, are we not initialized?... Calling orig."); + orig(self); + return; + } + if (SharedMoneyValue >= (int) currentDrain) + { + SharedMoneyValue -= (int) (players * self.GetFieldValue("appliedDrain")); + orig(self); + return; + } + else { +#if DEBUG + Debug.Log("Not Draining due to too little money"); +#endif + return; + } + } + private static void OnShopPurchase(On.RoR2.PurchaseInteraction.orig_OnInteractionBegin orig, PurchaseInteraction self, Interactor activator) {