Botre Posted May 10, 2014 Share Posted May 10, 2014 (edited) 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: 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 May 19, 2014 by Botrepreneur Link to comment Share on other sites More sharing options...
Apaec Posted May 10, 2014 Share Posted May 10, 2014 cool Link to comment Share on other sites More sharing options...
Swizzbeat Posted May 10, 2014 Share Posted May 10, 2014 Why do you create an iterator object? The enhanced for loop creates an implicit Iterator by default. Link to comment Share on other sites More sharing options...
Kenneh Posted May 10, 2014 Share Posted May 10, 2014 This post literally gave me cancer. 1 Link to comment Share on other sites More sharing options...
zScripz Posted May 10, 2014 Share Posted May 10, 2014 dear god what am I reading Link to comment Share on other sites More sharing options...
Botre Posted May 10, 2014 Author Share Posted May 10, 2014 (edited) Why do you create an iterator object? The enhanced for loop creates an implicit Iterator by default. vs Are the same right ? This post literally gave me cancer. dear god what am I reading Very constructive. Care to elaborate maybe? Edited May 10, 2014 by Botrepreneur Link to comment Share on other sites More sharing options...
Swizzbeat Posted May 10, 2014 Share Posted May 10, 2014 vs Is the same right ? Wait what? 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... Link to comment Share on other sites More sharing options...
Botre Posted May 10, 2014 Author Share Posted May 10, 2014 (edited) 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 May 10, 2014 by Botrepreneur Link to comment Share on other sites More sharing options...
Eliot Posted May 10, 2014 Share Posted May 10, 2014 (edited) Use the API to your advantage, they have filters for a reason. 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 May 10, 2014 by Eliot Link to comment Share on other sites More sharing options...
Botre Posted May 10, 2014 Author Share Posted May 10, 2014 (edited) 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 May 10, 2014 by Botrepreneur Link to comment Share on other sites More sharing options...
Eliot Posted May 10, 2014 Share Posted May 10, 2014 npcs.closest() doesn't reliably return the closest NPC in terms of distance. 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. 1 Link to comment Share on other sites More sharing options...
Botre Posted May 10, 2014 Author Share Posted May 10, 2014 (edited) 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 May 10, 2014 by Botrepreneur 1 Link to comment Share on other sites More sharing options...
Kenneh Posted May 10, 2014 Share Posted May 10, 2014 Of course I tried that one first >< But... "Can only iterate over an array or an instance of java.lang.Iterable" 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() Link to comment Share on other sites More sharing options...
Botre Posted May 10, 2014 Author Share Posted May 10, 2014 (edited) 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" Edited May 10, 2014 by Botrepreneur Link to comment Share on other sites More sharing options...
Kenneh Posted May 10, 2014 Share Posted May 10, 2014 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" 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; } 1 Link to comment Share on other sites More sharing options...