adc Posted June 26, 2014 Posted June 26, 2014 Perhaps I've overlooked something obvious, but despite scouring the Osbot2 API I can't find a method to return an object that is specifically in an area (at a point would work just as well), as closestObjectForName(Area, String) did in Osbot1. Of course, I suppose I could write my own method to do this, but I'd rather be able to use a built-in method if one exists.
Botre Posted June 26, 2014 Posted June 26, 2014 This should do the job ;) public static RS2Object getClosest(Script script, String name, String action, Integer minDistance, Integer maxDistance, Area mandatoryArea, Area illegalArea, Position mandatoryPosition, Position illegalPosition, boolean mustBeReachable) { RS2Object closestObject = null; double lowestDistance = Double.MAX_VALUE; List<RS2Object> objectList = script.objects.getAll(); int size = objectList.size(); int index = 0; for (RS2Object object : objectList) { index++; if (object == null) { continue; } if (!object.exists()) { continue; } if (!object.getName().toLowerCase().equalsIgnoreCase(name)) { continue; } if (object.getDefinition() == null) { continue; } if (!Arrays.asList(object.getDefinition().getActions()).contains(action)) { continue; } if (minDistance != null && object.getPosition().distance(script.myPosition()) < minDistance) { continue; } if (maxDistance != null && object.getPosition().distance(script.myPosition()) > maxDistance) { continue; } if (mandatoryArea != null && !mandatoryArea.contains(object.getPosition())) { continue; } if (illegalArea != null && illegalArea.contains(object.getPosition())) { continue; } if (mandatoryPosition != null && !object.getPosition().equals(mandatoryPosition)) { continue; } if (illegalPosition != null && object.getPosition().equals(illegalPosition)) { continue; } if (mustBeReachable && !script.map.canReach(object)) { continue; } if (object.getPosition().distance(script.myPosition()) < lowestDistance) { closestObject = object; lowestDistance = object.getPosition().distance(script.myPosition()); } if (object.getPosition().distance(script.myPosition()) == lowestDistance) { if (new Random().nextInt(size - index + 1) == 0) { closestObject = object; lowestDistance = object.getPosition().distance(script.myPosition()); } } } return closestObject; } 1
Eliot Posted June 26, 2014 Posted June 26, 2014 (edited) script.objects.closest(new BestMatch(script, name, area)); public class BestMatch implements Filter<RS2Object> { Script script; String name; Area area; public BestMatch(Script script, String name, Area area) { this.script = script; this.name = name; this.area = area; } @Override public boolean match(RS2Object o) { return o.getName().equals(name) && area.contains(o) && script.map.canReach(o); } } Edited June 26, 2014 by Eliot 3
Botre Posted June 26, 2014 Posted June 26, 2014 script.objects.closest(new BestMatch(script, name, area)); public class BestMatch implements Filter<RS2Object> { Script script; String name; Area area; public BestMatch(Script script, String name, Area area) { this.script = script; this.name = name; this.area = area; } @Override public boolean match(RS2Object o) { return o.getName().equals(this.name) && area.contains(o) && script.map.canReach(o); } } objects.closest seems to always return the first loaded object when multiple objects are the closest (have the same distance), I hate that.
Eliot Posted June 26, 2014 Posted June 26, 2014 objects.closest seems to always return the first loaded object when multiple objects are the closest (have the same distance), I hate that. This works great for me. 10/10 would use again.
Alek Posted June 26, 2014 Posted June 26, 2014 I'm creating API for this exactly in OSBot 2 as we speak. 1
Botre Posted June 26, 2014 Posted June 26, 2014 This works great for me. 10/10 would use again. I have no doubts about it's functionality ^^ But it's the reason why most people botting are standing at the same bankbooths for example, or will choose the same tree over and over again when standing in between two trees. Not sure if Jagex tracks such behavior, I doubt it actually :p But yeah, just annoys me
Eliot Posted June 26, 2014 Posted June 26, 2014 I have no doubts about it's functionality ^^ But it's the reason why most people botting are standing at the same bankbooths for example, or will choose the same tree over and over again when standing in between two trees. Not sure if Jagex tracks such behavior, I doubt it actually But yeah, just annoys me I use the same bank booth and click on the same trees, so I'm glad my script tends to do the same.
Botre Posted June 26, 2014 Posted June 26, 2014 I use the same bank booth and click on the same trees, so I'm glad my script tends to do the same. If 50 bots stand in between two trees and all of them pick the right one over the left one then there's nothing glad about that :p Ah well, I'll keep the left one for myself I guess (a)
adc Posted June 26, 2014 Author Posted June 26, 2014 I'll definitely use this for my current script, as the object I need to find is a single door in a set location; Also, this just taught me how to use filters, so thanks for that. However, your method will be very helpful once I move on to the Barrows script I have planned 2