Skip to content

Commit 8394b2f

Browse files
authored
Merge pull request #100 from Eureka-dot-net/Feature/pickup-debris
Feature/pickup debris
2 parents f59878a + b1326b1 commit 8394b2f

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

Farmtronics/Bot/BotObject.cs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ public int currentToolIndex {
5151
}
5252
}
5353

54+
public bool shouldPickupDebris { get; set; }
55+
5456
internal Vector2 Position { get =>farmer.Position; set => farmer.Position = value; } // our current position, in pixels
5557
private Vector2 targetPos; // position we're moving to, in pixels
5658
private int scytheUseFrame = 0; // > 0 when using the scythe
@@ -664,6 +666,47 @@ public void Update(GameTime gameTime) {
664666
}
665667

666668
farmer.Update(gameTime, farmer.currentLocation);
669+
if (shouldPickupDebris) PickUpDebris(farmer, gameTime);
670+
}
671+
672+
public void PickUpDebris(Farmtronics.Bot.BotFarmer farmer, GameTime gameTime) {
673+
GameLocation loc = farmer.currentLocation;
674+
int range = 128; // Same as default magnetism of player
675+
float moveSpeed = 400f; // Speed at which debris moves toward the bot
676+
677+
for (int i = loc.debris.Count - 1; i >= 0; i--) {
678+
Debris d = loc.debris[i];
679+
680+
if (d == null || string.IsNullOrEmpty(d.itemId) || d.timeSinceDoneBouncing <= 0)
681+
continue; // Skip null or invalid debris
682+
683+
Item item = ItemRegistry.Create(d.itemId, 1, d.itemQuality);
684+
685+
if (item == null || !farmer.couldInventoryAcceptThisItem(item))
686+
continue; // Skip if item is null or farmer can't accept it
687+
688+
Vector2 debrisPosition = d.Chunks[0].position.Value;
689+
float distance = Vector2.Distance(debrisPosition, Position);
690+
691+
if (distance < range) {
692+
// Move each chunk of debris toward the bot
693+
foreach (var chunk in d.Chunks) {
694+
Vector2 currentChunkPosition = chunk.position.Value;
695+
Vector2 direction = (Position - currentChunkPosition);
696+
direction.Normalize();
697+
698+
// Move debris towards the bot
699+
chunk.position.Value += direction * moveSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;
700+
}
701+
702+
// If debris is close enough, collect it
703+
if (distance < 10f) {
704+
item.Stack = d.Chunks.Count;
705+
Item itemAdded = farmer.addItemToInventory(item);
706+
loc.debris.RemoveAt(i); // Remove debris once collected
707+
}
708+
}
709+
}
667710
}
668711

669712
public override bool checkForAction(Farmer who, bool justCheckingForActivity = false) {

Farmtronics/M1/M1API.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -721,6 +721,20 @@ public static ValMap MeModule() {
721721
};
722722
meModule["harvest"] = f.GetFunc();
723723

724+
f = Intrinsic.Create("");
725+
f.AddParam("shouldCollect", 0); // Default to false
726+
f.code = (context, partialResult) => {
727+
Shell sh = context.interpreter.hostData as Shell;
728+
if (RequireBot(sh, "collect")) return Intrinsic.Result.Null;
729+
730+
// Get the 'shouldCollect' parameter and set it to the bot's property
731+
bool shouldCollect = context.GetLocalBool("shouldCollect");
732+
sh.bot.shouldPickupDebris = shouldCollect;
733+
734+
return Intrinsic.Result.Null;
735+
};
736+
meModule["collect"] = f.GetFunc();
737+
724738
botProtectedKeys = new HashSet<string>();
725739
foreach (Value key in meModule.Keys) {
726740
botProtectedKeys.Add(key.ToString());

0 commit comments

Comments
 (0)