Jump to content

My TreeFilter


Botre

Recommended Posts

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;
	}
	
}
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

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