Jump to content

checking if bot should pick up items on certain tile


Aqla

Recommended Posts

Hello all.

I'm writing a fighter bot that picks up loot from the tile where the last target died. I was wondering if someone could point me to a more efficient and clean way to write the for loops that check for relevant items on the tile and picks them up (see below).

Looking forward to learn more.

public void lootingLastKilled() {
        
        List<GroundItem> itemsOnTile = getGroundItems().get(targetTile.getX(), targetTile.getY());
        List<String> itemsToLoot = Stream.of("Feather","Bones").collect(Collectors.toList());

  		// Check for items on target tile that match items the bot should loot
        for (int i = 0; i < itemsOnTile.size(); i++) {
            for (int j = 0; j < itemsToLoot.size(); j++) {
                if (itemsOnTile.get(i).getName().equals(itemsToLoot.get(j))) {
                  	// Pick up ground item that matches items to loot
                    GroundItem itemToPickUp = itemsOnTile.get(i);
                    itemToPickUp.interact("Take");

                    (new ConditionalSleep(3000) {
                        public boolean condition() throws InterruptedException {
                            return myPlayer().isAnimating();
                        }
                    }).sleep();
                }
            }
        }
    }
Edited by Aqla
  • Like 1
Link to comment
Share on other sites

private boolean lootItems(Position targetPosition) {
  List<String> itemsToLoot = Stream.of("Feather", "Bones").collect(Collectors.toList());    
  GroundItem itemToLoot = getGroundItems().closest(item -> item.getPosition().equals(targetPosition) && itemsToLoot.contains(item.getName()));

  if (itemToLoot != null) {
     if (itemToLoot.interact("Take")) {
         Timing.waitCondition(() -> !itemToLoot.exists(), 5_000);
      }
   return true;
   }
return false;
}

Pick them up 1 by 1, returns true when loot is found 😉

Edited by Khaleesi
  • Heart 1
Link to comment
Share on other sites

8 hours ago, Aqla said:

Hello all.

I'm writing a fighter bot that picks up loot from the tile where the last target died. I was wondering if someone could point me to a more efficient and clean way to write the for loops that check for relevant items on the tile and picks them up (see below).

Looking forward to learn more.

List<String> itemsToLoot = Stream.of("Feather", "Bones").collect(Collectors.toList());
        // Use streams to filter out anything not in our itemsToLoot and create a new collection.
        List<GroundItem> itemsOnTile = getGroundItems().get(targetTile.getX(), targetTile.getY()).stream()
                .filter(g -> g != null && itemsToLoot.contains(g.getName()))
                .collect(Collectors.toList());

        // Check if there is anything available.
        if (!itemsOnTile.isEmpty()) {
            // We have items on this tile matching our filter.
            // Start looting. (Below is just a for each loop)
            for (GroundItem groundItem : itemsOnTile) {
                // Always null check when dealing with the OSBot api it will save you in the long run.
                if (groundItem != null && groundItem.interact("Take")) {
                    new ConditionalSleep(3000) {
                        @Override
                        public boolean condition() throws InterruptedException {
                            // item doesnt exist OR our players position is on the current tile (faster looting ig)
                            return !groundItem.exists() || myPosition().equals(groundItem.getPosition());
                        }
                    }.sleep();
                }
            }
        }

Added some comments but lmk if u need to understand something

7yMKTva.png

  • Like 1
  • Heart 1
Link to comment
Share on other sites

17 hours ago, Khaleesi said:

Pick them up 1 by 1, returns true when loot is found 😉

Thank you, Khal!

 

10 hours ago, Chris said:

Added some comments but lmk if u need to understand something

7yMKTva.png

Thank you so much, Chris! This helped me a great deal and taught me a lot. One step closer to writing great scripts. Really appreciate you putting time into the comments and syntax clarification.

  • Like 2
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...