diff --git a/Farmtronics/Bot/BotObject.cs b/Farmtronics/Bot/BotObject.cs index 43d3796..fb1a0f8 100644 --- a/Farmtronics/Bot/BotObject.cs +++ b/Farmtronics/Bot/BotObject.cs @@ -51,6 +51,8 @@ public int currentToolIndex { } } + public bool shouldPickupDebris { get; set; } + internal Vector2 Position { get =>farmer.Position; set => farmer.Position = value; } // our current position, in pixels private Vector2 targetPos; // position we're moving to, in pixels private int scytheUseFrame = 0; // > 0 when using the scythe @@ -664,6 +666,47 @@ public void Update(GameTime gameTime) { } farmer.Update(gameTime, farmer.currentLocation); + if (shouldPickupDebris) PickUpDebris(farmer, gameTime); + } + + public void PickUpDebris(Farmtronics.Bot.BotFarmer farmer, GameTime gameTime) { + GameLocation loc = farmer.currentLocation; + int range = 128; // Same as default magnetism of player + float moveSpeed = 400f; // Speed at which debris moves toward the bot + + for (int i = loc.debris.Count - 1; i >= 0; i--) { + Debris d = loc.debris[i]; + + if (d == null || string.IsNullOrEmpty(d.itemId) || d.timeSinceDoneBouncing <= 0) + continue; // Skip null or invalid debris + + Item item = ItemRegistry.Create(d.itemId, 1, d.itemQuality); + + if (item == null || !farmer.couldInventoryAcceptThisItem(item)) + continue; // Skip if item is null or farmer can't accept it + + Vector2 debrisPosition = d.Chunks[0].position.Value; + float distance = Vector2.Distance(debrisPosition, Position); + + if (distance < range) { + // Move each chunk of debris toward the bot + foreach (var chunk in d.Chunks) { + Vector2 currentChunkPosition = chunk.position.Value; + Vector2 direction = (Position - currentChunkPosition); + direction.Normalize(); + + // Move debris towards the bot + chunk.position.Value += direction * moveSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds; + } + + // If debris is close enough, collect it + if (distance < 10f) { + item.Stack = d.Chunks.Count; + Item itemAdded = farmer.addItemToInventory(item); + loc.debris.RemoveAt(i); // Remove debris once collected + } + } + } } public override bool checkForAction(Farmer who, bool justCheckingForActivity = false) { diff --git a/Farmtronics/M1/M1API.cs b/Farmtronics/M1/M1API.cs index 3e1a4fc..955e7a3 100644 --- a/Farmtronics/M1/M1API.cs +++ b/Farmtronics/M1/M1API.cs @@ -721,6 +721,20 @@ public static ValMap MeModule() { }; meModule["harvest"] = f.GetFunc(); + f = Intrinsic.Create(""); + f.AddParam("shouldCollect", 0); // Default to false + f.code = (context, partialResult) => { + Shell sh = context.interpreter.hostData as Shell; + if (RequireBot(sh, "collect")) return Intrinsic.Result.Null; + + // Get the 'shouldCollect' parameter and set it to the bot's property + bool shouldCollect = context.GetLocalBool("shouldCollect"); + sh.bot.shouldPickupDebris = shouldCollect; + + return Intrinsic.Result.Null; + }; + meModule["collect"] = f.GetFunc(); + botProtectedKeys = new HashSet(); foreach (Value key in meModule.Keys) { botProtectedKeys.Add(key.ToString());