From 78357c1da45de6f687358e277bf48775212f030c Mon Sep 17 00:00:00 2001 From: Rocco Jiang Date: Tue, 23 Apr 2024 18:16:04 +0100 Subject: [PATCH 1/3] Add rudimentary support for hardcoded extra farm --- BetterJunimos/Abilities/JunimoAbilities.cs | 1 + BetterJunimos/BetterJunimos.cs | 24 +++++++++------ BetterJunimos/Patches/JunimoHutPatches.cs | 3 +- BetterJunimos/Utils/JunimoProgression.cs | 2 +- BetterJunimos/Utils/Util.cs | 34 ++++++++++++++++++---- 5 files changed, 47 insertions(+), 17 deletions(-) diff --git a/BetterJunimos/Abilities/JunimoAbilities.cs b/BetterJunimos/Abilities/JunimoAbilities.cs index 33ce603..c3636d4 100644 --- a/BetterJunimos/Abilities/JunimoAbilities.cs +++ b/BetterJunimos/Abilities/JunimoAbilities.cs @@ -215,6 +215,7 @@ public static bool ActionCoolingDown(GameLocation location, IJunimoAbility abili } private static bool ItemInHut(Guid id, string item) { + // BetterJunimos.SMonitor.Log($"Items in huts keys = ${string.Join(",", ItemsInHuts.Keys)}", LogLevel.Debug); return ItemsInHuts[id].TryGetValue(item, out var present) && present; } diff --git a/BetterJunimos/BetterJunimos.cs b/BetterJunimos/BetterJunimos.cs index 70626e6..13d5756 100644 --- a/BetterJunimos/BetterJunimos.cs +++ b/BetterJunimos/BetterJunimos.cs @@ -191,7 +191,7 @@ private static bool AlternativeTexturesActive() { } private bool ShowPerfectionTracker(ButtonPressedEventArgs e) { - if (Game1.player.currentLocation is not Farm) return false; + if (!Game1.player.currentLocation.IsFarm) return false; if (Game1.activeClickableMenu != null) return false; if (!JunimoProgression.HutOnTile(e.Cursor.Tile)) return false; if (Helper.ModRegistry.Get("ceruleandeep.BetterJunimosForestry") != null) return false; @@ -255,7 +255,7 @@ void OnDayStarted(object sender, DayStartedEventArgs e) { Util.Payments.WereJunimosPaidToday = false; } - var huts = Game1.getFarm().buildings.OfType().ToList(); + var huts = Util.GetAllHuts(); // tag each hut chest so later we can tell whether a GrabMenu close is for a Junimo chest or some other chest foreach (var hut in huts) { @@ -288,9 +288,8 @@ void OnDayStarted(object sender, DayStartedEventArgs e) { private void CheckHutsForWagesAndProgressionItems() { var alreadyPaid = Util.Payments.WereJunimosPaidToday; - - var huts = Game1.getFarm().buildings.OfType(); - var junimoHuts = huts.ToList(); + + var junimoHuts = Util.GetAllHuts(); if (!junimoHuts.Any()) return; // Monitor.Log("Updating hut items", LogLevel.Debug); @@ -298,6 +297,8 @@ private void CheckHutsForWagesAndProgressionItems() { // this might be getting called a bit too much // but since OnMenuChanged doesn't tell us reliably which hut has changed // it's safer to update items from all huts here + // SMonitor.Log($"Getting GUID for ${hut}...", LogLevel.Debug); + // SMonitor.Log($"\tGot GUID as ${Util.GetHutIdFromHut(hut)}", LogLevel.Debug); Util.Abilities.UpdateHutItems(Util.GetHutIdFromHut(hut)); if (Config.JunimoPayment.WorkForWages) { @@ -643,16 +644,21 @@ private static void AllowJunimoHutPurchasing() { } private void SpawnJunimoCommand() { - if (Game1.player.currentLocation.IsFarm || Game1.player.currentLocation.IsGreenhouse) { - var huts = Game1.getFarm().buildings.OfType(); - var junimoHuts = huts.ToList(); + var currentLocation = Game1.player.currentLocation; + + if (currentLocation.IsFarm || currentLocation.IsGreenhouse) { + var junimoHuts = Util.getFarms() + .FindAll(farm => farm.Equals(currentLocation)) + .SelectMany(farm => farm.buildings.OfType()) + .ToList(); + if (!junimoHuts.Any()) { Util.SendMessage(Helper.Translation.Get("msg.cannot-spawn-without-hut")); return; } var hut = junimoHuts.ElementAt(Game1.random.Next(0, junimoHuts.Count)); - Util.SpawnJunimoAtPosition(Game1.player.currentLocation, Game1.player.Position, hut, Game1.random.Next(4, 100)); + Util.SpawnJunimoAtPosition(currentLocation, Game1.player.Position, hut, Game1.random.Next(4, 100)); } else { Util.SendMessage(Helper.Translation.Get("msg.cannot-spawn-here")); diff --git a/BetterJunimos/Patches/JunimoHutPatches.cs b/BetterJunimos/Patches/JunimoHutPatches.cs index 83bd9f4..57bc87a 100644 --- a/BetterJunimos/Patches/JunimoHutPatches.cs +++ b/BetterJunimos/Patches/JunimoHutPatches.cs @@ -40,7 +40,8 @@ public static bool Prefix(JunimoHut __instance, ref bool __result) { private static bool SearchAroundHut(JunimoHut hut) { var id = Util.GetHutIdFromHut(hut); var radius = Util.CurrentWorkingRadius; - GameLocation farm = Game1.getFarm(); + // GameLocation farm = Game1.getFarm(); + GameLocation farm = Game1.currentLocation; // SearchHutGrid manages hut.lastKnownCropLocation and Util.Abilities.lastKnownCropLocations var foundWork = SearchHutGrid(hut, radius, farm, id); diff --git a/BetterJunimos/Utils/JunimoProgression.cs b/BetterJunimos/Utils/JunimoProgression.cs index 399e167..87cdf4a 100644 --- a/BetterJunimos/Utils/JunimoProgression.cs +++ b/BetterJunimos/Utils/JunimoProgression.cs @@ -496,7 +496,7 @@ internal void ListAvailableActions(Guid id) { public static bool HutOnTile(Vector2 pos) { - return Game1.getFarm().buildings.Any(b => b is JunimoHut && b.occupiesTile(pos)); + return Util.getFarms().Any(farm => farm.buildings.Any(b => b is JunimoHut && b.occupiesTile(pos))); } private string Get(string key) { diff --git a/BetterJunimos/Utils/Util.cs b/BetterJunimos/Utils/Util.cs index 605ecf5..5976509 100644 --- a/BetterJunimos/Utils/Util.cs +++ b/BetterJunimos/Utils/Util.cs @@ -30,6 +30,22 @@ public class Util { internal static JunimoProgression Progression; internal static JunimoGreenhouse Greenhouse; + public static List getFarms() { + // TODO: use the trick from Discord? + + var farms = new List { Game1.getFarm() }; + + var ridgesideSummitFarm = Game1.getLocationFromName("Custom_Ridgeside_SummitFarm"); + if (ridgesideSummitFarm != null) { + farms.Add(ridgesideSummitFarm); + } + + // BetterJunimos.SMonitor.Log($"Buildings found in main farm ${string.Join(",", farms[0].buildings)}", LogLevel.Debug); + // BetterJunimos.SMonitor.Log($"Buildings found in summit farm ${string.Join(",", farms[1].buildings)}", LogLevel.Debug); + + return farms; + } + public static int CurrentWorkingRadius { get { if (!BetterJunimos.Config.JunimoPayment.WorkForWages) return BetterJunimos.Config.JunimoHuts.MaxRadius; @@ -39,15 +55,23 @@ public static int CurrentWorkingRadius { } public static List GetAllHuts() { - return Game1.getFarm().buildings.OfType().ToList(); + return getFarms().SelectMany(farm => farm.buildings.OfType().ToList()).ToList(); } public static Guid GetHutIdFromHut(JunimoHut hut) { - return Game1.getFarm().buildings.GuidOf(hut); + return getFarms().Select(farm => farm.buildings.GuidOf(hut)).ToList().Find(guid => guid != Guid.Empty); } public static JunimoHut GetHutFromId(Guid id) { - return Game1.getFarm().buildings[id] as JunimoHut; + foreach (var farm in getFarms()) { + if (farm.buildings.TryGetValue(id, out var hut)) { + return hut as JunimoHut; + } + } + + BetterJunimos.SMonitor.Log($"Could not get hut from id ${id}", LogLevel.Error); + return null; + // return getFarms().Select(farm => farm.buildings[id]).ToList().Find(hut => hut != null) as JunimoHut; } public static void AddItemToChest(GameLocation farm, Chest chest, SObject item) { @@ -74,7 +98,7 @@ public static void RemoveItemFromChest(Chest chest, Item item, int count = 1) { public static void SpawnJunimoAtHut(JunimoHut hut) { // I don't know why we're multiplying by 64 here var pos = new Vector2((float) hut.tileX.Value + 1, (float) hut.tileY.Value + 1) * 64f + new Vector2(0.0f, 32f); - SpawnJunimoAtPosition(Game1.getFarm(), pos, hut, hut.getUnusedJunimoNumber()); + SpawnJunimoAtPosition(Game1.player.currentLocation, pos, hut, hut.getUnusedJunimoNumber()); } public static void SpawnJunimoAtPosition(GameLocation location, Vector2 pos, JunimoHut hut, int junimoNumber) { @@ -133,8 +157,6 @@ public static void SpawnJunimoAtPosition(GameLocation location, Vector2 pos, Jun if (!Utility.isOnScreen(Utility.Vector2ToPoint(pos), 64, location)) return; location.playSound("junimoMeep1"); - - } /* From 226d9963a1dbdab9e5fce4f59029201ad6496b72 Mon Sep 17 00:00:00 2001 From: Rocco Jiang Date: Tue, 23 Apr 2024 18:42:45 +0100 Subject: [PATCH 2/3] Detect all locations indicated to be outdoor farms --- BetterJunimos/Abilities/JunimoAbilities.cs | 1 - BetterJunimos/BetterJunimos.cs | 2 +- BetterJunimos/Patches/JunimoHutPatches.cs | 1 - BetterJunimos/Utils/JunimoProgression.cs | 2 +- BetterJunimos/Utils/Util.cs | 23 +++++----------------- 5 files changed, 7 insertions(+), 22 deletions(-) diff --git a/BetterJunimos/Abilities/JunimoAbilities.cs b/BetterJunimos/Abilities/JunimoAbilities.cs index c3636d4..33ce603 100644 --- a/BetterJunimos/Abilities/JunimoAbilities.cs +++ b/BetterJunimos/Abilities/JunimoAbilities.cs @@ -215,7 +215,6 @@ public static bool ActionCoolingDown(GameLocation location, IJunimoAbility abili } private static bool ItemInHut(Guid id, string item) { - // BetterJunimos.SMonitor.Log($"Items in huts keys = ${string.Join(",", ItemsInHuts.Keys)}", LogLevel.Debug); return ItemsInHuts[id].TryGetValue(item, out var present) && present; } diff --git a/BetterJunimos/BetterJunimos.cs b/BetterJunimos/BetterJunimos.cs index 13d5756..3637045 100644 --- a/BetterJunimos/BetterJunimos.cs +++ b/BetterJunimos/BetterJunimos.cs @@ -647,7 +647,7 @@ private void SpawnJunimoCommand() { var currentLocation = Game1.player.currentLocation; if (currentLocation.IsFarm || currentLocation.IsGreenhouse) { - var junimoHuts = Util.getFarms() + var junimoHuts = Util.GetAllFarms() .FindAll(farm => farm.Equals(currentLocation)) .SelectMany(farm => farm.buildings.OfType()) .ToList(); diff --git a/BetterJunimos/Patches/JunimoHutPatches.cs b/BetterJunimos/Patches/JunimoHutPatches.cs index 57bc87a..a5c7a75 100644 --- a/BetterJunimos/Patches/JunimoHutPatches.cs +++ b/BetterJunimos/Patches/JunimoHutPatches.cs @@ -40,7 +40,6 @@ public static bool Prefix(JunimoHut __instance, ref bool __result) { private static bool SearchAroundHut(JunimoHut hut) { var id = Util.GetHutIdFromHut(hut); var radius = Util.CurrentWorkingRadius; - // GameLocation farm = Game1.getFarm(); GameLocation farm = Game1.currentLocation; // SearchHutGrid manages hut.lastKnownCropLocation and Util.Abilities.lastKnownCropLocations diff --git a/BetterJunimos/Utils/JunimoProgression.cs b/BetterJunimos/Utils/JunimoProgression.cs index 87cdf4a..a1dd20d 100644 --- a/BetterJunimos/Utils/JunimoProgression.cs +++ b/BetterJunimos/Utils/JunimoProgression.cs @@ -496,7 +496,7 @@ internal void ListAvailableActions(Guid id) { public static bool HutOnTile(Vector2 pos) { - return Util.getFarms().Any(farm => farm.buildings.Any(b => b is JunimoHut && b.occupiesTile(pos))); + return Util.GetAllFarms().Any(farm => farm.buildings.Any(b => b is JunimoHut && b.occupiesTile(pos))); } private string Get(string key) { diff --git a/BetterJunimos/Utils/Util.cs b/BetterJunimos/Utils/Util.cs index 5976509..a2a4cee 100644 --- a/BetterJunimos/Utils/Util.cs +++ b/BetterJunimos/Utils/Util.cs @@ -30,20 +30,8 @@ public class Util { internal static JunimoProgression Progression; internal static JunimoGreenhouse Greenhouse; - public static List getFarms() { - // TODO: use the trick from Discord? - - var farms = new List { Game1.getFarm() }; - - var ridgesideSummitFarm = Game1.getLocationFromName("Custom_Ridgeside_SummitFarm"); - if (ridgesideSummitFarm != null) { - farms.Add(ridgesideSummitFarm); - } - - // BetterJunimos.SMonitor.Log($"Buildings found in main farm ${string.Join(",", farms[0].buildings)}", LogLevel.Debug); - // BetterJunimos.SMonitor.Log($"Buildings found in summit farm ${string.Join(",", farms[1].buildings)}", LogLevel.Debug); - - return farms; + public static List GetAllFarms() { + return Game1.locations.Where(loc => loc.IsFarm && loc.IsOutdoors).ToList(); } public static int CurrentWorkingRadius { @@ -55,15 +43,15 @@ public static int CurrentWorkingRadius { } public static List GetAllHuts() { - return getFarms().SelectMany(farm => farm.buildings.OfType().ToList()).ToList(); + return GetAllFarms().SelectMany(farm => farm.buildings.OfType().ToList()).ToList(); } public static Guid GetHutIdFromHut(JunimoHut hut) { - return getFarms().Select(farm => farm.buildings.GuidOf(hut)).ToList().Find(guid => guid != Guid.Empty); + return GetAllFarms().Select(farm => farm.buildings.GuidOf(hut)).ToList().Find(guid => guid != Guid.Empty); } public static JunimoHut GetHutFromId(Guid id) { - foreach (var farm in getFarms()) { + foreach (var farm in GetAllFarms()) { if (farm.buildings.TryGetValue(id, out var hut)) { return hut as JunimoHut; } @@ -71,7 +59,6 @@ public static JunimoHut GetHutFromId(Guid id) { BetterJunimos.SMonitor.Log($"Could not get hut from id ${id}", LogLevel.Error); return null; - // return getFarms().Select(farm => farm.buildings[id]).ToList().Find(hut => hut != null) as JunimoHut; } public static void AddItemToChest(GameLocation farm, Chest chest, SObject item) { From 156f5af0dde33574249faa0f3ff912e14259b7f6 Mon Sep 17 00:00:00 2001 From: Rocco Jiang Date: Tue, 23 Apr 2024 18:55:06 +0100 Subject: [PATCH 3/3] Remove unneeded debug messages --- BetterJunimos/BetterJunimos.cs | 2 -- 1 file changed, 2 deletions(-) diff --git a/BetterJunimos/BetterJunimos.cs b/BetterJunimos/BetterJunimos.cs index 3637045..6a4fb41 100644 --- a/BetterJunimos/BetterJunimos.cs +++ b/BetterJunimos/BetterJunimos.cs @@ -297,8 +297,6 @@ private void CheckHutsForWagesAndProgressionItems() { // this might be getting called a bit too much // but since OnMenuChanged doesn't tell us reliably which hut has changed // it's safer to update items from all huts here - // SMonitor.Log($"Getting GUID for ${hut}...", LogLevel.Debug); - // SMonitor.Log($"\tGot GUID as ${Util.GetHutIdFromHut(hut)}", LogLevel.Debug); Util.Abilities.UpdateHutItems(Util.GetHutIdFromHut(hut)); if (Config.JunimoPayment.WorkForWages) {