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

Discussion in 'Tutorials & Resources' started by Cloud, Jun 8, 2014.

  1. Anyone who has ever tried to write a script bot 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:
    Code (Java):
    1.  
    2. LocarableQueryResults<GameObject> results = GameObjects.getLoaded("Tree").sortByDistance();
    3. if(results.size()>=2){
    4. final GameObject secondNearestTree=results.get(1);
    5. }
    6.  
    Code (Java):
    1.  
    2. List<GameObject> trees = Sort.byDistance(GameObjects.getLoaded("Tree"));
    3. if (trees.size() >= 2) {
    4.      final GameObject secondNearestTree = trees.get(1);
    5. }
    6.  
    Code (Java):
    1.  
    2. GameObjects.getLoaded("Man").sortByDistance().get(1);
    3.  
    The same can be done with GroundItems, Projectiles, Players, and any other type of Locatable.
     
    #1 Cloud, Jun 8, 2014
    Last edited: Oct 3, 2014
    Sh4dows likes this.
  2. Definetely made it easier, nice one :D
     
  3. 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 (Text):
    1.  
    2. final Set<GameObject> unsorted = GameObjects.getLoaded();
    3. final Comparator<GameObject> cmp = new Comparator<GameObject>() {
    4.      @Override
    5.      public int compare(GameObject o1, GameObject o2) {
    6.           return Integer.compare(o1.getId(), o2.getId());
    7.      }
    8.   };
    9. final List<GameObject> sorted = new ArrayList<GameObject>(unsorted);
    10. Collections.sort(sorted, cmp);
    11.  
     
  4. ^- which sorts game objects based on id
     
  5. Correct. One would have to customize the return statement for the compare function to sort by another parameter. More info here.
     
  6. GameObjects.getLoaded("Man").sortByDistance().get(1);
     
  7. This was written before that was available :p
     
  8. You should also add the way to do this using queries @Cloud
     
  9. done
     
  10. @Cloud for the above.

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

    Code (Java):
    1. private boolean hoverNextRock() {
    2.         LocatableEntityQueryResults<GameObject> rocks = GameObjects.getLoaded(new Filter<GameObject>() {
    3.             @Override
    4.             public boolean accepts(GameObject gameObject) {
    5.                 return LazyAIOMiner.oreObjectIds.contains(gameObject.getId()) && LazyAIOMiner.mineArea.contains(gameObject);
    6.             }
    7.         });
    8.  
    9.         GameObject firstRock = rocks.nearestTo(Players.getLocal());
    10.         GameObject nextRock = rocks.sortByDistance().limit(2).nearestTo(Players.getLocal());
    11.         LazyAIOMiner.status = "Hovering next rock";
    12.         return nextRock != null && !nextRock.equals(firstRock) && nextRock.hover();
    13.     }
    In the above example I want to hover over the other rock, so not the one I am currently mining.
     
  11. Player sense takes care of that for you :)
     
  12. Are you sure? It has to hover over the other rock...
     
  13. something along the lines

    Code (Text):
    1.  
    2. GameObject nextTree;
    3.            
    4.             if (nextTree == null || !nextTree.isValid()) {
    5.                 nextTree = GameObjects.newQuery().names("trees").visible().results().sortByDistance().limit(1, 2).random();
    6.             }
    7.             if (nextTree != null) {
    8.                 nextTree.hover();
    9.             }
     
    Geashaw likes this.
  14. If you have 2 rocks with same distance, pick one randomly, or pick one that you're facing (if that is the case)
     
  15. 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.
     
  16. So say you are mining here;

    [​IMG]

    @Cloud How would I decide which one to hover when mining the other one =)
     
  17. Well you know which rock you're facing (Aka mining) so just get the other one.
     
    Geashaw likes this.

Share This Page

Loading...