Jump to content

My TreeFilter


Recommended Posts

Posted

I wrote this after realizing that calling @getMap().canReach(...) is really expensive.

package org.bjornkrols.events;

import java.util.HashMap;
import java.util.Map;

import org.bjornkrols.script.BotreMethodProvider;
import org.osbot.rs07.api.filter.Filter;
import org.osbot.rs07.api.map.Position;
import org.osbot.rs07.api.model.RS2Object;
import org.osbot.rs07.script.Script;

/**
 * @author 		Bjorn Krols (Botre)
 * @version		0.2
 * @since		March 26, 2015
 */

public class TreeFilter implements Filter<RS2Object> {
	
	/**
	 * The script instance.
	 */
	private final Script script;
	
	/**
	 * The tree's name.
	 */
	private String name;
	
	/**
	 * The chop radius's center.
	 */
	private Position chopCenter;
	
	/**
	 * The chop radius.
	 */
	private int chopRadius;
	
	/**
	 * <Position, reachable && in range>
	 */
	private Map<Position, Boolean> validityMap;
	
	public TreeFilter(final Script script) {
		this.script = script;
		validityMap = new HashMap<Position, Boolean>();
	}
	
	@Override
	public boolean match(RS2Object o) {
		if (!BotreMethodProvider.isValid(o))  	return false;
		if (!o.hasAction("Chop down")) 		return false;
		if (!o.getName().equals(name))		return false;
		Position p = o.getPosition();
		if (validityMap.containsKey(p))		return validityMap.get(p);

		validityMap.put(p, chopCenter.distance(o) < chopRadius && script.getMap().canReach(o));

		return validityMap.get(p);
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Position getChopCenter() {
		return chopCenter;
	}

	public void setChopCenter(Position chopCenter) {
		this.chopCenter = chopCenter;
	}

	public int getChopRadius() {
		return chopRadius;
	}

	public void setChopRadius(int chopRadius) {
		this.chopRadius = chopRadius;
	}
	
}

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

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