Skip to content

Commit

Permalink
Merge pull request #40 from roccojiang/support-other-farms
Browse files Browse the repository at this point in the history
Add support for additional farms
  • Loading branch information
hawkfalcon authored May 4, 2024
2 parents 65e9e2a + 156f5af commit 9b6256a
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 17 deletions.
22 changes: 13 additions & 9 deletions BetterJunimos/BetterJunimos.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -255,7 +255,7 @@ void OnDayStarted(object sender, DayStartedEventArgs e) {
Util.Payments.WereJunimosPaidToday = false;
}

var huts = Game1.getFarm().buildings.OfType<JunimoHut>().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) {
Expand Down Expand Up @@ -288,9 +288,8 @@ void OnDayStarted(object sender, DayStartedEventArgs e) {

private void CheckHutsForWagesAndProgressionItems() {
var alreadyPaid = Util.Payments.WereJunimosPaidToday;

var huts = Game1.getFarm().buildings.OfType<JunimoHut>();
var junimoHuts = huts.ToList();

var junimoHuts = Util.GetAllHuts();
if (!junimoHuts.Any()) return;

// Monitor.Log("Updating hut items", LogLevel.Debug);
Expand Down Expand Up @@ -643,16 +642,21 @@ private static void AllowJunimoHutPurchasing() {
}

private void SpawnJunimoCommand() {
if (Game1.player.currentLocation.IsFarm || Game1.player.currentLocation.IsGreenhouse) {
var huts = Game1.getFarm().buildings.OfType<JunimoHut>();
var junimoHuts = huts.ToList();
var currentLocation = Game1.player.currentLocation;

if (currentLocation.IsFarm || currentLocation.IsGreenhouse) {
var junimoHuts = Util.GetAllFarms()
.FindAll(farm => farm.Equals(currentLocation))
.SelectMany(farm => farm.buildings.OfType<JunimoHut>())
.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"));
Expand Down
2 changes: 1 addition & 1 deletion BetterJunimos/Patches/JunimoHutPatches.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ 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
var foundWork = SearchHutGrid(hut, radius, farm, id);
Expand Down
2 changes: 1 addition & 1 deletion BetterJunimos/Utils/JunimoProgression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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.GetAllFarms().Any(farm => farm.buildings.Any(b => b is JunimoHut && b.occupiesTile(pos)));
}

private string Get(string key) {
Expand Down
21 changes: 15 additions & 6 deletions BetterJunimos/Utils/Util.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ public class Util {
internal static JunimoProgression Progression;
internal static JunimoGreenhouse Greenhouse;

public static List<GameLocation> GetAllFarms() {
return Game1.locations.Where(loc => loc.IsFarm && loc.IsOutdoors).ToList();
}

public static int CurrentWorkingRadius {
get {
if (!BetterJunimos.Config.JunimoPayment.WorkForWages) return BetterJunimos.Config.JunimoHuts.MaxRadius;
Expand All @@ -39,15 +43,22 @@ public static int CurrentWorkingRadius {
}

public static List<JunimoHut> GetAllHuts() {
return Game1.getFarm().buildings.OfType<JunimoHut>().ToList();
return GetAllFarms().SelectMany(farm => farm.buildings.OfType<JunimoHut>().ToList()).ToList();
}

public static Guid GetHutIdFromHut(JunimoHut hut) {
return Game1.getFarm().buildings.GuidOf(hut);
return GetAllFarms().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 GetAllFarms()) {
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;
}

public static void AddItemToChest(GameLocation farm, Chest chest, SObject item) {
Expand All @@ -74,7 +85,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) {
Expand Down Expand Up @@ -133,8 +144,6 @@ public static void SpawnJunimoAtPosition(GameLocation location, Vector2 pos, Jun

if (!Utility.isOnScreen(Utility.Vector2ToPoint(pos), 64, location)) return;
location.playSound("junimoMeep1");


}

/*
Expand Down

0 comments on commit 9b6256a

Please sign in to comment.