Jump to content

how to filter: walking to npcs only when players arent near them


roguehippo

Recommended Posts

Using filters!

 

Two ways:

 

 

1.

        getNpcs().filter(new Filter<NPC>() {
            @[member='Override']
            public boolean match(NPC obj) {
                return obj.getPosition().distance(myPosition()) > 2;
            }
        });
        

 

2.

        java.util.List<NPC> npcs = getNpcs().getAll().stream().filter(n -> n.getPosition().distance(myPosition()) > 2).collect(Collectors.toList())
       
 

 

Edit:

Then you can use a comparator:

        npcs.sort(new Comparator<NPC>() {
            public int compare(NPC npc1, NPC npc2) {
                return npc1.getPosition().distance(myPosition()) - npc2.getPosition().distance(myPosition());
            }
        });

So this will return the closest npc which is more than 2 tiles away.

  • Like 2
Link to comment
Share on other sites

Hello :) I was just wondering how i would implement a filter to get the closest npc of a certain type but to make sure it is not ~2 squares away from the npc so that im not crashing someone else's spot as that would look quite botty.

Optional<NPC> npcOpt = getNpcs().getAll()
                        .stream()
                        .filter(n -> n.getName().equals("NPC Name") &&
                                     n.getPosition().distance(myPosition()) > 2)
                        .min((n1, n2) -> Integer.compare(n1.getPosition().distance(myPosition()),
                                                       ​n2.getPosition().distance(myPosition())));

Explanation:

 

  1. Construct a Stream<NPC> of all the NPCs around the Player
  2. Filter the Stream<NPC> so that only the NPCs who's name matches "NPC Name" and is more than two tiles away from the player remain
  3. Apply the min function to the Stream<NPC> using a custom comparator that compares distances from the player (essentially returns the closest NPC to the player)
  4. Returns an Optional<NPC>

 

With the return value of Optional<NPC>, instead of null checking you do something like:

npcOpt.ifPresent(npc -> {

});

For more information on the Optional class see https://docs.oracle.com/javase/8/docs/api/java/util/Optional.html

 

For more information on streams see http://www.oracle.com/technetwork/articles/java/ma14-java-se-8-streams-2177646.html and https://docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html

Edited by Explv
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...