Jump to content

Getting the closest NPC for name and action (OSBot 2)


Botre

Recommended Posts

Kenneh wrote a version that put mine to shame, so I'll just put his one on here for now ;)

 

Kenneh, on 11 May 2014 - 12:54 AM, said:snapback.png

 

excuse syntax errors, I don't have the client downloaded.

private NPC getClosestNPC(String name, String action) { // not as pretty but less heavy method
        NPC closest = null;
        double lowest = Double.MAX_VALUE;
        
        for(NPC npc : getNpcs().getAll()) {
            final List<String> actions = Arrays.asList(npc.getDefinition().getActions());
            final double distance = npc.getPosition().distance(myPosition());
            if(actions.contains(action) && distance < lowest) {
                closest = npc;
                lowest = distance;
            }
        }
        return closest;
    }
    
    
    private NPC closestNPC(final String name, final String action) { // much prettier but more resourceful.
        final Filter<NPC> filter = new Filter<NPC>() {
            @Override
            public boolean accept(NPC npc) {
                return Arrays.asList(npc.getDefinition().getActions()).contains(action);
            }
        };
        
        final List<NPC> filtered = new ArrayList<>();
        for(NPC npc : getNpcs().getAll()) {
            if(filter.accept(npc))
                filtered.add(npc);
        }
        
        final Comparator<NPC> comparator = new Comparator<NPC>() {
            @Override
            public int compare(NPC o1, NPC o2) {
                return o1.getPosition().distance(myPosition()) - o2.getPosition().distance(myPosition());
            }
        };
        Collections.sort(filtered, comparator);
        
        return filtered.size() > 0 ? filtered.get(0) : null;
    }
Edited by Botrepreneur
Link to comment
Share on other sites

for (NPC currentNPC : getNpcs()) {
    //iterate over each npc
}

I'm assuming #getNpcs() would return an implementation of List or at the very minimal an Array...

 

Of course I tried that one first ><

 

But...

 

"Can only iterate over an array or an instance of java.lang.Iterable"

Edited by Botrepreneur
Link to comment
Share on other sites

Use the API to your advantage, they have filters for a reason. biggrin.png

 

Maybe something like this:

npcs.closest(new BestMatch(this));

	public boolean match(NPC subject) {
			return subject.getName().equals(name) &&
				Arrays.asList(subject.getDefinition().getActions()).contains(action); 

	}
Edited by Eliot
Link to comment
Share on other sites

 

Use the API to your advantage.

 

Maybe something like this:

npcs.closest(BestMatch());

	public boolean match(NPC subject) {
				return subject.getName().equals(name) &&
				Arrays.asList(subject.getDefinition().getActions()).contains(action) 
}
	}

 

npcs.closest() doesn't reliably return the closest NPC in terms of distance :(

Edited by Botrepreneur
Link to comment
Share on other sites

Are you sure, I've used it quite a bit and it always seems to pick a close target. Picking the closest 100% of the time isn't usually the best choice.

 

I am, agreed that in combat situations (picking a target) getting the closest 100% of the time may not be the best choice in terms of detectability and whatnot.

 

But try getting the closest banker at Varock west bank with the API stock method, it will always return the same banker (the most southern one) no matter where you are standing in the bank.

Edited by Botrepreneur
  • Like 1
Link to comment
Share on other sites

Quick look at the v2 api would show that you need to do

 

for(NPC npc : getNpcs().getAll()) {

 

}

 

http://osbot.org/osbot2_api/org/osbot/rs07/api/NPCS.html#getAll()

 

Isn't that what my iterator does?

Edit: I would really like to know what exactly is wrong with it considering it "gave you cancer" tongue.png

Edited by Botrepreneur
Link to comment
Share on other sites

Isn't that what my iterator does?

Edit: I would really like to know what exactly is wrong with it considering it "gave you cancer" tongue.png

 

excuse syntax errors, I don't have the client downloaded.

 

private NPC getClosestNPC(String name, String action) { // not as pretty but less heavy method
        NPC closest = null;
        double lowest = Double.MAX_VALUE;
        
        for(NPC npc : getNpcs().getAll()) {
            final List<String> actions = Arrays.asList(npc.getDefinition().getActions());
            final double distance = npc.getPosition().distance(myPosition());
            if(actions.contains(action) && distance < lowest) {
                closest = npc;
                lowest = distance;
            }
        }
        return closest;
    }
    
    
    private NPC closestNPC(final String name, final String action) { // much prettier but more resourceful.
        final Filter<NPC> filter = new Filter<NPC>() {
            @Override
            public boolean accept(NPC npc) {
                return Arrays.asList(npc.getDefinition().getActions()).contains(action);
            }
        };
        
        final List<NPC> filtered = new ArrayList<>();
        for(NPC npc : getNpcs().getAll()) {
            if(filter.accept(npc))
                filtered.add(npc);
        }
        
        final Comparator<NPC> comparator = new Comparator<NPC>() {
            @Override
            public int compare(NPC o1, NPC o2) {
                return o1.getPosition().distance(myPosition()) - o2.getPosition().distance(myPosition());
            }
        };
        Collections.sort(filtered, comparator);
        
        return filtered.size() > 0 ? filtered.get(0) : null;
    }
  • Like 1
Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
  • Recently Browsing   0 members

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