From 17d15e0c991d47be1083eaab3c749cdbf6f9024f Mon Sep 17 00:00:00 2001 From: arannya Date: Wed, 1 May 2024 09:29:49 +0600 Subject: [PATCH] [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) {