From 2e030b6fe2093988203241ed6e47deba3b8044ec Mon Sep 17 00:00:00 2001 From: PhatPhuckDave Date: Sat, 28 Sep 2024 23:47:21 +0200 Subject: [PATCH] Implement echoing tower selection to all towers --- .../InfectionFreeZone/InfectionFreeZone.cs | 75 ++++++++++++++++++- .../InfectionFreeZone.csproj | 3 + 2 files changed, 76 insertions(+), 2 deletions(-) diff --git a/Projects/InfectionFreeZone/InfectionFreeZone/InfectionFreeZone.cs b/Projects/InfectionFreeZone/InfectionFreeZone/InfectionFreeZone.cs index 02b4e54..8245773 100644 --- a/Projects/InfectionFreeZone/InfectionFreeZone/InfectionFreeZone.cs +++ b/Projects/InfectionFreeZone/InfectionFreeZone/InfectionFreeZone.cs @@ -16,6 +16,7 @@ using Gameplay.GameResources; using Gameplay.InGameResources; using Gameplay.Production; using Gameplay.Rebuilding; +using Gameplay.Rebuilding.Towers; using Gameplay.Rebuilding.Walls; using Gameplay.Scavenge; using Gameplay.Units.Characters; @@ -26,6 +27,7 @@ using Gameplay.Vehicles; using HarmonyLib; using HarmonyLib.Tools; using MapEssentials.Temp; +using UI.InfoPanels.NestedUIElements; using UnityEngine; using Zenject; @@ -95,7 +97,7 @@ namespace InfectionFreeZone { public static ConfigEntry childToAdultRatioInfluenceFloorParam; public static ConfigEntry childToAdultRatioInfluenceCeilingParam; - public static ConfigEntry rotationTimestepMultiplier; + public static ConfigEntry rotationTimestepMultiplier; // This is NOT unused! public static ConfigEntry sunsetHourOffset; public static ConfigEntry sunriseHourOffset; @@ -103,6 +105,12 @@ namespace InfectionFreeZone { public static ConfigEntry productionProfitMultiplier; public static Dictionary productionProfitMultiplierDict; + public static ConfigEntry towerEchoSelectionDebug; + public static ConfigEntry towerEchoSelection; + public static List> towerEchoSelectionList; + + // See System.Void UI.InfoPanels.NestedUIElements.SelectWeaponPanel::OnWeaponItemClicked(UI.InfoPanels.NestedUIElements.WeaponItem) + public void Awake() { resourceMultiplierDebug = Config.Bind("General", "Resource Multiplier Debug", false); resourceMultiplier = Config.Bind("General", "Resource Multiplier", 1f); @@ -196,6 +204,10 @@ namespace InfectionFreeZone { } }; + towerEchoSelectionDebug = Config.Bind("General", "Tower Echo Selection Debug", false); + towerEchoSelection = Config.Bind("General", "Tower Echo Selection", false); + towerEchoSelectionList = new List>(); + Logger.LogInfo("Cyka mod loaded"); HarmonyFileLog.Enabled = true; Harmony harmony = new Harmony(pluginGuid); @@ -543,7 +555,7 @@ namespace InfectionFreeZone { __result += Main.sunsetHourOffset.Value; __result = Mathf.Clamp(__result, 0f, 24f); } - + [HarmonyPostfix] [HarmonyPatch(typeof(TimeController), "GetSunriseHour")] public static void PostfixGetSunriseHour(ref float __result) { @@ -574,5 +586,64 @@ namespace InfectionFreeZone { } } } + + [HarmonyPostfix] + [HarmonyPatch(typeof(StructureDefenceModule), "Awake")] + public static void PostfixStructureDefenceModule(StructureDefenceModule __instance) { + if (Main.towerEchoSelectionDebug.Value) + Console.WriteLine($"Structure defence module created"); + Main.towerEchoSelectionList.Add(new WeakReference(__instance)); + } + + [HarmonyPrefix] + [HarmonyPatch(typeof(SelectWeaponPanel), "OnWeaponItemClicked")] + public static void PostfixSelectWeaponPanel(WeaponItem selectedItem) { + if (!Main.towerEchoSelection.Value) + return; + if (selectedItem == null) { + if (Main.towerEchoSelectionDebug.Value) + Console.WriteLine("Selected weapon item is null !?"); + return; + } + + if (selectedItem.WeaponData == null) { + if (Main.towerEchoSelectionDebug.Value) + Console.WriteLine("Selected weapon data is null !?"); + return; + } + + if (Main.towerEchoSelectionDebug.Value) { + Console.WriteLine($"Selected weapon item is {selectedItem.name}"); + Console.WriteLine($"Echoing weapon to {Main.towerEchoSelectionList.Count} towers"); + } + + for (var i = 0; i < Main.towerEchoSelectionList.Count; i++) { + try { + var towerEchoSelection = Main.towerEchoSelectionList[i]; + if (towerEchoSelection.TryGetTarget(out var towerEchoSelectionT)) { + if (towerEchoSelectionT == null) { + if (Main.towerEchoSelectionDebug.Value) + Console.WriteLine("Tower defence module is null !?"); + continue; + } + + if (Main.towerEchoSelectionDebug.Value) + Console.WriteLine( + $"Tower defence weapon is {towerEchoSelectionT.SelectedWeapon.ToString()}"); + + towerEchoSelectionT.SetFieldOfView(selectedItem.WeaponData); + towerEchoSelectionT.SetupWeapon(selectedItem.WeaponData); + + var selectedWeapon = towerEchoSelectionT.SelectedWeapon == null + ? "null" + : towerEchoSelectionT.SelectedWeapon.ToString(); + if (Main.towerEchoSelectionDebug.Value) + Console.WriteLine($"Tower defence weapon modified to {selectedWeapon}"); + } + } catch (NullReferenceException e) { + Console.WriteLine($"Failed to set tower defence weapon: {e}"); + } + } + } } } \ No newline at end of file diff --git a/Projects/InfectionFreeZone/InfectionFreeZone/InfectionFreeZone.csproj b/Projects/InfectionFreeZone/InfectionFreeZone/InfectionFreeZone.csproj index dff4014..877f12f 100644 --- a/Projects/InfectionFreeZone/InfectionFreeZone/InfectionFreeZone.csproj +++ b/Projects/InfectionFreeZone/InfectionFreeZone/InfectionFreeZone.csproj @@ -63,6 +63,9 @@ $(GAME_MANAGED)/Zenject.dll + + $(GAME_MANAGED)/Sirenix.Serialization.dll +