1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

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. Cloud

    Cloud Engineer

    Joined:
    Jul 28, 2013
    Messages:
    2,777
    Likes Received:
    1,124
    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
    xCookie and Sh4dows like this.
  2. Viewer

    Viewer Discretion is advised

    Joined:
    Jan 2, 2014
    Messages:
    306
    Likes Received:
    77
    Definetely made it easier, nice one :D
     
    LucasSousa likes this.
  3. Arbiter

    Arbiter Mod Automation

    Joined:
    Jul 26, 2013
    Messages:
    2,937
    Likes Received:
    1,266
    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. Salvation

    Salvation First Bot Author

    Joined:
    Aug 7, 2013
    Messages:
    262
    Likes Received:
    68
    ^- which sorts game objects based on id
     
  5. Arbiter

    Arbiter Mod Automation

    Joined:
    Jul 26, 2013
    Messages:
    2,937
    Likes Received:
    1,266
    Correct. One would have to customize the return statement for the compare function to sort by another parameter. More info here.
     
  6. frazboyz

    Joined:
    Nov 15, 2013
    Messages:
    339
    Likes Received:
    56
    GameObjects.getLoaded("Man").sortByDistance().get(1);
     
  7. Cloud

    Cloud Engineer

    Joined:
    Jul 28, 2013
    Messages:
    2,777
    Likes Received:
    1,124
    This was written before that was available :p
     
  8. Aidden

    Aidden Author of MaxiBots

    Joined:
    Dec 3, 2013
    Messages:
    6,482
    Likes Received:
    990
    You should also add the way to do this using queries @Cloud
     
  9. Cloud

    Cloud Engineer

    Joined:
    Jul 28, 2013
    Messages:
    2,777
    Likes Received:
    1,124
    done
     
  10. Geashaw

    Joined:
    Jan 8, 2015
    Messages:
    1,429
    Likes Received:
    252
    @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. Infinite Inferno

    Infinite Inferno The Pip Collector

    Joined:
    Sep 14, 2014
    Messages:
    445
    Likes Received:
    122
    Player sense takes care of that for you :)
     
  12. Geashaw

    Joined:
    Jan 8, 2015
    Messages:
    1,429
    Likes Received:
    252
    Are you sure? It has to hover over the other rock...
     
  13. PhaseCoder

    Joined:
    Jan 1, 2015
    Messages:
    272
    Likes Received:
    26
    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. Viewer

    Viewer Discretion is advised

    Joined:
    Jan 2, 2014
    Messages:
    306
    Likes Received:
    77
    If you have 2 rocks with same distance, pick one randomly, or pick one that you're facing (if that is the case)
     
  15. Cloud

    Cloud Engineer

    Joined:
    Jul 28, 2013
    Messages:
    2,777
    Likes Received:
    1,124
    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. Geashaw

    Joined:
    Jan 8, 2015
    Messages:
    1,429
    Likes Received:
    252
    So say you are mining here;

    [​IMG]

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

    Cloud Engineer

    Joined:
    Jul 28, 2013
    Messages:
    2,777
    Likes Received:
    1,124
    Well you know which rock you're facing (Aka mining) so just get the other one.
     
    Geashaw likes this.

Share This Page

Loading...