Jump to content

Closest object for an area/point in Osbot2?


Recommended Posts

Posted

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.

Posted

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;
	}
  • Like 1
Posted (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 by Eliot
  • Like 3
Posted
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.

Posted

This works great for me. 10/10 would use again. smile.png

 

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 ph34r.png

Posted

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 tongue.png

 

But yeah, just annoys me ph34r.png

I use the same bank booth and click on the same trees, so I'm glad my script tends to do the same.

Posted

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 <3

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

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