Skip to content

Commit

Permalink
Merge pull request #100 from Eureka-dot-net/Feature/pickup-debris
Browse files Browse the repository at this point in the history
Feature/pickup debris
  • Loading branch information
JoeStrout authored Nov 7, 2024
2 parents f59878a + b1326b1 commit 8394b2f
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
43 changes: 43 additions & 0 deletions Farmtronics/Bot/BotObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down
14 changes: 14 additions & 0 deletions Farmtronics/M1/M1API.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>();
foreach (Value key in meModule.Keys) {
botProtectedKeys.Add(key.ToString());
Expand Down

0 comments on commit 8394b2f

Please sign in to comment.