From 17d15e0c991d47be1083eaab3c749cdbf6f9024f Mon Sep 17 00:00:00 2001 From: arannya Date: Wed, 1 May 2024 09:29:49 +0600 Subject: [PATCH 1/8] [Enhancement] Addresses recharging bots with batteries: issue #25 --- Farmtronics/Bot/BotObject.cs | 15 +++++++++++++++ Farmtronics/M1/M1API.cs | 10 ++++++++++ 2 files changed, 25 insertions(+) diff --git a/Farmtronics/Bot/BotObject.cs b/Farmtronics/Bot/BotObject.cs index ee2ef7d..c1f9831 100644 --- a/Farmtronics/Bot/BotObject.cs +++ b/Farmtronics/Bot/BotObject.cs @@ -5,6 +5,8 @@ This class is a stardew valley Object subclass that represents a Bot. using System; using System.Collections.Generic; using System.IO; +using System.Linq; +using System.Threading; using Farmtronics.M1; using Farmtronics.Utils; using Microsoft.Xna.Framework; @@ -163,7 +165,20 @@ public override bool placementAction(GameLocation location, int x, int y, Farmer location.playSound("hammer"); return true; } + + public bool UseBattery() { + if (farmer == null || inventory == null || farmer.CurrentTool == null) + return false; + if (farmer.Items.Any(b => b is not null && b.QualifiedItemId.Equals("(O)787"))) + { + farmer.stamina = Math.Min(Farmer.startingStamina, farmer.stamina + 100); + farmer.removeFirstOfThisItemFromInventory("(O)787"); + return true; + } + return false; + } + // Apply the currently-selected item as a tool (or weapon) on // the square in front of the bot. public void UseTool() { diff --git a/Farmtronics/M1/M1API.cs b/Farmtronics/M1/M1API.cs index 3e1a4fc..5f0d227 100644 --- a/Farmtronics/M1/M1API.cs +++ b/Farmtronics/M1/M1API.cs @@ -720,6 +720,16 @@ public static ValMap MeModule() { return result ? Intrinsic.Result.True : Intrinsic.Result.False; }; meModule["harvest"] = f.GetFunc(); + + f = Intrinsic.Create(""); + f.code = (context, partialResult) => { + Shell sh = context.interpreter.hostData as Shell; + if (RequireBot(sh, "useBattery")) return Intrinsic.Result.Null; + + bool result = sh.bot.UseBattery(); + return result ? Intrinsic.Result.True : Intrinsic.Result.False; + }; + meModule["useBattery"] = f.GetFunc(); botProtectedKeys = new HashSet(); foreach (Value key in meModule.Keys) { From f846c7bbbce8bb01cb847144d6ebe844222b6392 Mon Sep 17 00:00:00 2001 From: arannya Date: Wed, 1 May 2024 14:35:38 +0600 Subject: [PATCH 2/8] [Fix] Minor syntax change. --- Farmtronics/Bot/BotObject.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Farmtronics/Bot/BotObject.cs b/Farmtronics/Bot/BotObject.cs index c1f9831..368569d 100644 --- a/Farmtronics/Bot/BotObject.cs +++ b/Farmtronics/Bot/BotObject.cs @@ -170,7 +170,7 @@ public bool UseBattery() { if (farmer == null || inventory == null || farmer.CurrentTool == null) return false; - if (farmer.Items.Any(b => b is not null && b.QualifiedItemId.Equals("(O)787"))) + if (farmer.Items.Any(b => b != null && b.QualifiedItemId.Equals("(O)787"))) { farmer.stamina = Math.Min(Farmer.startingStamina, farmer.stamina + 100); farmer.removeFirstOfThisItemFromInventory("(O)787"); From dc75d4d0f5338078122f2a2d9b408315c275de1b Mon Sep 17 00:00:00 2001 From: arannya Date: Wed, 1 May 2024 17:41:07 +0600 Subject: [PATCH 3/8] [Fix] Added log messages, prevent battery usage if stamina already full. --- Farmtronics/Bot/BotObject.cs | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/Farmtronics/Bot/BotObject.cs b/Farmtronics/Bot/BotObject.cs index 368569d..12fc141 100644 --- a/Farmtronics/Bot/BotObject.cs +++ b/Farmtronics/Bot/BotObject.cs @@ -169,10 +169,17 @@ public override bool placementAction(GameLocation location, int x, int y, Farmer public bool UseBattery() { if (farmer == null || inventory == null || farmer.CurrentTool == null) return false; - - if (farmer.Items.Any(b => b != null && b.QualifiedItemId.Equals("(O)787"))) + + if (farmer.stamina >= Farmer.startingStamina) { + ModEntry.instance.Monitor.Log($"UseBattery called: Stamina already full, battery not used.", LogLevel.Trace); + return false; + } + + if (farmer.Items.Any(b => b != null && b.QualifiedItemId.Equals("(O)787"))) { + float oldStamina = farmer.stamina; farmer.stamina = Math.Min(Farmer.startingStamina, farmer.stamina + 100); + ModEntry.instance.Monitor.Log($"UseBattery called: Stamina increased from {oldStamina} to {farmer.stamina}", LogLevel.Trace); farmer.removeFirstOfThisItemFromInventory("(O)787"); return true; } From 5c1abe534834d12582fa1ac96ba51eb2325280ed Mon Sep 17 00:00:00 2001 From: arannya Date: Sat, 11 May 2024 20:54:24 +0600 Subject: [PATCH 4/8] Revert "[Fix] Added log messages, prevent battery usage if stamina already full." This reverts commit dc75d4d0f5338078122f2a2d9b408315c275de1b. --- Farmtronics/Bot/BotObject.cs | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/Farmtronics/Bot/BotObject.cs b/Farmtronics/Bot/BotObject.cs index 12fc141..368569d 100644 --- a/Farmtronics/Bot/BotObject.cs +++ b/Farmtronics/Bot/BotObject.cs @@ -169,17 +169,10 @@ public override bool placementAction(GameLocation location, int x, int y, Farmer public bool UseBattery() { if (farmer == null || inventory == null || farmer.CurrentTool == null) return false; - - if (farmer.stamina >= Farmer.startingStamina) { - ModEntry.instance.Monitor.Log($"UseBattery called: Stamina already full, battery not used.", LogLevel.Trace); - return false; - } - - if (farmer.Items.Any(b => b != null && b.QualifiedItemId.Equals("(O)787"))) + + if (farmer.Items.Any(b => b != null && b.QualifiedItemId.Equals("(O)787"))) { - float oldStamina = farmer.stamina; farmer.stamina = Math.Min(Farmer.startingStamina, farmer.stamina + 100); - ModEntry.instance.Monitor.Log($"UseBattery called: Stamina increased from {oldStamina} to {farmer.stamina}", LogLevel.Trace); farmer.removeFirstOfThisItemFromInventory("(O)787"); return true; } From ea72b7f09eb8c76681a84a41f151a56d8cb98b08 Mon Sep 17 00:00:00 2001 From: arannya Date: Sat, 11 May 2024 20:54:40 +0600 Subject: [PATCH 5/8] Revert "[Fix] Minor syntax change." This reverts commit f846c7bbbce8bb01cb847144d6ebe844222b6392. --- Farmtronics/Bot/BotObject.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Farmtronics/Bot/BotObject.cs b/Farmtronics/Bot/BotObject.cs index 368569d..c1f9831 100644 --- a/Farmtronics/Bot/BotObject.cs +++ b/Farmtronics/Bot/BotObject.cs @@ -170,7 +170,7 @@ public bool UseBattery() { if (farmer == null || inventory == null || farmer.CurrentTool == null) return false; - if (farmer.Items.Any(b => b != null && b.QualifiedItemId.Equals("(O)787"))) + if (farmer.Items.Any(b => b is not null && b.QualifiedItemId.Equals("(O)787"))) { farmer.stamina = Math.Min(Farmer.startingStamina, farmer.stamina + 100); farmer.removeFirstOfThisItemFromInventory("(O)787"); From 80bd3245e61fc6c271ff74287e05db115017f190 Mon Sep 17 00:00:00 2001 From: arannya Date: Sat, 11 May 2024 20:57:32 +0600 Subject: [PATCH 6/8] Revert "[Enhancement] Addresses recharging bots with batteries: issue #25" This reverts commit 17d15e0c991d47be1083eaab3c749cdbf6f9024f. --- Farmtronics/Bot/BotObject.cs | 15 --------------- Farmtronics/M1/M1API.cs | 10 ---------- 2 files changed, 25 deletions(-) diff --git a/Farmtronics/Bot/BotObject.cs b/Farmtronics/Bot/BotObject.cs index c1f9831..ee2ef7d 100644 --- a/Farmtronics/Bot/BotObject.cs +++ b/Farmtronics/Bot/BotObject.cs @@ -5,8 +5,6 @@ This class is a stardew valley Object subclass that represents a Bot. using System; using System.Collections.Generic; using System.IO; -using System.Linq; -using System.Threading; using Farmtronics.M1; using Farmtronics.Utils; using Microsoft.Xna.Framework; @@ -165,20 +163,7 @@ public override bool placementAction(GameLocation location, int x, int y, Farmer location.playSound("hammer"); return true; } - - public bool UseBattery() { - if (farmer == null || inventory == null || farmer.CurrentTool == null) - return false; - if (farmer.Items.Any(b => b is not null && b.QualifiedItemId.Equals("(O)787"))) - { - farmer.stamina = Math.Min(Farmer.startingStamina, farmer.stamina + 100); - farmer.removeFirstOfThisItemFromInventory("(O)787"); - return true; - } - return false; - } - // Apply the currently-selected item as a tool (or weapon) on // the square in front of the bot. public void UseTool() { diff --git a/Farmtronics/M1/M1API.cs b/Farmtronics/M1/M1API.cs index 5f0d227..3e1a4fc 100644 --- a/Farmtronics/M1/M1API.cs +++ b/Farmtronics/M1/M1API.cs @@ -720,16 +720,6 @@ public static ValMap MeModule() { return result ? Intrinsic.Result.True : Intrinsic.Result.False; }; meModule["harvest"] = f.GetFunc(); - - f = Intrinsic.Create(""); - f.code = (context, partialResult) => { - Shell sh = context.interpreter.hostData as Shell; - if (RequireBot(sh, "useBattery")) return Intrinsic.Result.Null; - - bool result = sh.bot.UseBattery(); - return result ? Intrinsic.Result.True : Intrinsic.Result.False; - }; - meModule["useBattery"] = f.GetFunc(); botProtectedKeys = new HashSet(); foreach (Value key in meModule.Keys) { From 22e470f1d57ffe6550fb488aa477e4262e7ffe7a Mon Sep 17 00:00:00 2001 From: arannya Date: Sat, 11 May 2024 20:59:24 +0600 Subject: [PATCH 7/8] [Feature] Added support for consuming batteries with useTool. --- Farmtronics/Bot/BotObject.cs | 18 ++++++++++++++++-- Farmtronics/Farmtronics.csproj | 3 +++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/Farmtronics/Bot/BotObject.cs b/Farmtronics/Bot/BotObject.cs index ee2ef7d..7824dbb 100644 --- a/Farmtronics/Bot/BotObject.cs +++ b/Farmtronics/Bot/BotObject.cs @@ -167,6 +167,20 @@ public override bool placementAction(GameLocation location, int x, int y, Farmer // Apply the currently-selected item as a tool (or weapon) on // the square in front of the bot. public void UseTool() { + float oldStamina = farmer.stamina; + if (farmer.CurrentTool == null && + farmer.Items[farmer.CurrentToolIndex].QualifiedItemId.Equals("(O)787")) + { + if (farmer.stamina >= Farmer.startingStamina) { + ModEntry.instance.Monitor.Log($"UseBattery called: Stamina already full, battery not used.", LogLevel.Trace); + return; + } + farmer.stamina = Math.Min(Farmer.startingStamina, farmer.stamina + 100); + ModEntry.instance.Monitor.Log($"UseBattery called: Stamina increased from {oldStamina} to {farmer.stamina}", LogLevel.Trace); + farmer.removeFirstOfThisItemFromInventory("(O)787"); + return; + } + if (farmer == null || inventory == null || farmer.CurrentTool == null) return; Vector2 toolLocation = farmer.GetToolLocation(true); ModEntry.instance.Monitor.Log($"UseTool called: {farmer.CurrentTool.Name}[{farmer.CurrentToolIndex}] {toolLocation}", LogLevel.Trace); @@ -178,13 +192,13 @@ public void UseTool() { return; } - float oldStamina = farmer.stamina; if (farmer.CurrentTool is not MeleeWeapon) { ModEntry.instance.Monitor.Log($"farmer.CurrentTool.DoFunction", LogLevel.Trace); farmer.CurrentTool.DoFunction(farmer.currentLocation, toolLocation.GetIntX(), toolLocation.GetIntY(), 1, farmer); farmer.checkForExhaustion(oldStamina); data.Update(); - } else { + } + else { // Special case for using the Scythe farmer.CurrentTool.beginUsing(currentLocation, toolLocation.GetIntX(), toolLocation.GetIntY(), farmer); Farmer.showToolSwipeEffect(farmer); diff --git a/Farmtronics/Farmtronics.csproj b/Farmtronics/Farmtronics.csproj index a53f8ed..f2853cc 100644 --- a/Farmtronics/Farmtronics.csproj +++ b/Farmtronics/Farmtronics.csproj @@ -6,6 +6,9 @@ Farmtronics ThirdParty 1.3 + /Users/arannyamonzur/Library/Application Support/Steam/steamapps/common/Stardew Valley/Contents/MacOS/ + /Users/arannyamonzur/Library/Application Support/Steam/steamapps/common/Stardew Valley/Contents/MacOS/StardewModdingAPI + -mode dryrun From b263c9255bb79893de6d2535f2796237e6bd7ef5 Mon Sep 17 00:00:00 2001 From: arannya Date: Sat, 11 May 2024 21:13:28 +0600 Subject: [PATCH 8/8] Revert .csproj modification --- Farmtronics/Farmtronics.csproj | 3 --- 1 file changed, 3 deletions(-) diff --git a/Farmtronics/Farmtronics.csproj b/Farmtronics/Farmtronics.csproj index f2853cc..a53f8ed 100644 --- a/Farmtronics/Farmtronics.csproj +++ b/Farmtronics/Farmtronics.csproj @@ -6,9 +6,6 @@ Farmtronics ThirdParty 1.3 - /Users/arannyamonzur/Library/Application Support/Steam/steamapps/common/Stardew Valley/Contents/MacOS/ - /Users/arannyamonzur/Library/Application Support/Steam/steamapps/common/Stardew Valley/Contents/MacOS/StardewModdingAPI - -mode dryrun