Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

Sign up now!

Resource [Sorting] Getting the 2nd nearest rock/tree/man/stall/etc for hovering and other tasks

Engineer
Joined
Jul 28, 2013
Messages
2,776
Anyone who has ever tried to write a script that hovers the next object while waiting for the current task to be completed has probably noticed that most APIs make it relatively difficult to get the second object.

In an effort to make this task simpler I have added a Sort class to the API.

The following are examples of how to get the second nearest tree and man, but the concept can be expanded to get the third, fourth, fifth, and so on.

Edit: There are now two ways to easily do this:
Edit2: Now there are three:
Java:
LocarableQueryResults<GameObject> results = GameObjects.getLoaded("Tree").sortByDistance();
if(results.size()>=2){
final GameObject secondNearestTree=results.get(1);
}
Java:
List<GameObject> trees = Sort.byDistance(GameObjects.getLoaded("Tree"));
if (trees.size() >= 2) {
     final GameObject secondNearestTree = trees.get(1);
}
Java:
GameObjects.getLoaded("Man").sortByDistance().get(1);
The same can be done with GroundItems, Projectiles, Players, and any other type of Locatable.
 
Last edited:
Mod Automation
Joined
Jul 26, 2013
Messages
3,046
Keep in mind that the Sort#byDistance is just a convenience method. For a more generic and universal way of sorting an arbitrary List<E> use Comparator.

Example:
Code:
final Set<GameObject> unsorted = GameObjects.getLoaded();
final Comparator<GameObject> cmp = new Comparator<GameObject>() {
     @Override
     public int compare(GameObject o1, GameObject o2) {
          return Integer.compare(o1.getId(), o2.getId());
     }
  };
final List<GameObject> sorted = new ArrayList<GameObject>(unsorted);
Collections.sort(sorted, cmp);
 
Joined
Jan 8, 2015
Messages
1,427
@Cloud for the above.

How would one write this nearest object when there are two at the same distance?

Java:
private boolean hoverNextRock() {
        LocatableEntityQueryResults<GameObject> rocks = GameObjects.getLoaded(new Filter<GameObject>() {
            @Override
            public boolean accepts(GameObject gameObject) {
                return LazyAIOMiner.oreObjectIds.contains(gameObject.getId()) && LazyAIOMiner.mineArea.contains(gameObject);
            }
        });

        GameObject firstRock = rocks.nearestTo(Players.getLocal());
        GameObject nextRock = rocks.sortByDistance().limit(2).nearestTo(Players.getLocal());
        LazyAIOMiner.status = "Hovering next rock";
        return nextRock != null && !nextRock.equals(firstRock) && nextRock.hover();
    }

In the above example I want to hover over the other rock, so not the one I am currently mining.
 
The Pip Collector
Joined
Sep 14, 2014
Messages
445
@Cloud for the above.

How would one write this nearest object when there are two at the same distance?

Java:
private boolean hoverNextRock() {
        LocatableEntityQueryResults<GameObject> rocks = GameObjects.getLoaded(new Filter<GameObject>() {
            @Override
            public boolean accepts(GameObject gameObject) {
                return LazyAIOMiner.oreObjectIds.contains(gameObject.getId()) && LazyAIOMiner.mineArea.contains(gameObject);
            }
        });

        GameObject firstRock = rocks.nearestTo(Players.getLocal());
        GameObject nextRock = rocks.sortByDistance().limit(2).nearestTo(Players.getLocal());
        LazyAIOMiner.status = "Hovering next rock";
        return nextRock != null && !nextRock.equals(firstRock) && nextRock.hover();
    }

In the above example I want to hover over the other rock, so not the one I am currently mining.
Player sense takes care of that for you :)
 
Joined
Jan 1, 2015
Messages
272
something along the lines

Code:
GameObject nextTree;
           
            if (nextTree == null || !nextTree.isValid()) {
                nextTree = GameObjects.newQuery().names("trees").visible().results().sortByDistance().limit(1, 2).random();
            }
            if (nextTree != null) {
                nextTree.hover();
            }
 
Engineer
Joined
Jul 28, 2013
Messages
2,776
If you have 2 rocks with same distance, pick one randomly, or pick one that you're facing (if that is the case)
If you have two rocks with the same distance you should just use nearest because that will perform a much better decision making process than what the majority of bot authors would write.
 
Joined
Jan 8, 2015
Messages
1,427
If you have two rocks with the same distance you should just use nearest because that will perform a much better decision making process than what the majority of bot authors would write.

So say you are mining here;

b40ba7affd.png


@Cloud How would I decide which one to hover when mining the other one =)
 
Top